Skip to content

fix(mcp): negotiate protocol version with fallback - #132

Merged
BaaRai merged 2 commits into
BaaRai:mainfrom
htcom-code:fix/131-mcp-version-fallback
Nov 6, 2026
Merged

BaaRai merged 2 commits into
BaaRai:mainfrom
htcom-code:fix/131-mcp-version-fallback

Conversation

@htcom-code

@htcom-code htcom-code commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Closes #131

Problem

The Streamable HTTP MCP client always offered 2025-06-18 and kept sending MCP-Protocol-Version: 2025-06-18 after initialize, regardless of the version the server answered with. Under the 2025-11-25 spec a server that does not support the requested version answers initialize with one it does support (Lifecycle › Version Negotiation), and must reject requests carrying an unsupported MCP-Protocol-Version with 400 Bad Request (Transports › Protocol Version Header). Against a 2025-11-25-only server, tools/list therefore failed and no tools were loaded. The client also sent the header on initialize itself, 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.go

  • protocolVersion is now 2025-11-25; initialize offers it first, then 2025-06-18.
  • initialize is sent without MCP-Protocol-Version. Once it succeeds, the protocolVersion from its result is stored and sent as MCP-Protocol-Version from notifications/initialized on. A server on any version can therefore negotiate, not only the two the client offers.
  • The fallback stays as a net for servers that reject the initialize body version. A retry happens only when the error is a version rejection (isUnsupportedVersion): an HTTP 400 body saying "Unsupported protocol version", the JSON-RPC -32602 "Unsupported protocol version" error from the spec's example, or an UNSUPPORTED_PROTOCOL_VERSION code. Other errors (e.g. 401) are returned immediately.
  • The legacy HTTP+SSE transport still uses 2024-11-05 only.

mcphttp/client_test.go

  • TestStreamableClientProtocolVersions: spec-shaped servers that each support one version (2025-11-25, 2025-06-18, 2025-03-26). Each test asserts one initialize attempt with no version header, offering 2025-11-25, and that every later request carries the negotiated version.
  • TestStreamableClientVersionRejectionForms: fallback on HTTP 400 text and on JSON-RPC -32602, no retry on 401, no header on initialize, and the negotiated version on notifications/initialized.
  • Both tests fail against the first commit with initialize carried MCP-Protocol-Version "2025-11-25".

Known limits

  • For servers that reject the initialize body version, a 400 with an empty or unrelated body still does not trigger the fallback, because the spec does not define the body of that response.
  • If the server answers with a version the client does not know, the client still uses it; the spec says the client SHOULD disconnect in that case.
  • MCP 2026-07-28 (stateless, per-request _meta, server/discover, Mcp-Method/Mcp-Name headers) is not covered. The stdio client is in the Norma dependency and still pins 2024-11-05.

Testing

  • go vet ./mcphttp — clean
  • go test -count=1 -race ./mcphttp — pass
  • go build ./... — pass
  • The full go test -p 1 ./... needs PostgreSQL and was left to CI.

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]>
@BaaRai

BaaRai commented Nov 2, 2026

Copy link
Copy Markdown
Owner

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. roundTrip still sets MCP-Protocol-Version on the initialize request itself. That is the root cause of the second gap in your issue, and the spec scopes the header to requests after initialization ("on all subsequent requests"), while the offered version already travels in the initialize body.

I tested this against a server shaped like the one in your report: it supports only 2025-03-26, accepts a request without the header, and rejects any other value.

  • Current PR: attempts = [2025-11-25, 2025-06-18], both answered 400 UNSUPPORTED_PROTOCOL_VERSION, connect fails.
  • With the header omitted until initialize succeeds (one bool set after the successful call, three lines): connects on the first attempt, negotiates 2025-03-26, and sends it on notifications/initialized and tools/list.

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 initialize with a version error, keep the newest-first order, and keep the negotiated-version persistence. Note that TestStreamableClientProtocolVersions currently encodes the old behavior, since its fake server rejects an empty header, so it needs a small update plus a case asserting that initialize carries no version header and that later requests carry the negotiated one.

BaaRai
BaaRai previously requested changes Nov 2, 2026

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@BaaRai

BaaRai commented Nov 2, 2026

Copy link
Copy Markdown
Owner

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 2025-11-25, and with your change it connects on the first attempt. That part is confirmed.

Two things I overstated:

  1. My probe server was not "shaped like the one in your report". Yours supports only 2025-11-25. My probe was a hypothetical server supporting only 2025-03-26 that validates the header on every request, which is unlikely in practice since the header was introduced after that revision. The evidence is still valid as a demonstration of what sending the header on initialize costs, but I described it wrongly.

  2. The spec does not forbid the header on initialize. It requires it "on all subsequent requests", so sending it there is outside the spec's scope rather than a violation. The reference SDKs I checked omit it on initialize, but that is convention, not a rule.

Also, dropping the header is not a two-test change: both TestStreamableClientProtocolVersions and TestStreamableClientVersionRejectionForms encode the old behaviour and would need updating.

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.

@BaaRai
BaaRai dismissed their stale review November 2, 2026 15:59

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]>
@htcom-code

Copy link
Copy Markdown
Contributor Author

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: initialize now goes out without MCP-Protocol-Version, and the header starts on notifications/initialized with the negotiated version. The fallback loop, the newest-first order and the negotiated-version persistence are unchanged.

Both TestStreamableClientProtocolVersions and TestStreamableClientVersionRejectionForms are rewritten around that, including your 2025-03-26-only server. They fail against the previous commit with initialize carried MCP-Protocol-Version "2025-11-25". go vet, go test -race ./mcphttp and go build ./... pass.

The PR is ready for review (it was never a draft), so it can be merged whenever you're happy with it.

@BaaRai
BaaRai merged commit 44d755f into BaaRai:main Nov 6, 2026
2 checks passed
@BaaRai BaaRai mentioned this pull request Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcp: Streamable HTTP client fails against servers that only support 2025-11-25

2 participants