fastmcp 2.1.1__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.1 → fastmcp-2.2.0}/.github/workflows/run-static.yml +12 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/PKG-INFO +20 -7
- {fastmcp-2.1.1 → fastmcp-2.2.0}/README.md +5 -4
- fastmcp-2.1.1/docs/clients/overview.mdx → fastmcp-2.2.0/docs/clients/client.mdx +5 -1
- {fastmcp-2.1.1 → fastmcp-2.2.0}/docs/clients/transports.mdx +1 -1
- {fastmcp-2.1.1 → fastmcp-2.2.0}/docs/docs.json +2 -2
- {fastmcp-2.1.1 → fastmcp-2.2.0}/docs/getting-started/quickstart.mdx +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.1/docs/patterns/proxying.mdx → fastmcp-2.2.0/docs/patterns/proxy.mdx +15 -11
- {fastmcp-2.1.1 → fastmcp-2.2.0}/docs/servers/context.mdx +5 -2
- {fastmcp-2.1.1 → fastmcp-2.2.0}/docs/servers/fastmcp.mdx +72 -85
- {fastmcp-2.1.1 → fastmcp-2.2.0}/docs/servers/prompts.mdx +8 -9
- {fastmcp-2.1.1 → fastmcp-2.2.0}/docs/servers/resources.mdx +146 -34
- {fastmcp-2.1.1 → fastmcp-2.2.0}/docs/servers/tools.mdx +8 -9
- fastmcp-2.2.0/docs/snippets/version-badge.mdx +8 -0
- fastmcp-2.2.0/docs/style.css +53 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/pyproject.toml +33 -7
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/cli/cli.py +32 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/client/client.py +16 -15
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/client/transports.py +28 -7
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/exceptions.py +8 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/prompts/prompt.py +20 -9
- fastmcp-2.2.0/src/fastmcp/prompts/prompt_manager.py +83 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/resources/resource.py +19 -8
- fastmcp-2.2.0/src/fastmcp/resources/resource_manager.py +245 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/resources/template.py +82 -17
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/server/openapi.py +10 -16
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/server/proxy.py +102 -76
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/server/server.py +319 -256
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/settings.py +8 -11
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/tools/tool.py +66 -14
- fastmcp-2.2.0/src/fastmcp/tools/tool_manager.py +98 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/utilities/logging.py +14 -6
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/utilities/openapi.py +0 -87
- fastmcp-2.2.0/tests/cli/test_run.py +77 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/client/test_client.py +109 -0
- {fastmcp-2.1.1 → 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.1 → fastmcp-2.2.0}/tests/resources/test_file_resources.py +0 -4
- {fastmcp-2.1.1 → 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.1 → fastmcp-2.2.0}/tests/resources/test_resources.py +0 -1
- {fastmcp-2.1.1 → 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.1 → fastmcp-2.2.0}/tests/server/test_lifespan.py +0 -2
- fastmcp-2.2.0/tests/server/test_mount.py +429 -0
- fastmcp-2.2.0/tests/server/test_openapi.py +757 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/server/test_proxy.py +64 -40
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/server/test_run_server.py +1 -1
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/server/test_server.py +398 -197
- fastmcp-2.2.0/tests/tools/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/tools/test_tool_manager.py +203 -150
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/utilities/test_func_metadata.py +0 -3
- fastmcp-2.2.0/tests/utilities/test_logging.py +30 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/uv.lock +8 -4
- fastmcp-2.1.1/docs/patterns/composition.mdx +0 -186
- fastmcp-2.1.1/docs/patterns/fastapi.mdx +0 -114
- fastmcp-2.1.1/docs/patterns/openapi.mdx +0 -174
- fastmcp-2.1.1/docs/servers/resources_backup.mdx +0 -270
- fastmcp-2.1.1/docs/style.css +0 -13
- fastmcp-2.1.1/src/fastmcp/prompts/prompt_manager.py +0 -91
- fastmcp-2.1.1/src/fastmcp/resources/resource_manager.py +0 -277
- fastmcp-2.1.1/src/fastmcp/tools/tool_manager.py +0 -96
- fastmcp-2.1.1/tests/prompts/test_prompt_manager.py +0 -344
- fastmcp-2.1.1/tests/resources/test_resource_manager.py +0 -558
- fastmcp-2.1.1/tests/resources/test_resource_template.py +0 -188
- fastmcp-2.1.1/tests/server/test_mount.py +0 -232
- fastmcp-2.1.1/tests/server/test_openapi.py +0 -355
- {fastmcp-2.1.1 → fastmcp-2.2.0}/.cursor/rules/core-mcp-objects.mdc +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/.github/release.yml +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/.github/workflows/publish.yml +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/.github/workflows/run-tests.yml +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/.gitignore +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/.pre-commit-config.yaml +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/LICENSE +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/Windows_Notes.md +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/docs/assets/demo-inspector.png +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/docs/getting-started/installation.mdx +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/docs/getting-started/welcome.mdx +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/docs/patterns/decorating-methods.mdx +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/complex_inputs.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/desktop.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/echo.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/memory.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/mount_example.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/readme-quickstart.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/sampling.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/screenshot.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/simple_echo.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/smart_home/README.md +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/smart_home/pyproject.toml +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/__main__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/hub.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/lights/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/lights/hue_utils.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/lights/server.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/py.typed +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/smart_home/src/smart_home/settings.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/smart_home/uv.lock +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/examples/text_me.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/justfile +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/cli/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/cli/claude.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/client/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/client/base.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/client/roots.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/client/sampling.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/prompts/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/py.typed +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/resources/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/resources/types.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/server/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/server/context.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/tools/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/utilities/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/utilities/decorators.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/utilities/func_metadata.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/src/fastmcp/utilities/types.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/client/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/client/test_roots.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/client/test_sampling.py +0 -0
- /fastmcp-2.1.1/tests/prompts/__init__.py → /fastmcp-2.2.0/tests/conftest.py +0 -0
- {fastmcp-2.1.1/tests/resources → fastmcp-2.2.0/tests/prompts}/__init__.py +0 -0
- {fastmcp-2.1.1/tests/server → fastmcp-2.2.0/tests/resources}/__init__.py +0 -0
- {fastmcp-2.1.1/tests/tools → fastmcp-2.2.0/tests/server}/__init__.py +0 -0
- {fastmcp-2.1.1/tests/server → fastmcp-2.2.0/tests}/test_servers/fastmcp_server.py +0 -0
- {fastmcp-2.1.1/tests/server → fastmcp-2.2.0/tests}/test_servers/sse.py +0 -0
- {fastmcp-2.1.1/tests/server → fastmcp-2.2.0/tests}/test_servers/stdio.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/utilities/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/utilities/openapi/__init__.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/utilities/openapi/conftest.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/utilities/openapi/test_openapi.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/utilities/openapi/test_openapi_advanced.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/utilities/openapi/test_openapi_fastapi.py +0 -0
- {fastmcp-2.1.1 → fastmcp-2.2.0}/tests/utilities/test_decorated_function.py +0 -0
|
@@ -8,7 +8,19 @@ env:
|
|
|
8
8
|
on:
|
|
9
9
|
push:
|
|
10
10
|
branches: ["main"]
|
|
11
|
+
paths:
|
|
12
|
+
- "src/**"
|
|
13
|
+
- "tests/**"
|
|
14
|
+
- "uv.lock"
|
|
15
|
+
- "pyproject.toml"
|
|
16
|
+
- ".github/workflows/**"
|
|
11
17
|
pull_request:
|
|
18
|
+
paths:
|
|
19
|
+
- "src/**"
|
|
20
|
+
- "tests/**"
|
|
21
|
+
- "uv.lock"
|
|
22
|
+
- "pyproject.toml"
|
|
23
|
+
- ".github/workflows/**"
|
|
12
24
|
workflow_dispatch:
|
|
13
25
|
|
|
14
26
|
permissions:
|
|
@@ -1,13 +1,25 @@
|
|
|
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
|
+
Project-URL: Homepage, https://gofastmcp.com
|
|
6
|
+
Project-URL: Repository, https://github.com/jlowin/fastmcp
|
|
7
|
+
Project-URL: Documentation, https://gofastmcp.com
|
|
5
8
|
Author: Jeremiah Lowin
|
|
6
|
-
License: Apache-2.0
|
|
9
|
+
License-Expression: Apache-2.0
|
|
7
10
|
License-File: LICENSE
|
|
11
|
+
Keywords: agent,fastmcp,llm,mcp,mcp client,mcp server,model context protocol
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
18
|
+
Classifier: Typing :: Typed
|
|
8
19
|
Requires-Python: >=3.10
|
|
9
20
|
Requires-Dist: dotenv>=0.9.9
|
|
10
|
-
Requires-Dist:
|
|
21
|
+
Requires-Dist: exceptiongroup>=1.2.2
|
|
22
|
+
Requires-Dist: httpx>=0.28.1
|
|
11
23
|
Requires-Dist: mcp<2.0.0,>=1.6.0
|
|
12
24
|
Requires-Dist: openapi-pydantic>=0.5.1
|
|
13
25
|
Requires-Dist: rich>=13.9.4
|
|
@@ -26,6 +38,7 @@ Description-Content-Type: text/markdown
|
|
|
26
38
|
[](https://github.com/jlowin/fastmcp/actions/workflows/run-tests.yml)
|
|
27
39
|
[](https://github.com/jlowin/fastmcp/blob/main/LICENSE)
|
|
28
40
|
|
|
41
|
+
<a href="https://trendshift.io/repositories/13266" target="_blank"><img src="https://trendshift.io/api/badge/repositories/13266" alt="jlowin%2Ffastmcp | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
|
29
42
|
</div>
|
|
30
43
|
|
|
31
44
|
The [Model Context Protocol (MCP)](https://modelcontextprotocol.io) is a new, standardized way to provide context and tools to your LLMs, and FastMCP makes building MCP servers and clients simple and intuitive. Create tools, expose resources, define prompts, and connect components with clean, Pythonic code.
|
|
@@ -109,7 +122,7 @@ FastMCP provides a high-level, Pythonic interface for building and interacting w
|
|
|
109
122
|
|
|
110
123
|
## Why FastMCP?
|
|
111
124
|
|
|
112
|
-
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.
|
|
113
126
|
|
|
114
127
|
FastMCP aims to be:
|
|
115
128
|
|
|
@@ -568,13 +581,13 @@ proxy_client = Client(
|
|
|
568
581
|
)
|
|
569
582
|
|
|
570
583
|
# Create a proxy server that connects to the client and exposes its capabilities
|
|
571
|
-
proxy = FastMCP.
|
|
584
|
+
proxy = FastMCP.from_client(proxy_client, name="Stdio-to-SSE Proxy")
|
|
572
585
|
|
|
573
586
|
if __name__ == "__main__":
|
|
574
587
|
proxy.run(transport='sse')
|
|
575
588
|
```
|
|
576
589
|
|
|
577
|
-
`FastMCP.
|
|
590
|
+
`FastMCP.from_client` is a class method that connects to the target, discovers its capabilities, and dynamically builds the proxy server instance.
|
|
578
591
|
|
|
579
592
|
|
|
580
593
|
|
|
@@ -787,4 +800,4 @@ We use `ruff` via `pre-commit`.
|
|
|
787
800
|
|
|
788
801
|
Please open an issue or discussion for questions or suggestions!
|
|
789
802
|
|
|
790
|
-
</details>
|
|
803
|
+
</details>
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
[](https://github.com/jlowin/fastmcp/actions/workflows/run-tests.yml)
|
|
10
10
|
[](https://github.com/jlowin/fastmcp/blob/main/LICENSE)
|
|
11
11
|
|
|
12
|
+
<a href="https://trendshift.io/repositories/13266" target="_blank"><img src="https://trendshift.io/api/badge/repositories/13266" alt="jlowin%2Ffastmcp | Trendshift" style="width: 250px; height: 55px;" width="250" height="55"/></a>
|
|
12
13
|
</div>
|
|
13
14
|
|
|
14
15
|
The [Model Context Protocol (MCP)](https://modelcontextprotocol.io) is a new, standardized way to provide context and tools to your LLMs, and FastMCP makes building MCP servers and clients simple and intuitive. Create tools, expose resources, define prompts, and connect components with clean, Pythonic code.
|
|
@@ -92,7 +93,7 @@ FastMCP provides a high-level, Pythonic interface for building and interacting w
|
|
|
92
93
|
|
|
93
94
|
## Why FastMCP?
|
|
94
95
|
|
|
95
|
-
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.
|
|
96
97
|
|
|
97
98
|
FastMCP aims to be:
|
|
98
99
|
|
|
@@ -551,13 +552,13 @@ proxy_client = Client(
|
|
|
551
552
|
)
|
|
552
553
|
|
|
553
554
|
# Create a proxy server that connects to the client and exposes its capabilities
|
|
554
|
-
proxy = FastMCP.
|
|
555
|
+
proxy = FastMCP.from_client(proxy_client, name="Stdio-to-SSE Proxy")
|
|
555
556
|
|
|
556
557
|
if __name__ == "__main__":
|
|
557
558
|
proxy.run(transport='sse')
|
|
558
559
|
```
|
|
559
560
|
|
|
560
|
-
`FastMCP.
|
|
561
|
+
`FastMCP.from_client` is a class method that connects to the target, discovers its capabilities, and dynamically builds the proxy server instance.
|
|
561
562
|
|
|
562
563
|
|
|
563
564
|
|
|
@@ -770,4 +771,4 @@ We use `ruff` via `pre-commit`.
|
|
|
770
771
|
|
|
771
772
|
Please open an issue or discussion for questions or suggestions!
|
|
772
773
|
|
|
773
|
-
</details>
|
|
774
|
+
</details>
|
|
@@ -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",
|
|
@@ -5,7 +5,7 @@ icon: rocket
|
|
|
5
5
|
|
|
6
6
|
Welcome! This guide will help you quickly set up FastMCP and run your first MCP server.
|
|
7
7
|
|
|
8
|
-
If you haven't already installed FastMCP, follow the [installation instructions](/
|
|
8
|
+
If you haven't already installed FastMCP, follow the [installation instructions](/getting-started/installation).
|
|
9
9
|
|
|
10
10
|
## Creating a FastMCP Server
|
|
11
11
|
|
|
@@ -123,5 +123,5 @@ fastmcp run my_server.py:mcp
|
|
|
123
123
|
Note that FastMCP *does not* require the `__main__` block in the server file, and will ignore it if it is present. Instead, it looks for the server object provided in the CLI command (here, `mcp`). If no server object is provided, `fastmcp run` will automatically search for servers called "mcp", "app", or "server" in the file.
|
|
124
124
|
|
|
125
125
|
<Tip>
|
|
126
|
-
We pointed our client at the server file, which is recognized as a Python MCP server and executed with `python my_server.py` by default. This exceutes the `__main__` block of the server file. There are other ways to run the server, which are described in the [server configuration](/
|
|
126
|
+
We pointed our client at the server file, which is recognized as a Python MCP server and executed with `python my_server.py` by default. This exceutes the `__main__` block of the server file. There are other ways to run the server, which are described in the [server configuration](/servers/fastmcp#running-the-server) guide.
|
|
127
127
|
</Tip>
|
|
@@ -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
|