cert-x-gen 1.9.2

Advanced Multi-Language Security Scanning Engine
Documentation
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
//! CXG MCP Server implementation
//!
//! Provides 12 tools for AI agents:
//! - cxg_search: Search templates by query/filters
//! - cxg_template_list: List templates with optional filters
//! - cxg_template_info: Get detailed info on a specific template
//! - cxg_scan: Run security scans against targets
//! - cxg_template_validate: Validate template code (12-language checker)
//! - cxg_template_create: Scaffold a new template with boilerplate
//! - cxg_template_write: Validate + save a completed template atomically
//! - cxg_template_get_notes: Get the AI generation guide for a language
//! - cxg_ai_generate: Generate a template from natural language (dual-mode)
//! - cxg_template_test: Test a specific template against a target
//! - cxg_template_stats: Get template collection statistics
//! - cxg_template_update: Pull latest templates from remote repository

use crate::config::Config;
use crate::core::CertXGen;
use crate::search::{SearchArgs, SearchFormat, SearchSort, TemplateSearchEngine};
use crate::template::TemplateFilter;
use crate::types::{Protocol, Severity, Target, TemplateLanguage};

use rmcp::handler::server::router::tool::ToolRouter;
use rmcp::handler::server::wrapper::Parameters;
use rmcp::model::*;
use rmcp::{tool, tool_handler, tool_router, ServerHandler};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

// ─── Request types ───────────────────────────────────────────────────────────

/// Parameters for the `cxg_search` MCP tool — searches the template library.
#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct SearchRequest {
    /// Search query text (searches name, description, tags)
    pub query: Option<String>,
    /// Filter by language: yaml, python, rust, shell, javascript, c, cpp, java, go, ruby, perl, php
    pub language: Option<String>,
    /// Filter by severity: critical, high, medium, low, info
    pub severity: Option<String>,
    /// Filter by tags (comma-separated)
    pub tags: Option<String>,
    /// Maximum results to return (default: 20)
    pub limit: Option<usize>,
}

/// Parameters for the `cxg_template_list` MCP tool — lists templates with optional filters.
#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct TemplateListRequest {
    /// Filter by programming language
    pub language: Option<String>,
    /// Filter by severity level
    pub severity: Option<String>,
    /// Filter by tags (comma-separated)
    pub tags: Option<String>,
}

/// Parameters for the `cxg_template_info` MCP tool — fetches metadata for a single template.
#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct TemplateInfoRequest {
    /// Template ID to look up
    pub template_id: String,
}

/// Parameters for the `cxg_scan` MCP tool — runs templates against a target.
#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct ScanRequest {
    /// Target: URL, IP, domain, CIDR (e.g. "https://example.com")
    pub scope: String,
    /// Template IDs to run (comma-separated). Omit for all.
    pub templates: Option<String>,
    /// Filter by tags (comma-separated)
    pub tags: Option<String>,
    /// Filter by severity
    pub severity: Option<String>,
    /// Timeout per template in seconds (default: 30)
    pub timeout: Option<u64>,
    /// Additional ports (comma-separated)
    pub ports: Option<String>,
    /// JSON context for parameterised templates. Injected as CERT_X_GEN_CONTEXT env var.
    /// Example: {"auth_token":"Bearer eyJ...","user_id":"6","endpoints":["/api/users/6"]}
    pub context: Option<String>,
    /// Run only templates in this batch group (e.g. "auth-context", "endpoint-params")
    pub batch_group: Option<String>,
}

/// Parameters for the `cxg_template_validate` MCP tool — statically validates template source.
#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct TemplateValidateRequest {
    /// Template source code to validate
    pub code: String,
    /// Language of the template: python, yaml, rust, shell, javascript, c, cpp, java, go, ruby, perl, php
    pub language: String,
    /// Optional filename (helps detect language mismatches)
    pub filename: Option<String>,
}

/// Parameters for the `cxg_template_create` MCP tool — scaffolds a new template skeleton.
#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct TemplateCreateRequest {
    /// Unique template ID (kebab-case, e.g. "graphql-introspection")
    pub id: String,
    /// Programming language for the template
    pub language: String,
    /// Human-readable name (auto-generated from ID if omitted)
    pub name: Option<String>,
}

/// Parameters for the `cxg_template_write` MCP tool — validates and saves a template.
#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct TemplateWriteRequest {
    /// Unique template ID (kebab-case, e.g. "jwt-none-alg-check")
    pub id: String,
    /// Programming language: python, yaml, rust, shell, javascript, c, cpp, java, go, ruby, perl, php
    pub language: String,
    /// Complete template source code (must pass validation before saving)
    pub code: String,
    /// Overwrite if a template with this ID already exists (default: false)
    pub overwrite: Option<bool>,
}

/// Parameters for the `cxg_template_get_notes` MCP tool — returns per-language authoring guidance.
#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct TemplateNotesRequest {
    /// Programming language to get guidance for: python, yaml, rust, shell, javascript, c, cpp, java, go, ruby, perl, php
    pub language: String,
}

/// Parameters for the `cxg_ai_generate` MCP tool — generates a template from a natural-language prompt.
#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct AiGenerateRequest {
    /// Natural language description of what to detect.
    /// Examples: "detect Redis without authentication", "find JWT none algorithm", "check for exposed Memcached"
    pub prompt: String,
    /// Programming language for the template (default: yaml)
    pub language: Option<String>,
    /// LLM provider override: anthropic, openai, deepseek, ollama.
    /// If omitted, uses the configured default provider.
    pub provider: Option<String>,
    /// Model name override (e.g. claude-sonnet-4-20250514, gpt-4o).
    /// If omitted, uses the provider default.
    pub model: Option<String>,
}

/// Parameters for the `cxg_template_test` MCP tool — runs a single template against a target.
#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct TemplateTestRequest {
    /// Template ID to test
    pub template_id: String,
    /// Target to test against (URL, IP, hostname)
    pub target: String,
    /// Timeout in seconds (default: 30)
    pub timeout: Option<u64>,
}

/// Parameters for the `cxg_template_stats` MCP tool — reports template counts.
#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct TemplateStatsRequest {
    /// Optional: only count templates for this language
    pub language: Option<String>,
}

