Skip to content

add AudioContent to McpSchema #184

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion mcp/src/main/java/io/modelcontextprotocol/spec/McpSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -1254,8 +1254,9 @@ public record CompleteCompletion(
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @JsonSubTypes.Type(value = TextContent.class, name = "text"),
@JsonSubTypes.Type(value = ImageContent.class, name = "image"),
@JsonSubTypes.Type(value = AudioContent.class, name = "audio"),
@JsonSubTypes.Type(value = EmbeddedResource.class, name = "resource") })
public sealed interface Content permits TextContent, ImageContent, EmbeddedResource {
public sealed interface Content permits TextContent, ImageContent, AudioContent, EmbeddedResource {

default String type() {
if (this instanceof TextContent) {
Expand All @@ -1264,6 +1265,9 @@ default String type() {
else if (this instanceof ImageContent) {
return "image";
}
else if (this instanceof AudioContent) {
return "audio";
}
else if (this instanceof EmbeddedResource) {
return "resource";
}
Expand Down Expand Up @@ -1293,6 +1297,15 @@ public record ImageContent( // @formatter:off
@JsonProperty("mimeType") String mimeType) implements Content { // @formatter:on
}

@JsonInclude(JsonInclude.Include.NON_ABSENT)
@JsonIgnoreProperties(ignoreUnknown = true)
public record AudioContent( // @formatter:off
@JsonProperty("audience") List<Role> audience,
@JsonProperty("priority") Double priority,
@JsonProperty("data") String data,
@JsonProperty("mimeType") String mimeType) implements Content { // @formatter:on
}

@JsonInclude(JsonInclude.Include.NON_ABSENT)
@JsonIgnoreProperties(ignoreUnknown = true)
public record EmbeddedResource( // @formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void testContentDeserializationWrongType() throws Exception {
{"type":"WRONG","text":"XXX"}""", McpSchema.TextContent.class))
.isInstanceOf(InvalidTypeIdException.class)
.hasMessageContaining(
"Could not resolve type id 'WRONG' as a subtype of `io.modelcontextprotocol.spec.McpSchema$TextContent`: known type ids = [image, resource, text]");
"Could not resolve type id 'WRONG' as a subtype of `io.modelcontextprotocol.spec.McpSchema$TextContent`: known type ids = [audio, image, resource, text]");
}

@Test
Expand All @@ -83,6 +83,46 @@ void testImageContentDeserialization() throws Exception {
assertThat(imageContent.mimeType()).isEqualTo("image/png");
}

@Test
void testAudioContent() throws Exception {
McpSchema.AudioContent test = new McpSchema.AudioContent(List.of(McpSchema.Role.USER), 0.8, "base64audiodata",
"audio/mp3");

String value = mapper.writeValueAsString(test);

assertThatJson(value).when(Option.IGNORING_ARRAY_ORDER)
.when(Option.IGNORING_EXTRA_ARRAY_ITEMS)
.isObject()
.isEqualTo(json("""
{
"type":"audio",
"audience": ["user"],
"priority": 0.8,
"data": "base64audiodata",
"mimeType": "audio/mp3"
}"""));
}

@Test
void testAudioContentDeserialization() throws Exception {
String json = """
{
"type":"audio",
"audience": ["user"],
"priority": 0.8,
"data": "base64audiodata",
"mimeType": "audio/mp3"
}""";

McpSchema.AudioContent audioContent = mapper.readValue(json, McpSchema.AudioContent.class);

assertThat(audioContent).isNotNull();
assertThat(audioContent.audience()).containsExactly(McpSchema.Role.USER);
assertThat(audioContent.priority()).isEqualTo(0.8);
assertThat(audioContent.data()).isEqualTo("base64audiodata");
assertThat(audioContent.mimeType()).isEqualTo("audio/mp3");
}

@Test
void testEmbeddedResource() throws Exception {
McpSchema.TextResourceContents resourceContents = new McpSchema.TextResourceContents("resource://test",
Expand Down