Skip to content

MCP client: reconnect/restart session on connection drop instead of hard-failing #177

Closed

Description

@JAORMX

The problem

When an MCP client connection times out or drops mid-session, the tool call fails hard with no recovery attempt:

mcp call failed: connection closed: calling "tools/call": client is closing:
sending "tools/call": failed to connect (session ID: ...): session not found

Once this happens, every subsequent MCP tool call in the same session also fails — the connection is dead and nothing attempts to re-establish it. The operator must restart the entire session to recover MCP tool access.

Expected behavior

When the MCP client detects a connection drop / session-not-found / client-closing error, it should attempt to recreate or restart the MCP session (reconnect, re-handshake, re-establish the tool session) and retry the call (or at least make the reconnected session available for the next call) rather than simply failing.

This is the standard resilient-client pattern: transient transport failures should be retried after reconnection, not surfaced as permanent failures. The harness already has retry/breaker patterns for LLM provider calls (llmresilience); the MCP client should have an analogous reconnection path.

Scope

  • Detect the "session not found" / "client is closing" / connection-closed error class on an MCP tool call.
  • Attempt to re-establish the MCP session (reconnect + re-handshake).
  • Retry the original call on the fresh session (bounded retry, e.g. 1-2 attempts).
  • If reconnection fails, surface a clear error to the model/operator (not a raw transport error).
  • Do NOT retry indefinitely or mask a permanently-down MCP server — a bounded retry with a clear terminal error is the goal.

Out of scope

  • MCP server-side resilience (that's the server's job).
  • Changes to the MCP protocol or the streaming-HTTP transport decision (ADR: no stdio MCP, ever).

Priority

Low — this is an operational annoyance, not a correctness bug. The workaround (restart the session) works. File this for after the guardrails rework (#168-#174) lands.

Activity

  1. JAORMX commented on Jun 24, 2026

    @JAORMX
    ContributorAuthor

    Investigation notes

    The MCP tool adapter (internal/adapter/mcp/tool.go) holds a *mcpsdk.ClientSession directly on the remoteTool struct. When CallTool fails with a "session not found" / "client is closing" error, it's surfaced to the model as a tool error (session.NewToolError), but no reconnection is attempted.

    The reconnection needs to happen at the MCP manager level (which owns the transport and the session lifecycle), not at the remoteTool level (which just holds a pointer). The remoteTool.Execute would need to detect the error class, signal the manager to reconnect, and retry on the fresh session.

    Key files:

    • internal/adapter/mcp/tool.go — remoteTool.Execute (line 99), the t.session.CallTool call (line 110).
    • internal/adapter/mcp/mcp.go — the MCP manager (session lifecycle, transport).
    • The mcpsdk.ClientSession type (from github.com/modelcontextprotocol/go-sdk/mcp) — need to check if it exposes a reconnection API or if the transport must be rebuilt.

    This is a non-trivial change that needs architectural investigation (where does reconnection live? does the SDK support it? is there a breaker?) before implementation. Leaving as tracked-but-deferred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions