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
Conversation
…ion on initialize HttpServletStreamableServerTransportProvider and HttpServletStatelessServerTransport accepted POST requests regardless of Content-Type, processing text/plain and form-encoded bodies as if they were application/json. Add an early Content-Type guard in doPost() on both transports that returns HTTP 415 Unsupported Media Type when Content-Type is absent or does not start with application/json. Also validate on initialize requests that the MCP-Protocol-Version HTTP header, when present, is consistent with the protocolVersion field in the JSON-RPC body. A mismatch returns HTTP 400 with a JSON-RPC INVALID_REQUEST error. Fixes modelcontextprotocol#961 Fixes modelcontextprotocol#963 Signed-off-by: Gorre Surya <[email protected]>
34d1eb6 to
580cdf3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #961 and #963.
Changes
#961 — Content-Type validation (HTTP 415)
HttpServletStreamableServerTransportProvider.doPost()accepted POST requests regardless of theirContent-Typeheader, processingtext/plain,application/x-www-form-urlencoded, and requests with noContent-Typeidentically toapplication/jsonrequests.Added an early guard in
doPost()that returns HTTP 415 Unsupported Media Type whenContent-Typeis absent or does not start withapplication/json.#963 — Protocol version header/body consistency on initialize
When the
MCP-Protocol-Versionrequest header on aninitializerequest disagrees withparams.protocolVersionin the JSON-RPC body, the server now returns HTTP 400 with a JSON-RPCINVALID_PARAMSerror rather than silently accepting the mismatched input.Test plan
Content-Type: text/plain→ 415Content-Type→ 415Content-Type: application/json→ proceeds normallyinitializewith header2025-03-26and body2025-11-25→ 400initializewith matching header and body → proceeds normallyinitializewith noMCP-Protocol-Versionheader → proceeds normally (header is optional)