Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/mcp/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,18 @@ def to_h
{ data: data, mimeType: mime_type, annotations: annotations, type: "image" }.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
39 changes: 39 additions & 0 deletions test/mcp/content_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,44 @@ class ImageTest < ActiveSupport::TestCase
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