Skip to main content
Tools are the primary way your MCP server exposes functionality to clients. They represent actions that an AI agent can invoke to perform tasks.

Example

Anatomy of a Tool

Here’s the JSON-RPC response that the example above produces when a client calls tools/list:
Here’s what maps to what:
Always use Annotated[type, Field(description="...")] for your tool parameters. Without it, LLMs calling your tool won’t know what each argument means.

Minimal Definition

At minimum, a tool only needs a function with type hints:
The function name becomes the tool name, the docstring becomes the description, and type hints define the input schema.

Tool Options

The @server.tool() decorator accepts these options:

Tool Annotations

Tool annotations provide hints to clients about the tool’s behavior:

Async Tools

Tools can be async for non-blocking I/O operations:

Using Context

Access the MCP context for logging and progress reporting. Add a Context parameter - it’s automatically excluded from the tool’s input schema:

Parameter Types and JSON Schema

Every tool parameter becomes a property in the tool’s inputSchema. mcp-use uses Pydantic to generate JSON Schema 2020-12 from your Python type hints.

Type mapping

Required, optional, and nullable

How you declare a parameter determines whether it appears in the required array:
This produces:
Optional[str] and str | None are identical at runtime and produce the same schema.

Enum parameters with Literal

Use Literal to restrict a parameter to a fixed set of values. MCP clients like the Inspector render these as dropdown selects:
Produces {"type": "string", "enum": ["public", "private", "team"]} in the schema.
mcp-use automatically simplifies Pydantic’s nullable anyOf schemas. Without this, str | None would produce {"anyOf": [{"type": "string"}, {"type": "null"}]} which breaks description rendering in the MCP Inspector and fails validation in some clients like Google’s Gemini API. mcp-use collapses this into {"type": "string", "default": null}.

Complex Input Types

Use Pydantic models for complex inputs:

Return Types

Tools can return various types:

Error Handling

Raise exceptions to indicate errors. The error message will be returned to the client: