pmat 3.17.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# PMAT MCP Integration Guide

**Protocol Version**: MCP v2024-11-05
**Last Updated**: October 19, 2025

## Table of Contents

1. [Quick Start]#quick-start
2. [Server Configuration]#server-configuration
3. [Client Connection]#client-connection
4. [Authentication]#authentication
5. [Common Workflows]#common-workflows
6. [Error Handling]#error-handling
7. [Best Practices]#best-practices

---

## Quick Start

### Start the PMAT MCP Server

```bash
# Start the server (default: localhost:3000)
pmat mcp-server

# Start with custom configuration
pmat mcp-server --bind 127.0.0.1:8080
```

### Connect a Client

```javascript
import { McpClient } from '@modelcontextprotocol/sdk';

const client = new McpClient({
  endpoint: 'http://localhost:3000',
  protocolVersion: '2024-11-05'
});

await client.connect();
```

---

## Server Configuration

### Default Configuration

The server uses the following defaults (from `server/src/mcp_integration/server.rs:31`):

```rust
ServerConfig {
    name: "PMAT MCP Server",
    version: env!("CARGO_PKG_VERSION"),
    bind_address: "127.0.0.1:3000",
    unix_socket: None,
    max_connections: 100,
    request_timeout: Duration::from_secs(30),
    enable_logging: true,

    // Semantic search (requires OPENAI_API_KEY)
    semantic_enabled: false,
    semantic_api_key: None,
    semantic_db_path: Some("~/.pmat/embeddings.db"),
    semantic_workspace: Some(cwd),
}
```

### Environment Variables

```bash
# Enable semantic search tools (optional)
export OPENAI_API_KEY="sk-..."
export PMAT_VECTOR_DB_PATH="~/.pmat/embeddings.db"
export PMAT_WORKSPACE="/path/to/workspace"
```

### Custom Configuration

```bash
# Custom bind address
pmat mcp-server --bind 0.0.0.0:8080

# Unix socket (for local IPC)
pmat mcp-server --unix-socket /tmp/pmat.sock

# Enable verbose logging
RUST_LOG=debug pmat mcp-server
```

---

## Client Connection

### TypeScript/JavaScript Client

```typescript
import { McpClient } from '@modelcontextprotocol/sdk';

async function connectToPMAT() {
  const client = new McpClient({
    endpoint: 'http://localhost:3000',
    protocolVersion: '2024-11-05',
    timeout: 30000 // 30 seconds
  });

  try {
    await client.connect();

    // Initialize the connection
    const initResponse = await client.initialize({
      clientInfo: {
        name: "my-ai-agent",
        version: "1.0.0"
      }
    });

    console.log('Connected to PMAT MCP Server:', initResponse.serverInfo);

    return client;
  } catch (error) {
    console.error('Connection failed:', error);
    throw error;
  }
}
```

### Python Client

```python
from mcp import Client

async def connect_to_pmat():
    client = Client(
        endpoint="http://localhost:3000",
        protocol_version="2024-11-05",
        timeout=30.0
    )

    await client.connect()

    # Initialize
    init_response = await client.initialize({
        "clientInfo": {
            "name": "my-ai-agent",
            "version": "1.0.0"
        }
    })

    print(f"Connected to: {init_response['serverInfo']['name']}")

    return client
```

---

## Authentication

**Current Status**: No authentication required for local connections.

**Future**: When deploying to production, consider:
- API key authentication
- OAuth 2.0
- mTLS for service-to-service

---

## Common Workflows

### Workflow 1: Validate Documentation Before Commit

```javascript
async function validateDocs(client) {
  // Step 1: Generate deep context
  await runCommand('pmat context --output deep_context.md');

  // Step 2: Validate documentation
  const result = await client.callTool('validate_documentation', {
    documentation_path: 'README.md',
    deep_context_path: 'deep_context.md',
    similarity_threshold: 0.7,
    fail_on_error: true
  });

  // Step 3: Check results
  if (!result.summary.pass) {
    console.error('Documentation validation failed!');
    console.error(`Contradictions: ${result.summary.contradictions}`);
    console.error(`Unverified claims: ${result.summary.unverified}`);
    process.exit(1);
  }

  console.log('✅ Documentation validated successfully');
}
```

