Skip to content

mcpcompat/server: expose a session's negotiated protocol version - #468

Merged
tgrunnagle merged 3 commits into
mainfrom
majestic-song
Nov 5, 2026
Merged

tgrunnagle merged 3 commits into
mainfrom
majestic-song

Conversation

@tgrunnagle

@tgrunnagle tgrunnagle commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

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 -32042 URLElicitationRequiredError only on a 2025-11-25 session (needed by stacklok/toolhive#7023).

Changes

mcpcompat/server

  • Add the SessionWithProtocolVersion interface (ClientSession plus NegotiatedProtocolVersion() (version string, known bool)), implemented by *clientSession. Handlers reach it by type-asserting the session from ClientSessionFromContext.
  • sessionDispatchMiddleware now records the version from go-sdk's *InitializeResult after a successful stateful initialize, before registerAndSync, so OnRegisterSession hooks can read it.
  • clientSession gains an atomic negotiated field holding the recorded version and the go-sdk session it came from.
  • known is false, with an empty version, on a rehydrated or rebound session, a stateless request, SSE and stdio sessions, and before initialize completes.

Implementation Notes

  • The version is read from go-sdk's InitializeResult, not recomputed. go-sdk v1.8 has no exported getter for ServerSessionState.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.
  • known holds only while the recorded go-sdk session is the currently bound one (same approach as initializedOn for capabilities). It does not also require initializedOn, so the version is already available inside OnRegisterSession.
  • Sessions with no ID are skipped when recording, to avoid creating the shared sessionFor("") entry.
  • A new sibling interface was added rather than extending 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 0
  • go test -race -cover ./mcpcompat/server: "coverage: 86.4% of statements" (recorded at the implementation step)
  • Mutation check (recorded at the implementation step): disabling the middleware hook makes TestClientSession_NegotiatedProtocolVersion fail.

Acceptance criteria:

  • AC1.1 (known true with the negotiated version inside a tool handler): TestClientSession_NegotiatedProtocolVersion in mcpcompat/server/protocol_version_test.go. The go-sdk client reports 2025-11-25 and matches InitializeResult().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.
  • AC1.2 (known false on rehydrated and stateless): TestClientSession_NegotiatedProtocolVersion_UnknownWhenNotObserved, subtests rehydrated session and stateless request.
  • Also covered: TestClientSession_NegotiatedProtocolVersion_KnownInRegisterHook, plus internal tests for unknown states, rebinding, and the record-helper skip cases in protocol_version_internal_test.go.

Additional Notes

  • This is a shim-only extension with no mcp-go counterpart.
  • Persisting the negotiated version through rehydration and the 2026-07-28 per-request version are out of scope, per the issue. Consumers fall back when the version is unknown.
  • No dependency changes.

🤖 Generated with Claude Code

tgrunnagle and others added 2 commits November 5, 2026 10:44
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 tgrunnagle left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 in server.go and confirm TestClientSession_NegotiatedProtocolVersion and TestClientSession_NegotiatedProtocolVersion_KnownInRegisterHook fail. Move it after registerAndSync and confirm only the hook test fails. Drop the c.goSession.Load() != n.on comparison and confirm the rebinding unit tests fail.
  • task lint and task license-check should stay clean.

Reviewed by dev:pr-review (finders: correctness, readability, API design, tests). Generated with Claude Code.

Comment thread mcpcompat/server/protocol_version.go Resolved
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 tgrunnagle left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 clientSession implements SessionWithProtocolVersion (mcpcompat/server/protocol_version.go:57): server_internal_test.go:31 now asserts it.
  • initSessionWithVersion duplicates initSessionWithCaps (mcpcompat/server/protocol_version_test.go:64): both are now wrappers around initSessionRaw in client_capabilities_test.go.
  • Hardcoded 2025-11-25 in the unsupported-version case (mcpcompat/server/protocol_version_test.go:137): the case asserts notWant: "2099-01-01" and the go-sdk case carries a comment saying its literal tracks go-sdk's latest revision.
  • testSessionID constant used inconsistently (mcpcompat/server/protocol_version_internal_test.go:47): the table test now uses testSessionID.

How to test

  • go test -race -count=1 ./mcpcompat/server/... should pass.
  • Delete the s.recordNegotiatedProtocolVersion(ss, res) call in server.go; TestClientSession_NegotiatedProtocolVersion and TestClientSession_NegotiatedProtocolVersion_KnownInRegisterHook should 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.

@tgrunnagle
tgrunnagle marked this pull request as ready for review November 5, 2026 19:46
@tgrunnagle
tgrunnagle merged commit c4a2239 into main Nov 5, 2026
6 checks passed
@tgrunnagle
tgrunnagle deleted the majestic-song branch November 5, 2026 22:24
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.

mcpcompat: expose a session's negotiated protocol version

2 participants