Skip to content

Fix MCP tool name conflicts by adding server name prefixes #1178

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
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

fadeoreo
Copy link

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:

  1. Prefixes tool names with server name: Tool names are prefixed with {server_name}_{tool_name} format (e.g., serverA_run, serverB_run)
  2. Preserves original names: The original tool name is stored in the original_name attribute for actual tool calls
  3. Maintains backward compatibility: Tool invocation still uses the original name when calling the MCP server

Changes Made

Core Implementation

  • src/agents/mcp/server.py: Modified list_tools() method to add server name prefixes and preserve original names
  • src/agents/mcp/util.py: Updated invoke_mcp_tool() to use original names for tool calls

Testing

  • tests/mcp/test_tool_name_conflicts.py: Comprehensive test suite with 9 test cases covering:
    • Basic prefixing functionality
    • Multiple server conflict resolution
    • MCPUtil integration
    • Legacy behavior regression testing
    • Edge cases (empty names, special characters)
    • Tool invocation verification

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

Problem:
- When multiple MCP servers have tools with the same name, agent list MCP would hang
- Tool name conflicts made it impossible to distinguish tools from different servers
- This caused the agent SDK to malfunction when listing tools

Solution:
- Add server name prefixes to tool names (e.g., server1_run, server2_run)
- Store original tool names in 'original_name' attribute for actual tool calls
- Maintain backward compatibility - tool invocation still uses original names
- Handle edge cases like empty server names and special characters

Changes:
- Modified src/agents/mcp/server.py: Added tool name prefixing in list_tools()
- Modified src/agents/mcp/util.py: Updated invoke_mcp_tool() to use original names
- Enhanced call_tool() to handle both prefixed and original tool names
- Added comprehensive logging for debugging

This resolves the hanging issue and ensures tools from different servers
are properly distinguished while maintaining full backward compatibility.
@seratch seratch added enhancement New feature or request feature:mcp labels Jul 18, 2025
@seratch
Copy link
Member

seratch commented Jul 19, 2025

At a glance, the changes look good to me. Can you resolve the lint and typecheck failures?

@fadeoreo
Copy link
Author

Okay, I'll try it.

he.li and others added 2 commits July 21, 2025 10:51
…name and handle both prefixed and original names.

Updated invoke_mcp_tool method in util.py to ensure calls are made using original tool names.
Added test cases in test_tool_name_conflicts.py to verify the correctness of tool name prefixes and preservation of original names.
@seratch
Copy link
Member

seratch commented Jul 22, 2025

Can you check the mypy errors in tests?

uv run mypy .
tests/mcp/test_tool_name_conflicts.py:35: error: Missing type parameters for generic type "dict"  [type-arg]
tests/mcp/test_tool_name_conflicts.py:66: error: "Tool" has no attribute "original_name"  [attr-defined]
tests/mcp/test_tool_name_conflicts.py:205: error: Missing type parameters for generic type "dict"  [type-arg]
tests/mcp/test_tool_name_conflicts.py:279: error: "Tool" has no attribute "original_name"  [attr-defined]
tests/mcp/test_tool_name_conflicts.py:308: error: "Tool" has no attribute "original_name"  [attr-defined]
tests/mcp/test_tool_name_conflicts.py:325: error: "Tool" has no attribute "original_name"  [attr-defined]
tests/mcp/test_tool_name_conflicts.py:343: error: "Tool" has no attribute "original_name"  [attr-defined]
Found 7 errors in 1 file (checked 269 source files)
make: *** [Makefile:20: mypy] Error 1
Error: Process completed with exit code 2.

@fadeoreo
Copy link
Author

Ok, I will handle these errors.

@mshsheikh
Copy link
Contributor

Ok, I will handle these errors.

@fadeoreo if you need help, I can fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature:mcp
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Please fix the issue where duplicate tool names in multiple MCP tools cause the agent list to hang
3 participants