ruby-sdk/test/mcp/resource_template_test.rb
Yuki Minamiya 64219be2c8 Add annotations field to MCP::Resource and MCP::ResourceTemplate per MCP specification
## Motivation and Context

The MCP specification defines an optional `annotations` field on both the
`Resource` and `ResourceTemplate` types, but the Ruby SDK is the only official
SDK that does not wire it up — TypeScript, Python, and PHP all implement it.
https://modelcontextprotocol.io/specification/2025-11-25/server/resources#annotations

The `MCP::Annotations` class already exists; this commit connects it. The field
accepts an `MCP::Annotations` instance rather than a raw hash, for consistency
with other typed fields and to get `lastModified` key normalization for free.

## How Has This Been Tested?

Added tests in `test/mcp/resource_test.rb` and
`test/mcp/resource_template_test.rb`:

- `#to_h` omits `annotations` when nil
- `#to_h` includes `annotations` when present (also asserting that
  `last_modified` is serialized as `lastModified`)

The full unit suite and RuboCop pass locally.

## Breaking Changes

None. `annotations:` is a new optional keyword argument defaulting to `nil`,
and it is omitted from `#to_h` output when not set.
2026-06-14 16:05:25 +09:00

75 lines
2.6 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
module MCP
class ResourceTemplateTest < ActiveSupport::TestCase
test "#to_h does not have `:icons` key when icons is empty" do
resource_template = ResourceTemplate.new(
uri_template: "file:///{path}",
name: "resource_template_without_icons",
description: "a resource template without icons",
)
refute resource_template.to_h.key?(:icons)
end
test "#to_h does not have `:icons` key when icons is nil" do
resource_template = ResourceTemplate.new(
uri_template: "file:///{path}",
name: "resource_template_without_icons",
description: "a resource template without icons",
icons: nil,
)
refute resource_template.to_h.key?(:icons)
end
test "#to_h includes icons when present" do
resource_template = ResourceTemplate.new(
uri_template: "file:///{path}",
name: "resource_template_with_icons",
description: "a resource template with icons",
icons: [Icon.new(mime_type: "image/png", sizes: ["48x48"], src: "https://example.com", theme: "light")],
)
expected_icons = [{ mimeType: "image/png", sizes: ["48x48"], src: "https://example.com", theme: "light" }]
assert_equal expected_icons, resource_template.to_h[:icons]
end
test "#to_h omits _meta when nil" do
resource_template = ResourceTemplate.new(uri_template: "file:///{path}", name: "template_without_meta")
refute resource_template.to_h.key?(:_meta)
end
test "#to_h includes _meta when present" do
meta = { "application/vnd.ant.mcp-app" => { "csp" => "default-src 'self'" } }
resource_template = ResourceTemplate.new(
uri_template: "file:///{path}",
name: "template_with_meta",
meta: meta,
)
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