Skip to content

Commit fc4d832

Browse files
fix(mcp): escape duplicate tool names in diagnostics (#5217)
1 parent 3e5eac0 commit fc4d832

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

‎src/agents/mcp/util.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ async def get_all_function_tools(
307307
if duplicate_tool_names:
308308
raise UserError(
309309
"Duplicate tool names found across MCP servers: "
310-
f"{', '.join(duplicate_tool_names)}"
310+
f"{', '.join(repr(name) for name in duplicate_tool_names)}"
311311
)
312312
tool_names.update(server_tool_names)
313313
tools.extend(function_tools)
@@ -328,7 +328,7 @@ async def get_all_function_tools(
328328
if duplicate_tool_names:
329329
raise UserError(
330330
"Duplicate tool names found across MCP servers: "
331-
f"{', '.join(duplicate_tool_names)}. "
331+
f"{', '.join(repr(name) for name in duplicate_tool_names)}. "
332332
"Pass `include_server_in_tool_names=True` to "
333333
"`MCPUtil.get_all_function_tools()` or set "
334334
"`mcp_config={'include_server_in_tool_names': True}` on the "

‎tests/mcp/test_mcp_util.py‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,30 @@ def test_public_mcp_server_constructors_forward_guardrail_configuration():
237237
assert tool.tool_output_guardrails is not output_guardrails
238238

239239

240+
@pytest.mark.asyncio
241+
@pytest.mark.parametrize(
242+
("tool_name", "escaped_name"),
243+
[
244+
("alpha\nFORGED\x1b[31m, trailing", "'alpha\\nFORGED\\x1b[31m, trailing'"),
245+
("alpha\rFORGED\u2028\u202e", "'alpha\\rFORGED\\u2028\\u202e'"),
246+
],
247+
)
248+
async def test_agent_duplicate_mcp_tool_names_escape_diagnostics(tool_name, escaped_name):
249+
servers = [FakeMCPServer(server_name="server_1"), FakeMCPServer(server_name="server_2")]
250+
for server in servers:
251+
server.add_tool(tool_name, {})
252+
agent = Agent(name="test_agent", mcp_servers=servers)
253+
254+
with pytest.raises(UserError) as exc_info:
255+
await agent.get_all_tools(RunContextWrapper(context=None))
256+
257+
message = str(exc_info.value)
258+
assert message.startswith(f"Duplicate tool names found across MCP servers: {escaped_name}. ")
259+
assert "include_server_in_tool_names=True" in message
260+
assert not any(control in message for control in ("\n", "\r", "\x1b", "\u2028", "\u202e"))
261+
assert all(server.tool_calls == [] for server in servers)
262+
263+
240264
@pytest.mark.asyncio
241265
async def test_get_all_function_tools_duplicate_error_is_deterministic():
242266
server1 = FakeMCPServer(server_name="server_1")
@@ -254,7 +278,7 @@ async def test_get_all_function_tools_duplicate_error_is_deterministic():
254278
await MCPUtil.get_all_function_tools([server1, server2], False, run_context, agent)
255279

256280
assert str(exc_info.value) == (
257-
"Duplicate tool names found across MCP servers: alpha, zeta. "
281+
"Duplicate tool names found across MCP servers: 'alpha', 'zeta'. "
258282
"Pass `include_server_in_tool_names=True` to "
259283
"`MCPUtil.get_all_function_tools()` or set "
260284
"`mcp_config={'include_server_in_tool_names': True}` on the "
@@ -292,7 +316,7 @@ def _return_colliding_names(server_tool_batches, *, reserved_names):
292316
include_server_in_tool_names=True,
293317
)
294318

295-
assert str(exc_info.value) == "Duplicate tool names found across MCP servers: mcp_same__tool"
319+
assert str(exc_info.value) == "Duplicate tool names found across MCP servers: 'mcp_same__tool'"
296320

297321

298322
@pytest.mark.asyncio

0 commit comments

Comments
 (0)