fix(tui): suppress MCP discovery in slash_worker to prevent duplicate serve children (#15275)

The slash_worker creates a HermesCLI which imports model_tools, triggering
discover_mcp_tools() at module scope.  Meanwhile, the TUI server also calls
MCP discovery independently.  Both paths spawn ``hermes mcp serve`` child
processes per session.

Fix: defer the cli import in slash_worker to main() and set
HERMES_MCP_DISCOVERY=0 beforehand.  model_tools now checks this env var
and skips MCP discovery when suppressed.
This commit is contained in:
Tranquil-Flow 2026-04-25 08:56:52 +10:00
parent 00c3d848d8
commit c8e3c02c2b
3 changed files with 124 additions and 13 deletions

View file

@ -1,6 +1,12 @@
"""Persistent slash-command worker one HermesCLI per TUI session.
"""Persistent slash-command worker -- one HermesCLI per TUI session.
Protocol: reads JSON lines from stdin {id, command}, writes {id, ok, output|error} to stdout.
The slash_worker only needs CLI command processing, not MCP tool bootstrapping.
The TUI server already handles MCP discovery and spawns hermes mcp serve
children; without the HERMES_MCP_DISCOVERY guard both code paths independently
spawn duplicate MCP serve processes per session (#15275). The cli module is
imported lazily inside main() so the env var is set before model_tools runs.
"""
import argparse
@ -10,12 +16,14 @@ import json
import os
import sys
import cli as cli_mod
from cli import HermesCLI
from rich.console import Console
# cli module reference -- populated lazily in main() after setting the
# HERMES_MCP_DISCOVERY env var to prevent duplicate MCP serve children.
_cli_mod = None
def _run(cli: HermesCLI, command: str) -> str:
def _run(cli, command: str) -> str:
cmd = (command or "").strip()
if not cmd:
return ""
@ -29,21 +37,23 @@ def _run(cli: HermesCLI, command: str) -> str:
# underlying file to our buffer so self.console.print() is captured.
cli.console = Console(file=buf, force_terminal=True, width=120)
old = getattr(cli_mod, "_cprint", None)
old = getattr(_cli_mod, "_cprint", None)
if old is not None:
cli_mod._cprint = lambda text: print(text)
_cli_mod._cprint = lambda text: print(text)
try:
with contextlib.redirect_stdout(buf), contextlib.redirect_stderr(buf):
cli.process_command(cmd)
finally:
if old is not None:
cli_mod._cprint = old
_cli_mod._cprint = old
return buf.getvalue().rstrip()
def main():
global _cli_mod
p = argparse.ArgumentParser(add_help=False)
p.add_argument("--session-key", required=True)
p.add_argument("--model", default="")
@ -52,6 +62,16 @@ def main():
os.environ["HERMES_SESSION_KEY"] = args.session_key
os.environ["HERMES_INTERACTIVE"] = "1"
# Suppress MCP discovery -- the TUI server already handles MCP servers;
# importing cli triggers model_tools which calls discover_mcp_tools() at
# module scope. This env var tells model_tools to skip that step.
os.environ.setdefault("HERMES_MCP_DISCOVERY", "0")
import cli as cli_mod
from cli import HermesCLI
_cli_mod = cli_mod
with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
cli = HermesCLI(model=args.model or None, compact=True, resume=args.session_key, verbose=False)