// No params needed for update, but rmcp needs a struct
/// Parameters for the `cxg_template_update` MCP tool — refreshes the official template repository.
#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct TemplateUpdateRequest {
    /// Force update even if already up-to-date
    pub force: Option<bool>,
}

// ─── Response types ──────────────────────────────────────────────────────────

#[derive(Debug, Serialize)]
struct TemplateEntry {
    id: String,
    name: String,
    language: String,
    severity: String,
    description: String,
    tags: Vec<String>,
    cwe: Vec<String>,
    confidence: Option<u8>,
}

#[derive(Debug, Serialize)]
struct TemplateDetail {
    id: String,
    name: String,
    language: String,
    severity: String,
    description: String,
    tags: Vec<String>,
    cwe: Vec<String>,
    cve: Vec<String>,
    cvss: Option<f32>,
    confidence: Option<u8>,
    author: String,
    version: String,
    file_path: String,
    // Parameterisation & routing fields
    context_vars: Vec<String>,
    vuln_class: Option<String>,
    hypothesis_tags: Vec<String>,
    batch_group: Option<String>,
    auto_probe: bool,
}

#[derive(Debug, Serialize)]
struct ScanFinding {
    target: String,
    template_id: String,
    severity: String,
    confidence: u8,
    title: String,
    description: String,
    cwe: Vec<String>,
    cve: Vec<String>,
    evidence_patterns: Vec<String>,
    remediation: Option<String>,
}

#[derive(Debug, Serialize)]
struct ScanOutput {
    scan_id: String,
    targets_scanned: usize,
    templates_executed: usize,
    total_findings: usize,
    findings: Vec<ScanFinding>,
    errors: Vec<String>,
    duration_secs: f64,
}

#[derive(Debug, Serialize)]
struct DiagnosticEntry {
    severity: String,
    code: String,
    message: String,
    line: Option<usize>,
    column: Option<usize>,
    suggestion: Option<String>,
}

// ─── MCP Server ──────────────────────────────────────────────────────────────

/// CERT-X-GEN MCP Server — exposes scanning capabilities via Model Context Protocol
#[derive(Debug, Clone)]
pub struct CxgMcpServer {
    tool_router: ToolRouter<Self>,
}

impl Default for CxgMcpServer {
    fn default() -> Self {
        Self::new()
    }
}

impl CxgMcpServer {
    /// Create a new MCP server instance with the default tool router.
    pub fn new() -> Self {
        Self {
            tool_router: Self::tool_router(),
        }
    }

    async fn load_templates() -> Result<Vec<Box<dyn crate::template::Template>>, String> {
        // Spawn on a new thread with its own runtime because TemplateLoader
        // returns non-Send futures internally. The result (Vec<Box<dyn Template>>)
        // IS Send+Sync so this works fine across the channel.
        Self::run_non_send(|| async {
            let config = Config::default();
            let engine = CertXGen::new(config)
                .await
                .map_err(|e| format!("Failed to init engine: {}", e))?;
            engine
                .load_templates()
                .await
                .map_err(|e| format!("Failed to load templates: {}", e))
        })
        .await
    }

    /// Run an async closure that may contain non-Send futures on a dedicated thread.
    /// The closure's return type must be Send so it can cross the channel.
    async fn run_non_send<F, Fut, T>(f: F) -> Result<T, String>
    where
        F: FnOnce() -> Fut + Send + 'static,
        Fut: std::future::Future<Output = Result<T, String>>,
        T: Send + 'static,
    {
        let (tx, rx) = tokio::sync::oneshot::channel();
        std::thread::spawn(move || {
            let rt = tokio::runtime::Runtime::new().expect("Failed to create runtime");
            let result = rt.block_on(f());
            let _ = tx.send(result);
        });
        rx.await.map_err(|e| format!("Channel error: {}", e))?
    }

    fn parse_language(s: &str) -> Option<TemplateLanguage> {
        match s.to_lowercase().as_str() {
            "yaml" | "yml" => Some(TemplateLanguage::Yaml),
            "python" | "py" => Some(TemplateLanguage::Python),
            "rust" | "rs" => Some(TemplateLanguage::Rust),
            "shell" | "sh" | "bash" => Some(TemplateLanguage::Shell),
            "javascript" | "js" => Some(TemplateLanguage::JavaScript),
            "c" => Some(TemplateLanguage::C),
            "cpp" | "c++" => Some(TemplateLanguage::Cpp),
            "java" => Some(TemplateLanguage::Java),
            "go" | "golang" => Some(TemplateLanguage::Go),
            "ruby" | "rb" => Some(TemplateLanguage::Ruby),
            "perl" | "pl" => Some(TemplateLanguage::Perl),
            "php" => Some(TemplateLanguage::Php),
            _ => None,
        }
    }

    fn parse_severity(s: &str) -> Option<Severity> {
        match s.to_lowercase().as_str() {
            "critical" => Some(Severity::Critical),
            "high" => Some(Severity::High),
            "medium" => Some(Severity::Medium),
            "low" => Some(Severity::Low),
            "info" => Some(Severity::Info),
            _ => None,
        }
    }

    fn parse_target(scope: &str) -> Target {
        if let Ok(url) = url::Url::parse(scope) {
            if let Some(host) = url.host_str() {
                let protocol = match url.scheme().to_lowercase().as_str() {
                    "http" => Protocol::Http,
                    "https" => Protocol::Https,
                    "tcp" => Protocol::Tcp,
                    other => Protocol::Custom(other.to_string()),
                };
                let mut target = Target::new(host, protocol);
                if let Some(port) = url.port() {
                    target.port = Some(port);
                }
                return target;
            }
        }
        if let Some((host, port_str)) = scope.rsplit_once(':') {
            if let Ok(port) = port_str.parse::<u16>() {
                let protocol = match port {
                    80 | 8000 | 8080 => Protocol::Http,
                    443 | 8443 => Protocol::Https,
                    _ => Protocol::Https,
                };
                return Target::with_port(host, port, protocol);
            }
        }
        Target::new(scope, Protocol::Https)
    }

