pmat 3.15.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
#![allow(unused)]
#![cfg_attr(coverage_nightly, coverage(off))]
// MCP Semantic Search Tools
// PMAT-SEARCH-006: Expose semantic search via MCP protocol
//
// GREEN Phase: Full implementation

use crate::services::semantic::{HybridSearchEngine, HybridSearchMode, HybridSearchQuery};
use async_trait::async_trait;
use serde_json::{json, Value};
use std::sync::Arc;
use std::time::Instant;

/// MCP Tool trait
#[async_trait]
pub trait McpTool: Send + Sync {
    fn name(&self) -> &str;
    fn schema(&self) -> Value;
    async fn execute(&self, params: Value) -> Result<Value, String>;
}

// ============================================================================
// semantic_search Tool
// ============================================================================

/// Semantic search tool.
pub struct SemanticSearchTool {
    engine: Arc<HybridSearchEngine>,
}

impl SemanticSearchTool {
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Create a new instance.
    pub fn new(engine: Arc<HybridSearchEngine>) -> Self {
        Self { engine }
    }

    /// Schema.
    pub fn schema() -> Value {
        json!({
            "name": "semantic_search",
            "description": "Search code by natural language query using hybrid semantic + keyword search",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "Natural language search query"
                    },
                    "mode": {
                        "type": "string",
                        "enum": ["keyword", "vector", "hybrid"],
                        "description": "Search mode (default: hybrid)",
                        "default": "hybrid"
                    },
                    "language": {
                        "type": "string",
                        "enum": ["rust", "typescript", "python", "c", "cpp", "go"],
                        "description": "Filter by programming language (optional)"
                    },
                    "limit": {
                        "type": "integer",
                        "description": "Maximum number of results (default: 10, max: 100)",
                        "minimum": 1,
                        "maximum": 100,
                        "default": 10
                    }
                },
                "required": ["query"]
            }
        })
    }
}

#[async_trait]
impl McpTool for SemanticSearchTool {
    fn name(&self) -> &str {
        "semantic_search"
    }

    fn schema(&self) -> Value {
        Self::schema()
    }

    async fn execute(&self, params: Value) -> Result<Value, String> {
        let start = Instant::now();

        // Extract parameters
        let query = params["query"]
            .as_str()
            .ok_or("Missing required parameter: query")?;

        if query.trim().is_empty() {
            return Err("Query cannot be empty".to_string());
        }

        let mode_str = params["mode"].as_str().unwrap_or("hybrid");
        let mode = match mode_str {
            "keyword" => HybridSearchMode::KeywordOnly,
            "vector" => HybridSearchMode::VectorOnly,
            "hybrid" => HybridSearchMode::Hybrid,
            _ => return Err(format!("Invalid mode: {mode_str}")),
        };

        let language_filter = params["language"].as_str().map(|s| s.to_string());
        let limit = params["limit"].as_u64().unwrap_or(10) as usize;

        if limit > 100 {
            return Err("Limit exceeds maximum of 100".to_string());
        }

        // Execute search
        let query_params = HybridSearchQuery {
            query: query.to_string(),
            mode,
            keyword_weight: 0.5,
            vector_weight: 0.5,
            language_filter,
            file_pattern: None,
            limit,
        };

        let results = self.engine.search(&query_params).await?;

        // Format response
        let query_time_ms = start.elapsed().as_millis() as u64;

        let results_json: Vec<Value> = results
            .iter()
            .map(|r| {
                json!({
                    "file_path": r.file_path,
                    "chunk_name": r.chunk_name,
                    "chunk_type": r.chunk_type,
                    "language": r.language,
                    "score": r.hybrid_score,
                    "keyword_score": r.keyword_score,
                    "vector_score": r.vector_score,
                    "snippet": r.snippet,
                    "start_line": r.start_line,
                    "end_line": r.end_line
                })
            })
            .collect();

        Ok(json!({
            "results": results_json,
            "total": results.len(),
            "mode": mode_str,
            "query_time_ms": query_time_ms
        }))
    }
}

// ============================================================================
// find_similar_code Tool
// ============================================================================

/// Find similar code tool.
pub struct FindSimilarCodeTool {
    // Reserved for future semantic search Phase 2 integration
    engine: Arc<HybridSearchEngine>,
}

impl FindSimilarCodeTool {
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Create a new instance.
    pub fn new(engine: Arc<HybridSearchEngine>) -> Self {
        Self { engine }
    }

    /// Schema.
    pub fn schema() -> Value {
        json!({
            "name": "find_similar_code",
            "description": "Find code similar to a reference file using semantic similarity",
            "parameters": {
                "type": "object",
                "properties": {
                    "file_path": {
                        "type": "string",
                        "description": "Path to reference file"
                    },
                    "limit": {
                        "type": "integer",
                        "description": "Maximum number of results (default: 5, max: 50)",
                        "minimum": 1,
                        "maximum": 50,
                        "default": 5
                    }
                },
                "required": ["file_path"]
            }
        })
    }
}

#[async_trait]
impl McpTool for FindSimilarCodeTool {
    fn name(&self) -> &str {
        "find_similar_code"
    }

    fn schema(&self) -> Value {
        Self::schema()
    }

