Repository navigation
Don't send a cached protocol version header with initialize - #2050
Open
shrutimane2409 wants to merge 1 commit into
Open
shrutimane2409 wants to merge 1 commit into
shrutimane2409 wants to merge 1 commit into
Conversation
When the Streamable HTTP client fell back from server/discover to initialize, the transport still had the 2026-07-28 version cached from the server/discover request and sent it as the MCP-Protocol-Version header, while the initialize request body asked for 2025-11-25. Servers reject that mismatch with HTTP 400. Clear the cached version when sending initialize, since initialize negotiates the version. Fixes modelcontextprotocol#2044
sivaraman-krishnan
left a comment
There was a problem hiding this comment.
Thanks for the focused fix and the regression test, @shrutimane2409!
I traced how the Streamable HTTP client caches the version header through server/discover, the initialize fallback, notifications/initialized, later POSTs, the standalone GET and DELETE on dispose:
- Clearing
_negotiatedProtocolVersiononly forinitializekeeps the fallback from inheriting the2026-07-28value cached from a successfulserver/discover. - A version in the request's
_metastill takes priority. - After the initialize result is processed, the field is set again from its
protocolVersion, so later requests still sendMCP-Protocol-Versionas the spec requires after initialization.
The new test fails without the transport change and passes with it. I didn't find a request that should carry the negotiated header and loses it.
Author
|
Thanks for tracing through all the request paths and confirming! @sivaraman-krishnan |
This branch has not been deployed
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.
Fixes #2044
When the Streamable HTTP client falls back from
server/discovertoinitialize, the transport still has the2026-07-28version cached from theserver/discoverrequest and sends it as theMCP-Protocol-Versionheader, while theinitializebody asks for2025-11-25. This change clears the cached version before sendinginitialize.Motivation and Context
Servers reject the mismatched header with HTTP 400, so a client that falls back from
server/discovertoinitializecan fail to connect.initializenegotiates the protocol version, so it shouldn't carry a version cached from an earlier request. With the cached value cleared, the fallbackinitializegoes out without the stale header, like a normalinitialize, and the version is set from theinitializeresult as before.How Has This Been Tested?
Client_OnDiscoverWithoutCommonVersion_FallsBackTo_Initialize_WithMatchingProtocolVersionHeader, based on the repro in the issue:server/discoversucceeds but only lists2025-11-25, and the server returns 400 if theinitializeheader doesn't match the body. It fails without the fix withProtocol header differs from initialize body, and passes with it.Breaking Changes
None.
Types of changes
Checklist
Additional context
Restore currently fails on
mainwith NU1903 forMicrosoft.DotNet.ILCompiler9.0.20 insamples/TestServerWithHosting, so I ran the tests with-p:NuGetAudit=false