fix(client): handle HTTP 410 Gone for expired/stale MCP sessions #1402
+133
−0
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
This PR adds proper handling for HTTP 410 Gone responses in
StreamableHTTPClientTransport, enabling seamless reconnection when MCP sessions expire or servers restart.Problem
When an MCP server restarts or a session times out, the server returns HTTP 410 Gone to indicate the session ID is no longer valid. Currently, the client:
_sessionIdThis creates a poor user experience where MCP connections become permanently broken after server restarts, requiring users to manually restart their client applications.
Real-world scenario
Solution
Handle 410 responses by:
_sessionIdThis follows the same pattern already used for 401 (auth) and 403 (scope) responses.
After this fix
Changes
send()method: Added 410 handling that clears session ID and retries POST requests_startOrAuthSse()method: Added 410 handling that clears session ID and retries SSE GET connectionsTesting
All existing tests pass, plus 2 new tests:
should clear session ID and retry on 410 during POST requestshould clear session ID and retry on 410 during SSE GET requestRelated
This complements the server-side behavior where servers should return 410 Gone for expired sessions, as documented in the MCP Streamable HTTP transport specification.