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).
Why Compose Servers?
- Modularity: Break down large applications into smaller, focused servers (e.g., a
WeatherServer
, aDatabaseServer
, aCalendarServer
). - Reusability: Create common utility servers (e.g., a
TextProcessingServer
) and mount them wherever needed. - Teamwork: Different teams can work on separate FastMCP servers that are later combined.
- Organization: Keep related functionality grouped together logically.
Importing vs Mounting
The choice of importing or mounting depends on your use case and requirements.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 |
Performance | Fast - no runtime delegation | Slower - affected by slowest mounted server |
Prefix | Optional - omit for original names | Optional - omit for original names |
Best For | Bundling finalized components, performance-critical setups | Modular runtime composition |
Proxy Servers
FastMCP supports MCP proxying, which allows you to mirror a local or remote server in a local FastMCP instance. Proxies are fully compatible with both importing and mounting.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.
Importing (Static Composition)
Theimport_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.
How Importing Works
When you callawait main_mcp.import_server(subserver, prefix={whatever})
:
- Tools: All tools from
subserver
are added tomain_mcp
with names prefixed using{prefix}_
.subserver.tool(name="my_tool")
becomesmain_mcp.tool(name="{prefix}_my_tool")
.
- Resources: All resources are added with both URIs and names prefixed.
- URI:
subserver.resource(uri="data://info")
becomesmain_mcp.resource(uri="data://{prefix}/info")
. - Name:
resource.name
becomes"{prefix}_{resource.name}"
.
- URI:
- Resource Templates: Templates are prefixed similarly to resources.
- URI:
subserver.resource(uri="data://{id}")
becomesmain_mcp.resource(uri="data://{prefix}/{id}")
. - Name:
template.name
becomes"{prefix}_{template.name}"
.
- URI:
- Prompts: All prompts are added with names prefixed using
{prefix}_
.subserver.prompt(name="my_prompt")
becomesmain_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.
The
prefix
parameter is optional. If omitted, components are imported without modification.Importing Without Prefixes
New in version: 2.9.0
You can also import servers without specifying a prefix, which copies components using their original names:
Conflict Resolution
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.
Mounting (Live Linking)
Themount()
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.
How Mounting Works
When mounting is configured:- Live Link: The parent server establishes a connection to the mounted server.
- Dynamic Updates: Changes to the mounted server are immediately reflected when accessed through the parent.
- Prefixed Access: The parent server uses prefixes to route requests to the mounted server.
- Delegation: Requests for components matching the prefix are delegated to the mounted server at runtime.
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.
The
prefix
parameter is optional. If omitted, components are mounted without modification.Performance Considerations
Due to the “live link”, operations likelist_tools()
on the parent server will be impacted by the speed of the slowest mounted server. In particular, HTTP-based mounted servers can introduce significant latency (300-400ms vs 1-2ms for local tools), and this slowdown affects the whole server, not just interactions with the HTTP-proxied tools. If performance is important, importing tools via import_server()
may be a more appropriate solution as it copies components once at startup rather than delegating requests at runtime.
Mounting Without Prefixes
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.
Direct vs. Proxy Mounting
New in version: 2.2.7
FastMCP supports two mounting modes:
- Direct Mounting (default): The parent server directly accesses the mounted server’s objects in memory.
- No client lifecycle events occur on the mounted server
- The mounted server’s lifespan context is not executed
- Communication is handled through direct method calls
- Proxy Mounting: The parent server treats the mounted server as a separate entity and communicates with it through a client interface.
- Full client lifecycle events occur on the mounted server
- The mounted server’s lifespan is executed when a client connects
- Communication happens via an in-memory Client transport
as_proxy
parameter.
Interaction with Proxy Servers
When usingFastMCP.as_proxy()
to create a proxy server, mounting that server will always use proxy mounting:
Resource Prefix Formats
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:
Path Format (Default)
In path format, prefixes are added to the path component of the URI:Protocol Format (Legacy)
In protocol format, prefixes are added as part of the protocol:Configuring the Prefix Format
You can configure the prefix format globally in code:When mounting servers, custom HTTP routes defined with
@server.custom_route()
are also forwarded to the parent server, making them accessible through the parent’s HTTP application.