|
1 | 1 | # Largefile MCP Server |
2 | 2 |
|
3 | | -MCP server for working with large text files that exceed LLM context limits. |
| 3 | +MCP server that helps your AI assistant work with large files that exceed context limits. |
4 | 4 |
|
5 | | -This server enables LLMs to navigate, search, and edit files of any size without loading the entire content into memory. It provides targeted access to specific lines, patterns, and sections while maintaining file integrity. Perfect for working with large codebases, generated files, logs, and datasets that would otherwise be inaccessible to AI tools. |
| 5 | +[](https://github.com/peteretelej/largefile/actions/workflows/ci.yml) [](https://pypi.org/project/largefile/) [](https://www.python.org/downloads/) [](https://opensource.org/licenses/MIT) [](https://github.com/astral-sh/ruff) [](https://github.com/astral-sh/uv) |
6 | 6 |
|
7 | | -## Goal |
| 7 | +This MCP server enables AI assistants to navigate, search, and edit files of any size without loading entire content into memory. It provides targeted access to specific lines, patterns, and sections while maintaining file integrity using research-backed search/replace editing instead of error-prone line-based operations. Perfect for working with large codebases, generated files, logs, and datasets that would otherwise be inaccessible due to context window limitations. |
8 | 8 |
|
9 | | -Enable LLMs to perform precise operations on files of any size through intelligent navigation, targeted search, and atomic editing, eliminating the context barrier that prevents AI tools from working with production codebases or very large documents. |
| 9 | +## MCP Tools |
10 | 10 |
|
11 | | -## Planned Features |
| 11 | +The server provides 4 core tools that work together for progressive file exploration: |
12 | 12 |
|
13 | | -- **Smart Navigation**: Jump to specific lines, functions, or patterns instantly |
14 | | -- **Intelligent Search**: Pattern-based search across files without context limits |
15 | | -- **Precision Editing**: Atomic edit operations that preserve file integrity |
16 | | -- **Context Extraction**: Extract minimal relevant context for LLM consumption |
17 | | -- **Atomic Calculations**: Perform file-wide calculations without full loading |
| 13 | +- **`get_overview`** - Get file structure with Tree-sitter semantic analysis, line counts, and suggested search patterns |
| 14 | +- **`search_content`** - Find patterns with fuzzy matching, context lines, and semantic information |
| 15 | +- **`read_content`** - Read targeted content by line number or pattern with semantic chunking (complete functions/classes) |
| 16 | +- **`edit_content`** - Primary editing via search/replace blocks with automatic backups and preview mode |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +**Prerequisite:** Install [uv](https://docs.astral.sh/uv/getting-started/installation/) (an extremely fast Python package manager) which provides the `uvx` command. |
| 21 | + |
| 22 | +Add to your MCP configuration: |
| 23 | + |
| 24 | +```json |
| 25 | +{ |
| 26 | + "mcpServers": { |
| 27 | + "largefile": { |
| 28 | + "command": "uvx", |
| 29 | + "args": ["--from", "largefile", "largefile-mcp"] |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +## Usage Examples |
| 36 | + |
| 37 | +### Analyzing Large Code Files |
| 38 | + |
| 39 | +**AI Question:** _"Can you analyze this large Django models file and tell me about the class structure and any potential issues? It's a large file so use largefile."_ |
| 40 | + |
| 41 | +**AI Assistant workflow:** |
| 42 | + |
| 43 | +1. Get file overview to understand structure |
| 44 | +2. Search for classes and their methods |
| 45 | +3. Look for code issues like TODOs or long functions |
| 46 | + |
| 47 | +```python |
| 48 | +# AI gets file structure |
| 49 | +overview = get_overview("/path/to/django-models.py") |
| 50 | +# Returns: 2,847 lines, 15 classes, semantic outline with Tree-sitter |
| 51 | + |
| 52 | +# AI searches for all class definitions |
| 53 | +classes = search_content("/path/to/django-models.py", "class ", max_results=20) |
| 54 | +# Returns: Model classes with line numbers and context |
| 55 | + |
| 56 | +# AI examines specific class implementation |
| 57 | +model_code = read_content("/path/to/django-models.py", "class User", mode="semantic") |
| 58 | +# Returns: Complete class definition with all methods |
| 59 | +``` |
| 60 | + |
| 61 | +### Working with Documentation |
| 62 | + |
| 63 | +**AI Question:** _"Find all the installation methods mentioned in this README file and update the pip install to use uv instead."_ |
| 64 | + |
| 65 | +**AI Assistant workflow:** |
| 66 | + |
| 67 | +1. Search for installation patterns |
| 68 | +2. Read the installation section |
| 69 | +3. Replace pip commands with uv equivalents |
| 70 | + |
| 71 | +```python |
| 72 | +# AI finds installation instructions |
| 73 | +install_sections = search_content("/path/to/readme.md", "install", fuzzy=True, context_lines=3) |
| 74 | + |
| 75 | +# AI reads the installation section |
| 76 | +install_content = read_content("/path/to/readme.md", "## Installation", mode="semantic") |
| 77 | + |
| 78 | +# AI replaces pip with uv |
| 79 | +edit_result = edit_content( |
| 80 | + "/path/to/readme.md", |
| 81 | + search_text="pip install anthropic", |
| 82 | + replace_text="uv add anthropic", |
| 83 | + preview=True |
| 84 | +) |
| 85 | +``` |
| 86 | + |
| 87 | +### Debugging Large Log Files |
| 88 | + |
| 89 | +**AI Question:** _"Check this production log file for any critical errors in the last few thousand lines and show me the context around them. Use largefile mcp."_ |
| 90 | + |
| 91 | +**AI Assistant workflow:** |
| 92 | + |
| 93 | +1. Get log file overview |
| 94 | +2. Search for error patterns |
| 95 | +3. Read context around critical issues |
| 96 | + |
| 97 | +```python |
| 98 | +# AI gets log file overview |
| 99 | +overview = get_overview("/path/to/production.log") |
| 100 | +# Returns: 150,000 lines, 2.1GB file size |
| 101 | + |
| 102 | +# AI searches for critical errors |
| 103 | +errors = search_content("/path/to/production.log", "CRITICAL|ERROR", fuzzy=True, max_results=10) |
| 104 | + |
| 105 | +# AI examines context around each error |
| 106 | +for error in errors: |
| 107 | + context = read_content("/path/to/production.log", error.line_number, mode="lines") |
| 108 | + # Shows surrounding log entries for debugging |
| 109 | +``` |
| 110 | + |
| 111 | +### Refactoring Code |
| 112 | + |
| 113 | +**AI Question:** _"I need to rename the function `process_data` to `transform_data` throughout this large codebase file. Can you help me do this safely?"_ |
| 114 | + |
| 115 | +**AI Assistant workflow:** |
| 116 | + |
| 117 | +1. Find all occurrences of the function |
| 118 | +2. Preview changes to ensure accuracy |
| 119 | +3. Apply changes with automatic backup |
| 120 | + |
| 121 | +```python |
| 122 | +# AI finds all usages |
| 123 | +usages = search_content("/path/to/codebase.py", "process_data", fuzzy=False, max_results=50) |
| 124 | + |
| 125 | +# AI previews the changes |
| 126 | +preview = edit_content( |
| 127 | + "/path/to/codebase.py", |
| 128 | + search_text="process_data", |
| 129 | + replace_text="transform_data", |
| 130 | + preview=True |
| 131 | +) |
| 132 | + |
| 133 | +# AI applies changes after confirmation |
| 134 | +result = edit_content( |
| 135 | + "/path/to/codebase.py", |
| 136 | + search_text="process_data", |
| 137 | + replace_text="transform_data", |
| 138 | + preview=False |
| 139 | +) |
| 140 | +# Creates automatic backup before changes |
| 141 | +``` |
| 142 | + |
| 143 | +### Exploring API Documentation |
| 144 | + |
| 145 | +**AI Question:** _"What are all the available methods in this large API documentation file and can you show me examples of authentication?"_ |
| 146 | + |
| 147 | +**AI Assistant workflow:** |
| 148 | + |
| 149 | +1. Get document structure overview |
| 150 | +2. Search for method definitions and auth patterns |
| 151 | +3. Extract relevant code examples |
| 152 | + |
| 153 | +```python |
| 154 | +# AI analyzes document structure |
| 155 | +overview = get_overview("/path/to/api-docs.md") |
| 156 | +# Returns: Section outline, headings, suggested search patterns |
| 157 | + |
| 158 | +# AI finds API methods |
| 159 | +methods = search_content("/path/to/api-docs.md", "###", max_results=30) |
| 160 | +# Returns: All method headings with context |
| 161 | + |
| 162 | +# AI searches for authentication examples |
| 163 | +auth_examples = search_content("/path/to/api-docs.md", "auth", fuzzy=True, context_lines=5) |
| 164 | + |
| 165 | +# AI reads complete authentication section |
| 166 | +auth_section = read_content("/path/to/api-docs.md", "## Authentication", mode="semantic") |
| 167 | +``` |
| 168 | + |
| 169 | +## File Size Handling |
| 170 | + |
| 171 | +- **Small files (<50MB)**: Memory loading with Tree-sitter AST caching |
| 172 | +- **Medium files (50-500MB)**: Memory-mapped access |
| 173 | +- **Large files (>500MB)**: Streaming processing |
| 174 | +- **Long lines (>1000 chars)**: Automatic truncation for display |
| 175 | + |
| 176 | +## Supported Languages |
| 177 | + |
| 178 | +Tree-sitter semantic analysis for: |
| 179 | + |
| 180 | +- Python (.py) |
| 181 | +- JavaScript/JSX (.js, .jsx) |
| 182 | +- TypeScript/TSX (.ts, .tsx) |
| 183 | +- Rust (.rs) |
| 184 | +- Go (.go) |
| 185 | + |
| 186 | +Files without Tree-sitter support use text-based analysis with graceful degradation. |
| 187 | + |
| 188 | +## Configuration |
| 189 | + |
| 190 | +Configure via environment variables: |
| 191 | + |
| 192 | +```bash |
| 193 | +# File processing thresholds |
| 194 | +LARGEFILE_MEMORY_THRESHOLD_MB=50 # Memory loading limit |
| 195 | +LARGEFILE_MMAP_THRESHOLD_MB=500 # Memory mapping limit |
| 196 | + |
| 197 | +# Search settings |
| 198 | +LARGEFILE_FUZZY_THRESHOLD=0.8 # Fuzzy match sensitivity |
| 199 | +LARGEFILE_MAX_SEARCH_RESULTS=20 # Result limit |
| 200 | +LARGEFILE_CONTEXT_LINES=2 # Context window |
| 201 | + |
| 202 | +# Performance |
| 203 | +LARGEFILE_ENABLE_TREE_SITTER=true # Semantic features |
| 204 | +LARGEFILE_BACKUP_DIR=".largefile_backups" # Backup location |
| 205 | +``` |
| 206 | + |
| 207 | +## Key Features |
| 208 | + |
| 209 | +- **Search/replace primary**: Eliminates LLM line number errors |
| 210 | +- **Fuzzy matching**: Handles whitespace and formatting variations |
| 211 | +- **Atomic operations**: File integrity with automatic backups |
| 212 | +- **Semantic awareness**: Tree-sitter integration for code structure |
| 213 | +- **Memory efficient**: Handles files of any size without context limits |
| 214 | +- **Error recovery**: Graceful degradation with clear error messages |
| 215 | + |
| 216 | +## Documentation |
| 217 | + |
| 218 | +- [API Reference](docs/api-reference.md) - Detailed tool documentation |
| 219 | +- [Configuration Guide](docs/configuration.md) - Environment variables and tuning |
| 220 | +- [Examples](docs/examples.md) - Real-world usage examples and workflows |
| 221 | +- [Design Document](docs/design.md) - Architecture and implementation details |
0 commit comments