Skip to content

feat(mcp): #1167 add include_server_in_tool_names option to avoid duplicate name collisions#2830

Open
hashwnath wants to merge 2 commits intoopenai:mainfrom
hashwnath:fix/1167-mcp-duplicate-tool-names
Open

feat(mcp): #1167 add include_server_in_tool_names option to avoid duplicate name collisions#2830
hashwnath wants to merge 2 commits intoopenai:mainfrom
hashwnath:fix/1167-mcp-duplicate-tool-names

Conversation

@hashwnath
Copy link
Copy Markdown

Summary

When multiple MCP servers expose tools with the same name, the SDK raises a UserError and the agent cannot proceed. This is a common scenario in production multi-server deployments where different MCP servers may independently define tools with generic names like run, search, or execute.

This PR adds an opt-in include_server_in_tool_names option to MCPConfig that prefixes each tool's name with its originating server name (e.g. my_server__run), allowing tools from different servers to coexist without collisions. Key design decisions:

  • Opt-in, not default — existing behavior (raising UserError on duplicates) is preserved. Users enable prefixing explicitly via mcp_config={"include_server_in_tool_names": True}.
  • Server name sanitization — non-alphanumeric characters are replaced with underscores, with a fallback to "server" for empty/all-special-character names. This ensures the prefixed name is always a valid tool identifier.
  • Original name preserved for invocation — the MCPTool object captured in the functools.partial closure retains the original name, so server.call_tool() always receives the correct MCP tool name regardless of the display prefix.
  • Config key name — uses include_server_in_tool_names as suggested by @seratch in the review of the prior PR feat(mcp): optional server-name prefix for colliding tool names #2442.

This follows up on the closed PR #2442 by @weiguangli-io, which implemented the same feature but went stale awaiting re-review.

Test plan

  • test_duplicate_tool_names_raises_by_default: verifies the existing UserError behavior is unchanged.
  • test_include_server_in_tool_names_avoids_collision: two servers with same tool name → no error, both tools available with prefixed names.
  • test_include_server_in_tool_names_invokes_with_original_name: verifies the MCP server receives the original (unprefixed) tool name during invocation.
  • test_include_server_in_tool_names_sanitizes_server_name: verifies special characters in server names are sanitized (e.g. my-cool.server/v2my_cool_server_v2).
  • test_include_server_in_tool_names_empty_server_name_fallback: verifies fallback to "server" prefix for degenerate names.
  • Full test suite: 2672 passed, 0 failures.
  • make format, make lint, make typecheck all pass clean (only pre-existing mypy error in unrelated file).

Issue number

Fixes #1167

Checks

  • I've added new tests (if relevant)
  • I've added/updated the relevant documentation
  • I've run make lint and make format
  • I've made sure tests pass

… name collisions

When multiple MCP servers expose tools with the same name, the agent
currently raises a UserError. This adds an opt-in MCPConfig option
`include_server_in_tool_names` that prefixes tool names with the
sanitized server name (e.g. `my_server__run`), allowing tools from
different servers to coexist. The original MCP tool name is preserved
for server invocation via functools.partial.

Fixes openai#1167
@github-actions github-actions bot added enhancement New feature or request feature:mcp labels Apr 2, 2026
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 214e19efe2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +238 to +242
if include_server_in_tool_names:
prefix = _sanitize_server_name(server.name)
for tool in server_tools:
if isinstance(tool, FunctionTool):
tool.name = f"{prefix}__{tool.name}"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Detect collisions after server-name prefixing

When include_server_in_tool_names is enabled, tool names are rewritten using a sanitized server prefix but this branch never validates that the rewritten names are unique. For example, servers named a-b and a_b both sanitize to a_b, so run becomes a_b__run for both servers. Downstream function-tool lookup is last-wins, so one tool silently shadows the other and dispatch can target the wrong server instead of raising a UserError like the default path does.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

@seratch seratch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, #2788 already goes beyond, but even the PR is not ready for merging yet. Can you fix all the comments by codex first?

@seratch seratch changed the title feat(mcp): add include_server_in_tool_names option to avoid duplicate name collisions feat(mcp): #1167 add include_server_in_tool_names option to avoid duplicate name collisions Apr 2, 2026
Move the duplicate-name check so it runs after prefixing as well.
Servers whose names sanitize to the same prefix (e.g. "a-b" and "a_b")
now correctly raise UserError instead of silently shadowing tools.

Addresses Codex review feedback on PR openai#2830.
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.

Duplicate tool names in multiple MCP tools cause the agent list to hang

2 participants