analyze_repository
Analyze GitHub repositories to identify code examples demonstrating specific code smells for structured learning sessions.
Instructions
Analyze a GitHub repository to find real code examples for Learning Hours
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repositoryUrl | Yes | GitHub repository URL to analyze | |
| codeSmell | Yes | Type of code smell to find (e.g., 'Feature Envy', 'Long Method') |
Implementation Reference
- src/index.ts:369-430 (handler)The primary handler for the 'analyze_repository' tool. Validates input using Zod schema, delegates analysis to RepositoryAnalyzer, handles errors, and returns formatted results.private async analyzeRepository(args: any) { const input = AnalyzeRepositoryInputSchema.parse(args); try { const analysisResult = await this.repositoryAnalyzer.analyzeRepository(input.repositoryUrl, input.codeSmell); return { content: [ { type: "text", text: `✅ Repository analysis completed for: ${input.codeSmell}`, }, { type: "text", text: `Found ${analysisResult.examples.length} examples in ${input.repositoryUrl}`, }, { type: "text", text: JSON.stringify(analysisResult, null, 2), }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); if (errorMessage.includes('GitHub integration not configured')) { return { content: [ { type: "text", text: `❌ GitHub integration not configured`, }, { type: "text", text: `To use repository analysis, please set GITHUB_TOKEN in your environment.`, }, { type: "text", text: `Visit https://github.com/settings/tokens to create a personal access token with 'repo' scope.`, }, ], }; } if (errorMessage.includes('No examples')) { return { content: [ { type: "text", text: `⚠️ No examples found`, }, { type: "text", text: errorMessage, }, ], }; } throw new Error(`Failed to analyze repository: ${errorMessage}`); } }
- src/RepositoryAnalyzer.ts:30-80 (helper)Core implementation of repository analysis: parses GitHub URL, generates search queries for code smells, searches files using GitHub client, analyzes content for examples, and returns AnalysisResult.async analyzeRepository(repositoryUrl: string, codeSmell: string): Promise<AnalysisResult> { const { owner, repo } = this.parseGitHubUrl(repositoryUrl); try { const searchQueries = this.getSearchQueriesForCodeSmell(codeSmell); const examples: CodeExample[] = []; for (const query of searchQueries) { try { const searchResult = await this.githubClient.searchRepositoryFiles(owner, repo, query); const items = (searchResult as any)?.content?.[0]?.text ? JSON.parse((searchResult as any).content[0].text).items : []; for (const item of items.slice(0, 3)) { const fileContent = await this.githubClient.getFileContent(owner, repo, item.path); const content = this.extractFileContent(fileContent); const example = this.analyzeFileForCodeSmell( item.path, content, codeSmell ); if (example) { examples.push(example); } } } catch (error) { if (error instanceof Error && error.message.includes('GitHub MCP client not connected')) { throw error; } logger.error(`Search query failed: ${query}`, error); } } if (examples.length === 0) { throw new Error(`No examples of '${codeSmell}' found in repository ${repositoryUrl}. Try searching for a different code smell or repository.`); } return { examples: examples.slice(0, 5), codeSmell, repositoryUrl }; } catch (error) { if (error instanceof Error && error.message.includes('GitHub MCP client not connected')) { throw new Error('GitHub integration not configured. Please ensure GITHUB_TOKEN is set in your environment.'); } throw error; } }
- src/index.ts:30-33 (schema)Zod schema used for input validation in the analyze_repository handler.const AnalyzeRepositoryInputSchema = z.object({ repositoryUrl: z.string().min(1, "Repository URL is required"), codeSmell: z.string().min(1, "Code smell type is required"), });
- src/index.ts:153-170 (registration)Tool registration in the ListTools response, defining name, description, and JSON input schema.{ name: "analyze_repository", description: "Analyze a GitHub repository to find real code examples for Learning Hours", inputSchema: { type: "object", properties: { repositoryUrl: { type: "string", description: "GitHub repository URL to analyze", }, codeSmell: { type: "string", description: "Type of code smell to find (e.g., 'Feature Envy', 'Long Method')", }, }, required: ["repositoryUrl", "codeSmell"], }, },
- src/index.ts:246-247 (registration)Dispatcher in CallToolRequestHandler that routes 'analyze_repository' calls to the handler method.case "analyze_repository": return await this.analyzeRepository(request.params.arguments);