summaryrefslogtreecommitdiff
path: root/packages/core/src/tools/mcp-client.ts
diff options
context:
space:
mode:
authorOlcan <[email protected]>2025-06-08 21:52:11 -0700
committerGitHub <[email protected]>2025-06-08 21:52:11 -0700
commita2fee6bdd3927a2a08520ab87dabc8fecdb6075b (patch)
tree90f80f532a6faa524ef10ab9e38aabc5731a3684 /packages/core/src/tools/mcp-client.ts
parenta3d11e8fef23f466abec7a8a5f400cbc30a6c5fb (diff)
fix mcp timeouts and missing description on mcp errors (#868)
Diffstat (limited to 'packages/core/src/tools/mcp-client.ts')
-rw-r--r--packages/core/src/tools/mcp-client.ts21
1 files changed, 18 insertions, 3 deletions
diff --git a/packages/core/src/tools/mcp-client.ts b/packages/core/src/tools/mcp-client.ts
index 9a02df0c..1b797ba4 100644
--- a/packages/core/src/tools/mcp-client.ts
+++ b/packages/core/src/tools/mcp-client.ts
@@ -13,6 +13,8 @@ import { DiscoveredMCPTool } from './mcp-tool.js';
import { CallableTool, FunctionDeclaration, mcpToTool } from '@google/genai';
import { ToolRegistry } from './tool-registry.js';
+export const MCP_DEFAULT_TIMEOUT_MSEC = 10 * 60 * 1000; // default to 10 minutes
+
/**
* Enum representing the connection status of an MCP server
*/
@@ -149,11 +151,24 @@ async function connectAndDiscover(
const mcpClient = new Client({
name: 'gemini-cli-mcp-client',
version: '0.0.1',
- timeout: mcpServerConfig.timeout,
});
+ // patch Client.callTool to use request timeout as genai McpCallTool.callTool does not do it
+ // TODO: remove this hack once GenAI SDK does callTool with request options
+ if ('callTool' in mcpClient) {
+ const origCallTool = mcpClient.callTool.bind(mcpClient);
+ mcpClient.callTool = function (params, resultSchema, options) {
+ return origCallTool(params, resultSchema, {
+ ...options,
+ timeout: mcpServerConfig.timeout ?? MCP_DEFAULT_TIMEOUT_MSEC,
+ });
+ };
+ }
+
try {
- await mcpClient.connect(transport);
+ await mcpClient.connect(transport, {
+ timeout: mcpServerConfig.timeout ?? MCP_DEFAULT_TIMEOUT_MSEC,
+ });
// Connection successful
updateMCPServerStatus(mcpServerName, MCPServerStatus.CONNECTED);
} catch (error) {
@@ -242,7 +257,7 @@ async function connectAndDiscover(
funcDecl.description ?? '',
parameterSchema,
funcDecl.name,
- mcpServerConfig.timeout,
+ mcpServerConfig.timeout ?? MCP_DEFAULT_TIMEOUT_MSEC,
mcpServerConfig.trust,
),
);