Skip to content

Commit 0334dc0

Browse files
committed
fix: improve error handling
1 parent decdee8 commit 0334dc0

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

mcp/utils.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,18 @@ func NewToolResultStructured(structured any, fallbackText string) *CallToolResul
267267
}
268268
}
269269

270-
// NewToolResultStructuredOnly creates a new CallToolResult with only structured content.
271-
// This is useful when you want to provide structured data without a text fallback.
270+
// NewToolResultStructuredOnly creates a new CallToolResult with structured
271+
// content and creates a JSON string fallback for backwards compatibility.
272+
// This is useful when you want to provide structured data without any specific text fallback.
272273
func NewToolResultStructuredOnly(structured any) *CallToolResult {
274+
var fallbackText string
273275
// Convert to JSON string for backward compatibility
274-
jsonBytes, _ := json.Marshal(structured)
275-
fallbackText := string(jsonBytes)
276+
jsonBytes, err := json.Marshal(structured)
277+
if err != nil {
278+
fallbackText = fmt.Sprintf("Error serializing structured content: %v", err)
279+
} else {
280+
fallbackText = string(jsonBytes)
281+
}
276282

277283
return &CallToolResult{
278284
Content: []Content{
@@ -477,7 +483,6 @@ func FormatNumberResult(value float64) *CallToolResult {
477483
return NewToolResultText(fmt.Sprintf("%.2f", value))
478484
}
479485

480-
481486
func ExtractString(data map[string]any, key string) string {
482487
if value, ok := data[key]; ok {
483488
if str, ok := value.(string); ok {

0 commit comments

Comments
 (0)