search_github
Search GitHub repositories and code by query, filtering by programming language, and set result limits to find relevant coding resources efficiently.
Instructions
Search GitHub for repositories and code
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| language | No | Filter by programming language | |
| limit | No | Maximum number of results per category (default: 5) | |
| query | Yes | Search query |
Implementation Reference
- src/index.ts:138-205 (handler)Core implementation of the search_github tool. Searches GitHub for repositories and code using the GitHub API, supports language filtering, caching with node-cache, rate limit handling with token fallback, and formats results into repositories and code sections.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, defining parameters query (required), optional language filter, and limit with bounds.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 MCP server's tool list, 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 (handler)Dispatch handler in the CallToolRequestSchema that extracts arguments and invokes the searchGitHub function for the search_github tool.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 } ] }; }