mcp-code-context 2.5.1 → 3.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/CHANGELOG.md +92 -236
  2. package/README.md +304 -298
  3. package/dist/src/core/engine.d.ts +21 -0
  4. package/dist/src/core/engine.js +68 -0
  5. package/dist/src/core/engine.js.map +1 -0
  6. package/dist/src/core/validator.d.ts +15 -0
  7. package/dist/src/core/validator.js +44 -0
  8. package/dist/src/core/validator.js.map +1 -0
  9. package/dist/src/index.d.ts +7 -11
  10. package/dist/src/index.js +182 -397
  11. package/dist/src/index.js.map +1 -1
  12. package/dist/src/operations/write.d.ts +51 -0
  13. package/dist/src/operations/write.js +169 -0
  14. package/dist/src/operations/write.js.map +1 -0
  15. package/dist/src/parsers/base.d.ts +28 -0
  16. package/dist/src/parsers/base.js +27 -0
  17. package/dist/src/parsers/base.js.map +1 -0
  18. package/dist/src/parsers/dartTreeSitter.d.ts +13 -0
  19. package/dist/src/parsers/dartTreeSitter.js +91 -0
  20. package/dist/src/parsers/dartTreeSitter.js.map +1 -0
  21. package/dist/src/parsers/php.d.ts +12 -0
  22. package/dist/src/parsers/php.js +86 -0
  23. package/dist/src/parsers/php.js.map +1 -0
  24. package/dist/src/parsers/python.d.ts +12 -0
  25. package/dist/src/parsers/python.js +86 -0
  26. package/dist/src/parsers/python.js.map +1 -0
  27. package/dist/src/parsers/registry.d.ts +15 -0
  28. package/dist/src/parsers/registry.js +70 -0
  29. package/dist/src/parsers/registry.js.map +1 -0
  30. package/dist/src/parsers/typescript.d.ts +13 -0
  31. package/dist/src/parsers/typescript.js +98 -0
  32. package/dist/src/parsers/typescript.js.map +1 -0
  33. package/llms.txt +246 -234
  34. package/mcp-code-context-3.0.2.tgz +0 -0
  35. package/package.json +59 -57
  36. package/tsconfig.tests.json +27 -0
  37. package/.io/RULES.md +0 -12
  38. package/CONTRIBUTING.md +0 -243
  39. package/PLAN_v2.5.0.md +0 -407
  40. package/TROUBLESHOOTING.md +0 -38
  41. package/dist/src/ast/dartCompressor.d.ts +0 -27
  42. package/dist/src/ast/dartCompressor.js +0 -859
  43. package/dist/src/ast/dartCompressor.js.map +0 -1
  44. package/dist/src/ast/phpCompressor.d.ts +0 -24
  45. package/dist/src/ast/phpCompressor.js +0 -634
  46. package/dist/src/ast/phpCompressor.js.map +0 -1
  47. package/dist/src/ast/semanticCompressor.d.ts +0 -22
  48. package/dist/src/ast/semanticCompressor.js +0 -677
  49. package/dist/src/ast/semanticCompressor.js.map +0 -1
  50. package/dist/src/ast/writers/dartWriter.d.ts +0 -37
  51. package/dist/src/ast/writers/dartWriter.js +0 -544
  52. package/dist/src/ast/writers/dartWriter.js.map +0 -1
  53. package/dist/src/ast/writers/phpWriter.d.ts +0 -37
  54. package/dist/src/ast/writers/phpWriter.js +0 -411
  55. package/dist/src/ast/writers/phpWriter.js.map +0 -1
  56. package/dist/src/ast/writers/pyWriter.d.ts +0 -37
  57. package/dist/src/ast/writers/pyWriter.js +0 -355
  58. package/dist/src/ast/writers/pyWriter.js.map +0 -1
  59. package/dist/src/ast/writers/safeRename.d.ts +0 -14
  60. package/dist/src/ast/writers/safeRename.js +0 -258
  61. package/dist/src/ast/writers/safeRename.js.map +0 -1
  62. package/dist/src/ast/writers/symbolWriter.d.ts +0 -64
  63. package/dist/src/ast/writers/symbolWriter.js +0 -174
  64. package/dist/src/ast/writers/symbolWriter.js.map +0 -1
  65. package/dist/src/ast/writers/tsWriter.d.ts +0 -38
  66. package/dist/src/ast/writers/tsWriter.js +0 -344
  67. package/dist/src/ast/writers/tsWriter.js.map +0 -1
  68. package/dist/src/cache/astCache.d.ts +0 -39
  69. package/dist/src/cache/astCache.js +0 -169
  70. package/dist/src/cache/astCache.js.map +0 -1
  71. package/dist/src/handlers/readHandlers.d.ts +0 -70
  72. package/dist/src/handlers/readHandlers.js +0 -455
  73. package/dist/src/handlers/readHandlers.js.map +0 -1
  74. package/dist/src/handlers/utilHandlers.d.ts +0 -31
  75. package/dist/src/handlers/utilHandlers.js +0 -69
  76. package/dist/src/handlers/utilHandlers.js.map +0 -1
  77. package/dist/src/handlers/writeHandlers.d.ts +0 -57
  78. package/dist/src/handlers/writeHandlers.js +0 -499
  79. package/dist/src/handlers/writeHandlers.js.map +0 -1
  80. package/dist/src/tools/readFileLines.d.ts +0 -26
  81. package/dist/src/tools/readFileLines.js +0 -77
  82. package/dist/src/tools/readFileLines.js.map +0 -1
  83. package/dist/src/tools/searchCodePattern.d.ts +0 -37
  84. package/dist/src/tools/searchCodePattern.js +0 -144
  85. package/dist/src/tools/searchCodePattern.js.map +0 -1
  86. package/dist/src/utils/arrayUtils.d.ts +0 -12
  87. package/dist/src/utils/arrayUtils.js +0 -27
  88. package/dist/src/utils/arrayUtils.js.map +0 -1
  89. package/dist/src/utils/backupManager.d.ts +0 -31
  90. package/dist/src/utils/backupManager.js +0 -124
  91. package/dist/src/utils/backupManager.js.map +0 -1
  92. package/dist/src/utils/confirmationCache.d.ts +0 -16
  93. package/dist/src/utils/confirmationCache.js +0 -40
  94. package/dist/src/utils/confirmationCache.js.map +0 -1
  95. package/dist/src/utils/constants.d.ts +0 -46
  96. package/dist/src/utils/constants.js +0 -112
  97. package/dist/src/utils/constants.js.map +0 -1
  98. package/dist/src/utils/dependencyChecker.d.ts +0 -1
  99. package/dist/src/utils/dependencyChecker.js +0 -55
  100. package/dist/src/utils/dependencyChecker.js.map +0 -1
  101. package/dist/src/utils/diffEngine.d.ts +0 -21
  102. package/dist/src/utils/diffEngine.js +0 -163
  103. package/dist/src/utils/diffEngine.js.map +0 -1
  104. package/dist/src/utils/fuzzyMatch.d.ts +0 -8
  105. package/dist/src/utils/fuzzyMatch.js +0 -69
  106. package/dist/src/utils/fuzzyMatch.js.map +0 -1
  107. package/dist/src/utils/ignoreManager.d.ts +0 -30
  108. package/dist/src/utils/ignoreManager.js +0 -186
  109. package/dist/src/utils/ignoreManager.js.map +0 -1
  110. package/dist/src/utils/normalization.d.ts +0 -49
  111. package/dist/src/utils/normalization.js +0 -173
  112. package/dist/src/utils/normalization.js.map +0 -1
  113. package/dist/src/utils/projectRoot.d.ts +0 -17
  114. package/dist/src/utils/projectRoot.js +0 -42
  115. package/dist/src/utils/projectRoot.js.map +0 -1
  116. package/dist/src/utils/secureValidation.d.ts +0 -15
  117. package/dist/src/utils/secureValidation.js +0 -41
  118. package/dist/src/utils/secureValidation.js.map +0 -1
  119. package/dist/src/utils/syntaxValidator.d.ts +0 -15
  120. package/dist/src/utils/syntaxValidator.js +0 -218
  121. package/dist/src/utils/syntaxValidator.js.map +0 -1
  122. package/dist/src/utils/transactionManager.d.ts +0 -46
  123. package/dist/src/utils/transactionManager.js +0 -123
  124. package/dist/src/utils/transactionManager.js.map +0 -1
  125. package/dist/src/utils/validation.d.ts +0 -87
  126. package/dist/src/utils/validation.js +0 -231
  127. package/dist/src/utils/validation.js.map +0 -1
  128. package/docs/ARCHITECTURE.md +0 -300
  129. package/docs/architecture/ADR-001-modular-handlers.md +0 -119
