detect_existing_patterns
Analyze codebase directories to identify naming, structure, imports, testing, and styling patterns for maintaining consistency and improving code quality.
Instructions
Analyze existing codebase to detect patterns and conventions
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| directory | Yes | Directory to analyze | |
| patternTypes | No | Types of patterns to detect |
Implementation Reference
- Core handler function that scans files in the given directory for various code patterns (imports, components, hooks, state management, error handling, styling), computes frequencies and confidence scores, and generates recommendations based on detected conventions.export async function detectExistingPatterns( directory: string, fileType: string ): Promise<PatternAnalysis> { const analysis: PatternAnalysis = { directory, fileType, patterns: { imports: [], components: [], hooks: [], stateManagement: [], errorHandling: [], styling: [], }, recommendations: [], }; try { // Find all relevant files const files = findFiles(directory, fileType); if (files.length === 0) { analysis.recommendations.push(`No ${fileType} files found in ${directory}`); return analysis; } // Analyze each file const fileContents = files.map(file => ({ path: file, content: readFileSync(file, 'utf-8'), })); // Detect patterns analysis.patterns.imports = detectImportPatterns(fileContents); analysis.patterns.components = detectComponentPatterns(fileContents, fileType); analysis.patterns.hooks = detectHookPatterns(fileContents); analysis.patterns.stateManagement = detectStatePatterns(fileContents); analysis.patterns.errorHandling = detectErrorPatterns(fileContents); analysis.patterns.styling = detectStylingPatterns(fileContents); // Generate recommendations generateRecommendations(analysis); } catch (error) { analysis.recommendations.push(`Error analyzing directory: ${error}`); } return analysis; }
- Input schema definition for the tool, specifying required 'directory' parameter and optional 'patternTypes' for targeted analysis.name: 'detect_existing_patterns', description: 'Analyze existing codebase to detect patterns and conventions', inputSchema: { type: 'object', properties: { directory: { type: 'string', description: 'Directory to analyze', }, patternTypes: { type: 'array', items: { type: 'string', enum: ['naming', 'structure', 'imports', 'testing', 'styling'], }, description: 'Types of patterns to detect', }, }, required: ['directory'], }, },
- src/tools/index.ts:122-140 (registration)Registration and execution handler in the main tool dispatcher; validates input with Zod (adapting schema to use 'fileType'), invokes the detectExistingPatterns handler, and formats response.case 'detect_existing_patterns': { const params = z.object({ directory: z.string(), fileType: z.string(), }).parse(args); const patterns = await detectExistingPatterns( params.directory, params.fileType ); return { content: [ { type: 'text', text: JSON.stringify(patterns, null, 2), }, ], }; }