|
| 1 | +import type OpenAI from "openai" |
| 2 | +import { getMcpServerTools } from "../mcp_server" |
| 3 | +import type { McpHub } from "../../../../../services/mcp/McpHub" |
| 4 | +import type { McpServer, McpTool } from "../../../../../shared/mcp" |
| 5 | + |
| 6 | +// Helper type to access function tools |
| 7 | +type FunctionTool = OpenAI.Chat.ChatCompletionTool & { type: "function" } |
| 8 | + |
| 9 | +// Helper to get the function property from a tool |
| 10 | +const getFunction = (tool: OpenAI.Chat.ChatCompletionTool) => (tool as FunctionTool).function |
| 11 | + |
| 12 | +describe("getMcpServerTools", () => { |
| 13 | + const createMockTool = (name: string, description = "Test tool"): McpTool => ({ |
| 14 | + name, |
| 15 | + description, |
| 16 | + inputSchema: { |
| 17 | + type: "object", |
| 18 | + properties: {}, |
| 19 | + }, |
| 20 | + }) |
| 21 | + |
| 22 | + const createMockServer = (name: string, tools: McpTool[], source: "global" | "project" = "global"): McpServer => ({ |
| 23 | + name, |
| 24 | + config: JSON.stringify({ type: "stdio", command: "test" }), |
| 25 | + status: "connected", |
| 26 | + source, |
| 27 | + tools, |
| 28 | + }) |
| 29 | + |
| 30 | + const createMockMcpHub = (servers: McpServer[]): Partial<McpHub> => ({ |
| 31 | + getServers: vi.fn().mockReturnValue(servers), |
| 32 | + }) |
| 33 | + |
| 34 | + it("should return empty array when mcpHub is undefined", () => { |
| 35 | + const result = getMcpServerTools(undefined) |
| 36 | + expect(result).toEqual([]) |
| 37 | + }) |
| 38 | + |
| 39 | + it("should return empty array when no servers are available", () => { |
| 40 | + const mockHub = createMockMcpHub([]) |
| 41 | + const result = getMcpServerTools(mockHub as McpHub) |
| 42 | + expect(result).toEqual([]) |
| 43 | + }) |
| 44 | + |
| 45 | + it("should generate tool definitions for server tools", () => { |
| 46 | + const server = createMockServer("testServer", [createMockTool("testTool")]) |
| 47 | + const mockHub = createMockMcpHub([server]) |
| 48 | + |
| 49 | + const result = getMcpServerTools(mockHub as McpHub) |
| 50 | + |
| 51 | + expect(result).toHaveLength(1) |
| 52 | + expect(result[0].type).toBe("function") |
| 53 | + expect(getFunction(result[0]).name).toBe("mcp--testServer--testTool") |
| 54 | + expect(getFunction(result[0]).description).toBe("Test tool") |
| 55 | + }) |
| 56 | + |
| 57 | + it("should filter out tools with enabledForPrompt set to false", () => { |
| 58 | + const enabledTool = createMockTool("enabledTool") |
| 59 | + const disabledTool = { ...createMockTool("disabledTool"), enabledForPrompt: false } |
| 60 | + const server = createMockServer("testServer", [enabledTool, disabledTool]) |
| 61 | + const mockHub = createMockMcpHub([server]) |
| 62 | + |
| 63 | + const result = getMcpServerTools(mockHub as McpHub) |
| 64 | + |
| 65 | + expect(result).toHaveLength(1) |
| 66 | + expect(getFunction(result[0]).name).toBe("mcp--testServer--enabledTool") |
| 67 | + }) |
| 68 | + |
| 69 | + it("should deduplicate tools when same server exists in both global and project configs", () => { |
| 70 | + const globalServer = createMockServer( |
| 71 | + "context7", |
| 72 | + [createMockTool("resolve-library-id", "Global description")], |
| 73 | + "global", |
| 74 | + ) |
| 75 | + const projectServer = createMockServer( |
| 76 | + "context7", |
| 77 | + [createMockTool("resolve-library-id", "Project description")], |
| 78 | + "project", |
| 79 | + ) |
| 80 | + |
| 81 | + // McpHub.getServers() deduplicates with project servers taking priority |
| 82 | + // This test simulates the deduplicated result (only project server returned) |
| 83 | + const mockHub = createMockMcpHub([projectServer]) |
| 84 | + |
| 85 | + const result = getMcpServerTools(mockHub as McpHub) |
| 86 | + |
| 87 | + // Should only have one tool (from project server) |
| 88 | + expect(result).toHaveLength(1) |
| 89 | + expect(getFunction(result[0]).name).toBe("mcp--context7--resolve-library-id") |
| 90 | + // Project server takes priority |
| 91 | + expect(getFunction(result[0]).description).toBe("Project description") |
| 92 | + }) |
| 93 | + |
| 94 | + it("should allow tools with different names from the same server", () => { |
| 95 | + const server = createMockServer("testServer", [ |
| 96 | + createMockTool("tool1"), |
| 97 | + createMockTool("tool2"), |
| 98 | + createMockTool("tool3"), |
| 99 | + ]) |
| 100 | + const mockHub = createMockMcpHub([server]) |
| 101 | + |
| 102 | + const result = getMcpServerTools(mockHub as McpHub) |
| 103 | + |
| 104 | + expect(result).toHaveLength(3) |
| 105 | + const toolNames = result.map((t) => getFunction(t).name) |
| 106 | + expect(toolNames).toContain("mcp--testServer--tool1") |
| 107 | + expect(toolNames).toContain("mcp--testServer--tool2") |
| 108 | + expect(toolNames).toContain("mcp--testServer--tool3") |
| 109 | + }) |
| 110 | + |
| 111 | + it("should allow tools with same name from different servers", () => { |
| 112 | + const server1 = createMockServer("server1", [createMockTool("commonTool")]) |
| 113 | + const server2 = createMockServer("server2", [createMockTool("commonTool")]) |
| 114 | + const mockHub = createMockMcpHub([server1, server2]) |
| 115 | + |
| 116 | + const result = getMcpServerTools(mockHub as McpHub) |
| 117 | + |
| 118 | + expect(result).toHaveLength(2) |
| 119 | + const toolNames = result.map((t) => getFunction(t).name) |
| 120 | + expect(toolNames).toContain("mcp--server1--commonTool") |
| 121 | + expect(toolNames).toContain("mcp--server2--commonTool") |
| 122 | + }) |
| 123 | + |
| 124 | + it("should skip servers without tools", () => { |
| 125 | + const serverWithTools = createMockServer("withTools", [createMockTool("tool1")]) |
| 126 | + const serverWithoutTools = createMockServer("withoutTools", []) |
| 127 | + const serverWithUndefinedTools: McpServer = { |
| 128 | + ...createMockServer("undefinedTools", []), |
| 129 | + tools: undefined, |
| 130 | + } |
| 131 | + const mockHub = createMockMcpHub([serverWithTools, serverWithoutTools, serverWithUndefinedTools]) |
| 132 | + |
| 133 | + const result = getMcpServerTools(mockHub as McpHub) |
| 134 | + |
| 135 | + expect(result).toHaveLength(1) |
| 136 | + expect(getFunction(result[0]).name).toBe("mcp--withTools--tool1") |
| 137 | + }) |
| 138 | + |
| 139 | + it("should include required fields from tool schema", () => { |
| 140 | + const toolWithRequired: McpTool = { |
| 141 | + name: "toolWithRequired", |
| 142 | + description: "Tool with required fields", |
| 143 | + inputSchema: { |
| 144 | + type: "object", |
| 145 | + properties: { |
| 146 | + requiredField: { type: "string" }, |
| 147 | + optionalField: { type: "number" }, |
| 148 | + }, |
| 149 | + required: ["requiredField"], |
| 150 | + }, |
| 151 | + } |
| 152 | + const server = createMockServer("testServer", [toolWithRequired]) |
| 153 | + const mockHub = createMockMcpHub([server]) |
| 154 | + |
| 155 | + const result = getMcpServerTools(mockHub as McpHub) |
| 156 | + |
| 157 | + expect(result).toHaveLength(1) |
| 158 | + expect(getFunction(result[0]).parameters).toEqual({ |
| 159 | + type: "object", |
| 160 | + properties: { |
| 161 | + requiredField: { type: "string" }, |
| 162 | + optionalField: { type: "number" }, |
| 163 | + }, |
| 164 | + additionalProperties: false, |
| 165 | + required: ["requiredField"], |
| 166 | + }) |
| 167 | + }) |
| 168 | + |
| 169 | + it("should not include required field when schema has no required fields", () => { |
| 170 | + const toolWithoutRequired: McpTool = { |
| 171 | + name: "toolWithoutRequired", |
| 172 | + description: "Tool without required fields", |
| 173 | + inputSchema: { |
| 174 | + type: "object", |
| 175 | + properties: { |
| 176 | + optionalField: { type: "string" }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + } |
| 180 | + const server = createMockServer("testServer", [toolWithoutRequired]) |
| 181 | + const mockHub = createMockMcpHub([server]) |
| 182 | + |
| 183 | + const result = getMcpServerTools(mockHub as McpHub) |
| 184 | + |
| 185 | + expect(result).toHaveLength(1) |
| 186 | + expect(getFunction(result[0]).parameters).toEqual({ |
| 187 | + type: "object", |
| 188 | + properties: { |
| 189 | + optionalField: { type: "string" }, |
| 190 | + }, |
| 191 | + additionalProperties: false, |
| 192 | + }) |
| 193 | + expect(getFunction(result[0]).parameters).not.toHaveProperty("required") |
| 194 | + }) |
| 195 | +}) |
0 commit comments