fix(mcp): reconnect stale server entries - #37772
Conversation
tonydwb
left a comment
There was a problem hiding this comment.
Code Review Summary
Verdict: Approved
Overview
Fixes MCP server reconnection: when an MCP server entry becomes stale (connection dropped), the agent now properly reconnects instead of silently using a dead session.
✅ Looks Good
- Small, focused fix: 33 additions, 3 deletions
- Addresses real failure mode in long-running gateway sessions
- Properly detects and re-establishes stale MCP connections
- No security concerns
- The fix is defensive: handles edge case without breaking existing reconnect logic
Reviewed by Hermes Agent
|
The PR implements the same fix as #37899 — reconnecting servers whose 1. TOCTOU / duplicate-task risk # Cancel lingering tasks for stale entries
for k in list(_servers.keys()):
srv = _servers[k]
if getattr(srv, "session", None) is None:
old_task = getattr(srv, "_task", None)
if old_task and not old_task.done():
old_task.cancel()
del _servers[k]2. Stale-task cleanup 3. Test coverage If you'd like, I can help back-port the cleanup code and the extra test from #37899. Let me know! |
register_mcp_servers now nudges cached entries whose session is None via _signal_reconnect, so a new agent session recovers a parked server immediately instead of waiting up to _PARKED_RETRY_INTERVAL for the next self-probe (#50170). Gate-check idea credit: @izumi0uu (#50184), @LeonSGP43 (#37772), @Tranquil-Flow (#37899).
register_mcp_servers now nudges cached entries whose session is None via _signal_reconnect, so a new agent session recovers a parked server immediately instead of waiting up to _PARKED_RETRY_INTERVAL for the next self-probe (#50170). Gate-check idea credit: @izumi0uu (#50184), @LeonSGP43 (#37772), @Tranquil-Flow (#37899).
|
Closing with credit — you were the earliest (Jun 3, 01:27) to propose rediscovering stale entries in the register gate. The design went a different way: the run task now never exits (parks + self-probes, PR #59222), so respawning a duplicate task would race the always-alive one. Instead, |
register_mcp_servers now nudges cached entries whose session is None via _signal_reconnect, so a new agent session recovers a parked server immediately instead of waiting up to _PARKED_RETRY_INTERVAL for the next self-probe (NousResearch#50170). Gate-check idea credit: @izumi0uu (NousResearch#50184), @LeonSGP43 (NousResearch#37772), @Tranquil-Flow (NousResearch#37899).
register_mcp_servers now nudges cached entries whose session is None via _signal_reconnect, so a new agent session recovers a parked server immediately instead of waiting up to _PARKED_RETRY_INTERVAL for the next self-probe (NousResearch#50170). Gate-check idea credit: @izumi0uu (NousResearch#50184), @LeonSGP43 (NousResearch#37772), @Tranquil-Flow (NousResearch#37899).
register_mcp_servers now nudges cached entries whose session is None via _signal_reconnect, so a new agent session recovers a parked server immediately instead of waiting up to _PARKED_RETRY_INTERVAL for the next self-probe (NousResearch#50170). Gate-check idea credit: @izumi0uu (NousResearch#50184), @LeonSGP43 (NousResearch#37772), @Tranquil-Flow (NousResearch#37899).
register_mcp_servers now nudges cached entries whose session is None via _signal_reconnect, so a new agent session recovers a parked server immediately instead of waiting up to _PARKED_RETRY_INTERVAL for the next self-probe (NousResearch#50170). Gate-check idea credit: @izumi0uu (NousResearch#50184), @LeonSGP43 (NousResearch#37772), @Tranquil-Flow (NousResearch#37899).
Summary
_servers[name]entry exists but itssessionhas already dropped toNoneregister_mcp_servers()from treating disconnected entries as healthy, which avoids exposing handlers that only return"MCP server '<name>' is not connected"Fixes #37768.
Verification
/Users/leongong/Desktop/LeonProjects/worktrees/hermes-agent/.base/.venv/bin/pytest -o addopts='' tests/tools/test_mcp_tool.py -q -k 'TestRegisterMcpServers'/Users/leongong/Desktop/LeonProjects/worktrees/hermes-agent/.base/.venv/bin/ruff check tools/mcp_tool.py tests/tools/test_mcp_tool.pygit diff --check