fastmcp/examples/sampling
Rach Granville dd803a0d7f
docs: quote pip extras install examples (#4568)
* docs: quote pip extras install examples

* docs: quote remaining unquoted pip install extras

---------

Co-authored-by: Jeremiah Lowin <153965+jlowin@users.noreply.github.com>
2026-07-20 11:09:50 -04:00
..
README.md docs: quote pip extras install examples (#4568) 2026-07-20 11:09:50 -04:00
server_fallback.py Consolidate sampling examples and fix tool_choice bug (#2618) 2025-12-14 20:47:00 -05:00
structured_output.py [Doc]: fixing spelling issues in multiple files 2026-01-23 07:17:38 +01:00
text.py Consolidate sampling examples and fix tool_choice bug (#2618) 2025-12-14 20:47:00 -05:00
tool_use.py [Doc]: fixing spelling issues in multiple files 2026-01-23 07:17:38 +01:00

Sampling Examples

These examples demonstrate FastMCP's sampling API, which allows server tools to request LLM completions from the client.

Prerequisites

pip install 'fastmcp[anthropic]'
export ANTHROPIC_API_KEY=your-key

Or run directly with uv:

uv run examples/sampling/text.py

Examples

Simple Text Sampling (text.py)

Basic sampling flow where a server tool requests an LLM completion:

uv run examples/sampling/text.py

Structured Output (structured_output.py)

Uses result_type to get validated Pydantic models from the LLM:

uv run examples/sampling/structured_output.py

Tool Use (tool_use.py)

Gives the LLM tools to use during sampling (calculator, time, dice):

uv run examples/sampling/tool_use.py

Server Fallback (server_fallback.py)

Configures a fallback sampling handler on the server, enabling sampling even when clients don't support it:

uv run examples/sampling/server_fallback.py

Using OpenAI Instead

To use OpenAI instead of Anthropic, change the handler:

from fastmcp.client.sampling.handlers.openai import OpenAISamplingHandler

handler = OpenAISamplingHandler(default_model="gpt-4o-mini")

And install with pip install 'fastmcp[openai]'.