diff --git a/deep_agent/aegra/mcp.py b/deep_agent/aegra/mcp.py index 97ea95eb..9c00ea90 100644 --- a/deep_agent/aegra/mcp.py +++ b/deep_agent/aegra/mcp.py @@ -423,6 +423,7 @@ async def _connect_single_server( tool_interceptors=[ _TokenInjectorInterceptor(name, server_cfg), ], + tool_name_prefix=bool(server_cfg.get("tool_prefix", name)), ) tools: list[Any] = await client.get_tools() logger.info(f"[{name}] loaded {len(tools)} tool(s)") diff --git a/tests/unit/infrastructure/test_mcp.py b/tests/unit/infrastructure/test_mcp.py index 3043b7b9..7aaed096 100644 --- a/tests/unit/infrastructure/test_mcp.py +++ b/tests/unit/infrastructure/test_mcp.py @@ -158,6 +158,44 @@ async def test_successful_connection(self): assert len(tools) == 1 assert tools[0].name == "test_tool" + @pytest.mark.asyncio + async def test_tool_prefix_enables_name_prefix(self): + """Test that tool_prefix in server_cfg sets tool_name_prefix=True.""" + mock_client = MagicMock() + mock_client.get_tools = AsyncMock(return_value=[]) + + config = {"url": "http://localhost:8000/mcp/", "transport": "http"} + server_cfg = {"tool_prefix": "myprefix"} + + with patch( + "deep_agent.aegra.mcp.MultiServerMCPClient", + return_value=mock_client, + ) as mock_cls: + await _connect_single_server("myprefix", config, server_cfg, timeout=5) + + mock_cls.assert_called_once() + call_kwargs = mock_cls.call_args[1] + assert call_kwargs["tool_name_prefix"] is True + + @pytest.mark.asyncio + async def test_no_tool_prefix_falls_back_to_name(self): + """Test that without tool_prefix, name is used as fallback and prefixing is enabled.""" + mock_client = MagicMock() + mock_client.get_tools = AsyncMock(return_value=[]) + + config = {"url": "http://localhost:8000/mcp/", "transport": "streamable_http"} + server_cfg = {} # no tool_prefix + + with patch( + "deep_agent.aegra.mcp.MultiServerMCPClient", + return_value=mock_client, + ) as mock_cls: + await _connect_single_server("server-key", config, server_cfg, timeout=5) + + mock_cls.assert_called_once() + call_kwargs = mock_cls.call_args[1] + assert call_kwargs["tool_name_prefix"] is True + @pytest.mark.asyncio async def test_connection_timeout_returns_empty_list(self): """Test that connection timeout returns empty list."""