Home Glossary
HomeGlossaryMCP Sampling
MCP Glossary

MCP Sampling

TL;DR

MCP sampling lets a server request a completion from the host's LLM mid-session. This inverts the usual flow: normally the LLM calls server tools, but with sampling the server can ask the LLM to reason, summarize, or classify — enabling servers to build mini-agents of their own.

In depth

Sampling is one of MCP's most powerful features. Normally, LLMs are the client: they call server tools. Sampling inverts this — the server sends a `sampling/createMessage` request back up to the client, which forwards it to its LLM. The server gets a completion back and uses it to shape its next response.

Use cases: a server receives a user query and wants to classify its intent before choosing which data source to query. A server wants to summarize a long document before returning it. A server wants to validate user input conversationally.

Sampling requires the client to declare the `sampling` capability during initialize. The host typically shows the user a prompt before forwarding sampling requests — this preserves user control over LLM invocation and cost.

Sampling makes MCP servers composable mini-agents. Instead of every server being dumb I/O, servers can reason using the client's LLM, opening up sophisticated multi-hop workflows.

Code example

// Server → Client: sampling request
{
  "jsonrpc": "2.0",
  "method": "sampling/createMessage",
  "params": {
    "messages": [
      { "role": "user", "content": { "type": "text", "text": "Classify: 'refund order 123' → intent?" } }
    ],
    "maxTokens": 50,
    "modelPreferences": { "speedPriority": 0.8 }
  },
  "id": 5
}

// Client → Server: LLM response
{
  "jsonrpc": "2.0",
  "result": {
    "role": "assistant",
    "content": { "type": "text", "text": "refund_request" },
    "model": "claude-haiku-4.5"
  },
  "id": 5
}

Examples

  • 1
    A knowledge-base server sampling the LLM to pick the right retrieval strategy per query
  • 2
    A support-ticket server sampling to triage priority before returning
  • 3
    A natural-language SQL server that samples to translate query intent
  • 4
    An email draft server that samples to propose multiple tone variants
  • 5
    A code-review server sampling the LLM to categorize the diff

What it's NOT

  • ✗Sampling is NOT a standard LLM API call — it goes through the host's LLM, not the server's.
  • ✗Sampling is NOT free — the cost is paid by the user's host, so always use sparingly.
  • ✗Sampling is NOT required — most MCP servers work fine without ever sampling.
  • ✗Sampling is NOT the same as function calling — it's the LLM being used as a tool BY the server.

Related terms

MCP CapabilityMCP ServerLarge Language Model (LLM)Model Context Protocol (MCP)

See also

  • MCP Sampling

Frequently asked questions

Does every MCP host support sampling?

No — sampling is optional. Claude Desktop supports it; some other hosts do not. Always check `capabilities.sampling`.

Can sampling be abused?

Theoretically — a malicious server could flood sampling requests. Hosts should rate-limit and require user approval.

Can servers pick the model for sampling?

They can express preferences (speed vs quality, cost vs capability), but the host chooses the actual model.

Build with MCP

Browse 300+ MCP servers, explore recipes, or continue learning the MCP vocabulary.

Browse MarketplaceAll terms