[Feature] MCP: retry an unhealthy server and restore its tools when it answers again
Description
When an MCP server misses a single health ping, Nanocoder marks it unhealthy and removes its tools. It never pings it again, so the tools stay gone even if the server answers fine a moment later.
This was a deliberate choice in #1569, which closed #1521: "Automatic server restart is intentionally not included. Unhealthy servers can be restored by reinitializing MCP servers or restarting Nanocoder." docs/configuration/mcp-configuration.md:203 documents it the same way. This issue asks to reconsider it with a smaller scope than a restart: keep pinging the server and restore its tools when it responds. The server process is not restarted.
In source/mcp/mcp-client.ts, check() (lines 158-176) returns early unless the status is connected, and only the first connect sets that status. markServerUnhealthy (line 140) clears the tools, and ToolManager unregisters them (source/tools/tool-manager.ts:181).
Use Case
A server can miss one ping without being dead:
- A laptop sleeps and resumes (HTTP servers).
- A stdio server is busy with a tool call that takes longer than the 10 second ping timeout.
The tools then stay missing until Nanocoder is restarted. This hurts most in nanocoder daemon, which can run for days. Today any ping error counts as a dead server, even a JSON-RPC error reply such as -32601, which actually shows the server is alive.
Proposed Solution
- Keep pinging unhealthy servers, with exponential backoff.
- When a ping succeeds, set the status back to
connectedand register the server's tools again (or callconnectToServer). - Treat an MCP error reply to a ping as "alive".
- Optional: add
/mcp reconnect <name>for a manual retry. - Update the
healthCheckIntervalline in the docs, and add tests for recovery after a failed ping.
Alternatives Considered
- Set
healthCheckIntervalto0. This turns off periodic pings, but a transport error still triggers a ping, and a failed ping still removes the tools. - Restart Nanocoder after every hiccup. This is the documented workaround today.
- Restart the server process automatically. #1569 chose not to do this, so this issue does not ask for it.
Additional Context
I reproduced this with a small stdio MCP server that ignores its first ping and answers every later one. It was connected with healthCheckInterval: 500 and timeout: 1000:
[0.3s] connected=true tools=1
[1.9s] health event: unhealthy (MCP error -32001: Request timed out)
[6.3s] connected=false tools=0
[6.3s] callTool error: MCP tool not found: flaky_echo
[6.4s] manual ping to the same server now: OK {}
The server was healthy again after about 2 seconds, but its tools were never restored. Tested on Windows 11, Node 22.22.3, Nanocoder 1.31.0 (main at 5c2e693).
Related: #1521 (original request, closed), #1569 (health checks, merged).
- [x] I have searched existing issues to ensure this is not a duplicate
- [x] This feature aligns with the project's goals (local-first AI assistance)
Here is my plan to implement this feature let me know what you think!
Implementation Plan
-
Persistent Health Checks with Exponential Backoff (
source/mcp/mcp-client.ts):- Continue scheduling health checks for servers in
'unhealthy'state with exponential backoff (e.g., backing off from basehealthCheckIntervalup to a max cap like 60s). - In
check(), treat valid JSON-RPC error responses from the server (e.g.,-32601 Method not found) as alive. - When a ping succeeds on an unhealthy server:
- Refresh/fetch tools (
client.listTools()). - Update internal caches (
serverTools,toolMappingCache). - Transition health status back to
'connected'and emitonHealthChange({ serverName, status: 'connected' }).
- Refresh/fetch tools (
- Continue scheduling health checks for servers in
-
Tool Restoration in ToolManager (
source/tools/tool-manager.ts):- In
onHealthChange, handlechange.status === 'connected':- Re-register the server's tools into the active registry (or add to
mcpCatalogwhen on-demand discovery is active).
- Re-register the server's tools into the active registry (or add to
- In
-
Documentation & Manual Reconnect (
docs/configuration/mcp-configuration.md):- Update documentation to reflect that unhealthy servers are retried automatically in the background and restored upon response.
- (Optional) Support manual trigger if needed via
/mcp reconnect <name>.
-
Automated Tests (
source/mcp/mcp-client.spec.ts&source/tools/tool-manager.spec.ts):- Add unit tests simulating a server that fails an initial ping, transitions to
'unhealthy'(tools removed), and then responds successfully on a subsequent retry (tools restored and status set back to'connected'). - Verify that JSON-RPC error responses (e.g.,
-32601) maintain connected status. - Run full verification suite (
test:ava,test:types,test:format,test:knip,test:security).
- Add unit tests simulating a server that fails an initial ping, transitions to