-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix: handle duplicate tool names across MCP servers by auto-renaming #2954
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
Open
houziershi
wants to merge
1
commit into
openai:main
Choose a base branch
from
houziershi:fix-mcp-duplicate-tool-names
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+268
−19
Open
Changes from all commits
Commits
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
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,154 @@ | ||
| """Tests for MCP duplicate tool name handling.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from agents import Agent, FunctionTool, RunContextWrapper | ||
| from agents.mcp import MCPServer, MCPUtil | ||
|
|
||
| from .helpers import FakeMCPServer | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_all_function_tools_with_duplicate_names(): | ||
| """Test that duplicate tool names across MCP servers are automatically renamed.""" | ||
| server1 = FakeMCPServer(server_name="server1") | ||
| server1.add_tool("search", {}) | ||
| server1.add_tool("fetch", {}) | ||
|
|
||
| server2 = FakeMCPServer(server_name="server2") | ||
| server2.add_tool("search", {}) # duplicate name | ||
| server2.add_tool("update", {}) | ||
|
|
||
| servers: list[MCPServer] = [server1, server2] | ||
| run_context = RunContextWrapper(context=None) | ||
| agent = Agent(name="test_agent", instructions="Test agent") | ||
|
|
||
| tools = await MCPUtil.get_all_function_tools(servers, False, run_context, agent) | ||
|
|
||
| # Should have 4 tools total | ||
| assert len(tools) == 4 | ||
|
|
||
| tool_names = [tool.name for tool in tools] | ||
| # Original names from first server should be preserved | ||
| assert "search" in tool_names | ||
| assert "fetch" in tool_names | ||
| # Duplicate from second server should be renamed | ||
| assert "server2__search" in tool_names | ||
| assert "update" in tool_names | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_all_function_tools_with_duplicate_names_three_servers(): | ||
| """Test duplicate tool name handling with three servers having the same tool name.""" | ||
| server1 = FakeMCPServer(server_name="server1") | ||
| server1.add_tool("search", {}) | ||
|
|
||
| server2 = FakeMCPServer(server_name="server2") | ||
| server2.add_tool("search", {}) # duplicate | ||
|
|
||
| server3 = FakeMCPServer(server_name="server3") | ||
| server3.add_tool("search", {}) # another duplicate | ||
|
|
||
| servers: list[MCPServer] = [server1, server2, server3] | ||
| run_context = RunContextWrapper(context=None) | ||
| agent = Agent(name="test_agent", instructions="Test agent") | ||
|
|
||
| tools = await MCPUtil.get_all_function_tools(servers, False, run_context, agent) | ||
|
|
||
| assert len(tools) == 3 | ||
| tool_names = [tool.name for tool in tools] | ||
| assert "search" in tool_names | ||
| assert "server2__search" in tool_names | ||
| assert "server3__search" in tool_names | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_all_function_tools_normalizes_server_name_in_renamed_tool(): | ||
| """Test renamed tool names use a function-calling-safe server prefix.""" | ||
| server1 = FakeMCPServer(server_name="Primary Server") | ||
| server1.add_tool("search", {}) | ||
|
|
||
| server2 = FakeMCPServer(server_name="Secondary-Server") | ||
| server2.add_tool("search", {}) # duplicate | ||
|
|
||
| servers: list[MCPServer] = [server1, server2] | ||
| run_context = RunContextWrapper(context=None) | ||
| agent = Agent(name="test_agent", instructions="Test agent") | ||
|
|
||
| tools = await MCPUtil.get_all_function_tools(servers, False, run_context, agent) | ||
|
|
||
| tool_names = [tool.name for tool in tools] | ||
| assert "search" in tool_names | ||
| assert "secondary_server__search" in tool_names | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_all_function_tools_no_duplicates(): | ||
| """Test that non-duplicate tool names are not affected.""" | ||
| server1 = FakeMCPServer(server_name="server1") | ||
| server1.add_tool("search", {}) | ||
|
|
||
| server2 = FakeMCPServer(server_name="server2") | ||
| server2.add_tool("fetch", {}) # no duplicate | ||
|
|
||
| servers: list[MCPServer] = [server1, server2] | ||
| run_context = RunContextWrapper(context=None) | ||
| agent = Agent(name="test_agent", instructions="Test agent") | ||
|
|
||
| tools = await MCPUtil.get_all_function_tools(servers, False, run_context, agent) | ||
|
|
||
| assert len(tools) == 2 | ||
| tool_names = [tool.name for tool in tools] | ||
| assert "search" in tool_names | ||
| assert "fetch" in tool_names | ||
| # Should not have any prefixed names | ||
| assert "server1__search" not in tool_names | ||
| assert "server2__fetch" not in tool_names | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_get_all_function_tools_preserves_mcp_origin(): | ||
| """Test that renamed tools preserve their MCP origin metadata.""" | ||
| server1 = FakeMCPServer(server_name="server1") | ||
| server1.add_tool("search", {}) | ||
|
|
||
| server2 = FakeMCPServer(server_name="server2") | ||
| server2.add_tool("search", {}) # duplicate | ||
|
|
||
| servers: list[MCPServer] = [server1, server2] | ||
| run_context = RunContextWrapper(context=None) | ||
| agent = Agent(name="test_agent", instructions="Test agent") | ||
|
|
||
| tools = await MCPUtil.get_all_function_tools(servers, False, run_context, agent) | ||
|
|
||
| # Find the renamed tool | ||
| renamed_tool = next((t for t in tools if t.name == "server2__search"), None) | ||
| assert renamed_tool is not None | ||
| assert isinstance(renamed_tool, FunctionTool) | ||
| # Check that MCP origin is preserved | ||
| assert renamed_tool._tool_origin is not None | ||
| assert renamed_tool._tool_origin.mcp_server_name == "server2" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_renamed_tool_can_be_invoked(): | ||
| """Test that renamed tools can still be invoked successfully.""" | ||
| server1 = FakeMCPServer(server_name="server1") | ||
| server1.add_tool("search", {}) | ||
|
|
||
| server2 = FakeMCPServer(server_name="server2") | ||
| server2.add_tool("search", {}) # duplicate | ||
|
|
||
| servers: list[MCPServer] = [server1, server2] | ||
| run_context = RunContextWrapper(context=None) | ||
| agent = Agent(name="test_agent", instructions="Test agent") | ||
|
|
||
| tools = await MCPUtil.get_all_function_tools(servers, False, run_context, agent) | ||
|
|
||
| # Find the renamed tool and invoke it | ||
| renamed_tool = next((t for t in tools if t.name == "server2__search"), None) | ||
| assert renamed_tool is not None | ||
| assert isinstance(renamed_tool, FunctionTool) | ||
|
|
||
| # The tool should be invocable | ||
| assert renamed_tool.on_invoke_tool is not None |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a duplicate MCP tool is renamed here, the uniqueness check only considers
tool_namesfrom MCP servers, so the generated alias can still collide with a user-defined tool name (for example a local function tool already namedserver2__search).AgentBase.get_all_tools()later concatenates MCP tools with local tools without deduplicating (src/agents/agent.py), andbuild_function_tool_lookup_map()resolves name collisions with last-wins semantics (src/agents/_tool_identity.py), so a model call to the renamed alias can execute the wrong tool. This regression is introduced by the new auto-rename path, because the previous behavior failed fast withUserErroron MCP duplicates.Useful? React with 👍 / 👎.