diff options
| author | Taylor Mullen <[email protected]> | 2025-05-28 00:43:23 -0700 |
|---|---|---|
| committer | N. Taylor Mullen <[email protected]> | 2025-05-28 17:28:45 -0700 |
| commit | d74c0f581bf5ba0c74a7b7874f6638db6897f907 (patch) | |
| tree | 77165172f69a1b8d718b93b74967c726b64a17a0 /packages/server/src/tools/mcp-tool.ts | |
| parent | c413988ae00f4a01003748c5e6fbf511da07ab06 (diff) | |
refactor: Extract MCP discovery from ToolRegistry
- Moves MCP tool discovery logic from ToolRegistry into a new, dedicated MCP client (mcp-client.ts and mcp-tool.ts).
- Updates ToolRegistry to utilize the new MCP client.
- Adds comprehensive tests for the new MCP client and its integration with ToolRegistry.
Part of https://github.com/google-gemini/gemini-cli/issues/577
Diffstat (limited to 'packages/server/src/tools/mcp-tool.ts')
| -rw-r--r-- | packages/server/src/tools/mcp-tool.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/packages/server/src/tools/mcp-tool.ts b/packages/server/src/tools/mcp-tool.ts new file mode 100644 index 00000000..05ad750c --- /dev/null +++ b/packages/server/src/tools/mcp-tool.ts @@ -0,0 +1,49 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; +import { BaseTool, ToolResult } from './tools.js'; + +type ToolParams = Record<string, unknown>; + +export const MCP_TOOL_DEFAULT_TIMEOUT_MSEC = 10 * 60 * 1000; // default to 10 minutes + +export class DiscoveredMCPTool extends BaseTool<ToolParams, ToolResult> { + constructor( + private readonly mcpClient: Client, + readonly name: string, + readonly description: string, + readonly parameterSchema: Record<string, unknown>, + readonly serverToolName: string, + readonly timeout?: number, + ) { + description += ` + +This MCP tool was discovered from a local MCP server using JSON RPC 2.0 over stdio transport protocol. +When called, this tool will invoke the \`tools/call\` method for tool name \`${name}\`. +MCP servers can be configured in project or user settings. +Returns the MCP server response as a json string. +`; + super(name, name, description, parameterSchema); + } + + async execute(params: ToolParams): Promise<ToolResult> { + const result = await this.mcpClient.callTool( + { + name: this.serverToolName, + arguments: params, + }, + undefined, // skip resultSchema to specify options (RequestOptions) + { + timeout: this.timeout ?? MCP_TOOL_DEFAULT_TIMEOUT_MSEC, + }, + ); + return { + llmContent: JSON.stringify(result, null, 2), + returnDisplay: JSON.stringify(result, null, 2), + }; + } +} |
