Skip to content

Commit 5d4fb64

Browse files
andigclaude
andcommitted
fix: prevent panic from unsafe type assertion in example server
Replace unsafe type assertion result.Content.(mcp.TextContent).Text with safe type checking to handle cases where Content might not be a TextContent struct. Now gracefully handles different content types without panicking. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 204b273 commit 5d4fb64

File tree

1 file changed

+9
-1
lines changed
  • examples/sampling_http_server

1 file changed

+9
-1
lines changed

examples/sampling_http_server/main.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,20 @@ func main() {
8080
}, nil
8181
}
8282

83+
// Extract response text safely
84+
var responseText string
85+
if textContent, ok := result.Content.(mcp.TextContent); ok {
86+
responseText = textContent.Text
87+
} else {
88+
responseText = fmt.Sprintf("%v", result.Content)
89+
}
90+
8391
// Return the LLM response
8492
return &mcp.CallToolResult{
8593
Content: []mcp.Content{
8694
mcp.TextContent{
8795
Type: "text",
88-
Text: fmt.Sprintf("LLM Response (model: %s): %s", result.Model, result.Content.(mcp.TextContent).Text),
96+
Text: fmt.Sprintf("LLM Response (model: %s): %s", result.Model, responseText),
8997
},
9098
},
9199
}, nil

0 commit comments

Comments
 (0)