fix: Remove JsonSchema and use a Map for inputSchema to support json schemas dialect (#749)
* feat: remove JsonSchema an use a Map for inputSchema - Fixes #886 Co-authored-by: Daniel Garnier-Moiroux <git@garnier.wf>
This commit is contained in:
parent
fcdc0d4c14
commit
95203233c8
3 changed files with 72 additions and 45 deletions
|
|
@ -44,11 +44,10 @@ class McpAsyncClientTests {
|
|||
Map<String, Object> inputSchemaMap = Map.of("type", "object", "properties",
|
||||
Map.of("expression", Map.of("type", "string")), "required", List.of("expression"));
|
||||
|
||||
McpSchema.JsonSchema inputSchema = new McpSchema.JsonSchema("object", inputSchemaMap, null, null, null, null);
|
||||
McpSchema.Tool.Builder toolBuilder = McpSchema.Tool.builder()
|
||||
.name("calculator")
|
||||
.description("Performs mathematical calculations")
|
||||
.inputSchema(inputSchema);
|
||||
.inputSchema(inputSchemaMap);
|
||||
|
||||
if (hasOutputSchema) {
|
||||
Map<String, Object> outputSchema = Map.of("type", "object", "properties",
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
package io.modelcontextprotocol.server;
|
||||
|
||||
import static io.modelcontextprotocol.util.ToolsUtils.EMPTY_JSON_SCHEMA;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
|
@ -48,7 +50,6 @@ import org.springframework.web.client.RestClient;
|
|||
import static io.modelcontextprotocol.server.transport.HttpServletStatelessServerTransport.APPLICATION_JSON;
|
||||
import static io.modelcontextprotocol.server.transport.HttpServletStatelessServerTransport.TEXT_EVENT_STREAM;
|
||||
import static io.modelcontextprotocol.util.McpJsonMapperUtils.JSON_MAPPER;
|
||||
import static io.modelcontextprotocol.util.ToolsUtils.EMPTY_JSON_SCHEMA;
|
||||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
|
||||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.json;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import io.modelcontextprotocol.spec.McpSchema.TextResourceContents;
|
|||
import org.assertj.core.api.InstanceOfAssertFactories;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import io.modelcontextprotocol.json.TypeRef;
|
||||
import net.javacrumbs.jsonunit.core.Option;
|
||||
|
||||
/**
|
||||
|
|
@ -713,13 +714,15 @@ public class McpSchemaTests {
|
|||
""";
|
||||
|
||||
// Deserialize the original string to a JsonSchema object
|
||||
McpSchema.JsonSchema schema = JSON_MAPPER.readValue(schemaJson, McpSchema.JsonSchema.class);
|
||||
Map<String, Object> schema = JSON_MAPPER.readValue(schemaJson, new TypeRef<HashMap<String, Object>>() {
|
||||
});
|
||||
|
||||
// Serialize the object back to a string
|
||||
String serialized = JSON_MAPPER.writeValueAsString(schema);
|
||||
|
||||
// Deserialize again
|
||||
McpSchema.JsonSchema deserialized = JSON_MAPPER.readValue(serialized, McpSchema.JsonSchema.class);
|
||||
Map<String, Object> deserialized = JSON_MAPPER.readValue(serialized, new TypeRef<HashMap<String, Object>>() {
|
||||
});
|
||||
|
||||
// Serialize one more time and compare with the first serialization
|
||||
String serializedAgain = JSON_MAPPER.writeValueAsString(deserialized);
|
||||
|
|
@ -756,13 +759,15 @@ public class McpSchemaTests {
|
|||
""";
|
||||
|
||||
// Deserialize the original string to a JsonSchema object
|
||||
McpSchema.JsonSchema schema = JSON_MAPPER.readValue(schemaJson, McpSchema.JsonSchema.class);
|
||||
Map<String, Object> schema = JSON_MAPPER.readValue(schemaJson, new TypeRef<HashMap<String, Object>>() {
|
||||
});
|
||||
|
||||
// Serialize the object back to a string
|
||||
String serialized = JSON_MAPPER.writeValueAsString(schema);
|
||||
|
||||
// Deserialize again
|
||||
McpSchema.JsonSchema deserialized = JSON_MAPPER.readValue(serialized, McpSchema.JsonSchema.class);
|
||||
Map<String, Object> deserialized = JSON_MAPPER.readValue(serialized, new TypeRef<HashMap<String, Object>>() {
|
||||
});
|
||||
|
||||
// Serialize one more time and compare with the first serialization
|
||||
String serializedAgain = JSON_MAPPER.writeValueAsString(deserialized);
|
||||
|
|
@ -845,8 +850,11 @@ public class McpSchemaTests {
|
|||
assertThatJson(serializedAgain).when(Option.IGNORING_ARRAY_ORDER).isEqualTo(json(serialized));
|
||||
|
||||
// Just verify the basic structure was preserved
|
||||
assertThat(deserializedTool.inputSchema().defs()).isNotNull();
|
||||
assertThat(deserializedTool.inputSchema().defs()).containsKey("Address");
|
||||
assertThat(deserializedTool.inputSchema()).containsKey("$defs")
|
||||
.extractingByKey("$defs")
|
||||
.isNotNull()
|
||||
.asInstanceOf(InstanceOfAssertFactories.MAP)
|
||||
.containsKey("Address");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -866,14 +874,14 @@ public class McpSchemaTests {
|
|||
}
|
||||
""";
|
||||
|
||||
McpSchema.JsonSchema schema = JSON_MAPPER.readValue(schemaJson, McpSchema.JsonSchema.class);
|
||||
Map<String, Object> inputSchema = Map.of("inputSchema", schemaJson);
|
||||
Map<String, Object> meta = Map.of("metaKey", "metaValue");
|
||||
|
||||
McpSchema.Tool tool = McpSchema.Tool.builder()
|
||||
.name("addressTool")
|
||||
.title("addressTool")
|
||||
.description("Handles addresses")
|
||||
.inputSchema(schema)
|
||||
.inputSchema(inputSchema)
|
||||
.meta(meta)
|
||||
.build();
|
||||
|
||||
|
|
@ -1114,7 +1122,7 @@ public class McpSchemaTests {
|
|||
assertThat(tool.name()).isEqualTo("test-tool");
|
||||
assertThat(tool.description()).isEqualTo("A test tool");
|
||||
assertThat(tool.inputSchema()).isNotNull();
|
||||
assertThat(tool.inputSchema().type()).isEqualTo("object");
|
||||
assertThat(tool.inputSchema().get("type")).isEqualTo("object");
|
||||
assertThat(tool.outputSchema()).isNotNull();
|
||||
assertThat(tool.outputSchema()).containsKey("type");
|
||||
assertThat(tool.outputSchema().get("type")).isEqualTo("object");
|
||||
|
|
|
|||
Loading…
Reference in a new issue