streamable_http_manager
StreamableHTTP Session Manager for MCP servers.
DEFAULT_MAX_REQUEST_BODY_SIZE
module-attribute
DEFAULT_MAX_REQUEST_BODY_SIZE: Final = 4 * 1024 * 1024
Default maximum Streamable HTTP request body size in bytes (4 MiB).
StreamableHTTPSessionManager
Manages StreamableHTTP sessions with optional resumability via event store.
This class abstracts away the complexity of session management, event storage, and request handling for StreamableHTTP transports. It handles:
- Session tracking for clients
- Resumability via an optional event store
- Connection management and lifecycle
- Request handling and transport setup
- Idle session cleanup via optional timeout
Important: Only one StreamableHTTPSessionManager instance should be created per application. The instance cannot be reused after its run() context has completed. If you need to restart the manager, create a new instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
Server[Any]
|
The MCP server instance |
required |
event_store
|
EventStore | None
|
Optional event store for resumability support. If provided, enables resumable connections where clients can reconnect and receive missed events. If None, sessions are still tracked but not resumable. |
None
|
json_response
|
bool
|
Whether to use JSON responses instead of SSE streams |
False
|
stateless
|
bool
|
If True, creates a completely fresh transport for each request with no session tracking or state persistence between requests. |
False
|
security_settings
|
TransportSecuritySettings | None
|
Optional transport security settings. |
None
|
retry_interval
|
int | None
|
Retry interval in milliseconds to suggest to clients in SSE retry field. Used for SSE polling behavior. |
None
|
session_idle_timeout
|
float | None
|
Optional idle timeout in seconds for stateful sessions. If set, sessions that receive no HTTP requests for this duration will be automatically terminated and removed. When retry_interval is also configured, ensure the idle timeout comfortably exceeds the retry interval to avoid reaping sessions during normal SSE polling gaps. Default is None (no timeout). A value of 1800 (30 minutes) is recommended for most deployments. |
None
|
max_request_body_size
|
int
|
Maximum size in bytes for Streamable HTTP POST request bodies. Requests that exceed this limit receive a 413 response before parsing or session creation. Defaults to 4 MiB. |
DEFAULT_MAX_REQUEST_BODY_SIZE
|
Source code in src/mcp/server/streamable_http_manager.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
run
async
run() -> AsyncIterator[None]
Run the session manager with proper lifecycle management.
This creates and manages the task group for all session operations.
Important: This method can only be called once per instance. The same StreamableHTTPSessionManager instance cannot be reused after this context manager exits. Create a new instance if you need to restart.
Use this in the lifespan context manager of your Starlette app:
@contextlib.asynccontextmanager async def lifespan(app: Starlette) -> AsyncIterator[None]: async with session_manager.run(): yield
Source code in src/mcp/server/streamable_http_manager.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
handle_request
async
handle_request(
scope: Scope, receive: Receive, send: Send
) -> None
Process ASGI request with proper session handling and transport setup.
Dispatches to the appropriate handler based on stateless mode.
Source code in src/mcp/server/streamable_http_manager.py
161 162 163 164 165 166 | |
RequestBodyLimitMiddleware
Reject oversized HTTP request bodies before invoking an ASGI application.
Source code in src/mcp/server/streamable_http_manager.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | |
StreamableHTTPASGIApp
ASGI application for Streamable HTTP server transport.
Source code in src/mcp/server/streamable_http_manager.py
387 388 389 390 391 392 393 394 | |