Skip to content

Commit e2d64f4

Browse files
committed
Add Content::EmbeddedResource class for embedded resource content type
The MCP spec defines an embedded resource content type, but the SDK had no corresponding class. Add `Content::EmbeddedResource` that wraps a resource object and serializes it via `to_h`. https://modelcontextprotocol.io/specification/2025-11-25/server/tools#embedded-resources
1 parent 76ab822 commit e2d64f4

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

lib/mcp/content.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,18 @@ def to_h
2828
{ data: data, mimeType: mime_type, annotations: annotations, type: "image" }.compact
2929
end
3030
end
31+
32+
class EmbeddedResource
33+
attr_reader :resource, :annotations
34+
35+
def initialize(resource, annotations: nil)
36+
@resource = resource
37+
@annotations = annotations
38+
end
39+
40+
def to_h
41+
{ resource: resource.to_h, annotations: annotations, type: "resource" }.compact
42+
end
43+
end
3144
end
3245
end

test/mcp/content_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,44 @@ class ImageTest < ActiveSupport::TestCase
2929
refute result.key?(:annotations)
3030
end
3131
end
32+
33+
class EmbeddedResourceTest < ActiveSupport::TestCase
34+
test "#to_h returns correct format per MCP spec" do
35+
resource = Object.new
36+
def resource.to_h
37+
{ uri: "test://example", mimeType: "text/plain", text: "content" }
38+
end
39+
40+
embedded = EmbeddedResource.new(resource)
41+
result = embedded.to_h
42+
43+
assert_equal "resource", result[:type]
44+
assert_equal({ uri: "test://example", mimeType: "text/plain", text: "content" }, result[:resource])
45+
end
46+
47+
test "#to_h with annotations" do
48+
resource = Object.new
49+
def resource.to_h
50+
{ uri: "test://x" }
51+
end
52+
53+
embedded = EmbeddedResource.new(resource, annotations: { role: "data" })
54+
result = embedded.to_h
55+
56+
assert_equal({ role: "data" }, result[:annotations])
57+
end
58+
59+
test "#to_h without annotations omits the key" do
60+
resource = Object.new
61+
def resource.to_h
62+
{ uri: "test://x" }
63+
end
64+
65+
embedded = EmbeddedResource.new(resource)
66+
result = embedded.to_h
67+
68+
refute result.key?(:annotations)
69+
end
70+
end
3271
end
3372
end

0 commit comments

Comments
 (0)