Skip to content

feat: cap the includeContext a sampling handler sees - #411

Merged
varun06 merged 1 commit into
modelcontextprotocol:rel/1.0.0from
varun06:feat/max-context-inclusion
Nov 30, 2026
Merged

varun06 merged 1 commit into
modelcontextprotocol:rel/1.0.0from
varun06:feat/max-context-inclusion

Conversation

@varun06

@varun06 varun06 commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Summary

A server can request includeContext: allServers in sampling/createMessage, which asks the client to share context from every connected MCP server with that one server. The 2025-11-25 specification soft-deprecates thisServer and allServers (servers should use them only when the client declares sampling.context), and SEP-2596 deprecates them. This PR lets the app cap what a sampling handler sees.

Changes

  • Client.Configuration.maxContextInclusion (default .none). Before a sampling handler runs, the client lowers a higher includeContext to the cap. A request without includeContext is unchanged.
  • The cap is applied in Client.withMethodHandler, next to the existing sampling checks. It covers sampling/createMessage requests (2025-11-25) and sampling input requests (2026-07-28), and reads the configuration when the request arrives.
  • Sampling.ContextInclusion is Comparable: none < thisServer < allServers. The ordering reads the raw value, so the SDK does not name the deprecated cases.
  • Client.Configuration encodes the cap; a missing key decodes to .none, so configurations saved by earlier versions still decode.
  • Docs: client guide (Sampling), migration guide (behavior changes), CHANGELOG.

The wire format does not change. The deprecation of thisServer and allServers is already on rel/1.0.0 (#341), so this PR does not touch it.

API changes

Symbol Change
Client.Configuration.maxContextInclusion new property
Client.Configuration.init(...maxContextInclusion:) new last parameter, default .none
Sampling.ContextInclusion: Comparable new conformance

Behavior changes

By default a sampling handler now sees includeContext == .none when a server asks for thisServer or allServers. Apps that want the previous behavior set maxContextInclusion: .allServers. This is the one decision I would like a maintainer's view on: the default is the safe one, and it is a change for apps on rel/1.0.0.

Tests

SamplingContextInclusionCapTests in SamplingTests.swift:

  • the ordering and the default;
  • saving the configuration, and a missing key decoding to .none;
  • the cap matrix (cap x requested -> value the handler sees) at both 2025-11-25 and 2026-07-28;
  • a request without includeContext staying without one;
  • capping(includeContext:) keeping every other field.

I checked that the matrix fails at both eras when the cap is not applied. An existing integration test that expected the handler to see thisServer now sets maxContextInclusion: allServers.

Verification on macOS: swift test (1540 tests pass), swift scripts/weave.swift --check, swift scripts/doc-coverage.swift, and DocC with --warnings-as-errors. I did not run the conformance legs, the interop check or Linux.

Co-authored-by: Alex Bullitt

AI usage disclosure: the rebase onto rel/1.0.0, the tests, the docs and this description were written with Claude and reviewed by the author.

A server can request includeContext: allServers, which asks the client
to share context from every connected MCP server with that one server.
The 2025-11-25 specification soft-deprecates thisServer and allServers,
and servers should use them only when the client declares
sampling.context; SEP-2596 deprecates them.

Add Client.Configuration.maxContextInclusion, default .none. Before a
sampling handler runs, the client lowers a higher includeContext to the
cap. The cap applies in withMethodHandler, so it covers requests at
2025-11-25 and input requests at 2026-07-28, and reads the configuration
when the request arrives. A request without includeContext is unchanged.
Sampling.ContextInclusion becomes Comparable
(none < thisServer < allServers).

The configuration saves the cap; a missing key decodes to .none.

Co-authored-by: Alex Bullitt <[email protected]>
@varun06
varun06 requested a review from a team as a code owner November 30, 2026 18:23

@jeanmatthieu jeanmatthieu 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.

Thanks, looks good

@varun06
varun06 merged commit d96515e into modelcontextprotocol:rel/1.0.0 Nov 30, 2026
12 of 14 checks passed
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.

2 participants