Skip to content

Commit ddb59dd

Browse files
authored
fix: handle nil rawMessage in response parsing functions (#218)
Added checks for nil rawMessage in ParseGetPromptResult, ParseCallToolResult, and ParseReadResourceResult to prevent panics when the tool call response contains an empty result.
1 parent f0a648b commit ddb59dd

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

mcp/utils.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mcp
33
import (
44
"encoding/json"
55
"fmt"
6+
67
"github.com/spf13/cast"
78
)
89

@@ -440,6 +441,10 @@ func ParseContent(contentMap map[string]any) (Content, error) {
440441
}
441442

442443
func ParseGetPromptResult(rawMessage *json.RawMessage) (*GetPromptResult, error) {
444+
if rawMessage == nil {
445+
return nil, fmt.Errorf("response is nil")
446+
}
447+
443448
var jsonContent map[string]any
444449
if err := json.Unmarshal(*rawMessage, &jsonContent); err != nil {
445450
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
@@ -502,6 +507,10 @@ func ParseGetPromptResult(rawMessage *json.RawMessage) (*GetPromptResult, error)
502507
}
503508

504509
func ParseCallToolResult(rawMessage *json.RawMessage) (*CallToolResult, error) {
510+
if rawMessage == nil {
511+
return nil, fmt.Errorf("response is nil")
512+
}
513+
505514
var jsonContent map[string]any
506515
if err := json.Unmarshal(*rawMessage, &jsonContent); err != nil {
507516
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
@@ -580,6 +589,10 @@ func ParseResourceContents(contentMap map[string]any) (ResourceContents, error)
580589
}
581590

582591
func ParseReadResourceResult(rawMessage *json.RawMessage) (*ReadResourceResult, error) {
592+
if rawMessage == nil {
593+
return nil, fmt.Errorf("response is nil")
594+
}
595+
583596
var jsonContent map[string]any
584597
if err := json.Unmarshal(*rawMessage, &jsonContent); err != nil {
585598
return nil, fmt.Errorf("failed to unmarshal response: %w", err)

0 commit comments

Comments
 (0)