Skip to content

Commit 1bee8d4

Browse files
authored
refactor(mcp): remove redundant type field from Content implementations (#27)
- The type field and associated methods were redundant in Content implementations (TextContent, ImageContent, EmbeddedResource) as the type information is already handled by Jackson's polymorphic type handling via @JsonSubTypes annotation. - Add default implementation that returns the appropriate type string based on the implementing class (text, image, or resource) - Added comprehensive unit tests for McpSchema to validate serialization/deserialization behavior of all schema components. - Add deserialization tests for all content types - Add json-unit-assertj for flexible JSON testing - ignores array order and extra array items, reducing test brittleness while maintaining functional validation. Resolves #26 Resolve spring-projects/spring-ai#2350 Signed-off-by: Christian Tzolov <[email protected]>
1 parent b2c62d5 commit 1bee8d4

File tree

4 files changed

+619
-33
lines changed

4 files changed

+619
-33
lines changed

mcp/pom.xml

+12-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<artifactId>mcp</artifactId>
1212
<packaging>jar</packaging>
1313
<name>Java MCP SDK</name>
14-
<description>Java SDK implementation of the Model Context Protocol, enabling seamless integration with language models and AI tools</description>
14+
<description>Java SDK implementation of the Model Context Protocol, enabling seamless integration with language models and AI tools</description>
1515
<url>https://github.com/modelcontextprotocol/java-sdk</url>
1616

1717
<scm>
@@ -51,7 +51,7 @@
5151
</execution>
5252
</executions>
5353
</plugin>
54-
54+
5555
<plugin>
5656
<groupId>org.apache.maven.plugins</groupId>
5757
<artifactId>maven-jar-plugin</artifactId>
@@ -158,12 +158,20 @@
158158
<scope>test</scope>
159159
</dependency>
160160

161+
<dependency>
162+
<groupId>net.javacrumbs.json-unit</groupId>
163+
<artifactId>json-unit-assertj</artifactId>
164+
<version>${json-unit-assertj.version}</version>
165+
<scope>test</scope>
166+
</dependency>
167+
168+
161169
<!-- Used by the HttpServletSseServerTransport -->
162170
<dependency>
163171
<groupId>jakarta.servlet</groupId>
164172
<artifactId>jakarta.servlet-api</artifactId>
165173
<version>${jakarta.servlet.version}</version>
166-
<scope>provided</scope>
174+
<scope>provided</scope>
167175
</dependency>
168176

169177
<!-- Tomcat dependencies for testing -->
@@ -183,4 +191,4 @@
183191
</dependencies>
184192

185193

186-
</project>
194+
</project>

mcp/src/main/java/io/modelcontextprotocol/spec/McpSchema.java

+13-29
Original file line numberDiff line numberDiff line change
@@ -964,61 +964,45 @@ public record CompleteCompletion(// @formatter:off
964964
@JsonSubTypes.Type(value = EmbeddedResource.class, name = "resource") })
965965
public sealed interface Content permits TextContent, ImageContent, EmbeddedResource {
966966

967-
String type();
967+
default String type() {
968+
if (this instanceof TextContent) {
969+
return "text";
970+
}
971+
else if (this instanceof ImageContent) {
972+
return "image";
973+
}
974+
else if (this instanceof EmbeddedResource) {
975+
return "resource";
976+
}
977+
throw new IllegalArgumentException("Unknown content type: " + this);
978+
}
968979

969980
}
970981

971982
@JsonInclude(JsonInclude.Include.NON_ABSENT)
972983
public record TextContent( // @formatter:off
973984
@JsonProperty("audience") List<Role> audience,
974985
@JsonProperty("priority") Double priority,
975-
@JsonProperty("type") String type,
976986
@JsonProperty("text") String text) implements Content { // @formatter:on
977987

978-
public TextContent {
979-
type = "text";
980-
}
981-
982-
public String type() {
983-
return type;
984-
}
985-
986988
public TextContent(String content) {
987-
this(null, null, "text", content);
989+
this(null, null, content);
988990
}
989991
}
990992

991993
@JsonInclude(JsonInclude.Include.NON_ABSENT)
992994
public record ImageContent( // @formatter:off
993995
@JsonProperty("audience") List<Role> audience,
994996
@JsonProperty("priority") Double priority,
995-
@JsonProperty("type") String type,
996997
@JsonProperty("data") String data,
997998
@JsonProperty("mimeType") String mimeType) implements Content { // @formatter:on
998-
999-
public ImageContent {
1000-
type = "image";
1001-
}
1002-
1003-
public String type() {
1004-
return type;
1005-
}
1006999
}
10071000

10081001
@JsonInclude(JsonInclude.Include.NON_ABSENT)
10091002
public record EmbeddedResource( // @formatter:off
10101003
@JsonProperty("audience") List<Role> audience,
10111004
@JsonProperty("priority") Double priority,
1012-
@JsonProperty("type") String type,
10131005
@JsonProperty("resource") ResourceContents resource) implements Content { // @formatter:on
1014-
1015-
public EmbeddedResource {
1016-
type = "resource";
1017-
}
1018-
1019-
public String type() {
1020-
return type;
1021-
}
10221006
}
10231007

10241008
// ---------------------------

0 commit comments

Comments
 (0)