Xgrep
Ultra-fast indexed code search with MCP server for AI agents. Trigram index trades precision for speed and simplicity.
Ask AI about Xgrep
Powered by Claude Β· Grounded in docs
I know everything about Xgrep. Ask me about installation, configuration, usage, or troubleshooting.
0/500
Reviews
Documentation
xgrep
Ultra-fast indexed code search engine with MCP server for AI coding tools.
Pre-builds a trigram inverted index, then searches in milliseconds. Designed for repeated searches on large codebases β by humans and AI agents alike.
Features
- Indexed search β trigram inverted index makes repeated searches 2-46x faster than ripgrep
- File discovery β
--findmode locates files 4-34x faster than fd/find - MCP server β built-in Model Context Protocol server for AI coding tools (Claude Code, Cursor, etc.)
- LLM-optimized output β
--format llmproduces Markdown with language tags, context lines, and token-aware truncation - Git-aware β search only changed files (
--changed), recent commits (--since 1h), respects.gitignore - Zero config β
cargo install xgrep-search, thenxg "pattern". Index builds automatically on first search - Hybrid search β serves results from index instantly while rebuilding in the background
Why xgrep?
| ripgrep | zoekt | xgrep | |
|---|---|---|---|
| Setup | None | Server required | None (cargo install) |
| First search | Instant | After server start | Auto-builds index |
| Repeated search (Linux kernel) | 1,687ms | 170ms (server) | 37ms |
| File discovery (next.js, 26K files) | N/A | N/A | 9ms (fd: 191ms) |
| Index size | N/A | 155% of source | 8% of source |
| AI agent integration | None | None | MCP server built-in |
| Memory (search) | 11MB | 288MB | 208MB |
xgrep is not a ripgrep replacement. Use ripgrep for one-off searches. Use xgrep when you search the same codebase repeatedly β the index pays for itself after ~2 searches.
Quick Start
cargo install xgrep-search # Installs the `xg` command
xg "pattern" # Search (auto-builds index on first run)
Requires Rust 1.85+. Works on macOS, Linux, and Windows.
Build from source
git clone https://github.com/momokun7/xgrep.git
cd xgrep/rust
cargo build --release
cp target/release/xg ~/.local/bin/
Usage
xg "pattern" # Fixed string search
xg "pattern" /path/to/repo # Search a specific directory
xg -e "handle_\w+" # Regex search
xg "pattern" -t rs # Filter by file type
xg "pattern" -C 3 # Context lines
xg "pattern" --format llm # Markdown output for LLMs
xg "pattern" --changed # Only git changed files
xg "pattern" --exclude vendor # Exclude paths containing "vendor"
xg "pattern" --absolute-paths # Show absolute paths
xg "pattern" --no-hints # Suppress regex pattern hints
xg --find "*.rs" # Find files by glob pattern
xg --list-types # Show supported file types
xg status # Show index status
xg init # Explicitly rebuild index
Environment Variables
| Variable | Description | Default |
|---|---|---|
XGREP_LLM_CONTEXT | Default context lines for --format llm | 3 |
XGREP_ABSOLUTE_PATHS | Set to 1 to always use absolute paths | unset |
XGREP_NO_HINTS | Set to 1 to suppress regex pattern hints | unset |
Run xg --help for all options.
MCP Server
xgrep runs as an MCP server, giving AI coding tools fast indexed search.
xg serve # Start MCP server
xg serve --root /path/to/repo # Specific directory
Claude Code
{
"mcpServers": {
"xgrep": {
"command": "xg",
"args": ["serve"]
}
}
}
Available tools: search, find_definitions, read_file, index_status, build_index
Performance
Benchmarked with hyperfine on Apple M4, 32GB RAM, macOS. All numbers are warm cache, after index build.
Search: Linux kernel (92,947 files, 2.0GB)
| Query | xg | ripgrep | vs ripgrep |
|---|---|---|---|
struct file_operations | 37ms | 1,687ms | 46x faster |
printk | 52ms | 1,756ms | 34x faster |
EXPORT_SYMBOL | 66ms | 1,773ms | 27x faster |
File discovery: next.js (26,424 files)
| Query | xg --find | fd | vs fd |
|---|---|---|---|
*.ts (4,643 files) | 8.9ms | 190.9ms | 21x faster |
config (substring) | 5.5ms | 189.0ms | 34x faster |
Index cost
| Metric | xgrep | zoekt |
|---|---|---|
| Build time (Linux kernel) | 6s | 46s |
| Index size | 175MB (8% of source) | 3.0GB (155%) |
| Breakeven | ~2 searches | β |
First run includes a one-time index build. See docs/benchmarks.md for full results including medium/small repos.
Limitations
- Short queries (< 3 chars) bypass the index β no speed advantage over ripgrep
- Index staleness β background rebuild runs every ~30s. Use
--freshfor up-to-date results - find_definitions uses regex heuristics, not AST analysis β false positives expected
When to use ripgrep instead: one-off searches, very small codebases (< 100 files), or queries shorter than 3 characters.
How It Works
- Index Build: Walks the codebase, extracts 3-byte trigrams from each file, builds an inverted index with delta+varint compression
- Search: Extracts trigrams from query, intersects posting lists to find candidate files, verifies matches
- Hybrid Mode: Combines index results with direct scanning of changed files when index is stale
- MCP Server: Exposes search via JSON-RPC over stdio, with token-aware truncation
Contributing
See CONTRIBUTING.md for development setup and guidelines.
License
MIT
