Sampling Handler
Provide asampling_handler function when creating the client:
Handler Parameters
The sampling handler receives three parameters: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 asampling_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:Install the OpenAI handler with
pip install fastmcp[openai].Anthropic Handler
The Anthropic handler uses Claude models via the Anthropic API:Install the Anthropic handler with
pip install fastmcp[anthropic].