### Workflow 2: Code Quality Check Before Merge

```javascript
async function qualityCheck(client, filePaths) {
  const issues = [];

  for (const path of filePaths) {
    // Analyze technical debt
    const analysis = await client.callTool('analyze_technical_debt', {
      path,
      include_penalties: true
    });

    // Get recommendations if score is low
    if (analysis.score.total < 70) {
      const recommendations = await client.callTool('get_quality_recommendations', {
        path,
        max_recommendations: 5,
        min_severity: 'high'
      });

      issues.push({
        file: path,
        score: analysis.score.total,
        grade: analysis.score.grade,
        recommendations: recommendations.recommendations
      });
    }
  }

  return issues;
}
```

### Workflow 3: AI-Assisted Code Review

```javascript
async function aiCodeReview(client, changedFiles) {
  const reviews = [];

  for (const file of changedFiles) {
    // Get quality recommendations
    const recommendations = await client.callTool('get_quality_recommendations', {
      path: file.path,
      max_recommendations: 10,
      min_severity: 'medium'
    });

    // Analyze technical debt
    const analysis = await client.callTool('analyze_technical_debt', {
      path: file.path,
      include_penalties: true
    });

    // Generate review comments
    const comments = recommendations.recommendations.map(rec => ({
      file: file.path,
      severity: rec.severity,
      message: `**${rec.category}**: ${rec.issue}\n\n` +
               `**Suggestion**: ${rec.suggestion}\n\n` +
               `**Impact**: ${rec.impact.toFixed(1)} points`,
      line: null // Could be enhanced with line numbers
    }));

    reviews.push({
      file: file.path,
      score: analysis.score.total,
      grade: analysis.score.grade,
      comments
    });
  }

  return reviews;
}
```

### Workflow 4: Documentation Accuracy CI/CD Gate

```javascript
async function documentationGate(client) {
  try {
    // Validate all documentation files
    const docFiles = ['README.md', 'CLAUDE.md', 'AGENT.md', 'GEMINI.md'];

    // Generate deep context once
    await runCommand('pmat context --output deep_context.md');

    let allPassed = true;
    const results = [];

    for (const docFile of docFiles) {
      const result = await client.callTool('validate_documentation', {
        documentation_path: docFile,
        deep_context_path: 'deep_context.md',
        similarity_threshold: 0.7,
        fail_on_error: false
      });

      results.push({
        file: docFile,
        passed: result.summary.pass,
        stats: result.summary
      });

      if (!result.summary.pass) {
        allPassed = false;
      }
    }

    // Generate report
    console.log('## Documentation Validation Report\n');
    for (const result of results) {
      const status = result.passed ? '✅ PASS' : '❌ FAIL';
      console.log(`### ${result.file}: ${status}`);
      console.log(`- Total Claims: ${result.stats.total_claims}`);
      console.log(`- Verified: ${result.stats.verified}`);
      console.log(`- Contradictions: ${result.stats.contradictions}`);
      console.log(`- Unverified: ${result.stats.unverified}\n`);
    }

    return allPassed;
  } catch (error) {
    console.error('Documentation gate failed:', error);
    return false;
  }
}
```

---

## Error Handling

### Error Code Reference

```javascript
const MCP_ERRORS = {
  PARSE_ERROR: -32700,
  INVALID_REQUEST: -32600,
  METHOD_NOT_FOUND: -32601,
  INVALID_PARAMS: -32602,
  INTERNAL_ERROR: -32603
};
```

### Handling Errors Gracefully

```javascript
async function robustToolCall(client, toolName, params) {
  try {
    return await client.callTool(toolName, params);
  } catch (error) {
    if (error.code === MCP_ERRORS.INVALID_PARAMS) {
      // Parameter validation error - check error.data.suggestion
      console.error(`Invalid parameters for ${toolName}:`, error.message);
      if (error.data?.suggestion) {
        console.error(`Suggestion: ${error.data.suggestion}`);
      }
      throw new Error(`Parameter error: ${error.message}`);
    } else if (error.code === MCP_ERRORS.INTERNAL_ERROR) {
      // Internal server error - retry with backoff
      console.warn(`Internal error in ${toolName}, retrying...`);
      await sleep(1000);
      return await client.callTool(toolName, params);
    } else {
      // Unknown error
      console.error(`Unexpected error calling ${toolName}:`, error);
      throw error;
    }
  }
}
```

### Timeout Handling

```javascript
async function callWithTimeout(client, toolName, params, timeoutMs = 30000) {
  return Promise.race([
    client.callTool(toolName, params),
    new Promise((_, reject) =>
      setTimeout(() => reject(new Error('Tool call timed out')), timeoutMs)
    )
  ]);
}
```

---

## Best Practices

### 1. Connection Management

```javascript
// Use connection pooling for multiple requests
class PMATClient {
  constructor(endpoint) {
    this.endpoint = endpoint;
    this.client = null;
  }

