fastmcp 2.1.2__tar.gz → 2.2.0__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.1.2 → fastmcp-2.2.0}/PKG-INFO +6 -5
- {fastmcp-2.1.2 → fastmcp-2.2.0}/README.md +3 -3
- fastmcp-2.1.2/docs/clients/overview.mdx → fastmcp-2.2.0/docs/clients/client.mdx +5 -1
- {fastmcp-2.1.2 → fastmcp-2.2.0}/docs/clients/transports.mdx +1 -1
- {fastmcp-2.1.2 → fastmcp-2.2.0}/docs/docs.json +2 -2
- fastmcp-2.2.0/docs/patterns/composition.mdx +297 -0
- fastmcp-2.2.0/docs/patterns/fastapi.mdx +123 -0
- fastmcp-2.2.0/docs/patterns/openapi.mdx +156 -0
- fastmcp-2.1.2/docs/patterns/proxying.mdx → fastmcp-2.2.0/docs/patterns/proxy.mdx +15 -11
- {fastmcp-2.1.2 → fastmcp-2.2.0}/docs/servers/context.mdx +4 -1
- {fastmcp-2.1.2 → fastmcp-2.2.0}/docs/servers/fastmcp.mdx +66 -75
- {fastmcp-2.1.2 → fastmcp-2.2.0}/docs/servers/prompts.mdx +7 -8
- {fastmcp-2.1.2 → fastmcp-2.2.0}/docs/servers/resources.mdx +146 -34
- {fastmcp-2.1.2 → fastmcp-2.2.0}/docs/servers/tools.mdx +7 -8
- fastmcp-2.2.0/docs/snippets/version-badge.mdx +8 -0
- fastmcp-2.2.0/docs/style.css +53 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/pyproject.toml +8 -6
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/cli/cli.py +32 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/client/client.py +16 -15
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/client/transports.py +28 -7
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/exceptions.py +8 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/prompts/prompt.py +20 -1
- fastmcp-2.2.0/src/fastmcp/prompts/prompt_manager.py +83 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/resources/resource.py +20 -1
- fastmcp-2.2.0/src/fastmcp/resources/resource_manager.py +245 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/resources/template.py +81 -8
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/server/openapi.py +10 -16
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/server/proxy.py +102 -76
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/server/server.py +319 -256
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/settings.py +8 -11
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/tools/tool.py +67 -7
- fastmcp-2.2.0/src/fastmcp/tools/tool_manager.py +98 -0
- fastmcp-2.2.0/tests/cli/test_run.py +77 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/client/test_client.py +109 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/prompts/test_base.py +0 -11
- fastmcp-2.2.0/tests/prompts/test_prompt_manager.py +261 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/resources/test_file_resources.py +0 -4
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/resources/test_function_resources.py +0 -7
- fastmcp-2.2.0/tests/resources/test_resource_manager.py +542 -0
- fastmcp-2.2.0/tests/resources/test_resource_template.py +378 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/resources/test_resources.py +0 -1
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/server/test_file_server.py +2 -7
- fastmcp-2.2.0/tests/server/test_import_server.py +391 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/server/test_lifespan.py +0 -2
- fastmcp-2.2.0/tests/server/test_mount.py +429 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/server/test_openapi.py +68 -30
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/server/test_proxy.py +64 -40
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/server/test_run_server.py +1 -1
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/server/test_server.py +398 -197
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/tools/test_tool_manager.py +203 -150
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/utilities/test_func_metadata.py +0 -3
- {fastmcp-2.1.2 → fastmcp-2.2.0}/uv.lock +7 -3
- fastmcp-2.1.2/docs/patterns/composition.mdx +0 -186
- fastmcp-2.1.2/docs/patterns/fastapi.mdx +0 -114
- fastmcp-2.1.2/docs/patterns/openapi.mdx +0 -174
- fastmcp-2.1.2/docs/servers/resources_backup.mdx +0 -270
- fastmcp-2.1.2/docs/style.css +0 -13
- fastmcp-2.1.2/src/fastmcp/prompts/prompt_manager.py +0 -93
- fastmcp-2.1.2/src/fastmcp/resources/resource_manager.py +0 -278
- fastmcp-2.1.2/src/fastmcp/tools/tool_manager.py +0 -99
- fastmcp-2.1.2/tests/prompts/test_prompt_manager.py +0 -344
- fastmcp-2.1.2/tests/resources/test_resource_manager.py +0 -558
- fastmcp-2.1.2/tests/resources/test_resource_template.py +0 -188
- fastmcp-2.1.2/tests/server/test_mount.py +0 -232
- {fastmcp-2.1.2 → fastmcp-2.2.0}/.cursor/rules/core-mcp-objects.mdc +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/.github/release.yml +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/.github/workflows/publish.yml +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/.github/workflows/run-static.yml +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/.github/workflows/run-tests.yml +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/.gitignore +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/.pre-commit-config.yaml +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/LICENSE +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/Windows_Notes.md +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/docs/assets/demo-inspector.png +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/docs/getting-started/installation.mdx +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/docs/getting-started/quickstart.mdx +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/docs/getting-started/welcome.mdx +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/docs/patterns/decorating-methods.mdx +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/complex_inputs.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/desktop.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/echo.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/memory.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/mount_example.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/readme-quickstart.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/sampling.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/screenshot.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/simple_echo.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/smart_home/README.md +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/smart_home/pyproject.toml +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/__main__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/hub.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/lights/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/lights/hue_utils.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/lights/server.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/py.typed +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/settings.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/smart_home/uv.lock +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/examples/text_me.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/justfile +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/cli/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/cli/claude.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/client/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/client/base.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/client/roots.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/client/sampling.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/prompts/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/py.typed +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/resources/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/resources/types.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/server/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/server/context.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/tools/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/utilities/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/utilities/decorators.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/utilities/func_metadata.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/utilities/logging.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/utilities/openapi.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/src/fastmcp/utilities/types.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/client/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/client/test_roots.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/client/test_sampling.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/conftest.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/prompts/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/resources/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/server/__init__.py +0 -0
- {fastmcp-2.1.2/tests/server → fastmcp-2.2.0/tests}/test_servers/fastmcp_server.py +0 -0
- {fastmcp-2.1.2/tests/server → fastmcp-2.2.0/tests}/test_servers/sse.py +0 -0
- {fastmcp-2.1.2/tests/server → fastmcp-2.2.0/tests}/test_servers/stdio.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/tools/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/utilities/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/utilities/openapi/__init__.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/utilities/openapi/conftest.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/utilities/openapi/test_openapi.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/utilities/openapi/test_openapi_advanced.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/utilities/openapi/test_openapi_fastapi.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/utilities/test_decorated_function.py +0 -0
- {fastmcp-2.1.2 → fastmcp-2.2.0}/tests/utilities/test_logging.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fastmcp
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.2.0
|
|
4
4
|
Summary: The fast, Pythonic way to build MCP servers.
|
|
5
5
|
Project-URL: Homepage, https://gofastmcp.com
|
|
6
6
|
Project-URL: Repository, https://github.com/jlowin/fastmcp
|
|
@@ -18,7 +18,8 @@ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
|
18
18
|
Classifier: Typing :: Typed
|
|
19
19
|
Requires-Python: >=3.10
|
|
20
20
|
Requires-Dist: dotenv>=0.9.9
|
|
21
|
-
Requires-Dist:
|
|
21
|
+
Requires-Dist: exceptiongroup>=1.2.2
|
|
22
|
+
Requires-Dist: httpx>=0.28.1
|
|
22
23
|
Requires-Dist: mcp<2.0.0,>=1.6.0
|
|
23
24
|
Requires-Dist: openapi-pydantic>=0.5.1
|
|
24
25
|
Requires-Dist: rich>=13.9.4
|
|
@@ -121,7 +122,7 @@ FastMCP provides a high-level, Pythonic interface for building and interacting w
|
|
|
121
122
|
|
|
122
123
|
## Why FastMCP?
|
|
123
124
|
|
|
124
|
-
The MCP protocol is powerful but implementing it involves a lot of boilerplate - server setup, protocol handlers, content types, error management. FastMCP handles all the complex protocol details and server management, so you can focus on building great tools. It
|
|
125
|
+
The MCP protocol is powerful but implementing it involves a lot of boilerplate - server setup, protocol handlers, content types, error management. FastMCP handles all the complex protocol details and server management, so you can focus on building great tools. It's designed to be high-level and Pythonic; in most cases, decorating a function is all you need.
|
|
125
126
|
|
|
126
127
|
FastMCP aims to be:
|
|
127
128
|
|
|
@@ -580,13 +581,13 @@ proxy_client = Client(
|
|
|
580
581
|
)
|
|
581
582
|
|
|
582
583
|
# Create a proxy server that connects to the client and exposes its capabilities
|
|
583
|
-
proxy = FastMCP.
|
|
584
|
+
proxy = FastMCP.from_client(proxy_client, name="Stdio-to-SSE Proxy")
|
|
584
585
|
|
|
585
586
|
if __name__ == "__main__":
|
|
586
587
|
proxy.run(transport='sse')
|
|
587
588
|
```
|
|
588
589
|
|
|
589
|
-
`FastMCP.
|
|
590
|
+
`FastMCP.from_client` is a class method that connects to the target, discovers its capabilities, and dynamically builds the proxy server instance.
|
|
590
591
|
|
|
591
592
|
|
|
592
593
|
|
|
@@ -93,7 +93,7 @@ FastMCP provides a high-level, Pythonic interface for building and interacting w
|
|
|
93
93
|
|
|
94
94
|
## Why FastMCP?
|
|
95
95
|
|
|
96
|
-
The MCP protocol is powerful but implementing it involves a lot of boilerplate - server setup, protocol handlers, content types, error management. FastMCP handles all the complex protocol details and server management, so you can focus on building great tools. It
|
|
96
|
+
The MCP protocol is powerful but implementing it involves a lot of boilerplate - server setup, protocol handlers, content types, error management. FastMCP handles all the complex protocol details and server management, so you can focus on building great tools. It's designed to be high-level and Pythonic; in most cases, decorating a function is all you need.
|
|
97
97
|
|
|
98
98
|
FastMCP aims to be:
|
|
99
99
|
|
|
@@ -552,13 +552,13 @@ proxy_client = Client(
|
|
|
552
552
|
)
|
|
553
553
|
|
|
554
554
|
# Create a proxy server that connects to the client and exposes its capabilities
|
|
555
|
-
proxy = FastMCP.
|
|
555
|
+
proxy = FastMCP.from_client(proxy_client, name="Stdio-to-SSE Proxy")
|
|
556
556
|
|
|
557
557
|
if __name__ == "__main__":
|
|
558
558
|
proxy.run(transport='sse')
|
|
559
559
|
```
|
|
560
560
|
|
|
561
|
-
`FastMCP.
|
|
561
|
+
`FastMCP.from_client` is a class method that connects to the target, discovers its capabilities, and dynamically builds the proxy server instance.
|
|
562
562
|
|
|
563
563
|
|
|
564
564
|
|
|
@@ -5,6 +5,10 @@ description: Learn how to use the FastMCP Client to interact with MCP servers.
|
|
|
5
5
|
icon: user-robot
|
|
6
6
|
---
|
|
7
7
|
|
|
8
|
+
import { VersionBadge } from '/snippets/version-badge.mdx'
|
|
9
|
+
|
|
10
|
+
<VersionBadge version="2.0.0" />
|
|
11
|
+
|
|
8
12
|
The `fastmcp.Client` provides a high-level, asynchronous interface for interacting with any Model Context Protocol (MCP) server, whether it's built with FastMCP or another implementation. It simplifies communication by handling protocol details and connection management.
|
|
9
13
|
|
|
10
14
|
## FastMCP Client
|
|
@@ -248,5 +252,5 @@ async def safe_call_tool():
|
|
|
248
252
|
Other errors, like connection failures, will raise standard Python exceptions (e.g., `ConnectionError`, `TimeoutError`).
|
|
249
253
|
|
|
250
254
|
<Tip>
|
|
251
|
-
The client transport often has its own error-handling mechanisms, so you can not always trap errors like those raised by `call_tool` outside of the `async with` block. Instead, you can call `
|
|
255
|
+
The client transport often has its own error-handling mechanisms, so you can not always trap errors like those raised by `call_tool` outside of the `async with` block. Instead, you can call `call_tool(..., _return_raw_result=True)` to get the raw `mcp.types.CallToolResult` object and handle errors yourself by checking its `isError` attribute.
|
|
252
256
|
</Tip>
|
|
@@ -7,7 +7,7 @@ icon: link
|
|
|
7
7
|
|
|
8
8
|
The FastMCP `Client` relies on a `ClientTransport` object to handle the specifics of connecting to and communicating with an MCP server. FastMCP provides several built-in transport implementations for common connection methods.
|
|
9
9
|
|
|
10
|
-
While the `Client` often infers the correct transport automatically (see [Client Overview](/clients/
|
|
10
|
+
While the `Client` often infers the correct transport automatically (see [Client Overview](/clients/client#transport-inference)), you can also instantiate transports explicitly for more control.
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
## Stdio Transports
|
|
@@ -50,14 +50,14 @@
|
|
|
50
50
|
{
|
|
51
51
|
"group": "Clients",
|
|
52
52
|
"pages": [
|
|
53
|
-
"clients/
|
|
53
|
+
"clients/client",
|
|
54
54
|
"clients/transports"
|
|
55
55
|
]
|
|
56
56
|
},
|
|
57
57
|
{
|
|
58
58
|
"group": "Patterns",
|
|
59
59
|
"pages": [
|
|
60
|
-
"patterns/
|
|
60
|
+
"patterns/proxy",
|
|
61
61
|
"patterns/composition",
|
|
62
62
|
"patterns/decorating-methods",
|
|
63
63
|
"patterns/openapi",
|
|
@@ -0,0 +1,297 @@
|
|
|
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
|
+
| **Lifespan** | Not managed | Automatically managed |
|
|
34
|
+
| **Synchronicity** | Async (must be awaited) | Sync |
|
|
35
|
+
| **Best For** | Bundling finalized components | Modular runtime composition |
|
|
36
|
+
|
|
37
|
+
### Proxy Servers
|
|
38
|
+
|
|
39
|
+
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.
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
## Importing (Static Composition)
|
|
43
|
+
|
|
44
|
+
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.
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
from fastmcp import FastMCP
|
|
48
|
+
import asyncio
|
|
49
|
+
|
|
50
|
+
# --- Define Subservers ---
|
|
51
|
+
|
|
52
|
+
# Weather Service
|
|
53
|
+
weather_mcp = FastMCP(name="WeatherService")
|
|
54
|
+
|
|
55
|
+
@weather_mcp.tool()
|
|
56
|
+
def get_forecast(city: str) -> dict:
|
|
57
|
+
"""Get weather forecast."""
|
|
58
|
+
return {"city": city, "forecast": "Sunny"}
|
|
59
|
+
|
|
60
|
+
@weather_mcp.resource("data://cities/supported")
|
|
61
|
+
def list_supported_cities() -> list[str]:
|
|
62
|
+
"""List cities with weather support."""
|
|
63
|
+
return ["London", "Paris", "Tokyo"]
|
|
64
|
+
|
|
65
|
+
# Calculator Service
|
|
66
|
+
calc_mcp = FastMCP(name="CalculatorService")
|
|
67
|
+
|
|
68
|
+
@calc_mcp.tool()
|
|
69
|
+
def add(a: int, b: int) -> int:
|
|
70
|
+
"""Add two numbers."""
|
|
71
|
+
return a + b
|
|
72
|
+
|
|
73
|
+
@calc_mcp.prompt()
|
|
74
|
+
def explain_addition() -> str:
|
|
75
|
+
"""Explain the concept of addition."""
|
|
76
|
+
return "Addition is the process of combining two or more numbers."
|
|
77
|
+
|
|
78
|
+
# --- Define Main Server ---
|
|
79
|
+
main_mcp = FastMCP(name="MainApp")
|
|
80
|
+
|
|
81
|
+
# --- Import Subservers ---
|
|
82
|
+
async def setup():
|
|
83
|
+
# Import weather service with prefix "weather"
|
|
84
|
+
await main_mcp.import_server("weather", weather_mcp)
|
|
85
|
+
|
|
86
|
+
# Import calculator service with prefix "calc"
|
|
87
|
+
await main_mcp.import_server("calc", calc_mcp)
|
|
88
|
+
|
|
89
|
+
# --- Now, main_mcp contains *copied* components ---
|
|
90
|
+
# Tools:
|
|
91
|
+
# - "weather_get_forecast"
|
|
92
|
+
# - "calc_add"
|
|
93
|
+
# Resources:
|
|
94
|
+
# - "weather+data://cities/supported" (prefixed URI)
|
|
95
|
+
# Prompts:
|
|
96
|
+
# - "calc_explain_addition"
|
|
97
|
+
|
|
98
|
+
if __name__ == "__main__":
|
|
99
|
+
# In a real app, you might run this async or setup imports differently
|
|
100
|
+
asyncio.run(setup())
|
|
101
|
+
# Run the main server, which now includes components from both subservers
|
|
102
|
+
main_mcp.run()
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### How Importing Works
|
|
106
|
+
|
|
107
|
+
When you call `await main_mcp.import_server(prefix, subserver)`:
|
|
108
|
+
|
|
109
|
+
1. **Tools**: All tools from `subserver` are added to `main_mcp`. Their names are automatically prefixed using the `prefix` and a default separator (`_`).
|
|
110
|
+
- `subserver.tool(name="my_tool")` becomes `main_mcp.tool(name="{prefix}_my_tool")`.
|
|
111
|
+
2. **Resources**: All resources from `subserver` are added. Their URIs are prefixed using the `prefix` and a default separator (`+`).
|
|
112
|
+
- `subserver.resource(uri="data://info")` becomes `main_mcp.resource(uri="{prefix}+data://info")`.
|
|
113
|
+
3. **Resource Templates**: All templates from `subserver` are added. Their URI *templates* are prefixed similarly to resources.
|
|
114
|
+
- `subserver.resource(uri="data://{id}")` becomes `main_mcp.resource(uri="{prefix}+data://{id}")`.
|
|
115
|
+
4. **Prompts**: All prompts from `subserver` are added, with names prefixed like tools.
|
|
116
|
+
- `subserver.prompt(name="my_prompt")` becomes `main_mcp.prompt(name="{prefix}_my_prompt")`.
|
|
117
|
+
|
|
118
|
+
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`.
|
|
119
|
+
|
|
120
|
+
### Customizing Separators
|
|
121
|
+
|
|
122
|
+
You might prefer different separators for the prefixed names and URIs. You can customize these when calling `import_server()`:
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
await main_mcp.import_server(
|
|
126
|
+
prefix="api",
|
|
127
|
+
app=some_subserver,
|
|
128
|
+
tool_separator="/", # Tool name becomes: "api/sub_tool_name"
|
|
129
|
+
resource_separator=":", # Resource URI becomes: "api:data://sub_resource"
|
|
130
|
+
prompt_separator="." # Prompt name becomes: "api.sub_prompt_name"
|
|
131
|
+
)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
<Warning>
|
|
135
|
+
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.
|
|
136
|
+
</Warning>
|
|
137
|
+
|
|
138
|
+
## Mounting (Live Linking)
|
|
139
|
+
|
|
140
|
+
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.
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
import asyncio
|
|
144
|
+
from fastmcp import FastMCP, Client
|
|
145
|
+
|
|
146
|
+
# --- Define Subserver ---
|
|
147
|
+
dynamic_mcp = FastMCP(name="DynamicService")
|
|
148
|
+
@dynamic_mcp.tool()
|
|
149
|
+
def initial_tool(): return "Initial Tool Exists"
|
|
150
|
+
|
|
151
|
+
# --- Define Main Server ---
|
|
152
|
+
main_mcp = FastMCP(name="MainAppLive")
|
|
153
|
+
|
|
154
|
+
# --- Mount Subserver (Sync operation) ---
|
|
155
|
+
main_mcp.mount("dynamic", dynamic_mcp)
|
|
156
|
+
|
|
157
|
+
print("Mounted dynamic_mcp.")
|
|
158
|
+
|
|
159
|
+
# --- Add a tool AFTER mounting ---
|
|
160
|
+
@dynamic_mcp.tool()
|
|
161
|
+
def added_later(): return "Tool Added Dynamically!"
|
|
162
|
+
|
|
163
|
+
print("Added 'added_later' tool to dynamic_mcp.")
|
|
164
|
+
|
|
165
|
+
# --- Test Access ---
|
|
166
|
+
async def test_dynamic_mount():
|
|
167
|
+
# Need to use await for get_tools now
|
|
168
|
+
tools_before = await main_mcp.get_tools()
|
|
169
|
+
print("Tools available via main_mcp:", list(tools_before.keys()))
|
|
170
|
+
# Expected: ['dynamic_initial_tool', 'dynamic_added_later']
|
|
171
|
+
|
|
172
|
+
async with Client(main_mcp) as client:
|
|
173
|
+
# Call the dynamically added tool via the main server
|
|
174
|
+
result = await client.call_tool("dynamic_added_later")
|
|
175
|
+
print("Result of calling dynamic_added_later:", result[0].text)
|
|
176
|
+
# Expected: Tool Added Dynamically!
|
|
177
|
+
|
|
178
|
+
if __name__ == "__main__":
|
|
179
|
+
# Need async context to test
|
|
180
|
+
asyncio.run(test_dynamic_mount())
|
|
181
|
+
# To run the server itself:
|
|
182
|
+
# main_mcp.run()
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### How Mounting Works
|
|
186
|
+
|
|
187
|
+
When you call `main_mcp.mount(prefix, server)`:
|
|
188
|
+
|
|
189
|
+
1. **Live Link**: A live connection is established between `main_mcp` and the `subserver`.
|
|
190
|
+
2. **Dynamic Updates**: Changes made to the `subserver` (e.g., adding new tools) **will be reflected** immediately when accessing components through `main_mcp`.
|
|
191
|
+
3. **Lifespan Management**: The `subserver`'s `lifespan` context **is automatically managed** and executed within the `main_mcp`'s lifespan.
|
|
192
|
+
4. **Delegation**: Requests for components matching the prefix are delegated to the subserver at runtime.
|
|
193
|
+
|
|
194
|
+
The same prefixing rules apply as with `import_server` for naming tools, resources, templates, and prompts.
|
|
195
|
+
|
|
196
|
+
### Customizing Separators
|
|
197
|
+
|
|
198
|
+
Similar to `import_server`, you can customize the separators for the prefixed names and URIs:
|
|
199
|
+
|
|
200
|
+
```python
|
|
201
|
+
main_mcp.mount(
|
|
202
|
+
prefix="api",
|
|
203
|
+
app=some_subserver,
|
|
204
|
+
tool_separator="/", # Tool name becomes: "api/sub_tool_name"
|
|
205
|
+
resource_separator=":", # Resource URI becomes: "api:data://sub_resource"
|
|
206
|
+
prompt_separator="." # Prompt name becomes: "api.sub_prompt_name"
|
|
207
|
+
)
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
## Example: Modular Application
|
|
212
|
+
|
|
213
|
+
Here's how a modular application might use `import_server`:
|
|
214
|
+
|
|
215
|
+
<CodeGroup>
|
|
216
|
+
```python main.py
|
|
217
|
+
from fastmcp import FastMCP
|
|
218
|
+
import asyncio
|
|
219
|
+
|
|
220
|
+
# Import the servers (see other files)
|
|
221
|
+
from modules.text_server import text_mcp
|
|
222
|
+
from modules.data_server import data_mcp
|
|
223
|
+
|
|
224
|
+
app = FastMCP(name="MainApplication")
|
|
225
|
+
|
|
226
|
+
# Setup function for async imports
|
|
227
|
+
async def setup():
|
|
228
|
+
# Import the utility servers
|
|
229
|
+
await app.import_server("text", text_mcp)
|
|
230
|
+
await app.import_server("data", data_mcp)
|
|
231
|
+
|
|
232
|
+
@app.tool()
|
|
233
|
+
def process_and_analyze(record_id: int) -> str:
|
|
234
|
+
"""Fetches a record and analyzes its string representation."""
|
|
235
|
+
# In a real application, you'd use proper methods to interact between
|
|
236
|
+
# imported tools rather than accessing internal managers
|
|
237
|
+
|
|
238
|
+
# Get record data
|
|
239
|
+
record = {"id": record_id, "value": random.random()}
|
|
240
|
+
|
|
241
|
+
# Count words in the record string representation
|
|
242
|
+
word_count = len(str(record).split())
|
|
243
|
+
|
|
244
|
+
return (
|
|
245
|
+
f"Record {record_id} has {word_count} words in its string "
|
|
246
|
+
f"representation."
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
if __name__ == "__main__":
|
|
250
|
+
# Run async setup before starting the server
|
|
251
|
+
asyncio.run(setup())
|
|
252
|
+
# Run the server
|
|
253
|
+
app.run()
|
|
254
|
+
```
|
|
255
|
+
```python modules/text_server.py
|
|
256
|
+
from fastmcp import FastMCP
|
|
257
|
+
|
|
258
|
+
text_mcp = FastMCP(name="TextUtilities")
|
|
259
|
+
|
|
260
|
+
@text_mcp.tool()
|
|
261
|
+
def count_words(text: str) -> int:
|
|
262
|
+
"""Counts words in a text."""
|
|
263
|
+
return len(text.split())
|
|
264
|
+
|
|
265
|
+
@text_mcp.resource("resource://stopwords")
|
|
266
|
+
def get_stopwords() -> list[str]:
|
|
267
|
+
"""Return a list of common stopwords."""
|
|
268
|
+
return ["the", "a", "is", "in"]
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
```python modules/data_server.py
|
|
272
|
+
from fastmcp import FastMCP
|
|
273
|
+
import random
|
|
274
|
+
from typing import dict
|
|
275
|
+
|
|
276
|
+
data_mcp = FastMCP(name="DataAPI")
|
|
277
|
+
|
|
278
|
+
@data_mcp.tool()
|
|
279
|
+
def fetch_record(record_id: int) -> dict:
|
|
280
|
+
"""Fetches a dummy data record."""
|
|
281
|
+
return {"id": record_id, "value": random.random()}
|
|
282
|
+
|
|
283
|
+
@data_mcp.resource("data://schema/{table}")
|
|
284
|
+
def get_table_schema(table: str) -> dict:
|
|
285
|
+
"""Provides a dummy schema for a table."""
|
|
286
|
+
return {"table": table, "columns": ["id", "value"]}
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
</CodeGroup>
|
|
290
|
+
Now, running `main.py` starts a server that exposes:
|
|
291
|
+
- `text_count_words`
|
|
292
|
+
- `data_fetch_record`
|
|
293
|
+
- `process_and_analyze`
|
|
294
|
+
- `text+resource://stopwords`
|
|
295
|
+
- `data+data://schema/{table}` (template)
|
|
296
|
+
|
|
297
|
+
This pattern promotes code organization and reuse within your FastMCP projects.
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: FastAPI Integration
|
|
3
|
+
sidebarTitle: FastAPI
|
|
4
|
+
description: Generate MCP servers from FastAPI apps
|
|
5
|
+
icon: square-bolt
|
|
6
|
+
---
|
|
7
|
+
import { VersionBadge } from '/snippets/version-badge.mdx'
|
|
8
|
+
|
|
9
|
+
<VersionBadge version="2.0.0" />
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
FastMCP can automatically convert FastAPI applications into MCP servers.
|
|
13
|
+
|
|
14
|
+
<Tip>
|
|
15
|
+
FastMCP does *not* include FastAPI as a dependency; you must install it separately to run these examples.
|
|
16
|
+
</Tip>
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
```python {2, 22, 25}
|
|
20
|
+
from fastapi import FastAPI
|
|
21
|
+
from fastmcp import FastMCP
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# A FastAPI app
|
|
25
|
+
app = FastAPI()
|
|
26
|
+
|
|
27
|
+
@app.get("/items")
|
|
28
|
+
def list_items():
|
|
29
|
+
return [{"id": 1, "name": "Item 1"}, {"id": 2, "name": "Item 2"}]
|
|
30
|
+
|
|
31
|
+
@app.get("/items/{item_id}")
|
|
32
|
+
def get_item(item_id: int):
|
|
33
|
+
return {"id": item_id, "name": f"Item {item_id}"}
|
|
34
|
+
|
|
35
|
+
@app.post("/items")
|
|
36
|
+
def create_item(name: str):
|
|
37
|
+
return {"id": 3, "name": name}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
# Create an MCP server from your FastAPI app
|
|
41
|
+
mcp = FastMCP.from_fastapi(app=app)
|
|
42
|
+
|
|
43
|
+
if __name__ == "__main__":
|
|
44
|
+
mcp.run() # Start the MCP server
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Route Mapping
|
|
48
|
+
|
|
49
|
+
By default, FastMCP will map FastAPI routes to MCP components according to the following rules:
|
|
50
|
+
|
|
51
|
+
| FastAPI Route Type | FastAPI Example | MCP Component | Notes |
|
|
52
|
+
|--------------------|--------------|---------|-------|
|
|
53
|
+
| GET without path params | `@app.get("/stats")` | Resource | Simple resources for fetching data |
|
|
54
|
+
| GET with path params | `@app.get("/users/{id}")` | Resource Template | Path parameters become template parameters |
|
|
55
|
+
| POST, PUT, DELETE, etc. | `@app.post("/users")` | Tool | Operations that modify data |
|
|
56
|
+
|
|
57
|
+
For more details on route mapping or custom mapping rules, see the [OpenAPI integration documentation](/patterns/openapi#route-mapping); FastMCP uses the same mapping rules for both FastAPI and OpenAPI integrations.
|
|
58
|
+
|
|
59
|
+
## Complete Example
|
|
60
|
+
|
|
61
|
+
Here's a more detailed example with a data model:
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
import asyncio
|
|
65
|
+
from fastapi import FastAPI, HTTPException
|
|
66
|
+
from pydantic import BaseModel
|
|
67
|
+
from fastmcp import FastMCP, Client
|
|
68
|
+
|
|
69
|
+
# Define your Pydantic model
|
|
70
|
+
class Item(BaseModel):
|
|
71
|
+
name: str
|
|
72
|
+
price: float
|
|
73
|
+
|
|
74
|
+
# Create your FastAPI app
|
|
75
|
+
app = FastAPI()
|
|
76
|
+
items = {} # In-memory database
|
|
77
|
+
|
|
78
|
+
@app.get("/items")
|
|
79
|
+
def list_items():
|
|
80
|
+
"""List all items"""
|
|
81
|
+
return list(items.values())
|
|
82
|
+
|
|
83
|
+
@app.get("/items/{item_id}")
|
|
84
|
+
def get_item(item_id: int):
|
|
85
|
+
"""Get item by ID"""
|
|
86
|
+
if item_id not in items:
|
|
87
|
+
raise HTTPException(404, "Item not found")
|
|
88
|
+
return items[item_id]
|
|
89
|
+
|
|
90
|
+
@app.post("/items")
|
|
91
|
+
def create_item(item: Item):
|
|
92
|
+
"""Create a new item"""
|
|
93
|
+
item_id = len(items) + 1
|
|
94
|
+
items[item_id] = {"id": item_id, **item.model_dump()}
|
|
95
|
+
return items[item_id]
|
|
96
|
+
|
|
97
|
+
# Test your MCP server with a client
|
|
98
|
+
async def test():
|
|
99
|
+
# Create MCP server from FastAPI app
|
|
100
|
+
mcp = await FastMCP.from_fastapi(app=app)
|
|
101
|
+
|
|
102
|
+
# List the components that were created
|
|
103
|
+
tools = await mcp.list_tools()
|
|
104
|
+
resources = await mcp.list_resources()
|
|
105
|
+
templates = await mcp.list_resource_templates()
|
|
106
|
+
|
|
107
|
+
print(f"Generated {len(tools)} tools")
|
|
108
|
+
print(f"Generated {len(resources)} resources")
|
|
109
|
+
print(f"Generated {len(templates)} templates")
|
|
110
|
+
|
|
111
|
+
# In a real scenario, you would run the server:
|
|
112
|
+
# mcp.run()
|
|
113
|
+
|
|
114
|
+
if __name__ == "__main__":
|
|
115
|
+
asyncio.run(test())
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Benefits
|
|
119
|
+
|
|
120
|
+
- **Leverage existing FastAPI apps** - No need to rewrite your API logic
|
|
121
|
+
- **Schema reuse** - FastAPI's Pydantic models and validation are inherited
|
|
122
|
+
- **Full feature support** - Works with FastAPI's authentication, dependencies, etc.
|
|
123
|
+
- **ASGI transport** - Direct communication without additional HTTP overhead
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: OpenAPI Integration
|
|
3
|
+
sidebarTitle: OpenAPI
|
|
4
|
+
description: Generate MCP servers from OpenAPI specs
|
|
5
|
+
icon: code-branch
|
|
6
|
+
---
|
|
7
|
+
import { VersionBadge } from '/snippets/version-badge.mdx'
|
|
8
|
+
|
|
9
|
+
<VersionBadge version="2.0.0" />
|
|
10
|
+
|
|
11
|
+
FastMCP can automatically generate an MCP server from an OpenAPI specification. Users only need to provide an OpenAPI specification (3.0 or 3.1) and an API client.
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
import httpx
|
|
15
|
+
from fastmcp import FastMCP
|
|
16
|
+
|
|
17
|
+
# Create a client for your API
|
|
18
|
+
api_client = httpx.AsyncClient(base_url="https://api.example.com")
|
|
19
|
+
|
|
20
|
+
# Load your OpenAPI spec
|
|
21
|
+
spec = {...}
|
|
22
|
+
|
|
23
|
+
# Create an MCP server from your OpenAPI spec
|
|
24
|
+
mcp = FastMCP.from_openapi(openapi_spec=spec, client=api_client)
|
|
25
|
+
|
|
26
|
+
if __name__ == "__main__":
|
|
27
|
+
mcp.run()
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Route Mapping
|
|
31
|
+
|
|
32
|
+
By default, OpenAPI routes are mapped to MCP components based on these rules:
|
|
33
|
+
|
|
34
|
+
| OpenAPI Route | Example |MCP Component | Notes |
|
|
35
|
+
|- | - | - | - |
|
|
36
|
+
| `GET` without path params | `GET /stats` | Resource | Simple resources for fetching data |
|
|
37
|
+
| `GET` with path params | `GET /users/{id}` | Resource Template | Path parameters become template parameters |
|
|
38
|
+
| `POST`, `PUT`, `PATCH`, `DELETE`, etc. | `POST /users` | Tool | Operations that modify data |
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
Internally, FastMCP uses a priority-ordered set of `RouteMap` objects to determine the component type. Route maps indicate that a specific HTTP method (or methods) and path pattern should be treated as a specific component type. This is the default set of route maps:
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
# Simplified version of the actual mapping rules
|
|
45
|
+
DEFAULT_ROUTE_MAPPINGS = [
|
|
46
|
+
# GET with path parameters -> ResourceTemplate
|
|
47
|
+
RouteMap(methods=["GET"], pattern=r".*\{.*\}.*",
|
|
48
|
+
route_type=RouteType.RESOURCE_TEMPLATE),
|
|
49
|
+
|
|
50
|
+
# GET without path parameters -> Resource
|
|
51
|
+
RouteMap(methods=["GET"], pattern=r".*",
|
|
52
|
+
route_type=RouteType.RESOURCE),
|
|
53
|
+
|
|
54
|
+
# All other methods -> Tool
|
|
55
|
+
RouteMap(methods=["POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"],
|
|
56
|
+
pattern=r".*", route_type=RouteType.TOOL),
|
|
57
|
+
]
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Custom Route Maps
|
|
61
|
+
|
|
62
|
+
Users can add custom route maps to override the default mapping behavior. User-supplied route maps are always applied first, before the default route maps.
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from fastmcp.server.openapi import RouteMap, RouteType
|
|
66
|
+
|
|
67
|
+
# Custom mapping rules
|
|
68
|
+
custom_maps = [
|
|
69
|
+
# Force all analytics endpoints to be Tools
|
|
70
|
+
RouteMap(methods=["GET"],
|
|
71
|
+
pattern=r"^/analytics/.*",
|
|
72
|
+
route_type=RouteType.TOOL)
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
# Apply custom mappings
|
|
76
|
+
mcp = await FastMCP.from_openapi(
|
|
77
|
+
openapi_spec=spec,
|
|
78
|
+
client=api_client,
|
|
79
|
+
route_maps=custom_maps
|
|
80
|
+
)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## How It Works
|
|
84
|
+
|
|
85
|
+
1. FastMCP parses your OpenAPI spec to extract routes and schemas
|
|
86
|
+
2. It applies mapping rules to categorize each route
|
|
87
|
+
3. When an MCP client calls a tool or accesses a resource:
|
|
88
|
+
- FastMCP constructs an HTTP request based on the OpenAPI definition
|
|
89
|
+
- It sends the request through the provided httpx client
|
|
90
|
+
- It translates the HTTP response to the appropriate MCP format
|
|
91
|
+
|
|
92
|
+
## Complete Example
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
import asyncio
|
|
96
|
+
import httpx
|
|
97
|
+
from fastmcp import FastMCP
|
|
98
|
+
|
|
99
|
+
# Sample OpenAPI spec for a Pet Store API
|
|
100
|
+
petstore_spec = {
|
|
101
|
+
"openapi": "3.0.0",
|
|
102
|
+
"paths": {
|
|
103
|
+
"/pets": {
|
|
104
|
+
"get": {
|
|
105
|
+
"operationId": "listPets",
|
|
106
|
+
"summary": "List all pets"
|
|
107
|
+
},
|
|
108
|
+
"post": {
|
|
109
|
+
"operationId": "createPet",
|
|
110
|
+
"summary": "Create a new pet"
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
"/pets/{petId}": {
|
|
114
|
+
"get": {
|
|
115
|
+
"operationId": "getPet",
|
|
116
|
+
"summary": "Get a pet by ID",
|
|
117
|
+
"parameters": [
|
|
118
|
+
{
|
|
119
|
+
"name": "petId",
|
|
120
|
+
"in": "path",
|
|
121
|
+
"required": True,
|
|
122
|
+
"schema": {"type": "string"}
|
|
123
|
+
}
|
|
124
|
+
]
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async def main():
|
|
131
|
+
# Client for the Pet Store API
|
|
132
|
+
client = httpx.AsyncClient(base_url="https://petstore.example.com/api")
|
|
133
|
+
|
|
134
|
+
# Create the MCP server
|
|
135
|
+
mcp = await FastMCP.from_openapi(
|
|
136
|
+
openapi_spec=petstore_spec,
|
|
137
|
+
client=client,
|
|
138
|
+
name="PetStore"
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
# List what components were created
|
|
142
|
+
tools = await mcp.list_tools()
|
|
143
|
+
resources = await mcp.list_resources()
|
|
144
|
+
templates = await mcp.list_resource_templates()
|
|
145
|
+
|
|
146
|
+
print(f"Tools: {len(tools)}") # Should include createPet
|
|
147
|
+
print(f"Resources: {len(resources)}") # Should include listPets
|
|
148
|
+
print(f"Templates: {len(templates)}") # Should include getPet
|
|
149
|
+
|
|
150
|
+
# Start the MCP server
|
|
151
|
+
mcp.run()
|
|
152
|
+
|
|
153
|
+
if __name__ == "__main__":
|
|
154
|
+
asyncio.run(main())
|
|
155
|
+
```
|
|
156
|
+
|