Add tests for JSON Schema 2020-12 field preservation (SEP-1613) (#1649)
This commit is contained in:
parent
fcffa14b5b
commit
397089a78e
1 changed files with 65 additions and 0 deletions
|
|
@ -9,6 +9,8 @@ from mcp.types import (
|
||||||
InitializeRequestParams,
|
InitializeRequestParams,
|
||||||
JSONRPCMessage,
|
JSONRPCMessage,
|
||||||
JSONRPCRequest,
|
JSONRPCRequest,
|
||||||
|
ListToolsResult,
|
||||||
|
Tool,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -56,3 +58,66 @@ async def test_method_initialization():
|
||||||
assert initialize_request.method == "initialize", "method should be set to 'initialize'"
|
assert initialize_request.method == "initialize", "method should be set to 'initialize'"
|
||||||
assert initialize_request.params is not None
|
assert initialize_request.params is not None
|
||||||
assert initialize_request.params.protocolVersion == LATEST_PROTOCOL_VERSION
|
assert initialize_request.params.protocolVersion == LATEST_PROTOCOL_VERSION
|
||||||
|
|
||||||
|
|
||||||
|
def test_tool_preserves_json_schema_2020_12_fields():
|
||||||
|
"""Verify that JSON Schema 2020-12 keywords are preserved in Tool.inputSchema.
|
||||||
|
|
||||||
|
SEP-1613 establishes JSON Schema 2020-12 as the default dialect for MCP.
|
||||||
|
This test ensures the SDK doesn't strip $schema, $defs, or additionalProperties.
|
||||||
|
"""
|
||||||
|
input_schema = {
|
||||||
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||||
|
"type": "object",
|
||||||
|
"$defs": {
|
||||||
|
"address": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {"street": {"type": "string"}, "city": {"type": "string"}},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"properties": {
|
||||||
|
"name": {"type": "string"},
|
||||||
|
"address": {"$ref": "#/$defs/address"},
|
||||||
|
},
|
||||||
|
"additionalProperties": False,
|
||||||
|
}
|
||||||
|
|
||||||
|
tool = Tool(name="test_tool", description="A test tool", inputSchema=input_schema)
|
||||||
|
|
||||||
|
# Verify fields are preserved in the model
|
||||||
|
assert tool.inputSchema["$schema"] == "https://json-schema.org/draft/2020-12/schema"
|
||||||
|
assert "$defs" in tool.inputSchema
|
||||||
|
assert "address" in tool.inputSchema["$defs"]
|
||||||
|
assert tool.inputSchema["additionalProperties"] is False
|
||||||
|
|
||||||
|
# Verify fields survive serialization round-trip
|
||||||
|
serialized = tool.model_dump(mode="json", by_alias=True)
|
||||||
|
assert serialized["inputSchema"]["$schema"] == "https://json-schema.org/draft/2020-12/schema"
|
||||||
|
assert "$defs" in serialized["inputSchema"]
|
||||||
|
assert serialized["inputSchema"]["additionalProperties"] is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_list_tools_result_preserves_json_schema_2020_12_fields():
|
||||||
|
"""Verify JSON Schema 2020-12 fields survive ListToolsResult deserialization."""
|
||||||
|
raw_response = {
|
||||||
|
"tools": [
|
||||||
|
{
|
||||||
|
"name": "json_schema_tool",
|
||||||
|
"description": "Tool with JSON Schema 2020-12 features",
|
||||||
|
"inputSchema": {
|
||||||
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||||
|
"type": "object",
|
||||||
|
"$defs": {"item": {"type": "string"}},
|
||||||
|
"properties": {"items": {"type": "array", "items": {"$ref": "#/$defs/item"}}},
|
||||||
|
"additionalProperties": False,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
result = ListToolsResult.model_validate(raw_response)
|
||||||
|
tool = result.tools[0]
|
||||||
|
|
||||||
|
assert tool.inputSchema["$schema"] == "https://json-schema.org/draft/2020-12/schema"
|
||||||
|
assert "$defs" in tool.inputSchema
|
||||||
|
assert tool.inputSchema["additionalProperties"] is False
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue