The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(mcp): a running server adopts a changed config.yaml definition on rebuild
teknium1e8aa69c1a1ba
diff --git a/tests/tools/test_mcp_config_change_rebind.py b/tests/tools/test_mcp_config_change_rebind.pynew file mode 100644index 00000000000..0d1f1fa7c50--- /dev/null+++ b/tests/tools/test_mcp_config_change_rebind.py@@ -0,0 +1,62 @@+"""A remote MCP server definition edited in config.yaml while the process runs must take effect on+the next transport rebuild (#113907): ``run()`` used to keep its start-time dict forever, so a URL+change (catalog migration, dashboard re-auth to a new endpoint) left the loop probing the old URL+and its stale-URL OAuth provider evicted the fresh one built for the new URL."""++import asyncio++import pytest++[email protected]_isolate+def test_reconnect_adopts_changed_url_from_config_yaml(monkeypatch, tmp_path):+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ from tools import mcp_tool_config+ from tools.mcp_tool import MCPServerTask++ old = {"url": "https://mcp.example.test/sse", "auth": "oauth", "oauth": {"client_id": "a"}}+ new = {"url": "https://mcp.example.test/mcp", "auth": "oauth", "oauth": {"client_id": "a"}}+ disk = {"srv": dict(old)}+ monkeypatch.setattr(mcp_tool_config, "_load_mcp_config", lambda: {k: dict(v) for k, v in disk.items()})+ seen: list = []++ class _Task(MCPServerTask):+ async def _prepare_run(self, config):+ self._config = config+ self._auth_type = config["auth"]+ return True++ async def _run_http(self, config):+ seen.append((config["url"], self._config["url"], self._auth_type))+ self._ready.set()+ self.session = object()+ self._session_proven = True+ if len(seen) == 1:+ disk["srv"] = dict(new) # the dashboard saved the migrated definition mid-run+ return "reconnect"+ self._shutdown_event.set()+ return "shutdown"++ async def _scenario():+ task = _Task("srv")+ await asyncio.wait_for(task.run(dict(old)), timeout=5)++ asyncio.run(_scenario())+ assert seen == [(old["url"], old["url"], "oauth"), (new["url"], new["url"], "oauth")]++[email protected]_isolate+def test_unchanged_or_missing_definition_keeps_the_running_config(monkeypatch, tmp_path):+ """No rebuild churn when config.yaml agrees with the running dict (or no longer lists the+ server): the start-time config stays bound byte-for-byte."""+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))+ from tools import mcp_tool_config+ from tools.mcp_tool import MCPServerTask++ cfg = {"url": "https://mcp.example.test/mcp", "headers": {"X": "1"}, "connect_timeout": 5}+ task = MCPServerTask("srv")+ task._config = cfg+ monkeypatch.setattr(mcp_tool_config, "_load_mcp_config", lambda: {"srv": dict(cfg, connect_timeout=9)})+ assert task._refresh_remote_config(cfg) is cfg # non-endpoint keys do not trigger a rebind+ monkeypatch.setattr(mcp_tool_config, "_load_mcp_config", lambda: {})+ assert task._refresh_remote_config(cfg) is cfgdiff --git a/tools/mcp_tool_server_run.py b/tools/mcp_tool_server_run.pyindex 503aeff2be1..3f8b1137159 100644--- a/tools/mcp_tool_server_run.py+++ b/tools/mcp_tool_server_run.py@@ -240,6 +240,28 @@ class MCPServerRunMixin:self._error = excself._ready.set()+ _REMOTE_REBIND_KEYS = ("url", "auth", "oauth", "headers", "transport")++ def _refresh_remote_config(self, config: dict) -> dict:+ """Before rebuilding a remote transport, re-read this server's definition from config.yaml and+ adopt it when the endpoint or its auth changed (#113907). ``run()`` otherwise keeps the dict it+ was started with, so a ``url`` edited while the process runs (dashboard/Desktop re-auth, a+ catalog migration) left the loop probing the OLD URL — and that stale-URL provider evicted the+ fresh one the dashboard had just authorised for the new URL, dropping its tokens/DCR client."""+ if not self._is_http():+ return config+ from tools import mcp_tool_config as _config+ fresh = (_config._load_mcp_config() or {}).get(self.name)+ if not isinstance(fresh, dict) or "url" not in fresh or all(+ fresh.get(k) == config.get(k) for k in self._REMOTE_REBIND_KEYS):+ return config+ logger.info("MCP server '%s': definition changed in config.yaml (%s -> %s); rebuilding with the new one",+ self.name, config.get("url"), fresh.get("url"))+ self._config = fresh+ self._auth_type = (fresh.get("auth") or "").lower().strip()+ self._sse_fallback = False # latched for the old endpoint+ return fresh+async def run(self, config: dict):"""Long-lived: connecting -> connected -> (degraded -> parked -> revived)*. Unproven dropsand transport errors charge a rapid-drop budget with jittered backoff; exhausting it (or@@ -249,8 +271,12 @@ class MCPServerRunMixin:returnself._reconnect_retries = 0budget = _RetryBudget()+ rebuild = Falsewhile True:try:+ if rebuild:+ config = self._refresh_remote_config(config)+ rebuild = Truerun_transport = self._run_http if self._is_http() else self._run_stdioif not await self._on_clean_return(await run_transport(config), budget):break