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.
This commit is contained in:
Koichi ITO 2026-04-11 14:24:00 +09:00
commit 0ad9a89d97
6 changed files with 269 additions and 29 deletions

View file

@ -28,6 +28,19 @@ module MCP
refute result.key?(:annotations)
end
test "#to_h with meta" do
meta = { "application/vnd.ant.mcp-app" => { "csp" => "default-src 'self'" } }
image = Image.new("base64data", "image/png", meta: meta)
assert_equal meta, image.to_h[:_meta]
end
test "#to_h without meta omits the key" do
image = Image.new("base64data", "image/png")
refute image.to_h.key?(:_meta)
end
end
class AudioTest < ActiveSupport::TestCase
@ -53,6 +66,19 @@ module MCP
refute result.key?(:annotations)
end
test "#to_h with meta" do
meta = { "application/vnd.ant.mcp-app" => { "csp" => "default-src 'self'" } }
audio = Audio.new("base64data", "audio/wav", meta: meta)
assert_equal meta, audio.to_h[:_meta]
end
test "#to_h without meta omits the key" do
audio = Audio.new("base64data", "audio/wav")
refute audio.to_h.key?(:_meta)
end
end
class EmbeddedResourceTest < ActiveSupport::TestCase
@ -92,6 +118,52 @@ module MCP
refute result.key?(:annotations)
end
test "#to_h with meta" do
resource = Object.new
def resource.to_h
{ uri: "test://x" }
end
meta = { "application/vnd.ant.mcp-app" => { "csp" => "default-src 'self'" } }
embedded = EmbeddedResource.new(resource, meta: meta)
assert_equal meta, embedded.to_h[:_meta]
end
test "#to_h without meta omits the key" do
resource = Object.new
def resource.to_h
{ uri: "test://x" }
end
embedded = EmbeddedResource.new(resource)
refute embedded.to_h.key?(:_meta)
end
end
class TextTest < ActiveSupport::TestCase
test "#to_h returns correct format per MCP spec" do
text = Text.new("hello")
result = text.to_h
assert_equal "text", result[:type]
assert_equal "hello", result[:text]
end
test "#to_h with meta" do
meta = { "application/vnd.ant.mcp-app" => { "csp" => "default-src 'self'" } }
text = Text.new("hello", meta: meta)
assert_equal meta, text.to_h[:_meta]
end
test "#to_h without meta omits the key" do
text = Text.new("hello")
refute text.to_h.key?(:_meta)
end
end
end
end

View file

@ -0,0 +1,41 @@
# frozen_string_literal: true
require "test_helper"
module MCP
class Prompt
class ResultTest < ActiveSupport::TestCase
test "#to_h returns description and messages" do
result = Prompt::Result.new(
description: "a prompt",
messages: [Prompt::Message.new(role: "user", content: Content::Text.new("hi"))],
)
hash = result.to_h
assert_equal "a prompt", hash[:description]
assert_equal 1, hash[:messages].size
end
test "#to_h includes _meta when present" do
meta = { "application/vnd.ant.mcp-app" => { "csp" => "default-src 'self'" } }
result = Prompt::Result.new(
description: "a prompt",
messages: [Prompt::Message.new(role: "user", content: Content::Text.new("hi"))],
meta: meta,
)
assert_equal meta, result.to_h[:_meta]
end
test "#to_h omits _meta when nil" do
result = Prompt::Result.new(
description: "a prompt",
messages: [Prompt::Message.new(role: "user", content: Content::Text.new("hi"))],
)
refute result.to_h.key?(:_meta)
end
end
end
end

View file

@ -70,6 +70,78 @@ module MCP
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

View file

@ -36,5 +36,22 @@ module MCP
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

View file

@ -36,5 +36,18 @@ module MCP
assert_equal expected_icons, resource.to_h[:icons]
end
test "#to_h omits _meta when nil" do
resource = Resource.new(uri: "file:///test.txt", name: "resource_without_meta")
refute resource.to_h.key?(:_meta)
end
test "#to_h includes _meta when present" do
meta = { "application/vnd.ant.mcp-app" => { "csp" => "default-src 'self'" } }
resource = Resource.new(uri: "file:///test.txt", name: "resource_with_meta", meta: meta)
assert_equal meta, resource.to_h[:_meta]
end
end
end

View file

@ -122,6 +122,20 @@ module MCP
assert_equal structured_content, actual[:structuredContent]
refute actual[:isError]
end
test "#to_h includes _meta when present" do
meta = { "application/vnd.ant.mcp-app" => { "csp" => "default-src 'self'" } }
response = Response.new([{ type: "text", text: "ok" }], meta: meta)
actual = response.to_h
assert_equal meta, actual[:_meta]
end
test "#to_h omits _meta when nil" do
response = Response.new([{ type: "text", text: "ok" }])
refute response.to_h.key?(:_meta)
end
end
end
end