count_patterns
Count design patterns in the database to assess available solutions for programming problems, with optional category breakdown.
Instructions
Get the total number of design patterns in the database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| includeDetails | No | Include breakdown by category |
Implementation Reference
- src/mcp-server.ts:491-535 (handler)Primary handler function for 'count_patterns' tool. Validates args using InputValidator, executes efficient SQL COUNT(*) query for total patterns, and optional GROUP BY category for breakdown. Returns formatted markdown content with results.private handleCountPatterns(args: unknown): CallToolResult { try { const validatedArgs = InputValidator.validateCountPatternsArgs(args); // OPTIMIZATION: Use COUNT instead of loading all rows const totalResult = this.db.queryOne<{ total: number }>( 'SELECT COUNT(*) as total FROM patterns' ); const total = totalResult?.total || 0; if (validatedArgs.includeDetails) { // Get category breakdown efficiently const breakdown = this.db.query<{ category: string; count: number }>( 'SELECT category, COUNT(*) as count FROM patterns GROUP BY category ORDER BY count DESC' ); return { content: [ { type: 'text', text: `## Total Design Patterns: ${total}\n\n` + `### Breakdown by Category:\n` + breakdown.map(item => `- **${item.category}**: ${item.count} patterns`).join('\n') + '\n\n' + `*Total patterns from all sources: ${total}*`, }, ], }; } else { return { content: [ { type: 'text', text: `Total design patterns in database: **${total}**`, }, ], }; } } catch (error) { throw new McpError( ErrorCode.InternalError, `Pattern count failed: ${error instanceof Error ? error.message : 'Unknown error'}` ); } }
- src/mcp-server.ts:225-237 (registration)Tool registration in ListToolsRequestSchema handler. Defines name, description, and inputSchema for the count_patterns tool.name: 'count_patterns', description: 'Get the total number of design patterns in the database', inputSchema: { type: 'object', properties: { includeDetails: { type: 'boolean', description: 'Include breakdown by category', default: false, }, }, }, },
- src/types/mcp-args.ts:101-109 (schema)TypeScript interface and type guard for CountPatternsArgs, defining the expected input structure for the tool.export interface CountPatternsArgs { includeDetails?: boolean; } export function isCountPatternsArgs(args: unknown): args is CountPatternsArgs { if (typeof args !== 'object' || args === null) return false; const a = args as Record<string, unknown>; return a.includeDetails === undefined || typeof a.includeDetails === 'boolean'; }
- Input validation helper specifically for count_patterns tool arguments. Throws McpError on invalid input, sanitizes includeDetails boolean./** * Validates all inputs for count_patterns tool */ static validateCountPatternsArgs(args: unknown): { includeDetails: boolean; } { if (typeof args !== 'object' || args === null) { throw new McpError(ErrorCode.InvalidParams, 'Invalid arguments: expected object'); } const obj = args as Record<string, unknown>; const includeDetailsResult = this.validateIncludeDetails(obj.includeDetails); this.throwIfInvalid(includeDetailsResult); return { includeDetails: (includeDetailsResult.sanitized as boolean) ?? false, }; }