Skip to content

Commit 08832c6

Browse files
committed
Fix Resource::Contents#to_h to use correct property names per MCP spec
This PR fixes thr following fields with the MCP specification which requires: - Text resources use `text` and `mimeType` fields - Binary resources use `blob` (base64-encoded) and `mimeType` fields https://modelcontextprotocol.io/specification/2025-11-25/server/resources#resource-contents Fixes #234.
1 parent 6df8e4c commit 08832c6

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

lib/mcp/resource/contents.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(uri:, mime_type: nil)
1111
end
1212

1313
def to_h
14-
{ uri: uri, mime_type: mime_type }.compact
14+
{ uri: uri, mimeType: mime_type }.compact
1515
end
1616
end
1717

@@ -37,7 +37,7 @@ def initialize(data:, uri:, mime_type:)
3737
end
3838

3939
def to_h
40-
super.merge(data: data)
40+
super.merge(blob: data)
4141
end
4242
end
4343
end

test/mcp/resource/contents_test.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
module MCP
6+
class Resource
7+
class ContentsTest < ActiveSupport::TestCase
8+
test "Contents#to_h returns hash with mimeType" do
9+
contents = Resource::Contents.new(
10+
uri: "test://example",
11+
mime_type: "text/plain",
12+
)
13+
14+
result = contents.to_h
15+
16+
assert_equal "test://example", result[:uri]
17+
assert_equal "text/plain", result[:mimeType]
18+
refute result.key?(:mime_type), "Should use camelCase 'mimeType' not snake_case 'mime_type'"
19+
end
20+
21+
test "Contents#to_h omits mimeType when nil" do
22+
contents = Resource::Contents.new(uri: "test://example")
23+
24+
result = contents.to_h
25+
26+
assert_equal({ uri: "test://example" }, result)
27+
refute result.key?(:mimeType)
28+
end
29+
30+
test "TextContents#to_h returns hash with text and mimeType" do
31+
text_contents = Resource::TextContents.new(
32+
uri: "test://text",
33+
mime_type: "text/plain",
34+
text: "Hello, world!",
35+
)
36+
37+
result = text_contents.to_h
38+
39+
assert_equal "test://text", result[:uri]
40+
assert_equal "text/plain", result[:mimeType]
41+
assert_equal "Hello, world!", result[:text]
42+
refute result.key?(:mime_type), "Should use camelCase 'mimeType' not snake_case 'mime_type'"
43+
end
44+
45+
test "BlobContents#to_h returns hash with blob and mimeType" do
46+
blob_contents = Resource::BlobContents.new(
47+
uri: "test://binary",
48+
mime_type: "image/png",
49+
data: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
50+
)
51+
52+
result = blob_contents.to_h
53+
54+
assert_equal "test://binary", result[:uri]
55+
assert_equal "image/png", result[:mimeType]
56+
assert_equal "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==", result[:blob]
57+
refute result.key?(:data), "Should use 'blob' not 'data' per MCP specification"
58+
refute result.key?(:mime_type), "Should use camelCase 'mimeType' not snake_case 'mime_type'"
59+
end
60+
61+
test "BlobContents#to_h omits mimeType when nil" do
62+
blob_contents = Resource::BlobContents.new(
63+
uri: "test://binary",
64+
mime_type: nil,
65+
data: "base64data",
66+
)
67+
68+
result = blob_contents.to_h
69+
70+
assert_equal({ uri: "test://binary", blob: "base64data" }, result)
71+
refute result.key?(:mimeType)
72+
end
73+
end
74+
end
75+
end

0 commit comments

Comments
 (0)