mcp-code-context 2.2.0 → 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 +88 -1
- package/README.md +27 -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 -143
- package/dist/src/index.js +77 -1091
- package/dist/src/index.js.map +1 -1
- 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/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 +12 -5
- package/dist/src/utils/projectRoot.js +22 -20
- package/dist/src/utils/projectRoot.js.map +1 -1
- 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/package.json +5 -5
- package/AGENT_INSTRUCTIONS.md +0 -135
- package/DEPLOYMENT_INSTRUCTIONS.md +0 -110
- package/MEJORAS_PROPUESTAS.md +0 -324
- 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-backup-system.d.ts +0 -7
- package/dist/tests/test-backup-system.js +0 -152
- package/dist/tests/test-backup-system.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-new-tools.d.ts +0 -4
- package/dist/tests/test-new-tools.js +0 -147
- package/dist/tests/test-new-tools.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 -1784
- package/src/tools/readFileLines.ts +0 -104
- package/src/tools/searchCodePattern.ts +0 -129
- package/src/utils/backupManager.ts +0 -141
- 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/src/utils/projectRoot.ts +0 -44
- package/tests/test-backup-system.ts +0 -174
- 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-new-tools.ts +0 -165
- 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-code-context",
|
|
3
|
-
"version": "2.
|
|
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 && node dist/tests/test-new-tools.js && node dist/tests/test-backup-system.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"
|
package/AGENT_INSTRUCTIONS.md
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
# Smart Code Analysis & Editing Tools
|
|
2
|
-
**Available tools for efficient code analysis, context management, and surgical code modification.**
|
|
3
|
-
|
|
4
|
-
## Read Tools
|
|
5
|
-
|
|
6
|
-
### 1. `get_semantic_repo_map` — Architectural Map
|
|
7
|
-
**When to use**: At the start of a session, before a major refactor, or when you need to understand a new codebase or directory structure.
|
|
8
|
-
**NEVER use**: For reading specific implementations — this tool extracts ONLY structural signatures (classes, functions), not the code bodies.
|
|
9
|
-
```ts
|
|
10
|
-
get_semantic_repo_map(directoryPath: "/path/to/project/src", format: "markdown")
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
### 2. `read_file_surgical` — Surgical Reading
|
|
14
|
-
**When to use**: When you need a specific method, class, or symbol. ALWAYS prefer this over reading the entire file (like `fsRead`) if the symbol name is known.
|
|
15
|
-
**NEVER use**: For reading complete files if you only need a single function or symbol.
|
|
16
|
-
```ts
|
|
17
|
-
read_file_surgical(filePath: "/path/to/file.ts", symbolName: "ClassName")
|
|
18
|
-
read_file_surgical(filePath: "/path/to/file.dart", symbolName: "WidgetName")
|
|
19
|
-
// Omitting symbolName will read the entire file
|
|
20
|
-
read_file_surgical(filePath: "/path/to/file.php")
|
|
21
|
-
```
|
|
22
|
-
- If the symbol is not found, the tool returns a **structured JSON error** with `available_symbols` and `suggestions` (e.g., if a `className` scope is missing but the symbol exists in a class).
|
|
23
|
-
- **Pro-tip**: Use the suggestions to fix your next tool call (e.g., adding the correct `className`).
|
|
24
|
-
|
|
25
|
-
### 3. `analyze_impact` — Blast Radius Detection
|
|
26
|
-
**When to use**: ALWAYS before modifying a file that might be imported or depended upon by other parts of the system.
|
|
27
|
-
**NEVER use**: Ignore or skip this step when refactoring shared services, utilities, or core components.
|
|
28
|
-
**Features**: Detects imports across ecosystems (e.g., `use Namespace\Class` in PHP, `import` in JS/TS/Dart/Python).
|
|
29
|
-
```ts
|
|
30
|
-
analyze_impact(filePath: "/path/to/project/src/services/MyService.ts")
|
|
31
|
-
analyze_impact(filePath: "/path/to/project/src/Services/GeoIPService.php")
|
|
32
|
-
analyze_impact(filePath: "/path/to/detodo24_mobile/lib/services/tracking_service.dart")
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
---
|
|
36
|
-
|
|
37
|
-
## Write Tools
|
|
38
|
-
|
|
39
|
-
### Mandatory Two-Phase Write Workflow (v2.1.1+)
|
|
40
|
-
|
|
41
|
-
All write tools (`write_file_surgical`, `insert_symbol`, `rename_symbol`, `remove_symbol`) now enforce a safety-first confirmation flow.
|
|
42
|
-
|
|
43
|
-
**Phase 1: Dry-Run / Preview**
|
|
44
|
-
Call the tool with your proposed changes. You will receive:
|
|
45
|
-
1. A unified `diff` of the proposed changes.
|
|
46
|
-
2. A `confirmationToken`.
|
|
47
|
-
|
|
48
|
-
**Phase 2: Confirmation**
|
|
49
|
-
To apply the changes, call the SAME tool again with:
|
|
50
|
-
1. `confirmationToken`: The token from Phase 1.
|
|
51
|
-
2. `confirm: true`.
|
|
52
|
-
|
|
53
|
-
---
|
|
54
|
-
|
|
55
|
-
### 4. `write_file_surgical` — Surgical Symbol Replacement
|
|
56
|
-
**When to use**: Replace a specific function, method, class, or interface.
|
|
57
|
-
**Phase 1 (Preview)**:
|
|
58
|
-
```ts
|
|
59
|
-
write_file_surgical(filePath: "/path/to/file.ts", symbolName: "processOrder", newContent: "...")
|
|
60
|
-
```
|
|
61
|
-
**Phase 2 (Apply)**:
|
|
62
|
-
```ts
|
|
63
|
-
write_file_surgical(filePath: "/path/to/file.ts", symbolName: "processOrder", confirmationToken: "TOKEN_FROM_P1", confirm: true)
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### 5. `insert_symbol` — Precise Code Insertion
|
|
67
|
-
**When to use**: Adding new members relative to an existing anchor symbol.
|
|
68
|
-
**Phase 1 (Preview)**:
|
|
69
|
-
```ts
|
|
70
|
-
insert_symbol(filePath: "/path/to/file.dart", code: "void clear() { ... }", anchorSymbol: "getUser", position: "after")
|
|
71
|
-
```
|
|
72
|
-
**Phase 2 (Apply)**:
|
|
73
|
-
```ts
|
|
74
|
-
insert_symbol(filePath: "/path/to/file.dart", code: "void clear() { ... }", confirmationToken: "TOKEN_FROM_P1", confirm: true)
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
### 6. `rename_symbol` — Repository-Wide Rename
|
|
78
|
-
**When to use**: Atomic identifier renaming across the entire project.
|
|
79
|
-
**Phase 1 (Preview)**:
|
|
80
|
-
```ts
|
|
81
|
-
rename_symbol(filePath: "/path/to/UserService.ts", oldName: "UserService", newName: "AccountService")
|
|
82
|
-
```
|
|
83
|
-
**Phase 2 (Apply)**:
|
|
84
|
-
```ts
|
|
85
|
-
rename_symbol(filePath: "/path/to/UserService.ts", oldName: "UserService", newName: "AccountService", confirmationToken: "TOKEN_FROM_P1", confirm: true)
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
### 7. `remove_symbol` — Safe Symbol Deletion
|
|
89
|
-
**When to use**: Removing code with dependency checking.
|
|
90
|
-
**Phase 1 (Preview)**:
|
|
91
|
-
```ts
|
|
92
|
-
remove_symbol(filePath: "/path/to/file.ts", symbolName: "deprecatedHelper")
|
|
93
|
-
```
|
|
94
|
-
**Phase 2 (Apply)**:
|
|
95
|
-
```ts
|
|
96
|
-
remove_symbol(filePath: "/path/to/file.ts", symbolName: "deprecatedHelper", confirmationToken: "TOKEN_FROM_P1", confirm: true)
|
|
97
|
-
```
|
|
98
|
-
- **Safety**: Safe removal check happens in Phase 1. If dependents exist, use `force: true` in your next Phase 1 call to override.
|
|
99
|
-
|
|
100
|
-
### 8. `rollback_file` — Surgical Restoration
|
|
101
|
-
**When to use**: If an applied change introduces an unexpected error or side effect.
|
|
102
|
-
**Safety**: The server automatically keeps the last 5 versions in `.mcp-backups/`.
|
|
103
|
-
```ts
|
|
104
|
-
rollback_file(filePath: "/path/to/file.ts", steps: 1)
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
### 7. `remove_symbol` — Safe Symbol Deletion
|
|
108
|
-
**When to use**: When removing a function, method, or class from a file.
|
|
109
|
-
**Safety**: By default, checks for dependents and **refuses** to remove if other files reference it.
|
|
110
|
-
```ts
|
|
111
|
-
// Safe removal (checks dependents first)
|
|
112
|
-
remove_symbol(filePath: "/path/to/file.ts", symbolName: "deprecatedHelper")
|
|
113
|
-
// Force removal (skip dependency check)
|
|
114
|
-
remove_symbol(filePath: "/path/to/file.php", symbolName: "oldMethod", force: true)
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
---
|
|
118
|
-
|
|
119
|
-
## Mandatory Workflow
|
|
120
|
-
1. **New Session / Unknown Context** → Use `get_semantic_repo_map` on the relevant directory to understand the structure.
|
|
121
|
-
2. **Read Code** → Use `read_file_surgical` with a `symbolName` whenever possible.
|
|
122
|
-
3. **Handle Missing Symbols** → If you get a JSON error, use the `suggestions` to refine your `symbolName` or `className`.
|
|
123
|
-
4. **Before Modifying** → Use `analyze_impact` if the file is a shared module or component.
|
|
124
|
-
5. **Modify Code (Phase 1)** → Use write tools to generate a `diff` and `confirmationToken`.
|
|
125
|
-
6. **Verify Diff** → Read the diff carefully. If incorrect, correct your prompt and start Phase 1 again.
|
|
126
|
-
7. **Apply (Phase 2)** → Call the same write tool with the token and `confirm: true`.
|
|
127
|
-
8. **Verify Result** → Ensure nothing broke. Use `rollback_file` if needed.
|
|
128
|
-
|
|
129
|
-
## Supported Languages (Read + Write)
|
|
130
|
-
| Language | Read | Write | Engine |
|
|
131
|
-
|----------|------|-------|--------|
|
|
132
|
-
| TypeScript / JavaScript | ✅ | ✅ | AST (ts-morph) |
|
|
133
|
-
| PHP | ✅ | ✅ | AST (php-parser) |
|
|
134
|
-
| Dart | ✅ | ✅ | Brace-counting + regex |
|
|
135
|
-
| Python | ✅ | ✅ | Indentation-based |
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
# Deployment Instructions for v2.1.3
|
|
2
|
-
|
|
3
|
-
## ✅ Completed Steps
|
|
4
|
-
1. ✅ Code fixes implemented and tested
|
|
5
|
-
2. ✅ Version bumped to 2.1.3 in package.json
|
|
6
|
-
3. ✅ CHANGELOG.md updated
|
|
7
|
-
4. ✅ TROUBLESHOOTING.md updated
|
|
8
|
-
5. ✅ llms.txt updated
|
|
9
|
-
6. ✅ Test suite created and passing (11/11 tests)
|
|
10
|
-
7. ✅ Git commit created with semantic message
|
|
11
|
-
8. ✅ Git tag v2.1.3 created with release notes
|
|
12
|
-
9. ✅ Pushed to GitHub (main branch + tag)
|
|
13
|
-
|
|
14
|
-
## 🚀 Next Steps (Manual)
|
|
15
|
-
|
|
16
|
-
### 1. Publish to npm
|
|
17
|
-
You need to be logged in to npm. Run:
|
|
18
|
-
|
|
19
|
-
```bash
|
|
20
|
-
cd C:\code\antigravity-mcp-context
|
|
21
|
-
|
|
22
|
-
# Login to npm (if not already)
|
|
23
|
-
npm login
|
|
24
|
-
|
|
25
|
-
# Publish the package
|
|
26
|
-
npm publish
|
|
27
|
-
|
|
28
|
-
# Verify publication
|
|
29
|
-
npm view mcp-code-context@2.1.3
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
### 2. Create GitHub Release
|
|
33
|
-
Go to: https://github.com/achatainga/mcp-code-context/releases/new
|
|
34
|
-
|
|
35
|
-
- **Tag**: Select `v2.1.3` (already pushed)
|
|
36
|
-
- **Title**: `v2.1.3 - Windows CRLF & Parameter Brace Fixes`
|
|
37
|
-
- **Description**: Copy content from `RELEASE_NOTES_v2.1.3.md`
|
|
38
|
-
- **Attach files**: None needed (npm package is the artifact)
|
|
39
|
-
- Click **Publish release**
|
|
40
|
-
|
|
41
|
-
### 3. Verify Installation
|
|
42
|
-
Test that users can install the new version:
|
|
43
|
-
|
|
44
|
-
```bash
|
|
45
|
-
# Global install
|
|
46
|
-
npm install -g mcp-code-context@2.1.3
|
|
47
|
-
|
|
48
|
-
# Verify version
|
|
49
|
-
npx mcp-code-context --version # Should show 2.1.3
|
|
50
|
-
|
|
51
|
-
# Test with MCP client (Claude Desktop, Amazon Q, etc.)
|
|
52
|
-
# Update config to use @2.1.3 and restart client
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
### 4. Announce (Optional)
|
|
56
|
-
Consider announcing on:
|
|
57
|
-
- GitHub Discussions
|
|
58
|
-
- Twitter/X
|
|
59
|
-
- Reddit (r/LocalLLaMA, r/ClaudeAI)
|
|
60
|
-
- Discord communities
|
|
61
|
-
|
|
62
|
-
## 📋 Verification Checklist
|
|
63
|
-
|
|
64
|
-
Before announcing:
|
|
65
|
-
- [ ] npm package published successfully
|
|
66
|
-
- [ ] GitHub release created
|
|
67
|
-
- [ ] Can install via `npm install -g mcp-code-context@2.1.3`
|
|
68
|
-
- [ ] Version shows correctly in `package.json`
|
|
69
|
-
- [ ] Tag visible on GitHub releases page
|
|
70
|
-
- [ ] CHANGELOG.md shows v2.1.3 entry
|
|
71
|
-
- [ ] Tests passing on CI (if configured)
|
|
72
|
-
|
|
73
|
-
## 🐛 Rollback Plan (If Needed)
|
|
74
|
-
|
|
75
|
-
If critical issues are discovered:
|
|
76
|
-
|
|
77
|
-
```bash
|
|
78
|
-
# Unpublish from npm (within 72 hours)
|
|
79
|
-
npm unpublish mcp-code-context@2.1.3
|
|
80
|
-
|
|
81
|
-
# Delete GitHub release
|
|
82
|
-
# Go to releases page and delete v2.1.3
|
|
83
|
-
|
|
84
|
-
# Delete git tag
|
|
85
|
-
git tag -d v2.1.3
|
|
86
|
-
git push origin :refs/tags/v2.1.3
|
|
87
|
-
|
|
88
|
-
# Revert commit
|
|
89
|
-
git revert f0bfcfe
|
|
90
|
-
git push origin main
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
## 📊 Monitoring
|
|
94
|
-
|
|
95
|
-
After release, monitor:
|
|
96
|
-
- npm download stats: https://npm-stat.com/charts.html?package=mcp-code-context
|
|
97
|
-
- GitHub issues for bug reports
|
|
98
|
-
- GitHub stars/forks for adoption
|
|
99
|
-
|
|
100
|
-
## 🎉 Success Criteria
|
|
101
|
-
- No critical bugs reported within 48 hours
|
|
102
|
-
- Windows users confirm CRLF issues resolved
|
|
103
|
-
- Download count increases
|
|
104
|
-
- Positive feedback from users
|
|
105
|
-
|
|
106
|
-
---
|
|
107
|
-
|
|
108
|
-
**Current Status**: Ready for npm publish and GitHub release creation
|
|
109
|
-
**Blocked By**: npm authentication required
|
|
110
|
-
**Next Action**: Run `npm login` then `npm publish`
|