mcp-cpp-server 0.2.2

A high-performance Model Context Protocol (MCP) server for C++ code analysis using clangd LSP integration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# Symbol Context Analyzer Implementation Plan

## Overview

The Symbol Context Analyzer is a comprehensive tool that provides detailed context about any symbol in a C++ codebase. It aggregates information from multiple LSP calls to give AI agents a complete picture of a symbol's definition, usage, relationships, and semantic meaning.

## Feature Breakdown

### Input Schema

```json
{
  "tool": "analyze_symbol_context",
  "arguments": {
    "symbol": "string",
    "location": {
      "file_uri": "string",
      "position": {
        "line": "number",
        "character": "number"
      }
    }?,
    "include_usage_patterns": "boolean?",
    "max_usage_examples": "number?",
    "include_inheritance": "boolean?"
  }
}
```

### Output Schema

```json
{
  "symbol": {
    "name": "string",
    "kind": "class|function|variable|namespace|...",
    "fully_qualified_name": "string",
    "declaration": {
      "file": "string",
      "range": "Range",
      "text": "string"
    },
    "definition": {
      "file": "string",
      "range": "Range",
      "text": "string"
    }?,
    "type_info": {
      "type": "string",
      "is_template": "boolean",
      "template_parameters": ["string"]?,
      "underlying_type": "string?"
    },
    "documentation": "string?",
    "visibility": "public|private|protected",
    "storage_class": "static|extern|auto|...",
    "relationships": {
      "base_classes": ["string"]?,
      "derived_classes": ["string"]?,
      "overrides": ["string"]?,
      "overridden_by": ["string"]?,
      "called_by": ["string"]?,
      "calls": ["string"]?,
      "used_by_count": "number"
    },
    "usage_patterns": [
      {
        "file": "string",
        "line": "number",
        "context": "string",
        "pattern_type": "call|instantiation|reference|..."
      }
    ]?
  }
}
```

## Incremental Development Plan

### Increment 1: Basic Symbol Info Tool with Smart Search

**User Value**: Get basic symbol information using clangd's optimized search capabilities
**Deliverable**: Working MCP tool that leverages clangd's native symbol search for fast results

**What users get**:

- Symbol name and kind (function, class, variable, etc.) via clangd's classification
- Type information from hover with clangd's rich type analysis
- Documentation from clangd's hover responses
- File location with clangd's precise source mapping
- Fast symbol lookup using clangd's optimized indexing

**Implementation**:

1. Leverage clangd's workspace/symbol for fast symbol location using the required `symbol` parameter
2. Use optional `location` for disambiguation when multiple symbols have the same name
3. Use clangd's hover for rich symbol information once symbol is located
4. Build on existing `GetSymbolsTool` infrastructure and integrate with symbol search
5. Optimize symbol lookup using qualified names and workspace search

**Files to create/modify**:

- `src/lsp/types.rs` - Add `BasicSymbolInfo`, `SymbolPosition` structs
- `src/lsp/client.rs` - Ensure `textDocument/hover` optimization
- `src/tools.rs` - Add `AnalyzeSymbolContextTool`
- `src/handler.rs` - Register the tool

### Increment 2: Symbol Location Discovery

**User Value**: Find where symbols are defined and declared
**Deliverable**: Enhanced tool that shows symbol definitions and declarations

**What users get**:

- Definition location (where symbol is implemented)
- Declaration location (where symbol is declared)
- Distinction between declaration and definition
- Multiple definitions for templates/overloads

**Implementation**:

1. Extend data structures for locations
2. Add LSP definition/declaration requests
3. Enhance existing tool to include location info
4. Handle multiple results

**Files to modify**:

- `src/lsp/types.rs` - Add `SymbolLocation`, `SymbolDefinition`
- `src/lsp/client.rs` - Add `textDocument/definition`, `textDocument/declaration`
- `src/tools.rs` - Enhance tool with location data

### Increment 3: Reference Counting

**User Value**: See how popular/widely used a symbol is
**Deliverable**: Tool shows usage statistics for symbols

**What users get**:

- Total count of references to the symbol
- Quick assessment of symbol importance
- Basic usage validation (is this symbol actually used?)

**Implementation**:

1. Add LSP references request
2. Count and categorize references
3. Add usage statistics to output
4. Simple reference filtering

**Files to modify**:

- `src/lsp/client.rs` - Add `textDocument/references`
- `src/lsp/types.rs` - Add `UsageStatistics`
- `src/tools.rs` - Include reference counting

### Increment 4: Usage Examples

**User Value**: See concrete examples of how symbols are used
**Deliverable**: Tool provides actual code examples showing symbol usage

**What users get**:

- Real code snippets showing symbol usage
- Context around each usage
- Configurable number of examples
- Different types of usage (call sites, instantiations, etc.)

**Implementation**:

1. Collect references with context
2. Extract code snippets around usage
3. Classify usage types (basic classification)
4. Sample and limit examples

**Files to modify**:

- `src/lsp/types.rs` - Add `UsageExample`, `UsageContext`
- `src/tools.rs` - Add usage example collection
- Enhance reference processing for context extraction

### Increment 5: Class Hierarchy Information

**User Value**: Understand inheritance relationships for classes
**Deliverable**: Tool shows inheritance hierarchies and polymorphic relationships

**What users get**:

- Base classes and inheritance chain
- Derived classes using this symbol
- Interface implementations
- Virtual function overrides

**Implementation**:

1. Add LSP type hierarchy requests
2. Process inheritance information
3. Add hierarchy data to output
4. Handle complex inheritance scenarios

**Files to modify**:

- `src/lsp/client.rs` - Add `typeHierarchy/supertypes`, `typeHierarchy/subtypes`
- `src/lsp/types.rs` - Add `InheritanceInfo`
- `src/tools.rs` - Include hierarchy analysis

### Increment 6: Call Relationships

**User Value**: Understand function call relationships and dependencies
**Deliverable**: Tool shows what functions call this symbol and what it calls

**What users get**:

- Functions that call this symbol (incoming calls)
- Functions that this symbol calls (outgoing calls)
- Call chain analysis
- Dependency mapping

**Implementation**:

1. Add LSP call hierarchy requests
2. Process call relationship data
3. Add call information to output
4. Handle recursive and complex call patterns

**Files to modify**:

- `src/lsp/client.rs` - Add `callHierarchy/incomingCalls`, `callHierarchy/outgoingCalls`
- `src/lsp/types.rs` - Add `CallRelationships`
- `src/tools.rs` - Include call analysis

### Increment 7: Advanced Pattern Recognition

**User Value**: Smart classification of how symbols are used (patterns, anti-patterns)
**Deliverable**: Tool provides intelligent insights about symbol usage patterns

**What users get**:

- Pattern classification (factory usage, RAII, etc.)
- Usage quality assessment
- Potential issues or improvements
- Best practice alignment

**Implementation**:

1. Advanced usage pattern analysis
2. Heuristic-based pattern recognition
3. Quality metrics and suggestions
4. Configurable analysis depth

**Files to modify**:

- `src/lsp/symbol_analyzer.rs` - Create pattern analysis module
- `src/lsp/types.rs` - Add `UsagePattern`, `QualityMetrics`
- `src/tools.rs` - Include pattern analysis

### Increment 8: Performance and Robustness

**User Value**: Fast, reliable tool that works in large codebases
**Deliverable**: Production-quality tool with comprehensive error handling

**What users get**:

- Fast analysis even in large projects
- Graceful handling of LSP failures
- Helpful error messages
- Configurable timeouts and limits

**Implementation**:

1. Performance optimization (caching, concurrent requests)
2. Comprehensive error handling
3. Timeout and retry logic
4. Resource management

**Files to modify**:

- All existing files - Add error handling and performance optimizations
- `src/lsp/error.rs` - Comprehensive error types
- Add caching and optimization logic

### Increment 9: Testing and Documentation

**User Value**: Reliable tool with clear usage guidance
**Deliverable**: Well-tested tool with comprehensive documentation

**What users get**:

- Confidence in tool reliability
- Clear usage examples and guidance
- Troubleshooting help
- Performance characteristics documentation

**Implementation**:

1. Comprehensive test suite
2. Usage documentation and examples
3. Performance benchmarks
4. Troubleshooting guides

**Files to create**:

- `tests/symbol_analysis_test.rs` - Integration tests
- `docs/symbol_analyzer_guide.md` - User documentation
- Example projects for testing

## Development Strategy

Each increment delivers immediate user value and can be shipped independently. Users get progressively more powerful symbol analysis capabilities with each release.

### Value Progression:

1. **Basic Info** → Users can quickly understand any symbol
2. **Location Discovery** → Users can navigate to symbol definitions
3. **Popularity** → Users can assess symbol importance
4. **Examples** → Users see real usage patterns
5. **Inheritance** → Users understand class relationships
6. **Call Graph** → Users trace function dependencies
7. **Smart Analysis** → Users get intelligent insights
8. **Production Ready** → Users get reliable, fast tool
9. **Well Documented** → Users have complete guidance

### Release Strategy:

- Each increment can be released as a minor version
- Users benefit immediately from each improvement
- No need to wait for complete feature set
- Feedback can guide later increments

## Technical Considerations

### Performance Optimizations

- Use concurrent LSP calls where possible to reduce latency
- Implement caching for expensive operations (type hierarchy, etc.)
- Limit usage pattern collection to avoid overwhelming responses
- Use streaming responses for large result sets

### Error Handling Strategy

- Fail gracefully when optional LSP features are not available
- Provide partial results when some LSP calls fail
- Clear error messages with actionable guidance
- Fallback to simpler analysis when advanced features fail

### LSP Compatibility

- Test with clangd (primary target)
- Consider compatibility with other C++ LSP servers
- Handle LSP server restarts and connection issues
- Graceful degradation when LSP features are not supported

### Memory and Resource Management

- Efficient handling of large symbol hierarchies
- Bounded memory usage for usage pattern collection
- Proper cleanup of LSP resources
- Monitoring and alerting for resource usage

## Dependencies

### New Dependencies (if needed)

- None expected - should work with existing LSP infrastructure

### LSP Server Requirements

- clangd with full feature support
- Proper compilation database (compile_commands.json)
- Index database for fast symbol lookup

## Success Criteria

1. **Functionality**: Tool successfully analyzes symbols and returns comprehensive context
2. **Performance**: Analysis completes within 5 seconds for typical symbols
3. **Robustness**: Handles errors gracefully and provides meaningful feedback
4. **Coverage**: Works with classes, functions, variables, namespaces, and templates
5. **Integration**: Seamlessly integrates with existing MCP tool framework

## Future Enhancements

1. **Semantic Analysis**: Add AI-powered semantic understanding of symbol purpose
2. **Cross-Reference Maps**: Generate visual representations of symbol relationships
3. **Code Quality Metrics**: Include complexity and maintainability metrics
4. **Historical Analysis**: Track symbol usage changes over time
5. **Refactoring Suggestions**: Provide automated refactoring recommendations

## Usage Examples

### Basic Symbol Analysis

```json
{
  "method": "tools/call",
  "params": {
    "name": "analyze_symbol_context",
    "arguments": {
      "symbol": "MyClass::process"
    }
  }
}
```

### Disambiguating Overloaded Functions

```json
{
  "method": "tools/call",
  "params": {
    "name": "analyze_symbol_context",
    "arguments": {
      "symbol": "process",
      "location": {
        "file_uri": "file:///path/to/src/myfile.cpp",
        "position": {
          "line": 42,
          "character": 15
        }
      }
    }
  }
}
```

### Comprehensive Analysis with Usage Patterns

```json
{
  "method": "tools/call",
  "params": {
    "name": "analyze_symbol_context",
    "arguments": {
      "symbol": "std::vector",
      "include_usage_patterns": true,
      "max_usage_examples": 10,
      "include_inheritance": true
    }
  }
}
```

### Simple Function Analysis

```json
{
  "method": "tools/call",
  "params": {
    "name": "analyze_symbol_context",
    "arguments": {
      "symbol": "::main"
    }
  }
}
```

## Symbol Resolution Strategy

The tool resolves symbols using this priority order:

1. **Qualified Name Match**: If `symbol` contains `::`, search for exact qualified name match
2. **Workspace Search**: Use `workspace/symbol` to find all symbols matching the name
3. **Location Disambiguation**: If `location` is provided and multiple matches exist, filter by proximity to location
4. **Best Match Selection**: Choose the most relevant match based on:
   - Exact name match over partial match
   - Project symbols over external symbols (when applicable)
   - Definition over declaration when both exist

### Handling Multiple Matches

- **Single Match**: Proceed with analysis
- **Multiple Matches**:
  - If `location` provided: Select closest match
  - If no `location`: Return analysis for the first/best match with metadata about other matches
- **No Matches**: Return error with suggestions for similar symbol names