|
1 | 1 | ---
|
2 |
| -title: Transport |
3 |
| -description: How to communicate with the FastAPI app |
| 2 | +title: MCP Transport |
| 3 | +description: Understanding MCP transport methods and how to choose between them |
4 | 4 | icon: car
|
5 | 5 | ---
|
6 | 6 |
|
7 |
| -FastAPI-MCP uses ASGI transport by default, which means it communicates directly with your FastAPI app without making HTTP requests. This is more efficient and doesn't require a base URL. |
| 7 | +FastAPI-MCP supports two MCP transport methods for client-server communication: **HTTP transport** (recommended) and **SSE transport** (backwards compatibility). |
8 | 8 |
|
9 |
| -It's not even necessary that the FastAPI server will run. |
| 9 | +## HTTP Transport (Recommended) |
10 | 10 |
|
11 |
| -If you need to specify a custom base URL or use a different transport method, you can provide your own `httpx.AsyncClient`: |
| 11 | +HTTP transport is the **recommended** transport method as it implements the latest MCP Streamable HTTP specification. It provides better session management, more robust connection handling, and aligns with standard HTTP practices. |
12 | 12 |
|
13 |
| -```python {7-10, 14} |
14 |
| -import httpx |
| 13 | +### Using HTTP Transport |
| 14 | + |
| 15 | +```python {7} |
| 16 | +from fastapi import FastAPI |
| 17 | +from fastapi_mcp import FastApiMCP |
| 18 | + |
| 19 | +app = FastAPI() |
| 20 | +mcp = FastApiMCP(app) |
| 21 | + |
| 22 | +# Mount using HTTP transport (recommended) |
| 23 | +mcp.mount_http() |
| 24 | +``` |
| 25 | + |
| 26 | +## SSE Transport (Backwards Compatibility) |
| 27 | + |
| 28 | +SSE (Server-Sent Events) transport is maintained for backwards compatibility with older MCP implementations. |
| 29 | + |
| 30 | +### Using SSE Transport |
| 31 | + |
| 32 | +```python {7} |
15 | 33 | from fastapi import FastAPI
|
16 | 34 | from fastapi_mcp import FastApiMCP
|
17 | 35 |
|
18 | 36 | app = FastAPI()
|
| 37 | +mcp = FastApiMCP(app) |
| 38 | + |
| 39 | +# Mount using SSE transport (backwards compatibility) |
| 40 | +mcp.mount_sse() |
| 41 | +``` |
| 42 | + |
| 43 | +## Advanced Configuration |
| 44 | + |
| 45 | +Both transport methods support the same FastAPI integration features like custom routing and authentication: |
| 46 | + |
| 47 | +```python |
| 48 | +from fastapi import FastAPI, APIRouter |
| 49 | +from fastapi_mcp import FastApiMCP |
| 50 | + |
| 51 | +app = FastAPI() |
| 52 | +router = APIRouter(prefix="/api/v1") |
| 53 | + |
| 54 | +mcp = FastApiMCP(app) |
| 55 | + |
| 56 | +# Mount to custom path with HTTP transport |
| 57 | +mcp.mount_http(router, mount_path="/my-http") |
| 58 | + |
| 59 | +# Or with SSE transport |
| 60 | +mcp.mount_sse(router, mount_path="/my-sse") |
| 61 | +``` |
| 62 | + |
| 63 | +## Client Connection Examples |
| 64 | + |
| 65 | +### HTTP Transport Client Connection |
| 66 | + |
| 67 | +For HTTP transport, MCP clients connect directly to the HTTP endpoint: |
| 68 | + |
| 69 | +```json |
| 70 | +{ |
| 71 | + "mcpServers": { |
| 72 | + "fastapi-mcp": { |
| 73 | + "url": "http://localhost:8000/mcp" |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
19 | 78 |
|
20 |
| -custom_client = httpx.AsyncClient( |
21 |
| - base_url="https://api.example.com", |
22 |
| - timeout=30.0 |
23 |
| -) |
| 79 | +### SSE Transport Client Connection |
24 | 80 |
|
25 |
| -mcp = FastApiMCP( |
26 |
| - app, |
27 |
| - http_client=custom_client |
28 |
| -) |
| 81 | +For SSE transport, MCP clients use the same URL but communicate via Server-Sent Events: |
29 | 82 |
|
30 |
| -mcp.mount() |
| 83 | +```json |
| 84 | +{ |
| 85 | + "mcpServers": { |
| 86 | + "fastapi-mcp": { |
| 87 | + "url": "http://localhost:8000/sse" |
| 88 | + } |
| 89 | + } |
| 90 | +} |
31 | 91 | ```
|
0 commit comments