Map text DataContent to an MCP text resource, not a binary blob#541
Merged
Conversation
When exposing an agent as an MCP tool, agentContentToMCPContent converts a DataContent to an mcp.EmbeddedResource. Image and audio media types get dedicated content types, but every other media type — including text/* — fell through to the default case and was written to Resource.Blob as base64 binary. The reverse mapping (mcpContentToAgentContent) already branches on Resource.Text to reconstruct text, so the writer never producing Text is an asymmetry: a text DataContent round-tripped through MCP became an unreadable binary blob, and MCP clients reading the resource would treat text as binary. Add a text case that populates Resource.Text with the decoded string. Binary payloads (e.g. application/octet-stream) still use Blob.
There was a problem hiding this comment.
Pull request overview
This pull request fixes an MCP content-mapping asymmetry when exposing an agent as an MCP tool: message.DataContent with text/* media types is now emitted as an MCP embedded resource with Resource.Text (readable) instead of Resource.Blob (binary), matching how the reverse mapping already interprets text resources.
Changes:
- Add a
textmedia-type case inagentContentToMCPContentthat setsmcp.ResourceContents.Textinstead ofBlob. - Add a black-box regression test ensuring
text/plainDataContentis returned as an embedded text resource (Text set, Blob empty).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tool/mcptool/mcp.go | Emits text/* DataContent as an MCP embedded resource using Resource.Text rather than Resource.Blob. |
| tool/mcptool/mcp_test.go | Adds a regression test covering text DataContent → embedded text resource behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| return &mcp.EmbeddedResource{Resource: &mcp.ResourceContents{ | ||
| URI: contentValue.Name, | ||
| MIMEType: contentValue.MediaType, | ||
| Text: string(data), |
Member
|
@PratikDhanave merge conflict |
Address review feedback: a text-typed DataContent whose decoded bytes are not valid UTF-8 would be corrupted if placed in ResourceContents.Text (JSON transport replaces invalid sequences). Emit a binary Blob resource in that case. Adds a regression test.
# Conflicts: # tool/mcptool/mcp.go
qmuntal
approved these changes
Jul 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When exposing an agent as an MCP tool,
agentContentToMCPContentconverts amessage.DataContentinto anmcp.EmbeddedResource. Image and audio media types get dedicated content types, but every other media type — includingtext/*— fell through to the default case and was written toResource.Blobas base64 binary:mcp.ResourceContentsdeliberately separatesText(readable) fromBlob(base64 binary). The reverse mapping already relies on this —mcpContentToAgentContentbranches onResource.Text != ""to reconstruct aTextContent. So the writer never producingTextis an asymmetry: a textDataContentround-tripped through MCP becomes an unreadable binary blob, and MCP clients reading the resource treat text as binary.Fix
Add a
textcase that populatesResource.Textwith the decoded string. Binary payloads (e.g.application/octet-stream) still useBlob— unchanged.Test
TestAddToolReturnsTextDataAsTextResource(black-box) returns atext/plainDataContentfrom a tool and asserts the resultingEmbeddedResourcecarries the text inResource.Text(and notBlob). Fails onmain(Text = "", Blob populated), passes with the fix. The existingTestAddToolReturnsBinaryDataAsEmbeddedResource(octet-stream → Blob) is unchanged.