The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(mcp): bound concurrent server connects during discovery (#117373)
Halldrix5208541ba176
diff --git a/tests/tools/test_mcp_tool.py b/tests/tools/test_mcp_tool.pyindex db4dc5774bc..bf050524777 100644--- a/tests/tools/test_mcp_tool.py+++ b/tests/tools/test_mcp_tool.py@@ -2770,6 +2770,74 @@ class TestDiscoveryFailedCount:_servers.pop("fail1", None)+class TestDiscoveryConnectConcurrency:+ """MCP discovery bounds how many servers connect at once (#117373)."""++ def test_concurrent_connects_never_exceed_cap(self):+ """With more servers than the cap, in-flight connects stay capped."""+ import asyncio as _asyncio++ from tools import mcp_tool_discovery as _discovery+ from tools.mcp_tool import _servers+ from tools.mcp_tool_loop import _ensure_mcp_loop++ server_names = {f"srv{i}": {"command": "npx", "args": [f"s{i}"]} for i in range(8)}+ in_flight = 0+ max_in_flight = 0++ async def tracked_register(name, cfg):+ nonlocal in_flight, max_in_flight+ in_flight += 1+ max_in_flight = max(max_in_flight, in_flight)+ # Yield real control so gathers genuinely overlap.+ await _asyncio.sleep(0.05)+ in_flight -= 1+ return []++ with patch("tools.mcp_tool_config._load_mcp_config", return_value=server_names), \+ patch("tools.mcp_tool_discovery._discover_and_register_server", side_effect=tracked_register), \+ patch("tools.mcp_tool._MCP_AVAILABLE", True), \+ patch("tools.mcp_tool_registration._existing_tool_names", return_value=[]):+ _ensure_mcp_loop()+ _discovery._run_discovery_pass(server_names)++ try:+ assert max_in_flight > 1, "connects should still run concurrently"+ assert max_in_flight <= _discovery._DISCOVERY_CONNECT_CONCURRENCY, (+ f"in-flight connects peaked at {max_in_flight}, "+ f"cap is {_discovery._DISCOVERY_CONNECT_CONCURRENCY}"+ )+ finally:+ for name in server_names:+ _servers.pop(name, None)++ def test_cap_lower_than_server_count_still_connects_all(self):+ """A capped pass still connects every server, not just the first N."""+ from tools import mcp_tool_discovery as _discovery+ from tools.mcp_tool import _servers+ from tools.mcp_tool_loop import _ensure_mcp_loop++ server_names = {f"srv{i}": {"command": "npx", "args": [f"s{i}"]} for i in range(5)}+ connected = []++ async def recording_register(name, cfg):+ connected.append(name)+ return []++ with patch("tools.mcp_tool_config._load_mcp_config", return_value=server_names), \+ patch("tools.mcp_tool_discovery._discover_and_register_server", side_effect=recording_register), \+ patch("tools.mcp_tool._MCP_AVAILABLE", True), \+ patch("tools.mcp_tool_registration._existing_tool_names", return_value=[]):+ _ensure_mcp_loop()+ _discovery._run_discovery_pass(server_names)++ try:+ assert sorted(connected) == sorted(server_names)+ finally:+ for name in server_names:+ _servers.pop(name, None)++class TestMCPSelectiveToolLoading:"""Tests for per-server MCP filtering and utility tool policies."""diff --git a/tools/mcp_tool_discovery.py b/tools/mcp_tool_discovery.pyindex 22d7ba0f543..8cb2727e33a 100644--- a/tools/mcp_tool_discovery.py+++ b/tools/mcp_tool_discovery.py@@ -22,6 +22,10 @@ from tools.mcp_tool_scope import _key_name, _key_scope, _key_visible_in_scope, _logger = logging.getLogger("tools.mcp_tool")+# Max concurrent MCP server connections per discovery pass (one unbounded+# `asyncio.gather` spawned every server's subprocess tree simultaneously).+_DISCOVERY_CONNECT_CONCURRENCY = 3+def _record_connect_failure(server_name: str) -> None:"""Stamp a geometric, capped retry cooldown after a failed connect (under ``_lock``)."""@@ -372,9 +376,21 @@ def _register_lazy_from_cache(new_servers: Dict[str, dict]) -> Tuple[Dict[str, dasync def _discover_all(new_servers: Dict[str, dict]) -> None:- """Connect every candidate concurrently; record per-server outcome."""+ """Connect every candidate concurrently; record per-server outcome.++ Concurrency is bounded: every stdio server spawns child processes, so an+ unbounded gather turns a config with many servers into a simultaneous+ N-process spawn burst (RAM/CPU spike, EMFILE risk) on every backend boot.+ """+ # ponytail: flat cap for all transports; a per-transport cap only if stdio+HTTP mixes ever matter+ semaphore = asyncio.Semaphore(_DISCOVERY_CONNECT_CONCURRENCY)++ async def _connect_bounded(name: str, cfg: dict):+ async with semaphore:+ return await _discover_and_register_server(name, cfg)+results = await asyncio.gather(- *(_discover_and_register_server(name, cfg) for name, cfg in new_servers.items()),+ *(_connect_bounded(name, cfg) for name, cfg in new_servers.items()),return_exceptions=True)for name, result in zip(new_servers, results):if isinstance(result, BaseException):