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
6 changes: 4 additions & 2 deletions lib/mcp/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@

module MCP
class Resource
attr_reader :uri, :name, :title, :description, :icons, :mime_type, :size, :meta
attr_reader :uri, :name, :title, :description, :icons, :mime_type, :annotations, :size, :meta

def initialize(uri:, name:, title: nil, description: nil, icons: [], mime_type: nil, size: nil, meta: nil)
def initialize(uri:, name:, title: nil, description: nil, icons: [], mime_type: nil, annotations: nil, size: nil, meta: nil)
@uri = uri
@name = name
@title = title
@description = description
@icons = icons
@mime_type = mime_type
@annotations = annotations
@size = size
@meta = meta
end
Expand All @@ -26,6 +27,7 @@ def to_h
description: description,
icons: icons&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
mimeType: mime_type,
annotations: annotations&.to_h,
size: size,
_meta: meta,
}.compact
Expand Down
6 changes: 4 additions & 2 deletions lib/mcp/resource_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

module MCP
class ResourceTemplate
attr_reader :uri_template, :name, :title, :description, :icons, :mime_type, :meta
attr_reader :uri_template, :name, :title, :description, :icons, :mime_type, :annotations, :meta

def initialize(uri_template:, name:, title: nil, description: nil, icons: [], mime_type: nil, meta: nil)
def initialize(uri_template:, name:, title: nil, description: nil, icons: [], mime_type: nil, annotations: nil, meta: nil)
@uri_template = uri_template
@name = name
@title = title
@description = description
@icons = icons
@mime_type = mime_type
@annotations = annotations
@meta = meta
end

Expand All @@ -22,6 +23,7 @@ def to_h
description: description,
icons: icons&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
mimeType: mime_type,
annotations: annotations&.to_h,
_meta: meta,
}.compact
end
Expand Down
18 changes: 18 additions & 0 deletions test/mcp/resource_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,23 @@ class ResourceTemplateTest < ActiveSupport::TestCase

assert_equal meta, resource_template.to_h[:_meta]
end

test "#to_h omits annotations when nil" do
resource_template = ResourceTemplate.new(uri_template: "file:///{path}", name: "template_without_annotations")

refute resource_template.to_h.key?(:annotations)
end

test "#to_h includes annotations when present" do
annotations = Annotations.new(audience: ["user"], priority: 0.8, last_modified: "2025-01-12T15:00:58Z")
resource_template = ResourceTemplate.new(
uri_template: "file:///{path}",
name: "template_with_annotations",
annotations: annotations,
)

expected = { audience: ["user"], priority: 0.8, lastModified: "2025-01-12T15:00:58Z" }
assert_equal expected, resource_template.to_h[:annotations]
end
end
end
14 changes: 14 additions & 0 deletions test/mcp/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,19 @@ class ResourceTest < ActiveSupport::TestCase

assert_equal 0, resource.to_h[:size]
end

test "#to_h omits annotations when nil" do
resource = Resource.new(uri: "file:///test.txt", name: "resource_without_annotations")

refute resource.to_h.key?(:annotations)
end

test "#to_h includes annotations when present" do
annotations = Annotations.new(audience: ["user"], priority: 0.8, last_modified: "2025-01-12T15:00:58Z")
resource = Resource.new(uri: "file:///test.txt", name: "resource_with_annotations", annotations: annotations)

expected = { audience: ["user"], priority: 0.8, lastModified: "2025-01-12T15:00:58Z" }
assert_equal expected, resource.to_h[:annotations]
end
end
end