Skip to content

Commit b99c36b

Browse files
committed
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
1 parent 5b04089 commit b99c36b

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

lib/mcp/content.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,19 @@ def to_h
2828
{ data: data, mime_type: mime_type, annotations: annotations, type: "image" }.compact
2929
end
3030
end
31+
32+
class Audio
33+
attr_reader :data, :mime_type, :annotations
34+
35+
def initialize(data, mime_type, annotations: nil)
36+
@data = data
37+
@mime_type = mime_type
38+
@annotations = annotations
39+
end
40+
41+
def to_h
42+
{ data: data, mimeType: mime_type, annotations: annotations, type: "audio" }.compact
43+
end
44+
end
3145
end
3246
end

test/mcp/content_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
module MCP
6+
module Content
7+
class AudioTest < ActiveSupport::TestCase
8+
test "#to_h returns correct format per MCP spec" do
9+
audio = Audio.new("base64data", "audio/wav")
10+
result = audio.to_h
11+
12+
assert_equal "audio", result[:type]
13+
assert_equal "base64data", result[:data]
14+
assert_equal "audio/wav", result[:mimeType]
15+
end
16+
17+
test "#to_h with annotations" do
18+
audio = Audio.new("base64data", "audio/wav", annotations: { role: "recording" })
19+
result = audio.to_h
20+
21+
assert_equal({ role: "recording" }, result[:annotations])
22+
end
23+
24+
test "#to_h without annotations omits the key" do
25+
audio = Audio.new("base64data", "audio/wav")
26+
result = audio.to_h
27+
28+
refute result.key?(:annotations)
29+
end
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)