Skip to content

fix: reject non-JSON Content-Type with 415 and validate protocol version header on initialize#968

Open
suryateja-g13 wants to merge 1 commit into
modelcontextprotocol:mainfrom
suryateja-g13:fix/961-content-type-validation
Open

fix: reject non-JSON Content-Type with 415 and validate protocol version header on initialize#968
suryateja-g13 wants to merge 1 commit into
modelcontextprotocol:mainfrom
suryateja-g13:fix/961-content-type-validation

Conversation

@suryateja-g13
Copy link
Copy Markdown

Fixes #961 and #963.

Changes

#961 — Content-Type validation (HTTP 415)

HttpServletStreamableServerTransportProvider.doPost() accepted POST requests regardless of their Content-Type header, processing text/plain, application/x-www-form-urlencoded, and requests with no Content-Type identically to application/json requests.

Added an early guard in doPost() that returns HTTP 415 Unsupported Media Type when Content-Type is absent or does not start with application/json.

#963 — Protocol version header/body consistency on initialize

When the MCP-Protocol-Version request header on an initialize request disagrees with params.protocolVersion in the JSON-RPC body, the server now returns HTTP 400 with a JSON-RPC INVALID_PARAMS error rather than silently accepting the mismatched input.

Test plan

  • POST with Content-Type: text/plain → 415
  • POST with no Content-Type → 415
  • POST with Content-Type: application/json → proceeds normally
  • initialize with header 2025-03-26 and body 2025-11-25 → 400
  • initialize with matching header and body → proceeds normally
  • initialize with no MCP-Protocol-Version header → proceeds normally (header is optional)

HttpServletStreamableServerTransportProvider accepted POST requests
regardless of their Content-Type header, processing them normally even
when declared as text/plain, application/x-www-form-urlencoded, or
with no Content-Type at all.

Add an early Content-Type check in doPost() that returns HTTP 415
Unsupported Media Type when the request Content-Type is absent or
does not start with application/json, consistent with other MCP
server implementations and browser/CORS hardening expectations.

Also validate that the MCP-Protocol-Version request header on
initialize requests is consistent with the protocolVersion field in
the JSON-RPC body, returning HTTP 400 with a JSON-RPC INVALID_PARAMS
error on mismatch.

Fixes modelcontextprotocol#961
Fixes modelcontextprotocol#963

Signed-off-by: Gorre Surya <suryateja.g13@gmail.com>
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.

Streamable HTTP server accepts POST requests with non-JSON request Content-Type

1 participant