Skip to content

Commit 1aaf641

Browse files
feat - incorporated mcp server request configs for cli mode
1 parent 3b090d0 commit 1aaf641

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

cli/src/client/connection.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
22
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
33
import { McpResponse } from "./types.js";
4+
import { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js";
45

56
export const validLogLevels = [
67
"trace",
@@ -15,9 +16,10 @@ export type LogLevel = (typeof validLogLevels)[number];
1516
export async function connect(
1617
client: Client,
1718
transport: Transport,
19+
options: RequestOptions,
1820
): Promise<void> {
1921
try {
20-
await client.connect(transport);
22+
await client.connect(transport, options);
2123
} catch (error) {
2224
throw new Error(
2325
`Failed to connect to MCP server: ${error instanceof Error ? error.message : String(error)}`,

cli/src/index.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from "./client/index.js";
2020
import { handleError } from "./error-handler.js";
2121
import { createTransport, TransportOptions } from "./transport.js";
22+
import { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js";
2223

2324
type Args = {
2425
target: string[];
@@ -29,6 +30,9 @@ type Args = {
2930
logLevel?: LogLevel;
3031
toolName?: string;
3132
toolArg?: Record<string, string>;
33+
requestTimeout?: number;
34+
resetTimeoutOnProgress?: boolean;
35+
maxTotalTimeout?: number;
3236
};
3337

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

6973
try {
70-
await connect(client, transport);
74+
const mcpRequestOptions: RequestOptions = {
75+
timeout: args?.requestTimeout,
76+
resetTimeoutOnProgress: args?.resetTimeoutOnProgress,
77+
maxTotalTimeout: args?.maxTotalTimeout,
78+
};
79+
80+
await connect(client, transport, mcpRequestOptions);
7181

7282
let result: McpResponse;
7383

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

164+
function parseStringToNumber(value: string): number {
165+
const parsedValue = parseInt(value, 10);
166+
if (isNaN(parsedValue)) {
167+
throw new Error(`Invalid parameter format: ${value}. Use a number format.`);
168+
}
169+
return parsedValue;
170+
}
171+
172+
function parseStringToBoolean(value: string): boolean {
173+
if (value === "true") {
174+
return true;
175+
} else if (value === "false") {
176+
return false;
177+
} else {
178+
throw new Error(`Invalid parameter format: ${value}. Use true or false.`);
179+
}
180+
}
181+
154182
function parseArgs(): Args {
155183
const program = new Command();
156184

@@ -214,6 +242,24 @@ function parseArgs(): Args {
214242

215243
return value as LogLevel;
216244
},
245+
)
246+
.option(
247+
"--request-timeout <number>",
248+
"Timeout for requests to the MCP server (ms)",
249+
parseStringToNumber,
250+
10000,
251+
)
252+
.option(
253+
"--reset-timeout-on-progress <boolean>",
254+
"Reset timeout on progress notifications",
255+
parseStringToBoolean,
256+
true,
257+
)
258+
.option(
259+
"--max-total-timeout <number>",
260+
"Maximum total timeout for requests sent to the MCP server (ms) (Use with progress notifications)",
261+
parseStringToNumber,
262+
60000,
217263
);
218264

219265
// Parse only the arguments before --

0 commit comments

Comments
 (0)