Skip to main content
Glama
bswa006
by bswa006

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
NameRequiredDescriptionDefault
codeYesThe generated code to validate
targetFileNoTarget file path for context
typeYesType 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; }
  • 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), }, ], }; }
  • 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'], }, },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/bswa006/mcp-context-manager'

If you have feedback or need assistance with the MCP directory API, please join our Discord server