Skip to content

Commit 7243045

Browse files
Fix ImageContent data encoding - use raw bytes not double-encoded base64
The MCP SDK handles base64 encoding during JSON marshaling, so the Data field should contain raw binary data, not pre-encoded base64 strings. This prevents double-encoding where base64 strings were being encoded again. - Updated NewToolResultResourceWithFlag to pass raw binary data - Updated test to verify raw binary data instead of base64 string - Removed unused encoding/base64 imports Co-authored-by: SamMorrowDrums <4811358+SamMorrowDrums@users.noreply.github.com>
1 parent 9f93e65 commit 7243045

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

pkg/utils/result.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package utils //nolint:revive //TODO: figure out a better name for this package
22

33
import (
4-
"encoding/base64"
5-
64
"github.com/modelcontextprotocol/go-sdk/mcp"
75
)
86

@@ -79,8 +77,9 @@ func NewToolResultResourceWithFlag(message string, contents *mcp.ResourceContent
7977
}
8078
case len(contents.Blob) > 0:
8179
// Binary content - use ImageContent with base64 data
80+
// Note: MCP SDK will handle base64 encoding during JSON marshaling
8281
content = &mcp.ImageContent{
83-
Data: []byte(base64.StdEncoding.EncodeToString(contents.Blob)),
82+
Data: contents.Blob,
8483
MIMEType: contents.MIMEType,
8584
Meta: mcp.Meta{
8685
"uri": contents.URI,

pkg/utils/result_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package utils //nolint:revive //TODO: figure out a better name for this package
22

33
import (
4-
"encoding/base64"
54
"testing"
65

76
"github.com/modelcontextprotocol/go-sdk/mcp"
@@ -90,9 +89,8 @@ func TestNewToolResultResourceWithFlag_EnabledFlag_BinaryContent(t *testing.T) {
9089
imageContent, ok := result.Content[1].(*mcp.ImageContent)
9190
require.True(t, ok, "Expected ImageContent but got %T", result.Content[1])
9291

93-
// Data should be base64 encoded
94-
expectedBase64 := base64.StdEncoding.EncodeToString(binaryData)
95-
assert.Equal(t, []byte(expectedBase64), imageContent.Data)
92+
// Data should be raw binary (SDK handles base64 encoding during JSON marshaling)
93+
assert.Equal(t, binaryData, imageContent.Data)
9694
assert.Equal(t, "image/png", imageContent.MIMEType)
9795
assert.NotNil(t, imageContent.Meta)
9896
assert.Equal(t, "test://image.png", imageContent.Meta["uri"])

0 commit comments

Comments
 (0)