Skip to content

Commit 9480382

Browse files
committed
feat: add NewToolResultJSON
1 parent 9f16336 commit 9480382

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

mcp/utils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,25 @@ func NewToolResultText(text string) *CallToolResult {
253253
}
254254
}
255255

256+
// NewToolResultJSON creates a new CallToolResult with a JSON content.
257+
// This function panics if the JSON marshalling fails.
258+
func NewToolResultJSON[T any](data T) *CallToolResult {
259+
b, err := json.Marshal(data)
260+
if err != nil {
261+
panic("unable to marshal JSON")
262+
}
263+
264+
return &CallToolResult{
265+
Content: []Content{
266+
TextContent{
267+
Type: ContentTypeText,
268+
Text: string(b),
269+
},
270+
},
271+
StructuredContent: data,
272+
}
273+
}
274+
256275
// NewToolResultStructured creates a new CallToolResult with structured content.
257276
// It includes both the structured content and a text representation for backward compatibility.
258277
func NewToolResultStructured(structured any, fallbackText string) *CallToolResult {

0 commit comments

Comments
 (0)