Skip to content

feat - incorporated mcp server request configs for cli mode #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cli/src/client/connection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
import { McpResponse } from "./types.js";
import { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js";

export const validLogLevels = [
"trace",
Expand All @@ -15,9 +16,10 @@ export type LogLevel = (typeof validLogLevels)[number];
export async function connect(
client: Client,
transport: Transport,
options: RequestOptions,
): Promise<void> {
try {
await client.connect(transport);
await client.connect(transport, options);
} catch (error) {
throw new Error(
`Failed to connect to MCP server: ${error instanceof Error ? error.message : String(error)}`,
Expand Down
48 changes: 47 additions & 1 deletion cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from "./client/index.js";
import { handleError } from "./error-handler.js";
import { createTransport, TransportOptions } from "./transport.js";
import { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js";

type Args = {
target: string[];
Expand All @@ -29,6 +30,9 @@ type Args = {
logLevel?: LogLevel;
toolName?: string;
toolArg?: Record<string, string>;
requestTimeout?: number;
resetTimeoutOnProgress?: boolean;
maxTotalTimeout?: number;
};

function createTransportOptions(target: string[]): TransportOptions {
Expand Down Expand Up @@ -67,7 +71,13 @@ async function callMethod(args: Args): Promise<void> {
});

try {
await connect(client, transport);
const mcpRequestOptions: RequestOptions = {
timeout: args?.requestTimeout,
resetTimeoutOnProgress: args?.resetTimeoutOnProgress,
maxTotalTimeout: args?.maxTotalTimeout,
};

await connect(client, transport, mcpRequestOptions);

let result: McpResponse;

Expand Down Expand Up @@ -151,6 +161,24 @@ function parseKeyValuePair(
return { ...previous, [key as string]: val };
}

function parseStringToNumber(value: string): number {
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
throw new Error(`Invalid parameter format: ${value}. Use a number format.`);
}
return parsedValue;
}

function parseStringToBoolean(value: string): boolean {
if (value === "true") {
return true;
} else if (value === "false") {
return false;
} else {
throw new Error(`Invalid parameter format: ${value}. Use true or false.`);
}
}

function parseArgs(): Args {
const program = new Command();

Expand Down Expand Up @@ -214,6 +242,24 @@ function parseArgs(): Args {

return value as LogLevel;
},
)
.option(
"--request-timeout <number>",
"Timeout for requests to the MCP server (ms)",
parseStringToNumber,
10000,
)
.option(
"--reset-timeout-on-progress <boolean>",
"Reset timeout on progress notifications",
parseStringToBoolean,
true,
)
.option(
"--max-total-timeout <number>",
"Maximum total timeout for requests sent to the MCP server (ms) (Use with progress notifications)",
parseStringToNumber,
60000,
);

// Parse only the arguments before --
Expand Down