-
Notifications
You must be signed in to change notification settings - Fork 2.7k
fix: prevent duplicate MCP tools error by deduplicating servers at source #10096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
874770b
fix: deduplicate MCP tools to prevent API errors
daniel-lxs a8b6ac7
chore: add changeset
daniel-lxs ac462b2
docs: correct documentation about server priority order
daniel-lxs 51b88d1
fix: deduplicate MCP servers in getServers() with project priority
daniel-lxs c85bcc2
Delete .changeset/fix-duplicate-mcp-tools.md
daniel-lxs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "roo-cline": patch | ||
| --- | ||
|
|
||
| Fix duplicate MCP tools error when same server is defined in global and project configs | ||
|
|
||
| When the same MCP server (e.g., "context7") was defined in both global and project configs, the getMcpServerTools() function generated duplicate tool definitions with the same name, causing API errors like "The tool mcp--context7--resolve-library-id is already defined". | ||
|
|
||
| Added deduplication logic to getMcpServerTools() using a Set to track seen tool names. First occurrence wins (project servers take priority over global servers). | ||
|
|
||
| Fixes: https://roo-code.sentry.io/issues/7111443956/ |
194 changes: 194 additions & 0 deletions
194
src/core/prompts/tools/native-tools/__tests__/mcp_server.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import type OpenAI from "openai" | ||
| import { getMcpServerTools } from "../mcp_server" | ||
| import type { McpHub } from "../../../../../services/mcp/McpHub" | ||
| import type { McpServer, McpTool } from "../../../../../shared/mcp" | ||
|
|
||
| // Helper type to access function tools | ||
| type FunctionTool = OpenAI.Chat.ChatCompletionTool & { type: "function" } | ||
|
|
||
| // Helper to get the function property from a tool | ||
| const getFunction = (tool: OpenAI.Chat.ChatCompletionTool) => (tool as FunctionTool).function | ||
|
|
||
| describe("getMcpServerTools", () => { | ||
| const createMockTool = (name: string, description = "Test tool"): McpTool => ({ | ||
| name, | ||
| description, | ||
| inputSchema: { | ||
| type: "object", | ||
| properties: {}, | ||
| }, | ||
| }) | ||
|
|
||
| const createMockServer = (name: string, tools: McpTool[], source: "global" | "project" = "global"): McpServer => ({ | ||
| name, | ||
| config: JSON.stringify({ type: "stdio", command: "test" }), | ||
| status: "connected", | ||
| source, | ||
| tools, | ||
| }) | ||
|
|
||
| const createMockMcpHub = (servers: McpServer[]): Partial<McpHub> => ({ | ||
| getServers: vi.fn().mockReturnValue(servers), | ||
| }) | ||
|
|
||
| it("should return empty array when mcpHub is undefined", () => { | ||
| const result = getMcpServerTools(undefined) | ||
| expect(result).toEqual([]) | ||
| }) | ||
|
|
||
| it("should return empty array when no servers are available", () => { | ||
| const mockHub = createMockMcpHub([]) | ||
| const result = getMcpServerTools(mockHub as McpHub) | ||
| expect(result).toEqual([]) | ||
| }) | ||
|
|
||
| it("should generate tool definitions for server tools", () => { | ||
| const server = createMockServer("testServer", [createMockTool("testTool")]) | ||
| const mockHub = createMockMcpHub([server]) | ||
|
|
||
| const result = getMcpServerTools(mockHub as McpHub) | ||
|
|
||
| expect(result).toHaveLength(1) | ||
| expect(result[0].type).toBe("function") | ||
| expect(getFunction(result[0]).name).toBe("mcp--testServer--testTool") | ||
| expect(getFunction(result[0]).description).toBe("Test tool") | ||
| }) | ||
|
|
||
| it("should filter out tools with enabledForPrompt set to false", () => { | ||
| const enabledTool = createMockTool("enabledTool") | ||
| const disabledTool = { ...createMockTool("disabledTool"), enabledForPrompt: false } | ||
| const server = createMockServer("testServer", [enabledTool, disabledTool]) | ||
| const mockHub = createMockMcpHub([server]) | ||
|
|
||
| const result = getMcpServerTools(mockHub as McpHub) | ||
|
|
||
| expect(result).toHaveLength(1) | ||
| expect(getFunction(result[0]).name).toBe("mcp--testServer--enabledTool") | ||
| }) | ||
|
|
||
| it("should deduplicate tools when same server exists in both global and project configs", () => { | ||
| const globalServer = createMockServer( | ||
| "context7", | ||
| [createMockTool("resolve-library-id", "Global description")], | ||
| "global", | ||
| ) | ||
| const projectServer = createMockServer( | ||
| "context7", | ||
| [createMockTool("resolve-library-id", "Project description")], | ||
| "project", | ||
| ) | ||
|
|
||
| // Project servers come before global servers (as per McpHub.notifyWebviewOfServerChanges sorting) | ||
| const mockHub = createMockMcpHub([projectServer, globalServer]) | ||
|
|
||
| const result = getMcpServerTools(mockHub as McpHub) | ||
|
|
||
| // Should only have one tool, not two | ||
| expect(result).toHaveLength(1) | ||
| expect(getFunction(result[0]).name).toBe("mcp--context7--resolve-library-id") | ||
| // Project server takes priority (comes first in the list) | ||
| expect(getFunction(result[0]).description).toBe("Project description") | ||
| }) | ||
|
|
||
| it("should allow tools with different names from the same server", () => { | ||
| const server = createMockServer("testServer", [ | ||
| createMockTool("tool1"), | ||
| createMockTool("tool2"), | ||
| createMockTool("tool3"), | ||
| ]) | ||
| const mockHub = createMockMcpHub([server]) | ||
|
|
||
| const result = getMcpServerTools(mockHub as McpHub) | ||
|
|
||
| expect(result).toHaveLength(3) | ||
| const toolNames = result.map((t) => getFunction(t).name) | ||
| expect(toolNames).toContain("mcp--testServer--tool1") | ||
| expect(toolNames).toContain("mcp--testServer--tool2") | ||
| expect(toolNames).toContain("mcp--testServer--tool3") | ||
| }) | ||
|
|
||
| it("should allow tools with same name from different servers", () => { | ||
| const server1 = createMockServer("server1", [createMockTool("commonTool")]) | ||
| const server2 = createMockServer("server2", [createMockTool("commonTool")]) | ||
| const mockHub = createMockMcpHub([server1, server2]) | ||
|
|
||
| const result = getMcpServerTools(mockHub as McpHub) | ||
|
|
||
| expect(result).toHaveLength(2) | ||
| const toolNames = result.map((t) => getFunction(t).name) | ||
| expect(toolNames).toContain("mcp--server1--commonTool") | ||
| expect(toolNames).toContain("mcp--server2--commonTool") | ||
| }) | ||
|
|
||
| it("should skip servers without tools", () => { | ||
| const serverWithTools = createMockServer("withTools", [createMockTool("tool1")]) | ||
| const serverWithoutTools = createMockServer("withoutTools", []) | ||
| const serverWithUndefinedTools: McpServer = { | ||
| ...createMockServer("undefinedTools", []), | ||
| tools: undefined, | ||
| } | ||
| const mockHub = createMockMcpHub([serverWithTools, serverWithoutTools, serverWithUndefinedTools]) | ||
|
|
||
| const result = getMcpServerTools(mockHub as McpHub) | ||
|
|
||
| expect(result).toHaveLength(1) | ||
| expect(getFunction(result[0]).name).toBe("mcp--withTools--tool1") | ||
| }) | ||
|
|
||
| it("should include required fields from tool schema", () => { | ||
| const toolWithRequired: McpTool = { | ||
| name: "toolWithRequired", | ||
| description: "Tool with required fields", | ||
| inputSchema: { | ||
| type: "object", | ||
| properties: { | ||
| requiredField: { type: "string" }, | ||
| optionalField: { type: "number" }, | ||
| }, | ||
| required: ["requiredField"], | ||
| }, | ||
| } | ||
| const server = createMockServer("testServer", [toolWithRequired]) | ||
| const mockHub = createMockMcpHub([server]) | ||
|
|
||
| const result = getMcpServerTools(mockHub as McpHub) | ||
|
|
||
| expect(result).toHaveLength(1) | ||
| expect(getFunction(result[0]).parameters).toEqual({ | ||
| type: "object", | ||
| properties: { | ||
| requiredField: { type: "string" }, | ||
| optionalField: { type: "number" }, | ||
| }, | ||
| additionalProperties: false, | ||
| required: ["requiredField"], | ||
| }) | ||
| }) | ||
|
|
||
| it("should not include required field when schema has no required fields", () => { | ||
| const toolWithoutRequired: McpTool = { | ||
| name: "toolWithoutRequired", | ||
| description: "Tool without required fields", | ||
| inputSchema: { | ||
| type: "object", | ||
| properties: { | ||
| optionalField: { type: "string" }, | ||
| }, | ||
| }, | ||
| } | ||
| const server = createMockServer("testServer", [toolWithoutRequired]) | ||
| const mockHub = createMockMcpHub([server]) | ||
|
|
||
| const result = getMcpServerTools(mockHub as McpHub) | ||
|
|
||
| expect(result).toHaveLength(1) | ||
| expect(getFunction(result[0]).parameters).toEqual({ | ||
| type: "object", | ||
| properties: { | ||
| optionalField: { type: "string" }, | ||
| }, | ||
| additionalProperties: false, | ||
| }) | ||
| expect(getFunction(result[0]).parameters).not.toHaveProperty("required") | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.