Skip to content

[P27] sampling: tools in sampling requests (SEP-1577) - #344

Merged
jeanmatthieu merged 3 commits into
rel/1.0.0from
conformance/p27-sampling-tools
Nov 25, 2026
Merged

jeanmatthieu merged 3 commits into
rel/1.0.0from
conformance/p27-sampling-tools

Conversation

@jeanmatthieu

@jeanmatthieu jeanmatthieu commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor
Part P27 of P0–P35, merged into rel/1.0.0 in order (previous: P26 (#345) · next: P28 (#343))
Branch conformance/p27-sampling-tools
Base conformance/p26-sep-inventory (stacked: GitHub retargets this PR to rel/1.0.0 when the previous part merges)
Commits e886e35 sampling: tools in sampling requests (SEP-1577)
755f4a9 sampling: review fixes
ca6f02a sampling: third-pass review fixes
Size 17 files, +1080 / -51

Summary

Server.requestSampling takes tools and toolChoice, so a server can run the tool loop of SEP-1577: the client's model asks for a tool, the server runs it, and the next request carries the result. Server and client check the capability that tools need (sampling.tools) and the rules that the specification gives for tool uses and tool results. Sampling input requests (2026-07-28, the handshake bridge and tasks) get the same checks. A fix to ToolChoice(mode:) makes .none mean none again.

Motivation

The wire types for tools in sampling exist since the 2025-11-25 work, but requestSampling could not send them, and no side checked them. The specification makes sampling.tools a MUST for a request with tools or toolChoice, and gives rules for the message history; a server or client that breaks them gets an error from a strict peer, or a model call with a broken history.

Changes

Server (Sources/MCP/Server/Server.swift)

  • requestSampling(messages:modelPreferences:systemPrompt:includeContext:temperature:maxTokens:stopSequences:tools:toolChoice:_meta:): tools and toolChoice before _meta, both defaulting to nil.
  • A request with tools or toolChoice needs the client's sampling.tools: -32601 without it, also when the server is not strict (the parameter is new, so no existing caller breaks). An includeContext other than none needs sampling.context, in strict mode only, as the other capability checks.
  • A message history that breaks a rule throws -32602 before the request is sent.
  • Input requests: checkCapabilities is renamed checkInputRequests. Sampling input requests at 2026-07-28, through the handshake bridge and from tasks need the same sub-capabilities: -32021 names them in requiredCapabilities. A broken history fails the call with -32603, because the handler built it.

Client (Sources/MCP/Client/Client.swift, Sampling.swift)

  • The check sits in withMethodHandler, so it covers handshake requests and 2026-07-28 input requests. The client answers -32602 and does not call its handler when the request has tools and the client did not declare sampling.tools, or when the history breaks a rule.

The message rules (Sampling.swift, toolMessageProblem): a message with tool results holds only tool results; each tool use has a result in the next message, which is a user message; each tool result answers a tool use in the message before it. The whole history is checked, including a last tool use without a result. The TypeScript and Python SDKs check only the last two messages; the specification's MUST covers every assistant message with tool uses.

ToolChoice

  • init(mode: Mode = .auto). Since Mode became an open struct (P19 ([P19] api: an extensibility promise for every public enum #352)), .init(mode: .none) with the optional parameter was Optional.none, that is auto. The old init(mode: Mode?) is @_disfavoredOverload and deprecated, so existing code still compiles and .none resolves to the new initializer.

Docs

  • Server guide, "Sampling with tools": the loop as input requests, with a round limit and toolChoice none on the last round. Client guide: the refusals. Migration guide: the -32602 before sending and the ToolChoice fix. design/mrtr.md: the sampling sub-capabilities of input requests. CHANGELOG. ROADMAP: SEP-1577 Implemented.
  • Snippet mrtr-sampling-tools (Examples/Snippets/Server/MultiRoundTrip.swift), run by the snippet tests.

API changes

Symbol Change
Server.requestSampling(...tools:toolChoice:_meta:) tools and toolChoice added, with defaults
ToolChoice.init(mode:) non-optional Mode, default .auto
ToolChoice.init(mode: Mode?) deprecated, disfavored

Behavior changes

  • requestSampling with a broken tool history throws -32602 instead of sending.
  • requestSampling with tools to a client without sampling.tools throws -32601, in every mode.
  • The client answers -32602 to a sampling request with tools that it did not declare, or with a broken history.
  • ToolChoice(mode: .none) is none, not auto.

Conformance

Baselines unchanged (empty on every leg). Tier scores 100%: 2025-11-25 client 18/18, server 30/30; 2026-07-28 client 32/32, server 37/37.

Tests

Tests/MCPTests/SamplingToolsTests.swift: the message rules, the capability checks at both eras, the client refusals, and the specification's tool loop on the wire at 2025-11-25 (requestSampling and the bridge) and at 2026-07-28 (input requests). At e886e35, swift test passes 1029 swift-testing tests in MCPTests and 108 snippet tests on macOS, and 1121 tests on Linux (scripts/ci-linux.sh build-test).

Review notes

  • The includeContext check stays strict-only, as before, so 0.12 callers keep working.
  • One Linux run failed the task test "A task lives for its ttlMs" (a 100 ms ttl on a loaded host). P29 ([P29] tools: the tool name format (SEP-986) #342) makes the ttl longer.

Verification

  • swift test; DocC with warnings as errors; doc coverage; swift scripts/weave.swift --check.
  • Every conformance leg for client and server; both tier scores.
  • scripts/run-interop.sh --check; Linux scripts/ci-linux.sh build-test.

AI usage disclosure: the changes, the tests, the commit messages and this description were written with Claude Opus 5.5 and reviewed by the authors.

Server.requestSampling takes tools and toolChoice. A request with
either needs the client's sampling.tools capability, which the
specification makes a MUST, so requestSampling throws -32601 without
it also when the server is not strict. An includeContext other than
none needs sampling.context, in strict mode, as the other capability
checks. Sampling input requests, at 2026-07-28, through the handshake
bridge and from tasks, need the same sub-capabilities: -32021 names
them in requiredCapabilities.

The message rules are checked on both sides: a message with tool
results holds only tool results, each tool use has a result in the
next message, which is a user message, and each tool result answers a
tool use in the message before it. requestSampling throws -32602 before
it sends; an input request that breaks a rule fails the call with
-32603, because the handler built it; the client answers -32602 and
does not call its handler. The client also answers -32602 to a request
with tools when it did not declare sampling.tools.

ToolChoice(mode:) takes a Mode. With the optional parameter,
.init(mode: .none) was nil, which is auto; that initializer is
deprecated and disfavored.

Tests: the message rules, the capability checks at both eras, the
client refusals, and the specification's tool loop on the wire at
2025-11-25 (requestSampling and the bridge) and at 2026-07-28 (input
requests). The server guide shows the loop as input requests, with a
round limit and toolChoice none on the last round.
- Do not count blocks of an unknown type as other content in the tool
  message rules, so a block from a later revision next to tool results
  is not refused; the later revision decides where such blocks may go.
- Store .auto for a nil mode in the deprecated ToolChoice(mode: Mode?),
  as its documentation says, so it equals ToolChoice(mode: .auto) and
  survives encoding, which omits auto, and decoding, which reads a
  missing mode as auto.
- Check the roles in the tool message rules: tool uses are in assistant
  messages and tool results in user messages (2025-11-25 sampling,
  "Message Roles" and "Tool Use and Result Balance"). Two user messages,
  one with a tool use and the next with its result, were accepted on
  both sides.
- Note in the migration guide that requestSampling throws -32601 for
  tools or toolChoice without the client's sampling.tools, also when the
  server is not strict.
varun06
varun06 previously approved these changes Nov 25, 2026

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

Already reviewed and still looking good.

Base automatically changed from conformance/p26-sep-inventory to rel/1.0.0 November 25, 2026 12:51
@jeanmatthieu
jeanmatthieu dismissed varun06’s stale review November 25, 2026 12:51

The base branch was changed.

@jeanmatthieu
jeanmatthieu merged commit 3087570 into rel/1.0.0 Nov 25, 2026
@jeanmatthieu
jeanmatthieu deleted the conformance/p27-sampling-tools branch November 25, 2026 12:52
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