From cbb81a836c2ca58f6db66ab7d50126ed1aea5bfe Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 28 Feb 2026 00:57:09 +0900 Subject: [PATCH] Add `Content::Audio` class for audio content type The MCP spec defines an audio content type, but the SDK only had `Content::Text` and `Content::Image`. Add `Content::Audio` with the same structure as `Content::Image`. https://modelcontextprotocol.io/specification/2025-11-25/server/tools#audio-content --- lib/mcp/content.rb | 14 ++++++++++++++ test/mcp/content_test.rb | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/mcp/content.rb b/lib/mcp/content.rb index 872de32..c98d176 100644 --- a/lib/mcp/content.rb +++ b/lib/mcp/content.rb @@ -28,5 +28,19 @@ def to_h { data: data, mimeType: mime_type, annotations: annotations, type: "image" }.compact end end + + class Audio + attr_reader :data, :mime_type, :annotations + + def initialize(data, mime_type, annotations: nil) + @data = data + @mime_type = mime_type + @annotations = annotations + end + + def to_h + { data: data, mimeType: mime_type, annotations: annotations, type: "audio" }.compact + end + end end end diff --git a/test/mcp/content_test.rb b/test/mcp/content_test.rb index 46ac902..169d2d6 100644 --- a/test/mcp/content_test.rb +++ b/test/mcp/content_test.rb @@ -29,5 +29,30 @@ class ImageTest < ActiveSupport::TestCase refute result.key?(:annotations) 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 + end end end