nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Feature] MCP: retry an unhealthy server and restore its tools when it answers again

Open addyCooks opened this issue 1 month ago • 1 comments

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 connected and register the server's tools again (or call connectToServer).
  • Treat an MCP error reply to a ping as "alive".
  • Optional: add /mcp reconnect <name> for a manual retry.
  • Update the healthCheckInterval line in the docs, and add tests for recovery after a failed ping.

Alternatives Considered

  • Set healthCheckInterval to 0. 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)

addyCooks avatar Oct 08 '26 16:10 addyCooks

Here is my plan to implement this feature let me know what you think!

Implementation Plan

  1. 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 base healthCheckInterval up 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 emit onHealthChange({ serverName, status: 'connected' }).
  2. Tool Restoration in ToolManager (source/tools/tool-manager.ts):

    • In onHealthChange, handle change.status === 'connected':
      • Re-register the server's tools into the active registry (or add to mcpCatalog when on-demand discovery is active).
  3. 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>.
  4. 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).

rishu685 avatar Oct 08 '26 17:10 rishu685