Skip to content

Commit b5327f4

Browse files
committed
docs: Update examples to use stateless HTTP with JSON responses
Update README and example code to consistently recommend and demonstrate stateless HTTP with JSON responses (stateless_http=True, json_response=True) as the recommended pattern for production deployments. Changes: - Update quickstart example to use stateless HTTP + JSON and make it executable - Update all streamable HTTP mounting examples with recommended config - Update OAuth server example with recommended config - Reorder streamable_config.py to show recommended option first - Change quickstart integration instructions to use Claude Code with HTTP - Clarify that stateless HTTP + JSON is optimal for scalability This encourages users away from stdio/SSE/stateful HTTP patterns that have extra complexity and makes servers less reliable for most use cases.
1 parent 40acbc5 commit b5327f4

9 files changed

+57
-33
lines changed

README.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ Let's create a simple MCP server that exposes a calculator tool and some data:
132132
"""
133133
FastMCP quickstart example.
134134
135-
cd to the `examples/snippets/clients` directory and run:
136-
uv run server fastmcp_quickstart stdio
135+
Run from the repository root:
136+
uv run examples/snippets/servers/fastmcp_quickstart.py
137137
"""
138138

139139
from mcp.server.fastmcp import FastMCP
140140

141141
# Create an MCP server
142-
mcp = FastMCP("Demo")
142+
mcp = FastMCP("Demo", stateless_http=True, json_response=True)
143143

144144

145145
# Add an addition tool
@@ -167,15 +167,20 @@ def greet_user(name: str, style: str = "friendly") -> str:
167167
}
168168

169169
return f"{styles.get(style, styles['friendly'])} for someone named {name}."
170+
171+
172+
# Run with streamable HTTP transport
173+
if __name__ == "__main__":
174+
mcp.run(transport="streamable-http")
170175
```
171176

172177
_Full example: [examples/snippets/servers/fastmcp_quickstart.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/fastmcp_quickstart.py)_
173178
<!-- /snippet-source -->
174179

175-
You can install this server in [Claude Desktop](https://claude.ai/download) and interact with it right away by running:
180+
You can install this server in [Claude Code](https://docs.claude.com/en/docs/claude-code/mcp) and interact with it right away by running:
176181

177182
```bash
178-
uv run mcp install server.py
183+
claude mcp add --transport http my-server http://localhost:8000/mcp
179184
```
180185

181186
Alternatively, you can test it with the MCP Inspector:
@@ -888,6 +893,8 @@ class SimpleTokenVerifier(TokenVerifier):
888893
# Create FastMCP instance as a Resource Server
889894
mcp = FastMCP(
890895
"Weather Service",
896+
stateless_http=True,
897+
json_response=True,
891898
# Token verifier for authentication
892899
token_verifier=SimpleTokenVerifier(),
893900
# Auth settings for RFC 9728 Protected Resource Metadata
@@ -1103,7 +1110,7 @@ Note that `uv run mcp run` or `uv run mcp dev` only supports server using FastMC
11031110

11041111
### Streamable HTTP Transport
11051112

1106-
> **Note**: Streamable HTTP transport is superseding SSE transport for production deployments.
1113+
> **Note**: Streamable HTTP transport is the recommended transport for production deployments. Use `stateless_http=True` and `json_response=True` for optimal scalability.
11071114
11081115
<!-- snippet-source examples/snippets/servers/streamable_config.py -->
11091116
```python
@@ -1114,15 +1121,15 @@ Run from the repository root:
11141121

11151122
from mcp.server.fastmcp import FastMCP
11161123

1117-
# Stateful server (maintains session state)
1118-
mcp = FastMCP("StatefulServer")
1124+
# Stateless server with JSON responses (recommended)
1125+
mcp = FastMCP("StatelessServer", stateless_http=True, json_response=True)
11191126

11201127
# Other configuration options:
1121-
# Stateless server (no session persistence)
1128+
# Stateless server with SSE streaming responses
11221129
# mcp = FastMCP("StatelessServer", stateless_http=True)
11231130

1124-
# Stateless server (no session persistence, no sse stream with supported client)
1125-
# mcp = FastMCP("StatelessServer", stateless_http=True, json_response=True)
1131+
# Stateful server with session persistence
1132+
# mcp = FastMCP("StatefulServer")
11261133

11271134

11281135
# Add a simple tool to demonstrate the server
@@ -1157,7 +1164,7 @@ from starlette.routing import Mount
11571164
from mcp.server.fastmcp import FastMCP
11581165

11591166
# Create the Echo server
1160-
echo_mcp = FastMCP(name="EchoServer", stateless_http=True)
1167+
echo_mcp = FastMCP(name="EchoServer", stateless_http=True, json_response=True)
11611168

11621169

11631170
@echo_mcp.tool()
@@ -1167,7 +1174,7 @@ def echo(message: str) -> str:
11671174

11681175

11691176
# Create the Math server
1170-
math_mcp = FastMCP(name="MathServer", stateless_http=True)
1177+
math_mcp = FastMCP(name="MathServer", stateless_http=True, json_response=True)
11711178

11721179

11731180
@math_mcp.tool()
@@ -1268,7 +1275,7 @@ from starlette.routing import Mount
12681275
from mcp.server.fastmcp import FastMCP
12691276

12701277
# Create MCP server
1271-
mcp = FastMCP("My App")
1278+
mcp = FastMCP("My App", stateless_http=True, json_response=True)
12721279

12731280

12741281
@mcp.tool()
@@ -1305,7 +1312,7 @@ from starlette.routing import Host
13051312
from mcp.server.fastmcp import FastMCP
13061313

13071314
# Create MCP server
1308-
mcp = FastMCP("MCP Host App")
1315+
mcp = FastMCP("MCP Host App", stateless_http=True, json_response=True)
13091316

