FastMCP class is the central piece of every FastMCP application. It acts as the container for your tools, resources, and prompts, managing communication with MCP clients and orchestrating the entire server lifecycle.
Creating a Server
At its simplest, a FastMCP server just needs a name. Everything else has sensible defaults.Components
FastMCP servers expose three types of components to clients, each serving a distinct role in the MCP protocol. Tools are functions that clients invoke to perform actions or access external systems.Running the Server
Start your server by callingmcp.run(). The if __name__ guard ensures compatibility with MCP clients that launch your server as a subprocess.
- STDIO (default): For local integrations and CLI tools
- HTTP: For web services using the Streamable HTTP protocol
- SSE: Legacy web transport (deprecated)
Configuration Reference
TheFastMCP constructor accepts parameters organized into four categories: identity, composition, behavior, and handlers.
Identity
These parameters control how your server presents itself to clients.A human-readable name for your server, shown in client applications and logs
Description of how to interact with this server. Clients surface these instructions to help LLMs understand the server’s purpose and available functionality
Version string for your server. Defaults to the FastMCP library version if not provided
URL to a website with more information about your server. Displayed in client applications
Composition
These parameters control what your server is built from — its components, middleware, providers, and lifecycle.Tools to register on the server. An alternative to the
@mcp.tool decorator when you need to add tools programmaticallyAuthentication provider for securing HTTP-based transports. See Authentication for configuration
Middleware that intercepts and transforms every MCP message flowing through the server — requests, responses, and notifications in both directions. Use for cross-cutting concerns like logging, error handling, and rate limiting
Providers that supply tools, resources, and prompts dynamically. Providers are queried at request time, so they can serve components from databases, APIs, or other external sources
Server-level transforms to apply to all components. Transforms modify how tools, resources, and prompts are presented to clients — for example, search transforms replace large catalogs with on-demand discovery
Behavior
These parameters tune how the server processes requests and communicates with clients.How to handle duplicate component registrations
When
False (default), FastMCP uses Pydantic’s flexible validation that coerces compatible inputs (e.g., "10" → 10 for int parameters). When True, validates inputs against the exact JSON Schema before calling your function, rejecting type mismatches. See Input Validation Modes for detailsWhen
True, replaces internal error details in tool/resource responses with a generic message to avoid leaking implementation details to clients. Defaults to the FASTMCP_MASK_ERROR_DETAILS environment variableMaximum items per page for list operations (
tools/list, resources/list, etc.). When None, all results are returned in a single response. See Pagination for detailsEnable background task support. When
True, tools and resources can return CreateTaskResult to run work asynchronously while the client polls for resultsDefault minimum log level for messages sent to MCP clients via
context.log(). When set, messages below this level are suppressed. Individual clients can override this per-session using the MCP logging/setLevel request. One of "debug", "info", "notice", "warning", "error", "critical", "alert", or "emergency"Automatically dereference
$ref pointers in JSON schemas generated from complex Pydantic models. Most clients require flat schemas without $ref, so this should usually stay enabledHandlers and Storage
These parameters provide custom handlers for MCP capabilities and persistent storage for session state.Custom handler for MCP sampling requests (server-initiated LLM calls). See Sampling for details
When
"fallback", the sampling handler is used only when no tool-specific handler exists. When "always", this handler is used for all sampling requestsPersistent key-value store for session state that survives across requests. Defaults to an in-memory store. Provide a custom implementation for persistence across server restarts
Tag-Based Filtering
Tags let you categorize components and selectively expose them. This is useful for creating different views of your server for different environments or user types.- Enable with
only=True: Switches to allowlist mode — only components with at least one matching tag are exposed - Disable: Components with any matching tag are hidden
- Precedence: Later calls override earlier ones, so call
disableafterenableto exclude from an allowlist
Custom Routes
When running with HTTP transport, you can add custom web routes alongside your MCP endpoint using the@custom_route decorator.