    fn lang_to_ext(lang: &TemplateLanguage) -> &'static str {
        match lang {
            TemplateLanguage::Python => "py",
            TemplateLanguage::Rust => "rs",
            TemplateLanguage::Shell => "sh",
            TemplateLanguage::JavaScript => "js",
            TemplateLanguage::C => "c",
            TemplateLanguage::Cpp => "cpp",
            TemplateLanguage::Java => "java",
            TemplateLanguage::Go => "go",
            TemplateLanguage::Ruby => "rb",
            TemplateLanguage::Perl => "pl",
            TemplateLanguage::Php => "php",
            TemplateLanguage::Yaml => "yaml",
        }
    }
}

// ─── Tool implementations ────────────────────────────────────────────────────

#[tool_router]
impl CxgMcpServer {
    #[tool(
        description = "Search security scanning templates by query, language, severity, or tags. Returns matching templates with relevance scores."
    )]
    async fn cxg_search(
        &self,
        Parameters(req): Parameters<SearchRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        let templates = Self::load_templates()
            .await
            .map_err(|e| ErrorData::internal_error(e, None))?;

        let search_engine = TemplateSearchEngine::new(templates);
        let search_args = SearchArgs {
            query: req.query,
            language: req.language.as_deref().and_then(Self::parse_language),
            severity: req.severity.as_deref().and_then(Self::parse_severity),
            tags: req.tags,
            author: None,
            cwe: None,
            content: false,
            case_sensitive: false,
            regex: false,
            limit: req.limit.unwrap_or(20),
            format: SearchFormat::Json,
            detailed: false,
            sort: SearchSort::Relevance,
            reverse: false,
            ids_only: false,
            stats: false,
        };

        let (results, stats) = search_engine.search(&search_args);
        let output: Vec<serde_json::Value> = results
            .iter()
            .map(|r| {
                serde_json::json!({
                    "id": r.id, "name": r.name,
                    "language": format!("{}", r.language),
                    "severity": format!("{}", r.severity),
                    "description": r.description, "tags": r.tags,
                    "cwe": r.cwe, "relevance": r.relevance_score,
                })
            })
            .collect();

        let json = serde_json::json!({
            "total_available": stats.total_templates,
            "matching": stats.matching_templates,
            "results": output,
        });
        Ok(CallToolResult::success(vec![Content::text(
            json.to_string(),
        )]))
    }

    #[tool(
        description = "List available security scanning templates with optional filters by language, severity, or tags."
    )]
    async fn cxg_template_list(
        &self,
        Parameters(req): Parameters<TemplateListRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        let templates = Self::load_templates()
            .await
            .map_err(|e| ErrorData::internal_error(e, None))?;

        let filtered: Vec<&Box<dyn crate::template::Template>> = templates
            .iter()
            .filter(|t| {
                let m = t.metadata();
                if let Some(ref lang) = req.language {
                    if let Some(parsed) = Self::parse_language(lang) {
                        if m.language != parsed {
                            return false;
                        }
                    }
                }
                if let Some(ref sev) = req.severity {
                    let severities: Vec<&str> = sev.split(',').map(|s| s.trim()).collect();
                    if !severities
                        .iter()
                        .any(|s| Self::parse_severity(s) == Some(m.severity))
                    {
                        return false;
                    }
                }
                if let Some(ref tags) = req.tags {
                    let filter_tags: Vec<&str> = tags.split(',').map(|s| s.trim()).collect();
                    if !filter_tags
                        .iter()
                        .any(|ft| m.tags.iter().any(|t| t.eq_ignore_ascii_case(ft)))
                    {
                        return false;
                    }
                }
                true
            })
            .collect();

        let entries: Vec<TemplateEntry> = filtered
            .iter()
            .map(|t| {
                let m = t.metadata();
                TemplateEntry {
                    id: m.id.clone(),
                    name: m.name.clone(),
                    language: m.language.to_string(),
                    severity: m.severity.to_string(),
                    description: if m.description.len() > 120 {
                        format!("{}...", &m.description[..120])
                    } else {
                        m.description.clone()
                    },
                    tags: m.tags.clone(),
                    cwe: m.cwe_ids.clone(),
                    confidence: m.confidence,
                }
            })
            .collect();

        let json = serde_json::json!({ "total": entries.len(), "templates": entries });
        Ok(CallToolResult::success(vec![Content::text(
            json.to_string(),
        )]))
    }

    #[tool(
        description = "Get detailed info about a specific template by ID. Returns full metadata, CWEs, CVEs, author, and description."
    )]
    async fn cxg_template_info(
        &self,
        Parameters(req): Parameters<TemplateInfoRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        let templates = Self::load_templates()
            .await
            .map_err(|e| ErrorData::internal_error(e, None))?;

        let found = templates.iter().find(|t| {
            let m = t.metadata();
            m.id.eq_ignore_ascii_case(&req.template_id)
                || m.name.eq_ignore_ascii_case(&req.template_id)
        });

        match found {
            Some(template) => {
                let m = template.metadata();
                let detail = TemplateDetail {
                    id: m.id.clone(),
                    name: m.name.clone(),
                    language: m.language.to_string(),
                    severity: m.severity.to_string(),
                    description: m.description.clone(),
                    tags: m.tags.clone(),
                    cwe: m.cwe_ids.clone(),
                    cve: m.cve_ids.clone(),
                    cvss: m.cvss_score,
                    confidence: m.confidence,
                    author: m.author.name.clone(),
                    version: m.version.clone(),
                    file_path: m.file_path.to_string_lossy().to_string(),
                    context_vars: m.context_vars.clone(),
                    vuln_class: m.vuln_class.clone(),
                    hypothesis_tags: m.hypothesis_tags.clone(),
                    batch_group: m.batch_group.clone(),
                    auto_probe: m.auto_probe,
                };
                let json = serde_json::to_string_pretty(&detail)
                    .unwrap_or_else(|e| format!("{{\"error\": \"{}\"}}", e));
                Ok(CallToolResult::success(vec![Content::text(json)]))
            }
            None => {
                let suggestions: Vec<String> = templates
                    .iter()
                    .filter(|t| {
                        let id = t.metadata().id.to_lowercase();
                        let q = req.template_id.to_lowercase();
                        id.contains(&q) || q.contains(&id)
                    })
                    .take(5)
                    .map(|t| t.metadata().id.clone())
                    .collect();
                let json = serde_json::json!({
                    "error": format!("Template '{}' not found", req.template_id),
                    "suggestions": suggestions,
                    "hint": "Use cxg_search or cxg_template_list to find available templates"
                });
                Ok(CallToolResult::success(vec![Content::text(
                    json.to_string(),
                )]))
            }
        }
    }

    #[tool(
        description = "Run a security scan against a target. Executes vulnerability detection templates and returns findings with evidence and remediation. Pass 'context' as a JSON object to inject auth tokens and endpoint lists into parameterised templates (e.g. auth-context batch group). Use 'batch_group' to run only templates sharing a context shape."
    )]
    async fn cxg_scan(
        &self,
        Parameters(req): Parameters<ScanRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        let timeout = req.timeout;
        let scope = req.scope.clone();
        let template_ids = req.templates.clone();
        let tags = req.tags.clone();
        let severity = req.severity.clone();
        let ports = req.ports.clone();
        let context_json = req.context.clone();
        let batch_group = req.batch_group.clone();

        let output = Self::run_non_send(move || async move {
            let mut config = Config::default();
            if let Some(t) = timeout {
                config.network.timeout_secs = t;
                config.templates.timeout_secs = t;
            }

            let engine = CertXGen::new(config).await
                .map_err(|e| format!("Engine init failed: {}", e))?;
            let templates = engine.load_templates().await
                .map_err(|e| format!("Template load failed: {}", e))?;

            if templates.is_empty() {
                return Err("No templates available. Run cxg_template_update first.".to_string());
            }

            let targets: Vec<Target> = scope.split(',').map(|s| Self::parse_target(s.trim())).collect();
            let additional_ports: Vec<u16> = ports.as_deref()
                .map(|p| p.split(',').filter_map(|s| s.trim().parse::<u16>().ok()).collect())
                .unwrap_or_default();

            let mut job = engine.create_scan_job(targets, templates);
            let mut filter = TemplateFilter::new();
            if let Some(ref ids) = template_ids {
                filter.ids = ids.split(',').map(|s| s.trim().to_string()).collect();
            }
            if let Some(ref t) = tags {
                filter.tags = t.split(',').map(|s| s.trim().to_string()).collect();
            }
            if let Some(ref s) = severity {
                filter.severities = s.split(',').filter_map(|sv| Self::parse_severity(sv.trim())).collect();
            }
            if let Some(ref bg) = batch_group {
                filter.batch_group = Some(bg.clone());
            }
            job.filter_templates(&filter);
            if !additional_ports.is_empty() {
                job.context.additional_ports = additional_ports;
            }
            // Inject context variables — same logic as CLI --context flag
            if let Some(ref json_str) = context_json {
                match serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(json_str) {
                    Ok(map) => {
                        for (key, value) in map {
                            let str_value = match value {
                                serde_json::Value::String(s) => s,
                                other => other.to_string(),
                            };
                            job.context.variables.insert(key, str_value);
                        }
                    }
                    Err(e) => {
                        return Err(format!("Invalid context JSON: {}. Expected object like {{\"auth_token\":\"Bearer eyJ...\"}}", e));
                    }
                }
            }

            let template_count = job.templates.len();
            let target_count = job.targets.len();
            let start = std::time::Instant::now();

            let results = engine.execute_scan(job).await
                .map_err(|e| format!("Scan failed: {}", e))?;
            let duration = start.elapsed();

            let findings: Vec<ScanFinding> = results.findings.iter().map(|f| ScanFinding {
                target: f.target.clone(), template_id: f.template_id.clone(),
                severity: f.severity.to_string(), confidence: f.confidence,
                title: f.title.clone(), description: f.description.clone(),
                cwe: f.cwe_ids.clone(), cve: f.cve_ids.clone(),
                evidence_patterns: f.evidence.matched_patterns.clone(),
                remediation: f.remediation.clone(),
            }).collect();

            Ok(ScanOutput {
                scan_id: results.scan_id.to_string(),
                targets_scanned: target_count, templates_executed: template_count,
                total_findings: findings.len(), findings,
                errors: results.errors, duration_secs: duration.as_secs_f64(),
            })
        }).await.map_err(|e| ErrorData::internal_error(e, None))?;

        let json = serde_json::to_string_pretty(&output)
            .unwrap_or_else(|e| format!("{{\"error\": \"{}\"}}", e));
        Ok(CallToolResult::success(vec![Content::text(json)]))
    }

    // ─── New tools ───────────────────────────────────────────────────────────

    #[tool(
        description = "Validate template source code. Runs 12-language syntax checker, pattern analysis, and schema validation. Returns diagnostics with severity, line numbers, and fix suggestions."
    )]
    async fn cxg_template_validate(
        &self,
        Parameters(req): Parameters<TemplateValidateRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        use crate::ai::validator::{DiagnosticSeverity, TemplateValidator};

        let language = Self::parse_language(&req.language).ok_or_else(|| {
            ErrorData::invalid_params(
                format!("Unknown language '{}'. Supported: yaml, python, rust, shell, javascript, c, cpp, java, go, ruby, perl, php", req.language),
                None,
            )
        })?;

        let validator = TemplateValidator::new();
        let filename = req
            .filename
            .as_ref()
            .map(|f| std::path::Path::new(f.as_str()));

        let diagnostics = validator
            .validate_with_diagnostics(&req.code, language, filename)
            .map_err(|e| ErrorData::internal_error(format!("Validation failed: {}", e), None))?;

        let entries: Vec<DiagnosticEntry> = diagnostics
            .iter()
            .map(|d| DiagnosticEntry {
                severity: match d.severity {
                    DiagnosticSeverity::Error => "error".to_string(),
                    DiagnosticSeverity::Warning => "warning".to_string(),
                    DiagnosticSeverity::Info => "info".to_string(),
                },
                code: d.code.clone(),
                message: d.message.clone(),
                line: d.line,
                column: d.column,
                suggestion: None,
            })
            .collect();

        let errors = entries.iter().filter(|d| d.severity == "error").count();
        let warnings = entries.iter().filter(|d| d.severity == "warning").count();

        let json = serde_json::json!({
            "valid": errors == 0,
            "errors": errors,
            "warnings": warnings,
            "total_diagnostics": entries.len(),
            "diagnostics": entries,
        });
        Ok(CallToolResult::success(vec![Content::text(
            json.to_string(),
        )]))
    }

    #[tool(
        description = "Create a new template from scaffold with proper metadata headers. Returns the boilerplate code for the specified language. The agent can then customize the detection logic."
    )]
    async fn cxg_template_create(
        &self,
        Parameters(req): Parameters<TemplateCreateRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        let language = Self::parse_language(&req.language).ok_or_else(|| {
            ErrorData::invalid_params(
                format!("Unknown language '{}'. Supported: yaml, python, rust, shell, javascript, c, cpp, java, go, ruby, perl, php", req.language),
                None,
            )
        })?;

        let ext = Self::lang_to_ext(&language);
        let skeleton_name = format!(
            "{}-template-skeleton.{}",
            match language {
                TemplateLanguage::Python => "python",
                TemplateLanguage::Rust => "rust",
                TemplateLanguage::Shell => "shell",
                TemplateLanguage::JavaScript => "javascript",
                TemplateLanguage::C => "c",
                TemplateLanguage::Cpp => "cpp",
                TemplateLanguage::Java => "java",
                TemplateLanguage::Go => "go",
                TemplateLanguage::Ruby => "ruby",
                TemplateLanguage::Perl => "perl",
                TemplateLanguage::Php => "php",
                TemplateLanguage::Yaml => "yaml",
            },
            ext
        );

        // Search skeleton in known locations
        let skeleton_paths = [
            PathBuf::from("templates/skeleton").join(&skeleton_name),
            dirs::home_dir()
                .unwrap_or_default()
                .join(".cert-x-gen/templates/official/templates/skeleton")
                .join(&skeleton_name),
        ];

        let skeleton_content = skeleton_paths
            .iter()
            .find_map(|p| std::fs::read_to_string(p).ok());

        let template_name = req.name.unwrap_or_else(|| {
            req.id
                .split('-')
                .map(|word| {
                    let mut chars = word.chars();
                    match chars.next() {
                        Some(c) => c.to_uppercase().chain(chars).collect(),
                        None => String::new(),
                    }
                })
                .collect::<Vec<_>>()
                .join(" ")
        });

        let code = match skeleton_content {
            Some(skeleton) => skeleton
                .replace("template-skeleton", &req.id)
                .replace("Template Skeleton", &template_name),
            None => {
                // Generate minimal boilerplate if no skeleton found
                self.generate_minimal_scaffold(&req.id, &template_name, &language)
            }
        };

        let filename = format!("{}.{}", req.id, ext);
        let json = serde_json::json!({
            "template_id": req.id,
            "language": format!("{}", language),
            "filename": filename,
            "code": code,
            "hint": "Customize the detection logic, then use cxg_template_validate to check it, or cxg_template_write to validate and save in one step."
        });
        Ok(CallToolResult::success(vec![Content::text(
            json.to_string(),
        )]))
    }

    #[tool(
        description = "Validate and save a completed template atomically. Runs the full 12-language validator first — if any errors are found the file is NOT written and diagnostics are returned for the agent to fix. On success, saves to ~/.cert-x-gen/templates/agent-created/<id>.<ext> and returns the saved path. Use this after cxg_template_create + writing detection logic. Prefer this over cxg_template_validate + manual save to guarantee no broken template ever touches disk."
    )]
    async fn cxg_template_write(
        &self,
        Parameters(req): Parameters<TemplateWriteRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        use crate::ai::validator::{DiagnosticSeverity, TemplateValidator};

        let language = Self::parse_language(&req.language).ok_or_else(|| {
            ErrorData::invalid_params(
                format!("Unknown language '{}'. Supported: yaml, python, rust, shell, javascript, c, cpp, java, go, ruby, perl, php", req.language),
                None,
            )
        })?;

        // --- Step 1: validate ---
        let validator = TemplateValidator::new();
        let ext = Self::lang_to_ext(&language);
        let filename = format!("{}.{}", req.id, ext);
        let filepath = std::path::Path::new(&filename);

        let diagnostics = validator
            .validate_with_diagnostics(&req.code, language, Some(filepath))
            .map_err(|e| ErrorData::internal_error(format!("Validation failed: {}", e), None))?;

        let errors: Vec<_> = diagnostics
            .iter()
            .filter(|d| matches!(d.severity, DiagnosticSeverity::Error))
            .collect();
        let warnings: Vec<_> = diagnostics
            .iter()
            .filter(|d| matches!(d.severity, DiagnosticSeverity::Warning))
            .collect();

        // Block save if there are errors
        if !errors.is_empty() {
            let diag_entries: Vec<serde_json::Value> = diagnostics
                .iter()
                .map(|d| {
                    serde_json::json!({
                        "severity": match d.severity {
                            DiagnosticSeverity::Error   => "error",
                            DiagnosticSeverity::Warning => "warning",
                            DiagnosticSeverity::Info    => "info",
                        },
                        "code":    d.code,
                        "message": d.message,
                        "line":    d.line,
                        "column":  d.column,
                    })
                })
                .collect();

            let json = serde_json::json!({
                "saved": false,
                "reason": "Template has validation errors — fix them and retry cxg_template_write",
                "errors":   errors.len(),
                "warnings": warnings.len(),
                "diagnostics": diag_entries,
            });
            return Ok(CallToolResult::success(vec![Content::text(
                json.to_string(),
            )]));
        }

        // --- Step 2: resolve save path ---
        let save_dir = dirs::home_dir()
            .ok_or_else(|| {
                ErrorData::internal_error("Cannot determine home directory".to_string(), None)
            })?
            .join(".cert-x-gen")
            .join("templates")
            .join("agent-created");

        std::fs::create_dir_all(&save_dir).map_err(|e| {
            ErrorData::internal_error(format!("Cannot create save directory: {}", e), None)
        })?;

        let save_path = save_dir.join(&filename);

        // Overwrite guard
        if save_path.exists() && !req.overwrite.unwrap_or(false) {
            let json = serde_json::json!({
                "saved": false,
                "reason": format!("Template '{}' already exists at {}. Pass overwrite: true to replace it.", req.id, save_path.display()),
                "path": save_path.to_string_lossy(),
            });
            return Ok(CallToolResult::success(vec![Content::text(
                json.to_string(),
            )]));
        }

        // --- Step 3: write ---
        std::fs::write(&save_path, &req.code).map_err(|e| {
            ErrorData::internal_error(format!("Failed to write template: {}", e), None)
        })?;

        let json = serde_json::json!({
            "saved": true,
            "template_id": req.id,
            "language": format!("{}", language),
            "path": save_path.to_string_lossy(),
            "warnings": warnings.len(),
            "hint": if warnings.is_empty() {
                "Template saved. Use cxg_template_test to verify it detects correctly against a live target."
            } else {
                "Template saved with warnings. Review diagnostics — warnings won't break execution but may indicate quality issues."
            },
            "diagnostics": diagnostics.iter()
                .filter(|d| matches!(d.severity, DiagnosticSeverity::Warning))
                .map(|d| serde_json::json!({
                    "severity": "warning",
                    "code": d.code,
                    "message": d.message,
                    "line": d.line,
                }))
                .collect::<Vec<_>>(),
        });
        Ok(CallToolResult::success(vec![Content::text(
            json.to_string(),
        )]))
    }

    #[tool(
        description = "Get the AI generation guide for a specific template language. Returns the full guidance document covering metadata format, required fields, runtime environment variables (CERT_X_GEN_TARGET_HOST, CERT_X_GEN_CONTEXT, etc.), output JSON contract, validation rules, and a complete working example. Call this before cxg_template_create to understand what a correct template looks like in your chosen language."
    )]
    async fn cxg_template_get_notes(
        &self,
        Parameters(req): Parameters<TemplateNotesRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        let language = Self::parse_language(&req.language).ok_or_else(|| {
            ErrorData::invalid_params(
                format!("Unknown language '{}'. Supported: yaml, python, rust, shell, javascript, c, cpp, java, go, ruby, perl, php", req.language),
                None,
            )
        })?;

        let lang_name = match language {
            TemplateLanguage::Python => "python",
            TemplateLanguage::Rust => "rust",
            TemplateLanguage::Shell => "shell",
            TemplateLanguage::JavaScript => "javascript",
            TemplateLanguage::C => "c",
            TemplateLanguage::Cpp => "cpp",
            TemplateLanguage::Java => "java",
            TemplateLanguage::Go => "go",
            TemplateLanguage::Ruby => "ruby",
            TemplateLanguage::Perl => "perl",
            TemplateLanguage::Php => "php",
            TemplateLanguage::Yaml => "yaml",
        };

        let notes_filename = format!("{}-template-ai-notes.md", lang_name);
        let notes_paths = [
            PathBuf::from("templates/skeleton").join(&notes_filename),
            dirs::home_dir()
                .unwrap_or_default()
                .join(".cert-x-gen/templates/official/templates/skeleton")
                .join(&notes_filename),
        ];

        let notes = notes_paths.iter()
            .find_map(|p| std::fs::read_to_string(p).ok())
            .ok_or_else(|| ErrorData::internal_error(
                format!("AI notes for '{}' not found. Run cxg_template_update to fetch the latest templates.", lang_name),
                None,
            ))?;

        let json = serde_json::json!({
            "language": lang_name,
            "notes_file": notes_filename,
            "content": notes,
            "hint": "Read these notes carefully before writing template code. Then call cxg_template_create to get the skeleton, fill in the detection logic, and use cxg_template_write to validate and save.",
        });
        Ok(CallToolResult::success(vec![Content::text(
            json.to_string(),
        )]))
    }

    #[tool(
        description = "Generate a security template from a natural language prompt. Dual-mode: if an LLM provider is configured with an API key, generates the template internally and returns the code ready to save with cxg_template_write. If no provider is configured, returns the generation prompt + skeleton + ai-notes for the calling agent to generate directly — the agent should then call cxg_template_write with the result."
    )]
    async fn cxg_ai_generate(
        &self,
        Parameters(req): Parameters<AiGenerateRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        use crate::ai::manager::AIManager;
        use crate::ai::prompt::PromptBuilder;
        use crate::types::TemplateLanguage;

        let language = req
            .language
            .as_deref()
            .and_then(Self::parse_language)
            .unwrap_or(TemplateLanguage::Yaml);

        let lang_name = match language {
            TemplateLanguage::Python => "python",
            TemplateLanguage::Rust => "rust",
            TemplateLanguage::Shell => "shell",
            TemplateLanguage::JavaScript => "javascript",
            TemplateLanguage::C => "c",
            TemplateLanguage::Cpp => "cpp",
            TemplateLanguage::Java => "java",
            TemplateLanguage::Go => "go",
            TemplateLanguage::Ruby => "ruby",
            TemplateLanguage::Perl => "perl",
            TemplateLanguage::Php => "php",
            TemplateLanguage::Yaml => "yaml",
        };

        let _prompt = req.prompt.clone();
        let provider = req.provider.clone();

        // --- Check whether a provider is usable ---
        let manager_result: Result<_, String> =
            Self::run_non_send(move || async move { AIManager::new().map_err(|e| e.to_string()) })
                .await;

        let api_available = match &manager_result {
            Ok(manager) => {
                let p = provider
                    .as_deref()
                    .unwrap_or_else(|| manager.config().default_provider_name());
                manager.config().is_provider_enabled(p)
                    && manager
                        .config()
                        .get_provider(p)
                        .and_then(|pc| pc.api_key.as_ref())
                        .map(|k| !k.is_empty() && !k.starts_with("${"))
                        .unwrap_or(false)
            }
            Err(_) => false,
        };

        if api_available {
            // ── API mode: generate internally ──────────────────────────────
            let prompt2 = req.prompt.clone();
            let provider2 = req.provider.clone();

            let result = Self::run_non_send(move || async move {
                let manager =
                    AIManager::new().map_err(|e| format!("AIManager init failed: {}", e))?;
                let code = manager
                    .generate_template(&prompt2, language, provider2.as_deref())
                    .await
                    .map_err(|e| format!("Generation failed: {}", e))?;
                Ok::<String, String>(code)
            })
            .await
            .map_err(|e| ErrorData::internal_error(e, None))?;

            // Suggest a save ID from the prompt
            let save_id = req
                .prompt
                .to_lowercase()
                .split_whitespace()
                .take(5)
                .collect::<Vec<_>>()
                .join("-")
                .chars()
                .filter(|c| c.is_alphanumeric() || *c == '-')
                .collect::<String>();

            let json = serde_json::json!({
                "mode": "api",
                "language": lang_name,
                "code": result,
                "suggested_id": save_id,
                "hint": "Template generated. Call cxg_template_write with this code to validate and save it, then cxg_template_test to verify it detects correctly.",
            });
            return Ok(CallToolResult::success(vec![Content::text(
                json.to_string(),
            )]));
        }

        // ── Agent mode: no API key — return prompt + skeleton + notes ──────
        // Build the same generation prompt the CLI uses
        let builder = PromptBuilder::new();
        let generation_prompt = builder.build_generation_prompt(&req.prompt, language);

        // Load skeleton
        let skeleton_name = format!(
            "{}-template-skeleton.{}",
            lang_name,
            Self::lang_to_ext(&language)
        );
        let skeleton_paths = [
            PathBuf::from("templates/skeleton").join(&skeleton_name),
            dirs::home_dir()
                .unwrap_or_default()
                .join(".cert-x-gen/templates/official/templates/skeleton")
                .join(&skeleton_name),
        ];
        let skeleton = skeleton_paths
            .iter()
            .find_map(|p| std::fs::read_to_string(p).ok())
            .unwrap_or_default();

        // Load notes
        let notes_name = format!("{}-template-ai-notes.md", lang_name);
        let notes_paths = [
            PathBuf::from("templates/skeleton").join(&notes_name),
            dirs::home_dir()
                .unwrap_or_default()
                .join(".cert-x-gen/templates/official/templates/skeleton")
                .join(&notes_name),
        ];
        let notes = notes_paths
            .iter()
            .find_map(|p| std::fs::read_to_string(p).ok())
            .unwrap_or_default();

        let save_id = req
            .prompt
            .to_lowercase()
            .split_whitespace()
            .take(5)
            .collect::<Vec<_>>()
            .join("-")
            .chars()
            .filter(|c| c.is_alphanumeric() || *c == '-')
            .collect::<String>();

        let json = serde_json::json!({
            "mode": "agent",
            "message": "No LLM API key configured. Use the prompt, skeleton, and notes below to generate the template directly, then call cxg_template_write to validate and save it.",
            "language": lang_name,
            "suggested_id": save_id,
            "generation_prompt": generation_prompt,
            "skeleton": skeleton,
            "notes": notes,
            "save_hint": format!("After generating, call cxg_template_write with id='{}', language='{}', code=<generated code>", save_id, lang_name),
        });
        Ok(CallToolResult::success(vec![Content::text(
            json.to_string(),
        )]))
    }

    #[tool(
        description = "Test a specific template against a target. More targeted than cxg_scan — runs a single template and returns detailed results."
    )]
    async fn cxg_template_test(
        &self,
        Parameters(req): Parameters<TemplateTestRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        let template_id = req.template_id.clone();
        let target_str = req.target.clone();
        let timeout = req.timeout;

        let result = Self::run_non_send(move || async move {
            let mut config = Config::default();
            if let Some(t) = timeout {
                config.network.timeout_secs = t;
                config.templates.timeout_secs = t;
            }

            let engine = CertXGen::new(config)
                .await
                .map_err(|e| format!("Engine init failed: {}", e))?;
            let templates = engine
                .load_templates()
                .await
                .map_err(|e| format!("Template load failed: {}", e))?;

            // Check template exists
            if !templates
                .iter()
                .any(|t| t.metadata().id.eq_ignore_ascii_case(&template_id))
            {
                let suggestions: Vec<String> = templates
                    .iter()
                    .filter(|t| {
                        t.metadata()
                            .id
                            .to_lowercase()
                            .contains(&template_id.to_lowercase())
                    })
                    .take(5)
                    .map(|t| t.metadata().id.clone())
                    .collect();
                return Err(serde_json::json!({
                    "error": format!("Template '{}' not found", template_id),
                    "suggestions": suggestions,
                })
                .to_string());
            }

            let target = Self::parse_target(&target_str);
            let mut job = engine.create_scan_job(vec![target], templates);
            let mut filter = TemplateFilter::new();
            filter.ids = vec![template_id.clone()];
            job.filter_templates(&filter);

            let start = std::time::Instant::now();
            let results = engine
                .execute_scan(job)
                .await
                .map_err(|e| format!("Test failed: {}", e))?;
            let duration = start.elapsed();

            let findings: Vec<ScanFinding> = results
                .findings
                .iter()
                .map(|f| ScanFinding {
                    target: f.target.clone(),
                    template_id: f.template_id.clone(),
                    severity: f.severity.to_string(),
                    confidence: f.confidence,
                    title: f.title.clone(),
                    description: f.description.clone(),
                    cwe: f.cwe_ids.clone(),
                    cve: f.cve_ids.clone(),
                    evidence_patterns: f.evidence.matched_patterns.clone(),
                    remediation: f.remediation.clone(),
                })
                .collect();

            Ok(serde_json::json!({
                "template_id": template_id,
                "target": target_str,
                "findings_count": findings.len(),
                "findings": findings,
                "errors": results.errors,
                "duration_secs": duration.as_secs_f64(),
            }))
        })
        .await;

        match result {
            Ok(json) => Ok(CallToolResult::success(vec![Content::text(
                json.to_string(),
            )])),
            Err(e) => Ok(CallToolResult::error(vec![Content::text(e)])),
        }
    }

    #[tool(
        description = "Get template collection statistics — total count, breakdown by language and severity. Useful for understanding what checks are available."
    )]
    async fn cxg_template_stats(
        &self,
        Parameters(req): Parameters<TemplateStatsRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        let templates = Self::load_templates()
            .await
            .map_err(|e| ErrorData::internal_error(e, None))?;

        let mut by_language: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();
        let mut by_severity: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();
        let mut all_tags: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();

        for t in &templates {
            let m = t.metadata();

            // Apply language filter if specified
            if let Some(ref lang) = req.language {
                if let Some(parsed) = Self::parse_language(lang) {
                    if m.language != parsed {
                        continue;
                    }
                }
            }

            *by_language.entry(m.language.to_string()).or_insert(0) += 1;
            *by_severity.entry(m.severity.to_string()).or_insert(0) += 1;
            for tag in &m.tags {
                *all_tags.entry(tag.clone()).or_insert(0) += 1;
            }
        }

        // Sort tags by frequency
        let mut top_tags: Vec<_> = all_tags.into_iter().collect();
        top_tags.sort_by(|a, b| b.1.cmp(&a.1));
        let top_tags: Vec<_> = top_tags.into_iter().take(20).collect();

        let total = by_language.values().sum::<usize>();

        let json = serde_json::json!({
            "total_templates": total,
            "by_language": by_language,
            "by_severity": by_severity,
            "top_tags": top_tags,
        });
        Ok(CallToolResult::success(vec![Content::text(
            json.to_string(),
        )]))
    }

    #[tool(
        description = "Update templates from remote repository. Downloads latest security checks. Run this if cxg_scan reports no templates available."
    )]
    async fn cxg_template_update(
        &self,
        Parameters(_req): Parameters<TemplateUpdateRequest>,
    ) -> Result<CallToolResult, ErrorData> {
        use crate::template::AutoUpdater;

        let mut updater = AutoUpdater::new()
            .map_err(|e| ErrorData::internal_error(format!("Updater init failed: {}", e), None))?;

        if updater.needs_initial_install() {
            updater
                .auto_install()
                .map_err(|e| ErrorData::internal_error(format!("Install failed: {}", e), None))?;

            let stats = updater.get_stats();
            let json = serde_json::json!({
                "action": "initial_install",
                "success": true,
                "summary": stats.summary(),
                "total_templates": stats.total,
                "by_language": stats.by_language,
            });
            Ok(CallToolResult::success(vec![Content::text(
                json.to_string(),
            )]))
        } else {
            updater
                .perform_update()
                .map_err(|e| ErrorData::internal_error(format!("Update failed: {}", e), None))?;

            let stats = updater.get_stats();
            let json = serde_json::json!({
                "action": "update",
                "success": true,
                "summary": stats.summary(),
                "total_templates": stats.total,
                "by_language": stats.by_language,
            });
            Ok(CallToolResult::success(vec![Content::text(
                json.to_string(),
            )]))
        }
    }
}

