Skip to main content
Glama

calculate_complexity

Analyze code cyclomatic complexity to measure structural complexity and identify potential maintenance issues in software files.

Instructions

Calculate cyclomatic complexity for code files

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filesYesFile paths to analyze

Implementation Reference

  • Handler for the 'calculate_complexity' tool. Parses input files, reads their contents using FileReader, computes complexity using ComplexityAnalyzer, and returns the result.
    case 'calculate_complexity': { const files = params.files as string[]; const codeFiles = await FileReader.readFiles(files.join(',')); const result = complexityAnalyzer.analyzeComplexity(codeFiles); return result; }
  • Registration of the 'calculate_complexity' tool in the codeQualityTools array, including name, description, and input schema.
    { name: 'calculate_complexity', description: 'Calculate cyclomatic complexity for code files', inputSchema: { type: 'object', properties: { files: { type: 'array', items: { type: 'string' }, description: 'File paths to analyze', }, }, required: ['files'], }, },
  • Input schema definition for the 'calculate_complexity' tool, specifying required 'files' array of strings.
    inputSchema: { type: 'object', properties: { files: { type: 'array', items: { type: 'string' }, description: 'File paths to analyze', }, }, required: ['files'], },
  • Helper function analyzeComplexity in ComplexityAnalyzer class that computes complexity metrics (average, max, min, per-file) for multiple files by delegating to calculateCyclomaticComplexity.
    analyzeComplexity(files: CodeFile[]): { average: number; max: number; min: number; files: Array<{ path: string; complexity: number }>; } { if (files.length === 0) { return { average: 0, max: 0, min: 0, files: [], }; } const complexities = files.map((file) => ({ path: file.path, complexity: this.calculateCyclomaticComplexity(file.content), })); const values = complexities.map((c) => c.complexity); const average = values.reduce((sum, val) => sum + val, 0) / values.length; const max = Math.max(...values); const min = Math.min(...values); return { average: Math.round(average * 100) / 100, max, min, files: complexities, }; }
  • Core helper function calculateComplexity that implements cyclomatic complexity calculation by counting matches of control flow keywords and operators in the code string.
    calculateComplexity(code: string): number { // Simple complexity calculation based on control flow statements const complexityKeywords = [ /\bif\s*\(/g, /\belse\s*{/g, /\bfor\s*\(/g, /\bwhile\s*\(/g, /\bswitch\s*\(/g, /\bcase\s+/g, /\bcatch\s*\(/g, /\b&&/g, /\b\|\|/g, /\?\s*.*\s*:/g, // ternary operator ]; let complexity = 1; // Base complexity for (const pattern of complexityKeywords) { const matches = code.match(pattern); if (matches) { complexity += matches.length; } } return complexity; }

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/code-alchemist01/development-tools-mcp-Server'

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