|
1 | | -import { normalizeMCPToolParams } from "@/agent/mcp/client"; |
| 1 | +import { MCPClient, normalizeMCPToolParams } from "@/agent/mcp/client"; |
2 | 2 |
|
3 | 3 | describe("normalizeMCPToolParams", () => { |
4 | 4 | it("returns object inputs unchanged", () => { |
@@ -26,3 +26,105 @@ describe("normalizeMCPToolParams", () => { |
26 | 26 | ); |
27 | 27 | }); |
28 | 28 | }); |
| 29 | + |
| 30 | +describe("MCPClient.executeTool server selection", () => { |
| 31 | + function setServers( |
| 32 | + client: MCPClient, |
| 33 | + servers: Map< |
| 34 | + string, |
| 35 | + { |
| 36 | + tools: Map<string, unknown>; |
| 37 | + client: { callTool: jest.Mock }; |
| 38 | + } |
| 39 | + > |
| 40 | + ): void { |
| 41 | + ( |
| 42 | + client as unknown as { |
| 43 | + servers: Map< |
| 44 | + string, |
| 45 | + { |
| 46 | + tools: Map<string, unknown>; |
| 47 | + client: { callTool: jest.Mock }; |
| 48 | + } |
| 49 | + >; |
| 50 | + } |
| 51 | + ).servers = servers; |
| 52 | + } |
| 53 | + |
| 54 | + it("uses the only connected server when serverId is omitted", async () => { |
| 55 | + const mcpClient = new MCPClient(false); |
| 56 | + const callTool = jest.fn().mockResolvedValue({ content: [] }); |
| 57 | + setServers( |
| 58 | + mcpClient, |
| 59 | + new Map([ |
| 60 | + [ |
| 61 | + "server-1", |
| 62 | + { |
| 63 | + tools: new Map([["search", {}]]), |
| 64 | + client: { callTool }, |
| 65 | + }, |
| 66 | + ], |
| 67 | + ]) |
| 68 | + ); |
| 69 | + |
| 70 | + await mcpClient.executeTool("search", { query: "weather" }); |
| 71 | + |
| 72 | + expect(callTool).toHaveBeenCalledWith({ |
| 73 | + name: "search", |
| 74 | + arguments: { query: "weather" }, |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("finds matching server by tool name when multiple are connected", async () => { |
| 79 | + const mcpClient = new MCPClient(false); |
| 80 | + const searchCallTool = jest.fn().mockResolvedValue({ content: [] }); |
| 81 | + const notesCallTool = jest.fn().mockResolvedValue({ content: [] }); |
| 82 | + setServers( |
| 83 | + mcpClient, |
| 84 | + new Map([ |
| 85 | + [ |
| 86 | + "server-a", |
| 87 | + { |
| 88 | + tools: new Map([["notes", {}]]), |
| 89 | + client: { callTool: notesCallTool }, |
| 90 | + }, |
| 91 | + ], |
| 92 | + [ |
| 93 | + "server-b", |
| 94 | + { |
| 95 | + tools: new Map([["search", {}]]), |
| 96 | + client: { callTool: searchCallTool }, |
| 97 | + }, |
| 98 | + ], |
| 99 | + ]) |
| 100 | + ); |
| 101 | + |
| 102 | + await mcpClient.executeTool("search", { query: "coffee" }); |
| 103 | + |
| 104 | + expect(searchCallTool).toHaveBeenCalledWith({ |
| 105 | + name: "search", |
| 106 | + arguments: { query: "coffee" }, |
| 107 | + }); |
| 108 | + expect(notesCallTool).not.toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + |
| 111 | + it("throws when provided serverId does not exist", async () => { |
| 112 | + const mcpClient = new MCPClient(false); |
| 113 | + setServers( |
| 114 | + mcpClient, |
| 115 | + new Map([ |
| 116 | + [ |
| 117 | + "server-a", |
| 118 | + { |
| 119 | + tools: new Map([["notes", {}]]), |
| 120 | + client: { callTool: jest.fn() }, |
| 121 | + }, |
| 122 | + ], |
| 123 | + ]) |
| 124 | + ); |
| 125 | + |
| 126 | + await expect( |
| 127 | + mcpClient.executeTool("search", { query: "missing" }, "unknown-server") |
| 128 | + ).rejects.toThrow("No valid server found for tool search"); |
| 129 | + }); |
| 130 | +}); |
0 commit comments