fastmcp.server.sampling.sampling_tool
SamplingTool for use during LLM sampling requests.
Classes
SamplingTool
A tool that can be used during LLM sampling.
SamplingTools bundle a tool’s schema (name, description, parameters) with
an executor function, enabling servers to execute agentic workflows where
the LLM can request tool calls during sampling.
In most cases, pass functions directly to ctx.sample():
def search(query: str) -> str:
'''Search the web.'''
return web_search(query)
result = await context.sample(
messages=“Find info about Python”,
tools=[search], # Plain functions work directly
)
Create a SamplingTool explicitly when you need custom name/description:
tool = SamplingTool.from_function(search, name=“web_search”)
Methods:
run
arguments: Dictionary of arguments to pass to the tool function.
- The result of executing the tool function.
from_function
fn: The function to create a tool from.name: Optional name override. Defaults to the function’s name.description: Optional description override. Defaults to the function’s docstring.
- A SamplingTool wrapping the function.
ValueError: If the function is a lambda without a name override.

