Combine multiple FastMCP servers into a single, larger application using mounting and importing.
New in version: 2.2.0
As your MCP applications grow, you might want to organize your tools, resources, and prompts into logical modules or reuse existing server components. FastMCP supports composition through two methods:
import_server
: For a one-time copy of components with prefixing (static composition).mount
: For creating a live link where the main server delegates requests to the subserver (dynamic composition).WeatherServer
, a DatabaseServer
, a CalendarServer
).TextProcessingServer
) and mount them wherever needed.Feature | Importing | Mounting |
---|---|---|
Method | FastMCP.import_server(server, prefix=None) | FastMCP.mount(server, prefix=None) |
Composition Type | One-time copy (static) | Live link (dynamic) |
Updates | Changes to subserver NOT reflected | Changes to subserver immediately reflected |
Prefix | Optional - omit for original names | Optional - omit for original names |
Best For | Bundling finalized components | Modular runtime composition |
New in version: 2.4.0
You can also create proxies from configuration dictionaries that follow the MCPConfig schema, which is useful for quickly connecting to one or more remote servers. See the Proxy Servers documentation for details on configuration-based proxying. Note that MCPConfig follows an emerging standard and its format may evolve over time.
import_server()
method copies all components (tools, resources, templates, prompts) from one FastMCP
instance (the subserver) into another (the main server). An optional prefix
can be provided to avoid naming conflicts. If no prefix is provided, components are imported without modification. When multiple servers are imported with the same prefix (or no prefix), the most recently imported server’s components take precedence.
await main_mcp.import_server(subserver, prefix={whatever})
:
subserver
are added to main_mcp
with names prefixed using {prefix}_
.
subserver.tool(name="my_tool")
becomes main_mcp.tool(name="{prefix}_my_tool")
.subserver.resource(uri="data://info")
becomes main_mcp.resource(uri="data://{prefix}/info")
.resource.name
becomes "{prefix}_{resource.name}"
.subserver.resource(uri="data://{id}")
becomes main_mcp.resource(uri="data://{prefix}/{id}")
.template.name
becomes "{prefix}_{template.name}"
.{prefix}_
.
subserver.prompt(name="my_prompt")
becomes main_mcp.prompt(name="{prefix}_my_prompt")
.import_server
performs a one-time copy of components. Changes made to the subserver
after importing will not be reflected in main_mcp
. The subserver
’s lifespan
context is also not executed by the main server.
prefix
parameter is optional. If omitted, components are imported without modification.New in version: 2.9.0
You can also import servers without specifying a prefix, which copies components using their original names:
New in version: 2.9.0
When importing multiple servers with the same prefix, or no prefix, components from the most recently imported server take precedence.
mount()
method creates a live link between the main_mcp
server and the subserver
. Instead of copying components, requests for components matching the optional prefix
are delegated to the subserver
at runtime. If no prefix is provided, the subserver’s components are accessible without prefixing. When multiple servers are mounted with the same prefix (or no prefix), the most recently mounted server takes precedence for conflicting component names.
import_server
for naming tools, resources, templates, and prompts. This includes prefixing both the URIs/keys and the names of resources and templates for better identification in multi-server configurations.
prefix
parameter is optional. If omitted, components are mounted without modification.New in version: 2.9.0
You can also mount servers without specifying a prefix, which makes components accessible without prefixing. This works identically to importing without prefixes, including conflict resolution.
New in version: 2.2.7
FastMCP supports two mounting modes:
as_proxy
parameter.
FastMCP.as_proxy()
to create a proxy server, mounting that server will always use proxy mounting:
New in version: 2.4.0
When mounting or importing servers, resource URIs are usually prefixed to avoid naming conflicts. FastMCP supports two different formats for resource prefixes:
@server.custom_route()
are also forwarded to the parent server, making them accessible through the parent’s HTTP application.