Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions mcp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,24 @@ func NewToolResultText(text string) *CallToolResult {
}
}

// NewToolResultJSON creates a new CallToolResult with a JSON content.
func NewToolResultJSON[T any](data T) (*CallToolResult, error) {
b, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("unable to marshal JSON: %w", err)
}

return &CallToolResult{
Content: []Content{
TextContent{
Type: ContentTypeText,
Text: string(b),
},
},
StructuredContent: data,
}, nil
}

// NewToolResultStructured creates a new CallToolResult with structured content.
// It includes both the structured content and a text representation for backward compatibility.
func NewToolResultStructured(structured any, fallbackText string) *CallToolResult {
Expand Down
4 changes: 2 additions & 2 deletions www/docs/pages/servers/advanced.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func handleCreateUser(ctx context.Context, req mcp.CallToolRequest, input UserCr
Status: "created",
}

return mcp.NewToolResultJSON(output), nil
return mcp.NewToolResultJSON(output)
}
```

Expand Down Expand Up @@ -962,4 +962,4 @@ For complete sampling documentation, see **[Server Sampling Guide](/servers/adva
## Next Steps

- **[Client Development](/clients)** - Learn to build MCP clients
- **[Server Basics](/servers/basics)** - Review fundamental concepts
- **[Server Basics](/servers/basics)** - Review fundamental concepts
4 changes: 2 additions & 2 deletions www/docs/pages/transports/http.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ func handleStatelessTool(ctx context.Context, req mcp.CallToolRequest) (*mcp.Cal
return nil, err
}

return mcp.NewToolResultJSON(result), nil
return mcp.NewToolResultJSON(result)
}

// Use external storage for persistence
Expand Down Expand Up @@ -693,4 +693,4 @@ The headers are automatically populated by the transport layer and are available

- **[In-Process Transport](/transports/inprocess)** - Learn about embedded scenarios
- **[Client Development](/clients)** - Build MCP clients for HTTP transport
- **[Server Basics](/servers/basics)** - Review fundamental server concepts
- **[Server Basics](/servers/basics)** - Review fundamental server concepts