Skip to content

Commit 0aef1e6

Browse files
committed
Only list tools on initialization
1 parent e065b11 commit 0aef1e6

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

mcp-client-typescript/index.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class MCPClient {
2121
private mcp: Client;
2222
private anthropic: Anthropic;
2323
private transport: StdioClientTransport | null = null;
24+
private tools: Tool[] = [];
2425

2526
constructor() {
2627
// Initialize Anthropic client and MCP client
@@ -58,7 +59,7 @@ class MCPClient {
5859

5960
// List available tools
6061
const toolsResult = await this.mcp.listTools();
61-
const tools = toolsResult.tools.map((tool) => {
62+
this.tools = toolsResult.tools.map((tool) => {
6263
return {
6364
name: tool.name,
6465
description: tool.description,
@@ -67,7 +68,7 @@ class MCPClient {
6768
});
6869
console.log(
6970
"Connected to server with tools:",
70-
tools.map(({ name }) => name)
71+
this.tools.map(({ name }) => name)
7172
);
7273
} catch (e) {
7374
console.log("Failed to connect to MCP server: ", e);
@@ -89,22 +90,14 @@ class MCPClient {
8990
},
9091
];
9192

92-
// Get available tools
93-
const toolsResult = await this.mcp.listTools();
94-
const tools: Tool[] = toolsResult.tools.map((tool) => {
95-
return {
96-
name: tool.name,
97-
description: tool.description,
98-
input_schema: tool.inputSchema,
9993
};
100-
});
10194

10295
// Initial Claude API call
10396
const response = await this.anthropic.messages.create({
10497
model: "claude-3-5-sonnet-20241022",
10598
max_tokens: 1000,
10699
messages,
107-
tools,
100+
tools: this.tools,
108101
});
109102

110103
// Process response and handle tool calls

0 commit comments

Comments
 (0)