Skip to content

Commit 3a163ad

Browse files
authored
Merge pull request modelcontextprotocol#403 from yuki3738/add_annotations_field_to_resource
Add `annotations` field to `MCP::Resource` and `MCP::ResourceTemplate` per MCP specification
2 parents 783eb63 + 64219be commit 3a163ad

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

lib/mcp/resource.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55

66
module MCP
77
class Resource
8-
attr_reader :uri, :name, :title, :description, :icons, :mime_type, :size, :meta
8+
attr_reader :uri, :name, :title, :description, :icons, :mime_type, :annotations, :size, :meta
99

10-
def initialize(uri:, name:, title: nil, description: nil, icons: [], mime_type: nil, size: nil, meta: nil)
10+
def initialize(uri:, name:, title: nil, description: nil, icons: [], mime_type: nil, annotations: nil, size: nil, meta: nil)
1111
@uri = uri
1212
@name = name
1313
@title = title
1414
@description = description
1515
@icons = icons
1616
@mime_type = mime_type
17+
@annotations = annotations
1718
@size = size
1819
@meta = meta
1920
end
@@ -26,6 +27,7 @@ def to_h
2627
description: description,
2728
icons: icons&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
2829
mimeType: mime_type,
30+
annotations: annotations&.to_h,
2931
size: size,
3032
_meta: meta,
3133
}.compact

lib/mcp/resource_template.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
module MCP
44
class ResourceTemplate
5-
attr_reader :uri_template, :name, :title, :description, :icons, :mime_type, :meta
5+
attr_reader :uri_template, :name, :title, :description, :icons, :mime_type, :annotations, :meta
66

7-
def initialize(uri_template:, name:, title: nil, description: nil, icons: [], mime_type: nil, meta: nil)
7+
def initialize(uri_template:, name:, title: nil, description: nil, icons: [], mime_type: nil, annotations: nil, meta: nil)
88
@uri_template = uri_template
99
@name = name
1010
@title = title
1111
@description = description
1212
@icons = icons
1313
@mime_type = mime_type
14+
@annotations = annotations
1415
@meta = meta
1516
end
1617

@@ -22,6 +23,7 @@ def to_h
2223
description: description,
2324
icons: icons&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
2425
mimeType: mime_type,
26+
annotations: annotations&.to_h,
2527
_meta: meta,
2628
}.compact
2729
end

test/mcp/resource_template_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,23 @@ class ResourceTemplateTest < ActiveSupport::TestCase
5353

5454
assert_equal meta, resource_template.to_h[:_meta]
5555
end
56+
57+
test "#to_h omits annotations when nil" do
58+
resource_template = ResourceTemplate.new(uri_template: "file:///{path}", name: "template_without_annotations")
59+
60+
refute resource_template.to_h.key?(:annotations)
61+
end
62+
63+
test "#to_h includes annotations when present" do
64+
annotations = Annotations.new(audience: ["user"], priority: 0.8, last_modified: "2025-01-12T15:00:58Z")
65+
resource_template = ResourceTemplate.new(
66+
uri_template: "file:///{path}",
67+
name: "template_with_annotations",
68+
annotations: annotations,
69+
)
70+
71+
expected = { audience: ["user"], priority: 0.8, lastModified: "2025-01-12T15:00:58Z" }
72+
assert_equal expected, resource_template.to_h[:annotations]
73+
end
5674
end
5775
end

test/mcp/resource_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,19 @@ class ResourceTest < ActiveSupport::TestCase
6767

6868
assert_equal 0, resource.to_h[:size]
6969
end
70+
71+
test "#to_h omits annotations when nil" do
72+
resource = Resource.new(uri: "file:///test.txt", name: "resource_without_annotations")
73+
74+
refute resource.to_h.key?(:annotations)
75+
end
76+
77+
test "#to_h includes annotations when present" do
78+
annotations = Annotations.new(audience: ["user"], priority: 0.8, last_modified: "2025-01-12T15:00:58Z")
79+
resource = Resource.new(uri: "file:///test.txt", name: "resource_with_annotations", annotations: annotations)
80+
81+
expected = { audience: ["user"], priority: 0.8, lastModified: "2025-01-12T15:00:58Z" }
82+
assert_equal expected, resource.to_h[:annotations]
83+
end
7084
end
7185
end

0 commit comments

Comments
 (0)