D2 · Tool Design & MCP › Cluster 6 · MCP Primitives

6.4 · Sampling (Server → Client LLM Calls)

The server asks the model

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.

AspectSampling
DirectionServer → Client (opposite of tools/resources/prompts)
Methodsampling/createMessage
CapabilityDeclared by the client: "sampling": {}
Key benefitServer gets AI without its own API key or model
Model choiceAbstracted via modelPreferences (hints + priorities)
SafetyHuman-in-the-loop — user SHOULD approve/edit before & after
🔑 Capability ownership is a trap: clients declare 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.

⚠️ The spec insists there SHOULD always be a human in the loop able to deny sampling requests — review and edit the prompt before it runs, and review the response before it goes back to the server. A rejected request returns an error like "User rejected sampling request."

DIAGRAM Server request → human review → generation

sequenceDiagram participant Server participant Client participant User participant LLM Note over Server,Client: Server initiates sampling Server->>Client: sampling/createMessage Note over Client,User: Human-in-the-loop review Client->>User: Present request for approval User-->>Client: Approve / modify (or deny) Note over Client,LLM: Model interaction Client->>LLM: Forward approved request LLM-->>Client: Return generation Note over Client,User: Response review Client->>User: Present response for approval User-->>Client: Approve / modify Client-->>Server: Return approved response classDef srv fill:#d4edda,stroke:#28a745,color:#155724; classDef usr fill:#ffe8cc,stroke:#ff9f40,color:#9a4a0a; class Server srv; class User usr;

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 sampling capability
  • Human-in-the-loop adds latency & possible denial
  • Only a hint at the model — client makes the final choice

EXAM TRAPS

Trap: "Sampling is the client calling the server's model." → Backwards. The server requests a completion from the client's LLM via sampling/createMessage.
Trap: "The server needs its own API key for agentic behavior." → The whole point of sampling is that it does not — it borrows the client's model.
Trap: "The server picks the exact model." → It only sends modelPreferences (advisory hints + cost/speed/intelligence priorities); the client makes the final selection.
Trap: Forgetting human-in-the-loop. → The spec says users SHOULD be able to review, edit, or deny both the request and the response.

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.