reflect_on_code
Analyze code for quality, security, performance, and maintainability issues. Receive structured critiques with specific improvement suggestions to enhance your programming projects.
Instructions
Provides a structured critique of code, analyzing quality, security, performance, and maintainability. Returns specific improvement suggestions.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | The code to analyze | |
| language | Yes | Programming language of the code | |
| focus | No | Areas to focus analysis on |
Implementation Reference
- src/tools/cognitive.ts:90-165 (handler)The handler function that implements the core logic of the 'reflect_on_code' tool. It takes code, language, and optional focus areas, then generates a structured markdown report analyzing quality, security, performance, and maintainability.export function reflectOnCodeHandler(args: any) { const { code, language, focus = ["all"] } = args; const lines = code.split("\n").length; const reflection = `# Code Reflection: ${language} ## Overview - **Lines of Code**: ${lines} - **Language**: ${language} - **Focus Areas**: ${focus.join(", ")} ## Quality Analysis ${focus.includes("quality") || focus.includes("all") ? ` ### Strengths - Code structure appears organized - Follows basic conventions ### Improvements Needed - Consider adding more descriptive variable names - Add JSDoc/docstrings for public functions - Break down functions longer than 20 lines ` : "Not analyzed"} ## Security Analysis ${focus.includes("security") || focus.includes("all") ? ` ### Checklist - [ ] Input validation present? - [ ] No hardcoded secrets? - [ ] Proper error handling? - [ ] SQL injection prevention? - [ ] XSS prevention? ### Recommendations - Validate all user inputs - Use parameterized queries - Sanitize output appropriately ` : "Not analyzed"} ## Performance Analysis ${focus.includes("performance") || focus.includes("all") ? ` ### Considerations - Check for O(n²) or worse algorithms - Look for unnecessary iterations - Consider caching opportunities - Avoid blocking operations ### Recommendations - Profile before optimizing - Use appropriate data structures - Consider lazy evaluation ` : "Not analyzed"} ## Maintainability Analysis ${focus.includes("maintainability") || focus.includes("all") ? ` ### Metrics - Single Responsibility: Review needed - DRY Principle: Check for duplication - Coupling: Assess dependencies ### Recommendations - Add comprehensive tests - Document complex logic - Use consistent naming ` : "Not analyzed"} ## Action Items 1. Address security concerns first 2. Add missing documentation 3. Implement suggested improvements 4. Add/improve tests `; return { content: [{ type: "text", text: reflection }] }; }
- src/tools/cognitive.ts:80-88 (schema)The schema definition for the 'reflect_on_code' tool, including name, description, and Zod input schema for validation.export const reflectOnCodeSchema = { name: "reflect_on_code", description: "Provides a structured critique of code, analyzing quality, security, performance, and maintainability. Returns specific improvement suggestions.", inputSchema: z.object({ code: z.string().describe("The code to analyze"), language: z.string().describe("Programming language of the code"), focus: z.array(z.enum(["quality", "security", "performance", "maintainability", "all"])).optional().describe("Areas to focus analysis on") }) };
- src/index.ts:80-80 (registration)Registration of the 'reflect_on_code' tool in the main tool registry Map used by the stdio server.["reflect_on_code", { schema: reflectOnCodeSchema, handler: reflectOnCodeHandler }],
- src/server.ts:90-90 (registration)Registration of the 'reflect_on_code' tool in the HTTP server's tool registry Map.["reflect_on_code", { schema: reflectOnCodeSchema, handler: reflectOnCodeHandler }],