fix: handle nil Params in ParseToolArguments#2172
Merged
Conversation
CallToolRequest.Params is *CallToolParamsRaw (a pointer type), so it can be nil when a request is constructed without explicit initialization. ParseToolArguments now checks req.Params != nil before accessing req.Params.Arguments to prevent nil pointer dereference. Also fixes test cases to properly initialize Params via struct literals and adds a dedicated nil-params test case. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a nil-pointer panic in MCP tool argument parsing by safely handling CallToolRequest.Params being nil, and updates/adds tests to reflect correct request construction.
Changes:
- Guarded
ParseToolArguments()againstreq.Params == nilbefore accessingArguments. - Updated tests to initialize
CallToolRequest.Paramsvia struct literals instead of mutating a nil pointer field. - Added a dedicated test case for “nil params returns empty map”.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/mcp/tool_result.go | Adds a nil check for req.Params in ParseToolArguments to prevent panics. |
| internal/mcp/tool_result_test.go | Adjusts request construction in tests and adds coverage for nil Params. |
Comments suppressed due to low confidence (1)
internal/mcp/tool_result.go:117
- ParseToolArguments can still return a nil map when Arguments is present but set to JSON
null(json.Unmarshal succeeds and leaves toolArgs nil). This is inconsistent with the “no arguments → empty map” behavior and with callers that may assume a non-nil args object; consider normalizing toolArgs to an empty map after unmarshal when it’s nil (and ideally add a test forarguments: null).
func ParseToolArguments(req *sdk.CallToolRequest) (map[string]interface{}, error) {
var toolArgs map[string]interface{}
if req.Params != nil && req.Params.Arguments != nil {
if err := json.Unmarshal(req.Params.Arguments, &toolArgs); err != nil {
return nil, fmt.Errorf("failed to parse arguments: %w", err)
}
} else {
// No arguments provided, use empty map
toolArgs = make(map[string]interface{})
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
This was referenced Mar 19, 2026
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.
Problem
CallToolRequestisServerRequest[*CallToolParamsRaw]—Paramsis a pointer type that defaults tonilwhen not explicitly initialized.ParseToolArgumentsaccessedreq.Params.Argumentswithout checking for nil, causing a nil pointer dereference panic.Fix
req.ParamsinParseToolArguments()before accessingreq.Params.ArgumentsParamsvia struct literals instead of assigning to a nil pointer fieldnil params returns empty maptest caseTesting
All tests pass (
make agent-finished✅).