json_schema
Convert JSON files or remote URLs into TypeScript schemas to simplify development and reduce context size in large JSON datasets.
Instructions
Generate TypeScript schema for a JSON file or remote JSON URL. Provide the file path or HTTP/HTTPS URL as the only parameter.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | JSON file path (local) or HTTP/HTTPS URL to generate schema from |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"filePath": {
"description": "JSON file path (local) or HTTP/HTTPS URL to generate schema from",
"type": "string"
}
},
"required": [
"filePath"
],
"type": "object"
}
Implementation Reference
- src/index.ts:19-24 (schema)Zod input schema validation for the json_schema tool, defining the expected filePath parameter.const JsonSchemaInputSchema = z.object({ filePath: z.string().min(1, "File path or HTTP/HTTPS URL is required").refine( (val) => val.length > 0 && (val.startsWith('./') || val.startsWith('/') || val.startsWith('http://') || val.startsWith('https://') || !val.includes('/')), "Must be a valid file path or HTTP/HTTPS URL" ) });
- src/index.ts:49-63 (schema)Type definitions for JsonSchemaError and JsonSchemaResult used by the json_schema tool handler.interface JsonSchemaError { readonly type: 'file_not_found' | 'invalid_json' | 'network_error' | 'invalid_url' | 'unsupported_content_type' | 'rate_limit_exceeded' | 'validation_error' | 'authentication_required' | 'server_error' | 'content_too_large' | 'quicktype_error'; readonly message: string; readonly details?: unknown; } // Result type for better type safety type JsonSchemaResult = { readonly success: true; readonly schema: string; readonly fileSizeBytes: number; } | { readonly success: false; readonly error: JsonSchemaError; };
- src/index.ts:272-323 (handler)Core handler function that ingests JSON from file or URL and generates TypeScript schema using quicktype library.async function processJsonSchema(input: JsonSchemaInput): Promise<JsonSchemaResult> { try { // Use strategy pattern to ingest JSON content const ingestionResult = await jsonIngestionContext.ingest(input.filePath); if (!ingestionResult.success) { // Map strategy errors to existing error format for backward compatibility return { success: false, error: ingestionResult.error }; } const jsonContent = ingestionResult.content; // Calculate file size in bytes const fileSizeBytes = new TextEncoder().encode(jsonContent).length; // Generate schema using quicktype with fixed parameters try { const result = await quicktypeJSON( "typescript", "GeneratedType", jsonContent ); return { success: true, schema: result.lines.join('\n'), fileSizeBytes }; } catch (error) { return { success: false, error: { type: 'quicktype_error', message: 'Failed to generate schema', details: error } }; } } catch (error) { return { success: false, error: { type: 'validation_error', message: 'Unexpected error during processing', details: error } }; } }
- src/index.ts:507-561 (registration)MCP server tool registration for 'json_schema', including input schema, description, and execution handler that delegates to processJsonSchema.server.tool( "json_schema", "Generate TypeScript schema for a JSON file or remote JSON URL. Provide the file path or HTTP/HTTPS URL as the only parameter.", { filePath: z.string().describe("JSON file path (local) or HTTP/HTTPS URL to generate schema from") }, async ({ filePath }) => { try { const validatedInput = JsonSchemaInputSchema.parse({ filePath: filePath }); const result = await processJsonSchema(validatedInput); if (result.success) { // Format file size for display const formatFileSize = (bytes: number): string => { if (bytes < 1024) return `${bytes} bytes`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; }; const fileSizeInfo = `// File size: ${formatFileSize(result.fileSizeBytes)} (${result.fileSizeBytes} bytes)\n\n`; return { content: [ { type: "text", text: fileSizeInfo + result.schema } ] }; } else { return { content: [ { type: "text", text: `Error: ${result.error.message}` } ], isError: true }; } } catch (error) { return { content: [ { type: "text", text: `Validation error: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } } );