Server Composition
Combine multiple FastMCP servers into a single, larger application using mounting and importing.
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. In general, importing is best for simpler cases because it copies the imported server’s components into the main server, treating them as native integrations. Mounting is best for more complex cases where you need to delegate requests to the subserver at runtime.
Feature | Importing | Mounting |
---|---|---|
Method | FastMCP.import_server() | FastMCP.mount() |
Composition Type | One-time copy (static) | Live link (dynamic) |
Updates | Changes to subserver NOT reflected | Changes to subserver immediately reflected |
Lifespan | Not managed | Automatically managed |
Synchronicity | Async (must be awaited) | Sync |
Best For | Bundling finalized components | 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.
Importing (Static Composition)
The import_server()
method copies all components (tools, resources, templates, prompts) from one FastMCP
instance (the subserver) into another (the main server). A prefix
is added to avoid naming conflicts.
How Importing Works
When you call await main_mcp.import_server(prefix, subserver)
:
- Tools: All tools from
subserver
are added tomain_mcp
. Their names are automatically prefixed using theprefix
and a default separator (_
).subserver.tool(name="my_tool")
becomesmain_mcp.tool(name="{prefix}_my_tool")
.
- Resources: All resources from
subserver
are added. Their URIs are prefixed using theprefix
and a default separator (+
).subserver.resource(uri="data://info")
becomesmain_mcp.resource(uri="{prefix}+data://info")
.
- Resource Templates: All templates from
subserver
are added. Their URI templates are prefixed similarly to resources.subserver.resource(uri="data://{id}")
becomesmain_mcp.resource(uri="{prefix}+data://{id}")
.
- Prompts: All prompts from
subserver
are added, with names prefixed like tools.subserver.prompt(name="my_prompt")
becomesmain_mcp.prompt(name="{prefix}_my_prompt")
.
Note that import_server
performs a one-time copy of components from the subserver
into the main_mcp
instance at the time the method is called. Changes made to the subserver
after import_server
is called will not be reflected in main_mcp
. Also, the subserver
’s lifespan
context is not executed by the main server when using import_server
.
Customizing Separators
You might prefer different separators for the prefixed names and URIs. You can customize these when calling import_server()
:
Be cautious when choosing separators. Some MCP clients (like Claude Desktop) might have restrictions on characters allowed in tool names (e.g., /
might not be supported). The defaults (_
for names, +
for URIs) are generally safe.
Mounting (Live Linking)
The mount()
method creates a live link between the main_mcp
server and the subserver
. Instead of copying components, requests for components matching the prefix
are delegated to the subserver
at runtime.
How Mounting Works
When you call main_mcp.mount(prefix, server)
:
- Live Link: A live connection is established between
main_mcp
and thesubserver
. - Dynamic Updates: Changes made to the
subserver
(e.g., adding new tools) will be reflected immediately when accessing components throughmain_mcp
. - Lifespan Management: The
subserver
’slifespan
context is automatically managed and executed within themain_mcp
’s lifespan. - Delegation: Requests for components matching the prefix are delegated to the subserver at runtime.
The same prefixing rules apply as with import_server
for naming tools, resources, templates, and prompts.
Customizing Separators
Similar to import_server
, you can customize the separators for the prefixed names and URIs:
Example: Modular Application
Here’s how a modular application might use import_server
:
Now, running main.py
starts a server that exposes:
text_count_words
data_fetch_record
process_and_analyze
text+resource://stopwords
data+data://schema/{table}
(template)
This pattern promotes code organization and reuse within your FastMCP projects.