validate_interface_implementation
Check if TypeScript code correctly implements a specified interface by comparing implementation against the interface definition.
Instructions
Validate if code correctly implements an interface
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| implementation | Yes | The implementation code to validate | |
| interfaceDefinition | Yes | The interface definition | |
| interfaceName | Yes | Name of the interface being implemented |
Implementation Reference
- src/mcp-server.ts:339-358 (handler)MCP server handler method for 'validate_interface_implementation' tool. Validates arguments, delegates to TypeValidator, and formats the JSON response.private async handleValidateInterfaceImplementation( implementation: string, interfaceName: string, interfaceDefinition: string ) { const result = this.typeValidator.validateInterfaceImplementation( implementation, interfaceName, interfaceDefinition ); return { content: [ { type: "text", text: JSON.stringify(result, null, 2) } ] }; }
- src/type-validator.ts:109-162 (helper)Core implementation of interface validation logic. Combines interface definition and implementation code, compiles with TypeScript compiler API, and collects semantic diagnostics to determine validity.validateInterfaceImplementation(implementation: string, interfaceName: string, interfaceDefinition: string): ValidationResult { const errors: ValidationError[] = []; const warnings: ValidationWarning[] = []; try { // Combine interface definition with implementation const combinedCode = `${interfaceDefinition}\n\n${implementation}`; const sourceFile = ts.createSourceFile( "temp.ts", combinedCode, ts.ScriptTarget.Latest, true ); const defaultCompilerHost = ts.createCompilerHost(this.compilerOptions); const compilerHost = { ...defaultCompilerHost, getSourceFile: (fileName: string, languageVersion: ts.ScriptTarget) => { if (fileName === "temp.ts") { return sourceFile; } return defaultCompilerHost.getSourceFile(fileName, languageVersion); } }; const program = ts.createProgram(["temp.ts"], this.compilerOptions, compilerHost); const diagnostics = program.getSemanticDiagnostics(sourceFile); for (const diagnostic of diagnostics) { const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"); const position = diagnostic.start ? sourceFile.getLineAndCharacterOfPosition(diagnostic.start) : undefined; errors.push({ message, line: position ? position.line + 1 : undefined, column: position ? position.character + 1 : undefined, code: diagnostic.code?.toString() }); } } catch (error) { errors.push({ message: `Interface validation failed: ${error instanceof Error ? error.message : String(error)}`, code: "INTERFACE_VALIDATION_ERROR" }); } return { valid: errors.length === 0, errors, warnings }; }
- src/mcp-server.ts:140-161 (registration)Registration of the 'validate_interface_implementation' tool in the MCP server's list of tools, including name, description, and input schema.{ name: "validate_interface_implementation", description: "Validate if code correctly implements an interface", inputSchema: { type: "object", properties: { implementation: { type: "string", description: "The implementation code to validate" }, interfaceName: { type: "string", description: "Name of the interface being implemented" }, interfaceDefinition: { type: "string", description: "The interface definition" } }, required: ["implementation", "interfaceName", "interfaceDefinition"] } },
- src/mcp-server.ts:232-239 (handler)Dispatch case in the CallToolRequestHandler switch statement that routes 'validate_interface_implementation' calls to the handler method.case "validate_interface_implementation": { const implArgs = this.validateArgs<ToolArguments["validate_interface_implementation"]>(args); return await this.handleValidateInterfaceImplementation( implArgs.implementation, implArgs.interfaceName, implArgs.interfaceDefinition ); }
- src/mcp-server.ts:20-24 (schema)TypeScript interface definition for the tool's input arguments used for internal type safety.validate_interface_implementation: { implementation: string; interfaceName: string; interfaceDefinition: string; };