13101317

13111318
@mcp.tool()
@@ -1342,8 +1349,8 @@ from starlette.routing import Mount
13421349
from mcp.server.fastmcp import FastMCP
13431350

13441351
# Create multiple MCP servers
1345-
api_mcp = FastMCP("API Server")
1346-
chat_mcp = FastMCP("Chat Server")
1352+
api_mcp = FastMCP("API Server", stateless_http=True, json_response=True)
1353+
chat_mcp = FastMCP("Chat Server", stateless_http=True, json_response=True)
13471354

13481355

13491356
@api_mcp.tool()
@@ -1393,7 +1400,12 @@ from mcp.server.fastmcp import FastMCP
13931400

13941401
# Configure streamable_http_path during initialization
13951402
# This server will mount at the root of wherever it's mounted
1396-
mcp_at_root = FastMCP("My Server", streamable_http_path="/")
1403+
mcp_at_root = FastMCP(
1404+
"My Server",
1405+
stateless_http=True,
1406+
json_response=True,
1407+
streamable_http_path="/",
1408+
)
13971409

13981410

13991411
@mcp_at_root.tool()

examples/snippets/servers/fastmcp_quickstart.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""
22
FastMCP quickstart example.
33
4-
cd to the `examples/snippets/clients` directory and run:
5-
uv run server fastmcp_quickstart stdio
4+
Run from the repository root:
5+
uv run examples/snippets/servers/fastmcp_quickstart.py
66
"""
77

88
from mcp.server.fastmcp import FastMCP
99

1010
# Create an MCP server
11-
mcp = FastMCP("Demo")
11+
mcp = FastMCP("Demo", stateless_http=True, json_response=True)
1212

1313

1414
# Add an addition tool
@@ -36,3 +36,8 @@ def greet_user(name: str, style: str = "friendly") -> str:
3636
}
3737

3838
return f"{styles.get(style, styles['friendly'])} for someone named {name}."
39+
40+
41+
# Run with streamable HTTP transport
42+
if __name__ == "__main__":
43+
mcp.run(transport="streamable-http")

examples/snippets/servers/oauth_server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ async def verify_token(self, token: str) -> AccessToken | None:
2020
# Create FastMCP instance as a Resource Server
2121
mcp = FastMCP(
2222
"Weather Service",
23+
stateless_http=True,
24+
json_response=True,
2325
# Token verifier for authentication
2426
token_verifier=SimpleTokenVerifier(),
2527
# Auth settings for RFC 9728 Protected Resource Metadata

examples/snippets/servers/streamable_config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
from mcp.server.fastmcp import FastMCP
77

8-
# Stateful server (maintains session state)
9-
mcp = FastMCP("StatefulServer")
8+
# Stateless server with JSON responses (recommended)
9+
mcp = FastMCP("StatelessServer", stateless_http=True, json_response=True)
1010

1111
# Other configuration options:
12-
# Stateless server (no session persistence)
12+
# Stateless server with SSE streaming responses
1313
# mcp = FastMCP("StatelessServer", stateless_http=True)
1414

15-
# Stateless server (no session persistence, no sse stream with supported client)
16-
# mcp = FastMCP("StatelessServer", stateless_http=True, json_response=True)
15+
# Stateful server with session persistence
16+
# mcp = FastMCP("StatefulServer")
1717

1818

1919
# Add a simple tool to demonstrate the server

examples/snippets/servers/streamable_http_basic_mounting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from mcp.server.fastmcp import FastMCP
1212

1313
# Create MCP server
14-
mcp = FastMCP("My App")
14+
mcp = FastMCP("My App", stateless_http=True, json_response=True)
1515

1616

1717
@mcp.tool()

examples/snippets/servers/streamable_http_host_mounting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from mcp.server.fastmcp import FastMCP
1212

1313
# Create MCP server
14-
mcp = FastMCP("MCP Host App")
14+
mcp = FastMCP("MCP Host App", stateless_http=True, json_response=True)
1515

1616

1717
@mcp.tool()

examples/snippets/servers/streamable_http_multiple_servers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from mcp.server.fastmcp import FastMCP
1212

1313
# Create multiple MCP servers
14-
api_mcp = FastMCP("API Server")
15-
chat_mcp = FastMCP("Chat Server")
14+
api_mcp = FastMCP("API Server", stateless_http=True, json_response=True)
15+
chat_mcp = FastMCP("Chat Server", stateless_http=True, json_response=True)
1616

1717

1818
@api_mcp.tool()

examples/snippets/servers/streamable_http_path_config.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212

1313
# Configure streamable_http_path during initialization
1414
# This server will mount at the root of wherever it's mounted
15-
mcp_at_root = FastMCP("My Server", streamable_http_path="/")
15+
mcp_at_root = FastMCP(
16+
"My Server",
17+
stateless_http=True,
18+
json_response=True,
19+
streamable_http_path="/",
20+
)
1621

1722

1823
@mcp_at_root.tool()

examples/snippets/servers/streamable_starlette_mount.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from mcp.server.fastmcp import FastMCP
1212

1313
# Create the Echo server
14-
echo_mcp = FastMCP(name="EchoServer", stateless_http=True)
14+
echo_mcp = FastMCP(name="EchoServer", stateless_http=True, json_response=True)
1515

1616

1717
@echo_mcp.tool()
@@ -21,7 +21,7 @@ def echo(message: str) -> str:
2121

2222

2323
# Create the Math server
24-
math_mcp = FastMCP(name="MathServer", stateless_http=True)
24+
math_mcp = FastMCP(name="MathServer", stateless_http=True, json_response=True)
2525

2626

2727
@math_mcp.tool()

0 commit comments

Comments
 (0)