Skip to main content
Glama
bswa006

AI Agent Template MCP Server

by bswa006

validate_generated_code

Check generated code for security issues, patterns, and best practices to ensure quality and reliability in AI agent development.

Instructions

Validate generated code for patterns, security, and best practices

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesThe generated code to validate
typeYesType of code being validated
targetFileNoTarget file path for context

Implementation Reference

  • Core handler function that executes the validate_generated_code tool logic. Performs comprehensive validation including common issues, naming, error handling, security, contextual patterns, and consistency checks.
    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;
    }
  • Registers the tool handlers for list and call. The switch statement includes the specific handler dispatch for 'validate_generated_code' which parses input and calls the validator function.
    export function setupTools(server: Server) {
      // Handle tool listing
      server.setRequestHandler(ListToolsRequestSchema, async () => {
        console.error(`Handling tools/list request, returning ${toolDefinitions.length} tools`);
        return { tools: toolDefinitions };
      });
    
      // Handle tool execution
      server.setRequestHandler(CallToolRequestSchema, async (request) => {
        const { name, arguments: args } = request.params;
    
        try {
          switch (name) {
            case 'check_before_suggesting': {
              const params = z.object({
                imports: z.array(z.string()),
                methods: z.array(z.string()),
                patterns: z.array(z.string()).optional(),
              }).parse(args);
              
              const result = await checkBeforeSuggesting(
                params.imports,
                params.methods,
                params.patterns
              );
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify(result, null, 2),
                  },
                ],
              };
            }
    
            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),
                  },
                ],
              };
            }
    
            case 'get_pattern_for_task': {
              const params = z.object({
                taskType: z.enum(['component', 'hook', 'service', 'api', 'test', 'error-handling']),
                requirements: z.array(z.string()).optional(),
              }).parse(args);
              
              const pattern = await getPatternForTask(
                params.taskType,
                params.requirements
              );
              return {
                content: [
                  {
                    type: 'text',
                    text: pattern,
                  },
                ],
              };
            }
    
            case 'check_security_compliance': {
              const params = z.object({
                code: z.string(),
                sensitiveOperations: z.array(z.string()).optional(),
              }).parse(args);
              
              const result = await checkSecurityCompliance(
                params.code,
                params.sensitiveOperations
              );
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify(result, null, 2),
                  },
                ],
              };
            }
    
            case 'detect_existing_patterns': {
              const params = z.object({
                directory: z.string(),
                fileType: z.string(),
              }).parse(args);
              
              const patterns = await detectExistingPatterns(
                params.directory,
                params.fileType
              );
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify(patterns, null, 2),
                  },
                ],
              };
            }
    
            case 'initialize_agent_workspace': {
              const params = z.object({
                projectPath: z.string(),
                projectName: z.string(),
                techStack: z.object({
                  language: z.string().optional(),
                  framework: z.string().optional(),
                  uiLibrary: z.string().optional(),
                  testFramework: z.string().optional(),
                }).optional(),
              }).parse(args);
              
              const result = await initializeAgentWorkspace(params);
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify(result, null, 2),
                  },
                ],
              };
            }
    
            case 'generate_tests_for_coverage': {
              const params = z.object({
                targetFile: z.string(),
                testFramework: z.enum(['jest', 'vitest', 'mocha']).optional(),
                coverageTarget: z.number().optional(),
                includeEdgeCases: z.boolean().optional(),
                includeAccessibility: z.boolean().optional(),
              }).parse(args);
              
              const result = await generateTestsForCoverage(params);
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify(result, null, 2),
                  },
                ],
              };
            }
    
            case 'track_agent_performance': {
              const params = z.object({
                featureName: z.string(),
                timestamp: z.string(),
                metrics: z.object({
                  tokensUsed: z.number(),
                  timeElapsed: z.number(),
                  validationScore: z.number(),
                  securityScore: z.number(),
                  testCoverage: z.number(),
                  hallucinations: z.object({
                    detected: z.number(),
                    prevented: z.number(),
                    examples: z.array(z.string()),
                  }).optional().default({
                    detected: 0,
                    prevented: 0,
                    examples: [],
                  }),
                  errors: z.object({
                    syntax: z.number(),
                    runtime: z.number(),
                    type: z.number(),
                  }).optional().default({
                    syntax: 0,
                    runtime: 0,
                    type: 0,
                  }),
                }),
                improvements: z.object({
                  tokenReduction: z.number().optional(),
                  timeReduction: z.number().optional(),
                  qualityIncrease: z.number().optional(),
                }).optional(),
              }).parse(args);
              
              const result = await trackAgentPerformance(params);
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify(result, null, 2),
                  },
                ],
              };
            }
    
            case 'create_conversation_starters': {
              const params = z.object({
                projectPath: z.string(),
                analysisId: z.string().optional(),
                includeQuickTasks: z.boolean().optional(),
                includeCurrentWork: z.boolean().optional(),
                tokenLimit: z.number().optional(),
                customTasks: z.array(z.string()).optional(),
              }).parse(args);
              
              const result = await createConversationStarters(params);
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify(result, null, 2),
                  },
                ],
              };
            }
    
            case 'create_token_optimizer': {
              const params = z.object({
                projectPath: z.string(),
                analysisId: z.string().optional(),
                tiers: z.array(z.enum(['minimal', 'standard', 'comprehensive'])).optional(),
                trackUsage: z.boolean().optional(),
                generateMetrics: z.boolean().optional(),
              }).parse(args);
              
              const result = await createTokenOptimizer(params);
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify(result, null, 2),
                  },
                ],
              };
            }
    
            case 'create_ide_configs': {
              const params = z.object({
                projectPath: z.string(),
                analysisId: z.string().optional(),
                ide: z.enum(['cursor', 'vscode', 'intellij', 'all']),
                autoLoadContext: z.boolean().optional(),
                customRules: z.array(z.string()).optional(),
                includeDebugConfigs: z.boolean().optional(),
              }).parse(args);
              
              const result = await createIDEConfigs(params);
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify(result, null, 2),
                  },
                ],
              };
            }
    
            case 'setup_persistence_automation': {
              const params = z.object({
                projectPath: z.string(),
                analysisId: z.string().optional(),
                updateSchedule: z.enum(['daily', 'weekly', 'on-change', 'manual']),
                gitHooks: z.boolean().optional(),
                monitoring: z.boolean().optional(),
                notifications: z.object({
                  email: z.string().optional(),
                  slack: z.string().optional(),
                }).optional(),
              }).parse(args);
              
              const result = await setupPersistenceAutomation(params);
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify(result, null, 2),
                  },
                ],
              };
            }
    
            case 'create_maintenance_workflows': {
              const params = z.object({
                projectPath: z.string(),
                analysisId: z.string().optional(),
                teamSize: z.number(),
                updateFrequency: z.enum(['daily', 'weekly', 'biweekly', 'monthly']),
                includeChecklists: z.boolean().optional(),
                includeMetrics: z.boolean().optional(),
                includeTraining: z.boolean().optional(),
              }).parse(args);
              
              const result = await createMaintenanceWorkflows(params);
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify(result, null, 2),
                  },
                ],
              };
            }
    
            case 'analyze_codebase_deeply': {
              const params = z.object({
                projectPath: z.string(),
                maxDepth: z.number().optional(),
                excludePatterns: z.array(z.string()).optional(),
              }).parse(args);
              
              const result = await analyzeCodebaseDeeply(params);
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify(result, null, 2),
                  },
                ],
              };
            }
    
            case 'complete_setup_workflow': {
              const params = z.object({
                projectPath: z.string(),
                projectName: z.string(),
                teamSize: z.number().optional(),
                updateSchedule: z.enum(['daily', 'weekly', 'on-change', 'manual']).optional(),
                ide: z.enum(['cursor', 'vscode', 'intellij', 'all']).optional(),
                includeAll: z.boolean().optional(),
              }).parse(args);
              
              const result = await completeSetupWorkflow(params);
              return {
                content: [
                  {
                    type: 'text',
                    text: JSON.stringify(result, null, 2),
                  },
                ],
              };
            }
    
            default:
              throw new Error(`Unknown tool: ${name}`);
          }
        } catch (error) {
          throw new Error(`Tool execution failed: ${error}`);
        }
      });
    }
  • Tool definition including name, description, and input schema for validate_generated_code.
    {
      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'],
      },
    },
  • Zod schema used in the tool handler for input validation (note: uses 'context' instead of 'type').
    const params = z.object({
      code: z.string(),
      context: z.string(),
      targetFile: z.string().optional(),
    }).parse(args);
  • Type definition for the validation result returned by the handler.
    interface ValidationResult {
      valid: boolean;
      score: number; // 0-100
      issues: {
        severity: 'error' | 'warning' | 'info';
        category: string;
        message: string;
        line?: number;
        suggestion: string;
      }[];
      patterns: {
        followed: string[];
        violated: string[];
      };
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool validates code but doesn't explain what 'validate' entails—e.g., whether it returns errors, warnings, a score, or specific feedback. It also lacks details on permissions, rate limits, or side effects, leaving the agent uncertain about the tool's behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence: 'Validate generated code for patterns, security, and best practices.' It's front-loaded with the core purpose and wastes no words, making it easy for an agent to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of validation (which can involve detailed analysis), no annotations, and no output schema, the description is incomplete. It doesn't explain what the tool returns (e.g., validation results, errors, or a summary), leaving the agent guessing about the output format and usefulness in context.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all parameters ('code', 'type', 'targetFile') with descriptions and an enum for 'type'. The description adds no additional meaning beyond what the schema provides, such as explaining how validation might differ by 'type' or the role of 'targetFile'. Baseline 3 is appropriate as the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Validate generated code for patterns, security, and best practices.' It specifies the verb (validate) and resource (generated code) with three validation domains. However, it doesn't explicitly differentiate from sibling tools like 'check_security_compliance' or 'detect_existing_patterns,' which might overlap in functionality.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites, context, or exclusions, and with siblings like 'check_security_compliance' and 'detect_existing_patterns,' there's no clarification on how this tool differs or when it's preferred.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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