search_github
Search GitHub repositories and code using queries, with filters for programming languages and result limits, to find programming resources and examples.
Instructions
Search GitHub for repositories and code
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query | |
| language | No | Filter by programming language | |
| limit | No | Maximum number of results per category (default: 5) |
Implementation Reference
- src/index.ts:138-205 (handler)The core handler function that performs GitHub searches for repositories and code snippets using the GitHub Search API. Includes caching, language filtering, rate limit handling with token fallback, and formatted output.private async searchGitHub(query: string, language?: string, limit: number = 5): Promise<string> { const cacheKey = `github:${query}:${language}:${limit}`; const cached = cache.get<string>(cacheKey); if (cached) return cached; try { // Build search query with language filter if specified const q = language ? `${query} language:${language}` : query; // If GitHub token is invalid, fall back to unauthenticated requests const makeRequest = async (endpoint: string, params: any) => { try { const response = await this.githubInstance.get(endpoint, { params }); return response; } catch (error) { if (axios.isAxiosError(error) && error.response?.status === 401) { // Retry without auth token const response = await this.axiosInstance.get(`https://api.github.com${endpoint}`, { params, headers: { 'Accept': 'application/vnd.github.v3+json', 'User-Agent': 'CodeResearchBot/1.0' } }); return response; } throw error; } }; const [reposResponse, codeResponse] = await Promise.all([ makeRequest('/search/repositories', { q, sort: 'stars', order: 'desc', per_page: limit }), makeRequest('/search/code', { q, sort: 'indexed', order: 'desc', per_page: limit }) ]); let result = '=== Top Repositories ===\n'; result += reposResponse.data.items.map((repo: any, i: number) => `${i + 1}. ${repo.full_name} (⭐ ${repo.stargazers_count})\n` + ` ${repo.description || 'No description'}\n` + ` ${repo.html_url}\n` ).join('\n'); result += '\n=== Relevant Code ===\n'; result += codeResponse.data.items.map((item: any, i: number) => `${i + 1}. ${item.name} (${item.repository.full_name})\n` + ` Path: ${item.path}\n` + ` ${item.html_url}\n` ).join('\n'); cache.set(cacheKey, result); return result; } catch (error) { throw new McpError( ErrorCode.InternalError, `GitHub API error: ${error instanceof Error ? error.message : 'Unknown error'}` ); } }
- src/index.ts:357-376 (schema)Input schema definition for the search_github tool, specifying query (required), optional language filter, and limit.inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query' }, language: { type: 'string', description: 'Filter by programming language' }, limit: { type: 'number', description: 'Maximum number of results per category (default: 5)', minimum: 1, maximum: 10 } }, required: ['query'] }
- src/index.ts:354-377 (registration)Registration of the search_github tool in the ListToolsRequestSchema handler, including name, description, and input schema.{ name: 'search_github', description: 'Search GitHub for repositories and code', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'Search query' }, language: { type: 'string', description: 'Filter by programming language' }, limit: { type: 'number', description: 'Maximum number of results per category (default: 5)', minimum: 1, maximum: 10 } }, required: ['query'] } },
- src/index.ts:463-478 (registration)Registration of the search_github tool handler in the CallToolRequestSchema switch statement, which extracts arguments and calls the searchGitHub function.case 'search_github': { const { query, language, limit } = request.params.arguments as { query: string; language?: string; limit?: number; }; const results = await this.searchGitHub(query, language, limit); return { content: [ { type: 'text', text: results } ] }; }