Skip to main content

Ultra-fast indexed code search engine with MCP server for AI coding tools.

Project description

xgrep

CI Crates.io npm PyPI License: MIT

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--find mode locates files 2-15x faster than fd
  • MCP server — built-in Model Context Protocol server for AI coding tools (Claude Code, Cursor, etc.)
  • LLM-optimized output--format llm produces 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 — install via cargo/npm/pip, then xg "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/npm/pip)
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.

Installation

Choose your package manager:

# Rust (recommended — native binary, no runtime)
cargo install xgrep-search    # Requires Rust 1.85+

# npm (Node.js 18+)
npm install -g xgrep

# pip (Python 3.8+)
pip install xgrep-search

Prebuilt binaries are also available on the Releases page.

Quick Start

xg "pattern"                  # Search (auto-builds index on first run)
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"                  # Smart-case search (all-lowercase = case-insensitive)
xg "Pattern"                  # Mixed/upper case in pattern = case-sensitive
xg "pattern" -i               # Force case-insensitive
xg "pattern" -s               # Force case-sensitive (disable smart-case)
xg "pattern" /path/to/repo    # Search a specific directory
xg -e "handle_\w+"            # Regex search
xg "pattern" -w               # Match whole words only
xg "pattern" -t rs            # Filter by file type
xg "pattern" -C 3             # Context lines (symmetric)
xg "pattern" -A 2 -B 1        # 2 lines after, 1 line before
xg "pattern" -g "*.rs"        # Include only paths matching glob (repeatable)
xg "pattern" -g "!*_test.rs"  # Exclude paths matching glob (! prefix)
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

Search is smart-case by default: an all-lowercase pattern matches case-insensitively, while any uppercase letter makes the search case-sensitive. Use -i or -s to override (priority: -i > -s > smart-case).

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

See docs/agents.md for agent-oriented usage patterns.

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 (27,332 files)

Query xg --find fd vs fd
*.ts (4,838 files) 20.8ms 187.3ms 9x faster
config (substring) 12.7ms 188.1ms 15x 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
  • Tiny files (< 3 bytes) hold no trigrams and are invisible to indexed content search — a deliberate trade-off of the trigram index
  • Index staleness — background rebuild runs every ~30s. Use --fresh for 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

  1. Index Build: Walks the codebase, extracts 3-byte trigrams from each file, builds an inverted index with delta+varint compression
  2. Search: Extracts trigrams from query, intersects posting lists to find candidate files, verifies matches
  3. Hybrid Mode: Combines index results with direct scanning of changed files when index is stale
  4. MCP Server: Exposes search via JSON-RPC over stdio, with token-aware truncation

Contributing

See CONTRIBUTING.md for development setup and guidelines.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

xgrep_search-0.6.0-cp311-cp311-win_amd64.whl (1.0 MB view details)

Uploaded CPython 3.11Windows x86-64

xgrep_search-0.6.0-cp311-cp311-manylinux_2_39_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.39+ x86-64

xgrep_search-0.6.0-cp311-cp311-macosx_11_0_arm64.whl (996.3 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

xgrep_search-0.6.0-cp311-cp311-macosx_10_12_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

Details for the file xgrep_search-0.6.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for xgrep_search-0.6.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e496565000b6e543bb4e3d0bbde3b5549462247fd889769019b122d184a5dba3
MD5 7d3e8baa85f0b97c862ce397cd6103b9
BLAKE2b-256 c0ec7c4850e011e833c7246668cbed7a1431571a3b88ed583f69faf535a9e616

See more details on using hashes here.

File details

Details for the file xgrep_search-0.6.0-cp311-cp311-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for xgrep_search-0.6.0-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 ec4b038fc7dc9f8b97e70f41ee22c682e2848e75617eaf14d287ed79d49b907e
MD5 70d6962f9b4ef69e32d8bd78784408b1
BLAKE2b-256 8eea134766b853089a1191583106b50498285a876537cb6e257530c27e6cf610

See more details on using hashes here.

File details

Details for the file xgrep_search-0.6.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for xgrep_search-0.6.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 373a0a6dcea4df7a9bc4234bd5d25518dce9fa7e50dde623dbdbccf5eb737fa4
MD5 41169836ff7168c0a9e92fb2f41a869f
BLAKE2b-256 0acf9c9d5c22ba44c7183f8de6d5392ac6a1602b61830f1b75bf281f9a15f213

See more details on using hashes here.

File details

Details for the file xgrep_search-0.6.0-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for xgrep_search-0.6.0-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0c8a13bf72bfc985e285ee5f7c17524afa22265e603b0bb4ee23f2a3b394daf1
MD5 7257ec5b033cef3c1c697099ec4fa28b
BLAKE2b-256 6ef1cfb75284e5700d27e2ced9c3e26d00588a2d9592c19689f0f889a7a8c589

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page