The core FastMCP server class for building MCP applications with tools, resources, and prompts.
FastMCP
server class. This class acts as the main container for your application’s tools, resources, and prompts, and manages communication with MCP clients.
FastMCP
constructor accepts several arguments:
@mcp.tool
decoratorNew in version: 2.11.0
Whether 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 variableNew 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:
enabled=False
on the component itself. To learn more, see the component-specific documentation.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.
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.
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.
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.
FastMCP
instance and control server behavior:
FASTMCP_
) or in a .env
file:
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
resource_prefix_format
: How to format resource prefixes (“path” or “protocol”), set with FASTMCP_RESOURCE_PREFIX_FORMAT
include_fastmcp_meta
: Whether to include FastMCP metadata in component responses (default: True), set with FASTMCP_INCLUDE_FASTMCP_META
FASTMCP_
):
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: