fix: return 405 for GET/DELETE in stateless streamable-http mode#2262
Open
sys-2077 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: return 405 for GET/DELETE in stateless streamable-http mode#2262sys-2077 wants to merge 1 commit intomodelcontextprotocol:mainfrom
sys-2077 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
In stateless mode, GET opens a session-less SSE stream that idles until the platform kills it (e.g. Cloud Run 120s timeout). Clients like mcp-remote auto-reconnect, creating an infinite loop of billed CPU. The TypeScript SDK correctly returns 405 for GET and DELETE in stateless mode. This aligns the Python SDK with that behavior and the MCP spec (GET is MAY, not MUST; stateless servers have no session context for server-initiated notifications).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
stateless=True,StreamableHTTPSessionManager._handle_stateless_request()delegates all HTTP methods (GET, POST, DELETE) to a per-requestStreamableHTTPServerTransport. GET opens an SSE stream that has no session context and will never receive server-initiated messages. The stream idles until the platform kills it (e.g. Cloud Run 120s timeout), then clients likemcp-remoteauto-reconnect, creating an infinite loop of billed CPU time.The TypeScript SDK correctly returns 405 for GET and DELETE in stateless mode (see
src/server/streamableHttp.tsstateless example).Impact
Any MCP server deployed on serverless platforms (Cloud Run, Lambda, Azure Container Apps) with
stateless=Trueis affected. Each idle SSE reconnection cycle bills ~120s of CPU. A single forgotten client session can consume 180,000+ CPU-seconds/month (exceeding Cloud Run's entire free tier).Fix
In
_handle_stateless_request(), validate that the method is POST before creating the transport. Return 405 withAllow: POSTheader for GET and DELETE, matching the TypeScript SDK behavior and the MCP spec.Spec reference
MCP Streamable HTTP transport (2025-03-26):
Tests
Added two tests in
tests/server/test_streamable_http_manager.py:test_stateless_get_returns_405test_stateless_delete_returns_405