Skip to content

Return 404 for invalid session ID in handle_regular_request#257

Merged
koic merged 1 commit intomodelcontextprotocol:mainfrom
koic:return_404_for_invalid_session_id
Mar 17, 2026
Merged

Return 404 for invalid session ID in handle_regular_request#257
koic merged 1 commit intomodelcontextprotocol:mainfrom
koic:return_404_for_invalid_session_id

Conversation

@koic
Copy link
Member

@koic koic commented Mar 16, 2026

Motivation and Context

The MCP specification requires that when a server receives a request containing a session ID that is no longer valid, it MUST respond with HTTP 404 Not Found. The handle_get path already returned 404 correctly, but handle_regular_request (the POST path) was returning 400 "Invalid session ID" instead.

Ref: https://modelcontextprotocol.io/specification/2025-11-25/basic/transports#session-management

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.

How Has This Been Tested?

Added a test for POST requests with an invalid session ID, verifying that the response is 404 with "Session not found" error message. All existing tests pass.

Breaking Change

POST requests with an invalid session ID now return HTTP 404 "Session not found" instead of HTTP 400 "Invalid session ID". Clients that match on the 400 status code or the old error message will need to be updated.

However, this change is considered a bug fix because it brings the behavior into compliance with the MCP specification.

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

## Motivation and Context

The MCP specification requires that when a server receives a request containing
a session ID that is no longer valid, it MUST respond with HTTP 404 Not Found.
The `handle_get` path already returned 404 correctly, but `handle_regular_request`
(the POST path) was returning 400 "Invalid session ID" instead.

Ref: https://modelcontextprotocol.io/specification/2025-11-25/basic/transports#session-management

> 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.

## How Has This Been Tested?

Added a test for POST requests with an invalid session ID, verifying that the
response is 404 with "Session not found" error message. All existing tests pass.

## Breaking Change

POST requests with an invalid session ID now return HTTP 404 "Session not found"
instead of HTTP 400 "Invalid session ID". Clients that match on the 400 status
code or the old error message will need to be updated.

However, this change is considered a bug fix because it brings the behavior into compliance with
the MCP specification.
@koic koic force-pushed the return_404_for_invalid_session_id branch from 98bbda9 to cf1d578 Compare March 16, 2026 17:30
@koic
Copy link
Member Author

koic commented Mar 17, 2026

@modelcontextprotocol/ruby-sdk This may cause merge conflicts with upcoming work, so a review when you have a chance would be appreciated.

@koic koic merged commit 8823bd3 into modelcontextprotocol:main Mar 17, 2026
11 checks passed
@koic koic deleted the return_404_for_invalid_session_id branch March 17, 2026 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants