Analyze Code
analyze-codeAnalyze code for architecture, performance, security, or quality issues to identify problems and improve codebase health.
Instructions
Analyze code for architecture, performance, security, or quality issues
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| task | Yes | What to analyze (e.g., 'analyze performance of user authentication', 'review database queries') | |
| files | No | File paths to analyze (optional) | |
| focus | No | Analysis focus area | all |
| provider | No | AI provider to use | gemini |
Implementation Reference
- src/handlers/ai-tools.ts:299-348 (handler)Core handler function that executes the 'analyze-code' tool: selects AI provider, constructs focus-specific system prompt, generates analytical response.
async handleAnalyzeCode(params: z.infer<typeof AnalyzeCodeSchema>) { // Use provided provider or get the preferred one (Azure if configured) const providerName = params.provider || (await this.providerManager.getPreferredProvider(['openai', 'gemini', 'azure', 'grok'])); const provider = await this.providerManager.getProvider(providerName); const focusPrompts = { architecture: "Focus on architectural patterns, design decisions, modularity, and code organization", performance: "Focus on performance implications, optimization opportunities, and efficiency concerns", security: "Focus on security vulnerabilities, authentication, authorization, and data protection", quality: "Focus on code quality, maintainability, readability, and best practices", all: "Provide comprehensive analysis covering architecture, performance, security, and quality aspects" }; const systemPrompt = `You are an expert code analyst. Analyze the provided code or task systematically. ${focusPrompts[params.focus]} Provide insights on: - Key findings and patterns identified - Areas of concern or improvement opportunities - Recommendations for enhancement - Technical assessment and conclusions Be specific and actionable in your analysis.`; const prompt = `Analyze the following: ${params.task}${params.files ? `\n\nFiles to consider: ${params.files.join(", ")}` : ""}`; const response = await provider.generateText({ prompt, systemPrompt, temperature: 0.3, // Lower temperature for analytical tasks reasoningEffort: (providerName === "openai" || providerName === "azure" || providerName === "grok") ? "high" : undefined, useSearchGrounding: providerName === "gemini", }); return { content: [ { type: "text", text: response.text, }, ], metadata: { provider: providerName, model: response.model, focus: params.focus, usage: response.usage, ...response.metadata, }, }; } - src/server.ts:34-39 (schema)Zod input schema defining parameters for the analyze-code tool: task, optional files, focus area, and provider.
const AnalyzeCodeSchema = z.object({ task: z.string().describe("What to analyze (e.g., 'analyze performance of user authentication', 'review database queries')"), files: z.array(z.string()).optional().describe("File paths to analyze (optional)"), focus: z.enum(["architecture", "performance", "security", "quality", "all"]).default("all").describe("Analysis focus area"), provider: z.enum(["openai", "gemini", "azure", "grok"]).optional().default("gemini").describe("AI provider to use"), }); - src/server.ts:284-292 (registration)Registers the 'analyze-code' tool with MCP server, specifying title, description, input schema, and delegates execution to AIToolHandlers.handleAnalyzeCode
// Register analyze-code tool server.registerTool("analyze-code", { title: "Analyze Code", description: "Analyze code for architecture, performance, security, or quality issues", inputSchema: AnalyzeCodeSchema.shape, }, async (args) => { const aiHandlers = await getHandlers(); return await aiHandlers.handleAnalyzeCode(args); }); - src/server.ts:586-603 (registration)Registers the prompt version of 'analyze-code' for natural language invocation.
server.registerPrompt("analyze-code", { title: "Analyze Code", description: "Analyze code for architecture, performance, security, or quality issues", argsSchema: { task: z.string().optional(), files: z.string().optional(), focus: z.string().optional(), provider: z.string().optional(), }, }, (args) => ({ messages: [{ role: "user", content: { type: "text", text: `Analyze this code: ${args.task || 'Please specify what code to analyze (e.g., performance, security, architecture).'}${args.files ? `\n\nFocus on these files: ${args.files}` : ''}${args.focus ? ` (analysis focus: ${args.focus})` : ''}${args.provider ? ` (using ${args.provider} provider)` : ''}` } }] })); - src/server.ts:195-235 (helper)Lazy-initializes and returns the AIToolHandlers instance which contains the handleAnalyzeCode method, setting up providers and config.
async function getHandlers() { if (!handlers) { const { ConfigManager } = require("./config/manager"); const { ProviderManager } = require("./providers/manager"); const { AIToolHandlers } = require("./handlers/ai-tools"); const configManager = new ConfigManager(); // Load config and set environment variables const config = await configManager.getConfig(); if (config.openai?.apiKey) { process.env.OPENAI_API_KEY = config.openai.apiKey; } if (config.openai?.baseURL) { process.env.OPENAI_BASE_URL = config.openai.baseURL; } if (config.google?.apiKey) { process.env.GOOGLE_API_KEY = config.google.apiKey; } if (config.google?.baseURL) { process.env.GOOGLE_BASE_URL = config.google.baseURL; } if (config.azure?.apiKey) { process.env.AZURE_API_KEY = config.azure.apiKey; } if (config.azure?.baseURL) { process.env.AZURE_BASE_URL = config.azure.baseURL; } if (config.xai?.apiKey) { process.env.XAI_API_KEY = config.xai.apiKey; } if (config.xai?.baseURL) { process.env.XAI_BASE_URL = config.xai.baseURL; } providerManager = new ProviderManager(configManager); handlers = new AIToolHandlers(providerManager); } return handlers; }