Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added infographic/mcp-ws-discovery/infographic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions tests/test_tui_gateway_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,46 @@
import threading
import time

from hermes_cli import mcp_startup
from tui_gateway import server
from tui_gateway import ws as ws_mod


def test_ws_startup_starts_background_mcp_discovery(monkeypatch):
"""The desktop app and dashboard chat reach the agent through this WS
sidecar, not through tui_gateway.entry.main() (which spawns the discovery
thread for the stdio TUI). handle_ws must start discovery itself, otherwise
_make_agent's wait_for_mcp_discovery no-ops and the agent snapshots an
MCP-less tool list. Regression test for #38945."""
calls = []
monkeypatch.setattr(
mcp_startup,
"start_background_mcp_discovery",
lambda **kw: calls.append(kw),
)

class FakeWS:
async def accept(self):
pass

async def send_text(self, line):
pass

async def receive_text(self):
raise ws_mod._WebSocketDisconnect()

async def close(self):
pass

server._sessions.clear()
try:
asyncio.run(ws_mod.handle_ws(FakeWS()))
finally:
server._sessions.clear()

assert calls == [{"logger": ws_mod._log, "thread_name": "tui-ws-mcp-discovery"}]


def _run_disconnect(monkeypatch, seed):
"""Drive handle_ws to its disconnect `finally`, seeding sessions against the
live WSTransport the moment it exists. Returns nothing; inspect _sessions."""
Expand Down
16 changes: 16 additions & 0 deletions tui_gateway/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ async def handle_ws(ws: Any) -> None:

transport = WSTransport(ws, asyncio.get_running_loop(), peer=peer)

# The desktop app and dashboard chat reach the agent through this WS
# sidecar, NOT through tui_gateway.entry.main() (the stdio TUI path that
# spawns the background MCP discovery thread). Without starting it here,
# discovery never runs in this process: _make_agent only *waits* on the
# thread (wait_for_mcp_discovery), which no-ops when it was never
# created, so the agent snapshots an MCP-less tool list and the only way
# to surface MCP tools is a manual /reload-mcp. Start it once per
# process here (idempotent, config-gated) before gateway.ready so the
# first agent build can pick up already-spawning servers. (#38945)
from hermes_cli.mcp_startup import start_background_mcp_discovery

start_background_mcp_discovery(
logger=_log,
thread_name="tui-ws-mcp-discovery",
)

ready_ok = await transport.write_async(
{
"jsonrpc": "2.0",
Expand Down
Loading