grep
Find lines matching a regex in files or directories. Auto-detects encoding, supports recursive search, context lines, and multiple output modes.
Instructions
Searches file contents for a regex pattern. Encoding-aware: auto-detects file encoding. Can search a single file or recursively search a directory. Output modes: content (default), files_with_matches, count. Compact content groups matches under one file header by default; use output_format=classic for path:line:text. A CRLF ending is a terminator, not content: "^foo$" matches in a CRLF file and returned lines carry no stray CR. A lone CR is not a line break (same as read), so a CR-only file is one line. Context: use before/after/context to include surrounding lines (like grep -B/-A/-C). Large result sets stay usable: max_results supports up to 100000, while max_line_chars and max_output_chars bound the text returned to the agent. has_more and a visible continuation hint report when additional grep output exists. Directory search skips binary files (extension list + NUL-byte sniff); pass a binary file directly as path to search it anyway.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| glob | No | Glob pattern to filter files (e.g. *.go). Only used when path is a directory | |
| path | No | File or directory to search. Defaults to configured workspace, MCP client root, or current directory | |
| after | No | Lines after each match. Overrides context. Default: 0, Max: 1000 | |
| before | No | Lines before each match. Overrides context. Default: 0, Max: 1000 | |
| cursor | No | Opaque continuation cursor returned by a previous grep call. Not supported with context lines | |
| context | No | Lines before and after each match (like grep -C). Default: 0, Max: 1000 | |
| pattern | Yes | Regular expression pattern to search for | |
| file_path | No | Alias for path | |
| recursive | No | Recurse into subdirectories. Default: true | |
| ignore_case | No | Case-insensitive search. Default: false | |
| max_results | No | Maximum matching lines/files per page. Default: 100, Max: 100000 | |
| output_mode | No | Output mode: content (default), files_with_matches, or count | |
| output_format | No | Content layout: compact (group matches by file, default) or classic (path:line:text on every line) | |
| include_hidden | No | Search hidden directories. Explicitly provided hidden roots are always searched. Default: false | |
| max_line_chars | No | Maximum characters per matching/context line. Default: 4000, Max: 32768 | |
| relative_paths | No | Return paths relative to the search root. Default: true for directory searches | |
| include_ignored | No | Search common generated/vendor directories instead of skipping them. Default: false | |
| max_output_chars | No | Maximum total result characters. Default: 32768, Max: 131072 |