Skip to content

Reject POST requests without session ID in stateful mode#274

Merged
koic merged 1 commit intomodelcontextprotocol:mainfrom
koic:reject_missing_session_id
Mar 29, 2026
Merged

Reject POST requests without session ID in stateful mode#274
koic merged 1 commit intomodelcontextprotocol:mainfrom
koic:reject_missing_session_id

Conversation

@koic
Copy link
Copy Markdown
Member

@koic koic commented Mar 27, 2026

Motivation and Context

Per the MCP specification (Streamable HTTP > Session Management):

Servers that require a session ID SHOULD respond to requests without an Mcp-Session-Id header
(other than initialization) with HTTP 400 Bad Request.

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

Previously, non-initialize POST requests without Mcp-Session-Id in stateful mode were processed with HTTP 200 (for regular requests) or HTTP 202 (for notifications/responses).

This change adds an explicit check in handle_post to return HTTP 400 Bad Request when the session ID is missing in stateful mode for all non-initialize requests, aligning with the specification.

How Has This Been Tested?

Added a regression test and passed.

Breaking Changes

This is not a breaking change, as it is a bug fix to align 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

Per the MCP specification (Streamable HTTP > Session Management):

> Servers that require a session ID SHOULD respond to requests without an `Mcp-Session-Id` header
> (other than initialization) with HTTP 400 Bad Request.

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

Previously, non-initialize POST requests without `Mcp-Session-Id` in stateful mode were processed
with HTTP 200 (for regular requests) or HTTP 202 (for notifications/responses).

This change adds an explicit check in `handle_post` to return HTTP 400 Bad Request when the session
ID is missing in stateful mode for all non-initialize requests, aligning with the specification.
@koic koic merged commit 8969edf into modelcontextprotocol:main Mar 29, 2026
11 checks passed
@koic koic deleted the reject_missing_session_id branch March 29, 2026 03:34
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