Skip to content

Return 404 for invalid session ID in handle_delete#261

Open
koic wants to merge 1 commit intomodelcontextprotocol:mainfrom
koic:return_404_for_invalid_session_id_in_handle_delete
Open

Return 404 for invalid session ID in handle_delete#261
koic wants to merge 1 commit intomodelcontextprotocol:mainfrom
koic:return_404_for_invalid_session_id_in_handle_delete

Conversation

@koic
Copy link
Member

@koic koic commented Mar 18, 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. handle_delete was unconditionally calling cleanup_session and returning 200 even for nonexistent session IDs.

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 tests for DELETE with invalid session ID, and lifecycle tests for delete-then-POST and delete-then-DELETE scenarios.

Breaking Change

DELETE requests with an invalid session ID now return HTTP 404 "Session not found" instead of HTTP 200.

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.
`handle_delete` was unconditionally calling `cleanup_session` and returning 200
even for nonexistent session IDs.

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 tests for DELETE with invalid session ID, and lifecycle tests for
delete-then-POST and delete-then-DELETE scenarios.

## Breaking Change

DELETE requests with an invalid session ID now return HTTP 404 "Session not found"
instead of HTTP 200.

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_in_handle_delete branch from 80f8799 to 0a104d4 Compare March 18, 2026 03:36
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.

1 participant