// ─── Scaffold helper ─────────────────────────────────────────────────────────

impl CxgMcpServer {
    fn generate_minimal_scaffold(
        &self,
        id: &str,
        name: &str,
        language: &TemplateLanguage,
    ) -> String {
        match language {
            TemplateLanguage::Python => format!(r#"#!/usr/bin/env python3
# id: {id}
# name: {name}
# author: AI Agent
# severity: medium
# description: {name} detection template
# tags: custom
# language: python
# confidence: 70

import requests
import json
import sys

def scan(target: str) -> list:
    """Scan target for {name}."""
    findings = []

    try:
        url = target if target.startswith("http") else f"https://{{target}}"
        response = requests.get(url, timeout=10, verify=False)

        # TODO: Add detection logic here
        # Example:
        # if "vulnerable_pattern" in response.text:
        #     findings.append({{
        #         "title": "{name}",
        #         "severity": "medium",
        #         "confidence": 80,
        #         "description": "Detected {name}",
        #         "evidence": {{"matched": "vulnerable_pattern"}},
        #     }})

    except Exception as e:
        print(f"Error: {{e}}", file=sys.stderr)

    return findings

if __name__ == "__main__":
    target = sys.argv[1] if len(sys.argv) > 1 else "localhost"
    results = scan(target)
    print(json.dumps(results, indent=2))
"#),
            TemplateLanguage::Yaml => format!(r#"id: {id}
info:
  name: {name}
  author: AI Agent
  severity: medium
  description: |
    {name} detection template.
  tags:
    - custom

http:
  - method: GET
    path:
      - "/{{{{BaseURL}}}}"
    matchers:
      - type: word
        words:
          - "TODO_MATCH_PATTERN"
        condition: and
"#),
            _ => format!("// id: {id}\n// name: {name}\n// language: {}\n// TODO: Implement detection logic\n",
                language),
        }
    }
}

// ─── ServerHandler ───────────────────────────────────────────────────────────

#[tool_handler]
impl ServerHandler for CxgMcpServer {
    fn get_info(&self) -> ServerInfo {
        ServerInfo {
            instructions: Some(
                "CERT-X-GEN: Polyglot scanner/template tools; the deeper browser/runtime \
                 engine also handles supplied JS probes selected by plain CLI scan. \
                 Explicit pentest controls use CLI scan --pentest or pentest run. \
                 For everyday changes: keep GuardLink \
                 annotations current, scope guardlink translate to the change (authoring only; \
                 no browser session or Bravos ledger), then review generated code. \
                 For authoring here, read cxg_template_get_notes before cxg_template_create, \
                 or use cxg_ai_generate; these produce code, not findings. \
                 cxg_template_validate is static validation, not execution or proof. \
                 cxg_template_write validates and saves reviewed code to the home \
                 agent-created collection. Inspect actual installed inventory with \
                 cxg_template_list, cxg_search, cxg_template_info, or cxg_template_stats; \
                 detection templates are distributed separately, with cxg_template_update \
                 fetching the library. cxg_scan and cxg_template_test execute installed \
                 templates as the invoking user against an explicitly authorized target. \
                 They do not accept a repository template-directory parameter: for reviewed \
                 translated scanner templates select an existing reviewed file by path: \
                 `cxg scan --scope <target> --templates .guardlink/cxg-templates/reviewed-check.py`. \
                 These MCP execution tools handle scanner contracts; use plain CLI scan \
                 for supplied browser probes. Explicit engine replay supports --no-author. \
                 Do not combine --template-dir with --templates <id> for repository files. \
                 Create the report parent directory before using --output (for example, \
                 `mkdir -p .guardlink/pentest-findings/`). Alternatively, \
                 explicitly review and install code with cxg_template_write before testing \
                 its installed id. Retain regression tests; before review run the repository's \
                 root security/ suite with its configured runner and report failures, skips, \
                 missing prerequisites, and coverage. Independent results create no Bravos \
                 ledger verdict or managed approval record."
                    .to_string(),
            ),
            capabilities: ServerCapabilities::builder().enable_tools().build(),
            ..Default::default()
        }
    }
}