Repository navigation
mcpcompat/server: expose a session's declared client capabilities - #448
Conversation
Implements changes for issue #447: - Add SessionWithClientCapabilities with DeclaredClientCapabilities() (mcp.ClientCapabilities, known bool), implemented by the concrete session returned from ClientSessionFromContext. - Report known only when this instance observed initialize on the currently bound go-sdk session, so a rehydrated session's seeded elicitation capability and stateless requests read as unknown. - Translate go-sdk capabilities field by field (including elicitation.form/url) rather than via JSON, which would falsely report roots for every client. Co-Authored-By: Claude Opus 5.5 <[email protected]>
tgrunnagle
left a comment
There was a problem hiding this comment.
Review
This is an additive feature plus tests: a new optional interface, SessionWithClientCapabilities, whose single method DeclaredClientCapabilities() (caps, known) is implemented on the concrete session that ClientSessionFromContext returns. The approach is sound. known is true only when the session instance recorded initialize on the go-sdk session that is currently bound (initializedOn == goSession), so rehydrated sessions (whose elicitation: {} is seeded, not declared), stateless requests, and sessions re-bound to a newer go-sdk session after a reap all read as unknown with zero-value capabilities. Translating field by field instead of through JSON correctly avoids go-sdk's always-present deprecated roots field. The remaining trade-offs are documentation-level: SSE and stdio sessions are always reported unknown, and Experimental/Extensions are only shallow-copied.
Cross-cutting observations
- Backwards compatibility: purely additive.
ClientSessionis untouched, and the new interface follows theSessionWith*pattern with a compile-time assertion alongside the others. - Rehydration path: go-sdk rejects a second
notifications/initializedon a rehydrated session (itsInitializedParamsare seeded), soInitializedHandlercannot reachregisterAndSyncthere and wrongly mark the session as having observedinitialize. The only writer ofinitializedOnis therefore the local initialize path. - Intent: AC1.1 and AC1.2 of the linked issue map to the two named tests, and nothing in the diff goes beyond the issue (persisting capabilities through rehydration stays out of scope). The tagged release the issue asks for follows the merge and is not part of this diff.
- The PR is still marked draft.
How to test
- From
mcpcompat/:go test -race -count=1 -run 'DeclaredCapabilities|DeclaredClientCapabilities|FromGoSDKClientCapabilities' ./servershould pass. - From the repo root:
task(lint plus the full race test run) should pass.TestDiscoverFallback_ExtraSessionOnStatefulServeris described in the PR as an unrelated intermittent flake, so rerun it before treating a failure there as caused by this change. - Deliberately break it: make
DeclaredClientCapabilitiesreturnknown=truewheneverss != niland confirm the rehydrated and stateless subtests andTestDeclaredClientCapabilities_UnknownStatesfail; read roots fromin.Rootsinstead ofin.RootsV2and confirm the{}and elicitation-only cases fail.
Documentation updates: the doc comment on SessionWithClientCapabilities should list SSE/stdio and same-instance rehydration among the unknown cases and note the shim-only status (see findings). No other docs are affected.
Recommendation: COMMENT. There are no HIGH findings; everything raised is documentation or test polish.
Findings
| # | Severity | Finding | Location | Raised by |
|---|---|---|---|---|
| 1 | LOW | Doc says rehydrated means "from another replica"; same-instance rehydration also reads unknown | mcpcompat/server/client_capabilities.go:30 |
readability |
| 2 | LOW | known is always false on SSE and stdio sessions, undocumented | mcpcompat/server/client_capabilities.go:45 |
correctness |
| 3 | LOW | Redundant //nolint:staticcheck directives |
mcpcompat/server/client_capabilities.go:76-81 |
readability |
| 4 | LOW | No test for roots declared without listChanged | mcpcompat/server/client_capabilities_internal_test.go:45 |
tests |
| 5 | LOW | Map clone independence untested | mcpcompat/server/client_capabilities_internal_test.go:79 |
tests |
| 6 | LOW | params == nil branch untested |
mcpcompat/server/client_capabilities_internal_test.go:112 |
tests |
| 7 | LOW | initSessionWithCaps duplicates initSession | mcpcompat/server/client_capabilities_test.go:61 |
readability |
| 8 | INFO | Interface doc does not say it is a shim-only extension | mcpcompat/server/client_capabilities.go:22 |
readability, api-design |
| 9 | INFO | Shallow clone shares nested Experimental/Extensions values | mcpcompat/server/client_capabilities.go:69-70 |
correctness, api-design |
Reviewed by dev:pr-review (finders: correctness, readability, api-design, tests). Generated with Claude Code.
Addresses #448 review comments: - LOW mcpcompat/server/client_capabilities.go (4445802934): rehydrated covers same-instance rehydration after a reap - LOW mcpcompat/server/client_capabilities.go (4445802941): document SSE and stdio sessions as always unknown - LOW mcpcompat/server/client_capabilities.go (4445802952): remove //nolint:staticcheck covered by the mcpcompat SA1019 exclusion - INFO mcpcompat/server/client_capabilities.go (4445803001): name mcp-go's SessionWithClientInfo as the analogue and why this differs - INFO mcpcompat/server/client_capabilities.go (4445803004): note nested Experimental/Extensions values are read-only Co-Authored-By: Claude Opus 5.5 <[email protected]>
Addresses #448 review comments: - LOW mcpcompat/server/client_capabilities_internal_test.go (4445802959): cover roots declared without listChanged - LOW mcpcompat/server/client_capabilities_internal_test.go (4445802972): check returned maps are independent top-level copies - LOW mcpcompat/server/client_capabilities_internal_test.go (4445802980): reach the params == nil guard - LOW mcpcompat/server/client_capabilities_test.go (4445802991): initSession delegates to initSessionWithCaps Co-Authored-By: Claude Opus 5.5 <[email protected]>
tgrunnagle
left a comment
There was a problem hiding this comment.
Review
The two commits since the last review (4668c6f and c2f6661) address every earlier comment and change only doc comments, //nolint directives and tests; there is no behavior change in production code. The new doc claims check out against the code: SSE and stdio sessions are never registered as having seen initialize, so they always read as unknown, and a session rehydrated on the same instance after a reap is bound to a different go-sdk session than initializedOn, so it reads as unknown too. The new tests would fail under the regressions they target (roots presence keyed on ListChanged, removing maps.Clone, deleting the params == nil guard), and initSession delegating to initSessionWithCaps adds only a require.Nil(r.Error), which every existing caller satisfies. CI is green on this head (lint, unit tests, license headers, vulnerability scans).
No new findings.
How to test
- From
mcpcompat/:go test -race -count=1 -run 'DeclaredCapabilities|DeclaredClientCapabilities|FromGoSDKClientCapabilities' ./servershould pass. - From the repo root:
taskshould pass. - Deliberately break it: delete the
params == nilguard and confirm the "initialize observed but params missing" case fails; remove eithermaps.Cloneand confirmTestFromGoSDKClientCapabilities_ClonesMapsfails; key roots presence onListChangedand confirm the "roots via RootsV2 without listChanged" case fails.
Recommendation: COMMENT. Nothing blocks merging from this review; the PR is still marked draft.
Resolved since the last review
- Rehydrated sessions are not only "from another replica" (
mcpcompat/server/client_capabilities.go): resolved in 4668c6f. - SSE and stdio sessions always report
known=false(mcpcompat/server/client_capabilities.go): resolved in 4668c6f. - Redundant
//nolint:staticcheckdirectives (mcpcompat/server/client_capabilities.go): resolved in 4668c6f. - Interface doc did not say it is a shim-only extension (
mcpcompat/server/client_capabilities.go): resolved in 4668c6f. - Nested
Experimental/Extensionsvalues shared with go-sdk's session state (mcpcompat/server/client_capabilities.go): resolved in 4668c6f. - No test for roots declared without
listChanged(mcpcompat/server/client_capabilities_internal_test.go): resolved in c2f6661. - Map clone independence untested (
mcpcompat/server/client_capabilities_internal_test.go): resolved in c2f6661. params == nilbranch untested (mcpcompat/server/client_capabilities_internal_test.go): resolved in c2f6661.initSessionWithCapsduplicatedinitSession(mcpcompat/server/client_capabilities_test.go): resolved in c2f6661.
Reviewed by dev:pr-review (incremental review of 2 commits since dacab59; finders: correctness, readability, tests). Generated with Claude Code.
jhrozek
left a comment
There was a problem hiding this comment.
LGTM. The initializedOn == goSession check correctly reports rehydrated, stateless, and reaped/rebound sessions as unknown, and the field-by-field mapping (RootsV2 for presence) is right.
Non-blocking doc follow-ups:
- Per the MCP spec (2025-11-25 and 2026-07-28),
elicitation: {}means form-only support, so callers should treat form as supported whenElicitation != nil && (Form != nil || URL == nil), notForm != nilalone. Worth stating in the doc and covering with a test row. - The accessor only reflects 2025-11-25 initialize-handshake capabilities; 2026-07-28 requests carry capabilities per request in
_meta, soknown=falsethere doesn't mean the client lacks a capability. Tasksis always nil because go-sdk doesn't carry it, so it means "not reported" rather than "not declared".- An end-to-end reap → rehydrate-same-SID test asserting
known=falsewould pin the main case the pointer check exists for.
… cases (#450) Follow-ups from the review of #448: - Document that {"elicitation":{}} means form-only support and how to check for form support, that only initialize-handshake capabilities are read (2026-07-28 carries them per request in _meta), and that Tasks is always nil because go-sdk does not carry it. - Cover the form-support rule with an integration test row, including a form-only case. - Add an end-to-end test that a session reaped and rehydrated under the same ID on one instance reports known=false. - Add a test that binds one session ID to two go-sdk sessions that both carry InitializeParams, so the initializedOn comparison is what reports the newer session's seeded capabilities as unknown. Co-authored-by: Claude Sonnet 5.5 <[email protected]>
Closes #447
Summary
mcpcompat/serversessions now report the capabilities the client declared atinitialize, along with a flag saying whether those capabilities are actually known. An embedding server can use this to check that a client declaredelicitation.urlbefore sending a URL-mode elicitation, which the MCP spec forbids otherwise. Consumed by stacklok/toolhive#6970.Changes
mcpcompat/server
SessionWithClientCapabilities, which embedsClientSessionand addsDeclaredClientCapabilities() (mcp.ClientCapabilities, bool). A handler type-asserts the value returned byClientSessionFromContext.*clientSession. It readsInitializeParamsfrom the bound go-sdk session and translates them to mcpcompat types, including theelicitation.formandelicitation.urlsub-objects.clientSession.initializedOn, set inregisterAndSync, to record the go-sdk session on which this instance sawinitialize.client_capabilities_test.goandclient_capabilities_internal_test.go, plus a compile-time interface check inserver_internal_test.go.Implementation Notes
knowntrue only wheninitializedOnequals the currently bound go-sdk session.initializedOnis set only on the local initialize path, never on rehydration or stateless requests. A rehydrated session is seeded withelicitation: {}so go-sdk's gate admits form elicitation. The client never declared that value, so it reads as unknown (zero capabilities,knownfalse).Rootsfield always marshals as{}, so a round-trip would report roots for every client. Roots presence is read fromRootsV2.Taskshas no go-sdk counterpart and is left nil.ClientSession. This keeps the existing exported interface unchanged, following theSessionWith*pattern.DeclaredClientCapabilitiesis used instead of mcp-go'sGetClientCapabilities, so it does not collide with mcp-go'sSessionWithClientInfosignature, which has no known flag.Verification
Copied from the workflow ledger's final verification (pre-PR gate at dacab59):
task lint: exit 0, "0 issues."go test -race -count=1 ./...(uncachedtask testequivalent): run A exit 1, "--- FAIL: TestDiscoverFallback_ExtraSessionOnStatefulServer" (Delete "http://127.0.0.1:63901": net/http: HTTP/1.x transport connection broken: http: CloseIdleConnections calledat discover_fallback_test.go:162). Runs B and C exit 0, "ok github.com/stacklok/toolhive-core/mcpcompat/server 6.168s" and "ok github.com/stacklok/toolhive-core/mcpcompat/server 6.503s".task license-check: exit 0go test -race -run TestClientSession_DeclaredCapabilities -v ./mcpcompat/server: "--- PASS: TestClientSession_DeclaredCapabilities", "--- PASS: TestClientSession_DeclaredCapabilities_UnknownWhenNotObserved", "ok ... 1.149s"The run A failure is recorded in the ledger as a pre-existing intermittent flake unrelated to this change. The client's
Close()DELETE races idle-connection teardown under full-suite load. It failed 0 of 40 runs in isolation on both HEAD and base 0274256.Acceptance criteria:
elicitation.urlpresent for{url: {}}, absent for{}and for no elicitation):TestClientSession_DeclaredCapabilitiesinmcpcompat/server/client_capabilities_test.goTestClientSession_DeclaredCapabilities_UnknownWhenNotObserved, subtests "rehydrated session" and "stateless request", inmcpcompat/server/client_capabilities_test.goAdditional Notes
🤖 Generated with Claude Code