DS DevShelfHub Projects · AI tools
Tutorials / MCP / Sampling
MCP Intermediate · 8 min read Page 9 of 23

Sampling — Servers Asking the LLM

By DevShelfHub

Sampling is the inversion of "tool use": the server asks the client to run an LLM completion on its behalf. The client owns model selection, rate limiting, and user approval — the server stays model-independent.

Series progress9 / 23
MCP sampling tutorial — Sampling: Servers Asking the LLM

Why sampling matters

Imagine a travel server with a findBestFlight tool. It gathers 47 flight options from APIs — then needs LLM reasoning to balance price, layovers, and the user's preferences. Without sampling the server would have to ship an Anthropic/OpenAI key. With sampling it just asks the client via sampling/createMessage.

One-line version: Sampling lets servers borrow the user's LLM — never their API key.

A sampling request

sampling/createMessage
{
  "jsonrpc": "2.0",
  "id": 42,
  "method": "sampling/createMessage",
  "params": {
    "messages": [
      { "role": "user", "content": { "type": "text", "text": "Pick the best flight for a morning departure." } }
    ],
    "modelPreferences": {
      "hints": [{ "name": "claude-3-sonnet" }, { "name": "claude" }],
      "intelligencePriority": 0.8,
      "speedPriority": 0.5,
      "costPriority": 0.2
    },
    "systemPrompt": "You are a helpful travel agent.",
    "maxTokens": 200
  }
}
response
{
  "jsonrpc": "2.0",
  "id": 42,
  "result": {
    "role": "assistant",
    "content": { "type": "text", "text": "I recommend AA1234 at 7:15am..." },
    "model": "claude-sonnet-4-6",
    "stopReason": "endTurn"
  }
}

Model preferences

Three priority axes guide model selection — each 0 to 1:

  • costPriority — prefer cheaper models.
  • speedPriority — prefer faster models.
  • intelligencePriority — prefer more capable models.

The hints array is an ordered list of name substrings. Clients map them flexibly — if a user has no Claude access, a "claude-3-sonnet" hint might resolve to gemini-1.5-pro.

Context inclusion

includeContext controls what the client adds before calling the model:

  • "none" — strictly the messages provided.
  • "thisServer" — include context from the requesting server.
  • "allServers" — include context from every connected MCP server.

Clients MAY override this field — they will refuse to leak sensitive cross-server data even if a server asks.

Sampling with tools (SEP-1577)

Servers can include tool definitions in sampling requests, then handle ToolUseContent blocks the model returns. This lets servers run multi-turn agentic loops while the client keeps approving each step.

Trust boundary: the user MUST be able to see and approve each sampling call. Servers don't get implicit access to the LLM.

Quick summary

  • Servers ask the client to run LLM completions via sampling/createMessage
  • Three priority axes plus model hints bias selection without locking in a provider
  • includeContext controls cross-server data sharing
  • Tools in sampling enable agentic loops — but every call is user-approvable

MCP Sampling FAQ

What is MCP Sampling?

MCP Sampling is the mechanism that lets MCP servers request LLM completions through the host client — the opposite of tool use. The server calls sampling/createMessage and the client decides whether to approve the request and which model to use.

Why would an MCP server need to call an LLM?

Servers use sampling to build agentic loops — for example, having the LLM analyse retrieved data, generate code, or make decisions during a multi-step tool execution. Sampling lets servers be AI-powered without bundling their own LLM client.

Who controls which model is used in MCP Sampling?

The host client always controls model selection. Servers can express preferences via modelPreferences (hints and priority values), but the final model choice belongs to the client — the server cannot force a specific model.

Is MCP Sampling safe from prompt injection?

Sampling is a security-sensitive feature. Clients should display sampling requests to users before approving, filter harmful content, and be cautious about tool calls within sampling responses. Servers should minimise context sent to only what is needed.

Can MCP Sampling include tool calls?

Yes. A sampling response can include tool_use blocks that the server can execute, enabling multi-turn agentic loops. Each tool call in a sampling response is still subject to normal user approval flows on the client side.

Quick jump:API Reference