## 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.
169 lines
4.8 KiB
Ruby
169 lines
4.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "test_helper"
|
|
|
|
module MCP
|
|
module Content
|
|
class ImageTest < ActiveSupport::TestCase
|
|
test "#to_h returns mimeType (camelCase) per MCP spec" do
|
|
image = Image.new("base64data", "image/png")
|
|
result = image.to_h
|
|
|
|
assert_equal "image/png", result[:mimeType]
|
|
refute result.key?(:mime_type), "Expected camelCase mimeType, got snake_case mime_type"
|
|
assert_equal "image", result[:type]
|
|
assert_equal "base64data", result[:data]
|
|
end
|
|
|
|
test "#to_h with annotations" do
|
|
image = Image.new("base64data", "image/png", annotations: { role: "thumbnail" })
|
|
result = image.to_h
|
|
|
|
assert_equal({ role: "thumbnail" }, result[:annotations])
|
|
end
|
|
|
|
test "#to_h without annotations omits the key" do
|
|
image = Image.new("base64data", "image/png")
|
|
result = image.to_h
|
|
|
|
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
|
|
test "#to_h returns correct format per MCP spec" do
|
|
audio = Audio.new("base64data", "audio/wav")
|
|
result = audio.to_h
|
|
|
|
assert_equal "audio", result[:type]
|
|
assert_equal "base64data", result[:data]
|
|
assert_equal "audio/wav", result[:mimeType]
|
|
end
|
|
|
|
test "#to_h with annotations" do
|
|
audio = Audio.new("base64data", "audio/wav", annotations: { role: "recording" })
|
|
result = audio.to_h
|
|
|
|
assert_equal({ role: "recording" }, result[:annotations])
|
|
end
|
|
|
|
test "#to_h without annotations omits the key" do
|
|
audio = Audio.new("base64data", "audio/wav")
|
|
result = audio.to_h
|
|
|
|
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
|
|
test "#to_h returns correct format per MCP spec" do
|
|
resource = Object.new
|
|
def resource.to_h
|
|
{ uri: "test://example", mimeType: "text/plain", text: "content" }
|
|
end
|
|
|
|
embedded = EmbeddedResource.new(resource)
|
|
result = embedded.to_h
|
|
|
|
assert_equal "resource", result[:type]
|
|
assert_equal({ uri: "test://example", mimeType: "text/plain", text: "content" }, result[:resource])
|
|
end
|
|
|
|
test "#to_h with annotations" do
|
|
resource = Object.new
|
|
def resource.to_h
|
|
{ uri: "test://x" }
|
|
end
|
|
|
|
embedded = EmbeddedResource.new(resource, annotations: { role: "data" })
|
|
result = embedded.to_h
|
|
|
|
assert_equal({ role: "data" }, result[:annotations])
|
|
end
|
|
|
|
test "#to_h without annotations omits the key" do
|
|
resource = Object.new
|
|
def resource.to_h
|
|
{ uri: "test://x" }
|
|
end
|
|
|
|
embedded = EmbeddedResource.new(resource)
|
|
result = embedded.to_h
|
|
|
|
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
|