MCP Sampling

MCP

Definition

An advanced MCP capability that allows MCP servers to request LLM completions through the client, enabling servers to use AI without direct API access. The client controls model selection, permissions, and billing. Used for AI-powered tool implementations that need their own Claude calls.

Example Usage

An MCP server uses sampling to request Claude to summarize a document as part of processing a tool call, without the server needing its own API key.

Why It Matters for the CCA-F Exam

Sampling is one of the most exam-memorable MCP features because it reverses the expected request direction. Exam questions test whether candidates understand that the server initiates the LLM call (via the client) rather than the client. Questions also test the control model: the client — not the server — controls which model is used and who is billed.

In Depth

MCP Sampling is an advanced capability that inverts the normal flow of the Model Context Protocol: instead of the client sending requests to the server, the server requests an LLM completion from the client. When a server is processing a tool call and needs AI reasoning as part of that processing, it can send a sampling/createMessage request back to the client, which forwards it to the model (e.g., Claude), and then returns the completion result to the server.

Why This Matters

Normally, MCP servers are dumb executors — they run code, query databases, call APIs. Sampling makes them intelligent collaborators. An MCP server can use sampling to:

  • Summarize a long document before returning it as a tool result
  • Classify an incoming request to decide which internal logic to run
  • Generate a structured object from unstructured data as part of a tool
  • Implement a mini agentic loop inside a single tool invocation

Critically, the server never needs its own Anthropic API key. Billing, model selection, and permissions all remain with the client. This is a deliberate architectural choice: the operator (who configured the client) retains control over which models are used and how much is spent.

Control Flow

User → Client → Model → [tool_use] → Server
                                          ↓
                              sampling/createMessage
                                          ↓
                               Client → Model
                                          ↓
                              completion result
                                          ↓
                              Server processes result
                                          ↓
                               tool_result → Client → Model

This creates a nested LLM call within a tool invocation — a form of multi-agent coordination entirely within the MCP protocol.

Sampling vs Direct API Access

AspectMCP SamplingServer calls API directly
API key needed on serverNoYes
Model selection controlled byClient (operator)Server developer
Billing attributed toClientServer operator
Works in air-gapped serverYes (if client has access)No
Audit trailClient-sideServer-side

Sampling is closely related to the MCP client role in the architecture — the client acts as the bridge between the server's completion needs and the upstream model provider. It also interacts with MCP Roots: a server that uses sampling to process file content should still respect root boundaries declared by the client. For the broader context of how tool use flows through MCP, see Tool Use Flow & Mechanics.

How It Compares

DimensionNormal MCP flowSampling
InitiatorClientServer
LLM completion requested byClientServer (through client)
Model selectionClientClient (server cannot override)
API key locationClientClient only
Use caseTool executionAI-augmented tool execution

Example

MCP server requesting an LLM completion via sampling to summarize a document as part of a tool call

javascript
// Server-side: requesting a completion via sampling
const result = await server.requestSampling({
  messages: [
    {
      role: "user",
      content: {
        type: "text",
        text: `Summarize this in one sentence: ${longDocument}`
      }
    }
  ],
  maxTokens: 100
});
const summary = result.content[0].text;

Frequently Asked Questions

Can an MCP server use sampling to call any model, or only the one the client is using?

The server can include a model preference in the sampling request, but the client is not required to honor it. The client has final authority over model selection. This is intentional — it prevents a malicious or misconfigured server from forcing the client to use expensive or inappropriate models.

Is MCP Sampling the same as a nested agent or subagent?

They are similar in spirit but different in mechanism. A subagent in the Claude Agent SDK is a fully independent agent with its own context window and tool access, invoked via the Task tool. MCP Sampling is a lighter-weight, protocol-level request for a single LLM completion, mediated through the MCP client. Sampling does not spawn an autonomous agent loop by itself.