extract_patterns
Identify and extract architectural, design, naming, and testing patterns from codebases to analyze project structure and coding practices.
Instructions
Identify and extract architectural and coding patterns from the codebase
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Root directory path | |
| pattern_types | No | Types of patterns to extract (architectural, design, naming, testing) | |
| min_occurrences | No | Minimum occurrences to consider a pattern |
Implementation Reference
- src/tools/extract-patterns.ts:28-96 (handler)Core handler function for the 'extract_patterns' tool. Scans the codebase, extracts architectural/design/naming/testing patterns from code files, aggregates occurrences, enriches with KG data using YAGO resolver, stores in database, and returns formatted markdown results.export async function extractPatterns( args: ExtractPatternsArgs ): Promise<{ content: Array<{ type: string; text: string }> }> { const { path, pattern_types = ['architectural', 'design', 'naming', 'testing'], min_occurrences = 3 } = args; const patterns: Record<string, DetectedPattern[]> = { architectural: [], design: [], naming: [], testing: [], }; // Scan codebase for patterns const files = await getCodeFiles(path); for (const file of files) { const content = await readFile(file, 'utf-8'); const relPath = relative(path, file); // Extract different pattern types if (pattern_types.includes('architectural')) { const archPatterns = extractArchitecturalPatterns(content, relPath); patterns.architectural.push(...archPatterns); } if (pattern_types.includes('design')) { const designPatterns = extractDesignPatterns(content, relPath); patterns.design.push(...designPatterns); } if (pattern_types.includes('naming')) { const namingPatterns = extractNamingPatterns(content, relPath); patterns.naming.push(...namingPatterns); } if (pattern_types.includes('testing')) { const testPatterns = extractTestingPatterns(content, relPath); patterns.testing.push(...testPatterns); } } // Aggregate and filter by min_occurrences const aggregated: Record<string, DetectedPattern[]> = {}; for (const [type, typePatterns] of Object.entries(patterns)) { const grouped = groupPatterns(typePatterns, min_occurrences); aggregated[type] = grouped; // Enrich with knowledge graph for (const pattern of grouped) { await enrichPatternWithKG(pattern); } } // Store in database await storePatternsInDB(aggregated); // Format results const results = formatPatternResults(aggregated); return { content: [ { type: 'text', text: results, }, ], }; }
- src/index.ts:202-221 (schema)Input schema definition for the extract_patterns tool used in tool listing (ListToolsRequest). Defines parameters: path (required), pattern_types (array), min_occurrences (number, default 3).inputSchema: { type: "object", properties: { path: { type: "string", description: "Root directory path", }, pattern_types: { type: "array", items: { type: "string" }, description: "Types of patterns to extract (architectural, design, naming, testing)", }, min_occurrences: { type: "number", description: "Minimum occurrences to consider a pattern", default: 3, }, }, required: ["path"], },
- src/tools/index.ts:49-51 (registration)Tool handler registration: switch case dispatching calls to extract_patterns to the imported extractPatterns function in registerTools(server).case "extract_patterns": return await extractPatterns(args as any);
- src/index.ts:199-222 (registration)Tool registration in ListToolsRequest handler: defines name, description, and inputSchema for extract_patterns.{ name: "extract_patterns", description: "Identify and extract architectural and coding patterns from the codebase", inputSchema: { type: "object", properties: { path: { type: "string", description: "Root directory path", }, pattern_types: { type: "array", items: { type: "string" }, description: "Types of patterns to extract (architectural, design, naming, testing)", }, min_occurrences: { type: "number", description: "Minimum occurrences to consider a pattern", default: 3, }, }, required: ["path"], }, },
- Helper function to extract architectural patterns like MVC, Repository, Service Layer from file paths and content.function extractArchitecturalPatterns(content: string, file: string): DetectedPattern[] { const patterns: DetectedPattern[] = []; // MVC pattern if (file.includes('/controllers/') || content.includes('Controller')) { patterns.push({ type: 'architectural', name: 'MVC', occurrences: 1, examples: [file], confidence: 0.85, }); } // Repository pattern if (file.includes('/repositories/') || content.match(/class \w+Repository/)) { patterns.push({ type: 'architectural', name: 'Repository Pattern', occurrences: 1, examples: [file], confidence: 0.9, }); } // Service layer if (file.includes('/services/') || content.match(/class \w+Service/)) { patterns.push({ type: 'architectural', name: 'Service Layer', occurrences: 1, examples: [file], confidence: 0.85, }); } // Dependency injection if (content.includes('inject(') || content.includes('@Injectable') || content.includes('constructor(')) { patterns.push({ type: 'architectural', name: 'Dependency Injection', occurrences: 1, examples: [file], confidence: 0.8, }); } // Microservices if (file.includes('/microservices/') || file.includes('/services/') && content.includes('express')) { patterns.push({ type: 'architectural', name: 'Microservices', occurrences: 1, examples: [file], confidence: 0.75, }); } return patterns; }