server
fastmcp.server.server
FastMCP - A more ergonomic interface for MCP servers.
Functions
add_resource_prefix
Add a prefix to a resource URI.
Args:
uri
: The original resource URIprefix
: The prefix to add
Returns:
- The resource URI with the prefix added
Examples:
With new style:
With legacy style:
With absolute path:
Raises:
ValueError
: If the URI doesn’t match the expected protocol://path format
remove_resource_prefix
Remove a prefix from a resource URI.
Args:
uri
: The resource URI with a prefixprefix
: The prefix to removeprefix_format
: The format of the prefix to remove
Returns: The resource URI with the prefix removed
Examples:
With new style:
With legacy style:
With absolute path:
Raises:
ValueError
: If the URI doesn’t match the expected protocol://path format
has_resource_prefix
Check if a resource URI has a specific prefix.
Args:
uri
: The resource URI to checkprefix
: The prefix to look for
Returns:
- True if the URI has the specified prefix, False otherwise
Examples:
With new style:
With legacy style:
With other path:
Raises:
ValueError
: If the URI doesn’t match the expected protocol://path format
Classes
FastMCP
Methods:
settings
name
instructions
run
Run the FastMCP server. Note this is a synchronous function.
Args:
transport
: Transport protocol to use (“stdio”, “sse”, or “streamable-http”)
add_middleware
custom_route
Decorator to register a custom HTTP route on the FastMCP server.
Allows adding arbitrary HTTP endpoints outside the standard MCP protocol, which can be useful for OAuth callbacks, health checks, or admin APIs. The handler function must be an async function that accepts a Starlette Request and returns a Response.
Args:
path
: URL path for the route (e.g., “/oauth/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
Add a tool to the server.
The tool function can optionally request a Context object by adding a parameter with the Context type annotation. See the @tool decorator for examples.
Args:
tool
: The Tool instance to register
remove_tool
Remove a tool from the server.
Args:
name
: The name of the tool to remove
Raises:
NotFoundError
: If the tool is not found
tool
tool
tool
Decorator to register a tool.
Tools can optionally request a Context object by adding a parameter with the Context type annotation. The context provides access to MCP capabilities like logging, progress reporting, and resource access.
This decorator supports multiple calling patterns:
- @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)
Args:
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 toolannotations
: Optional annotations about the tool’s behaviorexclude_args
: Optional list of argument names to exclude from the tool schemaenabled
: Optional boolean to enable or disable the tool
Examples:
Register a tool with a custom name:
add_resource
Add a resource to the server.
Args:
resource
: A Resource instance to add
add_template
Add a resource template to the server.
Args:
template
: A ResourceTemplate instance to add
add_resource_fn
Add a resource or template to the server from a function.
If the URI contains parameters (e.g. “resource://”) or the function has parameters, it will be registered as a template resource.
Args:
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
Decorator to register a function as a resource.
The function will be called when the resource is read to generate its content. The function can return:
- str for text content
- bytes for binary content
- other types will be converted to JSON
Resources can optionally request a Context object by adding a parameter with the Context type annotation. The context provides access to MCP capabilities like logging, progress reporting, and session information.
If the URI contains parameters (e.g. “resource://”) or the function has parameters, it will be registered as a template resource.
Args:
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 resource
Examples:
Register a resource with a custom name:
add_prompt
Add a prompt to the server.
Args:
prompt
: A Prompt instance to add
prompt
prompt
prompt
Decorator to register a prompt.
Prompts can optionally request a Context object by adding a parameter with the Context type annotation. The context provides access to MCP capabilities like logging, progress reporting, and session information.
This decorator supports multiple calling patterns:
- @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)
Args: name_or_fn: Either a function (when used as @prompt), a string name, or None name: Optional name for the prompt (keyword-only, alternative to name_or_fn) description: Optional description of what the prompt does tags: Optional set of tags for categorizing the prompt enabled: Optional boolean to enable or disable the prompt
Examples:
sse_app
Create a Starlette app for the SSE server.
Args:
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
Create a Starlette app for the StreamableHTTP server.
Args:
path
: The path to the StreamableHTTP endpointmiddleware
: A list of middleware to apply to the app
http_app
Create a Starlette app using the specified HTTP transport.
Args:
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”
Returns:
- A Starlette application configured with the specified transport
mount
Mount another FastMCP server on this server with an optional prefix.
Unlike importing (with import_server), mounting establishes a dynamic connection between servers. When a client interacts with a mounted server’s objects through the parent server, requests are forwarded to the mounted server in real-time. This means changes to the mounted server are immediately reflected when accessed through the parent.
When a server is mounted with a prefix:
- 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”.
When a server is mounted without a prefix (prefix=None), its tools, resources, templates, and prompts are accessible with their original names. Multiple servers can be mounted without prefixes, and they will be tried in order until a match is found.
There are two modes for mounting servers:
-
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.
Args:
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.
from_openapi
Create a FastMCP server from an OpenAPI specification.
from_fastapi
Create a FastMCP server from a FastAPI application.
as_proxy
Create a FastMCP proxy server for the given backend.
The 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.
from_client
Create a FastMCP proxy server from a FastMCP client.