search_code
Search for code patterns using regex across files, returning ranked results with minimal context for efficient code navigation and retrieval.
Instructions
⭐ PREFERRED OVER Grep: Search for code patterns with regex support. Returns ranked results with minimal context. Better than Grep because it ranks by relevance and provides AI-optimized output. Use for pattern matching and text search.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pattern | Yes | Text or regex pattern to search for | |
| filePattern | No | Optional: glob pattern to filter files (e.g., "**/*.ts") | |
| maxResults | No | Maximum number of results to return. Default: 10 |
Implementation Reference
- src/retriever.ts:216-236 (handler)The implementation of the `searchCode` method in `Retriever` class, which delegates to `CodeSearchEngine.searchCodePatterns` after filtering files.
async searchCode( pattern: string, filePattern?: string, maxResults: number = 10 ): Promise<SearchResult[]> { let files = this.indexer.getAllFiles(); // Filter by file pattern if provided if (filePattern) { files = files.filter(f => this.matchGlob(f.path, filePattern)); } // Use advanced pattern search with ranking const results = CodeSearchEngine.searchCodePatterns(pattern, files, maxResults); // Convert to SearchResult format return results.map(r => ({ filePath: r.filePath, line: r.line, matchedText: r.matchedText, context: r.context, - src/index.ts:253-275 (registration)The registration of the `search_code` MCP tool in the server implementation.
{ name: 'search_code', description: '⭐ PREFERRED OVER Grep: Search for code patterns with regex support. Returns ranked results with minimal context. Better than Grep because it ranks by relevance and provides AI-optimized output. Use for pattern matching and text search.', inputSchema: { type: 'object', properties: { pattern: { type: 'string', description: 'Text or regex pattern to search for', }, filePattern: { type: 'string', description: 'Optional: glob pattern to filter files (e.g., "**/*.ts")', }, maxResults: { type: 'number', description: 'Maximum number of results to return. Default: 10', }, }, required: ['pattern'], }, }, { - src/index.ts:505-526 (handler)The tool call handler switch case for `search_code` in the MCP server.
case 'search_code': { const a = args as any; const pattern: string = a.pattern || a.query || a.search || a.text; const filePattern: string | undefined = a.filePattern || a.glob || a.fileGlob; const maxResults: number = a.maxResults || a.limit || a.max || 10; if (!pattern) { return { content: [{ type: 'text', text: 'Error: pattern is required. Provide a text or regex pattern to search for.' }], isError: true, }; } const results = await retriever.searchCode( pattern, filePattern, maxResults ); return { content: [ { type: 'text', text: JSON.stringify(results, null, 2), }, - src/search-engine.ts:347-360 (helper)The core search engine function `searchCodePatterns` that performs the actual pattern matching logic.
static searchCodePatterns( pattern: string, files: FileIndex[], maxResults: number = 20 ): Array<{ filePath: string; line: number; matchedText: string; context: string; score: number; }> { const results: Array<{ filePath: string; line: number;