diff --git a/mcp-json-jackson2/src/main/java/io/modelcontextprotocol/json/schema/jackson/DefaultJsonSchemaValidator.java b/mcp-json-jackson2/src/main/java/io/modelcontextprotocol/json/schema/jackson/DefaultJsonSchemaValidator.java index 15511c9c2..1ff28cb80 100644 --- a/mcp-json-jackson2/src/main/java/io/modelcontextprotocol/json/schema/jackson/DefaultJsonSchemaValidator.java +++ b/mcp-json-jackson2/src/main/java/io/modelcontextprotocol/json/schema/jackson/DefaultJsonSchemaValidator.java @@ -3,17 +3,17 @@ */ package io.modelcontextprotocol.json.schema.jackson; +import java.util.List; import java.util.Map; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.networknt.schema.JsonSchema; -import com.networknt.schema.JsonSchemaFactory; -import com.networknt.schema.SpecVersion; -import com.networknt.schema.ValidationMessage; +import com.networknt.schema.Schema; +import com.networknt.schema.SchemaRegistry; +import com.networknt.schema.Error; +import com.networknt.schema.dialect.Dialects; import io.modelcontextprotocol.json.schema.JsonSchemaValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,10 +31,10 @@ public class DefaultJsonSchemaValidator implements JsonSchemaValidator { private final ObjectMapper objectMapper; - private final JsonSchemaFactory schemaFactory; + private final SchemaRegistry schemaFactory; // TODO: Implement a strategy to purge the cache (TTL, size limit, etc.) - private final ConcurrentHashMap schemaCache; + private final ConcurrentHashMap schemaCache; public DefaultJsonSchemaValidator() { this(new ObjectMapper()); @@ -42,7 +42,7 @@ public DefaultJsonSchemaValidator() { public DefaultJsonSchemaValidator(ObjectMapper objectMapper) { this.objectMapper = objectMapper; - this.schemaFactory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V202012); + this.schemaFactory = SchemaRegistry.withDialect(Dialects.getDraft202012()); this.schemaCache = new ConcurrentHashMap<>(); } @@ -62,7 +62,7 @@ public ValidationResponse validate(Map schema, Object structured ? this.objectMapper.readTree((String) structuredContent) : this.objectMapper.valueToTree(structuredContent); - Set validationResult = this.getOrCreateJsonSchema(schema).validate(jsonStructuredOutput); + List validationResult = this.getOrCreateJsonSchema(schema).validate(jsonStructuredOutput); // Check if validation passed if (!validationResult.isEmpty()) { @@ -85,36 +85,36 @@ public ValidationResponse validate(Map schema, Object structured } /** - * Gets a cached JsonSchema or creates and caches a new one. + * Gets a cached Schema or creates and caches a new one. * @param schema the schema map to convert - * @return the compiled JsonSchema + * @return the compiled Schema * @throws JsonProcessingException if schema processing fails */ - private JsonSchema getOrCreateJsonSchema(Map schema) throws JsonProcessingException { + private Schema getOrCreateJsonSchema(Map schema) throws JsonProcessingException { // Generate cache key based on schema content String cacheKey = this.generateCacheKey(schema); // Try to get from cache first - JsonSchema cachedSchema = this.schemaCache.get(cacheKey); + Schema cachedSchema = this.schemaCache.get(cacheKey); if (cachedSchema != null) { return cachedSchema; } // Create new schema if not in cache - JsonSchema newSchema = this.createJsonSchema(schema); + Schema newSchema = this.createJsonSchema(schema); // Cache the schema - JsonSchema existingSchema = this.schemaCache.putIfAbsent(cacheKey, newSchema); + Schema existingSchema = this.schemaCache.putIfAbsent(cacheKey, newSchema); return existingSchema != null ? existingSchema : newSchema; } /** - * Creates a new JsonSchema from the given schema map. + * Creates a new Schema from the given schema map. * @param schema the schema map - * @return the compiled JsonSchema + * @return the compiled Schema * @throws JsonProcessingException if schema processing fails */ - private JsonSchema createJsonSchema(Map schema) throws JsonProcessingException { + private Schema createJsonSchema(Map schema) throws JsonProcessingException { // Convert schema map directly to JsonNode (more efficient than string // serialization) JsonNode schemaNode = this.objectMapper.valueToTree(schema); diff --git a/pom.xml b/pom.xml index 144a06c53..1ff45596e 100644 --- a/pom.xml +++ b/pom.xml @@ -96,7 +96,7 @@ 4.2.0 7.1.0 4.1.0 - 1.5.7 + 2.0.0