feat: MCP app sampling support via stock SDK types (alt to #477)#530
Open
feat: MCP app sampling support via stock SDK types (alt to #477)#530
Conversation
Alternative to #477. Instead of forking a stripped-down sampling schema, this reuses the stock `CreateMessageRequest` / `CreateMessageResult` / `CreateMessageResultWithTools` types from `@modelcontextprotocol/sdk` directly — same pattern already used for `tools/call`. - Spec: `sampling/createMessage` listed under `### Standard MCP Messages`, `sampling?: { tools?: {} }` added to `HostCapabilities` (mirrors MCP `ClientCapabilities.sampling` shape for easy pass-through). - SDK: `App.createSamplingMessage()` with overloads that narrow the return type based on whether `params.tools` is set; `AppBridge.oncreatesamplingmessage` setter; `CreateMessageRequest`/`CreateMessageResult*` added to the `AppRequest`/`AppResult` unions. - Picks up SEP-1577 tool-calling support (`tools`, `toolChoice`, `tool_use` content blocks, `stopReason: "toolUse"`, array content) for free — unblocks the "nested agents" use case motivated by the original PR. https://claude.ai/code/session_01ENGWTtsfcyP4S6fTWtUMAh
| /** | ||
| * Example: Forward sampling requests to your LLM provider. | ||
| */ | ||
| function AppBridge_oncreatesamplingmessage_forwardToLlm( |
| /** | ||
| * Example: Simple LLM completion via host sampling. | ||
| */ | ||
| async function App_createSamplingMessage_simple(app: App) { |
| /** | ||
| * Example: Agentic loop with tools (requires host sampling.tools capability). | ||
| */ | ||
| async function App_createSamplingMessage_withTools( |
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.
Alternative to #477. Instead of forking a stripped-down sampling schema, this reuses the stock
CreateMessageRequest/CreateMessageResult/CreateMessageResultWithToolstypes from@modelcontextprotocol/sdkdirectly — the same pattern already used fortools/call,resources/read, etc.Motivation
#477 introduces custom
McpUiSampling*types that diverge from core MCP sampling:maxTokenstools/toolChoiceblock | block[]tool_use,tool_result{type:"text"}onlysampling: { tools?: {} }sampling: { supportedModalities?: (...)[] }The original PR's headline use case ("nested agents") requires tool-calling loops, which that schema can't represent.
Approach
Treat
sampling/createMessageas a Standard MCP Message (spec § Standard MCP Messages), exactly liketools/call:Spec (
specification/draft/apps.mdx):sampling/createMessagelisted under Standard MCP Messages, referencing the upstreamCreateMessageRequestshape (incl. SEP-1577 tools).sampling?: { tools?: {} }added toHostCapabilities— mirrors MCPClientCapabilities.samplingso hosts that are already MCP clients can pass it straight through.Types (
src/types.ts,src/spec.types.ts):CreateMessageRequest→AppRequestunionCreateMessageResult/CreateMessageResultWithTools→AppResultunionSDK ergonomics:
App.createSamplingMessage(params)— overloaded: returnsCreateMessageResultwhentoolsis absent,CreateMessageResultWithToolswhen present. Picks the correct result schema at runtime.AppBridge.oncreatesamplingmessage = async (params, extra) => {...}— same setter pattern asoncalltool.App.assertCapabilityForMethodnow checkshostCapabilities.sampling(effective whenenforceStrictCapabilities: true).Tests & docs:
app-bridge.test.ts..examples.tsregions for bothAppandAppBridgeJSDoc.What we get for free
maxTokensrequired (cost-control guard rail)temperature,stopSequences,modelPreferences,metadatatools/toolChoice/tool_use/tool_resultcontent blocksstopReason: "toolUse"+ array content for parallel tool calls_metaon messagessampling/createMessagehandler.https://claude.ai/code/session_01ENGWTtsfcyP4S6fTWtUMAh