2.0.0
Use this when you need to respond to server requests for LLM completions.
MCP servers can request LLM completions from clients during tool execution. This enables servers to delegate AI reasoning to the client, which controls which LLM is used and how requests are made.
Handler Template
Handler Parameters
SamplingParams
str | None
Optional system prompt the server wants to use
ModelPreferences | None
Server preferences for model selection (hints, cost/speed/intelligence priorities)
float | None
Sampling temperature
int
Maximum tokens to generate
list[str] | None
Stop sequences for sampling
list[Tool] | None
Tools the LLM can use during sampling
ToolChoice | None
Tool usage behavior (
auto, required, or none)Built-in Handlers
FastMCP provides built-in handlers for OpenAI and Anthropic APIs that support the full sampling API including tool use.OpenAI Handler
New in version2.11.0
Install the OpenAI handler with
pip install fastmcp[openai].Anthropic Handler
New in version2.14.1
Install the Anthropic handler with
pip install fastmcp[anthropic].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: