Skip to content

The agent that grows with you

perf(cli): a chat client no longer spawns the MCP servers its gateway owns

Teknium4a10a108d542
diff --git a/hermes_cli/main.py b/hermes_cli/main.py
index 2fd07c56fc2..e89cf32753d 100644
--- a/hermes_cli/main.py
+++ b/hermes_cli/main.py
@@ -2907,6 +2907,14 @@ def _should_background_mcp_startup(args) -> bool:
return not _is_tui_chat_launch(args) and args.command in {None, "chat", "rl"}
+def _chat_runs_at_gateway(args) -> bool:
+ """Every ``hermes`` / ``hermes chat`` turn (classic view, ``-q``, ``-z``, TUI) executes in the
+ gateway, which discovers and owns MCP servers itself. Discovery in this client only spawns a
+ second copy of every server and holds the cross-process discovery lock the cold-starting gateway
+ then waits on (measured 2.0 s with one stdio server) for tools nothing here ever calls."""
+ return args.command in {None, "chat"}
+
+
def _prepare_agent_startup(args) -> None:
"""Discover plugins/MCP/hooks for commands that can run an agent turn."""
# --yolo chokepoint: HERMES_YOLO_MODE must be set before any discovery
@@ -2955,9 +2963,11 @@ def _prepare_agent_startup(args) -> None:
logger.debug("MCP server filter setup failed", exc_info=True)
# TUI launches hand off to a startup path that backgrounds MCP discovery
- # with a bounded join; acp/gateway/cron do their own on the runtime path.
+ # with a bounded join; acp/gateway/cron do their own on the runtime path;
+ # chat turns run in the gateway, which owns MCP for them.
_run_inline_mcp_discovery = not (
_is_tui_chat_launch(args) or _command_has_dedicated_mcp_startup(args)
+ or _chat_runs_at_gateway(args)
)
if _run_inline_mcp_discovery and _should_background_mcp_startup(args):
try:
diff --git a/tests/hermes_cli/test_mcp_startup.py b/tests/hermes_cli/test_mcp_startup.py
index 2e6e0f295ed..4f64730553c 100644
--- a/tests/hermes_cli/test_mcp_startup.py
+++ b/tests/hermes_cli/test_mcp_startup.py
@@ -44,7 +44,9 @@ def _agent_args(**overrides) -> Namespace:
return Namespace(**base)
-def test_prepare_agent_startup_backgrounds_blocking_mcp_for_chat(monkeypatch):
+def test_prepare_agent_startup_backgrounds_blocking_mcp_for_rl(monkeypatch):
+ """``hermes rl`` builds its agent in this process: discovery runs off-thread, never blocking.
+ (Chat turns run in the gateway; see the next test.)"""
stop = threading.Event()
calls = {"mcp": 0}
@@ -88,7 +90,7 @@ def test_prepare_agent_startup_backgrounds_blocking_mcp_for_chat(monkeypatch):
try:
start = time.monotonic()
- main_mod._prepare_agent_startup(_agent_args())
+ main_mod._prepare_agent_startup(_agent_args(command="rl"))
elapsed = time.monotonic() - start
assert elapsed < 2.0
deadline = time.monotonic() + 3.0
@@ -102,6 +104,22 @@ def test_prepare_agent_startup_backgrounds_blocking_mcp_for_chat(monkeypatch):
stop.set()
[email protected]("command", [None, "chat"])
+def test_chat_client_never_spawns_mcp_servers_the_gateway_owns(monkeypatch, command):
+ """A chat/`-q` client only proxies turns to the gateway: spawning every MCP server here doubled
+ them and held the discovery lock the cold-starting gateway waited on before READY."""
+ calls = {"background": 0, "inline": 0}
+ monkeypatch.setattr(main_mod, "_resolve_use_tui", lambda _args: False)
+ monkeypatch.setattr(mcp_startup, "start_background_mcp_discovery",
+ lambda **_kwargs: calls.__setitem__("background", calls["background"] + 1))
+ monkeypatch.setitem(sys.modules, "hermes_cli.plugins", types.SimpleNamespace(
+ discover_plugins=lambda: None, start_background_plugin_discovery=lambda: None))
+ monkeypatch.setitem(sys.modules, "tools.mcp_tool_discovery", types.SimpleNamespace(
+ discover_mcp_tools=lambda **_k: calls.__setitem__("inline", calls["inline"] + 1)))
+ main_mod._prepare_agent_startup(_agent_args(command=command))
+ assert calls == {"background": 0, "inline": 0}
+
+
def test_prepare_agent_startup_skips_discovery_when_chat_resolves_to_tui(
monkeypatch,
):