@@ -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\n FORGED\x1b [31m, trailing" , "'alpha\\ nFORGED\\ x1b[31m, trailing'" ),
245+ ("alpha\r FORGED\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
241265async 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