Skip to content

mcpcompat drops entire tool/prompt result when it contains an embedded resource #170

Description

@jhrozek

Summary

mcpcompat.EmbeddedResource has a Resource ResourceContents field where ResourceContents is a non-empty interface, and EmbeddedResource has no custom UnmarshalJSON. So json.Unmarshal of any "type":"resource" content item errors:

json: cannot unmarshal object into Go struct field EmbeddedResource.resource of type mcp.ResourceContents

Because CallToolResult.UnmarshalJSON (and the client's convertResult) propagate that error, any tool result — or prompt message — containing an embedded resource is lost in its entirety, including any sibling text/image parts in the same content array.

Impact

Discovered from a ToolHive vMCP MCP-conformance run (the mcp-go → mcpcompat swap). Two conformance scenarios regressed:

  • tools-call-embedded-resource → "No resource content found"
  • tools-call-mixed-content → "Expected multiple content items; Missing image content; Missing resource content"

The prompts/get path is affected too, since convertGetPromptResult also decodes content via mcp.UnmarshalContent.

Call chain

  1. client.CallTool converts the go-sdk result via the generic convertResultjsonConvert.
  2. That runs CallToolResult.UnmarshalJSON, which calls UnmarshalContent per item.
  3. UnmarshalContent, case ContentTypeResource, does json.Unmarshal(data, &EmbeddedResource{}) — stdlib cannot populate the ResourceContents interface field → error.
  4. The error aborts the whole decode; convertResult discards the partially-decoded result.

ReadResource and GetPrompt were hand-written to avoid this exact trap (convertReadResourceResult maps concrete text/blob types explicitly); CallTool was left on the generic path.

Fix

Add a custom UnmarshalJSON to EmbeddedResource that resolves the polymorphic Resource field to a concrete TextResourceContents/BlobResourceContents (blob-precedence, mirroring convertReadResourceResult). UnmarshalContent is the single chokepoint backing both tools/call and prompts/get.

Repro

wire := []byte(`{"type":"resource","resource":{"uri":"file:///x.txt","mimeType":"text/plain","text":"body"}}`)
_, err := mcp.UnmarshalContent(wire)
// err: json: cannot unmarshal object into Go struct field EmbeddedResource.resource of type mcp.ResourceContents

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triageIssue needs initial triage by a maintainer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions