Skip to content

Add support for audio content in prompts and tool results #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Sources/MCP/Server/Prompts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public struct Prompt: Hashable, Codable, Sendable {
case text(text: String)
/// Image content
case image(data: String, mimeType: String)
/// Audio content
case audio(data: String, mimeType: String)
/// Embedded resource content
case resource(uri: String, mimeType: String, text: String?, blob: String?)

Expand All @@ -82,6 +84,10 @@ public struct Prompt: Hashable, Codable, Sendable {
try container.encode("image", forKey: .type)
try container.encode(data, forKey: .data)
try container.encode(mimeType, forKey: .mimeType)
case .audio(let data, let mimeType):
try container.encode("audio", forKey: .type)
try container.encode(data, forKey: .data)
try container.encode(mimeType, forKey: .mimeType)
case .resource(let uri, let mimeType, let text, let blob):
try container.encode("resource", forKey: .type)
try container.encode(uri, forKey: .uri)
Expand All @@ -103,6 +109,10 @@ public struct Prompt: Hashable, Codable, Sendable {
let data = try container.decode(String.self, forKey: .data)
let mimeType = try container.decode(String.self, forKey: .mimeType)
self = .image(data: data, mimeType: mimeType)
case "audio":
let data = try container.decode(String.self, forKey: .data)
let mimeType = try container.decode(String.self, forKey: .mimeType)
self = .audio(data: data, mimeType: mimeType)
case "resource":
let uri = try container.decode(String.self, forKey: .uri)
let mimeType = try container.decode(String.self, forKey: .mimeType)
Expand Down Expand Up @@ -155,7 +165,7 @@ public enum ListPrompts: Method {

public struct Parameters: NotRequired, Hashable, Codable, Sendable {
public let cursor: String?

public init() {
self.cursor = nil
}
Expand Down
13 changes: 12 additions & 1 deletion Sources/MCP/Server/Tools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public struct Tool: Hashable, Codable, Sendable {
case text(String)
/// Image content
case image(data: String, mimeType: String, metadata: [String: String]?)
/// Audio content
case audio(data: String, mimeType: String)
/// Embedded resource content
case resource(uri: String, mimeType: String, text: String?)

Expand All @@ -36,6 +38,7 @@ public struct Tool: Hashable, Codable, Sendable {
case text
case image
case resource
case audio
case uri
case mimeType
case data
Expand All @@ -56,6 +59,10 @@ public struct Tool: Hashable, Codable, Sendable {
let metadata = try container.decodeIfPresent(
[String: String].self, forKey: .metadata)
self = .image(data: data, mimeType: mimeType, metadata: metadata)
case "audio":
let data = try container.decode(String.self, forKey: .data)
let mimeType = try container.decode(String.self, forKey: .mimeType)
self = .audio(data: data, mimeType: mimeType)
case "resource":
let uri = try container.decode(String.self, forKey: .uri)
let mimeType = try container.decode(String.self, forKey: .mimeType)
Expand All @@ -79,6 +86,10 @@ public struct Tool: Hashable, Codable, Sendable {
try container.encode(data, forKey: .data)
try container.encode(mimeType, forKey: .mimeType)
try container.encodeIfPresent(metadata, forKey: .metadata)
case .audio(let data, let mimeType):
try container.encode("audio", forKey: .type)
try container.encode(data, forKey: .data)
try container.encode(mimeType, forKey: .mimeType)
case .resource(let uri, let mimeType, let text):
try container.encode("resource", forKey: .type)
try container.encode(uri, forKey: .uri)
Expand Down Expand Up @@ -120,7 +131,7 @@ public enum ListTools: Method {

public struct Parameters: NotRequired, Hashable, Codable, Sendable {
public let cursor: String?

public init() {
self.cursor = nil
}
Expand Down
16 changes: 14 additions & 2 deletions Tests/MCPTests/PromptTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ struct PromptTests {
#expect(Bool(false), "Expected text content")
}

// Test audio content
let audioContent = Prompt.Message.Content.audio(
data: "base64audiodata", mimeType: "audio/wav")
let audioData = try encoder.encode(audioContent)
let decodedAudio = try decoder.decode(Prompt.Message.Content.self, from: audioData)
if case .audio(let data, let mimeType) = decodedAudio {
#expect(data == "base64audiodata")
#expect(mimeType == "audio/wav")
} else {
#expect(Bool(false), "Expected audio content")
}

// Test image content
let imageContent = Prompt.Message.Content.image(data: "base64data", mimeType: "image/png")
let imageData = try encoder.encode(imageContent)
Expand Down Expand Up @@ -142,7 +154,7 @@ struct PromptTests {
let emptyParams = ListPrompts.Parameters()
#expect(emptyParams.cursor == nil)
}

@Test("ListPrompts request decoding with omitted params")
func testListPromptsRequestDecodingWithOmittedParams() throws {
// Test decoding when params field is omitted
Expand All @@ -157,7 +169,7 @@ struct PromptTests {
#expect(decoded.id == "test-id")
#expect(decoded.method == ListPrompts.name)
}

@Test("ListPrompts request decoding with null params")
func testListPromptsRequestDecodingWithNullParams() throws {
// Test decoding when params field is null
Expand Down
20 changes: 20 additions & 0 deletions Tests/MCPTests/ToolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ struct ToolTests {
}
}

@Test("Audio content encoding and decoding")
func testToolContentAudioEncoding() throws {
let content = Tool.Content.audio(
data: "base64audiodata",
mimeType: "audio/wav"
)
let encoder = JSONEncoder()
let decoder = JSONDecoder()

let data = try encoder.encode(content)
let decoded = try decoder.decode(Tool.Content.self, from: data)

if case .audio(let data, let mimeType) = decoded {
#expect(data == "base64audiodata")
#expect(mimeType == "audio/wav")
} else {
#expect(Bool(false), "Expected audio content")
}
}

@Test("ListTools parameters validation")
func testListToolsParameters() throws {
let params = ListTools.Parameters(cursor: "next_page")
Expand Down