Only list tools on initialization

This commit is contained in:
Allen Zhou 2025-02-17 17:01:28 -08:00
commit 0aef1e60e9

View file

@ -21,6 +21,7 @@ class MCPClient {
private mcp: Client; private mcp: Client;
private anthropic: Anthropic; private anthropic: Anthropic;
private transport: StdioClientTransport | null = null; private transport: StdioClientTransport | null = null;
private tools: Tool[] = [];
constructor() { constructor() {
// Initialize Anthropic client and MCP client // Initialize Anthropic client and MCP client
@ -58,7 +59,7 @@ class MCPClient {
// List available tools // List available tools
const toolsResult = await this.mcp.listTools(); const toolsResult = await this.mcp.listTools();
const tools = toolsResult.tools.map((tool) => { this.tools = toolsResult.tools.map((tool) => {
return { return {
name: tool.name, name: tool.name,
description: tool.description, description: tool.description,
@ -67,7 +68,7 @@ class MCPClient {
}); });
console.log( console.log(
"Connected to server with tools:", "Connected to server with tools:",
tools.map(({ name }) => name) this.tools.map(({ name }) => name)
); );
} catch (e) { } catch (e) {
console.log("Failed to connect to MCP server: ", 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 // Initial Claude API call
const response = await this.anthropic.messages.create({ const response = await this.anthropic.messages.create({
model: "claude-3-5-sonnet-20241022", model: "claude-3-5-sonnet-20241022",
max_tokens: 1000, max_tokens: 1000,
messages, messages,
tools, tools: this.tools,
}); });
// Process response and handle tool calls // Process response and handle tool calls