Repository navigation
[P27] sampling: tools in sampling requests (SEP-1577) - #344
Merged
Merged
Conversation
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.
This was referenced Nov 25, 2026
varun06
previously approved these changes
Nov 25, 2026
varun06
left a comment
Contributor
There was a problem hiding this comment.
Already reviewed and still looking good.
varun06
approved these changes
Nov 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
rel/1.0.0in order (previous: P26 (#345) · next: P28 (#343))conformance/p27-sampling-toolsconformance/p26-sep-inventory(stacked: GitHub retargets this PR torel/1.0.0when the previous part merges)e886e35sampling: tools in sampling requests (SEP-1577)755f4a9sampling: review fixesca6f02asampling: third-pass review fixesSummary
Server.requestSamplingtakestoolsandtoolChoice, 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 toToolChoice(mode:)makes.nonemean none again.Motivation
The wire types for tools in sampling exist since the 2025-11-25 work, but
requestSamplingcould not send them, and no side checked them. The specification makessampling.toolsa MUST for a request withtoolsortoolChoice, 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:):toolsandtoolChoicebefore_meta, both defaulting tonil.toolsortoolChoiceneeds the client'ssampling.tools: -32601 without it, also when the server is not strict (the parameter is new, so no existing caller breaks). AnincludeContextother thannoneneedssampling.context, in strict mode only, as the other capability checks.checkCapabilitiesis renamedcheckInputRequests. Sampling input requests at 2026-07-28, through the handshake bridge and from tasks need the same sub-capabilities: -32021 names them inrequiredCapabilities. A broken history fails the call with -32603, because the handler built it.Client (
Sources/MCP/Client/Client.swift,Sampling.swift)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 declaresampling.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.ToolChoiceinit(mode: Mode = .auto). SinceModebecame an open struct (P19 ([P19] api: an extensibility promise for every public enum #352)),.init(mode: .none)with the optional parameter wasOptional.none, that is auto. The oldinit(mode: Mode?)is@_disfavoredOverloadand deprecated, so existing code still compiles and.noneresolves to the new initializer.Docs
toolChoicenoneon the last round. Client guide: the refusals. Migration guide: the -32602 before sending and theToolChoicefix.design/mrtr.md: the sampling sub-capabilities of input requests. CHANGELOG. ROADMAP: SEP-1577 Implemented.mrtr-sampling-tools(Examples/Snippets/Server/MultiRoundTrip.swift), run by the snippet tests.API changes
Server.requestSampling(...tools:toolChoice:_meta:)toolsandtoolChoiceadded, with defaultsToolChoice.init(mode:)Mode, default.autoToolChoice.init(mode: Mode?)Behavior changes
requestSamplingwith a broken tool history throws -32602 instead of sending.requestSamplingwith tools to a client withoutsampling.toolsthrows -32601, in every mode.ToolChoice(mode: .none)isnone, 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 (requestSamplingand the bridge) and at 2026-07-28 (input requests). Ate886e35,swift testpasses 1029 swift-testing tests inMCPTestsand 108 snippet tests on macOS, and 1121 tests on Linux (scripts/ci-linux.sh build-test).Review notes
includeContextcheck stays strict-only, as before, so 0.12 callers keep working.Verification
swift test; DocC with warnings as errors; doc coverage;swift scripts/weave.swift --check.scripts/run-interop.sh --check; Linuxscripts/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.