validate_code
Validates code in PHP, JavaScript, CSS, or HTML against configured coding standards.
Instructions
Validate code against configured coding standards
Input 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)Tool registration: 'validate_code' is defined with name, description, and inputSchema (requires 'code' and 'language' with language enum of php, javascript, css, html).
{ 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:140-169 (handler)Handler: The private method validateCode in GuidelinesManager delegates to the guidelineSource.validateCode() and formats the result into issues/suggestions or a pass message.
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'}`); } } - Helper: UrlGuidelineSource.validateCode fetches validation-rules guidelines and then parses them against the provided code and language.
async validateCode(code: string, language: string): Promise<{ issues: string[]; suggestions: string[] }> { const guidelines = await this.fetchGuidelines('validation-rules'); return this.parseValidationRules(guidelines, code, language); } - Helper: Parses validation rules from markdown guidelines, extracts rules under 'VALIDATION_RULES' section, and applies each rule to the code, collecting 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 }; } - Schema: The GuidelineSource interface defines the contract for validateCode, returning an object with issues (string[]) and suggestions (string[]).
export interface GuidelineSource { fetchGuidelines(category?: string): Promise<string>; validateCode(code: string, language: string): Promise<{ issues: string[]; suggestions: string[] }>; performSecurityCheck(code: string): Promise<{ vulnerabilities: string[]; warnings: string[]; recommendations: string[] }>; }