Skip to content

Commit 397089a

Browse files
Add tests for JSON Schema 2020-12 field preservation (SEP-1613) (#1649)
1 parent fcffa14 commit 397089a

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

tests/test_types.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
InitializeRequestParams,
1010
JSONRPCMessage,
1111
JSONRPCRequest,
12+
ListToolsResult,
13+
Tool,
1214
)
1315

1416

@@ -56,3 +58,66 @@ async def test_method_initialization():
5658
assert initialize_request.method == "initialize", "method should be set to 'initialize'"
5759
assert initialize_request.params is not None
5860
assert initialize_request.params.protocolVersion == LATEST_PROTOCOL_VERSION
61+
62+
63+
def test_tool_preserves_json_schema_2020_12_fields():
64+
"""Verify that JSON Schema 2020-12 keywords are preserved in Tool.inputSchema.
65+
66+
SEP-1613 establishes JSON Schema 2020-12 as the default dialect for MCP.
67+
This test ensures the SDK doesn't strip $schema, $defs, or additionalProperties.
68+
"""
69+
input_schema = {
70+
"$schema": "https://json-schema.org/draft/2020-12/schema",
71+
"type": "object",
72+
"$defs": {
73+
"address": {
74+
"type": "object",
75+
"properties": {"street": {"type": "string"}, "city": {"type": "string"}},
76+
}
77+
},
78+
"properties": {
79+
"name": {"type": "string"},
80+
"address": {"$ref": "#/$defs/address"},
81+
},
82+
"additionalProperties": False,
83+
}
84+
85+
tool = Tool(name="test_tool", description="A test tool", inputSchema=input_schema)
86+
87+
# Verify fields are preserved in the model
88+
assert tool.inputSchema["$schema"] == "https://json-schema.org/draft/2020-12/schema"
89+
assert "$defs" in tool.inputSchema
90+
assert "address" in tool.inputSchema["$defs"]
91+
assert tool.inputSchema["additionalProperties"] is False
92+
93+
# Verify fields survive serialization round-trip
94+
serialized = tool.model_dump(mode="json", by_alias=True)
95+
assert serialized["inputSchema"]["$schema"] == "https://json-schema.org/draft/2020-12/schema"
96+
assert "$defs" in serialized["inputSchema"]
97+
assert serialized["inputSchema"]["additionalProperties"] is False
98+
99+
100+
def test_list_tools_result_preserves_json_schema_2020_12_fields():
101+
"""Verify JSON Schema 2020-12 fields survive ListToolsResult deserialization."""
102+
raw_response = {
103+
"tools": [
104+
{
105+
"name": "json_schema_tool",
106+
"description": "Tool with JSON Schema 2020-12 features",
107+
"inputSchema": {
108+
"$schema": "https://json-schema.org/draft/2020-12/schema",
109+
"type": "object",
110+
"$defs": {"item": {"type": "string"}},
111+
"properties": {"items": {"type": "array", "items": {"$ref": "#/$defs/item"}}},
112+
"additionalProperties": False,
113+
},
114+
}
115+
]
116+
}
117+
118+
result = ListToolsResult.model_validate(raw_response)
119+
tool = result.tools[0]
120+
121+
assert tool.inputSchema["$schema"] == "https://json-schema.org/draft/2020-12/schema"
122+
assert "$defs" in tool.inputSchema
123+
assert tool.inputSchema["additionalProperties"] is False

0 commit comments

Comments
 (0)