detect_code_smells
Identify problematic patterns in code files to improve maintainability and reduce technical debt by analyzing complexity and other quality metrics.
Instructions
Detect code smells in code files
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| files | Yes | File paths to analyze | |
| maxComplexity | No | Maximum allowed complexity |
Implementation Reference
- src/tools/code-quality.ts:207-216 (handler)Handler for the 'detect_code_smells' tool: parses input, reads code files, runs analysis via CodeAnalyzer, returns detected code smells.
case 'detect_code_smells': { const files = params.files as string[]; const codeFiles = await FileReader.readFiles(files.join(',')); const options: CodeAnalysisOptions = { maxComplexity: params.maxComplexity as number, checkCodeSmells: true, }; const metrics = await codeAnalyzer.analyzeCodeQuality(codeFiles, options); return metrics.codeSmells; } - src/tools/code-quality.ts:66-80 (schema)Input schema validating files (required) and optional maxComplexity for the tool.
inputSchema: { type: 'object', properties: { files: { type: 'array', items: { type: 'string' }, description: 'File paths to analyze', }, maxComplexity: { type: 'number', description: 'Maximum allowed complexity', default: 10, }, }, required: ['files'], - src/tools/code-quality.ts:63-82 (registration)Tool definition and registration in codeQualityTools export array.
{ name: 'detect_code_smells', description: 'Detect code smells in code files', inputSchema: { type: 'object', properties: { files: { type: 'array', items: { type: 'string' }, description: 'File paths to analyze', }, maxComplexity: { type: 'number', description: 'Maximum allowed complexity', default: 10, }, }, required: ['files'], }, }, - src/server.ts:18-25 (registration)MCP server registration: codeQualityTools (including detect_code_smells) added to allTools for list tools capability.
const allTools = [ ...codeAnalysisTools, ...codeQualityTools, ...dependencyAnalysisTools, ...lintingTools, ...webScrapingTools, ...apiDiscoveryTools, ]; - Core helper function implementing code smell detection logic: long methods/files, high complexity, deep nesting, magic numbers, and simple duplicate detection.
private detectCodeSmells(files: CodeFile[], options?: CodeAnalysisOptions): CodeSmell[] { const smells: CodeSmell[] = []; for (const file of files) { const lines = file.content.split('\n'); // Long method detection if (file.lines > 100) { smells.push({ type: 'long_method', severity: file.lines > 200 ? 'high' : 'medium', location: file.path, description: `Method/file is too long (${file.lines} lines). Consider breaking it down.`, suggestion: 'Extract methods or split into multiple files.', }); } // High complexity detection const complexity = this.calculateComplexity(file.content); if (complexity > (options?.maxComplexity || 10)) { smells.push({ type: 'high_complexity', severity: complexity > 20 ? 'high' : 'medium', location: file.path, line: 1, description: `High cyclomatic complexity (${complexity}).`, suggestion: 'Refactor to reduce complexity by extracting methods.', }); } // Deep nesting detection let maxNesting = 0; let currentNesting = 0; for (let i = 0; i < lines.length; i++) { const line = lines[i]; const openBraces = (line.match(/{/g) || []).length; const closeBraces = (line.match(/}/g) || []).length; currentNesting += openBraces - closeBraces; maxNesting = Math.max(maxNesting, currentNesting); } if (maxNesting > 4) { smells.push({ type: 'deep_nesting', severity: maxNesting > 6 ? 'high' : 'medium', location: file.path, description: `Deep nesting detected (${maxNesting} levels).`, suggestion: 'Extract nested code into separate functions.', }); } // Magic numbers detection const magicNumberPattern = /\b\d{3,}\b/g; const magicNumbers = file.content.match(magicNumberPattern); if (magicNumbers && magicNumbers.length > 5) { smells.push({ type: 'magic_numbers', severity: 'low', location: file.path, description: `Multiple magic numbers detected (${magicNumbers.length}).`, suggestion: 'Extract magic numbers into named constants.', }); } // Duplicate code detection (simple) const duplicatePattern = /(.{20,})\1{2,}/g; if (duplicatePattern.test(file.content)) { smells.push({ type: 'duplicate_code', severity: 'medium', location: file.path, description: 'Potential duplicate code detected.', suggestion: 'Extract common code into reusable functions.', }); } } return smells; }