Skip to main content
Glama

RAG Document Server

by jaimeferj
CODE_INDEX_GUIDE.md10.6 kB
# Code Index System Guide ## Overview The code indexing system provides **fast, direct lookup** of Python code objects (classes, functions, methods) without relying on RAG semantic search. This dramatically improves performance and accuracy when searching for specific code. ### Benefits ✅ **Fast**: O(1) lookup vs O(n) semantic search ✅ **Accurate**: Exact matches, no hallucination ✅ **Complete**: Indexes ALL code, not just documented parts ✅ **Multi-repo**: Support multiple libraries simultaneously ✅ **Searchable**: Browse and explore codebase structure ## Architecture ### Components 1. **`utils/code_indexer.py`** - AST-based indexer that walks Python files 2. **`utils/code_index_store.py`** - SQLite storage with fast lookup 3. **`build_code_index.py`** - CLI tool to index repositories 4. **RAG System Integration** - Seamless integration with existing RAG queries 5. **MCP Tools** - New tools for Claude to search code directly ### How It Works ``` Ingestion Time: 1. Walk Python files with AST parser 2. Extract all definitions (classes, functions, methods) 3. Build index: name → {file_path, line_number, metadata} 4. Store in SQLite database Query Time: 1. User asks about code object (e.g., "show me AutomationCondition") 2. Direct lookup in index (instant) 3. Retrieve source code from file 4. Return to user ``` ## Quick Start ### 1. Index a Repository ```bash # Index Dagster repository python build_code_index.py --repo dagster --path /home/ubuntu/dagster # Index PyIceberg repository python build_code_index.py --repo pyiceberg --path /home/jfj/repos/iceberg-python # Include private objects (starting with _) python build_code_index.py --repo dagster --path /home/ubuntu/dagster --include-private # Use custom database location python build_code_index.py --repo dagster --path /home/ubuntu/dagster --db ./my_index.db ``` ### 2. Use in MCP (Claude) Once indexed, new MCP tools become available: #### Search for Code Objects ``` search_code_index: query: "AutomationCondition" search_type: "exact" # or "prefix", "contains" Returns: List of matching objects with file locations ``` #### Get Source Code by Name ``` get_code_by_name: name: "AutomationCondition.eager" mode: "full" # or "signature", "outline", "methods_list" Returns: Complete source code instantly ``` #### List Indexed Repositories ``` list_indexed_repos Returns: All repositories in the index ``` #### Get Index Statistics ``` get_code_index_stats Returns: Object counts by type and repository ``` ## Configuration ### Settings (config/settings.py) ```python # Code Index code_index_path: str = "./code_index.db" # Database location enable_code_index: bool = True # Enable/disable code index ``` ### Environment Variables (.env) ```bash CODE_INDEX_PATH=./code_index.db ENABLE_CODE_INDEX=true ``` ## Advanced Usage ### Search Types **Exact Match** - Find exact name or qualified name: ```python rag_system.search_code("AutomationCondition", search_type="exact") # Returns: AutomationCondition class ``` **Prefix Search** - Find objects starting with pattern: ```python rag_system.search_code("Automation", search_type="prefix") # Returns: AutomationCondition, AutomationContext, etc. ``` **Contains Search** - Find objects containing pattern: ```python rag_system.search_code("Condition", search_type="contains") # Returns: AutomationCondition, ConditionManager, etc. ``` ### Retrieval Modes **Signature** - Just the definition line: ```python rag_system.get_source_code_from_index("AutomationCondition.eager", mode="signature") # Returns: def eager() -> AutomationCondition: ``` **Methods List** - Class with method names only: ```python rag_system.get_source_code_from_index("AutomationCondition", mode="methods_list") # Returns: # class AutomationCondition: # - eager # - on_cron # - missing # ... (58 more methods) ``` **Outline** - Class with all method signatures: ```python rag_system.get_source_code_from_index("AutomationCondition", mode="outline") # Returns: Class with all method signatures but no implementations ``` **Full** - Complete implementation: ```python rag_system.get_source_code_from_index("AutomationCondition.eager", mode="full") # Returns: Complete source code with context ``` ### Repository Filtering When you have multiple repositories indexed: ```python # Search only in Dagster rag_system.search_code("Table", repo_name="dagster") # Search in PyIceberg rag_system.search_code("Table", repo_name="pyiceberg") # Search across all repos rag_system.search_code("Table") # Returns matches from all repos ``` ## Adding New Libraries ### Step 1: Index the Repository ```bash # Example: Adding PyIceberg python build_code_index.py \ --repo pyiceberg \ --path /home/jfj/repos/iceberg-python \ --exclude "**/test_*.py" "**/*_test.py" "**/tests/**" ``` ### Step 2: Verify Indexing ```python from utils.code_index_store import CodeIndexStore store = CodeIndexStore() print(store.list_repos()) # Should show ['dagster', 'pyiceberg'] print(store.get_stats()) # Shows object counts ``` ### Step 3: Start Using The code is immediately available for queries: ```bash # Via MCP search_code_index: query: "IcebergTable" repo_name: "pyiceberg" ``` ## Performance Comparison ### Before (RAG-based code search): ``` Query: "show me AutomationCondition.eager" → Semantic search through docs (500ms) → Extract references from text (100ms) → Hope docs mention GitHub URL (unreliable) → Parse URL and retrieve code (200ms) Total: ~800ms + unreliable ``` ### After (Index-based search): ``` Query: "show me AutomationCondition.eager" → Direct index lookup (5ms) → Retrieve source code (10ms) Total: ~15ms + 100% reliable ``` **50x faster** and fully reliable! ## Database Schema The SQLite database stores: ```sql CREATE TABLE code_objects ( id INTEGER PRIMARY KEY, name TEXT, -- Simple name (e.g., "eager") qualified_name TEXT UNIQUE, -- Full name (e.g., "dagster.AutomationCondition.eager") type TEXT, -- 'class', 'function', 'method', etc. file_path TEXT, -- Absolute path to file line_number INTEGER, -- Starting line end_line_number INTEGER, -- Ending line repo_name TEXT, -- Repository name relative_path TEXT, -- Path relative to repo docstring TEXT, -- First line of docstring parent_class TEXT, -- Parent class for methods decorators TEXT, -- JSON array of decorators is_private INTEGER -- Boolean flag ); ``` Indices for fast lookup: - `idx_name` - Fast lookup by simple name - `idx_qualified_name` - Fast lookup by qualified name - `idx_repo_name` - Filter by repository - `idx_type` - Filter by object type - `idx_parent_class` - Find methods of a class ## Best Practices ### 1. Exclude Test Files Test files add noise and are rarely needed: ```bash python build_code_index.py --repo mylib --path /path/to/mylib \ --exclude "**/test_*.py" "**/*_test.py" "**/tests/**" ``` ### 2. Use Qualified Names When Possible More specific = faster and more accurate: ```python # Good search_code("dagster.AutomationCondition.eager") # Less precise (may have multiple matches) search_code("eager") ``` ### 3. Re-index After Major Updates When the codebase changes significantly: ```bash # Replace existing index python build_code_index.py --repo dagster --path /home/ubuntu/dagster --replace ``` ### 4. Use Appropriate Retrieval Modes - **Exploring?** Use `methods_list` to see what's available - **Understanding API?** Use `outline` to see all signatures - **Quick reference?** Use `signature` - **Deep dive?** Use `full` ## Troubleshooting ### Issue: "Code index is not enabled" **Solution:** Check settings: ```python # config/settings.py enable_code_index: bool = True ``` ### Issue: "Repository not found in index" **Solution:** Index the repository first: ```bash python build_code_index.py --repo <name> --path /path/to/repo ``` ### Issue: "Too many results" **Solution:** Use more specific queries or add repository filter: ```python # Too broad search_code("query") # Returns hundreds # Better search_code("query", repo_name="dagster") # Best search_code("dagster.query_assets") ``` ### Issue: "Object not found" **Solution:** Verify the object exists: ```bash # Re-index to pick up new code python build_code_index.py --repo dagster --path /home/ubuntu/dagster --replace ``` ## Future Enhancements Potential improvements: 1. **Incremental Indexing** - Update index without full re-scan 2. **Git Integration** - Auto-detect changes and re-index 3. **Type Information** - Index type annotations for better search 4. **Import Resolution** - Understand import relationships 5. **Multi-Language** - Support JavaScript, TypeScript, etc. 6. **Fuzzy Search** - Typo-tolerant search 7. **Dependency Graph** - Understand code relationships ## Example Workflows ### Adding PyIceberg Support ```bash # 1. Clone/locate PyIceberg git clone https://github.com/apache/iceberg-python.git ~/repos/iceberg-python # 2. Index the repository python build_code_index.py \ --repo pyiceberg \ --path ~/repos/iceberg-python # 3. Verify python -c " from utils.code_index_store import CodeIndexStore store = CodeIndexStore() print('Repos:', store.list_repos()) results = store.get_by_name('Table', repo_name='pyiceberg') print(f'Found {len(results)} Table classes in PyIceberg') " # 4. Use in queries # Now you can ask: "show me PyIceberg Table class" ``` ### Exploring a New Codebase ```python # 1. Search for main entry points results = rag_system.search_code("main", repo_name="newlib", search_type="contains") # 2. Find all classes store = CodeIndexStore() classes = store.get_by_type("class", repo_name="newlib", limit=20) # 3. Explore a specific class outline = rag_system.get_source_code_from_index( "SomeClass", repo_name="newlib", mode="outline" ) # 4. Deep dive into a method impl = rag_system.get_source_code_from_index( "SomeClass.some_method", repo_name="newlib", mode="full" ) ``` ## Summary The code indexing system transforms the RAG from a documentation-only tool to a **powerful code exploration and search platform**. By indexing at ingestion time, we achieve: - ⚡ Lightning-fast code lookup - 🎯 100% accurate results - 🔍 Complete codebase coverage - 📚 Multi-library support - 🚀 Easy to add new libraries Ready to index your first library? Run: ```bash python build_code_index.py --repo <name> --path /path/to/repo ```

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jaimeferj/mcp-rag-docs'

If you have feedback or need assistance with the MCP directory API, please join our Discord server