  async connect() {
    if (!this.client) {
      this.client = new McpClient({ endpoint: this.endpoint });
      await this.client.connect();
      await this.client.initialize({
        clientInfo: { name: "pmat-client", version: "1.0.0" }
      });
    }
    return this.client;
  }

  async disconnect() {
    if (this.client) {
      await this.client.disconnect();
      this.client = null;
    }
  }
}
```

### 2. Caching Deep Context

```javascript
// Generate deep context once, reuse for multiple validations
async function generateDeepContext() {
  const cacheFile = '.pmat-cache/deep_context.md';
  const cacheAge = await getFileAge(cacheFile);

  // Regenerate if cache is older than 1 hour
  if (cacheAge > 3600) {
    await runCommand('pmat context --output ' + cacheFile);
  }

  return cacheFile;
}
```

### 3. Batch Processing

```javascript
// Process files in batches to avoid overwhelming the server
async function analyzeBatch(client, files, batchSize = 10) {
  const results = [];

  for (let i = 0; i < files.length; i += batchSize) {
    const batch = files.slice(i, i + batchSize);
    const batchResults = await Promise.all(
      batch.map(file =>
        client.callTool('analyze_technical_debt', { path: file })
      )
    );
    results.push(...batchResults);
  }

  return results;
}
```

### 4. Logging and Monitoring

```javascript
// Wrap client calls with logging
async function loggedToolCall(client, toolName, params) {
  const startTime = Date.now();

  try {
    console.log(`[MCP] Calling ${toolName}...`);
    const result = await client.callTool(toolName, params);
    const duration = Date.now() - startTime;
    console.log(`[MCP] ${toolName} completed in ${duration}ms`);
    return result;
  } catch (error) {
    const duration = Date.now() - startTime;
    console.error(`[MCP] ${toolName} failed after ${duration}ms:`, error.message);
    throw error;
  }
}
```

### 5. Health Checks

```javascript
async function checkServerHealth(client) {
  try {
    // List tools as a health check
    const tools = await client.listTools();
    return {
      healthy: true,
      toolCount: tools.tools.length,
      timestamp: new Date().toISOString()
    };
  } catch (error) {
    return {
      healthy: false,
      error: error.message,
      timestamp: new Date().toISOString()
    };
  }
}
```

---

## Troubleshooting

### Server Won't Start

```bash
# Check if port is already in use
lsof -i :3000

# Check logs
RUST_LOG=debug pmat mcp-server

# Check firewall
sudo ufw status
```

### Connection Timeouts

```javascript
// Increase timeout for slow operations
const client = new McpClient({
  endpoint: 'http://localhost:3000',
  timeout: 120000 // 2 minutes for large projects
});
```

### Semantic Search Not Available

```bash
# Ensure OpenAI API key is set
echo $OPENAI_API_KEY

# Check server logs for semantic tool registration
RUST_LOG=info pmat mcp-server | grep semantic
```

---

## Next Steps

- [Tools Catalog]TOOLS.md - Complete list of available tools
- [Examples]../../examples/mcp/ - Working code examples
- [Architecture]../architecture/ - System design documentation

---

**Maintained by**: PMAT Development Team
**Last Updated**: Sprint 40d (October 19, 2025)