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
@@ -1,300 +0,0 @@
1
- # Architecture Overview
2
-
3
- ## System Design
4
-
5
- `mcp-code-context` is a Model Context Protocol (MCP) server that provides semantic code analysis and surgical editing capabilities. The architecture follows a layered design with clear separation of concerns.
6
-
7
- ```
8
- ┌─────────────────────────────────────────────────────────────┐
9
- │ MCP Client │
10
- │ (Claude, Cursor, Amazon Q, etc.) │
11
- └────────────────────────┬────────────────────────────────────┘
12
- │ JSON-RPC over stdio
13
- ▼
14
- ┌─────────────────────────────────────────────────────────────┐
15
- │ MCP Server (index.ts) │
16
- │ ┌──────────────────────────────────────────────────────┐ │
17
- │ │ Tool Registry & Dispatcher │ │
18
- │ └──────────────────────────────────────────────────────┘ │
19
- └────────────────────────┬────────────────────────────────────┘
20
- │
21
- ┌───────────────┼───────────────┐
22
- ▼ ▼ ▼
23
- ┌────────────────┐ ┌────────────┐ ┌────────────┐
24
- │ Read Handlers │ │ Write │ │ Util │
25
- │ │ │ Handlers │ │ Handlers │
26
- │ • repo_map │ │ • surgical │ │ • rollback │
27
- │ • read_file │ │ • insert │ │ • cleanup │
28
- │ • analyze │ │ • rename │ └────────────┘
29
- │ • search │ │ • remove │
30
- └────────┬───────┘ └─────┬──────┘
31
- │ │
32
- └───────┬───────┘
33
- ▼
34
- ┌─────────────────────────────────────────────────────────────┐
35
- │ Core Services │
36
- │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
37
- │ │ AST │ │ Cache │ │Security │ │ Backup │ │
38
- │ │ Parsers │ │ LRU │ │Validation│ │ Manager │ │
39
- │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
40
- └─────────────────────────────────────────────────────────────┘
41
- │ │ │ │
42
- └───────────────┴───────────────┴───────────────┘
43
- ▼
44
- ┌────────────────────────┐
45
- │ File System │
46
- └────────────────────────┘
47
- ```
48
-
49
- ## Component Responsibilities
50
-
51
- ### 1. MCP Server Layer (`index.ts`)
52
-
53
- **Responsibility**: Protocol handling and request routing
54
-
55
- - Initializes MCP server with stdio transport
56
- - Registers available tools
57
- - Routes requests to appropriate handlers
58
- - Minimal business logic (~150 lines)
59
-
60
- ### 2. Handler Layer (`src/handlers/`)
61
-
62
- **Responsibility**: Tool implementation and orchestration
63
-
64
- #### Read Handlers (`readHandlers.ts`)
65
- - `get_semantic_repo_map`: Generate compressed repo overview
66
- - `read_file_surgical`: Extract specific symbols or full files
67
- - `analyze_impact`: Find file dependencies
68
- - `read_file_lines`: Read specific line ranges
69
- - `search_code_pattern`: Search patterns across repo
70
-
71
- #### Write Handlers (`writeHandlers.ts`)
72
- - `write_file_surgical`: Replace symbol code
73
- - `insert_symbol`: Insert code at precise locations
74
- - `rename_symbol`: Rename across repository
75
- - `remove_symbol`: Safe symbol deletion
76
-
77
- #### Util Handlers (`utilHandlers.ts`)
78
- - `rollback_file`: Restore from backup
79
- - `clean_backups`: Remove backup files
80
-
81
- ### 3. Core Services Layer
82
-
83
- #### AST Parsers (`src/ast/`)
84
- - **TypeScript/JavaScript**: Uses `ts-morph` (real AST)
85
- - **PHP**: Uses `php-parser` (real AST)
86
- - **Dart**: Brace-counting + regex (pragmatic)
87
- - **Python**: Indentation-aware regex (pragmatic)
88
-
89
- **Design Decision**: Mix of real parsers and pragmatic approaches balances robustness with simplicity.
90
-
91
- #### Cache (`src/cache/astCache.ts`)
92
- - LRU cache for parsed ASTs
93
- - Automatic invalidation via file mtime
94
- - 90% performance improvement for repeated operations
95
- - Memory-bounded (configurable max size)
96
-
97
- #### Security (`src/utils/validation.ts`)
98
- - Path traversal prevention
99
- - ReDoS protection
100
- - Input sanitization
101
- - File size limits
102
-
103
- #### Backup Manager (`src/utils/backupManager.ts`)
104
- - Rolling 5-version backup system
105
- - Centralized `.mcp-backups/` directory
106
- - Automatic cleanup
107
- - Surgical rollback capability
108
-
109
- ### 4. Utility Layer (`src/utils/`)
110
-
111
- - **constants.ts**: Centralized configuration
112
- - **normalization.ts**: CRLF/indentation handling
113
- - **validation.ts**: Security and input validation
114
- - **fuzzyMatch.ts**: Symbol name suggestions
115
- - **diffEngine.ts**: Unified diff generation
116
- - **confirmationCache.ts**: Two-phase write workflow
117
-
118
- ## Data Flow
119
-
120
- ### Read Operation Example: `read_file_surgical`
121
-
122
- ```
123
- 1. Client Request
124
- ↓
125
- 2. index.ts routes to readHandlers.handleReadFileSurgical()
126
- ↓
127
- 3. Validate file path (validation.ts)
128
- ↓
129
- 4. Check cache (astCache.ts)
130
- ├─ Cache hit → Return cached result
131
- └─ Cache miss → Continue
132
- ↓
133
- 5. Read file from disk
134
- ↓
135
- 6. Parse with appropriate AST parser
136
- ↓
137
- 7. Extract symbol
138
- ↓
139
- 8. Cache result
140
- ↓
141
- 9. Return to client
142
- ```
143
-
144
- ### Write Operation Example: `write_file_surgical`
145
-
146
- ```
147
- PHASE 1 (Dry-Run):
148
- 1. Client Request (no confirmation token)
149
- ↓
150
- 2. index.ts routes to writeHandlers.handleWriteFileSurgical()
151
- ↓
152
- 3. Validate inputs (validation.ts)
153
- ↓
154
- 4. Read file and parse
155
- ↓
156
- 5. Perform replacement in memory
157
- ↓
158
- 6. Generate diff (diffEngine.ts)
159
- ↓
160
- 7. Store in confirmation cache
161
- ↓
162
- 8. Return diff + confirmation token
163
-
164
- PHASE 2 (Apply):
165
- 1. Client Request (with confirmation token + confirm: true)
166
- ↓
167
- 2. Retrieve from confirmation cache
168
- ↓
169
- 3. Create backup (backupManager.ts)
170
- ↓
171
- 4. Write to disk
172
- ↓
173
- 5. Invalidate cache
174
- ↓
175
- 6. Return success
176
- ```
177
-
178
- ## Security Model
179
-
180
- ### Defense in Depth
181
-
182
- 1. **Input Validation**: All user inputs validated before processing
183
- 2. **Path Containment**: Paths must be within project root
184
- 3. **ReDoS Protection**: Regex patterns validated for safety
185
- 4. **File Size Limits**: Prevent memory exhaustion
186
- 5. **Two-Phase Writes**: Mandatory preview before applying changes
187
- 6. **Automatic Backups**: 5-version rolling backup system
188
-
189
- ### Threat Mitigation
190
-
191
- | Threat | Mitigation |
192
- |--------|-----------|
193
- | Path Traversal | `validateFilePath()` checks for `..` and dangerous patterns |
194
- | ReDoS | `validateRegexPattern()` detects catastrophic backtracking |
195
- | Memory Exhaustion | File size limits + LRU cache with max size |
196
- | Data Loss | Automatic backups + two-phase confirmation |
197
- | Injection | Input sanitization + AST-based parsing |
198
-
199
- ## Performance Optimizations
200
-
201
- ### 1. LRU Cache
202
- - **Impact**: 90% faster for repeated operations
203
- - **Trade-off**: Memory usage (bounded by max size)
204
-
205
- ### 2. Lazy Loading
206
- - AST parsers loaded on-demand
207
- - Reduces startup time
208
-
209
- ### 3. Incremental Processing
210
- - `get_semantic_repo_map` limited to 500 files
211
- - Prevents timeout on large repositories
212
-
213
- ### 4. Efficient Diff Generation
214
- - LCS algorithm for minimal diffs
215
- - Context lines configurable
216
-
217
- ## Extensibility
218
-
219
- ### Adding a New Language
220
-
221
- 1. Create parser in `src/ast/` (e.g., `rustCompressor.ts`)
222
- 2. Create writer in `src/ast/writers/` (e.g., `rustWriter.ts`)
223
- 3. Add extension to `WRITABLE_EXTENSIONS` in `constants.ts`
224
- 4. Update `symbolWriter.ts` to route to new writer
225
- 5. Add tests in `tests/`
226
-
227
- ### Adding a New Tool
228
-
229
- 1. Create handler function in appropriate handler file
230
- 2. Add tool definition to `TOOLS` array in `index.ts`
231
- 3. Add route case in request handler
232
- 4. Add tests in `tests/`
233
-
234
- ## Testing Strategy
235
-
236
- ### Unit Tests
237
- - Each handler tested in isolation
238
- - Mock file system operations
239
- - Test edge cases (missing files, invalid input, etc.)
240
-
241
- ### Integration Tests
242
- - Full workflow tests (read → write → verify)
243
- - Multi-file operations (rename across repo)
244
- - Cache invalidation
245
-
246
- ### Security Tests
247
- - Path traversal attempts
248
- - ReDoS patterns
249
- - Malformed input
250
-
251
- ### Performance Tests
252
- - Large file handling
253
- - Cache hit rate
254
- - Memory usage under load
255
-
256
- ## Deployment
257
-
258
- ### NPM Package
259
- ```bash
260
- npm install -g mcp-code-context
261
- ```
262
-
263
- ### MCP Client Configuration
264
- ```json
265
- {
266
- "mcpServers": {
267
- "code-context": {
268
- "command": "npx",
269
- "args": ["-y", "mcp-code-context"]
270
- }
271
- }
272
- }
273
- ```
274
-
275
- ## Monitoring & Observability
276
-
277
- ### Metrics (Future)
278
- - Tool invocation counts
279
- - Cache hit rates
280
- - Error rates by tool
281
- - Average response times
282
-
283
- ### Logging
284
- - Errors logged to stderr
285
- - MCP protocol messages on stdout
286
- - Structured logging for debugging
287
-
288
- ## Future Enhancements
289
-
290
- 1. **Async I/O**: Migrate to `fs.promises` for non-blocking operations
291
- 2. **Streaming**: Support large files via streaming
292
- 3. **Tree-sitter**: Unified parser for all languages
293
- 4. **LSP Integration**: Leverage Language Server Protocol for advanced features
294
- 5. **Telemetry**: Optional usage analytics for improvement
295
-
296
- ## References
297
-
298
- - [MCP Specification](https://modelcontextprotocol.io/)
299
- - [Clean Architecture](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html)
300
- - [SOLID Principles](https://en.wikipedia.org/wiki/SOLID)
@@ -1,119 +0,0 @@
1
- # ADR-001: Modular Handler Architecture
2
-
3
- **Status:** Accepted
4
- **Date:** 2026-04-23
5
- **Decision Makers:** Core Team
6
-
7
- ## Context
8
-
9
- The original `src/index.ts` file grew to 1100+ lines, violating the Single Responsibility Principle and making the codebase difficult to maintain. The file contained:
10
-
11
- - MCP server initialization
12
- - 11 tool handler implementations
13
- - Utility functions (formatting, import resolution, project root finding)
14
- - Type definitions
15
- - Constants
16
-
17
- This created several problems:
18
-
19
- 1. **High cognitive load**: Understanding any single tool required reading through 1100 lines
20
- 2. **Merge conflicts**: Multiple developers editing the same file
21
- 3. **Testing difficulty**: Unit testing individual handlers was cumbersome
22
- 4. **Violation of SRP**: One file had too many responsibilities
23
-
24
- ## Decision
25
-
26
- We will refactor `index.ts` into a modular architecture:
27
-
28
- ```
29
- src/
30
- ├─ handlers/
31
- │ ├─ readHandlers.ts (get_semantic_repo_map, read_file_surgical, etc.)
32
- │ ├─ writeHandlers.ts (write_file_surgical, insert_symbol, etc.)
33
- │ └─ utilHandlers.ts (rollback_file, clean_backups)
34
- ├─ utils/
35
- │ ├─ constants.ts (centralized configuration)
36
- │ ├─ normalization.ts (CRLF, indentation)
37
- │ └─ validation.ts (security, input validation)
38
- ├─ cache/
39
- │ └─ astCache.ts (LRU cache for performance)
40
- └─ index.ts (~150 lines, orchestration only)
41
- ```
42
-
43
- ### Key Principles
44
-
45
- 1. **Separation of Concerns**: Each handler file focuses on one category of operations
46
- 2. **DRY**: Shared logic extracted to utilities
47
- 3. **Security First**: Validation centralized in `validation.ts`
48
- 4. **Performance**: Cache layer added for repeated operations
49
- 5. **Testability**: Each module can be unit tested independently
50
-
51
- ## Consequences
52
-
53
- ### Positive
54
-
55
- - **Maintainability**: Each file is <500 lines, easy to understand
56
- - **Testability**: Handlers can be tested in isolation
57
- - **Performance**: Cache reduces redundant parsing by 90%
58
- - **Security**: Centralized validation prevents vulnerabilities
59
- - **Extensibility**: Adding new tools is straightforward
60
-
61
- ### Negative
62
-
63
- - **More files**: Developers need to navigate multiple files
64
- - **Import complexity**: More import statements
65
- - **Migration effort**: Existing code needs refactoring
66
-
67
- ### Mitigation
68
-
69
- - Clear naming conventions make navigation intuitive
70
- - IDE features (Go to Definition) handle imports
71
- - Incremental migration with continuous testing
72
-
73
- ## Alternatives Considered
74
-
75
- ### 1. Keep monolithic index.ts
76
-
77
- **Pros**: No refactoring needed
78
- **Cons**: Technical debt continues to grow
79
- **Rejected**: Unsustainable long-term
80
-
81
- ### 2. Microservices architecture
82
-
83
- **Pros**: Maximum separation
84
- **Cons**: Over-engineering for a single MCP server
85
- **Rejected**: Too complex for the problem
86
-
87
- ### 3. Class-based handlers
88
-
89
- **Pros**: OOP encapsulation
90
- **Cons**: Unnecessary abstraction for stateless handlers
91
- **Rejected**: Functional approach is simpler
92
-
93
- ## Implementation Plan
94
-
95
- 1. ✅ Create utility modules (constants, normalization, validation)
96
- 2. ✅ Create cache layer
97
- 3. ✅ Extract handlers to separate files
98
- 4. ⏳ Update index.ts to import and delegate
99
- 5. ⏳ Run full test suite to ensure no regressions
100
- 6. ⏳ Update documentation
101
-
102
- ## Metrics
103
-
104
- **Before:**
105
- - index.ts: 1100 lines
106
- - Cyclomatic complexity: High
107
- - Test coverage: 70%
108
-
109
- **After (Target):**
110
- - index.ts: ~150 lines
111
- - Handler files: <500 lines each
112
- - Cyclomatic complexity: Low-Medium
113
- - Test coverage: >85%
114
-
115
- ## References
116
-
117
- - [Single Responsibility Principle](https://en.wikipedia.org/wiki/Single-responsibility_principle)
118
- - [Clean Architecture](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html)
119
- - [MCP Protocol Specification](https://modelcontextprotocol.io/)