Add Anthropic sampling handler and move handlers out of experimental#2584
Add Anthropic sampling handler and move handlers out of experimental#2584
Conversation
- Add tools and result_type parameters to ctx.sample() - Update OpenAI handler for tool content types - Client advertises sampling.tools capability by default - Collect tool results into single message with list content
Functions passed to ctx.sample(tools=[...]) are now auto-converted via SamplingTool.from_function(). Users can still use that method directly for custom name/description overrides.
- Add AnthropicSamplingHandler in server/sampling/anthropic.py - Consolidate all sampling examples into examples/sampling/ with rich output - Examples: text.py, structured_output.py, tool_use.py, server_fallback.py - Add anthropic optional dependency
- Move from fastmcp.experimental.sampling.handlers.openai to fastmcp.server.sampling.openai - Update docs and tests to use new import path - Remove experimental handlers directory
- Add Pre-built Handlers section to clients/sampling.mdx with Anthropic and OpenAI - Simplify servers/sampling.mdx to reference client docs for handler details - Add examples for Azure OpenAI and local model providers
Test Failure AnalysisSummary: Tests are failing with Root Cause: In class SessionKwargs(TypedDict, total=False):
read_timeout_seconds: datetime.timedelta | None
sampling_callback: SamplingFnT | None
sampling_capabilities: mcp.types.SamplingCapability | None # ❌ This doesn't exist!
list_roots_callback: ListRootsFnT | None
logging_callback: LoggingFnT | None
# ...However, # mcp/client/session.py:112-125
def __init__(
self,
read_stream: MemoryObjectReceiveStream[SessionMessage | Exception],
write_stream: MemoryObjectSendStream[SessionMessage],
read_timeout_seconds: timedelta | None = None,
sampling_callback: SamplingFnT | None = None,
elicitation_callback: ElicitationFnT | None = None,
list_roots_callback: ListRootsFnT | None = None,
logging_callback: LoggingFnT | None = None,
message_handler: MessageHandlerFnT | None = None,
client_info: types.Implementation | None = None,
*,
experimental_task_handlers: ExperimentalTaskHandlers | None = None,
) -> None:The Suggested Solution: Remove class SessionKwargs(TypedDict, total=False):
read_timeout_seconds: datetime.timedelta | None
sampling_callback: SamplingFnT | None
- sampling_capabilities: mcp.types.SamplingCapability | None
list_roots_callback: ListRootsFnT | None
logging_callback: LoggingFnT | None
elicitation_callback: ElicitationFnT | None
message_handler: MessageHandlerFnT | None
client_info: mcp.types.Implementation | NoneThis will allow the Failing TestsThe following tests are failing with this error:
All with the same error message. Error ExampleWorkflow Run: #20083028791 |
Adds an Anthropic sampling handler alongside the existing OpenAI handler, and moves both out of experimental into
fastmcp.server.sampling.Both handlers support tool use during sampling and work as either client handlers or server fallback handlers.
Also consolidates sampling examples into
examples/sampling/with rich formatted output showing the MCP flow (client → server → sampling → LLM).