The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(mcp): a lazy server whose first connect failed is not ready
unsupportedpastelsb26bb96dbaa5
diff --git a/tests/gateway/test_mcp_reload_new_server.py b/tests/gateway/test_mcp_reload_new_server.pyindex 2fa799e5acd..c1df94296d6 100644--- a/tests/gateway/test_mcp_reload_new_server.py+++ b/tests/gateway/test_mcp_reload_new_server.py@@ -323,6 +323,36 @@ async def test_a_server_disabled_after_the_chat_picked_it_stops_on_resume_and_onstore.close_all_db_handles()+def test_a_lazy_server_whose_first_connect_failed_is_not_ready(tmp_path, monkeypatch):+ """A lazy (schema-cache) registration that never started is ready. Once its first-use connect fails it keeps+ the registration (a later use retries it) but serves nothing: a missing executable parks a registered+ task, and a server that never answers its handshake times out and leaves no task at all."""+ import sys+ from tools import mcp_tool, mcp_tool_discovery as discovery+ from tools.mcp_tool_lifecycle import shutdown_mcp_servers+ from tools.mcp_tool_scope import _server_key+ lazy = {'missing': {'command': str(tmp_path / 'no-such-mcp'), 'lazy': True, 'connect_timeout': 5},+ 'silent': {'command': sys.executable, 'args': ['-c', 'import sys; sys.stdin.read()'], 'lazy': True,+ 'connect_timeout': 1}}+ keys = {name: _server_key(name) for name in lazy}+ monkeypatch.setattr(discovery._config, '_load_mcp_config', lambda: lazy)+ for name, config in lazy.items():+ monkeypatch.setitem(mcp_tool._lazy_server_configs, keys[name], config)+ try:+ assert (discovery.mcp_servers_ready(), discovery.mcp_servers_not_connected()) == (['missing', 'silent'], [])+ for name, key in keys.items():+ assert discovery._ensure_lazy_server_connected(name) is False+ assert key in mcp_tool._lazy_server_configs and key in mcp_tool._server_connect_errors, name+ assert keys['silent'] not in mcp_tool._servers+ assert (discovery.mcp_servers_ready(), discovery.mcp_servers_not_connected()) == ([], ['missing', 'silent'])+ finally:+ shutdown_mcp_servers()+ for state in (mcp_tool._server_connect_errors, mcp_tool._server_connect_failures,+ mcp_tool._server_connect_retry_after):+ for key in keys.values():+ state.pop(key, None)++def test_not_connected_names_a_configured_server_in_cooldown(monkeypatch):from tools import mcp_tool, mcp_tool_discovery as discoveryfrom tools.mcp_tool_scope import _server_keydiff --git a/tools/mcp_tool_discovery.py b/tools/mcp_tool_discovery.pyindex 0608fa22c83..118527c2298 100644--- a/tools/mcp_tool_discovery.py+++ b/tools/mcp_tool_discovery.py@@ -719,23 +719,26 @@ def _enabled_config_names() -> set[str]:def _not_connected(wanted: set[str], scope) -> set[str]:with _core._lock:# A parked task stays registered (it self-probes) but serves nothing, as does one whose connect- # recorded an error since its last success.+ # recorded an error since its last success, and a lazy registration whose first-use connect failed+ # (a timed-out one leaves no task; one that never started has no error).serving_nothing = {name for name in wanted- if (key := _resolve_server_key(name, scope, current=False)) in _core._servers- and (key in _core._server_connect_errors- or getattr(_core._servers[key], "_park_reason", None) is not None)}+ if ((key := _resolve_server_key(name, scope, current=False)) in _core._servers+ and (key in _core._server_connect_errors+ or getattr(_core._servers[key], "_park_reason", None) is not None))+ or (key in _core._lazy_server_configs and key in _core._server_connect_errors)}return _unregistered(wanted, scope) | serving_nothingdef mcp_servers_not_connected() -> list[str]:"""Enabled ``mcp_servers`` entries the current registry scope has no live, connecting or lazy- registration for, in connect cooldown or not, or whose registration is parked or failed its- connect: after a reload, the servers that did not connect."""+ registration for, in connect cooldown or not, or whose registration (lazy included) is parked or+ failed its connect: after a reload, the servers that did not connect."""return sorted(_not_connected(_enabled_config_names(), _core._mcp_registry_scope()))def mcp_servers_ready() -> list[str]:- """Enabled ``mcp_servers`` entries the current registry scope serves now: live or lazily registered."""+ """Enabled ``mcp_servers`` entries the current registry scope serves now: live, or lazily registered+ and not failed on its first use."""wanted, scope = _enabled_config_names(), _core._mcp_registry_scope()with _core._lock:registered = {name for name in wanted