Skip to content

The agent that grows with you

fix(mcp): parked self-probe honours enabled:false and deleted config entries

kokhlo67737c37f892
diff --git a/tests/tools/test_mcp_failure_classification.py b/tests/tools/test_mcp_failure_classification.py
index 401b346ab2a..2bd8454c53a 100644
--- a/tests/tools/test_mcp_failure_classification.py
+++ b/tests/tools/test_mcp_failure_classification.py
@@ -185,6 +185,9 @@ def test_initial_auth_failure_parks_and_revives_after_relogin(
monkeypatch.setattr(mcp_tool, "_PARKED_RETRY_INTERVAL", 0.05)
+ from tools import mcp_tool_config as _config
+ monkeypatch.setattr(_config, "_load_mcp_config", lambda: {"figma": {"command": "x"}})
+
_real_sleep = asyncio.sleep
async def _fast_sleep(_delay, *a, **kw):
diff --git a/tests/tools/test_mcp_parked_probe_no_browser.py b/tests/tools/test_mcp_parked_probe_no_browser.py
index 06d6d6d82d8..2cd58048e47 100644
--- a/tests/tools/test_mcp_parked_probe_no_browser.py
+++ b/tests/tools/test_mcp_parked_probe_no_browser.py
@@ -17,6 +17,9 @@ def test_self_probe_revival_runs_with_interactive_oauth_suppressed(monkeypatch,
monkeypatch.setattr(mcp_tool, "_MAX_INITIAL_CONNECT_RETRIES", 0)
monkeypatch.setattr(mcp_tool, "_PARKED_RETRY_INTERVAL", 0.05)
+
+ from tools import mcp_tool_config as _config
+ monkeypatch.setattr(_config, "_load_mcp_config", lambda: {"srv": {"command": "x"}})
_real_sleep = asyncio.sleep
async def _fast_sleep(_delay, *a, **kw):
diff --git a/tests/tools/test_mcp_parked_probe_skips_disabled.py b/tests/tools/test_mcp_parked_probe_skips_disabled.py
new file mode 100644
index 00000000000..70e196115f7
--- /dev/null
+++ b/tests/tools/test_mcp_parked_probe_skips_disabled.py
@@ -0,0 +1,117 @@
+"""A parked server's timed self-probe must honour ``enabled: false`` / a deleted config
+entry: the probe rebuilds the transport from the config captured at start, so without the
+gate a disabled server re-runs its OAuth setup every ``_PARKED_RETRY_INTERVAL`` for the
+life of the process — the two-warning flood behind the errors.log cap being hit."""
+
+import asyncio
+
+import pytest
+
+
+def test_parked_self_probe_skips_disabled_or_deleted_entry(monkeypatch, tmp_path):
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+
+ from tools import mcp_tool
+ from tools import mcp_tool_config as _config
+ from tools.mcp_tool import MCPServerTask
+
+ monkeypatch.setattr(mcp_tool, "_MAX_RECONNECT_RETRIES", 1)
+ monkeypatch.setattr(mcp_tool, "_PARKED_RETRY_INTERVAL", 0.05)
+
+ configured = {"srv": {"command": "x"}}
+
+ def _load():
+ return {name: dict(cfg) for name, cfg in configured.items()}
+
+ monkeypatch.setattr(_config, "_load_mcp_config", _load)
+
+ _real_sleep = asyncio.sleep
+
+ async def _fast_sleep(_delay, *a, **kw):
+ await _real_sleep(0)
+
+ monkeypatch.setattr(mcp_tool.asyncio, "sleep", _fast_sleep)
+
+ state = {"transport_calls": 0, "deregistered": 0}
+
+ async def _scenario():
+ class _Task(MCPServerTask):
+ def _is_http(self):
+ return False
+
+ def _deregister_tools(self):
+ state["deregistered"] += 1
+ self._registered_tool_names = []
+
+ async def _run_stdio(self, config):
+ state["transport_calls"] += 1
+ if state["transport_calls"] == 1:
+ self.session = object()
+ self._ready.set()
+ self._ever_connected = True
+ self.session = None
+ raise RuntimeError("backend outage begins")
+ raise RuntimeError("backend still down")
+
+ task = _Task("srv")
+ task._registered_tool_names = ["srv__tool"]
+
+ run_task = asyncio.ensure_future(task.run({"command": "x"}))
+
+ for _ in range(2000):
+ await _real_sleep(0)
+ if state["deregistered"] >= 1:
+ break
+ assert state["deregistered"] >= 1, "server never parked"
+ assert not run_task.done(), "run task exited instead of parking"
+
+ baseline = state["transport_calls"]
+ for _ in range(100):
+ await _real_sleep(0.01)
+ assert state["transport_calls"] > baseline, (
+ "self-probe never attempted while enabled"
+ )
+
+ # The user disables the entry. Across several probe intervals the parked task
+ # must not rebuild the transport again.
+ configured["srv"]["enabled"] = False
+ disabled_at = state["transport_calls"]
+ for _ in range(100):
+ await _real_sleep(0.01)
+ assert state["transport_calls"] == disabled_at, (
+ "parked self-probe re-attempted a disabled server "
+ f"(transport_calls={state['transport_calls']})"
+ )
+ assert not run_task.done(), "disabled gate must park, not exit the run task"
+
+ # Same expectation for a deleted entry.
+ del configured["srv"]
+ deleted_at = state["transport_calls"]
+ for _ in range(100):
+ await _real_sleep(0.01)
+ assert state["transport_calls"] == deleted_at, (
+ "parked self-probe re-attempted a deleted server "
+ f"(transport_calls={state['transport_calls']})"
+ )
+
+ # An explicit reconnect (manual refresh, `hermes mcp login`) still revives
+ # regardless of the config gate.
+ task._reconnect_event.set()
+ revived_at = state["transport_calls"]
+ for _ in range(200):
+ await _real_sleep(0.01)
+ if state["transport_calls"] > revived_at:
+ break
+ assert state["transport_calls"] > revived_at, (
+ "explicit reconnect was gated by config"
+ )
+
+ task._shutdown_event.set()
+ task._reconnect_event.set()
+ try:
+ await asyncio.wait_for(run_task, timeout=15)
+ except (asyncio.TimeoutError, asyncio.CancelledError, Exception):
+ run_task.cancel()
+
+ asyncio.run(_scenario())
diff --git a/tests/tools/test_mcp_parked_self_probe.py b/tests/tools/test_mcp_parked_self_probe.py
index 81fccc188cc..203a803e63b 100644
--- a/tests/tools/test_mcp_parked_self_probe.py
+++ b/tests/tools/test_mcp_parked_self_probe.py
@@ -55,6 +55,9 @@ def test_parked_server_self_probes_and_revives(monkeypatch, tmp_path):
# Keep the self-probe cadence tiny so the test is fast.
monkeypatch.setattr(mcp_tool, "_PARKED_RETRY_INTERVAL", 0.05)
+ from tools import mcp_tool_config as _config
+ monkeypatch.setattr(_config, "_load_mcp_config", lambda: {"srv": {"command": "x"}})
+
_real_sleep = asyncio.sleep
async def _fast_sleep(_delay, *a, **kw):
diff --git a/tools/mcp_tool_server_run.py b/tools/mcp_tool_server_run.py
index d38ad72c7c6..377b84c089c 100644
--- a/tools/mcp_tool_server_run.py
+++ b/tools/mcp_tool_server_run.py
@@ -202,20 +202,55 @@ class MCPServerRunMixin:
self._park_reason = revival_reason
self._deregister_tools()
self._reconnect_event.clear()
- outcome = await self._wait_for_reconnect_or_shutdown(timeout=_core._PARKED_RETRY_INTERVAL)
- if outcome == "shutdown":
+ while True:
+ outcome = await self._wait_for_reconnect_or_shutdown(
+ timeout=_core._PARKED_RETRY_INTERVAL)
+ if outcome == "shutdown":
+ return True
+ # A disabled or deleted config entry must stop the self-probe here: the probe
+ # rebuilds the transport from the config captured at start, so it would re-run
+ # OAuth setup for a server the user turned off, every interval, for the life of
+ # the process (background loops that do not run the gateway reconcile tick
+ # never learn the entry changed). An explicit reconnect request — manual
+ # refresh or `hermes mcp login` — still revives immediately regardless of the
+ # config gate; only the unattended probe honours it.
+ if outcome == "self-probe" and not self._still_configured_enabled():
+ logger.info(
+ "MCP server '%s': skipping self-probe revival — entry is disabled or "
+ "gone from mcp_servers config",
+ self.name)
+ continue
+ # Nobody asked for this revival: a self-probe must never open a browser OAuth flow. The
+ # OAuth provider runs inside THIS task (the SDK's auth flow sits in the transport), so a
+ # task-local ContextVar reaches it; it stays set for the task's life — every later
+ # revival of a once-parked server is unattended too. Left interactive, an expired
+ # refresh token opened a new authorize tab every _PARKED_RETRY_INTERVAL, all night.
+ if outcome == "self-probe":
+ from tools.mcp_oauth import _oauth_interactive_enabled
+
+ _oauth_interactive_enabled.set(False)
+ logger.debug(
+ "MCP server '%s': attempting revival %s (%s); rebuilding transport.",
+ self.name,
+ revival_reason,
+ outcome,
+ )
+ return False
+
+ def _still_configured_enabled(self) -> bool:
+ """Whether ``mcp_servers`` on disk still wants this server connected: entry present
+ and ``enabled`` not false. Config read is cached on the file signature, so a parked
+ task polling this every ``_PARKED_RETRY_INTERVAL`` stays cheap. Fail-open on a
+ config-read error: a broken config must not wedge a healthy server's revival."""
+ try:
+ from tools import mcp_tool_config as _config
+ entry = (_config._load_mcp_config() or {}).get(self.name)
+ if entry is None:
+ return False
+ from tools.mcp_tool_common import _parse_boolish
+ return _parse_boolish(entry.get("enabled", True), default=True)
+ except Exception:
return True
- # Nobody asked for this revival: a self-probe must never open a browser OAuth flow. The
- # OAuth provider runs inside THIS task (the SDK's auth flow sits in the transport), so a
- # task-local ContextVar reaches it; it stays set for the task's life — every later
- # revival of a once-parked server is unattended too. Left interactive, an expired
- # refresh token opened a new authorize tab every _PARKED_RETRY_INTERVAL, all night.
- if outcome == "self-probe":
- from tools.mcp_oauth import _oauth_interactive_enabled
- _oauth_interactive_enabled.set(False)
- logger.debug("MCP server '%s': attempting revival %s (%s); rebuilding transport.",
- self.name, revival_reason, outcome)
- return False
async def _prepare_run(self, config: dict) -> bool:
"""Bind config, build sampling/elicitation handlers, validate HTTP. False when the server