Skip to content

Commit d710186

Browse files
committed
fix(mcp): encode prefixed tool names unambiguously
1 parent b9f25db commit d710186

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

src/agents/agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ class MCPConfig(TypedDict):
151151
"""
152152

153153
include_server_in_tool_names: NotRequired[bool]
154-
"""If True, MCP tools are exposed as `<server_name>_<tool_name>` to avoid collisions across
155-
servers that publish the same tool names. Defaults to False.
154+
"""If True, MCP tools are exposed with an unambiguous server-specific prefix to avoid
155+
collisions across servers that publish the same tool names. Defaults to False.
156156
"""
157157

158158

src/agents/mcp/util.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,11 @@ async def get_function_tools(
265265
convert_schemas_to_strict,
266266
agent,
267267
failure_error_function=failure_error_function,
268-
tool_name_override=f"{tool_name_prefix}{tool.name}" if tool_name_prefix else None,
268+
tool_name_override=(
269+
cls._prefixed_tool_name(tool_name_prefix, tool.name)
270+
if tool_name_prefix
271+
else None
272+
),
269273
)
270274
for tool in tools
271275
]
@@ -280,6 +284,10 @@ def _server_tool_name_prefix(server_name: str) -> str:
280284
normalized = "server"
281285
return f"{normalized}_"
282286

287+
@staticmethod
288+
def _prefixed_tool_name(tool_name_prefix: str, tool_name: str) -> str:
289+
return f"mcp_{len(tool_name_prefix)}_{tool_name_prefix}{tool_name}"
290+
283291
@classmethod
284292
def _server_tool_name_prefixes(cls, servers: list[MCPServer]) -> dict[int, str]:
285293
normalized_to_servers: dict[str, list[MCPServer]] = {}

tests/mcp/test_mcp_util.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,12 @@ async def test_get_all_function_tools_can_prefix_with_server_name():
120120

121121
tools = await agent.get_mcp_tools(run_context)
122122
tool_names = {tool.name for tool in tools}
123-
assert tool_names == {"GitHub_MCP_Server_create_issue", "linear_create_issue"}
123+
assert tool_names == {"mcp_18_GitHub_MCP_Server_create_issue", "mcp_7_linear_create_issue"}
124124

125-
github_tool = next(tool for tool in tools if tool.name == "GitHub_MCP_Server_create_issue")
126-
linear_tool = next(tool for tool in tools if tool.name == "linear_create_issue")
125+
github_tool = next(
126+
tool for tool in tools if tool.name == "mcp_18_GitHub_MCP_Server_create_issue"
127+
)
128+
linear_tool = next(tool for tool in tools if tool.name == "mcp_7_linear_create_issue")
127129
assert isinstance(github_tool, FunctionTool)
128130
assert isinstance(linear_tool, FunctionTool)
129131

@@ -165,7 +167,7 @@ async def test_get_all_function_tools_prefix_falls_back_for_empty_server_name_sl
165167
assert len(tools) == 1
166168
prefixed_tool = tools[0]
167169
assert isinstance(prefixed_tool, FunctionTool)
168-
assert prefixed_tool.name == "server_search"
170+
assert prefixed_tool.name == "mcp_7_server_search"
169171

170172
tool_context = ToolContext(
171173
context=None,
@@ -197,7 +199,7 @@ async def test_get_all_function_tools_disambiguates_colliding_server_name_prefix
197199
tools = await agent.get_mcp_tools(run_context)
198200
tool_names = {tool.name for tool in tools}
199201
assert len(tool_names) == 2
200-
assert all(tool_name.startswith("GitHub_MCP_Server_") for tool_name in tool_names)
202+
assert all(tool_name.startswith("mcp_27_GitHub_MCP_Server_") for tool_name in tool_names)
201203
assert all(tool_name.endswith("_create_issue") for tool_name in tool_names)
202204

203205
for idx, tool in enumerate(tools, start=1):
@@ -287,7 +289,7 @@ def resolve_meta(context):
287289

288290
prefixed_tool = tools[0]
289291
assert isinstance(prefixed_tool, FunctionTool)
290-
assert prefixed_tool.name == "GitHub_MCP_Server_create_issue"
292+
assert prefixed_tool.name == "mcp_18_GitHub_MCP_Server_create_issue"
291293

292294
tool_context = ToolContext(
293295
context=None,

0 commit comments

Comments
 (0)