ruby-sdk/test/mcp/resource_template_test.rb
Koichi ITO 0ad9a89d97 Add _meta field to resource, content, and result classes
## Motivation and Context

MCP 2025-11-25 spec defines a `_meta` general field on many types,
but the Ruby SDK only exposed it on `Tool` and `Prompt`.
To attach metadata (e.g. MCP Apps CSP hints on resource contents),
consumers had to subclass existing classes and override `to_h`.

Add a `meta:` keyword argument and `_meta` serialization to:

- `Resource::Contents`, `Resource::TextContents`, `Resource::BlobContents`
- `Resource`, `ResourceTemplate`
- `Content::Text`, `Content::Image`, `Content::Audio`, `Content::EmbeddedResource`
- `Tool::Response` (`CallToolResult`)
- `Prompt::Result` (`GetPromptResult`)

This aligns the Ruby SDK with the Python and TypeScript SDKs, which both expose `_meta` on these types.

Resolves #308.

## How Has This Been Tested?

Added unit tests for each updated class covering two patterns: `_meta` present
(asserts the value is reflected in `to_h[:_meta]`) and `_meta` omitted
(asserts no `:_meta` key in `to_h`, i.e., `.compact` behavior).
All existing tests continue to pass, and `rake rubocop` is clean.

## Breaking Changes

None. All new `meta:` parameters are optional keyword arguments defaulting to `nil`.
The `to_h` output stays backward-compatible because `nil` values are removed by the existing `.compact` call,
so existing callers require no changes.
2026-04-11 15:08:55 +09:00

57 lines
1.9 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
end
end