Skip to content

Commit 34d1eb6

Browse files
committed
fix: reject POST requests with non-JSON Content-Type with HTTP 415
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 #961 Fixes #963 Signed-off-by: Gorre Surya <suryateja.g13@gmail.com>
1 parent c09ee67 commit 34d1eb6

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

mcp-core/src/main/java/io/modelcontextprotocol/server/transport/HttpServletStreamableServerTransportProvider.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,12 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
415415
return;
416416
}
417417

418+
String contentType = request.getContentType();
419+
if (contentType == null || !contentType.startsWith(APPLICATION_JSON)) {
420+
response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "Content-Type must be application/json");
421+
return;
422+
}
423+
418424
List<String> badRequestErrors = new ArrayList<>();
419425

420426
String accept = request.getHeader(ACCEPT);
@@ -450,6 +456,17 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
450456
McpSchema.InitializeRequest initializeRequest = jsonMapper.convertValue(jsonrpcRequest.params(),
451457
new TypeRef<McpSchema.InitializeRequest>() {
452458
});
459+
460+
String headerVersion = request.getHeader(HttpHeaders.PROTOCOL_VERSION);
461+
if (headerVersion != null && !headerVersion.equals(initializeRequest.protocolVersion())) {
462+
this.responseError(response, HttpServletResponse.SC_BAD_REQUEST, McpError
463+
.builder(McpSchema.ErrorCodes.INVALID_PARAMS)
464+
.message("MCP-Protocol-Version header '" + headerVersion
465+
+ "' does not match body protocolVersion '" + initializeRequest.protocolVersion() + "'")
466+
.build());
467+
return;
468+
}
469+
453470
McpStreamableServerSession.McpStreamableServerSessionInit init = this.sessionFactory
454471
.startSession(initializeRequest);
455472
this.sessions.put(init.session().getId(), init.session());

0 commit comments

Comments
 (0)