- It sends a request with parameters based on the tool’s schema.
- FastMCP validates these parameters against your function’s signature.
- Your function executes with the validated inputs.
- The result is returned to the LLM, which can use it in its response.
The @tool Decorator
Creating a tool is as simple as decorating a Python function with @mcp.tool:
- Uses the function name (
add) as the tool name. - Uses the function’s docstring (
Adds two integer numbers...) as the tool description. - Generates an input schema based on the function’s parameters and type annotations.
- Handles parameter validation and error reporting.
Decorator Arguments
While FastMCP infers the name and description from your function, you can override these and add additional metadata using arguments to the@mcp.tool decorator:
@tool Decorator Arguments
Sets the explicit tool name exposed via MCP. If not provided, uses the function name
Provides the description exposed via MCP. If set, the function’s docstring is ignored for this purpose
A set of strings used to categorize the tool. These can be used by the server and, in some cases, by clients to filter or group available tools.
A boolean to enable or disable the tool. See Disabling Tools for more information
New in version: 2.14.0Optional list of icon representations for this tool. See Icons for detailed examplesA list of argument names to exclude from the tool schema shown to the LLM. See Excluding Arguments for more information
An optional
ToolAnnotations object or dictionary to add additional metadata about the tool.New in version: 2.11.0Optional meta information about the tool. This data is passed through to the MCP client as the _meta field of the client-side tool object and can be used for custom metadata, versioning, or other application-specific purposes.Async Support
FastMCP is an async-first framework that seamlessly supports both asynchronous (async def) and synchronous (def) functions as tools. Async tools are preferred for I/O-bound operations to keep your server responsive.
While synchronous tools work seamlessly in FastMCP, they can block the event loop during execution. For CPU-intensive or potentially blocking synchronous operations, consider alternative strategies. One approach is to use anyio (which FastMCP already uses internally) to wrap them as async functions, for example:
asyncio.get_event_loop().run_in_executor() or other threading techniques to manage blocking operations without impacting server responsiveness. For example, here’s a recipe for using the asyncer library (not included in FastMCP) to create a decorator that wraps synchronous functions, courtesy of @hsheth2:
Arguments
By default, FastMCP converts Python functions into MCP tools by inspecting the function’s signature and type annotations. This allows you to use standard Python type annotations for your tools. In general, the framework strives to “just work”: idiomatic Python behaviors like parameter defaults and type annotations are automatically translated into MCP schemas. However, there are a number of ways to customize the behavior of your tools.Type Annotations
MCP tools have typed arguments, and FastMCP uses type annotations to determine those types. Therefore, you should use standard Python type annotations for tool arguments:| Type Annotation | Example | Description |
|---|---|---|
| Basic types | int, float, str, bool | Simple scalar values |
| Binary data | bytes | Binary content (raw strings, not auto-decoded base64) |
| Date and Time | datetime, date, timedelta | Date and time objects (ISO format strings) |
| Collection types | list[str], dict[str, int], set[int] | Collections of items |
| Optional types | float | None, Optional[float] | Parameters that may be null/omitted |
| Union types | str | int, Union[str, int] | Parameters accepting multiple types |
| Constrained types | Literal["A", "B"], Enum | Parameters with specific allowed values |
| Paths | Path | File system paths (auto-converted from strings) |
| UUIDs | UUID | Universally unique identifiers (auto-converted from strings) |
| Pydantic models | UserData | Complex structured data with validation |
bytes parameters accept raw strings without automatic base64 decoding. For base64 data, use str and decode manually with base64.b64decode().
Enums: Clients send enum values ("red"), not names ("RED"). Your function receives the Enum member (Color.RED).
Paths and UUIDs: String inputs are automatically converted to Path and UUID objects.
Pydantic Models: Must be provided as JSON objects (dicts), not stringified JSON. Even with flexible validation, {"user": {"name": "Alice"}} works, but {"user": '{"name": "Alice"}'} does not.
Optional Arguments
FastMCP follows Python’s standard function parameter conventions. Parameters without default values are required, while those with default values are optional.query parameter, while max_results, sort_by, and category will use their default values if not explicitly provided.
Validation Modes
New in version: 2.13.0
By default, FastMCP uses Pydantic’s flexible validation that coerces compatible inputs to match your type annotations. This improves compatibility with LLM clients that may send string representations of values (like "10" for an integer parameter).
If you need stricter validation that rejects any type mismatches, you can enable strict input validation. Strict mode uses the MCP SDK’s built-in JSON Schema validation to validate inputs against the exact schema before passing them to your function:
| Input Type | strict_input_validation=False (default) | strict_input_validation=True |
|---|---|---|
String integers ("10" for int) | ✅ Coerced to integer | ❌ Validation error |
String floats ("3.14" for float) | ✅ Coerced to float | ❌ Validation error |
String booleans ("true" for bool) | ✅ Coerced to boolean | ❌ Validation error |
Lists with string elements (["1", "2"] for list[int]) | ✅ Elements coerced | ❌ Validation error |
| Pydantic model fields with type mismatches | ✅ Fields coerced | ❌ Validation error |
Invalid values ("abc" for int) | ❌ Validation error | ❌ Validation error |
Note on Pydantic Models: Even with
strict_input_validation=False, Pydantic model parameters must be provided as JSON objects (dicts), not as stringified JSON. For example, {"user": {"name": "Alice"}} works, but {"user": '{"name": "Alice"}'} does not.Parameter Metadata
You can provide additional metadata about parameters in several ways:Simple String Descriptions
New in version: 2.11.0
For basic parameter descriptions, you can use a convenient shorthand with Annotated:
Field(description=...) but more concise for simple descriptions.
Advanced Metadata with Field
For validation constraints and advanced metadata, use Pydantic’sField class with Annotated:
description: Human-readable explanation of the parameter (shown to LLMs)ge/gt/le/lt: Greater/less than (or equal) constraintsmin_length/max_length: String or collection length constraintspattern: Regex pattern for string validationdefault: Default value if parameter is omitted
Excluding Arguments
New in version: 2.6.0
You can exclude certain arguments from the tool schema shown to the LLM. This is useful for arguments that are injected at runtime (such as state, user_id, or credentials) and should not be exposed to the LLM or client. Only arguments with default values can be excluded; attempting to exclude a required argument will raise an error.
Example:
user_id will not appear in the tool’s parameter schema, but can still be set by the server or framework at runtime.
For more complex tool transformations, see Transforming Tools.
Return Values
FastMCP tools can return data in two complementary formats: traditional content blocks (like text and images) and structured outputs (machine-readable JSON). When you add return type annotations, FastMCP automatically generates output schemas to validate the structured data and enables clients to deserialize results back to Python objects. Understanding how these three concepts work together:- Return Values: What your Python function returns (determines both content blocks and structured data)
- Structured Outputs: JSON data sent alongside traditional content for machine processing
- Output Schemas: JSON Schema declarations that describe and validate the structured output format
Content Blocks
FastMCP automatically converts tool return values into appropriate MCP content blocks:str: Sent asTextContentbytes: Base64 encoded and sent asBlobResourceContents(within anEmbeddedResource)fastmcp.utilities.types.Image: Sent asImageContentfastmcp.utilities.types.Audio: Sent asAudioContentfastmcp.utilities.types.File: Sent as base64-encodedEmbeddedResource- MCP SDK content blocks: Sent as-is
- A list of any of the above: Converts each item according to the above rules
None: Results in an empty response
Media Helper Classes
FastMCP provides helper classes for returning images, audio, and files. When you return one of these classes, either directly or as part of a list, FastMCP automatically converts it to the appropriate MCP content block. For example, if you return afastmcp.utilities.types.Image object, FastMCP will convert it to an MCP ImageContent block with the correct MIME type and base64 encoding.
path= or data= (mutually exclusive):
path: File path (string or Path object) - MIME type detected from extensiondata: Raw bytes - requiresformat=parameter for MIME typeformat: Optional format override (e.g., “png”, “wav”, “pdf”)name: Optional name forFilewhen usingdata=annotations: Optional MCP annotations for the content
Structured Output
New in version: 2.10.0
The 6/18/2025 MCP spec update introduced structured content, which is a new way to return data from tools. Structured content is a JSON object that is sent alongside traditional content. FastMCP automatically creates structured outputs alongside traditional content when your tool returns data that has a JSON object representation. This provides machine-readable JSON data that clients can deserialize back to Python objects.
Automatic Structured Content Rules:
- Object-like results (
dict, Pydantic models, dataclasses) → Always become structured content (even without output schema) - Non-object results (
int,str,list) → Only become structured content if there’s an output schema to validate/serialize them - All results → Always become traditional content blocks for backward compatibility
This automatic behavior enables clients to receive machine-readable data alongside human-readable content without requiring explicit output schemas for object-like returns.
Dictionaries and Objects
When your tool returns a dictionary, dataclass, or Pydantic model, FastMCP automatically creates structured content from it. The structured content contains the actual object data, making it easy for clients to deserialize back to native objects.Primitives and Collections
When your tool returns a primitive type (int, str, bool) or a collection (list, set), FastMCP needs a return type annotation to generate structured content. The annotation tells FastMCP how to validate and serialize the result. Without a type annotation, the tool only producescontent:
-> int, FastMCP generates structuredContent by wrapping the primitive value in a {"result": ...} object, since JSON schemas require object-type roots for structured output:
Typed Models
Return type annotations work with any type that can be converted to a JSON schema. Dataclasses and Pydantic models are particularly useful because FastMCP extracts their field definitions to create detailed schemas.Person dataclass becomes an output schema (second tab) that describes the expected format. When executed, clients receive the result (third tab) with both content and structuredContent fields.
Output Schemas
New in version: 2.10.0
The 6/18/2025 MCP spec update introduced output schemas, which are a new way to describe the expected output format of a tool. When an output schema is provided, the tool must return structured output that matches the schema.
When you add return type annotations to your functions, FastMCP automatically generates JSON schemas that describe the expected output format. These schemas help MCP clients understand and validate the structured data they receive.
Primitive Type Wrapping
For primitive return types (likeint, str, bool), FastMCP automatically wraps the result under a "result" key to create valid structured output:
Manual Schema Control
You can override the automatically generated schema by providing a customoutput_schema:
ToolResult and Metadata
For complete control over tool responses, return aToolResult object. This gives you explicit control over all aspects of the tool’s output: traditional content, structured data, and metadata.
ToolResult accepts three fields:
content - The traditional MCP content blocks that clients display to users. Can be a string (automatically converted to TextContent), a list of MCP content blocks, or any serializable value (converted to JSON string). At least one of content or structured_content must be provided.
structured_content - A dictionary containing structured data that matches your tool’s output schema. This enables clients to programmatically process the results. If you provide structured_content, it must be a dictionary or None. If only structured_content is provided, it will also be used as content (converted to JSON string).
meta
New in version: 2.13.1
Runtime metadata about the tool execution. Use this for performance metrics, debugging information, or any client-specific data that doesn’t belong in the content or structured output.
The
meta field in ToolResult is for runtime metadata about tool execution (e.g., execution time, performance metrics). This is separate from the meta parameter in @mcp.tool(meta={...}), which provides static metadata about the tool definition itself.ToolResult, you have full control - FastMCP won’t automatically wrap or transform your data. ToolResult can be returned with or without an output schema.
Error Handling
New in version: 2.4.1
If your tool encounters an error, you can raise a standard Python exception (ValueError, TypeError, FileNotFoundError, custom exceptions, etc.) or a FastMCP ToolError.
By default, all exceptions (including their details) are logged and converted into an MCP error response to be sent back to the client LLM. This helps the LLM understand failures and react appropriately.
If you want to mask internal error details for security reasons, you can:
- Use the
mask_error_details=Trueparameter when creating yourFastMCPinstance:
- Or use
ToolErrorto explicitly control what error information is sent to clients:
mask_error_details=True, only error messages from ToolError will include details, other exceptions will be converted to a generic message.
Disabling Tools
New in version: 2.8.0
You can control the visibility and availability of tools by enabling or disabling them. This is useful for feature flagging, maintenance, or dynamically changing the toolset available to a client. Disabled tools will not appear in the list of available tools returned by list_tools, and attempting to call a disabled tool will result in an “Unknown tool” error, just as if the tool did not exist.
By default, all tools are enabled. You can disable a tool upon creation using the enabled parameter in the decorator:
MCP Annotations
New in version: 2.2.7
FastMCP allows you to add specialized metadata to your tools through annotations. These annotations communicate how tools behave to client applications without consuming token context in LLM prompts.
Annotations serve several purposes in client applications:
- Adding user-friendly titles for display purposes
- Indicating whether tools modify data or systems
- Describing the safety profile of tools (destructive vs. non-destructive)
- Signaling if tools interact with external systems
annotations parameter in the @mcp.tool decorator:
| Annotation | Type | Default | Purpose |
|---|---|---|---|
title | string | - | Display name for user interfaces |
readOnlyHint | boolean | false | Indicates if the tool only reads without making changes |
destructiveHint | boolean | true | For non-readonly tools, signals if changes are destructive |
idempotentHint | boolean | false | Indicates if repeated identical calls have the same effect as a single call |
openWorldHint | boolean | true | Specifies if the tool interacts with external systems |
Notifications
New in version: 2.9.1
FastMCP automatically sends notifications/tools/list_changed notifications to connected clients when tools are added, removed, enabled, or disabled. This allows clients to stay up-to-date with the current tool set without manually polling for changes.
Accessing the MCP Context
Tools can access MCP features like logging, reading resources, or reporting progress through theContext object. To use it, add a parameter to your tool function with the type hint Context.
- Logging:
ctx.debug(),ctx.info(),ctx.warning(),ctx.error() - Progress Reporting:
ctx.report_progress(progress, total) - Resource Access:
ctx.read_resource(uri) - LLM Sampling:
ctx.sample(...) - Request Information:
ctx.request_id,ctx.client_id
Server Behavior
Duplicate Tools
New in version: 2.1.0
You can control how the FastMCP server behaves if you try to register multiple tools with the same name. This is configured using the on_duplicate_tools argument when creating the FastMCP instance.
"warn"(default): Logs a warning and the new tool replaces the old one."error": Raises aValueError, preventing the duplicate registration."replace": Silently replaces the existing tool with the new one."ignore": Keeps the original tool and ignores the new registration attempt.
Removing Tools
New in version: 2.3.4
You can dynamically remove tools from a server using the remove_tool method:

