Skip to content

Fix/log unknown session#2223

Closed
dpdanpittman wants to merge 3 commits intomodelcontextprotocol:mainfrom
dpdanpittman:fix/log-unknown-session-id
Closed

Fix/log unknown session#2223
dpdanpittman wants to merge 3 commits intomodelcontextprotocol:mainfrom
dpdanpittman:fix/log-unknown-session-id

Conversation

@dpdanpittman
Copy link

Add logger.warning() call in _handle_stateful_request() when a request is rejected due to an unknown or expired session ID. The other two branches (existing session, new session) both log, but this error path was silent.

Motivation and Context

As described in #2204, the else branch in StreamableHTTPSessionManager._handle_stateful_request() returns a 404 response without any logging. In production, when a client sends a stale mcp-session-id after a server restart, there is no way to observe these rejections without wrapping the ASGI app in custom middleware. A logger.warning here makes the error immediately visible in server logs.

How Has This Been Tested?

Extended the existing test_unknown_session_id_returns_404 test to capture log output via caplog and assert the warning message contains the rejected session ID. All 11 tests in tests/server/test_streamable_http_manager.py pass. Lint passes.

Breaking Changes

None. This only adds a log message at WARNING level. Existing code is unaffected.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Single-line fix — adds logger.warning("Rejected request with unknown or expired session ID: %s", request_mcp_session_id) before the existing error response. Consistent with the logging pattern used throughout the rest of the method.

@maxisbey maxisbey added enhancement Request for a new feature that's not currently supported P3 Nice to haves, rare edge cases labels Mar 6, 2026
@maxisbey
Copy link
Contributor

maxisbey commented Mar 6, 2026

Thanks for the contribution — this was addressed in #2212 which was opened first. For good first issue tickets, it's worth checking the linked PRs on the issue before opening a new one.

@maxisbey maxisbey closed this Mar 6, 2026
@dpdanpittman
Copy link
Author

dpdanpittman commented Mar 6, 2026 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Request for a new feature that's not currently supported P3 Nice to haves, rare edge cases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants