Skip to content
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
17 changes: 13 additions & 4 deletions packages/mcp/src/mcp_communication_protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,22 @@ export class McpCommunicationProtocol implements CommunicationProtocol {
return mcpClient;
}

/**
* Returns the configured timeout for an MCP server in milliseconds.
* Defaults to 30 seconds when not specified.
*/
private _getTimeoutMs(serverConfig: McpServerConfig): number {
return (serverConfig.timeout ?? 30) * 1000;
}

private async _withSession<T>(
serverName: string,
serverConfig: McpServerConfig,
auth: OAuth2Auth | undefined,
operation: (client: McpClient) => Promise<T>
): Promise<T> {
const sessionKey = `${serverName}:${serverConfig.transport}`;
// Use configured timeout (in seconds) or default to 30s
const timeoutMs = (serverConfig.timeout ?? 30) * 1000;
const timeoutMs = this._getTimeoutMs(serverConfig);
try {
const client = await this._getOrCreateSession(serverName, serverConfig, auth);
return await Promise.race([
Expand Down Expand Up @@ -211,8 +218,9 @@ export class McpCommunicationProtocol implements CommunicationProtocol {
for (const [serverName, serverConfig] of Object.entries(mcpCallTemplate.config.mcpServers)) {
try {
this._logInfo(`Discovering tools from MCP server '${serverName}'...`);
const requestTimeout = this._getTimeoutMs(serverConfig);
const mcpToolsResult = await this._withSession(serverName, serverConfig, mcpCallTemplate.auth,
(client) => client.listTools()
(client) => client.listTools(undefined, { timeout: requestTimeout })
);
Comment on lines +221 to 224
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New behavior forwards a per-request timeout into the MCP SDK (listTools/callTool options). This is the core fix but isn’t covered by the existing MCP protocol tests. Please add a unit test that asserts the SDK client receives the expected timeout option (e.g., by monkeypatching _getOrCreateSession to return a fake client and asserting the passed options), so regressions don’t silently reintroduce the 60s default timeout issue.

Copilot uses AI. Check for mistakes.

if (!isMcpToolsResponse(mcpToolsResult)) {
Expand Down Expand Up @@ -292,8 +300,9 @@ export class McpCommunicationProtocol implements CommunicationProtocol {
}

this._logInfo(`Calling tool '${actualToolName}' on MCP server '${serverName}'...`);
const requestTimeout = this._getTimeoutMs(serverConfig);
const result = await this._withSession(serverName, serverConfig, mcpCallTemplate.auth,
(client) => client.callTool({ name: actualToolName, arguments: toolArgs })
(client) => client.callTool({ name: actualToolName, arguments: toolArgs }, undefined, { timeout: requestTimeout })
);
Comment on lines +303 to 306
Copy link

Copilot AI Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as listTools, callTool is now relying on passing a per-request timeout into the MCP SDK. Please add a unit test that verifies the timeout option is forwarded for tool execution as well (ideally without a long-running real server, by using a fake McpClient and asserting the arguments).

Copilot uses AI. Check for mistakes.

return this._processMcpToolResult(result);
Expand Down
32 changes: 32 additions & 0 deletions packages/mcp/tests/mcp_communication_protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,36 @@ describe("McpCommunicationProtocol", () => {
).rejects.toThrow("Configuration for MCP server 'unknown_server' not found in manual 'mock_http_manual'.");
}, 10000);
});

describe("Timeout forwarding", () => {
test("forwards configured timeout to listTools and callTool", async () => {
const capturedOpts: any[] = [];
const fakeClient = {
listTools: (_params: any, opts: any) => {
capturedOpts.push({ method: "listTools", opts });
return Promise.resolve({ tools: [{ name: "t", description: "", inputSchema: {}, outputSchema: {} }] });
},
callTool: (_params: any, _result: any, opts: any) => {
capturedOpts.push({ method: "callTool", opts });
return Promise.resolve({ content: [{ type: "text", text: "ok" }] });
},
};

const protocol = new McpCommunicationProtocol();
(protocol as any)._getOrCreateSession = () => Promise.resolve(fakeClient);

const template: McpCallTemplate = {
name: "m",
call_template_type: "mcp",
config: { mcpServers: { s: { transport: "stdio" as const, command: "true", timeout: 90 } } },
};

await protocol.registerManual(mockClient, template);
await protocol.callTool(mockClient, "s.t", {}, template);

expect(capturedOpts).toHaveLength(2);
expect(capturedOpts[0]).toEqual({ method: "listTools", opts: { timeout: 90_000 } });
expect(capturedOpts[1]).toEqual({ method: "callTool", opts: { timeout: 90_000 } });
});
});
});