fix(mcp): protocol version negotiation (7.0)
#24766
Merged
+85
−77
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.
Note: This is a backport of #24717 to
7.0.This PR:
200 OKresponse with server-supported protocol version) and strictMCP-Protocol-Versionheader validation for subsequent requests (i.e.400 Bad Requestresponses for invalid/unsupported protocol versions).McpExceptionin favor ofMcpErrorin SDK.This ensures proper error handling according to the Model Context Protocol specification and fixes compatibility issues with MCP clients like MCP Inspector and fastmcp.
Description
Spec Compliance
Per 2025-06-18:
File changes
McpException.java
Replaced with standard McpError from MCP SDK
McpRestResource.java
Protocol Version Handling:
MCP-Protocol-Versionheader on all requests (when present).400 Bad Requestfor invalid/unsupported header values per spec."2025-03-26"when header is missing per spec backwards compatibility requirement.TODOfor future session management to retrieve negotiated version from session state.Error Handling:
McpErrorspecifically and return400 Bad Requestwith proper JSON-RPC error structure.McpService.java
Version Negotiation:
supportedVersionsto protected static final for proper access from REST resource.FALLBACK_MCP_VERSIONconstant ("2025-03-26") per spec requirement.Error Handling:
McpExceptionto throwsMcpError.McpError.builder().METHOD_NOT_FOUND,RESOURCE_NOT_FOUND,INVALID_PARAMS.API Changes:
handle()method acceptingprotocolVersionparameter for future multi-version support.TODOindicating where version-specific handlers could be implemented.McpServiceTest.java
testInitializeWithUnsupportedVersion()to expect successful fallback instead of exception.McpExceptiontoMcpError.Motivation and Context
Fixes #24701
The previous implementation had two main issues:
400 Bad Requesterror. Per the MCP specification, servers must silently fall back to a supported version and return200 OK, allowing the client to decide whether to continue.McpExceptionclass instead of the standardMcpErrortypes provided by the MCP SDK, leading to inconsistent error codes and messages.How Has This Been Tested?
Screenshots (if appropriate):
Types of changes
Checklist: