The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(dashboard): standalone dashboard starts MCP discovery on first /api/ws client, not at boot
kshitijk4poorb47abc36f91d
diff --git a/hermes_cli/main.py b/hermes_cli/main.pyindex 82141b3376c..12f8013cc41 100644--- a/hermes_cli/main.py+++ b/hermes_cli/main.py@@ -2633,15 +2633,28 @@ def _dashboard_prepare_runtime(args, headless_backend) -> bool:# ~350ms `mcp` SDK import, which holds the GIL against the web_server# import and delays the READY sentinel; _make_agent's bounded# wait_for_mcp_discovery covers a server still connecting at first turn.- mcp_discovery_after_bind = headless_backend and os.environ.get("HERMES_DESKTOP") == "1"+ # A standalone (non-Desktop) dashboard may sit idle and unvisited for days+ # (#58733): it arms discovery instead and the first /api/ws client fires it.+ desktop = os.environ.get("HERMES_DESKTOP") == "1"+ mcp_discovery_after_bind = headless_backend and desktopif not mcp_discovery_after_bind:try:- from hermes_cli.mcp_startup import start_background_mcp_discovery-- start_background_mcp_discovery(- logger=logger,- thread_name="dashboard-mcp-discovery",+ from hermes_cli.mcp_startup import (+ defer_background_mcp_discovery,+ start_background_mcp_discovery,)++ if desktop:+ start_background_mcp_discovery(+ logger=logger,+ thread_name="dashboard-mcp-discovery",+ )+ else:+ defer_background_mcp_discovery(+ logger=logger,+ thread_name="dashboard-mcp-discovery",+ delay=None,+ )except Exception:logger.debug("Background MCP tool discovery failed at dashboard startup",diff --git a/hermes_cli/mcp_startup.py b/hermes_cli/mcp_startup.pyindex cfe1b6ac6d8..768d981aac6 100644--- a/hermes_cli/mcp_startup.py+++ b/hermes_cli/mcp_startup.py@@ -177,12 +177,16 @@ def _discover_mcp_tools_without_interactive_oauth() -> None:discover_mcp_tools(allowed_mcp_names=_mcp_server_filter)-def defer_background_mcp_discovery(*, logger, thread_name: str, delay: float) -> None:+def defer_background_mcp_discovery(*, logger, thread_name: str, delay: float | None) -> None:"""Arm ``start_background_mcp_discovery`` to run ``delay`` seconds from now.Used by the Desktop ``serve`` backend after its socket is announced: the thread's first act isthe ~350ms ``mcp`` SDK import, which would hold the GIL against the renderer's connect + firsthydration reads (or the web_server import) if started earlier.++ ``delay=None`` arms without a clock: the standalone dashboard fires it from the first ``/api/ws``+ client (``start_deferred_mcp_discovery_now``) or the first agent build (``wait_for_mcp_discovery``),+ so an idle, unvisited dashboard never spawns the configured stdio MCP servers (#58733)."""global _mcp_discovery_deferredwith _mcp_discovery_lock:@@ -195,14 +199,15 @@ def defer_background_mcp_discovery(*, logger, thread_name: str, delay: float) ->_mcp_discovery_deferred = Nonestart_background_mcp_discovery(logger=logger, thread_name=thread_name)- timer = threading.Timer(delay, _fire)+ timer = threading.Timer(delay or 0, _fire)timer.daemon = Truetimer.name = f"{thread_name}-deferred"_mcp_discovery_deferred = timer- timer.start()+ if delay is not None:+ timer.start()-def _start_deferred_mcp_discovery_now() -> None:+def start_deferred_mcp_discovery_now() -> None:"""Run an armed deferred start immediately (idempotent, thread-safe)."""with _mcp_discovery_lock:timer = _mcp_discovery_deferred@@ -219,7 +224,7 @@ def wait_for_mcp_discovery(timeout: "float | None" = None, *, single_query: boolserver's real connect time. ``single_query`` uses ``mcp_single_query_discovery_timeout``(15s vs 1.5s) because one-shot sessions have no second turn to recover."""- _start_deferred_mcp_discovery_now()+ start_deferred_mcp_discovery_now()thread = _current_home_thread()if thread is None or not thread.is_alive():returndiff --git a/hermes_cli/web_routers/chat_ws.py b/hermes_cli/web_routers/chat_ws.pyindex cea13905786..d2faadc18eb 100644--- a/hermes_cli/web_routers/chat_ws.py+++ b/hermes_cli/web_routers/chat_ws.py@@ -574,8 +574,12 @@ async def pty_ws(ws: WebSocket) -> None:async def gateway_ws(ws: WebSocket) -> None:if not await _close_unless_sidecar_allowed(ws):return+ from hermes_cli.mcp_startup import start_deferred_mcp_discovery_nowfrom tui_gateway.ws import handle_ws+ # First chat client of a standalone dashboard: fire the discovery armed at boot (no-op otherwise).+ start_deferred_mcp_discovery_now()+# The authenticated identity (ticket / internal credential) stamped by# _ws_auth_reason becomes the identity authority for privileged RPCs# (browser.controller.register). None on the legacy token path.diff --git a/tests/hermes_cli/test_serve_mcp_discovery_after_bind.py b/tests/hermes_cli/test_serve_mcp_discovery_after_bind.pyindex e2fb041971c..3f56679de72 100644--- a/tests/hermes_cli/test_serve_mcp_discovery_after_bind.py+++ b/tests/hermes_cli/test_serve_mcp_discovery_after_bind.py@@ -8,7 +8,9 @@ import path and delaying the READY sentinel the Desktop waits on.from __future__ import annotationsimport logging+import sysimport threading+import typesimport hermes_cli.mcp_startup as mcp_startupimport hermes_cli.web_server as web_server@@ -66,7 +68,66 @@ def test_deferred_discovery_fires_once_and_is_idempotent(monkeypatch):mcp_startup.defer_background_mcp_discovery(logger=log, thread_name="t", delay=60)mcp_startup.defer_background_mcp_discovery(logger=log, thread_name="t", delay=60) # second arm is a no-opfirst = mcp_startup._mcp_discovery_deferred- mcp_startup._start_deferred_mcp_discovery_now()- mcp_startup._start_deferred_mcp_discovery_now()+ mcp_startup.start_deferred_mcp_discovery_now()+ mcp_startup.start_deferred_mcp_discovery_now()assert calls == ["t"]assert first is not None and mcp_startup._mcp_discovery_deferred is None+++def _stub_dashboard_runtime(monkeypatch):+ import hermes_cli.main as main_mod++ monkeypatch.setattr(main_mod, "_resolve_dashboard_web_dist", lambda *a, **k: None)+ monkeypatch.setattr(main_mod, "_sync_bundled_skills_quietly", lambda: None)+ monkeypatch.setitem(sys.modules, "fastapi", types.SimpleNamespace())+ monkeypatch.setitem(sys.modules, "uvicorn", types.SimpleNamespace())+ monkeypatch.setitem(sys.modules, "hermes_logging", types.SimpleNamespace(setup_logging=lambda **_k: None))+ monkeypatch.setitem(sys.modules, "hermes_cli.plugins", types.SimpleNamespace(discover_plugins=lambda: None))+ return main_mod+++def test_standalone_dashboard_boot_arms_discovery_without_starting_it(monkeypatch):+ """#58733: an idle, unvisited `hermes dashboard` must not spawn the configured MCP servers."""+ _reset_discovery_state(monkeypatch)+ monkeypatch.delenv("HERMES_DESKTOP", raising=False)+ main_mod = _stub_dashboard_runtime(monkeypatch)+ calls: list[str] = []+ monkeypatch.setattr(+ mcp_startup, "start_background_mcp_discovery", lambda *, logger, thread_name: calls.append(thread_name)+ )++ after_bind = main_mod._dashboard_prepare_runtime(types.SimpleNamespace(skip_build=True), False)++ assert after_bind is False and calls == []+ armed = mcp_startup._mcp_discovery_deferred+ assert isinstance(armed, threading.Timer) and not armed.is_alive()+++def test_first_gateway_ws_client_starts_the_armed_discovery_once(monkeypatch):+ import asyncio++ import hermes_cli.web_routers.chat_ws as chat_ws++ _reset_discovery_state(monkeypatch)+ calls: list[str] = []+ monkeypatch.setattr(+ mcp_startup, "start_background_mcp_discovery", lambda *, logger, thread_name: calls.append(thread_name)+ )+ mcp_startup.defer_background_mcp_discovery(+ logger=logging.getLogger("test"), thread_name="dashboard-mcp-discovery", delay=None+ )+ assert calls == []++ async def _allowed(ws):+ return True++ async def _handle_ws(ws, **kwargs):+ return None++ monkeypatch.setattr(chat_ws, "_close_unless_sidecar_allowed", _allowed)+ monkeypatch.setattr("tui_gateway.ws.handle_ws", _handle_ws)+ asyncio.run(chat_ws.gateway_ws(object()))+ asyncio.run(chat_ws.gateway_ws(object()))++ assert calls == ["dashboard-mcp-discovery"]+ assert mcp_startup._mcp_discovery_deferred is None