fix(mcp): JSON-RPC spec compliance — flexible id, correct notification format#685
fix(mcp): JSON-RPC spec compliance — flexible id, correct notification format#685nick-stebbings wants to merge 2 commits intonearai:mainfrom
Conversation
…n format - McpRequest.id is now Option<u64> with skip_serializing_if, so notifications omit the id field as required by JSON-RPC 2.0 spec. Previously sent id: 0 which violates the spec. - McpResponse.id uses flexible deserialization that accepts number, string, or null — fixes interop with non-standard MCP servers that return string ids or missing id fields on error responses. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the MCP protocol types to achieve better JSON-RPC 2.0 specification compliance. It addresses two key areas: correctly handling the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request updates the MCP protocol types to be more compliant with the JSON-RPC 2.0 specification, specifically by making McpRequest.id and McpResponse.id optional and handling flexible id deserialization. However, the current implementation introduces a security regression, as highlighted by a rule regarding valid JSON responses: making the id field optional in the McpResponse struct can cause clients to incorrectly treat JSON-RPC notifications as responses. This is particularly problematic for SSE (Server-Sent Events) transport, potentially leading to Denial of Service or message spoofing. It is recommended to require the id field to be present in responses while maintaining the flexible parsing logic. Furthermore, as per the rule on refactoring and test coverage, there are broken unit tests that need to be fixed, and new tests are required to cover the new functionality, including deserialize_flexible_id with various inputs and verifying that initialized_notification serializes without an id field.
…est assertions - Remove #[serde(default)] from McpResponse.id so notifications (no id field) don't incorrectly parse as responses — prevents DoS/spoofing via SSE - Update test assertions to use Some(value) after id became Option<u64> Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Updated: Removed |
Summary
Two JSON-RPC 2.0 spec compliance fixes in the MCP protocol types:
Notifications must not have an
idfield —McpRequest.idchanged fromu64toOption<u64>withskip_serializing_if.initialized_notification()now setsid: Noneinstead ofid: 0, which violated the JSON-RPC 2.0 spec and caused some compliant MCP servers to reject the notification.Flexible response id deserialization — Some MCP servers return string or null ids instead of numeric ones (especially on error responses). Added
deserialize_flexible_idthat handles number, string (parsed to u64), or null. Non-parseable string ids resolve toNone, which is acceptable since id matching is not critical for notification flows.Test plan
initialized_notification()serializes without anidfieldMcpResponsedeserializes correctly with numeric, string, and null idsid: 0notifications🤖 Generated with Claude Code