Skip to content

mcpcompat/server: expose a session's declared client capabilities - #448

Merged
tgrunnagle merged 3 commits into
mainfrom
principled-allspice
Nov 2, 2026
Merged

tgrunnagle merged 3 commits into
mainfrom
principled-allspice

Conversation

@tgrunnagle

@tgrunnagle tgrunnagle commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Closes #447

Summary

mcpcompat/server sessions now report the capabilities the client declared at initialize, along with a flag saying whether those capabilities are actually known. An embedding server can use this to check that a client declared elicitation.url before sending a URL-mode elicitation, which the MCP spec forbids otherwise. Consumed by stacklok/toolhive#6970.

Changes

mcpcompat/server

  • Add the exported interface SessionWithClientCapabilities, which embeds ClientSession and adds DeclaredClientCapabilities() (mcp.ClientCapabilities, bool). A handler type-asserts the value returned by ClientSessionFromContext.
  • Implement it on *clientSession. It reads InitializeParams from the bound go-sdk session and translates them to mcpcompat types, including the elicitation.form and elicitation.url sub-objects.
  • Add clientSession.initializedOn, set in registerAndSync, to record the go-sdk session on which this instance saw initialize.
  • Add tests in client_capabilities_test.go and client_capabilities_internal_test.go, plus a compile-time interface check in server_internal_test.go.

Implementation Notes

  • Known-ness. The accessor reports known true only when initializedOn equals the currently bound go-sdk session. initializedOn is set only on the local initialize path, never on rehydration or stateless requests. A rehydrated session is seeded with elicitation: {} so go-sdk's gate admits form elicitation. The client never declared that value, so it reads as unknown (zero capabilities, known false).
  • Field-by-field translation instead of a JSON round-trip. go-sdk's deprecated non-pointer Roots field always marshals as {}, so a round-trip would report roots for every client. Roots presence is read from RootsV2. Tasks has no go-sdk counterpart and is left nil.
  • New interface instead of changing ClientSession. This keeps the existing exported interface unchanged, following the SessionWith* pattern.
  • Method name. DeclaredClientCapabilities is used instead of mcp-go's GetClientCapabilities, so it does not collide with mcp-go's SessionWithClientInfo signature, 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 ./... (uncached task test equivalent): run A exit 1, "--- FAIL: TestDiscoverFallback_ExtraSessionOnStatefulServer" (Delete "http://127.0.0.1:63901": net/http: HTTP/1.x transport connection broken: http: CloseIdleConnections called at 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 0
  • go 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:

  • AC1.1 (known true inside a handler; elicitation.url present for {url: {}}, absent for {} and for no elicitation): TestClientSession_DeclaredCapabilities in mcpcompat/server/client_capabilities_test.go
  • AC1.2 (known false on a rehydrated session and on a stateless request): TestClientSession_DeclaredCapabilities_UnknownWhenNotObserved, subtests "rehydrated session" and "stateless request", in mcpcompat/server/client_capabilities_test.go

Additional Notes

  • Out of scope, per the issue: persisting capabilities through rehydration. Consumers fall back when capabilities are unknown.
  • The issue calls for a tagged toolhive-core release after this PR merges.

🤖 Generated with Claude Code

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 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

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. ClientSession is untouched, and the new interface follows the SessionWith* pattern with a compile-time assertion alongside the others.
  • Rehydration path: go-sdk rejects a second notifications/initialized on a rehydrated session (its InitializedParams are seeded), so InitializedHandler cannot reach registerAndSync there and wrongly mark the session as having observed initialize. The only writer of initializedOn is 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

  1. From mcpcompat/: go test -race -count=1 -run 'DeclaredCapabilities|DeclaredClientCapabilities|FromGoSDKClientCapabilities' ./server should pass.
  2. From the repo root: task (lint plus the full race test run) should pass. TestDiscoverFallback_ExtraSessionOnStatefulServer is described in the PR as an unrelated intermittent flake, so rerun it before treating a failure there as caused by this change.
  3. Deliberately break it: make DeclaredClientCapabilities return known=true whenever ss != nil and confirm the rehydrated and stateless subtests and TestDeclaredClientCapabilities_UnknownStates fail; read roots from in.Roots instead of in.RootsV2 and 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.

Comment thread mcpcompat/server/client_capabilities.go Resolved
Comment thread mcpcompat/server/client_capabilities.go Resolved
Comment thread mcpcompat/server/client_capabilities.go Resolved
Comment thread mcpcompat/server/client_capabilities.go Resolved
Comment thread mcpcompat/server/client_capabilities.go Resolved
tgrunnagle and others added 2 commits November 2, 2026 10:02
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 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

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

  1. From mcpcompat/: go test -race -count=1 -run 'DeclaredCapabilities|DeclaredClientCapabilities|FromGoSDKClientCapabilities' ./server should pass.
  2. From the repo root: task should pass.
  3. Deliberately break it: delete the params == nil guard and confirm the "initialize observed but params missing" case fails; remove either maps.Clone and confirm TestFromGoSDKClientCapabilities_ClonesMaps fails; key roots presence on ListChanged and 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:staticcheck directives (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/Extensions values 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 == nil branch untested (mcpcompat/server/client_capabilities_internal_test.go): resolved in c2f6661.
  • initSessionWithCaps duplicated initSession (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.

@tgrunnagle
tgrunnagle marked this pull request as ready for review November 2, 2026 18:24

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 when Elicitation != nil && (Form != nil || URL == nil), not Form != nil alone. 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, so known=false there doesn't mean the client lacks a capability.
  • Tasks is 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=false would pin the main case the pointer check exists for.

@tgrunnagle
tgrunnagle merged commit bad28fe into main Nov 2, 2026
6 checks passed
@tgrunnagle
tgrunnagle deleted the principled-allspice branch November 2, 2026 20:43
@tgrunnagle

Copy link
Copy Markdown
Contributor Author

@jhrozek non-blocking follow ups addresed in #450

tgrunnagle added a commit that referenced this pull request Nov 2, 2026
… 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]>
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 declared client capabilities

2 participants