DefaultJsonSchemaValidator caches schemas by hashCode(), so a tool's input can be validated against another tool's schema
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 72/100
Hướng nghiên cứu
Read DefaultJsonSchemaValidator.generateCacheKey in both mcp-json-jackson2 and mcp-json-jackson3, then inspect the existing validator tests. Reproduce the collision with the issue's schemas and check validation in both call orders. Done when distinct schema contents use distinct cache entries and both valid and invalid inputs are handled according to their own schemas.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug description
DefaultJsonSchemaValidator caches each compiled schema under generateCacheKey(schema), which falls back to String.valueOf(schema.hashCode()) when the schema has no $id. Two different schemas with the same hashCode() therefore share one cache entry, and whichever is validated first is used for both. A server validating tool inputs then checks a call to one tool against another tool's input schema:
- a valid call is rejected (
required property 'a' not found), and - an invalid call can be accepted.
Which tools are affected depends on the schemas and on the order in which tools are first called, so it can pass tests and fail in production, or the reverse.
Collisions are not rare for tool input schemas. Map.hashCode() sums key.hashCode() ^ value.hashCode() over the entries, and a property name contributes twice (XORed with its sub-schema under properties, and inside the required list), so much of its contribution cancels. Measured with {"type":"object","properties":{N:{"type":"string"}},"required":[N]} over the 702 one- and two-letter names N: only 44 distinct hash codes (17 with "type":"number"; one group holds 190 schemas). Every single-letter name collides with every other.
The code is the same on main and in v2.1.0, in both mcp-json-jackson2 and mcp-json-jackson3 (DefaultJsonSchemaValidator.generateCacheKey).
Environment
MCP Java SDK 2.0.0 (mcp-json-jackson3; also present in 2.1.0 and on main), networknt json-schema-validator 3.0.0, Java 21.0.8. Found in a server exposing two tools with one parameter each, named a and q: after the first had been called, every call to the second was refused.
Steps to reproduce
- Create one
DefaultJsonSchemaValidator, as a server does. - Validate
{"a": "x"}against{"type":"object","properties":{"a":{"type":"string"}},"required":["a"],"additionalProperties":false}. - Validate
{"b": "x"}against the same schema withbin place ofa.
Expected behavior
Step 3 is valid: each schema is validated as itself. A fresh validator does say valid; the shared one says invalid, with the first schema's errors.
Minimal Complete Reproducible example
import io.modelcontextprotocol.json.schema.jackson3.DefaultJsonSchemaValidator;
import java.util.List;
import java.util.Map;
public class Repro {
static Map<String, Object> schema(String property) {
return Map.of("type", "object",
"properties", Map.of(property, Map.of("type", "string")),
"required", List.of(property),
"additionalProperties", false);
}
public static void main(String[] args) {
Map<String, Object> schemaA = schema("a");
Map<String, Object> schemaB = schema("b");
System.out.println("schemas equal: " + schemaA.equals(schemaB)
+ ", hashCodes equal: " + (schemaA.hashCode() == schemaB.hashCode()));
System.out.println("fresh validator, B with {b}: "
+ new DefaultJsonSchemaValidator().validate(schemaB, Map.of("b", "x")).valid());
DefaultJsonSchemaValidator v = new DefaultJsonSchemaValidator();
System.out.println("A with {a}: " + v.validate(schemaA, Map.of("a", "x")).valid());
var r = v.validate(schemaB, Map.of("b", "x"));
System.out.println("then B with {b} (valid for B): " + r.valid() + " " + (r.valid() ? "" : r.errorMessage()));
System.out.println("then B with {a} (invalid for B): " + v.validate(schemaB, Map.of("a", "x")).valid());
}
}
Output:
schemas equal: false, hashCodes equal: true
fresh validator, B with {b}: true
A with {a}: true
then B with {b} (valid for B): false Validation failed: JSON schema validation errors: [: required property 'a' not found, : property 'b' is not defined in the schema and the schema does not allow additional properties]
then B with {a} (invalid for B): true
Possible fix
Key the cache by the schema's content rather than its hash code, e.g. its serialized JSON (or String.valueOf(schema) for the nested Map/List values here), so that two keys are equal only when the schemas are. A server can work around it today by passing McpServer...jsonSchemaValidator(...) a DefaultJsonSchemaValidator subclass that overrides the protected generateCacheKey that way.
- Ngôn ngữ chính
- Java
- Star
- 3.8k
- Fork
- 1.1k
- Merge trung bình
- 1 ngày 3 giờ
- Pull request đã merge (30 ngày)
- 38
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của modelcontextprotocol/java-sdk
-
Modern `HttpServletMcpTransport` does not check `Mcp-Name` when `params.name` or `params.uri` is not a JSON stringCó thể đã có người làm @starryeye đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
modelcontextprotocol/java-sdk#1352 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Unknown tool error always says "Unknown tool: invalid_tool_name" instead of the name that was calledĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
modelcontextprotocol/java-sdk#1346 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
modelcontextprotocol/java-sdk#1345 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Tool result _meta is lost when the server automatically adds text content for structured outputĐang mởarea/server legacy P3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
modelcontextprotocol/java-sdk#1330 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Client request handlers that complete empty send no JSON-RPC responseCó thể đã có người làm @1fanwang đã nhận 83 ngày trước. Đang mởarea/client bug P2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
modelcontextprotocol/java-sdk#1124 · 6 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của modelcontextprotocol/java-sdk
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
crash
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
-
account bug course exam iris lecture
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày