Restore negotiated protocol version on reconnection transport#1591
Open
bhosmer-ant wants to merge 1 commit intomainfrom
Open
Restore negotiated protocol version on reconnection transport#1591bhosmer-ant wants to merge 1 commit intomainfrom
bhosmer-ant wants to merge 1 commit intomainfrom
Conversation
When Client.connect() is called with a transport that has a pre-set sessionId (reconnection), it skips the initialize handshake. Previously this meant transport.setProtocolVersion() was never called, so all subsequent HTTP requests omitted the mcp-protocol-version header — violating the MCP spec's MUST requirement. This change: - Stores the negotiated protocol version in Client during the initial handshake - Restores it onto the new transport in the reconnection early-return path - Adds a getNegotiatedProtocolVersion() getter for manual transport construction - Adds a protocolVersion option to StreamableHTTPClientTransportOptions for users who create a fresh Client on reconnect Addresses #812, #731
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
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.
When
Client.connect()is called with a transport that already has asessionIdset (i.e. reconnection), it skips the initialize handshake. Previously this meanttransport.setProtocolVersion()was never called, so the new transport's_protocolVersionstayedundefinedand all subsequent HTTP requests omitted themcp-protocol-versionheader — violating the MCP spec's MUST requirement that clients include this header on all post-initialization requests.This PR:
Client._negotiatedProtocolVersionduring the initial handshakegetNegotiatedProtocolVersion()getter so users can retrieve it for manual transport constructionprotocolVersionoption toStreamableHTTPClientTransportOptionsfor users who create a freshClienton reconnect (the pattern used inexamples/client/src/simpleStreamableHttp.ts)Resolves #812
Resolves #731
Motivation and Context
The bug is currently masked in SDK-to-SDK testing because the server's
validateProtocolVersion()is intentionally lenient — it accepts missing headers and only rejects when the header is present with an unsupported value. But any stricter MCP server implementation that enforces the spec's MUST requirement would reject all post-reconnection requests.How Has This Been Tested?
Two new tests:
test/integration/test/client/client.test.ts— verifies that reconnecting the sameClientwith a new transport (withsessionIdset) restores the protocol version viasetProtocolVersion()packages/client/test/client/streamableHttp.test.ts— verifies theprotocolVersionconstructor option is honored and the header is included in outgoing requestsExisting reconnection tests (
taskResumability.test.ts) continue to pass.Breaking Changes
None. This is purely additive — a new private field, a new getter, and a new optional constructor parameter.
Types of changes
Checklist
Additional context
This is one of several bugs contributing to reconnection failures in the StreamableHTTP transport. Related issues that are partially addressed by this fix: #764, #852.