validate_code
Validate PHP, JavaScript, CSS, or HTML code against WordPress coding standards to ensure compliance with best practices and maintain code quality.
Instructions
Validate code against configured coding standards
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | The code to validate | |
| language | Yes | The programming language of the code |
Implementation Reference
- src/guidelines-manager.ts:21-39 (registration)Registration of the 'validate_code' tool in GuidelinesManager.getTools(), including name, description, and input schema.{ name: 'validate_code', description: 'Validate code against configured coding standards', inputSchema: { type: 'object', properties: { code: { type: 'string', description: 'The code to validate', }, language: { type: 'string', enum: ['php', 'javascript', 'css', 'html'], description: 'The programming language of the code', }, }, required: ['code', 'language'], }, },
- src/guidelines-manager.ts:24-38 (schema)Input schema (JSON Schema) for the validate_code tool defining required parameters 'code' and 'language'.inputSchema: { type: 'object', properties: { code: { type: 'string', description: 'The code to validate', }, language: { type: 'string', enum: ['php', 'javascript', 'css', 'html'], description: 'The programming language of the code', }, }, required: ['code', 'language'], },
- src/guidelines-manager.ts:140-169 (handler)Primary handler for validate_code tool: invokes guidelineSource.validateCode, formats results (issues/suggestions) into MCP text response.private async validateCode(code: string, language: string) { try { const result = await this.guidelineSource.validateCode(code, language); const response = []; if (result.issues.length > 0) { response.push(`❌ **Issues Found:**\n${result.issues.map(issue => `- ${issue}`).join('\n')}`); } if (result.suggestions.length > 0) { response.push(`💡 **Suggestions:**\n${result.suggestions.map(suggestion => `- ${suggestion}`).join('\n')}`); } if (response.length === 0) { response.push('✅ Code validation passed. No issues detected.'); } return { content: [ { type: 'text', text: response.join('\n\n'), }, ], }; } catch (error) { throw new Error(`Code validation failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- validateCode implementation in UrlGuidelineSource: fetches validation rules and delegates to parseValidationRules.async validateCode(code: string, language: string): Promise<{ issues: string[]; suggestions: string[] }> { const guidelines = await this.fetchGuidelines('validation-rules'); return this.parseValidationRules(guidelines, code, language); }
- Core helper function parseValidationRules: extracts rules from guidelines markdown, applies each rule to code, collects issues and suggestions.private parseValidationRules(guidelines: string, code: string, language: string): { issues: string[]; suggestions: string[] } { const issues: string[] = []; const suggestions: string[] = []; // Parse markdown format guidelines and apply to code const rules = this.extractRules(guidelines, 'VALIDATION_RULES'); for (const rule of rules) { const result = this.applyRule(rule, code, language); if (result.violation) { issues.push(result.message); } else if (result.suggestion) { suggestions.push(result.message); } } return { issues, suggestions }; }