fastmcp 2.3.2__tar.gz → 2.3.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fastmcp-2.3.2 → fastmcp-2.3.3}/PKG-INFO +1 -1
- fastmcp-2.3.3/docs/servers/composition.mdx +212 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/complex_inputs.py +1 -1
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/desktop.py +1 -1
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/server/server.py +5 -3
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/utilities/tests.py +4 -2
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/client/test_sse.py +9 -2
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_mount.py +15 -0
- fastmcp-2.3.2/docs/servers/composition.mdx +0 -340
- {fastmcp-2.3.2 → fastmcp-2.3.3}/.cursor/rules/core-mcp-objects.mdc +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/.github/ISSUE_TEMPLATE/bug.yml +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/.github/ISSUE_TEMPLATE/config.yml +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/.github/ISSUE_TEMPLATE/enhancement.yml +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/.github/release.yml +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/.github/workflows/publish.yml +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/.github/workflows/run-static.yml +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/.github/workflows/run-tests.yml +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/.gitignore +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/.pre-commit-config.yaml +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/LICENSE +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/README.md +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/Windows_Notes.md +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/assets/demo-inspector.png +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/clients/client.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/clients/transports.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/deployment/asgi.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/deployment/authentication.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/deployment/cli.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/deployment/running-server.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/docs.json +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/getting-started/installation.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/getting-started/quickstart.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/getting-started/welcome.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/patterns/contrib.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/patterns/decorating-methods.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/patterns/fastapi.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/patterns/http-requests.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/patterns/openapi.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/patterns/testing.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/servers/context.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/servers/fastmcp.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/servers/prompts.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/servers/proxy.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/servers/resources.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/servers/tools.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/snippets/version-badge.mdx +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/docs/style.css +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/echo.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/memory.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/mount_example.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/sampling.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/screenshot.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/serializer.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/simple_echo.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/smart_home/README.md +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/smart_home/pyproject.toml +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/smart_home/src/smart_home/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/smart_home/src/smart_home/__main__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/smart_home/src/smart_home/hub.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/smart_home/src/smart_home/lights/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/smart_home/src/smart_home/lights/hue_utils.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/smart_home/src/smart_home/lights/server.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/smart_home/src/smart_home/py.typed +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/smart_home/src/smart_home/settings.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/smart_home/uv.lock +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/examples/text_me.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/justfile +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/pyproject.toml +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/cli/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/cli/claude.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/cli/cli.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/client/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/client/base.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/client/client.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/client/logging.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/client/roots.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/client/sampling.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/client/transports.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/contrib/README.md +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/contrib/bulk_tool_caller/README.md +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/contrib/bulk_tool_caller/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/contrib/bulk_tool_caller/bulk_tool_caller.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/contrib/bulk_tool_caller/example.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/contrib/mcp_mixin/README.md +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/contrib/mcp_mixin/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/contrib/mcp_mixin/example.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/contrib/mcp_mixin/mcp_mixin.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/exceptions.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/low_level/README.md +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/low_level/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/low_level/sse_server_transport.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/prompts/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/prompts/prompt.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/prompts/prompt_manager.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/py.typed +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/resources/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/resources/resource.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/resources/resource_manager.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/resources/template.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/resources/types.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/server/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/server/context.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/server/dependencies.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/server/http.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/server/openapi.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/server/proxy.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/settings.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/tools/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/tools/tool.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/tools/tool_manager.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/utilities/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/utilities/cache.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/utilities/decorators.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/utilities/json_schema.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/utilities/logging.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/utilities/openapi.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/src/fastmcp/utilities/types.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/cli/test_cli.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/cli/test_run.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/client/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/client/test_client.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/client/test_logs.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/client/test_roots.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/client/test_sampling.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/client/test_streamable_http.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/conftest.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/contrib/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/contrib/test_bulk_tool_caller.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/contrib/test_mcp_mixin.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/prompts/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/prompts/test_prompt.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/prompts/test_prompt_manager.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/resources/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/resources/test_file_resources.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/resources/test_function_resources.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/resources/test_resource_manager.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/resources/test_resource_template.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/resources/test_resources.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_auth_integration.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_context.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_file_server.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_http_dependencies.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_http_middleware.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_import_server.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_lifespan.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_openapi.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_proxy.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_run_server.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_server.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_server_interactions.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/server/test_tool_annotations.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/test_deprecated.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/test_examples.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/test_servers/fastmcp_server.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/test_servers/sse.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/test_servers/stdio.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/tools/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/tools/test_tool.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/tools/test_tool_manager.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/utilities/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/utilities/openapi/__init__.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/utilities/openapi/conftest.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/utilities/openapi/test_openapi.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/utilities/openapi/test_openapi_advanced.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/utilities/openapi/test_openapi_fastapi.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/utilities/test_cache.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/utilities/test_decorated_function.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/utilities/test_json_schema.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/utilities/test_logging.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/utilities/test_tests.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/utilities/test_typeadapter.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/tests/utilities/test_types.py +0 -0
- {fastmcp-2.3.2 → fastmcp-2.3.3}/uv.lock +0 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Server Composition
|
|
3
|
+
sidebarTitle: Composition
|
|
4
|
+
description: Combine multiple FastMCP servers into a single, larger application using mounting and importing.
|
|
5
|
+
icon: puzzle-piece
|
|
6
|
+
---
|
|
7
|
+
import { VersionBadge } from '/snippets/version-badge.mdx'
|
|
8
|
+
|
|
9
|
+
<VersionBadge version="2.2.0" />
|
|
10
|
+
|
|
11
|
+
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:
|
|
12
|
+
|
|
13
|
+
- **`import_server`**: For a one-time copy of components with prefixing (static composition).
|
|
14
|
+
- **`mount`**: For creating a live link where the main server delegates requests to the subserver (dynamic composition).
|
|
15
|
+
|
|
16
|
+
## Why Compose Servers?
|
|
17
|
+
|
|
18
|
+
- **Modularity**: Break down large applications into smaller, focused servers (e.g., a `WeatherServer`, a `DatabaseServer`, a `CalendarServer`).
|
|
19
|
+
- **Reusability**: Create common utility servers (e.g., a `TextProcessingServer`) and mount them wherever needed.
|
|
20
|
+
- **Teamwork**: Different teams can work on separate FastMCP servers that are later combined.
|
|
21
|
+
- **Organization**: Keep related functionality grouped together logically.
|
|
22
|
+
|
|
23
|
+
### Importing vs Mounting
|
|
24
|
+
|
|
25
|
+
The choice of importing or mounting depends on your use case and requirements.
|
|
26
|
+
|
|
27
|
+
| Feature | Importing | Mounting |
|
|
28
|
+
|---------|----------------|---------|
|
|
29
|
+
| **Method** | `FastMCP.import_server()` | `FastMCP.mount()` |
|
|
30
|
+
| **Composition Type** | One-time copy (static) | Live link (dynamic) |
|
|
31
|
+
| **Updates** | Changes to subserver NOT reflected | Changes to subserver immediately reflected |
|
|
32
|
+
| **Best For** | Bundling finalized components | Modular runtime composition |
|
|
33
|
+
|
|
34
|
+
### Proxy Servers
|
|
35
|
+
|
|
36
|
+
FastMCP supports [MCP proxying](/patterns/proxy), which allows you to mirror a local or remote server in a local FastMCP instance. Proxies are fully compatible with both importing and mounting.
|
|
37
|
+
|
|
38
|
+
## Importing (Static Composition)
|
|
39
|
+
|
|
40
|
+
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.
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from fastmcp import FastMCP
|
|
44
|
+
import asyncio
|
|
45
|
+
|
|
46
|
+
# Define subservers
|
|
47
|
+
weather_mcp = FastMCP(name="WeatherService")
|
|
48
|
+
|
|
49
|
+
@weather_mcp.tool()
|
|
50
|
+
def get_forecast(city: str) -> dict:
|
|
51
|
+
"""Get weather forecast."""
|
|
52
|
+
return {"city": city, "forecast": "Sunny"}
|
|
53
|
+
|
|
54
|
+
@weather_mcp.resource("data://cities/supported")
|
|
55
|
+
def list_supported_cities() -> list[str]:
|
|
56
|
+
"""List cities with weather support."""
|
|
57
|
+
return ["London", "Paris", "Tokyo"]
|
|
58
|
+
|
|
59
|
+
# Define main server
|
|
60
|
+
main_mcp = FastMCP(name="MainApp")
|
|
61
|
+
|
|
62
|
+
# Import subserver
|
|
63
|
+
async def setup():
|
|
64
|
+
await main_mcp.import_server("weather", weather_mcp)
|
|
65
|
+
|
|
66
|
+
# Result: main_mcp now contains prefixed components:
|
|
67
|
+
# - Tool: "weather_get_forecast"
|
|
68
|
+
# - Resource: "weather+data://cities/supported"
|
|
69
|
+
|
|
70
|
+
if __name__ == "__main__":
|
|
71
|
+
asyncio.run(setup())
|
|
72
|
+
main_mcp.run()
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### How Importing Works
|
|
76
|
+
|
|
77
|
+
When you call `await main_mcp.import_server(prefix, subserver)`:
|
|
78
|
+
|
|
79
|
+
1. **Tools**: All tools from `subserver` are added to `main_mcp` with names prefixed using `{prefix}_`.
|
|
80
|
+
- `subserver.tool(name="my_tool")` becomes `main_mcp.tool(name="{prefix}_my_tool")`.
|
|
81
|
+
2. **Resources**: All resources are added with URIs prefixed using `{prefix}+`.
|
|
82
|
+
- `subserver.resource(uri="data://info")` becomes `main_mcp.resource(uri="{prefix}+data://info")`.
|
|
83
|
+
3. **Resource Templates**: Templates are prefixed similarly to resources.
|
|
84
|
+
- `subserver.resource(uri="data://{id}")` becomes `main_mcp.resource(uri="{prefix}+data://{id}")`.
|
|
85
|
+
4. **Prompts**: All prompts are added with names prefixed like tools.
|
|
86
|
+
- `subserver.prompt(name="my_prompt")` becomes `main_mcp.prompt(name="{prefix}_my_prompt")`.
|
|
87
|
+
|
|
88
|
+
Note that `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.
|
|
89
|
+
|
|
90
|
+
## Mounting (Live Linking)
|
|
91
|
+
|
|
92
|
+
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.
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
import asyncio
|
|
96
|
+
from fastmcp import FastMCP, Client
|
|
97
|
+
|
|
98
|
+
# Define subserver
|
|
99
|
+
dynamic_mcp = FastMCP(name="DynamicService")
|
|
100
|
+
|
|
101
|
+
@dynamic_mcp.tool()
|
|
102
|
+
def initial_tool():
|
|
103
|
+
"""Initial tool demonstration."""
|
|
104
|
+
return "Initial Tool Exists"
|
|
105
|
+
|
|
106
|
+
# Mount subserver (synchronous operation)
|
|
107
|
+
main_mcp = FastMCP(name="MainAppLive")
|
|
108
|
+
main_mcp.mount("dynamic", dynamic_mcp)
|
|
109
|
+
|
|
110
|
+
# Add a tool AFTER mounting - it will be accessible through main_mcp
|
|
111
|
+
@dynamic_mcp.tool()
|
|
112
|
+
def added_later():
|
|
113
|
+
"""Tool added after mounting."""
|
|
114
|
+
return "Tool Added Dynamically!"
|
|
115
|
+
|
|
116
|
+
# Testing access to mounted tools
|
|
117
|
+
async def test_dynamic_mount():
|
|
118
|
+
tools = await main_mcp.get_tools()
|
|
119
|
+
print("Available tools:", list(tools.keys()))
|
|
120
|
+
# Shows: ['dynamic_initial_tool', 'dynamic_added_later']
|
|
121
|
+
|
|
122
|
+
async with Client(main_mcp) as client:
|
|
123
|
+
result = await client.call_tool("dynamic_added_later")
|
|
124
|
+
print("Result:", result[0].text)
|
|
125
|
+
# Shows: "Tool Added Dynamically!"
|
|
126
|
+
|
|
127
|
+
if __name__ == "__main__":
|
|
128
|
+
asyncio.run(test_dynamic_mount())
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### How Mounting Works
|
|
132
|
+
|
|
133
|
+
When mounting is configured:
|
|
134
|
+
|
|
135
|
+
1. **Live Link**: The parent server establishes a connection to the mounted server.
|
|
136
|
+
2. **Dynamic Updates**: Changes to the mounted server are immediately reflected when accessed through the parent.
|
|
137
|
+
3. **Prefixed Access**: The parent server uses prefixes to route requests to the mounted server.
|
|
138
|
+
4. **Delegation**: Requests for components matching the prefix are delegated to the mounted server at runtime.
|
|
139
|
+
|
|
140
|
+
The same prefixing rules apply as with `import_server` for naming tools, resources, templates, and prompts.
|
|
141
|
+
|
|
142
|
+
### Direct vs. Proxy Mounting
|
|
143
|
+
|
|
144
|
+
<VersionBadge version="2.2.7" />
|
|
145
|
+
|
|
146
|
+
FastMCP supports two mounting modes:
|
|
147
|
+
|
|
148
|
+
1. **Direct Mounting** (default): The parent server directly accesses the mounted server's objects in memory.
|
|
149
|
+
- No client lifecycle events occur on the mounted server
|
|
150
|
+
- The mounted server's lifespan context is not executed
|
|
151
|
+
- Communication is handled through direct method calls
|
|
152
|
+
|
|
153
|
+
2. **Proxy Mounting**: The parent server treats the mounted server as a separate entity and communicates with it through a client interface.
|
|
154
|
+
- Full client lifecycle events occur on the mounted server
|
|
155
|
+
- The mounted server's lifespan is executed when a client connects
|
|
156
|
+
- Communication happens via an in-memory Client transport
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
# Direct mounting (default when no custom lifespan)
|
|
160
|
+
main_mcp.mount("api", api_server)
|
|
161
|
+
|
|
162
|
+
# Proxy mounting (preserves full client lifecycle)
|
|
163
|
+
main_mcp.mount("api", api_server, as_proxy=True)
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
FastMCP automatically uses proxy mounting when the mounted server has a custom lifespan, but you can override this behavior with the `as_proxy` parameter.
|
|
167
|
+
|
|
168
|
+
#### Interaction with Proxy Servers
|
|
169
|
+
|
|
170
|
+
When using `FastMCP.from_client()` to create a proxy server, mounting that server will always use proxy mounting:
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
# Create a proxy for a remote server
|
|
174
|
+
remote_proxy = FastMCP.from_client(Client("http://example.com/mcp"))
|
|
175
|
+
|
|
176
|
+
# Mount the proxy (always uses proxy mounting)
|
|
177
|
+
main_server.mount("remote", remote_proxy)
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Customizing Separators
|
|
181
|
+
|
|
182
|
+
Both `import_server()` and `mount()` allow you to customize the separators used for prefixing components. The defaults are `_` for tools and prompts, and `+` for resources.
|
|
183
|
+
|
|
184
|
+
<CodeGroup>
|
|
185
|
+
|
|
186
|
+
```python import_server
|
|
187
|
+
await main_mcp.import_server(
|
|
188
|
+
prefix="api",
|
|
189
|
+
app=some_subserver,
|
|
190
|
+
tool_separator="_", # Tool name becomes: "api_sub_tool_name"
|
|
191
|
+
resource_separator="+", # Resource URI becomes: "api+data://sub_resource"
|
|
192
|
+
prompt_separator="_" # Prompt name becomes: "api_sub_prompt_name"
|
|
193
|
+
)
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
```python mount
|
|
197
|
+
main_mcp.mount(
|
|
198
|
+
prefix="api",
|
|
199
|
+
app=some_subserver,
|
|
200
|
+
tool_separator="_", # Tool name becomes: "api_sub_tool_name"
|
|
201
|
+
resource_separator="+", # Resource URI becomes: "api+data://sub_resource"
|
|
202
|
+
prompt_separator="_" # Prompt name becomes: "api_sub_prompt_name"
|
|
203
|
+
)
|
|
204
|
+
```
|
|
205
|
+
</CodeGroup>
|
|
206
|
+
<Warning>
|
|
207
|
+
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.
|
|
208
|
+
</Warning>
|
|
209
|
+
|
|
210
|
+
<Tip>
|
|
211
|
+
To "cleanly" import or mount a server, set the prefix and all separators to `""` (empty string). This is generally unecessary but could save a couple tokens at the risk of a name collision!
|
|
212
|
+
</Tip>
|
|
@@ -869,7 +869,7 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
869
869
|
elif transport == "sse":
|
|
870
870
|
return create_sse_app(
|
|
871
871
|
server=self,
|
|
872
|
-
message_path=
|
|
872
|
+
message_path=self.settings.message_path,
|
|
873
873
|
sse_path=path or self.settings.sse_path,
|
|
874
874
|
auth_server_provider=self._auth_server_provider,
|
|
875
875
|
auth_settings=self.settings.auth,
|
|
@@ -1094,11 +1094,13 @@ class FastMCP(Generic[LifespanResultT]):
|
|
|
1094
1094
|
|
|
1095
1095
|
def _validate_resource_prefix(prefix: str) -> None:
|
|
1096
1096
|
valid_resource = "resource://path/to/resource"
|
|
1097
|
+
test_case = f"{prefix}{valid_resource}"
|
|
1097
1098
|
try:
|
|
1098
|
-
AnyUrl(
|
|
1099
|
+
AnyUrl(test_case)
|
|
1099
1100
|
except pydantic.ValidationError as e:
|
|
1100
1101
|
raise ValueError(
|
|
1101
|
-
|
|
1102
|
+
"Resource prefix or separator would result in an "
|
|
1103
|
+
f"invalid resource URI (test case was {test_case!r}): {e}"
|
|
1102
1104
|
)
|
|
1103
1105
|
|
|
1104
1106
|
|
|
@@ -71,7 +71,7 @@ def _run_server(mcp_server: FastMCP, transport: Literal["sse"], port: int) -> No
|
|
|
71
71
|
|
|
72
72
|
@contextmanager
|
|
73
73
|
def run_server_in_process(
|
|
74
|
-
server_fn: Callable[[str, int], None],
|
|
74
|
+
server_fn: Callable[[str, int], None], *args
|
|
75
75
|
) -> Generator[str, None, None]:
|
|
76
76
|
"""
|
|
77
77
|
Context manager that runs a Starlette app in a separate process and returns the
|
|
@@ -88,7 +88,9 @@ def run_server_in_process(
|
|
|
88
88
|
s.bind((host, 0))
|
|
89
89
|
port = s.getsockname()[1]
|
|
90
90
|
|
|
91
|
-
proc = multiprocessing.Process(
|
|
91
|
+
proc = multiprocessing.Process(
|
|
92
|
+
target=server_fn, args=(host, port, *args), daemon=True
|
|
93
|
+
)
|
|
92
94
|
proc.start()
|
|
93
95
|
|
|
94
96
|
# Wait for server to be running
|
|
@@ -56,9 +56,9 @@ def fastmcp_server():
|
|
|
56
56
|
return server
|
|
57
57
|
|
|
58
58
|
|
|
59
|
-
def run_server(host: str, port: int) -> None:
|
|
59
|
+
def run_server(host: str, port: int, path: str | None = None) -> None:
|
|
60
60
|
try:
|
|
61
|
-
app = fastmcp_server().http_app(transport="sse")
|
|
61
|
+
app = fastmcp_server().http_app(transport="sse", path=path)
|
|
62
62
|
server = uvicorn.Server(
|
|
63
63
|
config=uvicorn.Config(app=app, host=host, port=port, log_level="error")
|
|
64
64
|
)
|
|
@@ -109,6 +109,13 @@ def run_nested_server(host: str, port: int) -> None:
|
|
|
109
109
|
sys.exit(0)
|
|
110
110
|
|
|
111
111
|
|
|
112
|
+
async def test_run_server_on_path():
|
|
113
|
+
with run_server_in_process(run_server, "/help") as url:
|
|
114
|
+
async with Client(transport=SSETransport(f"{url}/help")) as client:
|
|
115
|
+
result = await client.ping()
|
|
116
|
+
assert result is True
|
|
117
|
+
|
|
118
|
+
|
|
112
119
|
async def test_nested_sse_server_resolves_correctly():
|
|
113
120
|
# tests patch for
|
|
114
121
|
# https://github.com/modelcontextprotocol/python-sdk/pull/659
|
|
@@ -106,6 +106,21 @@ class TestBasicMount:
|
|
|
106
106
|
with pytest.raises(NotFoundError, match="Unknown tool: sub_sub_tool"):
|
|
107
107
|
await main_app._mcp_call_tool("sub_sub_tool", {})
|
|
108
108
|
|
|
109
|
+
async def test_mount_with_no_prefix(self):
|
|
110
|
+
main_app = FastMCP("MainApp")
|
|
111
|
+
sub_app = FastMCP("SubApp")
|
|
112
|
+
|
|
113
|
+
@sub_app.tool()
|
|
114
|
+
def sub_tool() -> str:
|
|
115
|
+
return "This is from the sub app"
|
|
116
|
+
|
|
117
|
+
main_app.mount(
|
|
118
|
+
prefix="", server=sub_app, tool_separator="", resource_separator=""
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
tools = await main_app.get_tools()
|
|
122
|
+
assert "sub_tool" in tools
|
|
123
|
+
|
|
109
124
|
|
|
110
125
|
class TestMultipleServerMount:
|
|
111
126
|
"""Test mounting multiple servers simultaneously."""
|
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: Server Composition
|
|
3
|
-
sidebarTitle: Composition
|
|
4
|
-
description: Combine multiple FastMCP servers into a single, larger application using mounting and importing.
|
|
5
|
-
icon: puzzle-piece
|
|
6
|
-
---
|
|
7
|
-
import { VersionBadge } from '/snippets/version-badge.mdx'
|
|
8
|
-
|
|
9
|
-
<VersionBadge version="2.2.0" />
|
|
10
|
-
|
|
11
|
-
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:
|
|
12
|
-
|
|
13
|
-
- **`import_server`**: For a one-time copy of components with prefixing (static composition).
|
|
14
|
-
- **`mount`**: For creating a live link where the main server delegates requests to the subserver (dynamic composition).
|
|
15
|
-
|
|
16
|
-
## Why Compose Servers?
|
|
17
|
-
|
|
18
|
-
- **Modularity**: Break down large applications into smaller, focused servers (e.g., a `WeatherServer`, a `DatabaseServer`, a `CalendarServer`).
|
|
19
|
-
- **Reusability**: Create common utility servers (e.g., a `TextProcessingServer`) and mount them wherever needed.
|
|
20
|
-
- **Teamwork**: Different teams can work on separate FastMCP servers that are later combined.
|
|
21
|
-
- **Organization**: Keep related functionality grouped together logically.
|
|
22
|
-
|
|
23
|
-
### Importing vs Mounting
|
|
24
|
-
|
|
25
|
-
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.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
| Feature | Importing | Mounting |
|
|
29
|
-
|---------|----------------|---------|
|
|
30
|
-
| **Method** | `FastMCP.import_server()` | `FastMCP.mount()` |
|
|
31
|
-
| **Composition Type** | One-time copy (static) | Live link (dynamic) |
|
|
32
|
-
| **Updates** | Changes to subserver NOT reflected | Changes to subserver immediately reflected |
|
|
33
|
-
| **Best For** | Bundling finalized components | Modular runtime composition |
|
|
34
|
-
|
|
35
|
-
### Proxy Servers
|
|
36
|
-
|
|
37
|
-
FastMCP supports [MCP proxying](/patterns/proxy), which allows you to mirror a local or remote server in a local FastMCP instance. Proxies are fully compatible with both importing and mounting.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
## Importing (Static Composition)
|
|
41
|
-
|
|
42
|
-
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.
|
|
43
|
-
|
|
44
|
-
```python
|
|
45
|
-
from fastmcp import FastMCP
|
|
46
|
-
import asyncio
|
|
47
|
-
|
|
48
|
-
# --- Define Subservers ---
|
|
49
|
-
|
|
50
|
-
# Weather Service
|
|
51
|
-
weather_mcp = FastMCP(name="WeatherService")
|
|
52
|
-
|
|
53
|
-
@weather_mcp.tool()
|
|
54
|
-
def get_forecast(city: str) -> dict:
|
|
55
|
-
"""Get weather forecast."""
|
|
56
|
-
return {"city": city, "forecast": "Sunny"}
|
|
57
|
-
|
|
58
|
-
@weather_mcp.resource("data://cities/supported")
|
|
59
|
-
def list_supported_cities() -> list[str]:
|
|
60
|
-
"""List cities with weather support."""
|
|
61
|
-
return ["London", "Paris", "Tokyo"]
|
|
62
|
-
|
|
63
|
-
# Calculator Service
|
|
64
|
-
calc_mcp = FastMCP(name="CalculatorService")
|
|
65
|
-
|
|
66
|
-
@calc_mcp.tool()
|
|
67
|
-
def add(a: int, b: int) -> int:
|
|
68
|
-
"""Add two numbers."""
|
|
69
|
-
return a + b
|
|
70
|
-
|
|
71
|
-
@calc_mcp.prompt()
|
|
72
|
-
def explain_addition() -> str:
|
|
73
|
-
"""Explain the concept of addition."""
|
|
74
|
-
return "Addition is the process of combining two or more numbers."
|
|
75
|
-
|
|
76
|
-
# --- Define Main Server ---
|
|
77
|
-
main_mcp = FastMCP(name="MainApp")
|
|
78
|
-
|
|
79
|
-
# --- Import Subservers ---
|
|
80
|
-
async def setup():
|
|
81
|
-
# Import weather service with prefix "weather"
|
|
82
|
-
await main_mcp.import_server("weather", weather_mcp)
|
|
83
|
-
|
|
84
|
-
# Import calculator service with prefix "calc"
|
|
85
|
-
await main_mcp.import_server("calc", calc_mcp)
|
|
86
|
-
|
|
87
|
-
# --- Now, main_mcp contains *copied* components ---
|
|
88
|
-
# Tools:
|
|
89
|
-
# - "weather_get_forecast"
|
|
90
|
-
# - "calc_add"
|
|
91
|
-
# Resources:
|
|
92
|
-
# - "weather+data://cities/supported" (prefixed URI)
|
|
93
|
-
# Prompts:
|
|
94
|
-
# - "calc_explain_addition"
|
|
95
|
-
|
|
96
|
-
if __name__ == "__main__":
|
|
97
|
-
# In a real app, you might run this async or setup imports differently
|
|
98
|
-
asyncio.run(setup())
|
|
99
|
-
# Run the main server, which now includes components from both subservers
|
|
100
|
-
main_mcp.run()
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
### How Importing Works
|
|
104
|
-
|
|
105
|
-
When you call `await main_mcp.import_server(prefix, subserver)`:
|
|
106
|
-
|
|
107
|
-
1. **Tools**: All tools from `subserver` are added to `main_mcp`. Their names are automatically prefixed using the `prefix` and a default separator (`_`).
|
|
108
|
-
- `subserver.tool(name="my_tool")` becomes `main_mcp.tool(name="{prefix}_my_tool")`.
|
|
109
|
-
2. **Resources**: All resources from `subserver` are added. Their URIs are prefixed using the `prefix` and a default separator (`+`).
|
|
110
|
-
- `subserver.resource(uri="data://info")` becomes `main_mcp.resource(uri="{prefix}+data://info")`.
|
|
111
|
-
3. **Resource Templates**: All templates from `subserver` are added. Their URI *templates* are prefixed similarly to resources.
|
|
112
|
-
- `subserver.resource(uri="data://{id}")` becomes `main_mcp.resource(uri="{prefix}+data://{id}")`.
|
|
113
|
-
4. **Prompts**: All prompts from `subserver` are added, with names prefixed like tools.
|
|
114
|
-
- `subserver.prompt(name="my_prompt")` becomes `main_mcp.prompt(name="{prefix}_my_prompt")`.
|
|
115
|
-
|
|
116
|
-
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`.
|
|
117
|
-
|
|
118
|
-
### Customizing Separators
|
|
119
|
-
|
|
120
|
-
You might prefer different separators for the prefixed names and URIs. You can customize these when calling `import_server()`:
|
|
121
|
-
|
|
122
|
-
```python
|
|
123
|
-
await main_mcp.import_server(
|
|
124
|
-
prefix="api",
|
|
125
|
-
app=some_subserver,
|
|
126
|
-
tool_separator="/", # Tool name becomes: "api/sub_tool_name"
|
|
127
|
-
resource_separator=":", # Resource URI becomes: "api:data://sub_resource"
|
|
128
|
-
prompt_separator="." # Prompt name becomes: "api.sub_prompt_name"
|
|
129
|
-
)
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
<Warning>
|
|
133
|
-
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.
|
|
134
|
-
</Warning>
|
|
135
|
-
|
|
136
|
-
## Mounting (Live Linking)
|
|
137
|
-
|
|
138
|
-
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.
|
|
139
|
-
|
|
140
|
-
```python
|
|
141
|
-
import asyncio
|
|
142
|
-
from fastmcp import FastMCP, Client
|
|
143
|
-
|
|
144
|
-
# --- Define Subserver ---
|
|
145
|
-
dynamic_mcp = FastMCP(name="DynamicService")
|
|
146
|
-
@dynamic_mcp.tool()
|
|
147
|
-
def initial_tool(): return "Initial Tool Exists"
|
|
148
|
-
|
|
149
|
-
# --- Define Main Server ---
|
|
150
|
-
main_mcp = FastMCP(name="MainAppLive")
|
|
151
|
-
|
|
152
|
-
# --- Mount Subserver (Sync operation) ---
|
|
153
|
-
main_mcp.mount("dynamic", dynamic_mcp)
|
|
154
|
-
|
|
155
|
-
print("Mounted dynamic_mcp.")
|
|
156
|
-
|
|
157
|
-
# --- Add a tool AFTER mounting ---
|
|
158
|
-
@dynamic_mcp.tool()
|
|
159
|
-
def added_later(): return "Tool Added Dynamically!"
|
|
160
|
-
|
|
161
|
-
print("Added 'added_later' tool to dynamic_mcp.")
|
|
162
|
-
|
|
163
|
-
# --- Test Access ---
|
|
164
|
-
async def test_dynamic_mount():
|
|
165
|
-
# Need to use await for get_tools now
|
|
166
|
-
tools_before = await main_mcp.get_tools()
|
|
167
|
-
print("Tools available via main_mcp:", list(tools_before.keys()))
|
|
168
|
-
# Expected: ['dynamic_initial_tool', 'dynamic_added_later']
|
|
169
|
-
|
|
170
|
-
async with Client(main_mcp) as client:
|
|
171
|
-
# Call the dynamically added tool via the main server
|
|
172
|
-
result = await client.call_tool("dynamic_added_later")
|
|
173
|
-
print("Result of calling dynamic_added_later:", result[0].text)
|
|
174
|
-
# Expected: Tool Added Dynamically!
|
|
175
|
-
|
|
176
|
-
if __name__ == "__main__":
|
|
177
|
-
# Need async context to test
|
|
178
|
-
asyncio.run(test_dynamic_mount())
|
|
179
|
-
# To run the server itself:
|
|
180
|
-
# main_mcp.run()
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
### How Mounting Works
|
|
184
|
-
|
|
185
|
-
Mounting creates a relationship between two servers where one server (the parent) delegates certain operations to another (the mounted server) based on prefixes. When mounting is configured:
|
|
186
|
-
|
|
187
|
-
1. **Live Link**: The parent server establishes a connection to the mounted server.
|
|
188
|
-
2. **Dynamic Updates**: Changes made to the mounted server (e.g., adding new tools) are immediately reflected when accessed through the parent server.
|
|
189
|
-
3. **Prefixed Access**: The parent server uses prefixes to route requests to the mounted server.
|
|
190
|
-
4. **Delegation**: Requests for components matching the prefix are delegated to the mounted server at runtime.
|
|
191
|
-
|
|
192
|
-
The same prefixing rules apply as with `import_server` for naming tools, resources, templates, and prompts.
|
|
193
|
-
|
|
194
|
-
### Customizing Separators
|
|
195
|
-
|
|
196
|
-
Similar to `import_server`, you can customize the separators for the prefixed names and URIs:
|
|
197
|
-
|
|
198
|
-
```python
|
|
199
|
-
main_mcp.mount(
|
|
200
|
-
prefix="api",
|
|
201
|
-
app=some_subserver,
|
|
202
|
-
tool_separator="/", # Tool name becomes: "api/sub_tool_name"
|
|
203
|
-
resource_separator=":", # Resource URI becomes: "api:data://sub_resource"
|
|
204
|
-
prompt_separator="." # Prompt name becomes: "api.sub_prompt_name"
|
|
205
|
-
)
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
### Direct vs. Proxy Mounting
|
|
209
|
-
|
|
210
|
-
<VersionBadge version="2.2.7" />
|
|
211
|
-
|
|
212
|
-
FastMCP supports two modes for mounting servers:
|
|
213
|
-
|
|
214
|
-
1. **Direct Mounting** (default): The parent server directly accesses the mounted server's objects in memory for optimal performance and observability. In this mode:
|
|
215
|
-
- No client lifecycle events occur on the mounted server
|
|
216
|
-
- The mounted server's lifespan context is not executed
|
|
217
|
-
- Communication is handled through direct method calls
|
|
218
|
-
|
|
219
|
-
2. **Proxy Mounting**: The parent server treats the mounted server as a separate entity and communicates with it through a client interface. In this mode:
|
|
220
|
-
- Full client lifecycle events occur on the mounted server
|
|
221
|
-
- The mounted server's lifespan is executed when a client connects
|
|
222
|
-
- Communication happens via an in-memory Client transport
|
|
223
|
-
- This preserves all client-facing behaviors but is slightly less efficient
|
|
224
|
-
|
|
225
|
-
You can control which mode to use with the `as_proxy` parameter:
|
|
226
|
-
|
|
227
|
-
```python
|
|
228
|
-
# Direct mounting (default when no custom lifespan)
|
|
229
|
-
main_mcp.mount("api", api_server)
|
|
230
|
-
|
|
231
|
-
# Proxy mounting (preserves full client lifecycle)
|
|
232
|
-
main_mcp.mount("api", api_server, as_proxy=True)
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
FastMCP automatically uses proxy mounting when the mounted server has a custom lifespan, but you can override this behavior by explicitly setting `as_proxy=False` or `as_proxy=True`.
|
|
236
|
-
|
|
237
|
-
#### Interaction with Proxy Servers
|
|
238
|
-
|
|
239
|
-
When using `FastMCP.from_client()` to create a proxy server, mounting that server will always use proxy mounting since the proxy server is already designed to be accessed via a client interface.
|
|
240
|
-
|
|
241
|
-
```python
|
|
242
|
-
from fastmcp import FastMCP, Client
|
|
243
|
-
|
|
244
|
-
# Create a proxy for a remote server
|
|
245
|
-
remote_proxy = FastMCP.from_client(Client("http://example.com/mcp"))
|
|
246
|
-
|
|
247
|
-
# Mount the proxy - this will preserve full client lifecycle
|
|
248
|
-
main_server.mount("remote", remote_proxy)
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
This is particularly useful for incorporating remote servers into your local application architecture.
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
## Example: Modular Application
|
|
255
|
-
|
|
256
|
-
Here's how a modular application might use `import_server`:
|
|
257
|
-
|
|
258
|
-
<CodeGroup>
|
|
259
|
-
```python main.py
|
|
260
|
-
from fastmcp import FastMCP
|
|
261
|
-
import asyncio
|
|
262
|
-
|
|
263
|
-
# Import the servers (see other files)
|
|
264
|
-
from modules.text_server import text_mcp
|
|
265
|
-
from modules.data_server import data_mcp
|
|
266
|
-
|
|
267
|
-
app = FastMCP(name="MainApplication")
|
|
268
|
-
|
|
269
|
-
# Setup function for async imports
|
|
270
|
-
async def setup():
|
|
271
|
-
# Import the utility servers
|
|
272
|
-
await app.import_server("text", text_mcp)
|
|
273
|
-
await app.import_server("data", data_mcp)
|
|
274
|
-
|
|
275
|
-
@app.tool()
|
|
276
|
-
def process_and_analyze(record_id: int) -> str:
|
|
277
|
-
"""Fetches a record and analyzes its string representation."""
|
|
278
|
-
# In a real application, you'd use proper methods to interact between
|
|
279
|
-
# imported tools rather than accessing internal managers
|
|
280
|
-
|
|
281
|
-
# Get record data
|
|
282
|
-
record = {"id": record_id, "value": random.random()}
|
|
283
|
-
|
|
284
|
-
# Count words in the record string representation
|
|
285
|
-
word_count = len(str(record).split())
|
|
286
|
-
|
|
287
|
-
return (
|
|
288
|
-
f"Record {record_id} has {word_count} words in its string "
|
|
289
|
-
f"representation."
|
|
290
|
-
)
|
|
291
|
-
|
|
292
|
-
if __name__ == "__main__":
|
|
293
|
-
# Run async setup before starting the server
|
|
294
|
-
asyncio.run(setup())
|
|
295
|
-
# Run the server
|
|
296
|
-
app.run()
|
|
297
|
-
```
|
|
298
|
-
```python modules/text_server.py
|
|
299
|
-
from fastmcp import FastMCP
|
|
300
|
-
|
|
301
|
-
text_mcp = FastMCP(name="TextUtilities")
|
|
302
|
-
|
|
303
|
-
@text_mcp.tool()
|
|
304
|
-
def count_words(text: str) -> int:
|
|
305
|
-
"""Counts words in a text."""
|
|
306
|
-
return len(text.split())
|
|
307
|
-
|
|
308
|
-
@text_mcp.resource("resource://stopwords")
|
|
309
|
-
def get_stopwords() -> list[str]:
|
|
310
|
-
"""Return a list of common stopwords."""
|
|
311
|
-
return ["the", "a", "is", "in"]
|
|
312
|
-
```
|
|
313
|
-
|
|
314
|
-
```python modules/data_server.py
|
|
315
|
-
from fastmcp import FastMCP
|
|
316
|
-
import random
|
|
317
|
-
from typing import dict
|
|
318
|
-
|
|
319
|
-
data_mcp = FastMCP(name="DataAPI")
|
|
320
|
-
|
|
321
|
-
@data_mcp.tool()
|
|
322
|
-
def fetch_record(record_id: int) -> dict:
|
|
323
|
-
"""Fetches a dummy data record."""
|
|
324
|
-
return {"id": record_id, "value": random.random()}
|
|
325
|
-
|
|
326
|
-
@data_mcp.resource("data://schema/{table}")
|
|
327
|
-
def get_table_schema(table: str) -> dict:
|
|
328
|
-
"""Provides a dummy schema for a table."""
|
|
329
|
-
return {"table": table, "columns": ["id", "value"]}
|
|
330
|
-
```
|
|
331
|
-
|
|
332
|
-
</CodeGroup>
|
|
333
|
-
Now, running `main.py` starts a server that exposes:
|
|
334
|
-
- `text_count_words`
|
|
335
|
-
- `data_fetch_record`
|
|
336
|
-
- `process_and_analyze`
|
|
337
|
-
- `text+resource://stopwords`
|
|
338
|
-
- `data+data://schema/{table}` (template)
|
|
339
|
-
|
|
340
|
-
This pattern promotes code organization and reuse within your FastMCP projects.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|