Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
0a9ed5a
1
Parent(s): 2d2e845
Update transport docs
Browse files- README.md +33 -43
- docs/clients/transports.mdx +64 -65
README.md
CHANGED
|
@@ -273,50 +273,40 @@ Learn more: [**OpenAPI Integration**](https://gofastmcp.com/patterns/openapi) |
|
|
| 273 |
|
| 274 |
## Running Your Server
|
| 275 |
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
1. **Development (`fastmcp dev`)**: Recommended for building and testing. Provides an interactive testing environment with the MCP Inspector.
|
| 279 |
-
```bash
|
| 280 |
-
fastmcp dev server.py
|
| 281 |
-
# Optionally add temporary dependencies
|
| 282 |
-
fastmcp dev server.py --with pandas numpy
|
| 283 |
-
```
|
| 284 |
-
|
| 285 |
-
2. **FastMCP CLI**: Run your server with the FastMCP CLI. This can autodetect and load your server object and run it with any transport configuration you want.
|
| 286 |
-
```bash
|
| 287 |
-
fastmcp run path/to/server.py:server_object
|
| 288 |
-
|
| 289 |
-
# Run as SSE on port 4200
|
| 290 |
-
fastmcp run path/to/server.py:server_object --transport sse --port 4200
|
| 291 |
-
```
|
| 292 |
-
FastMCP will auto-detect the server object if it's named `mcp`, `app`, or `server`. In these cases, you can omit the `:server_object` part unless you need to select a specific object.
|
| 293 |
-
|
| 294 |
-
3. **Direct Execution**: For maximum compatibility with the MCP ecosystem, you can run your server directly as part of a Python script. You will typically do this within an `if __name__ == "__main__":` block in your script:
|
| 295 |
-
```python
|
| 296 |
-
# Add this to server.py
|
| 297 |
-
if __name__ == "__main__":
|
| 298 |
-
# Default: runs stdio transport
|
| 299 |
-
mcp.run()
|
| 300 |
-
|
| 301 |
-
# Example: Run with SSE transport on a specific port
|
| 302 |
-
mcp.run(transport="sse", host="127.0.0.1", port=9000)
|
| 303 |
-
```
|
| 304 |
-
Run your script:
|
| 305 |
-
```bash
|
| 306 |
-
python server.py
|
| 307 |
-
# or using uv to manage the environment
|
| 308 |
-
uv run python server.py
|
| 309 |
-
```
|
| 310 |
-
4. **Claude Desktop Integration (`fastmcp install`)**: The easiest way to make your server persistently available in the Claude Desktop app. It handles creating an isolated environment using `uv`.
|
| 311 |
-
```bash
|
| 312 |
-
fastmcp install server.py --name "My Analysis Tool"
|
| 313 |
-
# Optionally add dependencies and environment variables
|
| 314 |
-
fastmcp install server.py --with requests -v API_KEY=123 -f .env
|
| 315 |
-
```
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
See the [**Server Documentation**](https://gofastmcp.com/servers/fastmcp#running-the-server) for more details on transports and configuration.
|
| 319 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
## Contributing
|
| 322 |
|
|
|
|
| 273 |
|
| 274 |
## Running Your Server
|
| 275 |
|
| 276 |
+
The main way to run a FastMCP server is by calling the `run()` method on your server instance:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
|
| 278 |
+
```python
|
| 279 |
+
# server.py
|
| 280 |
+
from fastmcp import FastMCP
|
| 281 |
+
|
| 282 |
+
mcp = FastMCP("Demo 🚀")
|
| 283 |
+
|
| 284 |
+
@mcp.tool()
|
| 285 |
+
def hello(name: str) -> str:
|
| 286 |
+
return f"Hello, {name}!"
|
| 287 |
+
|
| 288 |
+
if __name__ == "__main__":
|
| 289 |
+
mcp.run() # Default: uses STDIO transport
|
| 290 |
+
```
|
| 291 |
+
|
| 292 |
+
FastMCP supports three transport protocols:
|
| 293 |
+
|
| 294 |
+
**STDIO (Default)**: Best for local tools and command-line scripts.
|
| 295 |
+
```python
|
| 296 |
+
mcp.run(transport="stdio") # Default, so transport argument is optional
|
| 297 |
+
```
|
| 298 |
+
|
| 299 |
+
**Streamable HTTP**: Recommended for web deployments.
|
| 300 |
+
```python
|
| 301 |
+
mcp.run(transport="streamable-http", host="127.0.0.1", port=8000, path="/mcp")
|
| 302 |
+
```
|
| 303 |
+
|
| 304 |
+
**SSE**: For compatibility with existing SSE clients.
|
| 305 |
+
```python
|
| 306 |
+
mcp.run(transport="sse", host="127.0.0.1", port=8000)
|
| 307 |
+
```
|
| 308 |
+
|
| 309 |
+
See the [**Running Server Documentation**](https://gofastmcp.com/deployment/running-server) for more details.
|
| 310 |
|
| 311 |
## Contributing
|
| 312 |
|
docs/clients/transports.mdx
CHANGED
|
@@ -14,6 +14,69 @@ The FastMCP `Client` relies on a `ClientTransport` object to handle the specific
|
|
| 14 |
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.
|
| 15 |
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
## Stdio Transports
|
| 18 |
|
| 19 |
These transports manage an MCP server running as a subprocess, communicating with it via standard input (stdin) and standard output (stdout). This is the standard mechanism used by clients like Claude Desktop.
|
|
@@ -133,70 +196,6 @@ client = Client(transport)
|
|
| 133 |
# async with client:
|
| 134 |
# response = await client.call_tool("get_npm_data", {})
|
| 135 |
```
|
| 136 |
-
## Network Transports
|
| 137 |
-
|
| 138 |
-
These transports connect to servers running over a network, typically long-running services accessible via URLs.
|
| 139 |
-
|
| 140 |
-
### SSE (Server-Sent Events)
|
| 141 |
-
|
| 142 |
-
* **Class:** `fastmcp.client.transports.SSETransport`
|
| 143 |
-
* **Inferred From:** `http://` or `https://` URLs
|
| 144 |
-
* **Use Case:** Connecting to persistent MCP servers exposed over HTTP/S, often using FastMCP's `mcp.run(transport="sse")` mode.
|
| 145 |
-
|
| 146 |
-
SSE is a simple, unidirectional protocol where the server pushes messages to the client over a standard HTTP connection.
|
| 147 |
-
|
| 148 |
-
```python
|
| 149 |
-
from fastmcp import Client
|
| 150 |
-
from fastmcp.client.transports import SSETransport
|
| 151 |
-
|
| 152 |
-
sse_url = "http://localhost:8000/sse"
|
| 153 |
-
|
| 154 |
-
# Option 1: Inferred transport
|
| 155 |
-
client_inferred = Client(sse_url)
|
| 156 |
-
|
| 157 |
-
# Option 2: Explicit transport (e.g., to add custom headers)
|
| 158 |
-
headers = {"Authorization": "Bearer mytoken"}
|
| 159 |
-
transport_explicit = SSETransport(url=sse_url, headers=headers)
|
| 160 |
-
client_explicit = Client(transport_explicit)
|
| 161 |
-
|
| 162 |
-
async def use_sse_client(client):
|
| 163 |
-
async with client:
|
| 164 |
-
tools = await client.list_tools()
|
| 165 |
-
print(f"Connected via SSE, found tools: {tools}")
|
| 166 |
-
|
| 167 |
-
# asyncio.run(use_sse_client(client_inferred))
|
| 168 |
-
# asyncio.run(use_sse_client(client_explicit))
|
| 169 |
-
```
|
| 170 |
-
|
| 171 |
-
### WebSocket
|
| 172 |
-
|
| 173 |
-
* **Class:** `fastmcp.client.transports.WSTransport`
|
| 174 |
-
* **Inferred From:** `ws://` or `wss://` URLs
|
| 175 |
-
* **Use Case:** Connecting to MCP servers using the WebSocket protocol for bidirectional communication.
|
| 176 |
-
|
| 177 |
-
WebSockets provide a persistent, full-duplex connection between client and server.
|
| 178 |
-
|
| 179 |
-
```python
|
| 180 |
-
from fastmcp import Client
|
| 181 |
-
from fastmcp.client.transports import WSTransport
|
| 182 |
-
|
| 183 |
-
ws_url = "ws://localhost:9000"
|
| 184 |
-
|
| 185 |
-
# Option 1: Inferred transport
|
| 186 |
-
client_inferred = Client(ws_url)
|
| 187 |
-
|
| 188 |
-
# Option 2: Explicit transport
|
| 189 |
-
transport_explicit = WSTransport(url=ws_url)
|
| 190 |
-
client_explicit = Client(transport_explicit)
|
| 191 |
-
|
| 192 |
-
async def use_ws_client(client):
|
| 193 |
-
async with client:
|
| 194 |
-
tools = await client.list_tools()
|
| 195 |
-
print(f"Connected via WebSocket, found tools: {tools}")
|
| 196 |
-
|
| 197 |
-
# asyncio.run(use_ws_client(client_inferred))
|
| 198 |
-
# asyncio.run(use_ws_client(client_explicit))
|
| 199 |
-
```
|
| 200 |
|
| 201 |
## In-Memory Transports
|
| 202 |
|
|
@@ -240,6 +239,6 @@ Communication happens through efficient in-memory queues, making it very fast.
|
|
| 240 |
## Choosing a Transport
|
| 241 |
|
| 242 |
* **Local Development/Testing:** Use `PythonStdioTransport` (inferred from `.py` files) or `FastMCPTransport` (for same-process testing).
|
| 243 |
-
* **Connecting to Remote/Persistent Servers:** Use `
|
| 244 |
* **Running Packaged Tools:** Use `UvxStdioTransport` (Python/uv) or `NpxStdioTransport` (Node/npm) if you need to run MCP servers without local installation.
|
| 245 |
* **Integrating with Claude Desktop (or similar):** These tools typically expect to run a Python script, so your server should be runnable via `python your_server.py`, making `PythonStdioTransport` the relevant mechanism on the client side.
|
|
|
|
| 14 |
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.
|
| 15 |
|
| 16 |
|
| 17 |
+
## Network Transports
|
| 18 |
+
|
| 19 |
+
These transports connect to servers running over a network, typically long-running services accessible via URLs.
|
| 20 |
+
|
| 21 |
+
### Streamable HTTP
|
| 22 |
+
|
| 23 |
+
<VersionBadge version="2.3.0" />
|
| 24 |
+
|
| 25 |
+
* **Class:** `fastmcp.client.transports.StreamableHttpTransport`
|
| 26 |
+
* **Inferred From:** `http://` or `https://` URLs (default for HTTP URLs as of v2.3.0)
|
| 27 |
+
* **Use Case:** Connecting to persistent MCP servers exposed over HTTP/S using FastMCP's `mcp.run(transport="streamable-http")` mode.
|
| 28 |
+
|
| 29 |
+
Streamable HTTP is the recommended transport for web-based deployments, providing efficient bidirectional communication over HTTP.
|
| 30 |
+
|
| 31 |
+
```python
|
| 32 |
+
from fastmcp import Client
|
| 33 |
+
from fastmcp.client.transports import StreamableHttpTransport
|
| 34 |
+
|
| 35 |
+
http_url = "http://localhost:8000/mcp"
|
| 36 |
+
|
| 37 |
+
# Option 1: Inferred transport (default for HTTP URLs)
|
| 38 |
+
client_inferred = Client(http_url)
|
| 39 |
+
|
| 40 |
+
# Option 2: Explicit transport (e.g., to add custom headers)
|
| 41 |
+
headers = {"Authorization": "Bearer mytoken"}
|
| 42 |
+
transport_explicit = StreamableHttpTransport(url=http_url, headers=headers)
|
| 43 |
+
client_explicit = Client(transport_explicit)
|
| 44 |
+
|
| 45 |
+
async def use_streamable_http_client(client):
|
| 46 |
+
async with client:
|
| 47 |
+
tools = await client.list_tools()
|
| 48 |
+
print(f"Connected via Streamable HTTP, found tools: {tools}")
|
| 49 |
+
|
| 50 |
+
# asyncio.run(use_streamable_http_client(client_inferred))
|
| 51 |
+
# asyncio.run(use_streamable_http_client(client_explicit))
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
### SSE (Server-Sent Events)
|
| 55 |
+
|
| 56 |
+
* **Class:** `fastmcp.client.transports.SSETransport`
|
| 57 |
+
* **Inferred From:** Not automatically inferred for most HTTP URLs (as of v2.3.0)
|
| 58 |
+
* **Use Case:** Connecting to MCP servers using Server-Sent Events, often using FastMCP's `mcp.run(transport="sse")` mode.
|
| 59 |
+
|
| 60 |
+
While SSE is still supported, Streamable HTTP is the recommended transport for new web-based deployments.
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
from fastmcp import Client
|
| 64 |
+
from fastmcp.client.transports import SSETransport
|
| 65 |
+
|
| 66 |
+
sse_url = "http://localhost:8000/sse"
|
| 67 |
+
|
| 68 |
+
# Since v2.3.0, HTTP URLs default to StreamableHttpTransport,
|
| 69 |
+
# so you must explicitly use SSETransport for SSE connections
|
| 70 |
+
transport_explicit = SSETransport(url=sse_url)
|
| 71 |
+
client_explicit = Client(transport_explicit)
|
| 72 |
+
|
| 73 |
+
async def use_sse_client(client):
|
| 74 |
+
async with client:
|
| 75 |
+
tools = await client.list_tools()
|
| 76 |
+
print(f"Connected via SSE, found tools: {tools}")
|
| 77 |
+
|
| 78 |
+
# asyncio.run(use_sse_client(client_explicit))
|
| 79 |
+
```
|
| 80 |
## Stdio Transports
|
| 81 |
|
| 82 |
These transports manage an MCP server running as a subprocess, communicating with it via standard input (stdin) and standard output (stdout). This is the standard mechanism used by clients like Claude Desktop.
|
|
|
|
| 196 |
# async with client:
|
| 197 |
# response = await client.call_tool("get_npm_data", {})
|
| 198 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
## In-Memory Transports
|
| 201 |
|
|
|
|
| 239 |
## Choosing a Transport
|
| 240 |
|
| 241 |
* **Local Development/Testing:** Use `PythonStdioTransport` (inferred from `.py` files) or `FastMCPTransport` (for same-process testing).
|
| 242 |
+
* **Connecting to Remote/Persistent Servers:** Use `StreamableHttpTransport` (recommended, default for HTTP URLs) or `SSETransport` (legacy option).
|
| 243 |
* **Running Packaged Tools:** Use `UvxStdioTransport` (Python/uv) or `NpxStdioTransport` (Node/npm) if you need to run MCP servers without local installation.
|
| 244 |
* **Integrating with Claude Desktop (or similar):** These tools typically expect to run a Python script, so your server should be runnable via `python your_server.py`, making `PythonStdioTransport` the relevant mechanism on the client side.
|