-
Notifications
You must be signed in to change notification settings - Fork 722
fix: Prevent panic in parsing functions for null results #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
""" WalkthroughThe update introduces nil-pointer checks at the beginning of three parsing functions within Changes
Assessment against linked issues
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
It seems that transport will never return nil-result and nil-error at the same time. also see spec: https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#stdio
|
1d47768 to
d0453d8
Compare
Thank you for the feedback and for pointing out the MCP specification. I agree that, according to the MCP spec (https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#stdio), the transport layer should only output valid MCP messages to stdout, meaning a nil rawMessage should not occur in a compliant implementation. |
how about “response is nil” instead of “rawMessage is nil”, because error message below use “response/failed to unmarshal response” |
Added checks for nil rawMessage in ParseGetPromptResult, ParseCallToolResult, and ParseReadResourceResult to prevent panics when the tool call response contains an empty result.
d0453d8 to
6510b4e
Compare
Thank you for your suggestion! I agree—using “response is nil” makes the error message more consistent with the existing ones like “failed to unmarshal response.” I’ve updated the code accordingly. |
Added checks for nil rawMessage in ParseGetPromptResult, ParseCallToolResult, and ParseReadResourceResult to prevent panics when the tool call response contains an empty result.
This PR addresses a potential panic in ParseGetPromptResult, ParseCallToolResult, and ParseReadResourceResult. The panic could occur if the input rawMessage pointer is nil, as json. Unmarshal would attempt to dereference this nil pointer.
The functions now check for a nil rawMessage at the beginning. Instead of proceeding (which would lead to a panic) or returning (nil, nil), they now return nil for the result object and a specific error (fmt. Errorf("rawMessage is nil")).
Reasoning:
While a nil rawMessage could conceptually represent a null JSON value, in the context of the expected protocol (e.g., Model Context Protocol), receiving a nil message pointer without a preceding transport-level error is often indicative of an upstream issue or a protocol violation.
Returning an explicit error in this scenario, rather than (nil, nil), provides clearer feedback about unexpected input and helps surface potential problems in the data source or transport layer, rather than silently treating it as a valid "empty" response. This approach prioritizes stricter input validation and better error visibility.
Fixes #207
Summary by CodeRabbit
Bug Fixes
Enhanced the stability of message parsing functions (ParseGetPromptResult, ParseCallToolResult, ParseReadResourceResult) by adding checks for nil input messages, preventing potential crashes and returning specific errors instead.
Summary by CodeRabbit