search_repository
Search across GitHub repositories for patterns, text, functions, or classes using advanced filtering options to find specific code elements.
Instructions
Search for patterns, text, functions, or classes across the entire repository with advanced filtering options.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | GitHub repository URL | |
| query | Yes | Search query (supports regex patterns) | |
| search_type | No | Type of search to perform | text |
| options | No |
Implementation Reference
- src/index.ts:409-426 (handler)The main handler function for the 'search_repository' MCP tool. Extracts parameters from args, fetches repository info, performs the search via GitHubService, creates standardized response, and formats for MCP protocol.async function handleSearchRepository(args: any) { try { const { url, query, search_type = 'text', options = {} } = args; // Get repository content and perform search const repositoryInfo = await githubService.getRepositoryInfo(url); const searchResults = await githubService.searchInRepository(url, query, { type: search_type, ...options, }); const response = createResponse(searchResults); return formatToolResponse(response); } catch (error) { const response = createResponse(null, error, { tool: 'search_repository', url: args.url, query: args.query }); return formatToolResponse(response); } }
- src/tools/consolidated.ts:92-146 (schema)Tool schema definition including name, description, and input validation schema for 'search_repository'.{ name: 'search_repository', description: 'Search for patterns, text, functions, or classes across the entire repository with advanced filtering options.', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'GitHub repository URL', }, query: { type: 'string', description: 'Search query (supports regex patterns)', }, search_type: { type: 'string', enum: ['text', 'regex', 'function', 'class', 'variable', 'import'], description: 'Type of search to perform', default: 'text', }, options: { type: 'object', properties: { case_sensitive: { type: 'boolean', description: 'Case sensitive search', default: false, }, file_extensions: { type: 'array', items: { type: 'string' }, description: 'File extensions to search in', }, exclude_paths: { type: 'array', items: { type: 'string' }, description: 'Paths to exclude from search', default: ['node_modules', 'dist', 'build', '.git'], }, max_results: { type: 'number', description: 'Maximum number of results', default: 100, }, include_context: { type: 'boolean', description: 'Include surrounding code context', default: true, }, }, }, }, required: ['url', 'query'], }, },
- src/index.ts:260-262 (registration)Tool registration in the MCP CallToolRequestSchema request handler switch statement, dispatching to the specific handler function.case 'search_repository': result = await handleSearchRepository(args); break;
- src/services/github.ts:942-971 (helper)Core search implementation in GitHubService class. Retrieves key files from repository, performs case-insensitive line-by-line substring search for the query, collects matches with optional context, and returns structured results.async searchInRepository(url: string, query: string, options: any = {}): Promise<any> { const keyFiles = await this.getKeyFiles(url); const searchResults = []; for (const [filePath, content] of Object.entries(keyFiles)) { const lines = content.split('\n'); let lineNumber = 0; for (const line of lines) { lineNumber++; if (line.toLowerCase().includes(query.toLowerCase())) { searchResults.push({ file: filePath, line: lineNumber, content: line.trim(), context: options.include_context ? lines.slice(Math.max(0, lineNumber - 3), lineNumber + 3) : [], type: 'exact', }); } } } return { query, results: searchResults, totalMatches: searchResults.length, filesSearched: Object.keys(keyFiles).length, searchTime: Date.now(), }; }