Skip to content

Commit d783f8f

Browse files
author
Zheng Li
committed
create ParseCallToolResult utility method
1 parent a1f251a commit d783f8f

File tree

3 files changed

+53
-54
lines changed

3 files changed

+53
-54
lines changed

client/sse.go

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -491,54 +491,7 @@ func (c *SSEMCPClient) CallTool(
491491
return nil, err
492492
}
493493

494-
var jsonContent map[string]any
495-
if err := json.Unmarshal(*response, &jsonContent); err != nil {
496-
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
497-
}
498-
499-
var result mcp.CallToolResult
500-
501-
meta, ok := jsonContent["_meta"]
502-
if ok {
503-
if metaMap, ok := meta.(map[string]any); ok {
504-
result.Meta = metaMap
505-
}
506-
}
507-
508-
isError, ok := jsonContent["isError"]
509-
if ok {
510-
if isErrorBool, ok := isError.(bool); ok {
511-
result.IsError = isErrorBool
512-
}
513-
}
514-
515-
contents, ok := jsonContent["content"]
516-
if !ok {
517-
return nil, fmt.Errorf("content is missing")
518-
}
519-
520-
contentArr, ok := contents.([]any)
521-
if !ok {
522-
return nil, fmt.Errorf("content is not an array")
523-
}
524-
525-
for _, content := range contentArr {
526-
// Extract content
527-
contentMap, ok := content.(map[string]any)
528-
if !ok {
529-
return nil, fmt.Errorf("content is not an object")
530-
}
531-
532-
// Process content
533-
content, err := mcp.ParseContent(contentMap)
534-
if err != nil {
535-
return nil, err
536-
}
537-
538-
result.Content = append(result.Content, content)
539-
}
540-
541-
return &result, nil
494+
return mcp.ParseCallToolResult(response)
542495
}
543496

544497
func (c *SSEMCPClient) SetLevel(

client/stdio.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,7 @@ func (c *StdioMCPClient) CallTool(
417417
return nil, err
418418
}
419419

420-
var result mcp.CallToolResult
421-
if err := json.Unmarshal(*response, &result); err != nil {
422-
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
423-
}
424-
425-
return &result, nil
420+
return mcp.ParseCallToolResult(response)
426421
}
427422

428423
func (c *StdioMCPClient) SetLevel(

mcp/utils.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,54 @@ func ParseGetPromptResult(rawMessage *json.RawMessage) (*GetPromptResult, error)
492492

493493
return &result, nil
494494
}
495+
496+
func ParseCallToolResult(rawMessage *json.RawMessage) (*CallToolResult, error) {
497+
var jsonContent map[string]any
498+
if err := json.Unmarshal(*rawMessage, &jsonContent); err != nil {
499+
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
500+
}
501+
502+
var result CallToolResult
503+
504+
meta, ok := jsonContent["_meta"]
505+
if ok {
506+
if metaMap, ok := meta.(map[string]any); ok {
507+
result.Meta = metaMap
508+
}
509+
}
510+
511+
isError, ok := jsonContent["isError"]
512+
if ok {
513+
if isErrorBool, ok := isError.(bool); ok {
514+
result.IsError = isErrorBool
515+
}
516+
}
517+
518+
contents, ok := jsonContent["content"]
519+
if !ok {
520+
return nil, fmt.Errorf("content is missing")
521+
}
522+
523+
contentArr, ok := contents.([]any)
524+
if !ok {
525+
return nil, fmt.Errorf("content is not an array")
526+
}
527+
528+
for _, content := range contentArr {
529+
// Extract content
530+
contentMap, ok := content.(map[string]any)
531+
if !ok {
532+
return nil, fmt.Errorf("content is not an object")
533+
}
534+
535+
// Process content
536+
content, err := ParseContent(contentMap)
537+
if err != nil {
538+
return nil, err
539+
}
540+
541+
result.Content = append(result.Content, content)
542+
}
543+
544+
return &result, nil
545+
}

0 commit comments

Comments
 (0)