Merge pull request #244 from koic/add_content_embedded_resource

Add `Content::EmbeddedResource` class for embedded resource content type
This commit is contained in:
Koichi ITO 2026-03-02 01:35:37 +09:00 • committed by GitHub
commit 5f16abfd96
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 52 additions and 0 deletions

View file

@ -42,5 +42,18 @@ module MCP
{ data: data, mimeType: mime_type, annotations: annotations, type: "audio" }.compact
end
end
class EmbeddedResource
attr_reader :resource, :annotations
def initialize(resource, annotations: nil)
@resource = resource
@annotations = annotations
end
def to_h
{ resource: resource.to_h, annotations: annotations, type: "resource" }.compact
end
end
end
end

View file

@ -54,5 +54,44 @@ module MCP
refute result.key?(:annotations)
end
end
class EmbeddedResourceTest < ActiveSupport::TestCase
test "#to_h returns correct format per MCP spec" do
resource = Object.new
def resource.to_h
{ uri: "test://example", mimeType: "text/plain", text: "content" }
end
embedded = EmbeddedResource.new(resource)
result = embedded.to_h
assert_equal "resource", result[:type]
assert_equal({ uri: "test://example", mimeType: "text/plain", text: "content" }, result[:resource])
end
test "#to_h with annotations" do
resource = Object.new
def resource.to_h
{ uri: "test://x" }
end
embedded = EmbeddedResource.new(resource, annotations: { role: "data" })
result = embedded.to_h
assert_equal({ role: "data" }, result[:annotations])
end
test "#to_h without annotations omits the key" do
resource = Object.new
def resource.to_h
{ uri: "test://x" }
end
embedded = EmbeddedResource.new(resource)
result = embedded.to_h
refute result.key?(:annotations)
end
end
end
end