Skip to content

Commit 5f16abf

Browse files
authored
Merge pull request #244 from koic/add_content_embedded_resource
Add `Content::EmbeddedResource` class for embedded resource content type
2 parents 60f6935 + ba7a08a commit 5f16abf

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
@@ -42,5 +42,18 @@ def to_h
4242
{ data: data, mimeType: mime_type, annotations: annotations, type: "audio" }.compact
4343
end
4444
end
45+
46+
class EmbeddedResource
47+
attr_reader :resource, :annotations
48+
49+
def initialize(resource, annotations: nil)
50+
@resource = resource
51+
@annotations = annotations
52+
end
53+
54+
def to_h
55+
{ resource: resource.to_h, annotations: annotations, type: "resource" }.compact
56+
end
57+
end
4558
end
4659
end

test/mcp/content_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,44 @@ class AudioTest < ActiveSupport::TestCase
5454
refute result.key?(:annotations)
5555
end
5656
end
57+
58+
class EmbeddedResourceTest < ActiveSupport::TestCase
59+
test "#to_h returns correct format per MCP spec" do
60+
resource = Object.new
61+
def resource.to_h
62+
{ uri: "test://example", mimeType: "text/plain", text: "content" }
63+
end
64+
65+
embedded = EmbeddedResource.new(resource)
66+
result = embedded.to_h
67+
68+
assert_equal "resource", result[:type]
69+
assert_equal({ uri: "test://example", mimeType: "text/plain", text: "content" }, result[:resource])
70+
end
71+
72+
test "#to_h with annotations" do
73+
resource = Object.new
74+
def resource.to_h
75+
{ uri: "test://x" }
76+
end
77+
78+
embedded = EmbeddedResource.new(resource, annotations: { role: "data" })
79+
result = embedded.to_h
80+
81+
assert_equal({ role: "data" }, result[:annotations])
82+
end
83+
84+
test "#to_h without annotations omits the key" do
85+
resource = Object.new
86+
def resource.to_h
87+
{ uri: "test://x" }
88+
end
89+
90+
embedded = EmbeddedResource.new(resource)
91+
result = embedded.to_h
92+
93+
refute result.key?(:annotations)
94+
end
95+
end
5796
end
5897
end

0 commit comments

Comments
 (0)