Repository navigation
fix(mcp): negotiate protocol version with fallback - #132
Conversation
The Streamable HTTP client always offered 2025-06-18 and kept sending that MCP-Protocol-Version header after initialize, ignoring the version the server answered with. A server that only supports 2025-11-25 negotiates its own version and then rejects every later request with 400, so none of its tools load. Servers that validate the header on initialize itself reject the handshake outright, and the client never retried with another version. - Offer 2025-11-25 first and fall back to 2025-06-18 when the server rejects the version: HTTP 400 "Unsupported protocol version", JSON-RPC -32602, or an UNSUPPORTED_PROTOCOL_VERSION code - Send the protocolVersion from the initialize result on later requests - Keep the legacy HTTP+SSE transport on 2024-11-05 Closes BaaRai#131 Tags: #mcp Co-Authored-By: htjulia <[email protected]>
|
Thanks, this is solid work: the negotiated version is now stored and sent on later requests, the fallback only triggers on version rejections, and the three rejection shapes are covered by tests. I verified locally: go build, go vet and go test -race ./mcphttp all pass. One design point to change before this merges. I tested this against a server shaped like the one in your report: it supports only
Doing it this way also removes the handshake's dependence on matching the error body, which your own known limits section flags as fragile, and it works for a server on any version instead of only the two we offer. Could you rework it that way? Keep the fallback loop as a defensive net for servers that still answer |
BaaRai
left a comment
There was a problem hiding this comment.
Formalizing the design point from my comment above as a change request, so the state on this PR is explicit.
Please drop MCP-Protocol-Version from the initialize request and keep it only on the requests that follow, using the version negotiated from the initialize result. That is what the spec scopes the header to, it fixes the root cause of the second gap in your issue rather than guessing among versions, and it removes the handshake's dependence on matching the error body. Keep the fallback loop as a defensive net, keep the newest-first order, and update TestStreamableClientProtocolVersions, which currently encodes the header on initialize, plus add a case asserting that initialize carries no version header and that later requests carry the negotiated one.
The two probes and the exact numbers are in the comment above.
|
A correction to my own review, after re-reading both the spec and my probe setup. You are right that this PR fixes the reported server. The server in your issue supports only Two things I overstated:
Also, dropping the header is not a two-test change: both So this is a design preference, not a blocker, and merging your fallback alone is a fine outcome. I am dismissing my change request. If you want, mark the PR ready and I will merge it as it stands; the header change can then be a follow-up that I write, so you do not have to rework something you already delivered. |
Superseded by a correction: the requested header change is a design preference, not a spec requirement, and the PR already fixes the reported server.
The client sent MCP-Protocol-Version on the initialize request itself. The spec requires the header on requests after initialization, and the offered version already travels in the initialize body. A server that validates the header rejected the handshake unless the client happened to offer a version it supports, so connecting depended on matching the error body and only worked for the two versions the client offers. - Send the header only after initialize succeeds, with the version the server negotiated - Keep the fallback loop, newest-first order and negotiated-version persistence for servers that still reject the initialize body version - Rewrite both version tests around this, including a 2025-03-26-only server, and assert that initialize carries no version header Tags: #mcp Co-Authored-By: htjulia <[email protected]>
|
Thanks for the careful review, and for correcting the probe details yourself. I agree with the design point, so I've pushed it to this PR instead of a follow-up: Both The PR is ready for review (it was never a draft), so it can be merged whenever you're happy with it. |
Closes #131
Problem
The Streamable HTTP MCP client always offered
2025-06-18and kept sendingMCP-Protocol-Version: 2025-06-18afterinitialize, regardless of the version the server answered with. Under the2025-11-25spec a server that does not support the requested version answersinitializewith one it does support (Lifecycle › Version Negotiation), and must reject requests carrying an unsupportedMCP-Protocol-Versionwith400 Bad Request(Transports › Protocol Version Header). Against a2025-11-25-only server,tools/listtherefore failed and no tools were loaded. The client also sent the header oninitializeitself, although the spec requires it only "on all subsequent requests". Servers that validate it there rejected the handshake, and the client never retried.Changes
Two commits: the version negotiation and fallback, then the review follow-up that keeps the header off
initialize.mcphttp/client.goprotocolVersionis now2025-11-25;initializeoffers it first, then2025-06-18.initializeis sent withoutMCP-Protocol-Version. Once it succeeds, theprotocolVersionfrom its result is stored and sent asMCP-Protocol-Versionfromnotifications/initializedon. A server on any version can therefore negotiate, not only the two the client offers.initializebody version. A retry happens only when the error is a version rejection (isUnsupportedVersion): an HTTP400body saying "Unsupported protocol version", the JSON-RPC-32602"Unsupported protocol version" error from the spec's example, or anUNSUPPORTED_PROTOCOL_VERSIONcode. Other errors (e.g.401) are returned immediately.2024-11-05only.mcphttp/client_test.goTestStreamableClientProtocolVersions: spec-shaped servers that each support one version (2025-11-25,2025-06-18,2025-03-26). Each test asserts oneinitializeattempt with no version header, offering2025-11-25, and that every later request carries the negotiated version.TestStreamableClientVersionRejectionForms: fallback on HTTP400text and on JSON-RPC-32602, no retry on401, no header oninitialize, and the negotiated version onnotifications/initialized.initialize carried MCP-Protocol-Version "2025-11-25".Known limits
initializebody version, a400with an empty or unrelated body still does not trigger the fallback, because the spec does not define the body of that response.2026-07-28(stateless, per-request_meta,server/discover,Mcp-Method/Mcp-Nameheaders) is not covered. The stdio client is in the Norma dependency and still pins2024-11-05.Testing
go vet ./mcphttp— cleango test -count=1 -race ./mcphttp— passgo build ./...— passgo test -p 1 ./...needs PostgreSQL and was left to CI.