package/README.md CHANGED
@@ -1,298 +1,304 @@
1
- # mcp-code-context
2
-
3
- [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
4
- [![Tests](https://img.shields.io/badge/tests-passing-success.svg)]()
5
- [![TypeScript](https://img.shields.io/badge/TypeScript-100%25-blue.svg)]()
6
- [![Version](https://img.shields.io/badge/version-2.5.1-blue.svg)]()
7
-
8
- > MCP server that compresses any codebase into LLM-ready semantic context **and provides surgical code editing tools**. AST-based read & write for TypeScript, JavaScript, PHP, Dart & Python.
9
-
10
- ## 🚀 Quick Start (Claude Desktop)
11
-
12
- 1. **Install**: `npm install -g mcp-code-context`
13
- 2. **Configure**: Add to `claude_desktop_config.json`:
14
- ```json
15
- {
16
- "mcpServers": {
17
- "code-context": {
18
- "command": "npx",
19
- "args": ["-y", "mcp-code-context"]
20
- }
21
- }
22
- }
23
- ```
24
- 3. **Enjoy**: Use symbols like `@code-context` to map repos or edit code surgically.
25
-
26
- Works with **Claude Desktop**, **Cursor**, **Windsurf**, **GitHub Copilot**, **Amazon Q**, and any [Model Context Protocol](https://modelcontextprotocol.io/) compatible client.
27
-
28
- ## The Problem
29
-
30
- LLMs working with code face two bottlenecks:
31
- 1. **Reading**: Sending raw source files wastes the context window on function bodies and boilerplate. A 500-line file might contain only 30 lines of *structural* information the LLM needs.
32
- 2. **Writing**: Rewriting entire files to change one function is error-prone, token-expensive, and risks corrupting unrelated code.
33
-
34
- ## The Solution
35
-
36
- `mcp-code-context` provides **11 tools** — 5 for reading, 1 for cleanup, and 5 for writing — that operate at the **symbol level** (functions, classes, methods). Furthermore, tools support a `className` scope which correctly isolates identical symbol names in the same file (e.g. Flutter `build()` methods) to avoid reading or changing the wrong logic. Read tools extract structural skeletons. Write tools splice changes into the exact AST location.
37
-
38
- | File | Original | Compressed | Reduction |
39
- |------|----------|------------|-----------|
40
- | PHP class (426 lines) | 426 | 60 | **85.9%** |
41
- | Dart repository (230 lines) | 230 | 30 | **87.0%** |
42
- | PHP config (68 lines) | 68 | 15 | **77.9%** |
43
-
44
- ## Reliability & Testing
45
-
46
- Built to be robust and precise. Both read and write engines are tested against real-world, complex codebases (including nested generic types in Dart, complex interfaces in PHP, and multi-file rename operations) with a **100% test pass rate** across all languages and operations.
47
-
48
- ## Features
49
-
50
- ### Read
51
- - 🌳 **AST-based compression** — Real parsers for TypeScript/JavaScript ([ts-morph](https://ts-morph.com/)) and PHP ([php-parser](https://github.com/nickygerritsen/php-parser)). Brace-counting engine for Dart. Regex-based extraction for Python.
52
- - 🔬 **Surgical symbol extraction** — Extract a single function, class, or method from a file by name. Use `className` to scope disambiguation (e.g., getting multiple `build()` methods in Dart).
53
- - 💥 **Impact analysis** — Discover all files that depend on a given file before refactoring. Supports ES imports, CommonJS `require()`, Python imports, PHP `use`/`require_once`/`include`, and Dart imports.
54
- - 📁 **Smart file walking** — Respects `.gitignore` and `.repomixignore` rules. Automatically excludes `node_modules`, `dist`, `vendor`, `.git`, etc.
55
- - 📄 **Multi-format output** — XML (optimized for LLM consumption) or Markdown (human-readable).
56
-
57
- ### Write
58
- - ✏️ **Surgical symbol replacement** — Replace a function, method, or class body without touching the rest of the file. Narrow down the target using the `className` parameter.
59
- - ➕ **Precise code insertion** — Insert new code before/after a symbol, or inside a class at the start/end.
60
- - 🔄 **Repository-wide rename** — Rename a symbol in its definition AND all files that import it, atomically.
61
- - 🗑️ **Safe symbol removal** — Delete code with automatic dependency checking to prevent breakage.
62
- - 🔍 **Mandatory dry-run flow** — Write tools return a preview diff and a `confirmationToken` by default. Changes are only applied after explicit confirmation.
63
- - 💾 **Robust rolling backups** — Automatically keeps the last 5 versions of modified files in a hidden `.mcp-backups/` directory.
64
- - ⏪ **Surgical rollback** — Revert files to any of the 5 previous states using the new `rollback_file` tool.
65
- - 🤖 **Fuzzy symbol matching** — When a symbol is not found, the server provides structured suggestions based on Levenshtein distance.
66
- - 🔐 **Private symbol support** — Full support for `_` and `__` prefixed symbols in Dart and Python.
67
-
68
- ## Supported Languages
69
-
70
- | Language | Read (Compress + Extract) | Write (Replace + Insert + Rename + Remove) | Import Analysis |
71
- |----------|---------------------------|---------------------------------------------|------------------|
72
- | TypeScript / JavaScript | ✅ AST (ts-morph) | ✅ AST (ts-morph) | ✅ |
73
- | PHP | ✅ AST (php-parser) | ✅ AST + line-splice | ✅ |
74
- | Dart | ✅ Brace-counting + regex | ✅ Brace-counting + regex | ✅ |
75
- | Python | ✅ Regex-based | ✅ Indentation-aware | ✅ |
76
- | Others (JSON, YAML, CSS, etc.) | Passthrough / truncation | — | — |
77
-
78
- ## ⚠️ Known Limitations
79
-
80
- ### `rename_symbol` Tool
81
-
82
- **Dart and Python**: Cross-file rename is **NOT supported** for `.dart`, `.py`, and `.pyi` files.
83
-
84
- - **Reason**: No AST parser available for safe cross-file refactoring
85
- - **Risk**: Regex-based rename may corrupt strings, comments, or unrelated code
86
- - **Recommendation**: Use IDE refactoring tools instead:
87
- - **Dart**: VS Code with Dart extension (F2 key) or IntelliJ IDEA
88
- - **Python**: PyCharm, VS Code with Pylance (F2 key)
89
- - **Alternative**: Use `write_file_surgical` to rename within a single file
90
-
91
- **TypeScript, JavaScript, PHP**: Fully supported with AST-aware renaming ✅
92
-
93
- ### `get_semantic_repo_map` Tool
94
-
95
- - **Max files**: Limited to 500 files to prevent timeouts
96
- - **Performance**: Synchronous I/O may take 10-30 seconds on large repositories
97
- - **Recommendation**: Use `@folder` syntax to target specific directories
98
-
99
- ### General
100
-
101
- - **Validation**: No automatic syntax checking after edits. Always review diffs carefully before confirming.
102
- - **Backups**: 5-version rolling backup system. Use `rollback_file` if something goes wrong.
103
- - **Large files**: Files >10MB are skipped for safety.
104
-
105
- ## Installation
106
-
107
- ```bash
108
- # Clone the repository
109
- git clone https://github.com/YOUR_USERNAME/mcp-code-context.git
110
- cd mcp-code-context
111
-
112
- # Install dependencies
113
- npm install
114
-
115
- # Build
116
- npm run build
117
- ```
118
-
119
- ## Configuration
120
-
121
- ### Claude Desktop
122
-
123
- Add to your `claude_desktop_config.json`:
124
-
125
- ```json
126
- {
127
- "mcpServers": {
128
- "mcp-code-context": {
129
- "command": "node",
130
- "args": ["/absolute/path/to/mcp-code-context/dist/index.js"]
131
- }
132
- }
133
- }
134
- ```
135
-
136
- ### Cursor
137
-
138
- Add to your Cursor MCP settings (`.cursor/mcp.json`):
139
-
140
- ```json
141
- {
142
- "mcpServers": {
143
- "mcp-code-context": {
144
- "command": "node",
145
- "args": ["/absolute/path/to/mcp-code-context/dist/index.js"]
146
- }
147
- }
148
- }
149
- ```
150
-
151
- ### Windsurf
152
-
153
- Add to your Windsurf MCP config:
154
-
155
- ```json
156
- {
157
- "mcpServers": {
158
- "mcp-code-context": {
159
- "command": "node",
160
- "args": ["/absolute/path/to/mcp-code-context/dist/index.js"]
161
- }
162
- }
163
- }
164
- ```
165
-
166
- ### Amazon Q / Other MCP Clients
167
-
168
- Any MCP-compatible client can use this server. The transport is **stdio** (JSON-RPC over stdin/stdout). Point your client to `node dist/index.js`.
169
-
170
- ## Tools
171
-
172
- ### Read Tools
173
-
174
- #### 1. `get_semantic_repo_map`
175
- Generate a compressed architectural overview of an entire repository.
176
- - `directoryPath` (required) — Path to the repo root
177
- - `format` (optional) — `"xml"` (default) or `"markdown"`
178
-
179
- #### 2. `read_file_surgical`
180
- Read a file, or extract only a specific named symbol. Returns structured suggestions if the symbol is missing.
181
- - `filePath` (required) — Path to the source file
182
- - `symbolName` (optional) — Name of a function, class, method, or type
183
- - `className` (optional) — Scope the symbol to a specific class (to avoid duplicates)
184
-
185
- #### 3. `analyze_impact`
186
- Find all files that depend on a given file.
187
- - `filePath` (required) — Path to the file being modified
188
- - `rootDir` (optional) — Repository root (auto-detected)
189
-
190
- #### 4. `read_file_lines`
191
- Read specific line ranges from a file without loading the entire content. More efficient than `read_file_surgical` for small fragments.
192
- - `filePath` (required) — Path to the source file
193
- - `startLine` (optional) — Starting line number (1-indexed)
194
- - `endLine` (optional) — Ending line number (1-indexed)
195
- - `aroundPattern` (optional) — Search pattern to find and return surrounding lines
196
- - `contextLines` (optional) — Number of lines before/after pattern (default: 5)
197
-
198
- #### 5. `search_code_pattern`
199
- Search for code patterns across multiple files with context. Respects `.gitignore` rules.
200
- - `rootDir` (required) — Repository root directory
201
- - `pattern` (required) — Regular expression pattern to search
202
- - `fileExtensions` (optional) — Array of extensions to search (e.g., `[".ts", ".dart"]`)
203
- - `excludeDirs` (optional) — Directories to exclude (default: `["node_modules", "dist", "build"]`)
204
- - `showContext` (optional) — Include surrounding lines (default: true)
205
- - `contextLines` (optional) — Number of context lines (default: 3)
206
- - `maxResults` (optional) — Maximum matches to return (default: 50)
207
-
208
- #### 6. `rollback_file`
209
- Surgically restore a file to a previous state from the automated backup system.
210
- - `filePath` (required) — Path to the file to restore
211
- - `steps` (optional) — Number of versions to go back (1-5, default: 1)
212
-
213
- #### 7. `clean_backups`
214
- Remove all backup files for a project to keep the working directory clean.
215
- - `projectRoot` (required) — Absolute path to the project root
216
-
217
- **Note:** Backups are stored centrally at `[project-root]/.mcp-backups/` to keep your project organized.
218
-
219
- ### Write Tools (Phase 1: Preview)
220
-
221
- All write tools follow a **Two-Phase Workflow**:
222
- 1. **Call without token**: Returns a unified `diff` and a `confirmationToken`.
223
- 2. **Call with token**: Set `confirm: true` and provide the token to apply the changes.
224
-
225
- #### 7. `write_file_surgical`
226
- Replace the full source code of a named symbol in a file.
227
- - `filePath` (required) — Path to the file
228
- - `symbolName` (required) — Symbol to replace
229
- - `newContent` (required) — Replacement code (signature + body)
230
- - `confirmationToken` (optional) — Token from Phase 1 to apply changes
231
- - `confirm` (optional) — Set to `true` to apply
232
- - `className` (optional) — Scope the symbol to a specific class
233
-
234
- #### 8. `insert_symbol`
235
- Insert new code at a precise location relative to an existing symbol.
236
- - `filePath` (required) — Path to the file
237
- - `code` (required) — Code to insert
238
- - `anchorSymbol` (optional) — Symbol to position relative to
239
- - `position` (optional) — `"before"`, `"after"`, `"inside_start"`, `"inside_end"`
240
- - `className` (optional) — Scope the anchor to a specific class
241
- - `confirmationToken`, `confirm` (optional)
242
-
243
- #### 9. `rename_symbol`
244
- Rename a symbol across the entire repository (definition + all usages).
245
- - `filePath` (required) — File where the symbol is defined
246
- - `oldName` (required) — Current name
247
- - `newName` (required) — New name
248
- - `rootDir` (optional) — Repository root
249
- - `confirmationToken`, `confirm` (optional)
250
-
251
- #### 10. `remove_symbol`
252
- Safely remove a symbol from a file with dependency checking.
253
- - `filePath` (required) — Path to the file
254
- - `symbolName` (required) — Symbol to remove
255
- - `className` (optional) — Scope the symbol to a specific class
256
- - `force` (optional) — Skip dependency check
257
- - `confirmationToken`, `confirm` (optional)
258
-
259
- ## Recommended Workflow
260
-
261
- 1. **Understand** → `get_semantic_repo_map` to see the architecture
262
- 2. **Read** → `read_file_surgical` with symbol name for specific implementations
263
- 3. **Assess** → `analyze_impact` before modifying shared files
264
- 4. **Edit (Preview)** → Call write tools to generate a `diff` and `confirmationToken`
265
- 5. **Confim** → Call the same write tool with the token and `confirm: true` to apply
266
- 6. **Recovery** → Use `rollback_file` if something goes wrong after confirmation
267
-
268
- ## Development
269
-
270
- ```bash
271
- # Build
272
- npm run build
273
-
274
- # Run read tests (compression + extraction)
275
- npm run build && node dist/tests/test-dart.js && node dist/tests/test-php.js
276
-
277
- # Run write tests (replace, insert, rename, remove)
278
- npm run build && node dist/tests/writers/test-write-smoke.js
279
-
280
- # Run all tests
281
- npm run build && node dist/tests/test-dart.js && node dist/tests/test-php.js && node dist/tests/writers/test-write-smoke.js
282
-
283
- # Development (build + start)
284
- npm run dev
285
- ```
286
-
287
- ## Technical Details
288
-
289
- - **Transport:** stdio (JSON-RPC over stdin/stdout)
290
- - **Runtime:** Node.js >= 18
291
- - **Protocol:** [Model Context Protocol](https://modelcontextprotocol.io/)
292
- - **AST Engines:** ts-morph (TypeScript/JS), php-parser (PHP), brace-counting (Dart), regex (Python)
293
- - **Ignore Engine:** `ignore` npm package (full .gitignore spec support)
294
- - **Safety Features:** Mandatory two-phase confirmation, rolling 5-version backups, fuzzy matching, dependency checking, surgical restoration.
295
-
296
- ## License
297
-
298
- [MIT](LICENSE)
1
+ # mcp-code-context
2
+
3
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
4
+ [![Tests](https://img.shields.io/badge/tests-passing-success.svg)]()
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-100%25-blue.svg)]()
6
+ [![Version](https://img.shields.io/badge/version-3.0.4-blue.svg)]()
7
+
8
+ > MCP server with **Tree-sitter WASM parsers** for 100% AST accuracy. Zero native dependencies.
9
+
10
+ ## 🚀 Quick Start (Claude Desktop)
11
+
12
+ 1. **Install**: `npm install -g mcp-code-context`
13
+ 2. **Configure**: Add to `claude_desktop_config.json`:
14
+ ```json
15
+ {
16
+ "mcpServers": {
17
+ "code-context": {
18
+ "command": "npx",
19
+ "args": ["-y", "mcp-code-context"]
20
+ }
21
+ }
22
+ }
23
+ ```
24
+ 3. **Enjoy**: Use symbols like `@code-context` to map repos or edit code surgically.
25
+
26
+ **No build tools required** - Works on Windows/Mac/Linux without Visual Studio, Python, or node-gyp.
27
+
28
+ Works with **Claude Desktop**, **Cursor**, **Windsurf**, **GitHub Copilot**, **Amazon Q**, and any [Model Context Protocol](https://modelcontextprotocol.io/) compatible client.
29
+
30
+ ## The Problem
31
+
32
+ LLMs working with code face two bottlenecks:
33
+ 1. **Reading**: Sending raw source files wastes the context window on function bodies and boilerplate. A 500-line file might contain only 30 lines of *structural* information the LLM needs.
34
+ 2. **Writing**: Rewriting entire files to change one function is error-prone, token-expensive, and risks corrupting unrelated code.
35
+
36
+ ## The Solution
37
+
38
+ `mcp-code-context` provides **11 tools** — 5 for reading, 1 for cleanup, and 5 for writing — that operate at the **symbol level** (functions, classes, methods). Furthermore, tools support a `className` scope which correctly isolates identical symbol names in the same file (e.g. Flutter `build()` methods) to avoid reading or changing the wrong logic. Read tools extract structural skeletons. Write tools splice changes into the exact AST location.
39
+
40
+ | File | Original | Compressed | Reduction |
41
+ |------|----------|------------|-----------|
42
+ | PHP class (426 lines) | 426 | 60 | **85.9%** |
43
+ | Dart repository (230 lines) | 230 | 30 | **87.0%** |
44
+ | PHP config (68 lines) | 68 | 15 | **77.9%** |
45
+
46
+ ## Reliability & Testing
47
+
48
+ Built to be robust and precise. Both read and write engines are tested against real-world, complex codebases (including nested generic types in Dart, complex interfaces in PHP, and multi-file rename operations) with a **100% test pass rate** across all languages and operations.
49
+
50
+ ## Features
51
+
52
+ ### Read
53
+ - 🌳 **AST-based compression** — Real Tree-sitter WASM parsers for TypeScript/JavaScript/Python/PHP/Dart. Zero regex-based parsing.
54
+ - 🔬 **Surgical symbol extraction** — Extract a single function, class, or method from a file by name. Use `className` to scope disambiguation (e.g., getting multiple `build()` methods in Dart).
55
+ - 💥 **Impact analysis** — Discover all files that depend on a given file before refactoring. Supports ES imports, CommonJS `require()`, Python imports, PHP `use`/`require_once`/`include`, and Dart imports.
56
+ - 📁 **Smart file walking** — Respects `.gitignore` and `.repomixignore` rules. Automatically excludes `node_modules`, `dist`, `vendor`, `.git`, etc.
57
+ - 📄 **Multi-format output** — XML (optimized for LLM consumption) or Markdown (human-readable).
58
+
59
+ ### Write
60
+ - ✏️ **Surgical symbol replacement** — Replace a function, method, or class body without touching the rest of the file. Narrow down the target using the `className` parameter.
61
+ - ➕ **Precise code insertion** — Insert new code before/after a symbol, or inside a class at the start/end.
62
+ - 🔄 **Repository-wide rename** — Rename a symbol in its definition AND all files that import it, atomically.
63
+ - 🗑️ **Safe symbol removal** — Delete code with automatic dependency checking to prevent breakage.
64
+ - 🔍 **Mandatory dry-run flow** — Write tools return a preview diff and a `confirmationToken` by default. Changes are only applied after explicit confirmation.
65
+ - 💾 **Robust rolling backups** — Automatically keeps the last 5 versions of modified files in a hidden `.mcp-backups/` directory.
66
+ - ⏪ **Surgical rollback** — Revert files to any of the 5 previous states using the new `rollback_file` tool.
67
+ - 🤖 **Fuzzy symbol matching** — When a symbol is not found, the server provides structured suggestions based on Levenshtein distance.
68
+ - 🔐 **Private symbol support** — Full support for `_` and `__` prefixed symbols in Dart and Python.
69
+
70
+ ## Supported Languages
71
+
72
+ | Language | Read (Compress + Extract) | Write (Replace + Insert + Rename + Remove) | Import Analysis |
73
+ |----------|---------------------------|---------------------------------------------|------------------|
74
+ | TypeScript / JavaScript | ✅ AST (Tree-sitter WASM) | ✅ AST (Tree-sitter WASM) | ✅ |
75
+ | PHP | ✅ AST (Tree-sitter WASM) | ✅ AST + line-splice | ✅ |
76
+ | Dart | ✅ AST (Tree-sitter WASM) | ✅ AST + line-splice | ✅ |
77
+ | Python | ✅ AST (Tree-sitter WASM) | ✅ Indentation-aware | ✅ |
78
+ | Others (JSON, YAML, CSS, etc.) | Passthrough / truncation | — | — |
79
+
80
+ ## ⚠️ Known Limitations
81
+
82
+ ### `rename_symbol` Tool
83
+
84
+ **Dart and Python**: Cross-file rename is **NOT supported** for `.dart`, `.py`, and `.pyi` files.
85
+
86
+ - **Reason**: No AST parser available for safe cross-file refactoring
87
+ - **Risk**: Regex-based rename may corrupt strings, comments, or unrelated code
88
+ - **Recommendation**: Use IDE refactoring tools instead:
89
+ - **Dart**: VS Code with Dart extension (F2 key) or IntelliJ IDEA
90
+ - **Python**: PyCharm, VS Code with Pylance (F2 key)
91
+ - **Alternative**: Use `write_file_surgical` to rename within a single file
92
+
93
+ **TypeScript, JavaScript, PHP**: Fully supported with AST-aware renaming ✅
94
+
95
+ ### `get_semantic_repo_map` Tool
96
+
97
+ - **Max files**: Limited to 500 files to prevent timeouts
98
+ - **Performance**: Synchronous I/O may take 10-30 seconds on large repositories
99
+ - **Recommendation**: Use `@folder` syntax to target specific directories
100
+
101
+ ### General
102
+
103
+ - **Validation**: No automatic syntax checking after edits. Always review diffs carefully before confirming.
104
+ - **Backups**: 5-version rolling backup system. Use `rollback_file` if something goes wrong.
105
+ - **Large files**: Files >10MB are skipped for safety.
106
+
107
+ ## Installation
108
+
109
+ ```bash
110
+ # Clone the repository
111
+ git clone https://github.com/YOUR_USERNAME/mcp-code-context.git
112
+ cd mcp-code-context
113
+
114
+ # Install dependencies (no build tools required!)
115
+ npm install
116
+
117
+ # Build
118
+ npm run build
119
+ ```
120
+
121
+ **Note**: Unlike v2.x, this version uses **web-tree-sitter (WASM)** instead of native bindings. No Visual Studio, Python, or node-gyp required!
122
+
123
+ ## Configuration
124
+
125
+ ### Claude Desktop
126
+
127
+ Add to your `claude_desktop_config.json`:
128
+
129
+ ```json
130
+ {
131
+ "mcpServers": {
132
+ "mcp-code-context": {
133
+ "command": "node",
134
+ "args": ["/absolute/path/to/mcp-code-context/dist/index.js"]
135
+ }
136
+ }
137
+ }
138
+ ```
139
+
140
+ ### Cursor
141
+
142
+ Add to your Cursor MCP settings (`.cursor/mcp.json`):
143
+
144
+ ```json
145
+ {
146
+ "mcpServers": {
147
+ "mcp-code-context": {
148
+ "command": "node",
149
+ "args": ["/absolute/path/to/mcp-code-context/dist/index.js"]
150
+ }
151
+ }
152
+ }
153
+ ```
154
+
155
+ ### Windsurf
156
+
157
+ Add to your Windsurf MCP config:
158
+
159
+ ```json
160
+ {
161
+ "mcpServers": {
162
+ "mcp-code-context": {
163
+ "command": "node",
164
+ "args": ["/absolute/path/to/mcp-code-context/dist/index.js"]
165
+ }
166
+ }
167
+ }
168
+ ```
169
+
170
+ ### Amazon Q / Other MCP Clients
171
+
172
+ Any MCP-compatible client can use this server. The transport is **stdio** (JSON-RPC over stdin/stdout). Point your client to `node dist/index.js`.
173
+
174
+ ## Tools
175
+
176
+ ### Read Tools
177
+
178
+ #### 1. `get_semantic_repo_map`
179
+ Generate a compressed architectural overview of an entire repository.
180
+ - `directoryPath` (required) — Path to the repo root
181
+ - `format` (optional) — `"xml"` (default) or `"markdown"`
182
+
183
+ #### 2. `read_file_surgical`
184
+ Read a file, or extract only a specific named symbol. Returns structured suggestions if the symbol is missing.
185
+ - `filePath` (required) — Path to the source file
186
+ - `symbolName` (optional) — Name of a function, class, method, or type
187
+ - `className` (optional) — Scope the symbol to a specific class (to avoid duplicates)
188
+
189
+ #### 3. `analyze_impact`
190
+ Find all files that depend on a given file.
191
+ - `filePath` (required) — Path to the file being modified
192
+ - `rootDir` (optional) — Repository root (auto-detected)
193
+
194
+ #### 4. `read_file_lines`
195
+ Read specific line ranges from a file without loading the entire content. More efficient than `read_file_surgical` for small fragments.
196
+ - `filePath` (required) — Path to the source file
197
+ - `startLine` (optional) — Starting line number (1-indexed)
198
+ - `endLine` (optional) — Ending line number (1-indexed)
199
+ - `aroundPattern` (optional) — Search pattern to find and return surrounding lines
200
+ - `contextLines` (optional) — Number of lines before/after pattern (default: 5)
201
+
202
+ #### 5. `search_code_pattern`
203
+ Search for code patterns across multiple files with context. Respects `.gitignore` rules.
204
+ - `rootDir` (required) — Repository root directory
205
+ - `pattern` (required) — Regular expression pattern to search
206
+ - `fileExtensions` (optional) — Array of extensions to search (e.g., `[".ts", ".dart"]`)
207
+ - `excludeDirs` (optional) — Directories to exclude (default: `["node_modules", "dist", "build"]`)
208
+ - `showContext` (optional) — Include surrounding lines (default: true)
209
+ - `contextLines` (optional) — Number of context lines (default: 3)
210
+ - `maxResults` (optional) — Maximum matches to return (default: 50)
211
+
212
+ #### 6. `rollback_file`
213
+ Surgically restore a file to a previous state from the automated backup system.
214
+ - `filePath` (required) — Path to the file to restore
215
+ - `steps` (optional) — Number of versions to go back (1-5, default: 1)
216
+
217
+ #### 7. `clean_backups`
218
+ Remove all backup files for a project to keep the working directory clean.
219
+ - `projectRoot` (required) — Absolute path to the project root
220
+
221
+ **Note:** Backups are stored centrally at `[project-root]/.mcp-backups/` to keep your project organized.
222
+
223
+ ### Write Tools (Phase 1: Preview)
224
+
225
+ All write tools follow a **Two-Phase Workflow**:
226
+ 1. **Call without token**: Returns a unified `diff` and a `confirmationToken`.
227
+ 2. **Call with token**: Set `confirm: true` and provide the token to apply the changes.
228
+
229
+ #### 7. `write_file_surgical`
230
+ Replace the full source code of a named symbol in a file.
231
+ - `filePath` (required) — Path to the file
232
+ - `symbolName` (required) — Symbol to replace
233
+ - `newContent` (required) — Replacement code (signature + body)
234
+ - `confirmationToken` (optional) — Token from Phase 1 to apply changes
235
+ - `confirm` (optional) — Set to `true` to apply
236
+ - `className` (optional) — Scope the symbol to a specific class
237
+
238
+ #### 8. `insert_symbol`
239
+ Insert new code at a precise location relative to an existing symbol.
240
+ - `filePath` (required) — Path to the file
241
+ - `code` (required) — Code to insert
242
+ - `anchorSymbol` (optional) — Symbol to position relative to
243
+ - `position` (optional) — `"before"`, `"after"`, `"inside_start"`, `"inside_end"`
244
+ - `className` (optional) — Scope the anchor to a specific class
245
+ - `confirmationToken`, `confirm` (optional)
246
+
247
+ #### 9. `rename_symbol`
248
+ Rename a symbol across the entire repository (definition + all usages).
249
+ - `filePath` (required) — File where the symbol is defined
250
+ - `oldName` (required) — Current name
251
+ - `newName` (required) — New name
252
+ - `rootDir` (optional) — Repository root
253
+ - `confirmationToken`, `confirm` (optional)
254
+
255
+ #### 10. `remove_symbol`
256
+ Safely remove a symbol from a file with dependency checking.
257
+ - `filePath` (required) — Path to the file
258
+ - `symbolName` (required) — Symbol to remove
259
+ - `className` (optional) — Scope the symbol to a specific class
260
+ - `force` (optional) — Skip dependency check
261
+ - `confirmationToken`, `confirm` (optional)
262
+
263
+ ## Recommended Workflow
264
+
265
+ 1. **Understand** → `get_semantic_repo_map` to see the architecture
266
+ 2. **Read** → `read_file_surgical` with symbol name for specific implementations
267
+ 3. **Assess** → `analyze_impact` before modifying shared files
268
+ 4. **Edit (Preview)** → Call write tools to generate a `diff` and `confirmationToken`
269
+ 5. **Confim** → Call the same write tool with the token and `confirm: true` to apply
270
+ 6. **Recovery** → Use `rollback_file` if something goes wrong after confirmation
271
+
272
+ ## Development
273
+
274
+ ```bash
275
+ # Build
276
+ npm run build
277
+
278
+ # Run read tests (compression + extraction)
279
+ npm run build && node dist/tests/test-dart.js && node dist/tests/test-php.js
280
+
281
+ # Run write tests (replace, insert, rename, remove)
282
+ npm run build && node dist/tests/writers/test-write-smoke.js
283
+
284
+ # Run all tests
285
+ npm run build && node dist/tests/test-dart.js && node dist/tests/test-php.js && node dist/tests/writers/test-write-smoke.js
286
+
287
+ # Development (build + start)
288
+ npm run dev
289
+ ```
290
+
291
+ ## Technical Details
292
+
293
+ - **Transport:** stdio (JSON-RPC over stdin/stdout)
294
+ - **Runtime:** Node.js >= 18
295
+ - **Protocol:** [Model Context Protocol](https://modelcontextprotocol.io/)
296
+ - **AST Engines:** web-tree-sitter@0.25.1 (WASM) for TypeScript/JS/Python/PHP/Dart
297
+ - **Language Grammars:** tree-sitter-wasms@0.1.13 (ABI v15)
298
+ - **Ignore Engine:** `ignore` npm package (full .gitignore spec support)
299
+ - **Safety Features:** Mandatory two-phase confirmation, rolling 5-version backups, fuzzy matching, dependency checking, surgical restoration.
300
+ - **Portability:** 100% WASM - no native dependencies, works on all platforms
301
+
302
+ ## License
303
+
304
+ [MIT](LICENSE)