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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# MCP C++ End-to-End Testing Framework

## Overview

This comprehensive E2E testing framework provides seamless integration testing for the MCP C++ server with intelligent test identification, logging, and debugging capabilities.

## Quick Start

### Prerequisites

```bash
# Build the MCP server first
cd ../..
cargo build

# Install E2E test dependencies
cd test/e2e
npm install
```

#### System Requirements

- **clangd version 20**: Required for consistent LSP testing
  - Ubuntu/Debian: 
    ```bash
    # Add LLVM APT repository
    wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
    echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-20 main" | sudo tee /etc/apt/sources.list.d/llvm.list
    sudo apt-get update
    sudo apt-get install clangd-20
    ```
  - The framework is configured to use clangd-20 by default via `.env` file
  - CI environment automatically installs and configures clangd-20

#### Environment Configuration

The E2E tests use a `.env` file to configure the testing environment. Create one based on the example:

```bash
# Copy the example file and customize as needed
cp .env.example .env
```

Example `.env` configuration:
```env
# .env (in test/e2e directory)
CLANGD_PATH=/usr/bin/clangd-20
```

You can override these settings by:
1. Creating/modifying the `.env` file for permanent changes
2. Setting environment variables before running tests:
   ```bash
   CLANGD_PATH=/usr/bin/clangd-19 npm test
   ```

**Note**: The `.env` file is gitignored and created automatically in CI environments.

### Running Tests

```bash
# Run all E2E tests
npm test

# Run with UI interface
npm run test:ui

# Run with coverage
npm run test:coverage

# Run tests continuously (watch mode)
npm run test:watch
```

## Test Identification System

### The Problem We Solved

Previously, test temp folders were named with random UUIDs like `test-project-029e7d5a-a9e5-4634-8911-17cad9cacd28`, making it impossible to identify which test created which folder or logs.

### The Solution

Our enhanced system provides:

#### 1. **Descriptive Folder Names**
- **Before**: `test-project-029e7d5a-a9e5-4634-8911-17cad9cacd28`
- **After**: `list-build-dirs-test-029e7d5a` (test name + short UUID)

#### 2. **Test-Aware Log Files**
- **Before**: `mcp-cpp-server.log`, `mcp-cpp-clangd.log`
- **After**: `mcp-cpp-server-list-build-dirs-test.log`, `mcp-cpp-clangd-list-build-dirs-test.log`

#### 3. **Test Metadata Files**
Each temp folder contains `.test-info.json` with comprehensive test context:
```json
{
  "testName": "list-build-dirs-test",
  "describe": "List Build Dirs Tool",
  "timestamp": 1642680000000,
  "testId": "list-build-dirs-test-1642680000000",
  "uuid": "029e7d5a-a9e5-4634-8911-17cad9cacd28",
  "projectPath": "/path/to/temp/folder",
  "createdAt": "2024-01-20T12:00:00.000Z",
  "nodeVersion": "v18.17.0",
  "platform": "linux"
}
```

## Test Directory Inspector

### Basic Usage

```bash
# Inspect all test directories
npm run inspect

# Detailed view with metadata
npm run inspect:verbose

# Show log file details
npm run inspect:logs

# Preview cleanup (dry run)
npm run cleanup:dry

# Actually cleanup test directories
npm run cleanup
```

### Example Output

```
🔍 Inspecting test directories...

📝 list-build-dirs-test-029e7d5a
   Path: /path/to/temp/list-build-dirs-test-029e7d5a
   Size: 2.3 MB
   Created: 1/20/2024, 12:00:00 PM
   Modified: 1/20/2024, 12:01:30 PM
   Test: list-build-dirs-test
   Suite: List Build Dirs Tool
   ID: list-build-dirs-test-1642680000000
   📋 Log files:
     • mcp-cpp-server-list-build-dirs-test.log: 45.2 KB (342 lines)
     • mcp-cpp-clangd-list-build-dirs-test.log: 12.8 KB (89 lines)

🔍📝 symbol-search-test-1a2b3c4d
   Path: /path/to/temp/symbol-search-test-1a2b3c4d
   Size: 1.8 MB
   Created: 1/20/2024, 11:45:00 AM
   Modified: 1/20/2024, 11:46:15 AM
   Test: symbol-search-test
   Suite: Symbol Search Tool
   🔍 PRESERVED FOR DEBUGGING
   Reason: Test failed - investigating symbol resolution
   Preserved: 1/20/2024, 11:46:15 AM

📊 SUMMARY
   Total directories: 2
   With metadata: 2
   Preserved for debugging: 1
   With errors: 0
   Total size: 4.1 MB
```

