Skip to main content
Glama
suren2787

CodeGuard MCP Server

by suren2787

validate_code_security

Validate code snippets against security rules to identify vulnerabilities and provide remediation instructions for secure coding practices.

Instructions

Validate code snippet against security rules and return applicable instructions

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesCode snippet to validate
languageYesProgramming language of the code

Implementation Reference

  • The main handler function validateCodeSecurity that executes the tool logic. It validates code and language parameters, retrieves applicable security instructions using matchInstructions, and formats them into a response with applicable security rules and recommendations.
    function validateCodeSecurity( args: Record<string, unknown>, instructions: Instruction[] ) { const code = args.code as string; const language = args.language as string; if (!code || !language) { return { content: [ { type: 'text', text: 'Error: Both code and language are required', }, ], isError: true, }; } // Get applicable instructions const result = matchInstructions({ language }, instructions); // Build response with instructions and validation context const response = [ `# Security Validation for ${language.toUpperCase()} Code`, '', `Analyzing the provided code against ${result.instructions.length} security rules...`, '', '## Applicable Security Rules:', '', ]; result.instructions.forEach(i => { response.push(`### ${i.frontmatter.description}`); response.push(''); response.push(i.content); response.push(''); response.push('---'); response.push(''); }); response.push('## Recommendation:'); response.push('Review your code against the above security rules and ensure compliance.'); return { content: [ { type: 'text', text: response.join('\n'), }, ], isError: false, }; }
  • Tool registration definition in the listTools() function, specifying the tool name 'validate_code_security', description, and input schema with required 'code' and 'language' parameters.
    { name: 'validate_code_security', description: 'Validate code snippet against security rules and return applicable instructions', inputSchema: { type: 'object', properties: { code: { type: 'string', description: 'Code snippet to validate', }, language: { type: 'string', description: 'Programming language of the code', }, }, required: ['code', 'language'], }, },
  • Tool dispatch logic in callTool() that routes calls to the validate_code_security tool to the validateCodeSecurity handler function.
    if (name === 'validate_code_security') { return validateCodeSecurity(args, instructions); }
  • Helper function matchInstructions that scores and filters security instruction rules based on language, context, filepath, and criticality. Used by validateCodeSecurity to retrieve applicable security rules.
    export function matchInstructions( context: MatchContext, allInstructions: Instruction[] ): MatchResult { const scoredInstructions: ScoredInstruction[] = []; const matchedBy: MatchResult['metadata']['matchedBy'] = {}; // Score all instructions for (const instruction of allInstructions) { const scored = scoreInstruction(instruction, context); if (scored.score > 0) { scoredInstructions.push(scored); } } // Sort by priority (high to low), then by score scoredInstructions.sort((a, b) => { if (a.priority !== b.priority) { return b.priority - a.priority; } return b.score - a.score; }); // Count matches by type matchedBy.critical = scoredInstructions.filter(s => s.priority === Priority.CRITICAL).length; matchedBy.language = scoredInstructions.filter(s => s.matchReasons.includes('language')).length; matchedBy.filepath = scoredInstructions.filter(s => s.matchReasons.includes('filepath')).length; matchedBy.context = scoredInstructions.filter(s => s.matchReasons.includes('context')).length; // Priority breakdown const priorityBreakdown = { critical: scoredInstructions.filter(s => s.priority === Priority.CRITICAL).length, high: scoredInstructions.filter(s => s.priority === Priority.HIGH).length, medium: scoredInstructions.filter(s => s.priority === Priority.MEDIUM).length, low: scoredInstructions.filter(s => s.priority === Priority.LOW).length, }; // Limit to top 15 rules to keep response size manageable const topInstructions = scoredInstructions.slice(0, 15); return { instructions: topInstructions.map(s => s.instruction), metadata: { totalMatched: scoredInstructions.length, matchedBy, priorityBreakdown, }, }; }
  • Input schema definition for the validate_code_security tool, specifying that it requires 'code' (string) and 'language' (string) as mandatory parameters.
    inputSchema: { type: 'object', properties: { code: { type: 'string', description: 'Code snippet to validate', }, language: { type: 'string', description: 'Programming language of the code', }, }, required: ['code', 'language'], },

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/suren2787/codeguard-mcp-server'

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