Skip to main content
Glama

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
NameRequiredDescriptionDefault
depthNoAnalysis depth - higher values analyze more deeply but take longer (1-5)
includeComplexityNoInclude complexity metrics in the results
includeDependenciesNoInclude dependency analysis in the results
repositoryUrlYesURL of the repository to analyze (e.g., 'https://github.com/username/repo')
specificFilesNoSpecific files to analyze, can include glob patterns (e.g., ['src/*.ts', 'lib/utils.js'])

Implementation Reference

  • 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'])") },
  • 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) }] }; } );

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/0xjcf/MCP_CodeAnalysis'

If you have feedback or need assistance with the MCP directory API, please join our Discord server