find_duplicate_code
Identifies duplicate code blocks across multiple files to help developers maintain clean codebases and reduce redundancy.
Instructions
Find duplicate code blocks across files
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| files | Yes | File paths to analyze | |
| minLines | No | Minimum lines for duplication detection |
Implementation Reference
- src/tools/code-quality.ts:229-238 (handler)Handler function for the 'find_duplicate_code' tool. Reads input files, prepares options, calls CodeAnalyzer.analyzeCodeQuality, and returns the duplications from metrics.case 'find_duplicate_code': { const files = params.files as string[]; const codeFiles = await FileReader.readFiles(files.join(',')); const options: CodeAnalysisOptions = { checkDuplicates: true, minLinesForDuplication: params.minLines as number, }; const metrics = await codeAnalyzer.analyzeCodeQuality(codeFiles, options); return metrics.duplications; }
- src/tools/code-quality.ts:98-117 (schema)Tool definition including name, description, and input schema for 'find_duplicate_code'.{ name: 'find_duplicate_code', description: 'Find duplicate code blocks across files', inputSchema: { type: 'object', properties: { files: { type: 'array', items: { type: 'string' }, description: 'File paths to analyze', }, minLines: { type: 'number', description: 'Minimum lines for duplication detection', default: 5, }, }, required: ['files'], }, },
- src/server.ts:18-25 (registration)Registration of codeQualityTools (which includes 'find_duplicate_code') into the allTools array used by the MCP server.const allTools = [ ...codeAnalysisTools, ...codeQualityTools, ...dependencyAnalysisTools, ...lintingTools, ...webScrapingTools, ...apiDiscoveryTools, ];
- src/server.ts:64-65 (registration)Dispatch logic in MCP server that routes calls to 'find_duplicate_code' (in codeQualityTools) to the handleCodeQualityTool function.} else if (codeQualityTools.some((t) => t.name === name)) { result = await handleCodeQualityTool(name, args || {});
- Core helper method that implements duplicate code detection by pairwise file similarity comparison above 70% threshold.private async findDuplications( files: CodeFile[], options?: CodeAnalysisOptions ): Promise<import('../types/index.js').Duplication[]> { // This is a simplified version. In production, we'd use jscpd library const duplications: import('../types/index.js').Duplication[] = []; const minLines = options?.minLinesForDuplication || 5; for (let i = 0; i < files.length; i++) { for (let j = i + 1; j < files.length; j++) { const similarity = this.calculateSimilarity(files[i].content, files[j].content); if (similarity > 0.7 && files[i].lines >= minLines) { duplications.push({ lines: files[i].lines, tokens: 0, firstFile: files[i].path, secondFile: files[j].path, startLine1: 1, endLine1: files[i].lines, startLine2: 1, endLine2: files[j].lines, }); } } } return duplications; }