Principle:AI QL Tuui MCP Sampling
| Knowledge Sources | |
|---|---|
| Domains | MCP, LLM, Sampling |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
A server-initiated request pattern where an MCP server asks the connected client to generate an LLM completion on its behalf, with the client controlling which model and parameters are used.
Description
MCP Sampling allows an MCP server to request LLM completions without having direct access to an LLM API. The server sends a sampling/createMessage request containing messages, an optional system prompt, temperature, and max tokens. The client (host application) receives this request, routes it to its configured LLM, and returns the generated response. This gives the client control over model selection, API keys, and cost management, while allowing servers to leverage LLM capabilities. The request/response is asynchronous and uses a UUID-based correlation channel.
Usage
Use this principle when designing MCP servers that need LLM capabilities but should not manage their own API keys, or when designing MCP clients that need to mediate LLM access for connected servers.
Theoretical Basis
The sampling roundtrip:
MCP Server → Client.setRequestHandler(SamplingRequestSchema):
1. Server calls sampling/createMessage with { messages, systemPrompt?, temperature?, maxTokens? }
2. Client handler forwards to host application via IPC
3. Host application:
a. Convert MCP SamplingMessage[] → OpenAI ChatCompletionMessage[]
b. Call createCompletion(session, samplingParams)
c. Extract assistant response
4. Return SamplingResponse (CreateMessageResult) to server
The SamplingRequestParams includes:
messages: SamplingMessage[]— conversation context (role + content)systemPrompt?: string— optional system prompttemperature?: number— sampling temperaturemaxTokens?: number— max response tokens
Content translation uses convertItem to map MCP content types to OpenAI format.