fastmcp.server.sampling.run
Sampling types and helper functions for FastMCP servers.
Functions
determine_handler_mode
context: The MCP context.needs_tools: Whether the sampling request requires tool support.
- True if fallback handler should be used, False to use client.
ValueError: If client lacks required capability and no fallback configured.
call_sampling_handler
execute_tools
tool_calls: List of tool use requests from the LLM.tool_map: Mapping from tool name to SamplingTool.mask_error_details: If True, mask detailed error messages from tool execution. When masked, only generic error messages are returned to the LLM. Tools can explicitly raise ToolError to bypass masking when they want to provide specific error messages to the LLM.
- List of tool result content blocks.
Classes
SamplingResult
Result of a sampling operation.
Attributes:
text: The text representation of the result (raw text or JSON for structured).result: The typed result (str for text, parsed object for structured output).history: All messages exchanged during sampling.

