@@ -47,6 +47,8 @@ import {
4747 McpUiToolResultNotificationSchema ,
4848 McpUiRequestDisplayModeRequest ,
4949 McpUiRequestDisplayModeResultSchema ,
50+ McpUiSamplingCreateMessageRequest ,
51+ McpUiSamplingCreateMessageResultSchema ,
5052} from "./types" ;
5153import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js" ;
5254
@@ -970,6 +972,75 @@ export class App extends Protocol<AppRequest, AppNotification, AppResult> {
970972 ) ;
971973 }
972974
975+ /**
976+ * Request an LLM completion from the host.
977+ *
978+ * Sends a `sampling/createMessage` request to the host, which fulfills it
979+ * using its configured LLM provider. This enables Views to leverage the
980+ * host's language model capabilities without needing direct API access.
981+ *
982+ * The host must advertise `sampling` in its capabilities during initialization
983+ * for this method to succeed. Check capabilities before calling:
984+ *
985+ * ```typescript
986+ * if (app.getHostCapabilities()?.sampling) {
987+ * const result = await app.createSamplingMessage({ ... });
988+ * }
989+ * ```
990+ *
991+ * @param params - Sampling request parameters
992+ * - `messages` - Conversation messages providing context for the completion
993+ * - `systemPrompt` - Optional system prompt to guide LLM behavior
994+ * - `maxTokens` - Optional maximum number of tokens to generate
995+ * @param options - Request options (timeout, abort signal, etc.)
996+ * @returns Result containing the LLM's response with model info, role, content, and stop reason
997+ *
998+ * @throws {McpError } With `MethodNotFound` code if the host does not support sampling
999+ * @throws {McpError } With `InvalidRequest` code if the request is malformed
1000+ * @throws {Error } If the request times out or the connection is lost
1001+ *
1002+ * @example Basic text completion
1003+ * ```typescript
1004+ * const result = await app.createSamplingMessage({
1005+ * messages: [
1006+ * { role: "user", content: { type: "text", text: "Summarize this data" } },
1007+ * ],
1008+ * maxTokens: 512,
1009+ * });
1010+ * console.log(result.content.text);
1011+ * ```
1012+ *
1013+ * @example Multi-turn conversation with system prompt
1014+ * ```typescript
1015+ * const result = await app.createSamplingMessage({
1016+ * messages: [
1017+ * { role: "user", content: { type: "text", text: "What is 2+2?" } },
1018+ * { role: "assistant", content: { type: "text", text: "4" } },
1019+ * { role: "user", content: { type: "text", text: "And 3+3?" } },
1020+ * ],
1021+ * systemPrompt: "You are a helpful math tutor.",
1022+ * maxTokens: 256,
1023+ * });
1024+ * ```
1025+ *
1026+ * @see {@link McpUiSamplingCreateMessageRequest `McpUiSamplingCreateMessageRequest` } for the request type
1027+ * @see {@link McpUiSamplingCreateMessageResult `McpUiSamplingCreateMessageResult` } for the result type
1028+ * @see {@link McpUiHostCapabilities `McpUiHostCapabilities` } for checking sampling support
1029+ */
1030+ createSamplingMessage (
1031+ params : McpUiSamplingCreateMessageRequest [ "params" ] ,
1032+ options ?: RequestOptions ,
1033+ ) {
1034+ return this . request (
1035+ < McpUiSamplingCreateMessageRequest > {
1036+ method : "sampling/createMessage" ,
1037+ params,
1038+ } ,
1039+ McpUiSamplingCreateMessageResultSchema ,
1040+ options ,
1041+ ) ;
1042+ }
1043+
9731044 /**
9741045 * Notify the host of UI size changes.
9751046 *
0 commit comments