find_in_file
Search for text patterns in files using regular expressions, displaying matches with surrounding context lines for better analysis.
Instructions
Find occurrences of a pattern in a file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path to the file to search in | |
| pattern | Yes | Regular expression pattern to search for | |
| contextLines | No | Number of context lines to include before and after matches (default: 2) |
Implementation Reference
- src/index.ts:215-240 (registration)Registers the 'find_in_file' MCP tool with its description, input schema (requiring path and pattern, optional contextLines), and read-only 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 } });
- src/core/mcp-server.ts:397-423 (handler)Generic handler for all MCP tool calls ('tools/call'). Looks up the tool by name and currently returns a placeholder response indicating execution (intended location for specific tool logic)./** * Handles the tools/call request */ private async handleToolsCall(params: { name: string, arguments?: any }): Promise<CallToolResult> { const { name, arguments: args } = params; if (!name) { throw new Error('Tool name is required'); } const tool = this.tools.get(name); if (!tool) { throw new Error(`Tool not found: ${name}`); } // In a real implementation, we would execute the tool here // For now, we'll just return a placeholder return { content: [ { type: 'text', text: `Executed tool ${name} with arguments: ${JSON.stringify(args)}` } as TextContent ] };
- Core utility function that implements file search logic: reads file, splits into lines, finds RegExp matches, includes context lines before/after, returns structured SearchResult[] with line/column info. Matches 'find_in_file' tool signature exactly.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}`); } }