Skip to content

Commit 72003d9

Browse files
authored
StreamableHttp - update docs (#664)
1 parent e4e119b commit 72003d9

File tree

1 file changed

+97
-2
lines changed

1 file changed

+97
-2
lines changed

README.md

+97-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ The Model Context Protocol allows applications to provide context for LLMs in a
6666

6767
- Build MCP clients that can connect to any MCP server
6868
- Create MCP servers that expose resources, prompts and tools
69-
- Use standard transports like stdio and SSE
69+
- Use standard transports like stdio, SSE, and Streamable HTTP
7070
- Handle all MCP protocol messages and lifecycle events
7171

7272
## Installation
@@ -387,8 +387,81 @@ python server.py
387387
mcp run server.py
388388
```
389389

390+
### Streamable HTTP Transport
391+
392+
> **Note**: Streamable HTTP transport is superseding SSE transport for production deployments.
393+
394+
```python
395+
from mcp.server.fastmcp import FastMCP
396+
397+
# Stateful server (maintains session state)
398+
mcp = FastMCP("StatefulServer")
399+
400+
# Stateless server (no session persistence)
401+
mcp = FastMCP("StatelessServer", stateless_http=True)
402+
403+
# Run server with streamable_http transport
404+
mcp.run(transport="streamable-http")
405+
```
406+
407+
You can mount multiple FastMCP servers in a FastAPI application:
408+
409+
```python
410+
# echo.py
411+
from mcp.server.fastmcp import FastMCP
412+
413+
mcp = FastMCP(name="EchoServer", stateless_http=True)
414+
415+
416+
@mcp.tool(description="A simple echo tool")
417+
def echo(message: str) -> str:
418+
return f"Echo: {message}"
419+
```
420+
421+
```python
422+
# math.py
423+
from mcp.server.fastmcp import FastMCP
424+
425+
mcp = FastMCP(name="MathServer", stateless_http=True)
426+
427+
428+
@mcp.tool(description="A simple add tool")
429+
def add_two(n: int) -> str:
430+
return n + 2
431+
```
432+
433+
```python
434+
# main.py
435+
from fastapi import FastAPI
436+
from mcp.echo import echo
437+
from mcp.math import math
438+
439+
440+
app = FastAPI()
441+
442+
# Use the session manager's lifespan
443+
app = FastAPI(lifespan=lambda app: echo.mcp.session_manager.run())
444+
app.mount("/echo", echo.mcp.streamable_http_app())
445+
app.mount("/math", math.mcp.streamable_http_app())
446+
```
447+
448+
For low level server with Streamable HTTP implementations, see:
449+
- Stateful server: [`examples/servers/simple-streamablehttp/`](examples/servers/simple-streamablehttp/)
450+
- Stateless server: [`examples/servers/simple-streamablehttp-stateless/`](examples/servers/simple-streamablehttp-stateless/)
451+
452+
453+
454+
The streamable HTTP transport supports:
455+
- Stateful and stateless operation modes
456+
- Resumability with event stores
457+
- JSON or SSE response formats
458+
- Better scalability for multi-node deployments
459+
460+
390461
### Mounting to an Existing ASGI Server
391462

463+
> **Note**: SSE transport is being superseded by [Streamable HTTP transport](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http).
464+
392465
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.
393466

394467
```python
@@ -621,7 +694,7 @@ if __name__ == "__main__":
621694

622695
### Writing MCP Clients
623696

624-
The SDK provides a high-level client interface for connecting to MCP servers:
697+
The SDK provides a high-level client interface for connecting to MCP servers using various [transports](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports):
625698

626699
```python
627700
from mcp import ClientSession, StdioServerParameters, types
@@ -685,6 +758,28 @@ if __name__ == "__main__":
685758
asyncio.run(run())
686759
```
687760

761+
Clients can also connect using [Streamable HTTP transport](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http):
762+
763+
```python
764+
from mcp.client.streamable_http import streamablehttp_client
765+
from mcp import ClientSession
766+
767+
768+
async def main():
769+
# Connect to a streamable HTTP server
770+
async with streamablehttp_client("example/mcp") as (
771+
read_stream,
772+
write_stream,
773+
_,
774+
):
775+
# Create a session using the client streams
776+
async with ClientSession(read_stream, write_stream) as session:
777+
# Initialize the connection
778+
await session.initialize()
779+
# Call a tool
780+
tool_result = await session.call_tool("echo", {"message": "hello"})
781+
```
782+
688783
### MCP Primitives
689784

690785
The MCP protocol defines three core primitives that servers can implement:

0 commit comments

Comments
 (0)