Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109---title: function_toolsidebarTitle: function_tool---
# `fastmcp.tools.function_tool`
Standalone @tool decorator for FastMCP.
## Functions
### `tool` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/tools/function_tool.py#L437" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythontool(name_or_fn: str | Callable[..., Any] | None = None) -> Any```
Standalone decorator to mark a function as an MCP tool.
Returns the original function with metadata attached. Register with a serverusing mcp.add_tool().
**Args:**- `run_in_thread`: Applies to sync tool functions only. When True (default),the sync function is dispatched to a worker thread so it does notblock the event loop. Set to False to run the function inline on theevent loop thread — useful for libraries with thread affinity(e.g. Windows COM via `uiautomation`/`comtypes`/`pywin32`, `tkinter`,some GPU/driver bindings). Ignored for async functions. Cannot becombined with `timeout` on a sync function\: inline calls have nocancellation checkpoints, so the timeout would be a silent no-op.
## Classes
### `DecoratedTool` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/tools/function_tool.py#L59" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Protocol for functions decorated with @tool.
### `ToolMeta` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/tools/function_tool.py#L68" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Metadata attached to functions by the @tool decorator.
### `FunctionTool` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/tools/function_tool.py#L91" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
**Methods:**
#### `from_function` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/tools/function_tool.py#L111" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonfrom_function(cls, fn: Callable[..., Any]) -> FunctionTool```
Create a FunctionTool from a function.
**Args:**- `fn`: The function to wrap- `metadata`: ToolMeta object with all configuration. If provided,individual parameters must not be passed.- `name, title, etc.`: Individual parameters for backwards compatibility.Cannot be used together with metadata parameter.
#### `run` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/tools/function_tool.py#L285" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonrun(self, arguments: dict[str, Any]) -> ToolResult```
Run the tool with arguments.
#### `register_with_docket` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/tools/function_tool.py#L357" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonregister_with_docket(self, docket: Docket) -> None```
Register this tool with docket for background execution.
Registers the raw function so Docket sees and resolves ALLdependencies — both FastMCP's (CurrentContext, Progress) andDocket-native ones (Retry, Timeout, ConcurrencyLimit).
#### `add_to_docket` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/src/fastmcp/tools/function_tool.py#L368" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonadd_to_docket(self, docket: Docket, arguments: dict[str, Any], **kwargs: Any) -> Execution```
Schedule this tool for background execution via docket.
FunctionTool splats the arguments dict since .fn expects **kwargs.
**Args:**- `docket`: The Docket instance- `arguments`: Tool arguments- `fn_key`: Function lookup key in Docket registry (defaults to self.key)- `task_key`: Redis storage key for the result- `**kwargs`: Additional kwargs passed to docket.add()