find_in_file
Search for specific patterns in a file using regular expressions, with an option to include surrounding lines for better context. Ideal for precise text analysis and navigation.
Instructions
Find occurrences of a pattern in a file
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| contextLines | No | Number of context lines to include before and after matches (default: 2) | |
| path | Yes | Path to the file to search in | |
| pattern | Yes | Regular expression pattern to search for |
Input Schema (JSON Schema)
{
"properties": {
"contextLines": {
"description": "Number of context lines to include before and after matches (default: 2)",
"type": "number"
},
"path": {
"description": "Path to the file to search in",
"type": "string"
},
"pattern": {
"description": "Regular expression pattern to search for",
"type": "string"
}
},
"required": [
"path",
"pattern"
],
"type": "object"
}
Implementation Reference
- Core handler function that reads a file, searches for RegExp matches line-by-line, and returns SearchResult[] with match position and context lines.public async findInFile(filePath: string, pattern: RegExp, contextLines: number = 2): Promise<SearchResult[]> { try { const content = await this.readFile(filePath); const lines = content.split('\n'); const results: SearchResult[] = []; for (let i = 0; i < lines.length; i++) { const line = lines[i]; const match = pattern.exec(line); if (match) { const linesBefore = lines.slice(Math.max(0, i - contextLines), i); const linesAfter = lines.slice(i + 1, Math.min(lines.length, i + contextLines + 1)); results.push({ line: i + 1, column: match.index + 1, text: line, linesBefore, linesAfter }); } } return results; } catch (error: any) { throw new Error(`Failed to search in file ${filePath}: ${error.message}`); } }
- src/index.ts:215-240 (registration)Registers the MCP tool 'find_in_file' with schema, description, input parameters, and annotations.mcpServer.registerTool({ name: 'find_in_file', description: 'Find occurrences of a pattern in a file', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file to search in' }, pattern: { type: 'string', description: 'Regular expression pattern to search for' }, contextLines: { type: 'number', description: 'Number of context lines to include before and after matches (default: 2)' } }, required: ['path', 'pattern'] }, annotations: { readOnlyHint: true, openWorldHint: false } });
- Output schema/interface for search results returned by findInFile.export interface SearchResult { line: number; column: number; text: string; linesBefore: string[]; linesAfter: string[]; }
- src/http/http-server.ts:234-254 (registration)Registers HTTP REST endpoint /api/search that invokes the 'find_in_file' MCP tool.this.app.post('/api/search', async (req, res) => { try { const searchRequest = parseMessage({ jsonrpc: '2.0', method: 'tools/call', params: { name: 'find_in_file', arguments: { path: req.body.path, pattern: req.body.pattern, contextLines: req.body.contextLines || 2 } }, id: 'rest-' + Date.now() }); const response = await this.mcpServer.handleMessage(searchRequest); res.json(response); } catch (error: any) { res.status(500).json({ error: error.message }); } });
- Helper usage of findInFile in smart_refactor hybrid operation to locate symbol occurrences across files.operation.affectedFiles.map(file => this.fileSystemManager.findInFile( file, new RegExp(operation.params.oldName, 'g') ) ) );