upstream: re-initialize when the server expired our session (404) - #20
Merged
Conversation
The retry-once path only covered locally dropped transports. When an upstream expires or forgets the streamable-HTTP session server-side (restart, TTL — hit in production with Publora), the SDK client still looks connected, the server answers 404 "Unknown or expired MCP session", and the gateway propagated that error to the caller. Per the MCP spec a 404 on an existing session means re-initialize: the connection now detects it (StreamableHTTPError code 404, plus a message fallback for untyped rethrows), drops the pooled client without triggering backoff, reconnects and retries the call exactly once. Genuine tool errors still pass through untouched. Integration test runs a real StreamableHTTPServerTransport backend whose session store is cleared mid-test to reproduce the failure. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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
Production bug hit via the claude.ai connector: a
publoratool call returnedUpstream "publora" call failed: … {"error":"Unknown or expired MCP session. Re-initialize…"}and kept failing on every retry from the client side.Root cause:
UpstreamConnection.callTool's retry-once path only covered locally dropped transports (if (this.connected) throw err). When the upstream expires the streamable-HTTP session server-side (restart, TTL), the local SDK client still looks connected, the server answers404, and the gateway propagated the error to the caller — who has no way to fix it, since only the gateway owns the upstream session.Per the MCP spec, a 404 on an existing session means "re-initialize". The connection now:
StreamableHTTPErrorwith code 404, plus an "unknown or expired MCP session / session not found" message fallback for untyped rethrows),Genuine tool errors still pass through untouched — no needless session churn.
Test plan
connection.test.ts): realStreamableHTTPServerTransportbackend; clearing its session store mid-test reproduces the 404; the nextcallTooltransparently re-initializes (assert a secondinitializearrived) and succeeds; unknown-tool errors don't trigger re-initnpm test— 214 passed,npm run buildclean🤖 Generated with Claude Code