go-sdk icon indicating copy to clipboard operation
go-sdk copied to clipboard

Bug: Marshaling Content omits medadata

Open IAmSurajBobade opened this issue 2 months ago • 3 comments

Describe the bug Content interface has wireContent method - which marshals bytes to struct implementing Content interface.

Meta property in wireContent, has JSON tag _meta.

type wireContent struct {
        ....
	Meta        Meta              `json:"_meta,omitempty"`
}

however, structs do not have json tag defined, so it expects meta, instead of _meta from byte array.

type ResourceLink struct {
	...
	Meta        Meta
}

We should add json tag for Meta in all structs implementing Content interface. So ResourceLink would become

type ResourceLink struct {
	...
	Meta        Meta              `json:"_meta,omitempty"`
}

IAmSurajBobade avatar Nov 08 '25 15:11 IAmSurajBobade

Looks like @findleyr has already added relevant TODO comment https://github.com/modelcontextprotocol/go-sdk/blob/main/mcp/content.go#L5.

IAmSurajBobade avatar Nov 08 '25 15:11 IAmSurajBobade

Sorry, @IAmSurajBobade, I don't understand. The content types have custom MarshalJSON methods, which appear to preserve metadata. Is this not working?

findleyr avatar Nov 10 '25 22:11 findleyr

@findleyr, updated description of the issue. This is the issue, due to which https://github.com/modelcontextprotocol/go-sdk/pull/570#discussion_r2515018632 is not working.

IAmSurajBobade avatar Nov 11 '25 18:11 IAmSurajBobade

@IAmSurajBobade this is not a bug: the way this works is that each struct that has a Content field has a custom UnmarshalJSON method, which unmarshals using contentsFromWire.

It is not expected that marshaling directly into content types works as expected. The custom marshalling at the top level is required for selecting the correct content type.

findleyr avatar Nov 12 '25 22:11 findleyr