### Icons Reference

- 📝 = Directory with test metadata
- ❓ = Directory without metadata
- 🔍 = Directory preserved for debugging
- ❌ = Directory with errors

## Writing Tests

### Option 1: Enhanced Helper Functions (Recommended)

```typescript
import { TestHelpers } from '../framework/TestHelpers.js';

describe('My Test Suite', () => {
  let client: McpClient;
  let project: TestProject;

  beforeEach(async () => {
    const setup = await TestHelpers.setupTest({
      template: 'base',
      testName: 'my-test-name',
      describe: 'My Test Suite',
      logLevel: 'info'
    });
    
    project = setup.project;
    client = setup.client;
  });

  afterEach(async () => {
    await TestHelpers.cleanup(client, project);
  });

  it('should do something', async () => {
    // Your test logic here
    const result = await client.callTool('list_build_dirs');
    expect(result.content).toBeDefined();
  });
});
```

### Option 2: Manual Context Creation

```typescript
import { TestUtils } from '../framework/TestUtils.js';

describe('My Test Suite', () => {
  let client: McpClient;
  let project: TestProject;

  beforeEach(async () => {
    const testContext = TestUtils.createTestContext('my-test-name', 'My Test Suite');
    project = await TestProject.fromBaseProject(undefined, testContext);
    
    const serverPath = await TestUtils.findMcpServer();
    const logEnv = TestUtils.createTestEnvironment(
      project.getProjectPath(),
      testContext.testName,
      'warn'
    );

    client = new McpClient(serverPath, {
      workingDirectory: project.getProjectPath(),
      timeout: 15000,
      env: logEnv.env,
    });
    
    await client.start();
  });

  afterEach(async () => {
    await client.stop();
    await project.cleanup();
  });
});
```

## Debugging Failed Tests

### Preserve Test Artifacts

```typescript
it('should handle complex operation', async () => {
  try {
    // Test logic that might fail
    await project.runCmake();
    const result = await client.callTool('complex_operation');
    expect(result.content).toBeDefined();
  } catch (error) {
    // Preserve the test folder for debugging
    await TestHelpers.preserveForDebugging(project, `Test failed: ${error.message}`);
    throw error;
  }
});
```

### Inspect Preserved Directories

```bash
# See which directories are preserved
npm run inspect

# Look for the 🔍 icon indicating preserved directories
# Then manually investigate the logs and files
```

## Framework Components

### TestProject Class

Enhanced project management with test context:

```typescript
// Create project with test context
const project = await TestProject.fromBaseProject(options, testContext);

// Get test metadata
const metadata = await project.getTestMetadata();

// Preserve for debugging
await project.preserveForDebugging("Investigation needed");
```

### McpClient Class

MCP server communication with test-aware logging:

```typescript
const client = new McpClient(serverPath, {
  workingDirectory: project.getProjectPath(),
  timeout: 15000,
  env: logEnv.env, // Test-aware environment
});
```

### TestUtils Class

Utility functions for test setup:

```typescript
// Create test environment with logging
const { env, serverLogPath, clangdLogPath } = TestUtils.createTestEnvironment(
  projectPath,
  testName,
  'warn'
);

// Create test context
const testContext = TestUtils.createTestContext('my-test', 'My Suite');
```

## Directory Structure

After running tests, you'll see:

