ruby-sdk/test/mcp/resource/contents_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

147 lines
4.7 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
module MCP
class Resource
class ContentsTest < ActiveSupport::TestCase
test "Contents#to_h returns hash with mimeType" do
contents = Resource::Contents.new(
uri: "test://example",
mime_type: "text/plain",
)
result = contents.to_h
assert_equal "test://example", result[:uri]
assert_equal "text/plain", result[:mimeType]
refute result.key?(:mime_type), "Should use camelCase 'mimeType' not snake_case 'mime_type'"
end
test "Contents#to_h omits mimeType when nil" do
contents = Resource::Contents.new(uri: "test://example")
result = contents.to_h
assert_equal({ uri: "test://example" }, result)
refute result.key?(:mimeType)
end
test "TextContents#to_h returns hash with text and mimeType" do
text_contents = Resource::TextContents.new(
uri: "test://text",
mime_type: "text/plain",
text: "Hello, world!",
)
result = text_contents.to_h
assert_equal "test://text", result[:uri]
assert_equal "text/plain", result[:mimeType]
assert_equal "Hello, world!", result[:text]
refute result.key?(:mime_type), "Should use camelCase 'mimeType' not snake_case 'mime_type'"
end
test "BlobContents#to_h returns hash with blob and mimeType" do
blob_contents = Resource::BlobContents.new(
uri: "test://binary",
mime_type: "image/png",
data: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
)
result = blob_contents.to_h
assert_equal "test://binary", result[:uri]
assert_equal "image/png", result[:mimeType]
assert_equal "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==", result[:blob]
refute result.key?(:data), "Should use 'blob' not 'data' per MCP specification"
refute result.key?(:mime_type), "Should use camelCase 'mimeType' not snake_case 'mime_type'"
end
test "BlobContents#to_h omits mimeType when nil" do
blob_contents = Resource::BlobContents.new(
uri: "test://binary",
mime_type: nil,
data: "base64data",
)
result = blob_contents.to_h
assert_equal({ uri: "test://binary", blob: "base64data" }, result)
refute result.key?(:mimeType)
end
test "Contents#to_h omits _meta when nil" do
contents = Resource::Contents.new(uri: "test://example", mime_type: "text/plain")
refute contents.to_h.key?(:_meta)
end
test "Contents#to_h includes _meta when present" do
meta = { "application/vnd.ant.mcp-app" => { "csp" => "default-src 'self'" } }
contents = Resource::Contents.new(uri: "test://example", mime_type: "text/plain", meta: meta)
assert_equal meta, contents.to_h[:_meta]
end
test "TextContents#to_h includes _meta when present" do
meta = { "application/vnd.ant.mcp-app" => { "csp" => "default-src 'self'" } }
text_contents = Resource::TextContents.new(
uri: "test://text",
mime_type: "text/plain",
text: "Hello",
meta: meta,
)
result = text_contents.to_h
assert_equal meta, result[:_meta]
assert_equal "Hello", result[:text]
end
test "TextContents#to_h omits _meta when nil" do
text_contents = Resource::TextContents.new(
uri: "test://text",
mime_type: "text/plain",
text: "Hello",
)
refute text_contents.to_h.key?(:_meta)
end
test "BlobContents#to_h includes _meta when present" do
meta = { "application/vnd.ant.mcp-app" => { "csp" => "default-src 'self'" } }
blob_contents = Resource::BlobContents.new(
uri: "test://binary",
mime_type: "image/png",
data: "base64data",
meta: meta,
)
result = blob_contents.to_h
assert_equal meta, result[:_meta]
assert_equal "base64data", result[:blob]
end
test "BlobContents#to_h omits _meta when nil" do
blob_contents = Resource::BlobContents.new(
uri: "test://binary",
mime_type: "image/png",
data: "base64data",
)
refute blob_contents.to_h.key?(:_meta)
end
test "Contents#to_h preserves empty _meta hash" do
contents = Resource::Contents.new(uri: "test://example", mime_type: "text/plain", meta: {})
result = contents.to_h
assert result.key?(:_meta)
assert_equal({}, result[:_meta])
end
end
end
end