validate_generated_code
Analyze generated code for patterns, security, and best practices, ensuring compliance with specified types such as component, function, service, test, or config.
Instructions
Validate generated code for patterns, security, and best practices
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | The generated code to validate | |
| targetFile | No | Target file path for context | |
| type | Yes | Type of code being validated |
Implementation Reference
- Core handler function that executes the validation logic for generated code, checking naming conventions, error handling, security, project patterns, and consistency. Computes a validation score and lists issues.export async function validateGeneratedCode( code: string, context: string, targetFile?: string ): Promise<ValidationResult> { const result: ValidationResult = { valid: true, score: 100, issues: [], patterns: { followed: [], violated: [], }, }; // Load project patterns const projectPath = process.env.PROJECT_PATH || process.cwd(); const contextPath = join(projectPath, 'CODEBASE-CONTEXT.md'); let projectPatterns: any = {}; if (existsSync(contextPath)) { const contextContent = readFileSync(contextPath, 'utf-8'); projectPatterns = extractPatternsFromContext(contextContent); } // Validate against common issues validateCommonIssues(code, result); // Validate naming conventions validateNamingConventions(code, context, result, projectPatterns); // Validate error handling validateErrorHandling(code, result); // Validate security validateSecurity(code, result); // Validate patterns based on context validateContextualPatterns(code, context, result, projectPatterns); // Check if target file exists and validate consistency if (targetFile && existsSync(targetFile)) { validateConsistency(code, targetFile, result); } // Calculate final score const errorCount = result.issues.filter(i => i.severity === 'error').length; const warningCount = result.issues.filter(i => i.severity === 'warning').length; result.score = Math.max(0, 100 - (errorCount * 20) - (warningCount * 5)); result.valid = errorCount === 0; return result; }
- src/tools/index.ts:60-80 (registration)MCP tool handler registration in the switch statement within setupTools. Parses input arguments with Zod schema and calls the validateGeneratedCode function, returning JSON result.case 'validate_generated_code': { const params = z.object({ code: z.string(), context: z.string(), targetFile: z.string().optional(), }).parse(args); const result = await validateGeneratedCode( params.code, params.context, params.targetFile ); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; }
- src/tools/tool-definitions.ts:29-51 (schema)Tool definition including name, description, and input schema used for tools/list response in MCP protocol.{ name: 'validate_generated_code', description: 'Validate generated code for patterns, security, and best practices', inputSchema: { type: 'object', properties: { code: { type: 'string', description: 'The generated code to validate', }, type: { type: 'string', enum: ['component', 'function', 'service', 'test', 'config'], description: 'Type of code being validated', }, targetFile: { type: 'string', description: 'Target file path for context', }, }, required: ['code', 'type'], }, },