streamable_http_manager
StreamableHTTP Session Manager for MCP servers.
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
|
Source code in src/mcp/server/streamable_http_manager.py
32 33 34 35 36 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 | |
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
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 | |
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
139 140 141 142 143 144 145 146 147 148 149 150 151 | |
StreamableHTTPASGIApp
ASGI application for Streamable HTTP server transport.
Source code in src/mcp/server/streamable_http_manager.py
289 290 291 292 293 294 295 296 | |