Skip to content

Commit 4cd4883

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 5b04089 commit 4cd4883

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-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, mime_type: 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+
{ type: "resource", resource: resource.to_h, annotations: annotations }.compact
42+
end
43+
end
3144
end
3245
end

test/mcp/content_test.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
module MCP
6+
module Content
7+
class EmbeddedResourceTest < ActiveSupport::TestCase
8+
test "#to_h returns correct format per MCP spec" do
9+
resource = Object.new
10+
def resource.to_h
11+
{ uri: "test://example", mimeType: "text/plain", text: "content" }
12+
end
13+
14+
embedded = EmbeddedResource.new(resource)
15+
result = embedded.to_h
16+
17+
assert_equal "resource", result[:type]
18+
assert_equal({ uri: "test://example", mimeType: "text/plain", text: "content" }, result[:resource])
19+
end
20+
21+
test "#to_h with annotations" do
22+
resource = Object.new
23+
def resource.to_h
24+
{ uri: "test://x" }
25+
end
26+
27+
embedded = EmbeddedResource.new(resource, annotations: { role: "data" })
28+
result = embedded.to_h
29+
30+
assert_equal({ role: "data" }, result[:annotations])
31+
end
32+
33+
test "#to_h without annotations omits the key" do
34+
resource = Object.new
35+
def resource.to_h
36+
{ uri: "test://x" }
37+
end
38+
39+
embedded = EmbeddedResource.new(resource)
40+
result = embedded.to_h
41+
42+
refute result.key?(:annotations)
43+
end
44+
end
45+
end
46+
end

0 commit comments

Comments
 (0)