-
Notifications
You must be signed in to change notification settings - Fork 2.7k
StreamableHttp - update docs #664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 110 commits
682ff1e
331d51e
d283f56
e96d280
1e9dd4c
d535089
031cadf
765efb6
0637bc3
b99633a
9ae1c21
50683b9
2c5f26a
e605994
e7c5f87
83c0c9f
0c1aae9
038fb04
a4e17f3
5f11c60
571913a
d43647f
9d72c1e
a5079af
c4c2608
bc62d73
3852179
874838a
f788d79
152feb9
f37ebc4
c2873fd
fe2c029
3a13f5d
37c5fc4
792d302
6c48b11
a437566
9fee929
d79be8f
8d637b4
8c86bce
31618c1
5ebbc19
50673c6
02d76f3
88edddc
d774be7
a09e958
4e73552
56f694e
60da682
374a0b4
fb5a568
76ddc65
e42dbf5
10e00e7
87571d8
c6f991b
482149e
5230180
8e15abc
0a1a408
3069aa3
16f0688
5ecc7f0
8c251c9
f46dcb1
d3725cf
07f4e3a
1237148
1ad1842
fa068dd
9b5709a
67d568b
16a7efa
91c09a4
0582bf5
8194bce
2c63020
2ea68f2
b0fe041
ba366e3
e1a9fec
af4221f
f2840fe
cda4401
f2cc6ee
9c78e5e
59e6c64
cb7f0c4
8ddf7f7
2c85c66
ed6dad2
e9cee55
0eef578
01f3f05
ebb9151
fdab98f
c9f686f
013a295
3d8ff7a
0f7b167
5424832
a869767
472dc0f
97fe920
8c8d80d
ee70cb1
f3ace1e
563f8c9
ac9042f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,7 +66,7 @@ The Model Context Protocol allows applications to provide context for LLMs in a | |
|
||
- Build MCP clients that can connect to any MCP server | ||
- Create MCP servers that expose resources, prompts and tools | ||
- Use standard transports like stdio and SSE | ||
- Use standard transports like stdio, SSE, and Streamable HTTP | ||
- Handle all MCP protocol messages and lifecycle events | ||
|
||
## Installation | ||
|
@@ -387,8 +387,66 @@ python server.py | |
mcp run server.py | ||
``` | ||
|
||
### Streamable HTTP Transport | ||
|
||
> **Note**: Streamable HTTP transport is superseding SSE transport for production deployments. | ||
|
||
```python | ||
from mcp.server.fastmcp import FastMCP | ||
|
||
# Stateful server (maintains session state) | ||
mcp = FastMCP("StatefulServer") | ||
|
||
# Stateless server (no session persistence) | ||
mcp = FastMCP("StatelessServer", stateless_http=True) | ||
|
||
# Run server with streamable_http transport | ||
mcp.run(transport="streamable-http") | ||
``` | ||
|
||
You can mount multiple FastMCP servers in a FastAPI application: | ||
|
||
```python | ||
# echo.py | ||
from mcp.server.fastmcp import FastMCP | ||
|
||
mcp = FastMCP(name="EchoServer", stateless_http=True) | ||
|
||
|
||
@mcp.tool(description="A simple echo tool") | ||
def echo(message: str) -> str: | ||
return f"Echo: {message}" | ||
``` | ||
|
||
```python | ||
# main.py | ||
from fastapi import FastAPI | ||
from routers import echo | ||
|
||
app = FastAPI() | ||
|
||
# Use the session manager's lifespan | ||
app = FastAPI(lifespan=lambda app: echo.mcp.session_manager.run()) | ||
app.mount("/echo", echo.mcp.streamable_http_app()) | ||
``` | ||
|
||
For low level server with Streamable HTTP implementations, see: | ||
- Stateful server: [`examples/servers/simple-streamablehttp/`](examples/servers/simple-streamablehttp/) | ||
- Stateless server: [`examples/servers/simple-streamablehttp-stateless/`](examples/servers/simple-streamablehttp-stateless/) | ||
|
||
|
||
|
||
The streamable HTTP transport supports: | ||
- Stateful and stateless operation modes | ||
- Resumability with event stores | ||
- JSON or SSE response formats | ||
- Better scalability for multi-node deployments | ||
|
||
|
||
### Mounting to an Existing ASGI Server | ||
|
||
> **Note**: SSE transport is being superseded by streamable HTTP transport. Consider using streamable HTTP for production deployments. | ||
|
||
|
||
You can mount the SSE server to an existing ASGI server using the `sse_app` method. This allows you to integrate the SSE server with other ASGI applications. | ||
|
||
```python | ||
|
@@ -621,7 +679,7 @@ if __name__ == "__main__": | |
|
||
### Writing MCP Clients | ||
|
||
The SDK provides a high-level client interface for connecting to MCP servers: | ||
The SDK provides a high-level client interface for connecting to MCP servers using various transports: | ||
|
||
```python | ||
from mcp import ClientSession, StdioServerParameters, types | ||
|
@@ -685,6 +743,28 @@ if __name__ == "__main__": | |
asyncio.run(run()) | ||
``` | ||
|
||
Clients can also connect using streamable HTTP transport: | ||
|
||
|
||
```python | ||
from mcp.client.streamable_http import streamablehttp_client | ||
from mcp import ClientSession | ||
|
||
|
||
async def main(): | ||
# Connect to a streamable HTTP server | ||
async with streamablehttp_client("example/mcp") as ( | ||
read_stream, | ||
write_stream, | ||
_, | ||
): | ||
# Create a session using the client streams | ||
async with ClientSession(read_stream, write_stream) as session: | ||
# Initialize the connection | ||
await session.initialize() | ||
# Call a tool | ||
tool_result = await session.call_tool("echo", {"message": "hello"}) | ||
``` | ||
|
||
### MCP Primitives | ||
|
||
The MCP protocol defines three core primitives that servers can implement: | ||
|
Uh oh!
There was an error while loading. Please reload this page.