fastmcp/docs/servers
Jeremiah Lowin 41ec7ee06d
SEP-1577: Sampling with tools (#2551)
* MCP → SDK (vocab change only)

* WIP: Sampling API with SamplingResult[T] and result_type

* SEP-1577: Sampling with tools

- 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

* Fix tool result content handling in OpenAI handler

* Remove @sampling_tool decorator - pass functions directly to sample()

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.

* Remove auto-conversion of MCP tools to sampling tools

Users want MCP tools passed to ctx.sample() to go through the full MCP
machinery (middleware, native responses) rather than being auto-converted
to direct function calls. Now only SamplingTool and plain callables are
accepted - passing a FastMCP Tool raises a clear TypeError.

Also bumps mcp dependency to >=1.24.0 for required sampling features.

* Refactor sampling API: replace sample_iter() with sample_step()

Replace the mutable SampleRun/sample_iter() pattern with a simpler stateless
sample_step() function. sample_step() makes a single LLM call and returns a
SampleStep with the response and history. sample() now loops sample_step()
internally.

Key changes:
- Add sample_step() for fine-grained control over the sampling loop
- Remove SampleRun class and sample_iter() method
- Structured output uses tool description only (no prompt modification)
- execute_tools parameter controls automatic vs manual tool execution

* Address CodeRabbit nitpicks

* Address CodeRabbit review feedback for sampling tools

- Fix temperature=0.0 being dropped due to falsy evaluation
- Add ToolChoice.name support for forcing specific tools
- Replace assert statements with explicit RuntimeError checks
- Add mask_error_details parameter to sample()/sample_step() with ToolError escape hatch
- Fix hasattr patterns with proper isinstance checks
- Document mask_error_details and add OpenAI prerequisites to docs

* Address additional CodeRabbit review feedback

- Catch ValidationError specifically instead of bare Exception
- Update result_type docs to mention dataclasses and basic types
- Raise ValueError for unknown tool_choice modes
- Validate sampling_handler_behavior to catch typos
- Remove ToolChoice.name handling (not part of MCP spec)
- Validate tool_choice string in sample_step()

* Review fixes for sampling tools PR

- Remove internal functions from sampling __init__.py exports
- Remove fragile is_text property, use not is_tool_use instead
- Inline call_client into context.py, remove from run.py
- Fix SamplingMessage docs to use TextContent
- Handle result.text being None in doc examples
- Simplify client sampling docs to recommend OpenAISamplingHandler
- Add sampling_capabilities override documentation
- Raise iteration limit from 50 to 100
- Remove _parse_model_preferences duplication
- Use AsyncOpenAI in OpenAISamplingHandler
- Fix tool_choice docstring

* Fix OpenAI handler tests to use AsyncOpenAI

* Address remaining CodeRabbit review comments

- Fix message ordering in OpenAI handler: tool results now correctly
  follow assistant message with tool_calls
- sample_step() now always includes assistant message in history
- Raise ValueError on JSON parse errors instead of silent {}
- Add has_sampling capability check when behavior is None
- Raise RuntimeError when structured output receives text response
- Wrap primitive result_type schemas in object wrapper
- Fix docs example using invalid SamplingMessage construction
- Add comprehensive client_sampling_test.py example

* Add return type annotation to OpenAISamplingHandler.__init__

* Use explicit 'is not None' check for sampling_capabilities defaulting

---------

Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: Bill Easton <strawgate@users.noreply.github.com>
2025-12-14 13:51:05 -05:00
..
auth Fix RFC 8414 path-aware authorization server metadata discovery (#2533) 2025-12-03 19:16:59 -05:00
composition.mdx 2.14 deprecation removals (#2329) 2025-12-01 14:11:00 -05:00
context.mdx [2.14] SEP-1686 tasks (#2378) 2025-12-04 20:10:35 -05:00
elicitation.mdx SEP-1330 enum schema support (#2549) 2025-12-09 12:22:30 -05:00
icons.mdx Fix version number in VersionBadge: change 2.14.0 to 2.13.0 (#2491) 2025-11-26 16:51:18 -05:00
logging.mdx Also push client messages (info/warn/debug) to server debug log (#2063) 2025-10-14 11:38:26 -07:00
middleware.mdx feat: handle error from the initialize middleware (#2531) 2025-12-10 15:51:51 -05:00
progress.mdx Update parameter capitalization 2025-06-28 15:06:16 -04:00
prompts.mdx Revert "Refactor prompt behavior and add meta support (#2600)" (#2608) 2025-12-13 14:57:50 -05:00
proxy.mdx 2.14 deprecation removals (#2329) 2025-12-01 14:11:00 -05:00
resources.mdx Revert "Refactor resource behavior and add meta support (#2598)" (#2609) 2025-12-13 15:02:18 -05:00
sampling.mdx SEP-1577: Sampling with tools (#2551) 2025-12-14 13:51:05 -05:00
server.mdx 2.14 deprecation removals (#2329) 2025-12-01 14:11:00 -05:00
storage-backends.mdx Add maturity warnings for py-key-value backends 2025-10-29 21:43:31 +00:00
tasks.mdx docs: simplify Docket tip wording (#2599) 2025-12-11 21:08:37 -05:00
tools.mdx [Draft] Add documentation for read-only tool patterns (#2536) 2025-12-09 13:47:29 -05:00