Skip to main content
MCP servers can request LLM completions from clients. The client handles these requests through a sampling handler callback.

Sampling Handler

Provide a sampling_handler function when creating the client:

Handler Parameters

The sampling handler receives three parameters:

Sampling Handler Parameters

SamplingMessage
Sampling Message Object
SamplingParams
Sampling Parameters Object
RequestContext
Request Context Object

Basic Example

If the client doesn’t provide a sampling handler, servers can optionally configure a fallback handler. See Server Sampling for details.

Sampling Capabilities

When you provide a sampling_handler, FastMCP automatically advertises full sampling capabilities to the server, including tool support. To disable tool support (for simpler handlers that don’t support tools), pass sampling_capabilities explicitly:

Built-in Handlers

FastMCP provides built-in sampling handlers for OpenAI and Anthropic APIs. These handlers support the full sampling API including tool use, handling message conversion and response formatting automatically.

OpenAI Handler

The OpenAI handler works with OpenAI’s API and any OpenAI-compatible provider:
For OpenAI-compatible APIs (like local models), pass a custom client:
Install the OpenAI handler with pip install fastmcp[openai].

Anthropic Handler

The Anthropic handler uses Claude models via the Anthropic API:
You can pass a custom client for advanced configuration:
Install the Anthropic handler with pip install fastmcp[anthropic].

Tool Execution

Tool execution happens on the server side. The client’s role is to pass tools to the LLM and return the LLM’s response (which may include tool use requests). The server then executes the tools and may send follow-up sampling requests with tool results.
To implement a custom sampling handler, see the handler source code as a reference.