CONCEPT Sampling flips the direction: the server asks the LLM
Normally the client drives the model. Sampling reverses it: a server asks the
client's LLM for a completion. The spec: MCP "provides a standardized way for servers to request LLM
sampling ('completions' or 'generations') from language models via clients … enabling servers to leverage AI
capabilities — with no server API keys necessary." The server sends a
sampling/createMessage request; the client runs the model and returns the generation.
This is what lets a server add agentic behavior — an LLM call nested inside another server feature (a tool, a resource read) — without the server owning or paying for a model. The client keeps control over model access, selection, and permissions.
| Aspect | Sampling |
|---|---|
| Direction | Server → Client (opposite of tools/resources/prompts) |
| Method | sampling/createMessage |
| Capability | Declared by the client: "sampling": {} |
| Key benefit | Server gets AI without its own API key or model |
| Model choice | Abstracted via modelPreferences (hints + priorities) |
| Safety | Human-in-the-loop — user SHOULD approve/edit before & after |
sampling (they own the
model); servers initiate the request. Contrast with tools/resources/prompts, which servers declare and
clients call.CONCEPT Model preferences and human-in-the-loop
A server can't just name a model — the client may use a different provider. So sampling uses
modelPreferences: advisory hints (substring matches like claude-3-sonnet,
evaluated in order) plus three normalized 0–1 priorities — costPriority, speedPriority,
intelligencePriority. The client maps these to whatever model it actually has. Other params include
messages, systemPrompt, and maxTokens. The response returns the
assistant content, the actual model used, and a stopReason.
DIAGRAM Server request → human review → generation
CODE A sampling/createMessage request and result
The server supplies messages and abstract modelPreferences; the client (after human review)
runs its own model and returns the generation, naming the model it actually used.
// server → client : request a completion from the CLIENT's LLM { "method": "sampling/createMessage", "params": { "messages": [ { "role": "user", "content": { "type": "text", "text": "What is the capital of France?" } } ], "modelPreferences": { "hints": [ { "name": "claude-3-sonnet" } ], "intelligencePriority": 0.8, "speedPriority": 0.5 }, "systemPrompt": "You are a helpful assistant.", "maxTokens": 100 } } // client → server : the generation (after human-in-the-loop review) { "role": "assistant", "content": { "type": "text", "text": "The capital of France is Paris." }, "model": "claude-3-sonnet-20240307", "stopReason": "endTurn" }
TRADE-OFFS Sampling vs. the server holding its own API key
✅ Sampling (borrow the client's LLM)
- No server-side API key or model billing
- Client keeps control of model access & permissions
- Enables nested, agentic server behavior
⚠️ The constraints
- Requires the client to support the
samplingcapability - Human-in-the-loop adds latency & possible denial
- Only a hint at the model — client makes the final choice
EXAM TRAPS
sampling/createMessage.modelPreferences
(advisory hints + cost/speed/intelligence priorities); the client makes the final selection.PRACTICE
Q1 · In one line, what does sampling do and why is it useful?
It lets a server request a completion from the client's LLM via
sampling/createMessage — so the server can add agentic/AI behavior without its own API key or
model, while the client retains control.
Q2 · Which side declares the sampling capability, and why?
The client — because the client owns the model. The server then initiates
sampling/createMessage requests against that capability.
Q3 · How does the server influence model choice without naming a specific model?
Through modelPreferences: advisory hints (substring matches, in
preference order) plus normalized costPriority, speedPriority, and
intelligencePriority. The client maps these to a model it actually has.