detect_existing_patterns
Analyze codebases to identify existing patterns in naming, structure, imports, testing, and styling for consistent development.
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
- The core handler function that executes the tool logic: scans a directory for files of a given type, detects patterns across imports, components, hooks, state management, error handling, and styling, computes frequencies/confidence, and generates recommendations.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; }
- src/tools/index.ts:122-140 (registration)The switch case in the main tool dispatcher that handles calls to 'detect_existing_patterns', parses arguments, invokes the handler, and formats the MCP 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), }, ], }; }
- The tool definition including name, description, and input schema for 'detect_existing_patterns', used for listing tools and validation.{ 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:27-30 (registration)Registration of the tools/list handler that returns the toolDefinitions array including detect_existing_patterns.server.setRequestHandler(ListToolsRequestSchema, async () => { console.error(`Handling tools/list request, returning ${toolDefinitions.length} tools`); return { tools: toolDefinitions }; });
- Calls to helper functions that perform specific pattern detection (imports, components, etc.) and generate recommendations.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; }