Creating Tools in FastMCP
Tools are the core building blocks that allow your LLM to interact with external systems, execute code, and access data.
Basic Tool Definition
Creating a tool is as simple as decorating a Python function:
from fastmcp import FastMCP
mcp = FastMCP(name="CalculatorServer")
@mcp.tool
def add(a: int, b: int) -> int:
"""Adds two integer numbers together."""
return a + b
FastMCP automatically: - Uses the function name as the tool name - Uses the docstring as the tool description - Generates an input schema from type annotations - Handles parameter validation and error reporting
Decorator Arguments
@mcp.tool(
name="find_products",
description="Search the product catalog",
tags={"catalog", "search"},
meta={"version": "1.2"}
)
def search_products(query: str, category: str | None = None) -> list[dict]:
return [{"id": 2, "name": "Product"}]
Type Annotations
Supported types:
- Basic: int, float, str, bool
- Collections: list[str], dict[str, int], set[int]
- Optional: str | None, Optional[str]
- Union: str | int
- Literal: Literal["A", "B"]
- Pydantic models for complex data
Async Support
@mcp.tool
async def fetch_data(url: str) -> dict:
"""Fetch data asynchronously."""
async with httpx.AsyncClient() as client:
response = await client.get(url)
return response.json()
Use async def for I/O-bound operations to keep server responsive.