Skip to main content
Sampling allows your MCP server tools to request LLM completions from the client during execution. This enables tools to leverage the client’s LLM capabilities for tasks like sentiment analysis, text generation, or decision-making.

How It Works

When a tool needs an LLM response, it can use the Context to send a sampling request to the client. The client’s configured LLM processes the request and returns the result to the tool.

Basic Usage

Use the sample() method on the context object to request LLM completions:

Use Case Example

A content moderation server that uses the client’s LLM to analyze user-generated content:
This allows the tool to leverage sophisticated LLM reasoning without having to integrate an LLM provider directly.

Important Notes

  • The client must provide a sampling_callback to support sampling requests
  • If no callback is configured, sampling requests will fail with an error
  • Sampling requests are processed by the client’s configured LLM, not the server

Next Steps