feat(mcp): add tool_name_prefix to disambiguate duplicate MCP tool names#3401
feat(mcp): add tool_name_prefix to disambiguate duplicate MCP tool names#3401adityasingh2400 wants to merge 1 commit into
Conversation
|
Note on the duplicate label. PRs #2677 and #2788 both went stale before landing rather than being rejected on substance. The closest prior attempt, #2788, hit a concrete blocker that seratch flagged in his review, the auto-derived prefix from a non-ASCII server label produced invalid OpenAI tool names. This PR takes a different shape on that exact point. The prefix is user-provided as a keyword argument on the MCPServer constructor, not derived from the server label, so the non-ASCII generation path does not exist here. The user picks the short readable prefix per server ("github", "linear") and the SDK leaves it alone aside from length validation against the 64-char tool-name ceiling. The internal cache, tool_filter, and require_approval lookups continue to operate on the upstream tool name, and call_tool strips the prefix before dispatch so the server still receives its original name. Happy to rebase or split the docs section out if that helps land the core API change. |
Adds an optional keyword-only `tool_name_prefix` argument to `MCPServer`,
`_MCPServerWithClientSession`, and the `MCPServerStdio`, `MCPServerSse`, and
`MCPServerStreamableHttp` constructors. When set, `list_tools()` returns tools
named `f"{prefix}_{original_name}"`, `call_tool()` strips the prefix before
dispatching upstream, and `require_approval` policy lookups continue to match
the original tool name. The internal cache and `tool_filter` still operate on
the upstream tool names. Raises `UserError` early when a prefixed name would
exceed the 64-character MCP tool-name limit.
Closes openai#464
a185cc6 to
f3a5040
Compare
When two MCP servers expose tools with the same name (e.g. both a GitHub and a Linear server expose
create_issue), wiring both into an agent fails withDuplicate tool names found across MCP servers. The existinginclude_server_in_tool_namesagent-level flag auto-generates a server-derived prefix, but users have no way to pick a short, friendly prefix per server – the original ask in the issue.This PR adds an optional keyword-only
tool_name_prefixargument to theMCPServerbase class and theMCPServerStdio,MCPServerSse, andMCPServerStreamableHttpconstructors. When set,list_tools()returns tools namedf"{prefix}_{original_name}"andcall_tool()strips the prefix before dispatching to the upstream MCP server, so the server still receives its original tool name. The internal cache andtool_filtercontinue to operate on the upstream names, andrequire_approvalpolicy lookups continue to match the original tool name even with a prefix configured. A prefixed name that would exceed the 64-character MCP tool-name limit raisesUserErrorearly atlist_tools()time rather than failing later.Tests cover the two-servers-with-different-prefixes case, prefix stripping on
call_tool()(both via theFakeMCPServerand an end-to-end check against the realMCPServerStdiowith the MCP client patched), composition withtool_filter, the 64-char overflow error, and cache invariants. Docs indocs/mcp.mdget a short new section showing how to use it. Closes #464.