index_codebase
Create a searchable index from code and documentation to enable semantic search and cross-references across your codebase.
Instructions
YOU MUST CALL THIS TOOL FIRST before using search_code or search_docs. Use this tool to build the searchable index that powers all other code intelligence features.
TRIGGER: Call this tool immediately when:
Starting a new session with this codebase
search_code or search_docs returns empty or unexpected results
You haven't indexed recently or files have been modified
User asks about code structure, definitions, or documentation
This tool performs TWO critical operations:
CODE INDEXING: Uses tree-sitter for language-agnostic AST extraction (Python, JavaScript/TypeScript, Java, Kotlin, Go, Rust, C/C++, Ruby, and more). Extracts functions, classes, methods, variables, and cross-references.
DOCUMENTATION INDEXING: Parses markdown files, READMEs, and extracts docstrings from indexed code. Generates embeddings for semantic search.
IMPORTANT ADVANTAGES over built-in file search:
Creates persistent structural knowledge (AST-based, not just text)
Enables semantic search via vector embeddings
Builds cross-reference graphs for "find all usages" queries
Incremental indexing: unchanged files are automatically skipped
PARALLEL PROCESSING: Uses thread pool for faster indexing
Do NOT use this tool for:
Non-code files (images, binaries, data files)
Single-file lookups (use search_code after indexing)
Git history queries (use search_history instead)
Args: directory: The root directory to index. Must be a valid path. cpu: If True, force CPU-only mode for embedding generation. Use this when GPU memory is unavailable or constrained (CUDA OOM). Default is False (auto-detect best device: CUDA > MPS > CPU). Set CODE_MEMORY_DEVICE env var to override ('cuda', 'mps', 'cpu', or 'auto').
Returns: Summary with files_indexed, total_symbols, total_chunks, and details.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cpu | No | ||
| directory | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |