Skip to content

Commit 91fba14

Browse files
authored
Merge pull request #185 from jlowin/docs
Improve documentation
2 parents 08c40e7 + 28315a0 commit 91fba14

File tree

11 files changed

+151
-194
lines changed

11 files changed

+151
-194
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ description: Learn how to use the FastMCP Client to interact with MCP servers.
55
icon: user-robot
66
---
77

8+
import { VersionBadge } from '/snippets/version-badge.mdx'
9+
10+
<VersionBadge version="2.0.0" />
11+
812
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.
913

1014
## FastMCP Client

docs/clients/transports.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ icon: link
77

88
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.
99

10-
While the `Client` often infers the correct transport automatically (see [Client Overview](/clients/overview#transport-inference)), you can also instantiate transports explicitly for more control.
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.
1111

1212

1313
## Stdio Transports

docs/docs.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@
5050
{
5151
"group": "Clients",
5252
"pages": [
53-
"clients/overview",
53+
"clients/client",
5454
"clients/transports"
5555
]
5656
},
5757
{
5858
"group": "Patterns",
5959
"pages": [
60-
"patterns/proxying",
60+
"patterns/proxy",
6161
"patterns/composition",
6262
"patterns/decorating-methods",
6363
"patterns/openapi",

docs/patterns/composition.mdx

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ sidebarTitle: Composition
44
description: Combine multiple FastMCP servers into a single, larger application using mounting and importing.
55
icon: puzzle-piece
66
---
7+
import { VersionBadge } from '/snippets/version-badge.mdx'
8+
9+
<VersionBadge version="2.2.0" />
710

811
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:
912

@@ -17,13 +20,31 @@ As your MCP applications grow, you might want to organize your tools, resources,
1720
- **Teamwork**: Different teams can work on separate FastMCP servers that are later combined.
1821
- **Organization**: Keep related functionality grouped together logically.
1922

20-
## Importing Subservers (Static Composition)
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)
2143

2244
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.
2345

2446
```python
2547
from fastmcp import FastMCP
26-
from typing import dict, list
2748
import asyncio
2849

2950
# --- Define Subservers ---
@@ -114,7 +135,7 @@ await main_mcp.import_server(
114135
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.
115136
</Warning>
116137

117-
## Mounting Subservers (Live Linking)
138+
## Mounting (Live Linking)
118139

119140
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.
120141

@@ -186,61 +207,19 @@ main_mcp.mount(
186207
)
187208
```
188209

189-
## Comparing Import and Mount
190-
191-
| Feature | `import_server` | `mount` |
192-
|---------|----------------|---------|
193-
| **Synchronicity** | Async (must be awaited) | Sync |
194-
| **Composition Type** | One-time copy (static) | Live link (dynamic) |
195-
| **Updates** | Changes to subserver NOT reflected | Changes to subserver immediately reflected |
196-
| **Lifespan** | Not managed | Automatically managed |
197-
| **Best For** | Bundling finalized components | Modular runtime composition |
198210

199211
## Example: Modular Application
200212

201213
Here's how a modular application might use `import_server`:
202214

203-
```python
204-
# modules/text_utils.py
205-
from fastmcp import FastMCP
206-
from typing import list
207-
208-
text_mcp = FastMCP(name="TextUtilities")
209-
210-
@text_mcp.tool()
211-
def count_words(text: str) -> int:
212-
"""Counts words in a text."""
213-
return len(text.split())
214-
215-
@text_mcp.resource("resource://stopwords")
216-
def get_stopwords() -> list[str]:
217-
"""Return a list of common stopwords."""
218-
return ["the", "a", "is", "in"]
219-
220-
# ------------------------------
221-
# modules/data_api.py
222-
from fastmcp import FastMCP
223-
import random
224-
from typing import dict
225-
226-
data_mcp = FastMCP(name="DataAPI")
227-
228-
@data_mcp.tool()
229-
def fetch_record(record_id: int) -> dict:
230-
"""Fetches a dummy data record."""
231-
return {"id": record_id, "value": random.random()}
232-
233-
@data_mcp.resource("data://schema/{table}")
234-
def get_table_schema(table: str) -> dict:
235-
"""Provides a dummy schema for a table."""
236-
return {"table": table, "columns": ["id", "value"]}
237-
238-
# ------------------------------
239-
# main_app.py
215+
<CodeGroup>
216+
```python main.py
240217
from fastmcp import FastMCP
241218
import asyncio
242-
from modules.text_utils import text_mcp # Import server instances
243-
from modules.data_api import data_mcp
219+
220+
# Import the servers (see other files)
221+
from modules.text_server import text_mcp
222+
from modules.data_server import data_mcp
244223

245224
app = FastMCP(name="MainApplication")
246225

@@ -273,8 +252,42 @@ if __name__ == "__main__":
273252
# Run the server
274253
app.run()
275254
```
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+
```
276288

277-
Now, running `main_app.py` starts a server that exposes:
289+
</CodeGroup>
290+
Now, running `main.py` starts a server that exposes:
278291
- `text_count_words`
279292
- `data_fetch_record`
280293
- `process_and_analyze`

docs/patterns/fastapi.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ sidebarTitle: FastAPI
44
description: Generate MCP servers from FastAPI apps
55
icon: square-bolt
66
---
7+
import { VersionBadge } from '/snippets/version-badge.mdx'
8+
9+
<VersionBadge version="2.0.0" />
710

811

912
FastMCP can automatically convert FastAPI applications into MCP servers.

docs/patterns/openapi.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ sidebarTitle: OpenAPI
44
description: Generate MCP servers from OpenAPI specs
55
icon: code-branch
66
---
7+
import { VersionBadge } from '/snippets/version-badge.mdx'
8+
9+
<VersionBadge version="2.0.0" />
710

811
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.
912

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ sidebarTitle: Proxying
44
description: Use FastMCP to act as an intermediary or change transport for other MCP servers.
55
icon: arrows-retweet
66
---
7+
import { VersionBadge } from '/snippets/version-badge.mdx'
8+
9+
<VersionBadge version="2.0.0" />
710

811
FastMCP provides a powerful proxying capability that allows one FastMCP server instance to act as a frontend for another MCP server (which could be remote, running on a different transport, or even another FastMCP instance). This is achieved using the `FastMCP.from_client()` class method.
912

docs/servers/context.mdx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ sidebarTitle: Context
44
description: Access MCP capabilities like logging, progress, and resources within your tools.
55
icon: rectangle-code
66
---
7+
import { VersionBadge } from '/snippets/version-badge.mdx'
78

89
When defining FastMCP [tools](/servers/tools), your functions might need to interact with the underlying MCP session or access server capabilities. FastMCP provides the `Context` object for this purpose.
910

@@ -174,6 +175,8 @@ The returned content is typically accessed via `content_list[0].content` and can
174175

175176
### LLM Sampling
176177

178+
<VersionBadge version="2.0.0" />
179+
177180
Request the client's LLM to generate text based on provided messages. This is useful when your tool needs to leverage the LLM's capabilities to process data or generate responses.
178181

179182
```python
@@ -227,7 +230,7 @@ async def generate_example(concept: str, ctx: Context) -> str:
227230
return f"```python\n{code_example}\n```"
228231
```
229232

230-
See [Client Sampling](/clients/overview#llm-sampling) for more details on how clients handle these requests.
233+
See [Client Sampling](/clients/client#llm-sampling) for more details on how clients handle these requests.
231234

232235
### Request Information
233236

0 commit comments

Comments
 (0)