fastmcp.server.server
FastMCP - A more ergonomic interface for MCP servers.
Functions
default_lifespan
server: The server instance this lifespan is managing
- An empty dictionary as the lifespan result.
create_proxy
FastMCPProxy or ProxyProvider directly from fastmcp.server.providers.proxy.
Args:
target: The backend to proxy to. Can be:- A Client instance (connected or disconnected)
- A ClientTransport
- A FastMCP server instance
- A URL string or AnyUrl
- A Path to a server script
- An MCPConfig or dict
**settings: Additional settings passed to FastMCPProxy (name, etc.)
- A FastMCPProxy server that proxies to the target.
Classes
FastMCP
Methods:
settings
name
instructions
instructions
version
website_url
icons
docket
run_async
transport: Transport protocol to use (“stdio”, “sse”, or “streamable-http”)show_banner: Whether to display the server banner. If None, uses the FASTMCP_SHOW_SERVER_BANNER setting (default: True).
run
transport: Transport protocol to use (“http”, “stdio”, “sse”, or “streamable-http”)show_banner: Whether to display the server banner. If None, uses the FASTMCP_SHOW_SERVER_BANNER setting (default: True).
add_middleware
add_provider
provider: A Provider instance that will provide components dynamically.
enable
keys: Keys to enable (e.g.,"tool\:my_tool").tags: Tags to enable - components with these tags will be enabled.only: If True, switches to allowlist mode - ONLY show these keys/tags. This clears existing allowlists and sets default visibility to False.
disable
keys: Keys to disable (e.g.,"tool\:my_tool").tags: Tags to disable - components with these tags will be disabled.
get_tools
run_middleware: If True, apply the middleware chain before returning results. Used by MCP handlers and mounted servers.
get_tool
get_resources
run_middleware: If True, apply the middleware chain before returning results. Used by MCP handlers and mounted servers.
get_resource
get_resource_templates
run_middleware: If True, apply the middleware chain before returning results. Used by MCP handlers and mounted servers.
get_resource_template
get_prompts
run_middleware: If True, apply the middleware chain before returning results. Used by MCP handlers and mounted servers.
get_prompt
get_component
key: The prefixed key (e.g., “tool:name”, “resource:uri”, “template:uri”).
- The component if found.
NotFoundError: If no component is found with the given key.
call_tool
call_tool
call_tool
name: The tool namearguments: Tool arguments (optional)run_middleware: If True (default), apply the middleware chain. Set to False when called from middleware to avoid re-applying.task_meta: If provided, execute as a background task and return CreateTaskResult. If None (default), execute synchronously and return ToolResult.
- ToolResult when task_meta is None.
- CreateTaskResult when task_meta is provided.
NotFoundError: If tool not found or disabledToolError: If tool execution failsValidationError: If arguments fail validation
read_resource
read_resource
read_resource
uri: The resource URIrun_middleware: If True (default), apply the middleware chain. Set to False when called from middleware to avoid re-applying.task_meta: If provided, execute as a background task and return CreateTaskResult. If None (default), execute synchronously and return ResourceResult.
- ResourceResult when task_meta is None.
- CreateTaskResult when task_meta is provided.
NotFoundError: If resource not found or disabledResourceError: If resource read fails
render_prompt
render_prompt
render_prompt
name: The prompt namearguments: Prompt arguments (optional)run_middleware: If True (default), apply the middleware chain. Set to False when called from middleware to avoid re-applying.task_meta: If provided, execute as a background task and return CreateTaskResult. If None (default), execute synchronously and return PromptResult.
- PromptResult when task_meta is None.
- CreateTaskResult when task_meta is provided.
NotFoundError: If prompt not found or disabledPromptError: If prompt rendering fails
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 schema. Deprecated: UseDepends()for dependency injection instead.meta: Optional meta information about 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.
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 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 serverstateless: Whether to run in stateless mode (no session initialization)
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 appjson_response: Whether to use JSON response format (defaults to settings.json_response)stateless_http: Whether to use stateless HTTP (defaults to settings.stateless_http)stateless: Alias for stateless_http for CLI consistency
http_app
path: The path for the HTTP endpointmiddleware: A list of middleware to apply to the appjson_response: Whether to use JSON response formatstateless_http: Whether to use stateless mode (new transport per request)transport: Transport protocol to use - “http”, “streamable-http”, or “sse”event_store: Optional event store for SSE polling/resumability. When set, enables clients to reconnect and resume receiving events after server-initiated disconnections. Only used with streamable-http transport.retry_interval: Optional retry interval in milliseconds for SSE polling. Controls how quickly clients should reconnect after server-initiated disconnections. Requires event_store to be set. Only used with streamable-http transport.
- A Starlette application configured with the specified transport
mount
- Tools from the mounted server are accessible with namespaced names. Example: If server has a tool named “get_weather”, it will be available as “namespace_get_weather”.
- Resources are accessible with namespaced URIs. Example: If server has a resource with URI “weather://forecast”, it will be available as “weather://namespace/forecast”.
- Templates are accessible with namespaced URI templates. Example: If server has a template with URI “weather://location/”, it will be available as “weather://namespace/location/”.
- Prompts are accessible with namespaced names. Example: If server has a prompt named “weather_prompt”, it will be available as “namespace_weather_prompt”.
server: The FastMCP server to mount.namespace: Optional namespace to use for the mounted server’s objects. If None, the server’s objects are accessible with their original names.as_proxy: Deprecated. Mounted servers now always have their lifespan and middleware invoked. To create a proxy server, use create_proxy() explicitly before mounting.tool_names: Optional mapping of original tool names to custom names. Use this to override namespaced names. Keys are the original tool names from the mounted server.prefix: Deprecated. Use namespace instead.
import_server
mount instead. import_server will be removed in a
future version.
Note that when a server is imported, its objects are immediately
registered to the importing server. This is a one-time operation and
future changes to the imported server will not be reflected in the
importing server. Server-level configurations and lifespans are not imported.
When a server is imported with a prefix:
- 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.
from_openapi
openapi_spec: OpenAPI schema as a dictionaryclient: httpx AsyncClient for making HTTP requestsname: Name for the MCP serverroute_maps: Optional list of RouteMap objects defining route mappingsroute_map_fn: Optional callable for advanced route type mappingmcp_component_fn: Optional callable for component customizationmcp_names: Optional dictionary mapping operationId to component namestags: Optional set of tags to add to all componentstimeout: Optional timeout (in seconds) for all requests**settings: Additional settings passed to FastMCP
- A FastMCP server with an OpenAPIProvider attached.
from_fastapi
app: FastAPI application instancename: Name for the MCP server (defaults to app.title)route_maps: Optional list of RouteMap objects defining route mappingsroute_map_fn: Optional callable for advanced route type mappingmcp_component_fn: Optional callable for component customizationmcp_names: Optional dictionary mapping operationId to component nameshttpx_client_kwargs: Optional kwargs passed to httpx.AsyncClienttags: Optional set of tags to add to all componentstimeout: Optional timeout (in seconds) for all requests**settings: Additional settings passed to FastMCP
- A FastMCP server with an OpenAPIProvider attached.
as_proxy
fastmcp.server.create_proxy instead.
This method will be removed in a future version.
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.

