|
9 | 9 | InitializeRequestParams, |
10 | 10 | JSONRPCMessage, |
11 | 11 | JSONRPCRequest, |
| 12 | + ListToolsResult, |
| 13 | + Tool, |
12 | 14 | ) |
13 | 15 |
|
14 | 16 |
|
@@ -56,3 +58,66 @@ async def test_method_initialization(): |
56 | 58 | assert initialize_request.method == "initialize", "method should be set to 'initialize'" |
57 | 59 | assert initialize_request.params is not None |
58 | 60 | 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