The FastMCP Server
Learn about the core FastMCP server class and how to run it.
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:
name
: (Optional) A human-readable name for your server. Defaults to “FastMCP”.instructions
: (Optional) Description of how to interact with this server. These instructions help clients understand the server’s purpose and available functionality.lifespan
: (Optional) An async context manager function for server startup and shutdown logic.tags
: (Optional) A set of strings to tag the server itself.**settings
: Keyword arguments corresponding to additionalServerSettings
configuration
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.
Running the Server
FastMCP servers need a transport mechanism to communicate with clients. In the MCP protocol, servers typically run as separate processes that clients connect to.
The __main__
Block Pattern
The standard way to make your server executable is to include a run()
call inside an if __name__ == "__main__":
block:
This pattern is important because:
- Client Compatibility: Standard MCP clients (like Claude Desktop) expect to execute your server file directly with
python my_server.py
- Process Isolation: Each server runs in its own process, allowing clients to manage multiple servers independently
- Import Safety: The main block prevents the server from running when the file is imported by other code
While this pattern is technically optional when using FastMCP’s CLI, it’s considered a best practice for maximum compatibility with all MCP clients.
Transport Options
FastMCP supports two transport mechanisms:
STDIO Transport (Default)
The standard input/output (STDIO) transport is the default and most widely compatible option:
With STDIO:
- The client starts a new server process for each session
- Communication happens through standard input/output streams
- The server process terminates when the client disconnects
- This is ideal for integrations with tools like Claude Desktop, where each conversation gets its own server instance
SSE Transport (Server-Sent Events)
For long-running servers that serve multiple clients, FastMCP supports SSE:
With SSE:
- The server runs as a persistent web server
- Multiple clients can connect simultaneously
- The server stays running until explicitly terminated
- This is ideal for remote access to services
You can configure transport parameters directly when running the server:
Transport parameters passed to run()
or run_sse_async()
override any settings defined when creating the FastMCP instance. The most common parameters for SSE transport are:
host
: Host to bind to (default: “0.0.0.0”)port
: Port to bind to (default: 8000)log_level
: Logging level (default: “INFO”)
Advanced Transport Configuration
Under the hood, FastMCP’s run()
method accepts arbitrary keyword arguments (**transport_kwargs
) that are passed to the transport-specific run methods:
This means that any future transport-specific options will be automatically available through the same interface without requiring changes to your code.
Using the FastMCP CLI
The FastMCP CLI provides a convenient way to run servers:
The CLI can dynamically find and run FastMCP server objects in your files, but including the if __name__ == "__main__":
block ensures compatibility with all clients.
Composing Servers
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
FastMCP can act as a proxy for any MCP server (local or remote) using FastMCP.from_client
, 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.
See the Proxying Servers guide for details and advanced usage.
Server Configuration
Server behavior, like transport settings (host, port for SSE) and how duplicate components are handled, can be configured via ServerSettings
. These settings can be passed during FastMCP
initialization, set via environment variables (prefixed with FASTMCP_SERVER_
), or loaded from a .env
file.
Key Configuration Options
host
: Host address for SSE transport (default: “0.0.0.0”)port
: Port number for SSE transport (default: 8000)log_level
: Logging level (default: “INFO”)on_duplicate_tools
: How to handle duplicate tool registrationson_duplicate_resources
: How to handle duplicate resource registrationson_duplicate_prompts
: How to handle duplicate prompt registrations
All of these can be configured directly as parameters when creating the FastMCP
instance.