Add warning log when rejecting request with unknown/expired session ID#2212
Open
rameshreddy-adutla wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
Conversation
The else branch in _handle_stateful_request() silently returns 404 when a request has an unknown or expired session ID. The other two branches (existing session, new session) both log at debug/info level. Add a logger.warning() call so operators can diagnose why client connections are being rejected, e.g. after a server restart when clients send stale session IDs. Fixes modelcontextprotocol#2204 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Summary
Add a
logger.warning()call in theelsebranch ofStreamableHTTPSessionManager._handle_stateful_request()when rejecting a request with an unknown or expired session ID.Problem
The
elsebranch silently returns HTTP 404 without any logging. The other two branches both log:logger.debug("Session already exists...")(existing session)logger.info("Created new transport...")(new session)This made it very difficult to diagnose connection failures in production — for example, after a server restart when clients continued sending stale
mcp-session-idheaders. Operators had to wrap the ASGI app in custom middleware just to see why requests were being rejected.Fix
Add
logger.warning()with the rejected session ID before returning the 404 response. Usingwarninglevel (notdebug/info) since this typically indicates a problem that operators should investigate.Testing
Existing test
test_unknown_session_id_returns_404passes and now shows the warning in log output:Fixes #2204