internal/mcp: clean up handling of content

Add support for all content types, and formalize the conversion of
content to and from the wire format (as a discriminated union).

Change-Id: I93678ce98c02176a524d2c82425fb1267ad68bf0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/669135
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Auto-Submit: Robert Findley <rfindley@google.com>
This commit is contained in:
Rob Findley 2025-04-30 14:49:52 +00:00 • committed by Gopher Robot
commit bf608eeeae

View file

@ -11,6 +11,7 @@ import (
"github.com/modelcontextprotocol/go-sdk/internal/jsonschema"
"github.com/modelcontextprotocol/go-sdk/internal/protocol"
"github.com/modelcontextprotocol/go-sdk/internal/util"
)
// A ToolHandler handles a call to tools/call.
@ -56,12 +57,12 @@ func MakeTool[TReq any](name, description string, handler func(context.Context,
// rather than returned as jsonrpc2 server errors.
if err != nil {
return &protocol.CallToolResult{
Content: marshalContent([]Content{TextContent{Text: err.Error()}}),
Content: []protocol.Content{TextContent{Text: err.Error()}.ToWire()},
IsError: true,
}, nil
}
res := &protocol.CallToolResult{
Content: marshalContent(content),
Content: util.Apply(content, Content.ToWire),
}
return res, nil
}