Repository navigation
Conversation
597f2c0 to
c47e426
Compare
c47e426 to
79d2351
Compare
A server configured with SupportedProtocolVersions containing only versions >= 2026-07-28 should not silently serve legacy clients that use the initialize/initialized handshake protocol. Previously, such clients would receive a successful (if confusing) response: stateful servers would accept any post-initialize method call because ephemeral session setup bypassed the version check, and stateless servers would always appear initialized due to ephemeral connect opts synthesizing fake InitializeParams. Fix the stateful path by detecting a 2026-only server configuration in the uninitialized branch of handle() and returning CodeUnsupportedProtocolVersion before falling through to the generic "method invalid during initialization" error. Fix the stateless path by checking the legacy flag in serveStateless() immediately after ephemeralConnectOpts(), before any method dispatch. Both paths include an UnsupportedProtocolVersionData payload listing the server's supported versions so clients can adapt. Fixes modelcontextprotocol#1313
914ee33 to
d5a828d
Compare
chrikrah
left a comment
There was a problem hiding this comment.
@Enigmage requesting changes at 81b755b: the stateless rejection answers a request with an error that carries no id, so a client has nothing to match it to. The rest holds: both new rejection tests fail with server.go and streamable.go at the merge base and pass on your tree.
$ go test ./mcp/ # go1.25.3, tree at 81b755b
ok github.com/modelcontextprotocol/go-sdk/mcp 32.431s
$ go test ./mcp/ -run 'TestServerSupportedProtocolVersions|TestStreamableStateless_RejectsLegacyOn2026OnlyServer' # both files at da6ad80, tests kept
--- FAIL: TestServerSupportedProtocolVersions_LegacyDispatch
--- FAIL: TestStreamableStateless_RejectsLegacyOn2026OnlyServer
$ go test ./mcp/ -run TestProbe -v # probe: POST {"id":7,"method":"tools/list"} to a 2026-only server, no _meta
stateless=true -> 400 {"jsonrpc":"2.0","error":{"code":-32022,...,"data":{"supported":["2026-07-28"],"requested":""}}}
stateless=false -> 200 data: {"jsonrpc":"2.0","id":7,"error":{"code":-32022,...,"data":{"supported":["2026-07-28"],"requested":""}}}
blocking: streamable.go:481 passes jsonrpc.ID{} to writeJSONRPCError. The stateful sibling at streamable.go:1736 passes jreq.ID, and #1313 itself expects "id":1 back. ephemeralConnectOpts decodes the ID and does not return it.
non-blocking: requested is empty on both paths. The sibling fills it from the request's version. A legacy request has no header, and ephemeralConnectOpts already falls back to 2025-03-26 for that, so passing that version (or omitempty) would say more than "".
@guglielmo-san you invited this PR on #1313. Should the stateless path take the ID from ephemeralConnectOpts, or decode the body a second time?
…sion when no compatible handshake version exists
|
@Enigmage i added some changes on top of your PR, let me know what you think |
Fixes #1313.
Note: this PR does not change initialize behavior on a 2026-only server, it still succeeds with protocolVersion20251125 per the existing "server with no handshake version" test. We can discuss if we want to reject initialize with -32022 as well.
A server configured with
SupportedProtocolVersionscontaining only versions>= 2026-07-28should reject legacy clients that use theinitialize/initializedhandshake protocol with a clearCodeUnsupportedProtocolVersionerror, rather than silently serving them in a degraded state.What was broken
Two paths allowed legacy requests to slip through on a 2026-only server:
Stateful, uninitialized:
handle()reached the "method invalid during initialization" error path for any non-initializecall, but returned a generic error instead ofCodeUnsupportedProtocolVersion. The caller had no signal that the server will never support the legacy protocol.Stateless:
serveStateless()callsephemeralConnectOpts(), which synthesizes fakeInitializeParamsfor every request — making every session appear already initialized. This caused all legacy stateless calls to dispatch successfully and produce results, with no rejection at all.Fix
server.go— stateful uninitialized path:In the
handle()branch for uninitialized legacy calls, detect when the server has no legacy-supporting version configured and returnCodeUnsupportedProtocolVersionwith anUnsupportedProtocolVersionDatapayload before falling through to the generic error.streamable.go— stateless path:In
serveStateless(), afterephemeralConnectOpts(), check whether the server has any legacy-supporting version configured. If not, write aCodeUnsupportedProtocolVersionerror response and return.Behavior
initialize: allowed —negotiatedVersionreturnsprotocolVersion20251125as a courtesy per existing semantics inshared.go.initializecall (stateful, uninitialized): rejected withCodeUnsupportedProtocolVersion.CodeUnsupportedProtocolVersionand HTTP 400.SupportedProtocolVersionsare unaffected.Tests
TestServerSupportedProtocolVersions_InitializeLegacy: verifiesinitializesucceeds on a 2026-only server and returnsprotocolVersion20251125.TestServerSupportedProtocolVersions_LegacyDispatch: verifies a legacytools/listcall without_metais rejected withCodeUnsupportedProtocolVersionon a 2026-only stateful server.TestStreamableStateless_RejectsLegacyOn2026OnlyServer: verifies a legacy POST to a stateless 2026-only server returns HTTP 400 withCodeUnsupportedProtocolVersion.