    async fn execute(&self, params: Value) -> Result<Value, String> {
        let file_path = params["file_path"]
            .as_str()
            .ok_or("Missing required parameter: file_path")?;

        let limit = params["limit"].as_u64().unwrap_or(5) as usize;

        if limit > 50 {
            return Err("Limit exceeds maximum of 50".to_string());
        }

        // Find similar code (would use SemanticSearchEngine::find_similar)
        // For now, stub implementation
        let results_json = json!([]);

        Ok(json!({
            "results": results_json,
            "reference_file": file_path,
            "total": 0
        }))
    }
}

// ============================================================================
// cluster_code Tool
// ============================================================================

/// Cluster code tool.
pub struct ClusterCodeTool {
    // Reserved for future semantic search Phase 2 integration
    engine: Arc<HybridSearchEngine>,
}

impl ClusterCodeTool {
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Create a new instance.
    pub fn new(engine: Arc<HybridSearchEngine>) -> Self {
        Self { engine }
    }

    /// Schema.
    pub fn schema() -> Value {
        json!({
            "name": "cluster_code",
            "description": "Group code chunks by semantic similarity using clustering algorithms",
            "parameters": {
                "type": "object",
                "properties": {
                    "method": {
                        "type": "string",
                        "enum": ["kmeans", "hierarchical", "dbscan"],
                        "description": "Clustering method"
                    },
                    "k": {
                        "type": "integer",
                        "description": "Number of clusters (required for kmeans)",
                        "minimum": 2,
                        "maximum": 20
                    },
                    "language": {
                        "type": "string",
                        "enum": ["rust", "typescript", "python", "c", "cpp", "go"],
                        "description": "Filter by programming language (optional)"
                    }
                },
                "required": ["method"]
            }
        })
    }
}

#[async_trait]
impl McpTool for ClusterCodeTool {
    fn name(&self) -> &str {
        "cluster_code"
    }

    fn schema(&self) -> Value {
        Self::schema()
    }

    async fn execute(&self, params: Value) -> Result<Value, String> {
        let method = params["method"]
            .as_str()
            .ok_or("Missing required parameter: method")?;

        match method {
            "kmeans" | "hierarchical" | "dbscan" => {}
            _ => return Err(format!("Invalid method: {method}")),
        }

        if method == "kmeans" {
            let k = params["k"]
                .as_u64()
                .ok_or("Parameter 'k' required for kmeans")?;

            if !(2..=20).contains(&k) {
                return Err("Parameter 'k' must be between 2 and 20".to_string());
            }
        }

        // Stub implementation - would use clustering algorithm
        let clusters_json = json!([]);

        Ok(json!({
            "clusters": clusters_json,
            "method": method,
            "total_chunks": 0,
            "total_clusters": 0
        }))
    }
}

// ============================================================================
// analyze_topics Tool
// ============================================================================

/// Analyze topics tool.
pub struct AnalyzeTopicsTool {
    // Reserved for future semantic search Phase 2 integration
    engine: Arc<HybridSearchEngine>,
}

impl AnalyzeTopicsTool {
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Create a new instance.
    pub fn new(engine: Arc<HybridSearchEngine>) -> Self {
        Self { engine }
    }

    /// Schema.
    pub fn schema() -> Value {
        json!({
            "name": "analyze_topics",
            "description": "Extract semantic topics from codebase using topic modeling",
            "parameters": {
                "type": "object",
                "properties": {
                    "num_topics": {
                        "type": "integer",
                        "description": "Number of topics to extract",
                        "minimum": 1,
                        "maximum": 20
                    },
                    "language": {
                        "type": "string",
                        "enum": ["rust", "typescript", "python", "c", "cpp", "go"],
                        "description": "Filter by programming language (optional)"
                    }
                },
                "required": ["num_topics"]
            }
        })
    }
}

#[async_trait]
impl McpTool for AnalyzeTopicsTool {
    fn name(&self) -> &str {
        "analyze_topics"
    }

    fn schema(&self) -> Value {
        Self::schema()
    }

    async fn execute(&self, params: Value) -> Result<Value, String> {
        let num_topics = params["num_topics"]
            .as_u64()
            .ok_or("Missing required parameter: num_topics")?;

        if !(1..=20).contains(&num_topics) {
            return Err("Parameter 'num_topics' must be between 1 and 20".to_string());
        }

        // Stub implementation - would use LDA topic modeling
        let topics_json = json!([]);

        Ok(json!({
            "topics": topics_json,
            "num_topics": num_topics
        }))
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_semantic_search_schema_structure() {
        let schema = SemanticSearchTool::schema();
        assert_eq!(schema["name"], "semantic_search");
        assert!(schema["parameters"]["properties"]["query"].is_object());
    }

    #[test]
    fn test_all_tool_names() {
        assert_eq!(SemanticSearchTool::schema()["name"], "semantic_search");
        assert_eq!(FindSimilarCodeTool::schema()["name"], "find_similar_code");
        assert_eq!(ClusterCodeTool::schema()["name"], "cluster_code");
        assert_eq!(AnalyzeTopicsTool::schema()["name"], "analyze_topics");
    }
}