Skip to content

Commit 926dcb4

Browse files
ochafikclaude
andauthored
feat: MCP app sampling support via stock SDK types (alt to #477) (#530)
* feat: MCP app sampling support via stock SDK types 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 * feat(app): guard createSamplingMessage before handshake completes --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 4f89a6f commit 926dcb4

10 files changed

Lines changed: 323 additions & 1 deletion

File tree

specification/draft/apps.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,10 @@ UI iframes can use the following subset of standard MCP protocol messages:
516516

517517
- `resources/read` - Read resource content
518518

519+
**Sampling:**
520+
521+
- `sampling/createMessage` - Request an LLM completion from the host (uses the standard MCP [`CreateMessageRequest`](https://modelcontextprotocol.io/specification/2025-11-25/client/sampling) / `CreateMessageResult` types, including SEP-1577 `tools` / `toolChoice` / `tool_use` content blocks). The host has full discretion over model selection and SHOULD apply rate limiting, cost controls, and user approval (human-in-the-loop). Apps MUST check `hostCapabilities.sampling` before sending this request, and `hostCapabilities.sampling.tools` before including `tools` in the request params.
522+
519523
**Notifications:**
520524

521525
- `notifications/message` - Log messages to host
@@ -662,6 +666,14 @@ interface HostCapabilities {
662666
};
663667
/** Host accepts log messages. */
664668
logging?: {};
669+
/**
670+
* Host supports LLM sampling (sampling/createMessage) from the view.
671+
* Mirrors MCP ClientCapabilities.sampling so hosts can pass it through.
672+
*/
673+
sampling?: {
674+
/** Host supports tool use via `tools` and `toolChoice` params (SEP-1577). */
675+
tools?: {};
676+
};
665677
/** Sandbox configuration applied by the host. */
666678
sandbox?: {
667679
/** Permissions granted by the host (camera, microphone, geolocation, clipboard-write). */

src/app-bridge.examples.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
1212
import {
1313
CallToolResult,
1414
CallToolResultSchema,
15+
CreateMessageRequest,
16+
CreateMessageResult,
1517
ListResourcesResultSchema,
1618
ReadResourceResultSchema,
1719
ListPromptsResultSchema,
@@ -228,6 +230,26 @@ function AppBridge_oncalltool_forwardToServer(
228230
//#endregion AppBridge_oncalltool_forwardToServer
229231
}
230232

233+
/**
234+
* Example: Forward sampling requests to your LLM provider.
235+
*/
236+
function AppBridge_oncreatesamplingmessage_forwardToLlm(
237+
bridge: AppBridge,
238+
myLlmProvider: {
239+
complete: (
240+
params: CreateMessageRequest["params"],
241+
opts: { signal: AbortSignal },
242+
) => Promise<CreateMessageResult>;
243+
},
244+
) {
245+
//#region AppBridge_oncreatesamplingmessage_forwardToLlm
246+
bridge.oncreatesamplingmessage = async (params, extra) => {
247+
// Apply rate limiting, user approval, cost controls here
248+
return await myLlmProvider.complete(params, { signal: extra.signal });
249+
};
250+
//#endregion AppBridge_oncreatesamplingmessage_forwardToLlm
251+
}
252+
231253
/**
232254
* Example: Forward list resources requests to the MCP server.
233255
*/

src/app-bridge.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,43 @@ describe("App <-> AppBridge integration", () => {
737737
expect(result.content).toEqual(resultContent);
738738
});
739739

740+
it("oncreatesamplingmessage setter registers handler for sampling/createMessage requests", async () => {
741+
// Re-create bridge with sampling capability so App's capability check passes
742+
bridge = new AppBridge(null, testHostInfo, {
743+
...testHostCapabilities,
744+
sampling: { tools: {} },
745+
});
746+
747+
const receivedParams: unknown[] = [];
748+
bridge.oncreatesamplingmessage = async (params) => {
749+
receivedParams.push(params);
750+
return {
751+
role: "assistant",
752+
content: { type: "text", text: "Hello from the model" },
753+
model: "test-model",
754+
stopReason: "endTurn",
755+
};
756+
};
757+
758+
await bridge.connect(bridgeTransport);
759+
await app.connect(appTransport);
760+
761+
expect(app.getHostCapabilities()?.sampling?.tools).toEqual({});
762+
763+
const result = await app.createSamplingMessage({
764+
messages: [{ role: "user", content: { type: "text", text: "Hi" } }],
765+
maxTokens: 50,
766+
});
767+
768+
expect(receivedParams).toHaveLength(1);
769+
expect(receivedParams[0]).toMatchObject({ maxTokens: 50 });
770+
expect(result.model).toEqual("test-model");
771+
expect(result.content).toEqual({
772+
type: "text",
773+
text: "Hello from the model",
774+
});
775+
});
776+
740777
it("ondownloadfile setter registers handler for ui/download-file requests", async () => {
741778
const downloadParams = {
742779
contents: [

src/app-bridge.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import {
55
CallToolRequestSchema,
66
CallToolResult,
77
CallToolResultSchema,
8+
CreateMessageRequest,
9+
CreateMessageRequestSchema,
10+
CreateMessageResult,
11+
CreateMessageResultWithTools,
812
EmptyResult,
913
Implementation,
1014
ListPromptsRequest,
@@ -1049,6 +1053,49 @@ export class AppBridge extends ProtocolWithEvents<
10491053
);
10501054
}
10511055

1056+
/**
1057+
* Register a handler for LLM sampling requests from the view.
1058+
*
1059+
* The view sends standard MCP `sampling/createMessage` requests to obtain
1060+
* LLM completions via the host's model connection. The host has full
1061+
* discretion over which model to use and SHOULD apply rate limiting,
1062+
* cost controls, and user approval (human-in-the-loop) before sampling.
1063+
*
1064+
* Hosts that register this handler SHOULD advertise `sampling` (and
1065+
* `sampling.tools` if tool-calling is supported) in
1066+
* {@link McpUiHostCapabilities `McpUiHostCapabilities`}.
1067+
*
1068+
* @param callback - Handler that receives `CreateMessageRequest` params and
1069+
* returns a `CreateMessageResult` (or `CreateMessageResultWithTools` when
1070+
* `params.tools` was provided)
1071+
* - `params` - Standard MCP sampling params (messages, maxTokens, tools, etc.)
1072+
* - `extra` - Request metadata (abort signal, session info)
1073+
*
1074+
* @example Forward to your LLM provider
1075+
* ```ts source="./app-bridge.examples.ts#AppBridge_oncreatesamplingmessage_forwardToLlm"
1076+
* bridge.oncreatesamplingmessage = async (params, extra) => {
1077+
* // Apply rate limiting, user approval, cost controls here
1078+
* return await myLlmProvider.complete(params, { signal: extra.signal });
1079+
* };
1080+
* ```
1081+
*
1082+
* @see `CreateMessageRequest` from @modelcontextprotocol/sdk for the request type
1083+
* @see `CreateMessageResult` / `CreateMessageResultWithTools` from @modelcontextprotocol/sdk for result types
1084+
*/
1085+
set oncreatesamplingmessage(
1086+
callback: (
1087+
params: CreateMessageRequest["params"],
1088+
extra: RequestHandlerExtra,
1089+
) => Promise<CreateMessageResult | CreateMessageResultWithTools>,
1090+
) {
1091+
this.setRequestHandler(
1092+
CreateMessageRequestSchema,
1093+
async (request, extra) => {
1094+
return callback(request.params, extra);
1095+
},
1096+
);
1097+
}
1098+
10521099
/**
10531100
* Notify the view that the MCP server's tool list has changed.
10541101
*

src/app.examples.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,54 @@ async function App_callServerTool_fetchWeather(app: App) {
334334
//#endregion App_callServerTool_fetchWeather
335335
}
336336

337+
/**
338+
* Example: Simple LLM completion via host sampling.
339+
*/
340+
async function App_createSamplingMessage_simple(app: App) {
341+
//#region App_createSamplingMessage_simple
342+
const result = await app.createSamplingMessage({
343+
messages: [
344+
{
345+
role: "user",
346+
content: { type: "text", text: "Summarize this in one line." },
347+
},
348+
],
349+
maxTokens: 100,
350+
});
351+
console.log(result.content);
352+
//#endregion App_createSamplingMessage_simple
353+
}
354+
355+
/**
356+
* Example: Agentic loop with tools (requires host sampling.tools capability).
357+
*/
358+
async function App_createSamplingMessage_withTools(
359+
app: App,
360+
messages: import("@modelcontextprotocol/sdk/types.js").SamplingMessage[],
361+
) {
362+
//#region App_createSamplingMessage_withTools
363+
if (!app.getHostCapabilities()?.sampling?.tools) return;
364+
365+
const result = await app.createSamplingMessage({
366+
messages,
367+
maxTokens: 1024,
368+
tools: [
369+
{
370+
name: "get_weather",
371+
description: "Get the current weather",
372+
inputSchema: {
373+
type: "object",
374+
properties: { city: { type: "string" } },
375+
},
376+
},
377+
],
378+
});
379+
if (result.stopReason === "toolUse") {
380+
// result.content may be an array containing tool_use blocks
381+
}
382+
//#endregion App_createSamplingMessage_withTools
383+
}
384+
337385
/**
338386
* Example: Read a video resource and play it.
339387
*/

src/app.ts

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import {
88
CallToolRequestSchema,
99
CallToolResult,
1010
CallToolResultSchema,
11+
CreateMessageRequest,
12+
CreateMessageResult,
13+
CreateMessageResultSchema,
14+
CreateMessageResultWithTools,
15+
CreateMessageResultWithToolsSchema,
1116
EmptyResultSchema,
1217
Implementation,
1318
ListResourcesRequest,
@@ -802,7 +807,15 @@ export class App extends ProtocolWithEvents<
802807
* @internal
803808
*/
804809
assertCapabilityForMethod(method: AppRequest["method"]): void {
805-
// TODO
810+
switch (method) {
811+
case "sampling/createMessage":
812+
if (!this._hostCapabilities?.sampling) {
813+
throw new Error(
814+
`Host does not support sampling (required for ${method})`,
815+
);
816+
}
817+
break;
818+
}
806819
}
807820

808821
/**
@@ -1011,6 +1024,90 @@ export class App extends ProtocolWithEvents<
10111024
);
10121025
}
10131026

1027+
/**
1028+
* Request an LLM completion from the host (standard MCP `sampling/createMessage`).
1029+
*
1030+
* Enables the app to use the host's model connection for completions. The host
1031+
* has full discretion over which model to select and MAY modify or reject the
1032+
* request (human-in-the-loop). Check {@link getHostCapabilities `getHostCapabilities`}`()?.sampling`
1033+
* before calling — hosts without this capability will reject the request.
1034+
*
1035+
* This method reuses the stock MCP `CreateMessageRequest` shape. When `params.tools`
1036+
* is provided, the result is parsed with the extended schema that permits
1037+
* `stopReason: "toolUse"` and array content containing `tool_use` blocks.
1038+
*
1039+
* @param params - Standard MCP `CreateMessageRequest` params (messages, maxTokens,
1040+
* systemPrompt, temperature, modelPreferences, tools, toolChoice, etc.)
1041+
* @param options - Request options (timeout, abort signal)
1042+
* @returns `CreateMessageResult` (single content block) or `CreateMessageResultWithTools`
1043+
* (array content, may include `tool_use` blocks) depending on whether `tools` was set
1044+
*
1045+
* @throws {Error} If the host rejects the request or does not support sampling
1046+
* @throws {Error} If the request times out or the connection is lost
1047+
*
1048+
* @example Simple completion
1049+
* ```ts source="./app.examples.ts#App_createSamplingMessage_simple"
1050+
* const result = await app.createSamplingMessage({
1051+
* messages: [
1052+
* {
1053+
* role: "user",
1054+
* content: { type: "text", text: "Summarize this in one line." },
1055+
* },
1056+
* ],
1057+
* maxTokens: 100,
1058+
* });
1059+
* console.log(result.content);
1060+
* ```
1061+
*
1062+
* @example Agentic loop with tools
1063+
* ```ts source="./app.examples.ts#App_createSamplingMessage_withTools"
1064+
* if (!app.getHostCapabilities()?.sampling?.tools) return;
1065+
*
1066+
* const result = await app.createSamplingMessage({
1067+
* messages,
1068+
* maxTokens: 1024,
1069+
* tools: [
1070+
* {
1071+
* name: "get_weather",
1072+
* description: "Get the current weather",
1073+
* inputSchema: {
1074+
* type: "object",
1075+
* properties: { city: { type: "string" } },
1076+
* },
1077+
* },
1078+
* ],
1079+
* });
1080+
* if (result.stopReason === "toolUse") {
1081+
* // result.content may be an array containing tool_use blocks
1082+
* }
1083+
* ```
1084+
*
1085+
* @see `CreateMessageRequest` from @modelcontextprotocol/sdk for the request type
1086+
* @see `CreateMessageResult` / `CreateMessageResultWithTools` from @modelcontextprotocol/sdk for result types
1087+
*/
1088+
async createSamplingMessage(
1089+
params: CreateMessageRequest["params"] & { tools?: undefined },
1090+
options?: RequestOptions,
1091+
): Promise<CreateMessageResult>;
1092+
async createSamplingMessage(
1093+
params: CreateMessageRequest["params"],
1094+
options?: RequestOptions,
1095+
): Promise<CreateMessageResultWithTools>;
1096+
async createSamplingMessage(
1097+
params: CreateMessageRequest["params"],
1098+
options?: RequestOptions,
1099+
): Promise<CreateMessageResult | CreateMessageResultWithTools> {
1100+
this._assertInitialized("createSamplingMessage");
1101+
const resultSchema = params.tools
1102+
? CreateMessageResultWithToolsSchema
1103+
: CreateMessageResultSchema;
1104+
return await this.request(
1105+
{ method: "sampling/createMessage", params },
1106+
resultSchema,
1107+
options,
1108+
);
1109+
}
1110+
10141111
/**
10151112
* Send a message to the host's chat interface.
10161113
*

src/generated/schema.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)