Fix MCP tool name conflicts by adding server name prefixes #1178
+366
−10
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.
Problem
When two MCP servers have tools with the same internal tool name, running
agent list MCP: list tools from server xxxx
will hang, and after the tool list is (supposedly) returned, the agent cannot work properly.During troubleshooting, issues such as network, protocol, registration, or async problems were suspected, but the real cause was that one of the MCP tools had a conflicting tool name, making it impossible for the agent SDK to distinguish between tools from different servers.
Solution
This PR implements server name prefixing for MCP tool names to ensure global uniqueness and avoid conflicts:
{server_name}_{tool_name}
format (e.g.,serverA_run
,serverB_run
)original_name
attribute for actual tool callsChanges Made
Core Implementation
src/agents/mcp/server.py
: Modifiedlist_tools()
method to add server name prefixes and preserve original namessrc/agents/mcp/util.py
: Updatedinvoke_mcp_tool()
to use original names for tool callsTesting
tests/mcp/test_tool_name_conflicts.py
: Comprehensive test suite with 9 test cases covering:Example
Before (Conflict)
Server1: [run, echo]
Server2: [run, list]
Result: ❌ Conflict on 'run' tool name → agent list hangs
After (Resolved)
Server1: [server1_run, server1_echo]
Server2: [server2_run, server2_list]
Result: ✅ All tool names are unique → agent list works correctly
Fixes #1167