Skip to content

Commit 41b54dc

Browse files
mcp: use omitzero json tag for byte array field in ResourceContents, instead of omitempty
/mcp/content.go: use omitzero json tag for byte array, instead of omitempty earlier. This change removes requirement of custom MarshalJSON Fixes #781
1 parent 6a19c95 commit 41b54dc

1 file changed

Lines changed: 1 addition & 31 deletions

File tree

mcp/content.go

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package mcp
99

1010
import (
1111
"encoding/json"
12-
"errors"
1312
"fmt"
1413
)
1514

@@ -189,39 +188,10 @@ type ResourceContents struct {
189188
URI string `json:"uri"`
190189
MIMEType string `json:"mimeType,omitempty"`
191190
Text string `json:"text,omitempty"`
192-
Blob []byte `json:"blob,omitempty"`
191+
Blob []byte `json:"blob,omitzero"`
193192
Meta Meta `json:"_meta,omitempty"`
194193
}
195194

196-
func (r *ResourceContents) MarshalJSON() ([]byte, error) {
197-
// If we could assume Go 1.24, we could use omitzero for Blob and avoid this method.
198-
if r.URI == "" {
199-
return nil, errors.New("ResourceContents missing URI")
200-
}
201-
if r.Blob == nil {
202-
// Text. Marshal normally.
203-
type wireResourceContents ResourceContents // (lacks MarshalJSON method)
204-
return json.Marshal((wireResourceContents)(*r))
205-
}
206-
// Blob.
207-
if r.Text != "" {
208-
return nil, errors.New("ResourceContents has non-zero Text and Blob fields")
209-
}
210-
// r.Blob may be the empty slice, so marshal with an alternative definition.
211-
br := struct {
212-
URI string `json:"uri,omitempty"`
213-
MIMEType string `json:"mimeType,omitempty"`
214-
Blob []byte `json:"blob"`
215-
Meta Meta `json:"_meta,omitempty"`
216-
}{
217-
URI: r.URI,
218-
MIMEType: r.MIMEType,
219-
Blob: r.Blob,
220-
Meta: r.Meta,
221-
}
222-
return json.Marshal(br)
223-
}
224-
225195
// wireContent is the wire format for content.
226196
// It represents the protocol types TextContent, ImageContent, AudioContent,
227197
// ResourceLink, and EmbeddedResource.

0 commit comments

Comments
 (0)