Learn how to connect to local and remote MCP servers
The MCPClientManager is your gateway to MCP servers. It handles connections, manages multiple servers, and provides a unified interface for calling tools, reading resources, and accessing prompts.
exchanges the refresh token for an access token during connection
automatically retries with a fresh access token when the server returns an auth challenge
stores rotated refresh tokens returned by the authorization server
refreshToken is only supported for HTTP servers. When you use it, do not also set accessToken, authProvider, or an Authorization header in requestInit.headers.
After configuring servers, you must explicitly connect:
Copy
Ask AI
// Connect to a specific serverawait manager.connectToServer("myServer");// Now you can use itconst tools = await manager.listTools("myServer");console.log("Available tools:", tools.tools.map((tool) => tool.name));// Always disconnect when doneawait manager.disconnectServer("myServer");
Always disconnect servers when you’re done, especially in tests. This cleans up subprocess handles and network connections.
The most common pattern is connecting servers, then creating a TestAgent with their tools:
Copy
Ask AI
const manager = new MCPClientManager({ myServer: { command: "node", args: ["./server.js"] },});await manager.connectToServer("myServer");// Get tools for TestAgentconst tools = await manager.getTools();// Create agent with those toolsconst agent = new TestAgent({ tools, model: "anthropic/claude-sonnet-4-20250514", apiKey: process.env.ANTHROPIC_API_KEY,});// Now prompt the agentconst result = await agent.prompt("Add 2 and 3");
Connection failures throw errors. Wrap in try/catch for production code:
Copy
Ask AI
try { await manager.connectToServer("myServer");} catch (error) { console.error("Failed to connect:", error.message); // Handle gracefully - maybe fall back to another server}