fastmcp.server.server
FastMCP - A more ergonomic interface for MCP servers.
Functions
default_lifespan
server: The server instance this lifespan is managing
- An empty context object
add_resource_prefix
uri: The original resource URIprefix: The prefix to add
- The resource URI with the prefix added
ValueError: If the URI doesn’t match the expected protocol://path format
remove_resource_prefix
uri: The resource URI with a prefixprefix: The prefix to removeprefix_format: The format of the prefix to remove
ValueError: If the URI doesn’t match the expected protocol://path format
has_resource_prefix
uri: The resource URI to checkprefix: The prefix to look for
- True if the URI has the specified prefix, False otherwise
ValueError: If the URI doesn’t match the expected protocol://path format
Classes
FastMCP
Methods:
settings
name
instructions
instructions
version
run_async
transport: Transport protocol to use (“stdio”, “sse”, or “streamable-http”)
run
transport: Transport protocol to use (“stdio”, “sse”, or “streamable-http”)
add_middleware
get_tools
get_tool
get_resources
get_resource
get_resource_templates
get_resource_template
get_prompts
get_prompt
custom_route
path: URL path for the route (e.g., “/auth/callback”)methods: List of HTTP methods to support (e.g., [“GET”, “POST”])name: Optional name for the route (to reference this route with Starlette’s reverse URL lookup feature)include_in_schema: Whether to include in OpenAPI schema, defaults to True
add_tool
tool: The Tool instance to register
- The tool instance that was added to the server.
remove_tool
name: The name of the tool to remove
NotFoundError: If the tool is not found
add_tool_transformation
remove_tool_transformation
tool
tool
tool
- @server.tool (without parentheses)
- @server.tool (with empty parentheses)
- @server.tool(“custom_name”) (with name as first argument)
- @server.tool(name=“custom_name”) (with name as keyword argument)
- server.tool(function, name=“custom_name”) (direct function call)
name_or_fn: Either a function (when used as @tool), a string name, or Nonename: Optional name for the tool (keyword-only, alternative to name_or_fn)description: Optional description of what the tool doestags: Optional set of tags for categorizing the tooloutput_schema: Optional JSON schema for the tool’s outputannotations: Optional annotations about the tool’s behaviorexclude_args: Optional list of argument names to exclude from the tool schemameta: Optional meta information about the toolenabled: Optional boolean to enable or disable the tool
add_resource
resource: A Resource instance to add
- The resource instance that was added to the server.
add_template
template: A ResourceTemplate instance to add
- The template instance that was added to the server.
add_resource_fn
fn: The function to register as a resourceuri: The URI for the resourcename: Optional name for the resourcedescription: Optional description of the resourcemime_type: Optional MIME type for the resourcetags: Optional set of tags for categorizing the resource
resource
- str for text content
- bytes for binary content
- other types will be converted to JSON
uri: URI for the resource (e.g. “resource://my-resource” or “resource://”)name: Optional name for the resourcedescription: Optional description of the resourcemime_type: Optional MIME type for the resourcetags: Optional set of tags for categorizing the resourceenabled: Optional boolean to enable or disable the resourceannotations: Optional annotations about the resource’s behaviormeta: Optional meta information about the resource
add_prompt
prompt: A Prompt instance to add
- The prompt instance that was added to the server.
prompt
prompt
prompt
- @server.prompt (without parentheses)
- @server.prompt() (with empty parentheses)
- @server.prompt(“custom_name”) (with name as first argument)
- @server.prompt(name=“custom_name”) (with name as keyword argument)
- server.prompt(function, name=“custom_name”) (direct function call)
run_stdio_async
show_banner: Whether to display the server bannerlog_level: Log level for the server
run_http_async
transport: Transport protocol to use - either “streamable-http” (default) or “sse”host: Host address to bind to (defaults to settings.host)port: Port to bind to (defaults to settings.port)log_level: Log level for the server (defaults to settings.log_level)path: Path for the endpoint (defaults to settings.streamable_http_path or settings.sse_path)uvicorn_config: Additional configuration for the Uvicorn servermiddleware: A list of middleware to apply to the appstateless_http: Whether to use stateless HTTP (defaults to settings.stateless_http)
run_sse_async
sse_app
path: The path to the SSE endpointmessage_path: The path to the message endpointmiddleware: A list of middleware to apply to the app
streamable_http_app
path: The path to the StreamableHTTP endpointmiddleware: A list of middleware to apply to the app
http_app
path: The path for the HTTP endpointmiddleware: A list of middleware to apply to the apptransport: Transport protocol to use - either “streamable-http” (default) or “sse”
- A Starlette application configured with the specified transport
run_streamable_http_async
mount
- Tools from the mounted server are accessible with prefixed names. Example: If server has a tool named “get_weather”, it will be available as “prefix_get_weather”.
- Resources are accessible with prefixed URIs. Example: If server has a resource with URI “weather://forecast”, it will be available as “weather://prefix/forecast”.
- Templates are accessible with prefixed URI templates. Example: If server has a template with URI “weather://location/”, it will be available as “weather://prefix/location/”.
- Prompts are accessible with prefixed names. Example: If server has a prompt named “weather_prompt”, it will be available as “prefix_weather_prompt”.
- Direct mounting (default when server has no custom lifespan): The parent server directly accesses the mounted server’s objects in-memory for better performance. In this mode, no client lifecycle events occur on the mounted server, including lifespan execution.
- Proxy mounting (default when server has a custom lifespan): The parent server treats the mounted server as a separate entity and communicates with it via a Client transport. This preserves all client-facing behaviors, including lifespan execution, but with slightly higher overhead.
server: The FastMCP server to mount.prefix: Optional prefix to use for the mounted server’s objects. If None, the server’s objects are accessible with their original names.as_proxy: Whether to treat the mounted server as a proxy. If None (default), automatically determined based on whether the server has a custom lifespan (True if it has a custom lifespan, False otherwise).tool_separator: Deprecated. Separator character for tool names.resource_separator: Deprecated. Separator character for resource URIs.prompt_separator: Deprecated. Separator character for prompt names.
import_server
- The tools are imported with prefixed names Example: If server has a tool named “get_weather”, it will be available as “prefix_get_weather”
- The resources are imported with prefixed URIs using the new format Example: If server has a resource with URI “weather://forecast”, it will be available as “weather://prefix/forecast”
- The templates are imported with prefixed URI templates using the new format Example: If server has a template with URI “weather://location/”, it will be available as “weather://prefix/location/”
- The prompts are imported with prefixed names Example: If server has a prompt named “weather_prompt”, it will be available as “prefix_weather_prompt”
server: The FastMCP server to importprefix: Optional prefix to use for the imported server’s objects. If None, objects are imported with their original names.tool_separator: Deprecated. Separator for tool names.resource_separator: Deprecated and ignored. Prefix is now applied using the protocol://prefix/path formatprompt_separator: Deprecated. Separator for prompt names.
from_openapi
from_fastapi
as_proxy
backend argument can be either an existing fastmcp.client.Client
instance or any value accepted as the transport argument of
fastmcp.client.Client. This mirrors the convenience of the
fastmcp.client.Client constructor.

