analyze_maintainability
Calculate maintainability index for code files to assess software quality and identify improvement areas in development workflows.
Instructions
Calculate maintainability index for code
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"files": "<files>"
},
"name": "analyze_maintainability"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| files | Yes | File paths to analyze |
Implementation Reference
- src/tools/code-quality.ts:218-227 (handler)The switch case handler that executes the 'analyze_maintainability' tool. It processes input files, uses CodeAnalyzer to compute metrics, and returns maintainability index, complexity, and lines of code.
case 'analyze_maintainability': { const files = params.files as string[]; const codeFiles = await FileReader.readFiles(files.join(',')); const metrics = await codeAnalyzer.analyzeCodeQuality(codeFiles); return { maintainabilityIndex: metrics.maintainabilityIndex, complexity: metrics.complexity, linesOfCode: metrics.linesOfCode, }; } - src/tools/code-quality.ts:83-97 (schema)The tool definition including name, description, and input schema for 'analyze_maintainability'.
{ name: 'analyze_maintainability', description: 'Calculate maintainability index for code', inputSchema: { type: 'object', properties: { files: { type: 'array', items: { type: 'string' }, description: 'File paths to analyze', }, }, required: ['files'], }, }, - src/server.ts:64-65 (registration)Registration and dispatch logic in the main server that routes calls to 'analyze_maintainability' (via codeQualityTools check) to the handleCodeQualityTool function.
} else if (codeQualityTools.some((t) => t.name === name)) { result = await handleCodeQualityTool(name, args || {}); - src/analyzers/code-analyzer.ts:8-33 (helper)Core analysis method in CodeAnalyzer that computes the maintainabilityIndex along with other code quality metrics, called by the tool handler.
async analyzeCodeQuality( files: CodeFile[] | string[], options?: CodeAnalysisOptions ): Promise<CodeQualityMetrics> { const codeFiles = await this.getCodeFiles(files); const totalLines = codeFiles.reduce((sum, file) => sum + file.lines, 0); const complexity = this.calculateAverageComplexity(codeFiles); const maintainabilityIndex = this.calculateMaintainabilityIndex(codeFiles, complexity); const codeSmells = options?.checkCodeSmells !== false ? this.detectCodeSmells(codeFiles, options) : []; const duplications = options?.checkDuplicates !== false ? await this.findDuplications(codeFiles, options) : []; return { complexity, maintainabilityIndex, technicalDebt: this.estimateTechnicalDebt(codeSmells, duplications.length), codeSmells, duplications, linesOfCode: totalLines, cyclomaticComplexity: complexity, }; } - src/analyzers/code-analyzer.ts:82-94 (helper)The private method that implements the maintainability index formula using Halstead volume, complexity, and lines of code.
private calculateMaintainabilityIndex(files: CodeFile[], complexity: number): number { if (files.length === 0) return 100; const totalLines = files.reduce((sum, file) => sum + file.lines, 0); const avgLines = totalLines / files.length; // Simplified maintainability index calculation const halsteadVolume = this.estimateHalsteadVolume(files); const mi = 171 - 5.2 * Math.log(halsteadVolume || 1) - 0.23 * complexity - 16.2 * Math.log(avgLines || 1); // Clamp between 0 and 100 return Math.max(0, Math.min(100, Math.round(mi * 100) / 100)); }