mcp-code-context 2.1.3 → 2.3.1
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.
- package/.io/RULES.md +12 -0
- package/CHANGELOG.md +118 -58
- package/CONTRIBUTING.md +243 -0
- package/README.md +58 -7
- package/RELEASE_NOTES_v2.2.0.md +286 -0
- package/RELEASE_NOTES_v2.3.0.md +228 -0
- package/RELEASE_NOTES_v2.3.1.md +110 -0
- package/dist/src/ast/writers/safeRename.d.ts +14 -0
- package/dist/src/ast/writers/safeRename.js +258 -0
- package/dist/src/ast/writers/safeRename.js.map +1 -0
- package/dist/src/ast/writers/symbolWriter.d.ts +2 -0
- package/dist/src/ast/writers/symbolWriter.js +9 -14
- package/dist/src/ast/writers/symbolWriter.js.map +1 -1
- package/dist/src/cache/astCache.d.ts +33 -0
- package/dist/src/cache/astCache.js +113 -0
- package/dist/src/cache/astCache.js.map +1 -0
- package/dist/src/handlers/readHandlers.d.ts +70 -0
- package/dist/src/handlers/readHandlers.js +431 -0
- package/dist/src/handlers/readHandlers.js.map +1 -0
- package/dist/src/handlers/utilHandlers.d.ts +31 -0
- package/dist/src/handlers/utilHandlers.js +69 -0
- package/dist/src/handlers/utilHandlers.js.map +1 -0
- package/dist/src/handlers/writeHandlers.d.ts +57 -0
- package/dist/src/handlers/writeHandlers.js +475 -0
- package/dist/src/handlers/writeHandlers.js.map +1 -0
- package/dist/src/index.d.ts +6 -107
- package/dist/src/index.js +147 -965
- package/dist/src/index.js.map +1 -1
- package/dist/src/tools/readFileLines.d.ts +26 -0
- package/dist/src/tools/readFileLines.js +77 -0
- package/dist/src/tools/readFileLines.js.map +1 -0
- package/dist/src/tools/searchCodePattern.d.ts +32 -0
- package/dist/src/tools/searchCodePattern.js +80 -0
- package/dist/src/tools/searchCodePattern.js.map +1 -0
- package/dist/src/utils/arrayUtils.d.ts +12 -0
- package/dist/src/utils/arrayUtils.js +27 -0
- package/dist/src/utils/arrayUtils.js.map +1 -0
- package/dist/src/utils/backupManager.d.ts +13 -6
- package/dist/src/utils/backupManager.js +73 -8
- package/dist/src/utils/backupManager.js.map +1 -1
- package/dist/src/utils/constants.d.ts +46 -0
- package/dist/src/utils/constants.js +112 -0
- package/dist/src/utils/constants.js.map +1 -0
- package/dist/src/utils/ignoreManager.d.ts +8 -0
- package/dist/src/utils/ignoreManager.js +49 -0
- package/dist/src/utils/ignoreManager.js.map +1 -1
- package/dist/src/utils/normalization.d.ts +49 -0
- package/dist/src/utils/normalization.js +173 -0
- package/dist/src/utils/normalization.js.map +1 -0
- package/dist/src/utils/projectRoot.d.ts +17 -0
- package/dist/src/utils/projectRoot.js +42 -0
- package/dist/src/utils/projectRoot.js.map +1 -0
- package/dist/src/utils/secureValidation.d.ts +15 -0
- package/dist/src/utils/secureValidation.js +41 -0
- package/dist/src/utils/secureValidation.js.map +1 -0
- package/dist/src/utils/transactionManager.d.ts +45 -0
- package/dist/src/utils/transactionManager.js +102 -0
- package/dist/src/utils/transactionManager.js.map +1 -0
- package/dist/src/utils/validation.d.ts +87 -0
- package/dist/src/utils/validation.js +231 -0
- package/dist/src/utils/validation.js.map +1 -0
- package/docs/ARCHITECTURE.md +300 -0
- package/docs/architecture/ADR-001-modular-handlers.md +119 -0
- package/llms.txt +62 -7
- package/package.json +5 -5
- package/AGENT_INSTRUCTIONS.md +0 -135
- package/DEPLOYMENT_INSTRUCTIONS.md +0 -110
- package/dist/src/tests/test-confirmation-flow.d.ts +0 -1
- package/dist/src/tests/test-confirmation-flow.js +0 -43
- package/dist/src/tests/test-confirmation-flow.js.map +0 -1
- package/dist/tests/debug-replace.d.ts +0 -1
- package/dist/tests/debug-replace.js +0 -17
- package/dist/tests/debug-replace.js.map +0 -1
- package/dist/tests/test-confirmation-flow.d.ts +0 -1
- package/dist/tests/test-confirmation-flow.js +0 -43
- package/dist/tests/test-confirmation-flow.js.map +0 -1
- package/dist/tests/test-dart.d.ts +0 -1
- package/dist/tests/test-dart.js +0 -242
- package/dist/tests/test-dart.js.map +0 -1
- package/dist/tests/test-php-ast.d.ts +0 -1
- package/dist/tests/test-php-ast.js +0 -152
- package/dist/tests/test-php-ast.js.map +0 -1
- package/dist/tests/test-php.d.ts +0 -1
- package/dist/tests/test-php.js +0 -234
- package/dist/tests/test-php.js.map +0 -1
- package/dist/tests/test-smoke.d.ts +0 -5
- package/dist/tests/test-smoke.js +0 -83
- package/dist/tests/test-smoke.js.map +0 -1
- package/dist/tests/writers/test-crlf-handling.d.ts +0 -4
- package/dist/tests/writers/test-crlf-handling.js +0 -73
- package/dist/tests/writers/test-crlf-handling.js.map +0 -1
- package/dist/tests/writers/test-write-smoke.d.ts +0 -9
- package/dist/tests/writers/test-write-smoke.js +0 -346
- package/dist/tests/writers/test-write-smoke.js.map +0 -1
- package/src/ast/dartCompressor.ts +0 -1013
- package/src/ast/phpCompressor.ts +0 -814
- package/src/ast/semanticCompressor.ts +0 -808
- package/src/ast/writers/dartWriter.ts +0 -648
- package/src/ast/writers/phpWriter.ts +0 -508
- package/src/ast/writers/pyWriter.ts +0 -446
- package/src/ast/writers/symbolWriter.ts +0 -277
- package/src/ast/writers/tsWriter.ts +0 -418
- package/src/index.ts +0 -1553
- package/src/utils/backupManager.ts +0 -66
- package/src/utils/confirmationCache.ts +0 -61
- package/src/utils/diffEngine.ts +0 -223
- package/src/utils/fuzzyMatch.ts +0 -86
- package/src/utils/ignoreManager.ts +0 -144
- package/tests/test-confirmation-flow.ts +0 -49
- package/tests/test-dart.d.ts +0 -1
- package/tests/test-dart.js +0 -209
- package/tests/test-dart.js.map +0 -1
- package/tests/test-dart.ts +0 -265
- package/tests/test-php-ast.d.ts +0 -1
- package/tests/test-php-ast.js +0 -152
- package/tests/test-php-ast.js.map +0 -1
- package/tests/test-php-ast.ts +0 -155
- package/tests/test-php.d.ts +0 -1
- package/tests/test-php.js +0 -234
- package/tests/test-php.js.map +0 -1
- package/tests/test-php.ts +0 -261
- package/tests/test-smoke.d.ts +0 -5
- package/tests/test-smoke.js +0 -83
- package/tests/test-smoke.js.map +0 -1
- package/tests/test-smoke.ts +0 -93
- package/tests/writers/test-crlf-handling.ts +0 -100
- package/tests/writers/test-write-smoke.ts +0 -568
- package/tsconfig.json +0 -19
|
@@ -0,0 +1,300 @@
|
|
|
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)
|
|
@@ -0,0 +1,119 @@
|
|
|
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/)
|
package/llms.txt
CHANGED
|
@@ -57,24 +57,79 @@ This MCP server solves two problems:
|
|
|
57
57
|
|
|
58
58
|
**Best used when:** You're about to modify a file and need to understand the blast radius.
|
|
59
59
|
|
|
60
|
-
#### 4. `
|
|
60
|
+
#### 4. `read_file_lines`
|
|
61
|
+
**Purpose:** Read specific line ranges from a file without loading the entire content.
|
|
62
|
+
|
|
63
|
+
**Input:**
|
|
64
|
+
- `filePath` (required): Absolute path to the source file.
|
|
65
|
+
- `startLine` (optional): Starting line number (1-indexed).
|
|
66
|
+
- `endLine` (optional): Ending line number (1-indexed).
|
|
67
|
+
- `aroundPattern` (optional): Search pattern to find and return surrounding lines.
|
|
68
|
+
- `contextLines` (optional): Number of lines before/after pattern (default: 5).
|
|
69
|
+
|
|
70
|
+
**What it does:**
|
|
71
|
+
- **Mode 1 (Exact Range)**: When `startLine` and `endLine` are provided, returns only those lines.
|
|
72
|
+
- **Mode 2 (Pattern Search)**: When `aroundPattern` is provided, finds the first occurrence and returns surrounding lines with context.
|
|
73
|
+
- Validates line ranges and provides clear error messages.
|
|
74
|
+
- 90% token savings compared to reading full files for small fragments.
|
|
75
|
+
|
|
76
|
+
**Best used when:**
|
|
77
|
+
- Debugging specific code blocks when you know the line numbers
|
|
78
|
+
- Viewing context around error messages or logs
|
|
79
|
+
- Extracting small fragments without AST parsing overhead
|
|
80
|
+
|
|
81
|
+
**Example use cases:**
|
|
82
|
+
- "Show me lines 120-135 of coordinator.dart"
|
|
83
|
+
- "Show me the code around 'HeroBannerWidget' in hero_banner_widget.dart"
|
|
84
|
+
|
|
85
|
+
#### 5. `search_code_pattern`
|
|
86
|
+
**Purpose:** Search for code patterns across multiple files in a repository.
|
|
87
|
+
|
|
88
|
+
**Input:**
|
|
89
|
+
- `rootDir` (required): Absolute path to the repository root.
|
|
90
|
+
- `pattern` (required): Regular expression pattern to search for.
|
|
91
|
+
- `fileExtensions` (optional): Array of extensions to search (e.g., `[".ts", ".dart"]`). Defaults to common code extensions.
|
|
92
|
+
- `excludeDirs` (optional): Directories to exclude (default: `["node_modules", "dist", "build"]`).
|
|
93
|
+
- `showContext` (optional): Include surrounding lines (default: true).
|
|
94
|
+
- `contextLines` (optional): Number of context lines before/after (default: 3).
|
|
95
|
+
- `maxResults` (optional): Maximum matches to return (default: 50).
|
|
96
|
+
|
|
97
|
+
**What it does:**
|
|
98
|
+
- Walks the repository respecting `.gitignore` rules.
|
|
99
|
+
- Searches for regex patterns across all matching files.
|
|
100
|
+
- Returns structured results with file paths, line numbers, and optional context.
|
|
101
|
+
- Highlights the matching line with a `>` marker in context output.
|
|
102
|
+
- 85% more efficient than manual grep + analysis workflows.
|
|
103
|
+
|
|
104
|
+
**Best used when:**
|
|
105
|
+
- Finding all usages of a function or variable
|
|
106
|
+
- Locating specific log statements or debug markers
|
|
107
|
+
- Discovering patterns across the codebase
|
|
108
|
+
- Understanding how a feature is implemented across files
|
|
109
|
+
|
|
110
|
+
**Example use cases:**
|
|
111
|
+
- "Find all occurrences of 'widget.height' in the Flutter project"
|
|
112
|
+
- "Search for '🎯 \[BUILDER\]' logs in .dart files"
|
|
113
|
+
- "Find all files that call 'HeroBannerWidget'"
|
|
114
|
+
|
|
115
|
+
#### 6. `rollback_file`
|
|
61
116
|
**Purpose:** Restores a file to a previous state from the automated backup system.
|
|
62
117
|
**Input:**
|
|
63
118
|
- `filePath` (required): Absolute path.
|
|
64
119
|
- `steps` (optional): Number of versions to go back (1-5, default: 1).
|
|
65
120
|
**What it does:** Reverts the file to the state it was in before the last 1-5 modifications.
|
|
66
121
|
|
|
67
|
-
### Write Tools (v2.
|
|
122
|
+
### Write Tools (v2.2.0 Mandatory Workflow)
|
|
68
123
|
|
|
69
124
|
All write tools follow a **Two-Phase Workflow**:
|
|
70
125
|
1. **Phase 1 (Preview)**: Call without a token to get a unified `diff` and a `confirmationToken`.
|
|
71
126
|
2. **Phase 2 (Apply)**: Call with `confirmationToken` and `confirm: true` to apply the changes.
|
|
72
127
|
|
|
73
|
-
**Important Notes (v2.
|
|
128
|
+
**Important Notes (v2.2.0):**
|
|
74
129
|
- ✅ **Windows CRLF Support**: Fully resolved. All operations work correctly on Windows files with CRLF line endings.
|
|
75
130
|
- ✅ **Parameter Brace Handling**: Methods with braces in parameters (e.g., `{int? cart}`) are now handled correctly.
|
|
76
131
|
|
|
77
|
-
####
|
|
132
|
+
#### 7. `write_file_surgical`
|
|
78
133
|
**Purpose:** Replace the full source code of a named symbol in a file.
|
|
79
134
|
|
|
80
135
|
**Input:**
|
|
@@ -93,7 +148,7 @@ All write tools follow a **Two-Phase Workflow**:
|
|
|
93
148
|
|
|
94
149
|
**Best used when:** You need to modify a specific function or method without touching the rest of the file.
|
|
95
150
|
|
|
96
|
-
####
|
|
151
|
+
#### 8. `insert_symbol`
|
|
97
152
|
**Purpose:** Insert new code at a precise location relative to an existing symbol.
|
|
98
153
|
|
|
99
154
|
**Input:**
|
|
@@ -110,7 +165,7 @@ All write tools follow a **Two-Phase Workflow**:
|
|
|
110
165
|
|
|
111
166
|
**Best used when:** Adding new methods to a class or new functions to a module.
|
|
112
167
|
|
|
113
|
-
####
|
|
168
|
+
#### 9. `rename_symbol`
|
|
114
169
|
**Purpose:** Rename a symbol across an entire repository.
|
|
115
170
|
|
|
116
171
|
**Input:**
|
|
@@ -129,7 +184,7 @@ All write tools follow a **Two-Phase Workflow**:
|
|
|
129
184
|
|
|
130
185
|
**Best used when:** Refactoring a class, function, or method name that is used across multiple files.
|
|
131
186
|
|
|
132
|
-
####
|
|
187
|
+
#### 10. `remove_symbol`
|
|
133
188
|
**Purpose:** Safely remove a symbol from a file.
|
|
134
189
|
|
|
135
190
|
**Input:**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-code-context",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"description": "Powerful MCP server for LLM-ready semantic code context and surgical AST-aware editing. Supports TypeScript, JavaScript, PHP, Dart, and Python.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"build": "tsc",
|
|
12
12
|
"start": "node dist/src/index.js",
|
|
13
13
|
"dev": "tsc && node dist/src/index.js",
|
|
14
|
-
"test": "npm run build && node dist/tests/test-dart.js && node dist/tests/test-php.js && node dist/tests/writers/test-write-smoke.js",
|
|
14
|
+
"test": "npm run build && node dist/tests/test-dart.js && node dist/tests/test-php.js && node dist/tests/writers/test-write-smoke.js && node dist/tests/test-new-tools.js && node dist/tests/test-backup-system.js && node dist/tests/test-rename-limitations.js",
|
|
15
15
|
"clean": "rimraf dist",
|
|
16
16
|
"rebuild": "npm run clean && npm run build"
|
|
17
17
|
},
|
|
@@ -44,12 +44,12 @@
|
|
|
44
44
|
"@modelcontextprotocol/sdk": "^1.12.0",
|
|
45
45
|
"ignore": "^6.0.2",
|
|
46
46
|
"php-parser": "^3.5.1",
|
|
47
|
-
"ts-morph": "^24.0.0"
|
|
47
|
+
"ts-morph": "^24.0.0",
|
|
48
|
+
"typescript": "^5.7.0"
|
|
48
49
|
},
|
|
49
50
|
"devDependencies": {
|
|
50
51
|
"@types/node": "^22.10.0",
|
|
51
|
-
"rimraf": "^6.0.1"
|
|
52
|
-
"typescript": "^5.7.0"
|
|
52
|
+
"rimraf": "^6.0.1"
|
|
53
53
|
},
|
|
54
54
|
"engines": {
|
|
55
55
|
"node": ">=18.0.0"
|