Repository navigation
feat(mcp): reconnect client session on connection drop (#177) - #191
Merged
Merged
Conversation
When a remote MCP server's session drops mid-session (server restart loses its session map, returns 404 "session not found", or closes the transport), the harness now transparently re-establishes the session and retries the call once, instead of hard-failing every subsequent MCP call and requiring a full session restart. The reconnect lives on *Server (which owns the session lifecycle and now retains the ServerConfig + httpClient + diag to rebuild the transport). remoteTool holds *Server instead of a raw *ClientSession and calls through Server.withSession — the single retry shape shared by tool.Execute, readResource, and getPrompt. A connection-drop error class (isConnectionDrop: errors.Is the SDK's ErrConnectionClosed/ErrSessionMissing plus string-match for the unexported sentinels) triggers exactly one serialized reconnect (N concurrent failing calls produce ONE dial via a stale-session CAS under s.mu), then one retry. A permanently-down server surfaces a clear "unavailable after reconnect" tool error, never a raw transport string. Close is terminal (a separate `closed` flag): a post-close or racing in-flight call never dials and cannot resurrect a closed server. A breaker is deliberately deferred (ADR 0056): the one-attempt bound already prevents masking a permanently-down server; add a breaker only if field data shows flapping-server latency. Tests: restartableServer fixture (stable-URL listener swap) covering reconnect-after-restart, permanently-down clear error (both reject-404 and dial-failure paths), concurrent-calls-reconnect-once (CAS-skip is load-bearing), call-after-close-does-not-dial, readResource/getPrompt reconnect, reconnect-then-retry-fails-non-drop, IsError-does-not-trigger- reconnect, and the classifier table. All pass under -race -count=3. Docs: ADR 0056 (frozen), PRODUCTION-READINESS reliability row, architecture/extensibility.md one-liner, llms.txt regenerated. Closes #177
8 tasks
CI failed TestPermanentlyDownServerDialFailureClearError on Linux: a
hard-stopped server yields a TCP reset ("EOF") rather than "connection
refused", and the SDK wraps it as "rejected by transport" (its internal
ErrRejected marker for a transient transport failure). Neither was in
connectionDropSignatures, so isConnectionDrop returned false → no reconnect
→ the raw transport string leaked to the model (the spec's clear-error
requirement was violated on CI).
Both are genuine connection drops: the session is unusable and a reconnect
is the right response. Add them to the classifier. Table test gains the
exact CI error string as a case so the regression is pinned.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #177.
When a remote MCP server's session drops mid-session — the server restarts (losing its in-memory session map), returns HTTP 404 "session not found", or closes the transport — the harness now transparently re-establishes the session and retries the call once, instead of hard-failing every subsequent MCP call and requiring a full session restart.
The design
*Server, which owns the session lifecycle and now retains theServerConfig+httpClient+diagneeded to rebuild the transport (dialextracted fromConnect).remoteToolholds*Server(not a raw*ClientSession) and calls throughServer.withSession— the single retry shape shared bytool.Execute,readResource, andgetPrompt.isConnectionDrop):errors.Isthe SDK'sErrConnectionClosed/ErrSessionMissingplus string-match for the unexported sentinels ("session not found","client is closing","connection closed","connection refused").s.muvia a stale-session CAS — N concurrent failing calls produce exactly ONE dial."unavailable after reconnect"tool error, never a raw transport string.Closeis terminal (a separateclosedflag, distinct fromdroppedthe retry flag): a post-close or racing in-flight call never dials and cannot resurrect a closed server.Review
Ran the dev-pipeline: architect → implementer → panel review (spec + standards + secure-code-reviewer + software-architect + QA) → iterate → commit. Two cross-confirmed ship-blockers from the panel were fixed in iteration 2:
Closenot terminal (secure-code-reviewer + software-architect): a racing in-flight call could resurrect a closed server. Fixed by splittingdropped(clearable retry flag) fromclosed(terminal Close-only flag), with a top-of-reconnectcheck.errReconnectFailedsentinel mapped to the clear message at bothtool.ExecuteandreadResourceTool.Execute.QA test-adequacy gaps closed:
readResource/getPromptreconnect tests, a load-bearing CAS-skip concurrency test,TestCallAfterCloseDoesNotDial, reconnect-then-retry-fails-non-drop,IsError-doesn't-trigger-reconnect, and diag-count asserts on the permanently-down test.Files
internal/adapter/mcp/reconnect.go(new) — classifier,liveSession,reconnect(stale-session CAS + terminalclosedcheck),withSession, sentinels.internal/adapter/mcp/mcp.go—Serverretainscfg/diag/httpClient+mu/dropped/closed;dialextracted;Closesetsclosed.internal/adapter/mcp/tool.go—remoteToolholds*Server;Executeroutes throughwithSession.internal/adapter/mcp/resource.go/prompt.go—readResource/getPromptviawithSession.internal/adapter/mcp/restool.go— clear-error mapping onreadResourceTool.Execute.internal/adapter/mcp/reconnect_test.go(new) — restartableServer fixture + 9 tests.docs/adr/0056-mcp-client-reconnect.md(new),docs/design/PRODUCTION-READINESS.md,docs/architecture/extensibility.md,llms.txt(regenerated).Verification
task lint && task testgreen (full suite, -race, api:check, docs:check).go test -race -count=3 ./internal/adapter/mcp/...green.go run ./cmd/mecademoprints a full offline session.task generaterun;llms.txtregenerated.Checklist
internal/adapter/mcpimports onlyengine/port,engine/session,engine/tool+ SDK + stdlib)port.Diagnostics(nosloginengine/orinternal/)httptest+ in-process MCP server);-racecleanllms.txt)