mcp: fix CallToolResultFor[T] unmarshaling (#46)

Add an UnmarshalJSON method for that type.

Add a test.
This commit is contained in:
Jonathan Amsterdam 2025-06-25 16:45:58 -04:00 • committed by GitHub
commit 600ba61efe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -129,6 +129,25 @@ type CallToolResultFor[Out any] struct {
StructuredContent Out `json:"structuredContent,omitempty"`
}
// UnmarshalJSON handles the unmarshalling of content into the Content
// interface.
func (x *CallToolResultFor[Out]) UnmarshalJSON(data []byte) error {
type res CallToolResultFor[Out] // avoid recursion
var wire struct {
res
Content []*wireContent `json:"content"`
}
if err := json.Unmarshal(data, &wire); err != nil {
return err
}
var err error
if wire.res.Content, err = contentsFromWire(wire.Content, nil); err != nil {
return err
}
*x = CallToolResultFor[Out](wire.res)
return nil
}
type CancelledParams struct {
// This property is reserved by the protocol to allow clients and servers to
// attach additional metadata to their responses.