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. 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)
- Streamable HTTP (recommended for web services)
- 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.
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.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: “127.0.0.1”)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.
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.
Authentication
New in version: 2.2.7
FastMCP supports OAuth 2.0 authentication, allowing servers to protect their tools and resources. This is configured by providing an auth_server_provider
and auth
settings during FastMCP
initialization.
Due to the low-level nature of the current MCP SDK’s auth provider interface, detailed implementation is beyond a quick example. Refer to the MCP SDK documentation for specifics on implementing an OAuthAuthorizationServerProvider
. FastMCP integrates with this by passing the provider and settings to the underlying MCP server.
A dedicated Authentication guide will cover this in more detail once higher-level abstractions are available in FastMCP.