analyze-repository
Analyze repository code for dependencies, complexity, and specific file patterns. Customize depth and focus areas to optimize insights into code structure and behavior.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| depth | No | Analysis depth - higher values analyze more deeply but take longer (1-5) | |
| includeComplexity | No | Include complexity metrics in the results | |
| includeDependencies | No | Include dependency analysis in the results | |
| repositoryUrl | Yes | URL of the repository to analyze (e.g., 'https://github.com/username/repo') | |
| specificFiles | No | Specific files to analyze, can include glob patterns (e.g., ['src/*.ts', 'lib/utils.js']) |
Implementation Reference
- src/features/basic-analysis/index.ts:182-235 (registration)Primary registration of the 'analyze-repository' MCP tool. Includes input schema validation using Zod, the main handler function that logs args, calls the analyzeRepository helper, constructs response data, and returns MCP-formatted content or error.server.tool( "analyze-repository", { repositoryUrl: z.string().describe("URL of the repository to analyze (e.g., 'https://github.com/username/repo')"), depth: z.number().default(2).describe("Analysis depth - higher values analyze more deeply but take longer (1-5)"), includeDependencies: z.boolean().default(true).describe("Include dependency analysis in the results"), includeComplexity: z.boolean().default(true).describe("Include complexity metrics in the results"), specificFiles: z.array(z.string()).optional().describe("Specific files to analyze, can include glob patterns (e.g., ['src/*.ts', 'lib/utils.js'])") }, async (args) => { try { console.log("analyze-repository called with:", args); // Perform the actual analysis const startTime = Date.now(); const analysis = await analyzeRepository(args.repositoryUrl); // Create a standardized response with the results const responseData = { repository: args.repositoryUrl, result: { ...analysis.data, includedDependencies: args.includeDependencies, includedComplexity: args.includeComplexity, depth: args.depth, specificFiles: args.specificFiles || "all" } }; // Return MCP-formatted response return { content: [{ type: "text", text: JSON.stringify(responseData, null, 2) }] }; } catch (error) { // Create a standardized error response const errorResponse = createErrorResponse( error instanceof Error ? error.message : String(error), "analyze-repository" ); // Return MCP-formatted error response return { content: [{ type: "text", text: JSON.stringify(errorResponse, null, 2) }], isError: true }; } } );
- Core helper function 'analyzeRepository' called by the tool handler. Handles repository cloning, file listing, dependency extraction using extractDependencies, caching results with UUID, and single-file analysis fallback. Wrapped with executeWithTiming for performance tracking.export async function analyzeRepository( repositoryUrl?: string, fileContent?: string, language?: string ): Promise<any> { return executeWithTiming('analyze-repository', async () => { if (repositoryUrl) { const repoPath = await getRepository(repositoryUrl); const files = listFiles(repoPath); const analysisId = uuidv4(); // Perform dependency analysis const dependencies = extractDependencies(repoPath, files, language); // Store results in cache const results = { repositoryUrl, analysisId, dependencies, fileCount: files.length, timestamp: new Date().toISOString() }; analysisCache.set(analysisId, results); return results; } else if (fileContent) { // Analyze single file content const dependencies = analyzeCode(fileContent, language); return { analysisId: uuidv4(), dependencies, timestamp: new Date().toISOString() }; } else { throw new Error("Either repositoryUrl or fileContent must be provided"); } }); }
- Supporting utility 'extractDependencies' used by analyzeRepository to scan all repo files, read code, extract imports per file using extractImports, and build a dependency map.function extractDependencies(repoPath: string, files: string[], language?: string): Record<string, string[]> { const dependencies: Record<string, string[]> = {}; for (const file of files) { const fullPath = path.join(repoPath, file); try { const code = fs.readFileSync(fullPath, 'utf8'); const fileLanguage = language || path.extname(file).slice(1); dependencies[file] = extractImports(code, fileLanguage); } catch (error) { console.warn(`Error reading file ${file}: ${(error as Error).message}`); } } return dependencies; }
- Zod schema for input parameters of the analyze-repository tool, defining required repositoryUrl and optional flags for depth, dependencies, complexity, and specific files.{ repositoryUrl: z.string().describe("URL of the repository to analyze (e.g., 'https://github.com/username/repo')"), depth: z.number().default(2).describe("Analysis depth - higher values analyze more deeply but take longer (1-5)"), includeDependencies: z.boolean().default(true).describe("Include dependency analysis in the results"), includeComplexity: z.boolean().default(true).describe("Include complexity metrics in the results"), specificFiles: z.array(z.string()).optional().describe("Specific files to analyze, can include glob patterns (e.g., ['src/*.ts', 'lib/utils.js'])") },
- src/features/basic-analysis/analyzer.ts:258-287 (registration)Alternative/backup registration of analyze-repository tool in analyzer.ts with simplified mock handler, noted to match CLI exactly.server.tool( "analyze-repository", // This name must match exactly what the CLI calls { repositoryUrl: z.string().describe("URL of the repository to analyze"), depth: z.number().default(2).describe("Analysis depth"), includeDependencies: z.boolean().default(true).describe("Include dependency analysis"), includeComplexity: z.boolean().default(true).describe("Include complexity analysis"), specificFiles: z.array(z.string()).optional().describe("Specific files to analyze") }, async (args) => { // Basic implementation console.log("Analyzing repository:", args.repositoryUrl); // Handle specific files if provided if (args.specificFiles && args.specificFiles.length > 0) { console.log("Analyzing specific files:", args.specificFiles); } return { content: [{ type: "text", text: JSON.stringify({ repository: args.repositoryUrl, analysisDepth: args.depth, result: "Sample repository analysis results" }, null, 2) }] }; } );