Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102---title: sampling_toolsidebarTitle: sampling_tool---
# `fastmcp.server.sampling.sampling_tool`
SamplingTool for use during LLM sampling requests.
## Classes
### `SamplingTool` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/sampling/sampling_tool.py#L20" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
A tool that can be used during LLM sampling.
SamplingTools bundle a tool's schema (name, description, parameters) withan executor function, enabling servers to execute agentic workflows wherethe 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` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/sampling/sampling_tool.py#L51" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonrun(self, arguments: dict[str, Any] | None = None) -> Any```
Execute the tool with the given arguments.
**Args:**- `arguments`: Dictionary of arguments to pass to the tool function.
**Returns:**- The result of executing the tool function.
#### `from_function` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/sampling/sampling_tool.py#L81" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonfrom_function(cls, fn: Callable[..., Any]) -> SamplingTool```
Create a SamplingTool from a function.
The function's signature is analyzed to generate a JSON schema forthe tool's parameters. Type hints are used to determine parameter types.
**Args:**- `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.- `sequential`: If True, this tool requires sequential execution and preventsparallel execution of all tools in the batch. Set to True for toolswith shared state, file writes, or other operations that cannot runconcurrently. Defaults to False.
**Returns:**- A SamplingTool wrapping the function.
**Raises:**- `ValueError`: If the function is a lambda without a name override.
#### `from_callable_tool` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/server/sampling/sampling_tool.py#L123" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonfrom_callable_tool(cls, tool: FunctionTool | TransformedTool) -> SamplingTool```
Create a SamplingTool from a FunctionTool or TransformedTool.
Reuses existing server tools in sampling contexts. For TransformedTool,the tool's .run() method is used to ensure proper argument transformation,and the ToolResult is automatically unwrapped.
**Args:**- `tool`: A FunctionTool or TransformedTool to convert.- `name`: Optional name override. Defaults to tool.name.- `description`: Optional description override. Defaults to tool.description.
**Raises:**- `TypeError`: If the tool is not a FunctionTool or TransformedTool.