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
StateValue
Wrapper for stored context state values.
FastMCP
Methods:
settings
name
instructions
instructions
version
website_url
icons
add_middleware
add_provider
provider: A Provider instance that will provide components dynamically.namespace: Optional namespace prefix. When set:- Tools become “namespace_toolname”
- Resources become “protocol://namespace/path”
- Prompts become “namespace_promptname”
get_tasks
add_transform
transform: The transform to add.
add_tool_transformation
add_transform(ToolTransform({...})) instead.
remove_tool_transformation
list_tools
get_tool
name: The tool name.version: Version filter (None returns highest version).
- The tool if found and enabled, None otherwise.
list_resources
get_resource
uri: The resource URI.version: Version filter (None returns highest version).
- The resource if found and enabled, None otherwise.
list_resource_templates
get_resource_template
uri: The template URI.version: Version filter (None returns highest version).
- The template if found and enabled, None otherwise.
list_prompts
get_prompt
name: The prompt name.version: Version filter (None returns highest version).
- The prompt if found and enabled, None otherwise.
call_tool
call_tool
call_tool
name: The tool namearguments: Tool arguments (optional)version: Specific version to call. If None, calls highest version.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 URIversion: Specific version to read. If None, reads highest version.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 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)version: Specific version to render. If None, renders highest version.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
add_tool
tool: The Tool instance or @tool-decorated function to register
- The tool instance that was added to the server.
remove_tool
name: The name of the tool to remove.version: If None, removes ALL versions. If specified, removes only that version.
NotFoundError: If no matching tool is found.
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 or @resource-decorated function 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 or @prompt-decorated function 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)
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.

