Skip to main content
The central piece of a FastMCP application is the FastMCP server class. This class acts as the main container for your application’s tools, resources, and prompts, and manages communication with MCP clients.

Creating a Server

Instantiating a server is straightforward. You typically provide a name for your server, which helps identify it in client applications or logs.
The FastMCP constructor accepts several arguments:

FastMCP Constructor Parameters

name
str
default:"FastMCP"
A human-readable name for your server
instructions
str | None
Description of how to interact with this server. These instructions help clients understand the server’s purpose and available functionality
version
str | None
Version string for your server. If not provided, defaults to the FastMCP library version
website_url
str | None
New in version 2.13.0URL to a website with more information about your server. Displayed in client applications
icons
list[Icon] | None
New in version 2.13.0List of icon representations for your server. Icons help users visually identify your server in client applications. See Icons for detailed examples
auth
OAuthProvider | TokenVerifier | None
Authentication provider for securing HTTP-based transports. See Authentication for configuration options
lifespan
AsyncContextManager | None
An async context manager function for server startup and shutdown logic
tools
list[Tool | Callable] | None
A list of tools (or functions to convert to tools) to add to the server. In some cases, providing tools programmatically may be more convenient than using the @mcp.tool decorator
include_tags
set[str] | None
Only expose components with at least one matching tag
exclude_tags
set[str] | None
Hide components with any matching tag
on_duplicate_tools
Literal["error", "warn", "replace"]
default:"error"
How to handle duplicate tool registrations
on_duplicate_resources
Literal["error", "warn", "replace"]
default:"warn"
How to handle duplicate resource registrations
on_duplicate_prompts
Literal["error", "warn", "replace"]
default:"replace"
How to handle duplicate prompt registrations
strict_input_validation
bool
default:"False"
New in version 2.13.0Controls how tool input parameters are validated. When False (default), FastMCP uses Pydantic’s flexible validation that coerces compatible inputs (e.g., "10"10 for int parameters). When True, uses the MCP SDK’s JSON Schema validation to validate inputs against the exact schema before passing them to your function, rejecting any type mismatches. The default mode improves compatibility with LLM clients while maintaining type safety. See Input Validation Modes for details
include_fastmcp_meta
bool
default:"True"
New in version 2.11.0Whether to include FastMCP metadata in component responses. When True, component tags and other FastMCP-specific metadata are included in the _fastmcp namespace within each component’s meta field. When False, this metadata is omitted, resulting in cleaner integration with external systems. Can be overridden globally via FASTMCP_INCLUDE_FASTMCP_META environment variable

Components

FastMCP servers expose several types of components to the client:

Tools

Tools are functions that the client can call to perform actions or access external systems.
See Tools for detailed documentation.

Resources

Resources expose data sources that the client can read.
See Resources & Templates for detailed documentation.

Resource Templates

Resource templates are parameterized resources that allow the client to request specific data.
See Resources & Templates for detailed documentation.

Prompts

Prompts are reusable message templates for guiding the LLM.
See Prompts for detailed documentation.

Tag-Based Filtering

New in version 2.8.0 FastMCP supports tag-based filtering to selectively expose components based on configurable include/exclude tag sets. This is useful for creating different views of your server for different environments or users. Components can be tagged when defined using the tags parameter:
The filtering logic works as follows:
  • Include tags: If specified, only components with at least one matching tag are exposed
  • Exclude tags: Components with any matching tag are filtered out
  • Precedence: Exclude tags always take priority over include tags
To ensure a component is never exposed, you can set enabled=False on the component itself. To learn more, see the component-specific documentation.
You configure tag-based filtering when creating your server:
This filtering applies to all component types (tools, resources, resource templates, and prompts) and affects both listing and access.

Running the Server

FastMCP servers need a transport mechanism to communicate with clients. You typically start your server by calling the mcp.run() method on your FastMCP instance, often within an if __name__ == "__main__": block in your main server script. This pattern ensures compatibility with various MCP clients.
FastMCP supports several transport options:
  • STDIO (default, for local tools)
  • HTTP (recommended for web services, uses Streamable HTTP protocol)
  • SSE (legacy web transport, deprecated)
The server can also be run using the FastMCP CLI. For detailed information on each transport, how to configure them (host, port, paths), and when to use which, please refer to the Running Your FastMCP Server guide.

Custom Routes

When running your server with HTTP transport, you can add custom web routes alongside your MCP endpoint using the @custom_route decorator. This is useful for simple endpoints like health checks that need to be served alongside your MCP server:
Custom routes are served alongside your MCP endpoint and are useful for:
  • Health check endpoints for monitoring
  • Simple status or info endpoints
  • Basic webhooks or callbacks
For more complex web applications, consider mounting your MCP server into a FastAPI or Starlette app.

Composing Servers

New in version 2.2.0 FastMCP supports composing multiple servers together using import_server (static copy) and mount (live link). This allows you to organize large applications into modular components or reuse existing servers. See the Server Composition guide for full details, best practices, and examples.

Proxying Servers

New in version 2.0.0 FastMCP can act as a proxy for any MCP server (local or remote) using FastMCP.as_proxy, letting you bridge transports or add a frontend to existing servers. For example, you can expose a remote SSE server locally via stdio, or vice versa. Proxies automatically handle concurrent operations safely by creating fresh sessions for each request when using disconnected clients. See the Proxying Servers guide for details and advanced usage.

OpenAPI Integration

New in version 2.0.0 FastMCP can automatically generate servers from OpenAPI specifications or existing FastAPI applications using FastMCP.from_openapi() and FastMCP.from_fastapi(). This allows you to instantly convert existing APIs into MCP servers without manual tool creation. See the FastAPI Integration and OpenAPI Integration guides for detailed examples and configuration options.

Server Configuration

Servers can be configured using a combination of initialization arguments, global settings, and transport-specific settings.

Server-Specific Configuration

Server-specific settings are passed when creating the FastMCP instance and control server behavior:

Global Settings

Global settings affect all FastMCP servers and can be configured via environment variables (prefixed with FASTMCP_) or in a .env file:
Common global settings include:
  • log_level: Logging level (“DEBUG”, “INFO”, “WARNING”, “ERROR”, “CRITICAL”), set with FASTMCP_LOG_LEVEL
  • mask_error_details: Whether to hide detailed error information from clients, set with FASTMCP_MASK_ERROR_DETAILS
  • strict_input_validation: Controls tool input validation mode (default: False for flexible coercion), set with FASTMCP_STRICT_INPUT_VALIDATION. See Input Validation Modes
  • include_fastmcp_meta: Whether to include FastMCP metadata in component responses (default: True), set with FASTMCP_INCLUDE_FASTMCP_META
  • env_file: Path to the environment file to load settings from (default: “.env”), set with FASTMCP_ENV_FILE. Useful when your project uses a .env file with syntax incompatible with python-dotenv

Transport-Specific Configuration

Transport settings are provided when running the server and control network behavior:

Setting Global Configuration

Global FastMCP settings can be configured via environment variables (prefixed with FASTMCP_):

Custom Tool Serialization

New in version 2.2.7 By default, FastMCP serializes tool return values to JSON when they need to be converted to text. You can customize this behavior by providing a tool_serializer function when creating your server:
The serializer function takes any data object and returns a string representation. This is applied to all non-string return values from your tools. Tools that already return strings bypass the serializer. This customization is useful when you want to:
  • Format data in a specific way (like YAML or custom formats)
  • Control specific serialization options (like indentation or sorting)
  • Add metadata or transform data before sending it to clients
If the serializer function raises an exception, the tool will fall back to the default JSON serialization to avoid breaking the server.