Title: StreamableHTTPSessionManager returns 400 instead of 404 for unknown session ID · Issue #1727 · modelcontextprotocol/python-sdk · GitHub
Open Graph Title: StreamableHTTPSessionManager returns 400 instead of 404 for unknown session ID · Issue #1727 · modelcontextprotocol/python-sdk
X Title: StreamableHTTPSessionManager returns 400 instead of 404 for unknown session ID · Issue #1727 · modelcontextprotocol/python-sdk
Description: Description StreamableHTTPSessionManager._handle_stateful_request() returns HTTP 400 Bad Request when a session ID is provided but not found in the server instances. According to the MCP spec, this should be HTTP 404 Not Found. Spec Refe...
Open Graph Description: Description StreamableHTTPSessionManager._handle_stateful_request() returns HTTP 400 Bad Request when a session ID is provided but not found in the server instances. According to the MCP spec, this...
X Description: Description StreamableHTTPSessionManager._handle_stateful_request() returns HTTP 400 Bad Request when a session ID is provided but not found in the server instances. According to the MCP spec, this...
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk/issues/1727
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"StreamableHTTPSessionManager returns 400 instead of 404 for unknown session ID","articleBody":"## Description\n\n`StreamableHTTPSessionManager._handle_stateful_request()` returns HTTP 400 Bad Request when a session ID is provided but not found in the server instances. According to the MCP spec, this should be HTTP 404 Not Found.\n\n## Spec Reference\n\nFrom the [Streamable HTTP transport spec](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#session-management):\n\n\u003e Servers that require a session ID SHOULD respond to requests without an `Mcp-Session-Id` header (other than initialization) with **HTTP 400 Bad Request**.\n\n\u003e The server MAY terminate the session at any time, after which it MUST respond to requests containing that session ID with **HTTP 404 Not Found**.\n\n\u003e When a client receives HTTP 404 in response to a request containing an `Mcp-Session-Id`, it MUST start a new session by sending a new `InitializeRequest` without a session ID attached.\n\n## Current Behavior\n\nIn `src/mcp/server/streamable_http_manager.py`, the `_handle_stateful_request` method:\n\n```python\nif request_mcp_session_id is not None and request_mcp_session_id in self._server_instances:\n # Handle existing session\n ...\nelif request_mcp_session_id is None:\n # Create new session\n ...\nelse:\n # Session ID provided but not found\n response = Response(\n \"Bad Request: No valid session ID provided\",\n status_code=HTTPStatus.BAD_REQUEST, # ← Should be NOT_FOUND (404)\n )\n```\n\n## Expected Behavior\n\nThe `else` branch should return 404 Not Found:\n\n```python\nelse:\n response = Response(\n \"Not Found: Session not found\",\n status_code=HTTPStatus.NOT_FOUND,\n )\n```\n\n## Why This Matters\n\nThe distinction matters for client behavior:\n- **400** indicates a malformed request - clients may retry or report an error\n- **404** indicates the session doesn't exist - clients know to re-initialize\n\nThe transport layer (`streamable_http.py`) correctly implements this in `_validate_session()`, but the manager layer has its own session lookup that doesn't follow the same pattern.\n\n## Note\n\nThe TypeScript SDK's `StreamableHTTPServerTransport` correctly returns 404 for session mismatch in its `validateSession` method.","author":{"url":"https://github.com/johnw188","@type":"Person","name":"johnw188"},"datePublished":"2025-12-05T14:10:38.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/1727/python-sdk/issues/1727"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8d4aa58e-facc-0bce-aaaa-1d650710c4dc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9E30:3DB751:A5A907:EE2DB1:6A578E91 |
| html-safe-nonce | 6d23ea7d94467a22e652beb94ea3ac2c9e7069e666815c0fade22d0cdf788bc9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RTMwOjNEQjc1MTpBNUE5MDc6RUUyREIxOjZBNTc4RTkxIiwidmlzaXRvcl9pZCI6IjgzMzQzMzc5MDc0MjgzOTY2ODkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 282da6aaf534e472bc70bdc956a0dafd26d6df7aed0cf64703ed4404f74e56e4 |
| hovercard-subject-tag | issue:3699130128 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/modelcontextprotocol/python-sdk/1727/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9af5c7e00c07aadfa8574c1daf73b572c11c5410968f096eb94b0d2e2a31e6cd/modelcontextprotocol/python-sdk/issues/1727 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9af5c7e00c07aadfa8574c1daf73b572c11c5410968f096eb94b0d2e2a31e6cd/modelcontextprotocol/python-sdk/issues/1727 |
| og:image:alt | Description StreamableHTTPSessionManager._handle_stateful_request() returns HTTP 400 Bad Request when a session ID is provided but not found in the server instances. According to the MCP spec, this... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | johnw188 |
| hostname | github.com |
| expected-hostname | github.com |
| None | c6353e56e94423c9cd5abde7708a859afa36a0c1203c73749055bf6ee6704cd0 |
| turbo-cache-control | no-preview |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f59ab1c46b638204e2aea09126d089be5dc773b7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width