New in version: 2.0.0
MCP servers can request LLM completions from clients. The client handles these requests through a sampling handler callback.
Sampling Handler
Provide asampling_handler
function when creating the client:
Handler Parameters
The sampling handler receives three parameters:Basic Example
Sampling fallback
Client support for sampling is optional, if the client does not support sampling, the server will report an error indicating that the client does not support sampling. Asampling_handler
can also be provided to the FastMCP server, which will be used to handle sampling requests if the client
does not support sampling. This sampling handler bypasses the client and sends sampling requests directly to the LLM provider.
Sampling handlers can be implemented using any LLM provider but a sample implementation for OpenAI is provided as a Contrib
module. Sampling lacks the full capabilities of typical LLM completions. For this reason, the OpenAI sampling handler, pointed at
a third-party provider’s OpenAI-compatible API, is often sufficient to implement a sampling handler.