fix(session): return INVALID_REQUEST error instead of crashing on pre-init requests#2411
Open
Smeet23 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix(session): return INVALID_REQUEST error instead of crashing on pre-init requests#2411Smeet23 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Smeet23 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
…-init requests When an MCP client sends a request (e.g. tools/list) before completing the initialize handshake, the server now responds with a proper INVALID_REQUEST JSON-RPC error instead of raising a RuntimeError that propagates through the anyio task group and crashes the ASGI application. This affects real-world clients such as Cursor, MCP Inspector, and anything-llm that skip re-initialization after a server restart or dropped SSE connection. Github-Issue: modelcontextprotocol#423
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 an MCP client sends a request (e.g.
tools/list) before completing theinitializehandshake, the server raises aRuntimeErrorthat propagates through the anyio task group and crashes the entire ASGI application:This affects real-world clients such as Cursor, MCP Inspector, and anything-llm that skip re-initialization after a server restart or dropped SSE connection. The server dies instead of returning a useful error.
Fixes #423.
Solution
Replace the
raise RuntimeError(...)inServerSession._received_requestwith a proper JSON-RPCINVALID_REQUEST(-32600) error response sent via the responder. The server stays alive and the client receives a meaningful error it can act on.Test
Added
test_request_before_initialization_returns_errortotests/server/test_session.pythat sends atools/listrequest without any priorinitializehandshake and asserts that:JSONRPCErrorwith codeINVALID_REQUESTExceptionGroup)