```
temp/
├── list-build-dirs-test-029e7d5a/
│   ├── .test-info.json                           # Test metadata
│   ├── mcp-cpp-server-list-build-dirs-test.log  # Server logs
│   ├── mcp-cpp-clangd-list-build-dirs-test.log  # Clangd logs
│   ├── CMakeLists.txt                           # Project files
│   ├── src/
│   ├── include/
│   └── build-debug/
├── symbol-search-test-1a2b3c4d/
│   ├── .test-info.json
│   ├── .debug-preserved.json                    # Debug preservation info
│   ├── mcp-cpp-server-symbol-search-test.log
│   ├── mcp-cpp-clangd-symbol-search-test.log
│   └── ...
└── ...
```

## Available Templates

### Base Template (Default)
Full C++ project with:
- CMakeLists.txt
- Source files (src/)
- Header files (include/)
- Multiple build configurations

### Empty Template
Minimal temp directory for custom project setup

### Minimal CMake Template
Basic CMake project with:
- Simple CMakeLists.txt
- Single main.cpp file

## CI/CD Integration

### GitHub Actions

```yaml
- name: Run E2E Tests
  run: |
    cd test/e2e
    npm install
    npm run test:full
    
- name: Inspect Test Results
  if: failure()
  run: |
    cd test/e2e
    npm run inspect:verbose
```

### Environment Variables

The framework supports these environment variables:

- `MCP_SERVER_PATH`: Custom path to MCP server binary
- `MCP_TEST_NAME`: Current test name (auto-set)
- `MCP_TEST_ID`: Unique test identifier (auto-set)
- `MCP_LOG_FILE`: Server log file path (auto-set)
- `RUST_LOG`: Rust logging level

## Performance Considerations

### Test Isolation
- Each test gets its own temp directory
- Automatic cleanup after successful tests
- Log files are test-specific

### Resource Management
- Proper cleanup of MCP server processes
- Automatic temp directory cleanup
- Configurable timeouts

### Build Management
- Smart build directory detection
- CMake cache management
- Compilation database generation

## Troubleshooting

### Common Issues

1. **MCP server not found**
   ```bash
   # Build the server first
   cd ../..
   cargo build
   ```

2. **Test folders not cleaning up**
   ```bash
   # Manual cleanup
   npm run cleanup
   ```

3. **Port conflicts**
   - Each test uses stdio transport (no ports)
   - Tests are isolated by process

### Debug Logging

```typescript
// Enable debug logging
const setup = await TestHelpers.setupTest({
  logLevel: 'debug',
  testName: 'my-debug-test'
});
```

### Log Analysis

```bash
# Check specific test logs
npm run inspect:logs

# Look for ERROR or WARN entries in the output
```

## Best Practices

1. **Always use test context** for new tests
2. **Preserve failed tests** for debugging
3. **Use descriptive test names** for easy identification
4. **Clean up regularly** to avoid disk space issues
5. **Check logs** when tests fail unexpectedly

## Migration Guide

### Updating Existing Tests

Replace this:
```typescript
beforeEach(async () => {
  project = await TestProject.fromBaseProject();
  client = new McpClient(serverPath, {
    workingDirectory: project.getProjectPath(),
    timeout: 15000,
  });
});
```

With this:
```typescript
beforeEach(async () => {
  const setup = await TestHelpers.setupTest({
    testName: 'my-test',
    describe: 'My Test Suite'
  });
  project = setup.project;
  client = setup.client;
});
```

## Advanced Features

### Custom Test Templates

```typescript
// Create custom project structure
const project = await TestProject.fromTemplate('empty', testContext);
await project.writeFile('custom.cpp', customCode);
await project.runCmake({ options: { CUSTOM_FLAG: 'ON' } });
```

### Log Analysis

```typescript
// Read and analyze logs
const logEntries = await TestUtils.readLogFile(serverLogPath);
const analysis = TestUtils.analyzeLogEntries(logEntries);
expect(analysis.errors.length).toBe(0);
```

### Multi-Configuration Testing

```typescript
// Test different build configurations
await project.switchBuildConfig(BuildConfiguration.RELEASE);
await project.runCmake({ buildType: 'Release' });
```

This framework provides a robust foundation for testing the MCP C++ server with full traceability and debugging capabilities.