Repository navigation
mcpcompat/server: expose a session's negotiated protocol version - #468
Conversation
Implements changes for issue #467: - Add SessionWithProtocolVersion with NegotiatedProtocolVersion() (version string, known bool), implemented by the concrete session returned from ClientSessionFromContext, beside SessionWithClientCapabilities. - Record the version from go-sdk's InitializeResult in the dispatch middleware, so it is go-sdk's negotiation result rather than the requested version, and before OnRegisterSession hooks run. - Report known only while the go-sdk session that answered initialize is still bound, so rehydrated sessions, stateless requests, and SSE/stdio sessions read as unknown. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Address code review feedback: - MEDIUM: pin that the negotiated protocol version is recorded before OnRegisterSession hooks run, so moving the record call after registration fails a test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
tgrunnagle
left a comment
There was a problem hiding this comment.
Review
Shim-only feature addition to mcpcompat/server: a new exported SessionWithProtocolVersion interface whose NegotiatedProtocolVersion() (version string, known bool) is implemented by *clientSession. The dispatch middleware records go-sdk's InitializeResult.ProtocolVersion after a successful stateful initialize, before registerAndSync, so OnRegisterSession hooks can already read it. known follows the DeclaredClientCapabilities pattern: the recorded go-sdk session must still be the bound one, so rehydrated, rebound, stateless, and ID-less sessions report unknown. The approach is sound and matches issue #467 (sibling interface, no existing exported signature changes, both acceptance tests present under the named test functions). CI is green. A duplicate initialize cannot overwrite the recorded version (go-sdk rejects it), a stale version is never reported after rebinding or reaping, and legacy stateless dispatch with a client-supplied session ID is skipped. The remaining points are four LOW test and convention nits.
Findings
| # | Severity | Finding | Location | Raised by |
|---|---|---|---|---|
| 1 | LOW | No compile-time assertion that clientSession implements SessionWithProtocolVersion |
mcpcompat/server/protocol_version.go:57 |
readability |
| 2 | LOW | initSessionWithVersion duplicates initSessionWithCaps |
mcpcompat/server/protocol_version_test.go:64 |
readability |
| 3 | LOW | Hardcoded 2025-11-25 in the unsupported-version case breaks on a go-sdk upgrade |
mcpcompat/server/protocol_version_test.go:137 |
tests |
| 4 | LOW | testSessionID constant used inconsistently |
mcpcompat/server/protocol_version_internal_test.go:47 |
readability |
How to test
go test -race -count=1 ./mcpcompat/server/...should pass.- Mutation check: delete the
s.recordNegotiatedProtocolVersion(ss, res)call inserver.goand confirmTestClientSession_NegotiatedProtocolVersionandTestClientSession_NegotiatedProtocolVersion_KnownInRegisterHookfail. Move it afterregisterAndSyncand confirm only the hook test fails. Drop thec.goSession.Load() != n.oncomparison and confirm the rebinding unit tests fail. task lintandtask license-checkshould stay clean.
Reviewed by dev:pr-review (finders: correctness, readability, API design, tests). Generated with Claude Code.
Addresses #468 review comments: - LOW mcpcompat/server/protocol_version.go:57 (4485115938): add a compile-time SessionWithProtocolVersion assertion - LOW mcpcompat/server/protocol_version_test.go:64 (4485115947): share one initialize helper between the caps and version tests - LOW mcpcompat/server/protocol_version_test.go:137 (4485115958): assert the unsupported version is not echoed instead of a go-sdk literal - LOW mcpcompat/server/protocol_version_internal_test.go:47 (4485115963): use testSessionID consistently Co-Authored-By: Claude Opus 5.5 <[email protected]>
tgrunnagle
left a comment
There was a problem hiding this comment.
Review
Shim-only feature addition to mcpcompat/server: an exported SessionWithProtocolVersion interface, implemented by *clientSession, that reports the protocol version go-sdk answered initialize with. The version is recorded in the dispatch middleware before registerAndSync, so OnRegisterSession hooks can read it, and known is false for rehydrated, rebound, stateless, and ID-less sessions. The approach is sound and matches issue #467, with no change to any existing exported signature. The commits since the previous review are test-only: a compile-time interface assertion, a shared initSessionRaw handshake helper, a notWant assertion for the unsupported-version case, and a consistent testSessionID. They preserve every existing caller of initSessionWithCaps and keep a meaningful assertion in each table case. No findings remain, and CI is green.
Resolved since the last review
- No compile-time assertion that
clientSessionimplementsSessionWithProtocolVersion(mcpcompat/server/protocol_version.go:57):server_internal_test.go:31now asserts it. initSessionWithVersionduplicatesinitSessionWithCaps(mcpcompat/server/protocol_version_test.go:64): both are now wrappers aroundinitSessionRawinclient_capabilities_test.go.- Hardcoded
2025-11-25in the unsupported-version case (mcpcompat/server/protocol_version_test.go:137): the case assertsnotWant: "2099-01-01"and the go-sdk case carries a comment saying its literal tracks go-sdk's latest revision. testSessionIDconstant used inconsistently (mcpcompat/server/protocol_version_internal_test.go:47): the table test now usestestSessionID.
How to test
go test -race -count=1 ./mcpcompat/server/...should pass.- Delete the
s.recordNegotiatedProtocolVersion(ss, res)call inserver.go;TestClientSession_NegotiatedProtocolVersionandTestClientSession_NegotiatedProtocolVersion_KnownInRegisterHookshould fail. - Rename the method on
clientSession;go vet ./mcpcompat/server/should now fail on the compile-time assertion.
Reviewed by dev:pr-review (incremental; finders: correctness, readability). Generated with Claude Code.
mcpcompat/server: expose a session's negotiated protocol version
Closes #467
Summary
A tool handler can now read the MCP protocol version a session negotiated at
initialize, and whether that value is known, next to the declared client capabilities from #447. Consumers can then apply version-gated rules, such as sending-32042URLElicitationRequiredErroronly on a 2025-11-25 session (needed by stacklok/toolhive#7023).Changes
mcpcompat/server
SessionWithProtocolVersioninterface (ClientSessionplusNegotiatedProtocolVersion() (version string, known bool)), implemented by*clientSession. Handlers reach it by type-asserting the session fromClientSessionFromContext.sessionDispatchMiddlewarenow records the version from go-sdk's*InitializeResultafter a successful statefulinitialize, beforeregisterAndSync, soOnRegisterSessionhooks can read it.clientSessiongains an atomicnegotiatedfield holding the recorded version and the go-sdk session it came from.knownis false, with an empty version, on a rehydrated or rebound session, a stateless request, SSE and stdio sessions, and beforeinitializecompletes.Implementation Notes
InitializeResult, not recomputed. go-sdk v1.8 has no exported getter forServerSessionState.NegotiatedProtocolVersion, and reading the result means the value always matches what the client was told. A client that requests an unsupported version reports the server's choice (2025-11-25), not the requested one.knownholds only while the recorded go-sdk session is the currently bound one (same approach asinitializedOnfor capabilities). It does not also requireinitializedOn, so the version is already available insideOnRegisterSession.sessionFor("")entry.SessionWithClientCapabilities, so no existing exported interface changes.Verification
Copied from the workflow ledger's final verification (head 7fe0cfc):
task lint: exit 0, "0 issues."go test -race -count=1 ./...: exit 0, "29 packages ok"; "ok github.com/stacklok/toolhive-core/mcpcompat/server 5.307s"task license-check: exit 0go test -race -cover ./mcpcompat/server: "coverage: 86.4% of statements" (recorded at the implementation step)TestClientSession_NegotiatedProtocolVersionfail.Acceptance criteria:
TestClientSession_NegotiatedProtocolVersioninmcpcompat/server/protocol_version_test.go. The go-sdk client reports 2025-11-25 and matchesInitializeResult().ProtocolVersion. A client requesting 2025-06-18 reports 2025-06-18. A client requesting an unsupported version reports the version the server answered with, not the requested one.TestClientSession_NegotiatedProtocolVersion_UnknownWhenNotObserved, subtestsrehydrated sessionandstateless request.TestClientSession_NegotiatedProtocolVersion_KnownInRegisterHook, plus internal tests for unknown states, rebinding, and the record-helper skip cases inprotocol_version_internal_test.go.Additional Notes
🤖 Generated with Claude Code