|
| 1 | +/** |
| 2 | + * JSON Schema 2020-12 conformance test scenario (SEP-1613) |
| 3 | + * |
| 4 | + * Validates that MCP servers correctly preserve JSON Schema 2020-12 keywords |
| 5 | + * in tool definitions, ensuring implementations don't strip $schema, $defs, |
| 6 | + * or additionalProperties fields. |
| 7 | + */ |
| 8 | + |
| 9 | +import { ClientScenario, ConformanceCheck } from '../../types.js'; |
| 10 | +import { connectToServer } from './client-helper.js'; |
| 11 | + |
| 12 | +const EXPECTED_TOOL_NAME = 'json_schema_2020_12_tool'; |
| 13 | +const EXPECTED_SCHEMA_DIALECT = 'https://json-schema.org/draft/2020-12/schema'; |
| 14 | + |
| 15 | +export class JsonSchema2020_12Scenario implements ClientScenario { |
| 16 | + name = 'json-schema-2020-12'; |
| 17 | + description = `Validates JSON Schema 2020-12 keyword preservation (SEP-1613). |
| 18 | +
|
| 19 | +**Server Implementation Requirements:** |
| 20 | +
|
| 21 | +Implement tool \`${EXPECTED_TOOL_NAME}\` with inputSchema containing JSON Schema 2020-12 features: |
| 22 | +
|
| 23 | +\`\`\`json |
| 24 | +{ |
| 25 | + "name": "${EXPECTED_TOOL_NAME}", |
| 26 | + "description": "Tool with JSON Schema 2020-12 features", |
| 27 | + "inputSchema": { |
| 28 | + "$schema": "${EXPECTED_SCHEMA_DIALECT}", |
| 29 | + "type": "object", |
| 30 | + "$defs": { |
| 31 | + "address": { |
| 32 | + "type": "object", |
| 33 | + "properties": { |
| 34 | + "street": { "type": "string" }, |
| 35 | + "city": { "type": "string" } |
| 36 | + } |
| 37 | + } |
| 38 | + }, |
| 39 | + "properties": { |
| 40 | + "name": { "type": "string" }, |
| 41 | + "address": { "$ref": "#/$defs/address" } |
| 42 | + }, |
| 43 | + "additionalProperties": false |
| 44 | + } |
| 45 | +} |
| 46 | +\`\`\` |
| 47 | +
|
| 48 | +**Verification**: The test verifies that \`$schema\`, \`$defs\`, and \`additionalProperties\` are preserved in the tool listing response.`; |
| 49 | + |
| 50 | + async run(serverUrl: string): Promise<ConformanceCheck[]> { |
| 51 | + const checks: ConformanceCheck[] = []; |
| 52 | + const specReferences = [ |
| 53 | + { |
| 54 | + id: 'SEP-1613', |
| 55 | + url: 'https://github.com/modelcontextprotocol/specification/pull/655' |
| 56 | + } |
| 57 | + ]; |
| 58 | + |
| 59 | + try { |
| 60 | + const connection = await connectToServer(serverUrl); |
| 61 | + const result = await connection.client.listTools(); |
| 62 | + |
| 63 | + // Find the test tool |
| 64 | + const tool = result.tools?.find((t) => t.name === EXPECTED_TOOL_NAME); |
| 65 | + |
| 66 | + // Check 1: Tool exists |
| 67 | + checks.push({ |
| 68 | + id: 'json-schema-2020-12-tool-found', |
| 69 | + name: 'JsonSchema2020_12ToolFound', |
| 70 | + description: `Server advertises tool '${EXPECTED_TOOL_NAME}'`, |
| 71 | + status: tool ? 'SUCCESS' : 'FAILURE', |
| 72 | + timestamp: new Date().toISOString(), |
| 73 | + errorMessage: tool |
| 74 | + ? undefined |
| 75 | + : `Tool '${EXPECTED_TOOL_NAME}' not found. Available tools: ${result.tools?.map((t) => t.name).join(', ') || 'none'}`, |
| 76 | + specReferences, |
| 77 | + details: { |
| 78 | + toolFound: !!tool, |
| 79 | + availableTools: result.tools?.map((t) => t.name) || [] |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + if (!tool) { |
| 84 | + await connection.close(); |
| 85 | + return checks; |
| 86 | + } |
| 87 | + |
| 88 | + const inputSchema = tool.inputSchema as Record<string, unknown>; |
| 89 | + |
| 90 | + // Check 2: $schema field preserved |
| 91 | + const hasSchema = '$schema' in inputSchema; |
| 92 | + const schemaValue = inputSchema['$schema']; |
| 93 | + const schemaCorrect = schemaValue === EXPECTED_SCHEMA_DIALECT; |
| 94 | + |
| 95 | + checks.push({ |
| 96 | + id: 'json-schema-2020-12-$schema', |
| 97 | + name: 'JsonSchema2020_12$Schema', |
| 98 | + description: `inputSchema.$schema field preserved with value '${EXPECTED_SCHEMA_DIALECT}'`, |
| 99 | + status: hasSchema && schemaCorrect ? 'SUCCESS' : 'FAILURE', |
| 100 | + timestamp: new Date().toISOString(), |
| 101 | + errorMessage: !hasSchema |
| 102 | + ? '$schema field missing from inputSchema - field was likely stripped' |
| 103 | + : !schemaCorrect |
| 104 | + ? `$schema has unexpected value: ${JSON.stringify(schemaValue)}` |
| 105 | + : undefined, |
| 106 | + specReferences, |
| 107 | + details: { |
| 108 | + hasSchema, |
| 109 | + schemaValue, |
| 110 | + expected: EXPECTED_SCHEMA_DIALECT |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + // Check 3: $defs field preserved |
| 115 | + const hasDefs = '$defs' in inputSchema; |
| 116 | + const defsValue = inputSchema['$defs'] as |
| 117 | + | Record<string, unknown> |
| 118 | + | undefined; |
| 119 | + const defsHasAddress = defsValue && 'address' in defsValue; |
| 120 | + |
| 121 | + checks.push({ |
| 122 | + id: 'json-schema-2020-12-$defs', |
| 123 | + name: 'JsonSchema2020_12$Defs', |
| 124 | + description: |
| 125 | + 'inputSchema.$defs field preserved with expected structure', |
| 126 | + status: hasDefs && defsHasAddress ? 'SUCCESS' : 'FAILURE', |
| 127 | + timestamp: new Date().toISOString(), |
| 128 | + errorMessage: !hasDefs |
| 129 | + ? '$defs field missing from inputSchema - field was likely stripped' |
| 130 | + : !defsHasAddress |
| 131 | + ? '$defs exists but missing expected "address" definition' |
| 132 | + : undefined, |
| 133 | + specReferences, |
| 134 | + details: { |
| 135 | + hasDefs, |
| 136 | + defsKeys: defsValue ? Object.keys(defsValue) : [], |
| 137 | + defsValue |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + // Check 4: additionalProperties field preserved |
| 142 | + const hasAdditionalProps = 'additionalProperties' in inputSchema; |
| 143 | + const additionalPropsValue = inputSchema['additionalProperties']; |
| 144 | + const additionalPropsCorrect = additionalPropsValue === false; |
| 145 | + |
| 146 | + checks.push({ |
| 147 | + id: 'json-schema-2020-12-additionalProperties', |
| 148 | + name: 'JsonSchema2020_12AdditionalProperties', |
| 149 | + description: 'inputSchema.additionalProperties field preserved', |
| 150 | + status: |
| 151 | + hasAdditionalProps && additionalPropsCorrect ? 'SUCCESS' : 'FAILURE', |
| 152 | + timestamp: new Date().toISOString(), |
| 153 | + errorMessage: !hasAdditionalProps |
| 154 | + ? 'additionalProperties field missing from inputSchema - field was likely stripped' |
| 155 | + : !additionalPropsCorrect |
| 156 | + ? `additionalProperties has unexpected value: ${JSON.stringify(additionalPropsValue)}, expected: false` |
| 157 | + : undefined, |
| 158 | + specReferences, |
| 159 | + details: { |
| 160 | + hasAdditionalProps, |
| 161 | + additionalPropsValue, |
| 162 | + expected: false |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + await connection.close(); |
| 167 | + } catch (error) { |
| 168 | + checks.push({ |
| 169 | + id: 'json-schema-2020-12-error', |
| 170 | + name: 'JsonSchema2020_12Error', |
| 171 | + description: 'JSON Schema 2020-12 conformance test', |
| 172 | + status: 'FAILURE', |
| 173 | + timestamp: new Date().toISOString(), |
| 174 | + errorMessage: `Failed: ${error instanceof Error ? error.message : String(error)}`, |
| 175 | + specReferences |
| 176 | + }); |
| 177 | + } |
| 178 | + |
| 179 | + return checks; |
| 180 | + } |
| 181 | +} |
0 commit comments