From 0aef1e60e9760f6f48c02abc1513cff80bed01a8 Mon Sep 17 00:00:00 2001 From: Allen Zhou <46854522+allenzhou101@users.noreply.github.com> Date: Mon, 17 Feb 2025 17:01:28 -0800 Subject: [PATCH] Only list tools on initialization --- mcp-client-typescript/index.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/mcp-client-typescript/index.ts b/mcp-client-typescript/index.ts index 1f8fb1a..140cc3d 100644 --- a/mcp-client-typescript/index.ts +++ b/mcp-client-typescript/index.ts @@ -21,6 +21,7 @@ class MCPClient { private mcp: Client; private anthropic: Anthropic; private transport: StdioClientTransport | null = null; + private tools: Tool[] = []; constructor() { // Initialize Anthropic client and MCP client @@ -58,7 +59,7 @@ class MCPClient { // List available tools const toolsResult = await this.mcp.listTools(); - const tools = toolsResult.tools.map((tool) => { + this.tools = toolsResult.tools.map((tool) => { return { name: tool.name, description: tool.description, @@ -67,7 +68,7 @@ class MCPClient { }); console.log( "Connected to server with tools:", - tools.map(({ name }) => name) + this.tools.map(({ name }) => name) ); } catch (e) { console.log("Failed to connect to MCP server: ", e); @@ -89,22 +90,14 @@ class MCPClient { }, ]; - // Get available tools - const toolsResult = await this.mcp.listTools(); - const tools: Tool[] = toolsResult.tools.map((tool) => { - return { - name: tool.name, - description: tool.description, - input_schema: tool.inputSchema, }; - }); // Initial Claude API call const response = await this.anthropic.messages.create({ model: "claude-3-5-sonnet-20241022", max_tokens: 1000, messages, - tools, + tools: this.tools, }); // Process response and handle tool calls