ramparts 0.8.2

Security scanner for Model Context Protocol (MCP) servers and AI agent skills (Claude Code commands, agentskills.io bundles, Cursor / Codex / Windsurf / Gemini equivalents).
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
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
use crate::config::{self, MCPConfig, MCPConfigManager, MCPServerConfig, ScannerConfig};
use crate::constants::{messages, protocol};
use crate::mcp_client::McpClient;
use crate::security::{
    cross_origin_scanner::CrossOriginScanner, BatchScannableItem, SecurityScanResult,
    SecurityScanner,
};
use crate::types::{
    LlmPrompt, MCPPrompt, MCPResource, MCPServerInfo, MCPTool, ScanOptions, ScanResult, ScanStatus,
    YaraScanResult,
};
use crate::utils::{
    error_utils, error_utils::format_error_chain, performance::track_performance, Timer,
};
use anyhow::{anyhow, Result};
use reqwest::Client;
use std::collections::HashMap;
use std::path::Path;
use tokio::time::{timeout, Duration};
use tracing::{debug, warn};

#[cfg(feature = "yara-x-scanning")]
use yara_x::Rules;

#[cfg(feature = "yara-x-scanning")]
type YaraRules = Rules;

#[cfg(not(feature = "yara-x-scanning"))]
type YaraRules = ();

/// Map rule names to their file names for consistent naming
fn rule_name_to_file_name(rule_name: &str) -> Option<String> {
    match rule_name {
        // secrets_leakage.yar rules
        "SecretsLeakage" | "SSHKeyExposure" | "PEMFileAccess" | "EnvironmentVariableLeakage" => {
            Some("secrets_leakage".to_string())
        }
        // cross_origin_escalation.yar rules
        "CrossOriginEscalation"
        | "CrossDomainContamination"
        | "DomainOutlier"
        | "MixedSecuritySchemes" => Some("cross_origin_escalation".to_string()),
        // skill_prompt_injection.yar rules
        "PromptInjectionSignature"
        | "UnicodeSteganography"
        | "CoerciveInjection"
        | "IndirectPromptInjection" => Some("skill_prompt_injection".to_string()),
        // skill_authority.yar rules
        "AutonomyAbuse" | "CapabilityInflation" => Some("skill_authority".to_string()),
        // skill_credential_harvesting.yar
        "SkillCredentialHarvesting" => Some("skill_credential_harvesting".to_string()),
        // skill_tool_chaining_abuse.yar
        "SkillToolChainingExfiltration" => Some("skill_tool_chaining_abuse".to_string()),
        // skill_system_manipulation.yar
        "SkillSystemManipulation" => Some("skill_system_manipulation".to_string()),
        // NOTE: agentskills.io validation findings (AgentskillsNameMismatch,
        // AgentskillsInvalidName, AgentskillsMissingName,
        // AgentskillsUnknownFrontmatterField) are NOT mapped here. They're
        // synthesized in `src/skills.rs::make_heuristic_finding`, which
        // hard-codes `rule_file = "skill_parser"` on construction; this
        // mapping is only consulted for YARA-scan results, so any entry
        // would be dead code. Same goes for the other skill heuristics
        // (OverbroadAllowedTools, VagueSkillTrigger, etc.).
        _ => None,
    }
}

/// Generate descriptive context messages based on rule names
fn generate_context_message(item_type: &str, rule_name: &str) -> String {
    match rule_name {
        // Secrets leakage rules
        "SecretsLeakage" => format!("Potential secret exposure detected in {item_type}"),
        "SSHKeyExposure" => format!("SSH key or configuration file access detected in {item_type}"),
        "PEMFileAccess" => format!("PEM certificate or private key access detected in {item_type}"),
        "EnvironmentVariableLeakage" => {
            format!("Sensitive environment variable pattern detected in {item_type}")
        }

        // Cross-origin rules
        "CrossOriginEscalation" => {
            format!("Cross-origin escalation vulnerability detected in {item_type}")
        }
        "CrossDomainContamination" => {
            format!("Cross-domain contamination detected across multiple domains in {item_type}")
        }
        "DomainOutlier" => {
            format!("Domain outlier detected - {item_type} uses different domain than majority")
        }
        "MixedSecuritySchemes" => format!("Mixed HTTP/HTTPS schemes detected in {item_type}"),

        // Command injection rules
        "CommandInjection" => format!("Command injection vulnerability detected in {item_type}"),

        // Default fallback
        _ => format!("{item_type} matched by security rule {rule_name}"),
    }
}

/// Check if YARA is available and enabled
fn is_yara_available(config_enabled: bool) -> bool {
    if !config_enabled {
        return false;
    }

    #[cfg(feature = "yara-x-scanning")]
    {
        true
    }

    #[cfg(not(feature = "yara-x-scanning"))]
    {
        false
    }
}

/// Print friendly message about YARA installation
fn print_yara_install_message() {
    println!("📋 YARA-X Scanning Disabled");
    println!();
    println!("YARA-X rule scanning is enabled in your config but YARA-X is not available.");
    println!("To enable YARA-X scanning, please:");
    println!();
    println!("1. Reinstall ramparts with YARA-X support:");
    println!("   cargo install ramparts --force");
    println!();
    println!("2. Or disable YARA-X in your config.yaml:");
    println!("   scanner:");
    println!("     enable_yara: false");
    println!();
    println!("Continuing without YARA-X scanning...");
    println!();
}

// ============================================================================
// TRANSPORT LAYER - Support for multiple MCP transport mechanisms
// ============================================================================

// ============================================================================
// SCAN CAPABILITIES - Middleware-like system for extensible scanning
// ============================================================================

// =====================
// CAPABILITY TRAIT & CHAIN (Composable Middleware)
// =====================

/// Represents the phase in which a scan capability is executed.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ScanPhase {
    /// Pre-scan phase: runs before the main security scan.
    PreScan,
    /// Post-scan phase: runs after the main security scan.
    PostScan,
}

/// Trait for a composable scan capability (middleware hook).
///
/// Implement this trait to add custom security, analysis, or filtering logic
/// to the scan pipeline. Scanners can be registered for the pre-scan phase.
pub trait Scanner: Send + Sync {
    /// Returns the name of the capability.
    fn name(&self) -> &'static str;
    /// Returns the phase in which this capability should run.
    fn phase(&self) -> ScanPhase;
    /// Runs the capability, modifying scan data or reporting issues as needed.
    fn run(&self, _scan_data: &mut ScanData) -> anyhow::Result<()>;
    /// Clones the scanner as a boxed trait object.
    fn box_clone(&self) -> Box<dyn Scanner>;
}

/// Chain of scanners (middleware hooks) for pre-scan processing.
///
/// Scanners are executed in the order they are added.
#[derive(Default)]
pub struct ScannerChain {
    pre_scan: Vec<Box<dyn Scanner>>,
    post_scan: Vec<Box<dyn Scanner>>,
}

impl ScannerChain {
    /// Creates a new, empty scanner chain.
    pub fn new() -> Self {
        Self {
            pre_scan: Vec::new(),
            post_scan: Vec::new(),
        }
    }
    /// Adds a scanner to the chain for its specified phase.
    pub fn add(&mut self, scanner: Box<dyn Scanner>) {
        match scanner.phase() {
            ScanPhase::PreScan => self.pre_scan.push(scanner),
            ScanPhase::PostScan => self.post_scan.push(scanner),
        }
    }
    /// Runs all pre-scan scanners on the provided scan data.
    pub fn run_pre_scan(&self, scan_data: &mut ScanData) {
        for scanner in &self.pre_scan {
            if let Err(e) = scanner.run(scan_data) {
                tracing::warn!("Pre-scan scanner '{}' failed: {}", scanner.name(), e);
            }
        }
    }

    /// Runs all post-scan scanners on the provided scan data.
    pub fn run_post_scan(&self, scan_data: &mut ScanData) {
        for scanner in &self.post_scan {
            if let Err(e) = scanner.run(scan_data) {
                tracing::warn!("Post-scan scanner '{}' failed: {}", scanner.name(), e);
            }
        }
    }
}

// Implement Clone for ScannerChain (requires dyn-clone for trait objects)
impl Clone for ScannerChain {
    fn clone(&self) -> Self {
        Self {
            pre_scan: self.pre_scan.iter().map(|c| c.box_clone()).collect(),
            post_scan: self.post_scan.iter().map(|c| c.box_clone()).collect(),
        }
    }
}

// =====================
// THREAT RULES ENGINE (Replaces hardcoded YaraScanner)
// =====================

#[cfg(feature = "yara-x-scanning")]
use glob::glob;
use std::sync::Arc;

/// Enhanced YARA match with rule metadata
#[cfg(feature = "yara-x-scanning")]
pub struct YaraMatchInfo {
    pub rule_name: String,
    pub metadata: Option<crate::types::YaraRuleMetadata>,
}

/// Enhanced YARA match with rule metadata (non-YARA fallback)
#[cfg(not(feature = "yara-x-scanning"))]
pub struct YaraMatchInfo {
    pub rule_name: String,
    pub metadata: Option<crate::types::YaraRuleMetadata>,
}

/// Threat detection rules engine that loads YARA-X rules from directory structure
pub struct ThreatRules {
    pre_scan_rules: Vec<Arc<YaraRules>>,
    post_scan_rules: Vec<Arc<YaraRules>>,
    rules_dir: String,
    rule_metadata: HashMap<String, RuleMetadata>,
    memory_usage_bytes: usize,
    last_load_time: std::time::Instant,
}

/// Metadata for YARA rules
#[derive(Debug, Clone)]
pub struct RuleMetadata {
    pub name: String,
}

impl ThreatRules {
    /// Creates a new threat rules engine with rules from the specified directory
    pub fn new(rules_dir: &str) -> Result<Self> {
        Self::with_config(rules_dir, true)
    }

    /// Creates a new threat rules engine with optional YARA support based on config
    pub fn with_config(rules_dir: &str, enable_yara: bool) -> Result<Self> {
        if !is_yara_available(enable_yara) {
            if enable_yara {
                print_yara_install_message();
            }
            // Return a scanner that will skip YARA operations
            return Ok(Self::new_disabled(rules_dir));
        }

        Self::new_enabled(rules_dir)
    }

    /// Creates a disabled threat rules engine (no rule loading)
    fn new_disabled(rules_dir: &str) -> Self {
        let start_time = std::time::Instant::now();
        Self {
            pre_scan_rules: Vec::new(),
            post_scan_rules: Vec::new(),
            rules_dir: rules_dir.to_string(),
            rule_metadata: HashMap::new(),
            memory_usage_bytes: 0,
            last_load_time: start_time,
        }
    }

    /// Creates an enabled threat rules engine (loads rules)
    #[cfg(feature = "yara-x-scanning")]
    fn new_enabled(rules_dir: &str) -> Result<Self> {
        let start_time = std::time::Instant::now();
        let mut scanner = Self {
            pre_scan_rules: Vec::new(),
            post_scan_rules: Vec::new(),
            rules_dir: rules_dir.to_string(),
            rule_metadata: HashMap::new(),
            memory_usage_bytes: 0,
            last_load_time: start_time,
        };

        scanner.load_rules()?;
        scanner.last_load_time = start_time;
        Ok(scanner)
    }

    /// Fallback for when YARA feature is not available
    #[cfg(not(feature = "yara-x-scanning"))]
    fn new_enabled(_rules_dir: &str) -> Result<Self> {
        Err(anyhow!("YARA-X scanning feature is not available"))
    }

    /// Loads all rules from the directory structure
    #[cfg(feature = "yara-x-scanning")]
    fn load_rules(&mut self) -> Result<()> {
        let start_time = std::time::Instant::now();

        // Load pre-scan rules
        let pre_dir = format!("{}/pre", self.rules_dir);
        if Path::new(&pre_dir).exists() {
            self.pre_scan_rules = self.load_rules_from_directory(&pre_dir, "pre")?;
        }

        // Load post-scan rules
        let post_dir = format!("{}/post", self.rules_dir);
        if Path::new(&post_dir).exists() {
            self.post_scan_rules = self.load_rules_from_directory(&post_dir, "post")?;
        }

        // Calculate memory usage
        self.calculate_memory_usage();

        let load_duration = start_time.elapsed();
        debug!(
            "Loaded {} pre-scan rules, {} post-scan rules in {}ms (memory: {}KB)",
            self.pre_scan_rules.len(),
            self.post_scan_rules.len(),
            load_duration.as_millis(),
            self.memory_usage_bytes / 1024
        );
        Ok(())
    }

    /// Calculates estimated memory usage of loaded rules
    #[cfg(feature = "yara-x-scanning")]
    fn calculate_memory_usage(&mut self) {
        // Estimate memory usage based on rule count and metadata
        let rule_memory = (self.pre_scan_rules.len() + self.post_scan_rules.len()) * 1024; // ~1KB per rule
        let metadata_memory = self.rule_metadata.len() * 256; // ~256 bytes per metadata entry

        self.memory_usage_bytes = rule_memory + metadata_memory;
    }

    /// Gets memory usage statistics
    #[cfg(test)]
    pub fn memory_stats(&self) -> RuleStats {
        RuleStats {
            pre_scan_count: self.pre_scan_rules.len(),
            post_scan_count: self.post_scan_rules.len(),
            pre_scan_rules: Vec::new(), // Empty for memory stats - not needed for this use case
            post_scan_rules: Vec::new(), // Empty for memory stats - not needed for this use case
        }
    }

    /// Loads all .yar files from a directory and their metadata
    #[cfg(feature = "yara-x-scanning")]
    fn load_rules_from_directory(
        &mut self,
        dir_path: &str,
        phase: &str,
    ) -> Result<Vec<Arc<YaraRules>>> {
        let mut rules = Vec::new();
        let pattern = format!("{dir_path}/*.yar");

        for entry in glob(&pattern).map_err(|e| anyhow!("Glob error: {}", e))? {
            match entry {
                Ok(path) => {
                    // Safely convert path to string, skipping files with non-UTF8 characters
                    if let Some(path_str) = path.to_str() {
                        // Read the rule file content
                        let rule_content = std::fs::read_to_string(path_str)
                            .map_err(|e| anyhow!("Failed to read rule file {}: {}", path_str, e))?;

                        // Compile the rule using YARA-X compiler
                        let mut compiler = yara_x::Compiler::new();
                        if let Err(e) = compiler.add_source(rule_content.as_str()) {
                            warn!("Failed to add rule source from {}: {}", path_str, e);
                            continue;
                        }

                        let rule = compiler.build();
                        let rule_name = path
                            .file_stem()
                            .and_then(|s| s.to_str())
                            .unwrap_or("unknown")
                            .to_string();

                        debug!("Loaded YARA-X rule: {} (phase: {})", path.display(), phase);

                        // Create metadata for the rule
                        let metadata = RuleMetadata {
                            name: rule_name.clone(),
                        };
                        let metadata_key = format!("{phase}:{rule_name}");
                        self.rule_metadata.insert(metadata_key, metadata);

                        rules.push(Arc::new(rule));
                    } else {
                        warn!(
                            "Skipping rule file with non-UTF8 characters: {}",
                            path.display()
                        );
                    }
                }
                Err(e) => warn!("Failed to read rule file: {}", e),
            }
        }

        Ok(rules)
    }

    /// Extract metadata from a YARA-X rule match
    #[cfg(feature = "yara-x-scanning")]
    fn extract_rule_metadata(rule_match: &yara_x::Rule) -> Option<crate::types::YaraRuleMetadata> {
        let metadata_iter = rule_match.metadata();
        let metadata_vec: Vec<(&str, yara_x::MetaValue)> = metadata_iter.collect();

        if metadata_vec.is_empty() {
            return None;
        }

        let mut rule_metadata = crate::types::YaraRuleMetadata {
            name: None,
            author: None,
            date: None,
            version: None,
            description: None,
            severity: None,
            category: None,
            confidence: None,
            tags: Vec::new(),
        };

        // Helper function to convert MetaValue to String
        let meta_value_to_string = |value: &yara_x::MetaValue| -> String {
            match value {
                yara_x::MetaValue::Integer(i) => i.to_string(),
                yara_x::MetaValue::Float(f) => f.to_string(),
                yara_x::MetaValue::Bool(b) => b.to_string(),
                yara_x::MetaValue::String(s) => (*s).to_string(),
                yara_x::MetaValue::Bytes(b) => String::from_utf8_lossy(b).to_string(),
            }
        };

        // Extract metadata fields
        for (key, value) in &metadata_vec {
            match *key {
                "name" => rule_metadata.name = Some(meta_value_to_string(value)),
                "author" => rule_metadata.author = Some(meta_value_to_string(value)),
                "date" => rule_metadata.date = Some(meta_value_to_string(value)),
                "version" => rule_metadata.version = Some(meta_value_to_string(value)),
                "description" => rule_metadata.description = Some(meta_value_to_string(value)),
                "severity" => rule_metadata.severity = Some(meta_value_to_string(value)),
                "category" => rule_metadata.category = Some(meta_value_to_string(value)),
                "confidence" => rule_metadata.confidence = Some(meta_value_to_string(value)),
                "tags" => {
                    // Handle tags as comma-separated string or array
                    let tags_str = meta_value_to_string(value);
                    rule_metadata.tags =
                        tags_str.split(',').map(|s| s.trim().to_string()).collect();
                }
                _ => {} // Ignore unknown metadata fields
            }
        }

        Some(rule_metadata)
    }

    /// Consolidated method to scan text with rules and return enhanced match information
    #[cfg(feature = "yara-x-scanning")]
    fn scan_with_rules_enhanced_internal(
        text: &str,
        context: &str,
        rules: &[Arc<YaraRules>],
        phase: &str,
    ) -> Vec<YaraMatchInfo> {
        let mut all_matches = Vec::new();

        for (i, rule_set) in rules.iter().enumerate() {
            let mut scanner = yara_x::Scanner::new(rule_set);
            match scanner.scan(text.as_bytes()) {
                Ok(scan_results) => {
                    for m in scan_results.matching_rules() {
                        all_matches.push(YaraMatchInfo {
                            rule_name: m.identifier().to_string(),
                            metadata: Self::extract_rule_metadata(&m),
                        });
                    }
                }
                Err(e) => warn!("Failed to scan with {}-rule {}: {}", phase, i, e),
            }
        }

        if !all_matches.is_empty() {
            debug!(
                "{}-scan matches in {}: {} rules triggered",
                phase,
                context,
                all_matches.len()
            );
        }

        all_matches
    }

    /// Scans text with pre-scan rules and returns enhanced match information
    #[cfg(feature = "yara-x-scanning")]
    pub fn pre_scan(&self, text: &str, context: &str) -> Vec<YaraMatchInfo> {
        Self::scan_with_rules_enhanced_internal(text, context, &self.pre_scan_rules, "pre")
    }

    /// Scans text with post-scan rules and returns enhanced match information
    #[cfg(feature = "yara-x-scanning")]
    pub fn post_scan(&self, text: &str, context: &str) -> Vec<YaraMatchInfo> {
        Self::scan_with_rules_enhanced_internal(text, context, &self.post_scan_rules, "post")
    }

    /// Gets statistics about loaded rules
    pub fn stats(&self) -> RuleStats {
        let mut pre_scan_rules = Vec::new();
        let mut post_scan_rules = Vec::new();

        // Collect rule names for pre-scan rules
        for (key, metadata) in &self.rule_metadata {
            if key.starts_with("pre:") {
                pre_scan_rules.push(metadata.name.clone());
            }
        }

        // Collect rule names for post-scan rules
        for (key, metadata) in &self.rule_metadata {
            if key.starts_with("post:") {
                post_scan_rules.push(metadata.name.clone());
            }
        }

        RuleStats {
            pre_scan_count: self.pre_scan_rules.len(),
            post_scan_count: self.post_scan_rules.len(),
            pre_scan_rules,
            post_scan_rules,
        }
    }

    /// Validates that all loaded rules are valid
    #[cfg(all(test, feature = "yara-x-scanning"))]
    pub fn validate(&self) -> Result<Vec<String>> {
        let mut issues = Vec::new();

        // Check pre-scan rules
        for (i, rules) in self.pre_scan_rules.iter().enumerate() {
            let mut scanner = yara_x::Scanner::new(rules);
            if let Err(e) = scanner.scan(b"test") {
                issues.push(format!("Pre-scan rule {i}: {e}"));
            }
        }

        // Check post-scan rules
        for (i, rules) in self.post_scan_rules.iter().enumerate() {
            let mut scanner = yara_x::Scanner::new(rules);
            if let Err(e) = scanner.scan(b"test") {
                issues.push(format!("Post-scan rule {i}: {e}"));
            }
        }

        if issues.is_empty() {
            Ok(issues)
        } else {
            Err(anyhow!("Rule validation failed: {}", issues.join(", ")))
        }
    }

    /// Fallback validation for when YARA feature is not available  
    #[cfg(all(test, not(feature = "yara-x-scanning")))]
    pub fn validate(&self) -> Result<Vec<String>> {
        Ok(Vec::new()) // Always pass validation when YARA is disabled
    }
}

/// Statistics about loaded YARA rules
#[derive(Debug)]
pub struct RuleStats {
    pub pre_scan_count: usize,
    pub post_scan_count: usize,
    pub pre_scan_rules: Vec<String>,
    pub post_scan_rules: Vec<String>,
}

impl Clone for ThreatRules {
    fn clone(&self) -> Self {
        // Clone that shares the same rules via Arc - efficient and safe
        Self {
            pre_scan_rules: self.pre_scan_rules.clone(), // Arc<Rules> can be cloned efficiently
            post_scan_rules: self.post_scan_rules.clone(),
            rules_dir: self.rules_dir.clone(),
            rule_metadata: self.rule_metadata.clone(),
            memory_usage_bytes: self.memory_usage_bytes,
            last_load_time: self.last_load_time,
        }
    }
}

/// Threat detection capability that loads rules from directory structure
pub struct YaraScanner {
    scanner: ThreatRules,
    phase: ScanPhase,
}

impl YaraScanner {
    /// Creates a new dynamic YARA capability
    pub fn new(rules_dir: &str, phase: ScanPhase) -> Result<Self> {
        let scanner = ThreatRules::new(rules_dir)?;
        Ok(Self { scanner, phase })
    }

    /// Generic YARA scanning method for any item type that implements `BatchScannableItem`
    fn scan_items_with_yara<T>(&self, items: &[T], phase: ScanPhase) -> Vec<YaraScanResult>
    where
        T: crate::security::BatchScannableItem,
    {
        let mut results = Vec::new();

        for item in items {
            let item_text = Self::format_item_for_yara_scan(item);
            let context = format!("{} '{}'", T::item_type(), item.name());

            // Use enhanced scanning methods that return metadata
            #[cfg(feature = "yara-x-scanning")]
            let enhanced_matches = match phase {
                ScanPhase::PreScan => self.scanner.pre_scan(&item_text, &context),
                ScanPhase::PostScan => self.scanner.post_scan(&item_text, &context),
            };

            #[cfg(not(feature = "yara-x-scanning"))]
            let enhanced_matches: Vec<YaraMatchInfo> = Vec::new();

            if !enhanced_matches.is_empty() {
                warn!(
                    "Security issue detected in {} '{}': {} rules matched",
                    T::item_type(),
                    item.name(),
                    enhanced_matches.len()
                );

                // Store YARA results for each match with metadata
                for match_info in enhanced_matches {
                    let yara_result =
                        Self::create_yara_result_with_metadata::<T>(item, &match_info);
                    results.push(yara_result);
                }
            }
        }
        results
    }

    /// Format an item for YARA scanning. We feed YARA the same descriptive
    /// text the LLM analyzer sees (`format_for_analysis`) so rules that
    /// pattern-match on tool/prompt/skill bodies actually have content to
    /// match against. Previously this returned just `"PROMPT: <name>"`,
    /// which meant body-pattern rules (the new skill rules in particular,
    /// but also `command_injection.yar` against prompts) silently never
    /// fired because there was nothing to scan beyond the name.
    fn format_item_for_yara_scan<T>(item: &T) -> String
    where
        T: crate::security::BatchScannableItem,
    {
        item.format_for_analysis(0)
    }

    /// Create a YARA scan result with original rule metadata
    fn create_yara_result_with_metadata<T>(item: &T, match_info: &YaraMatchInfo) -> YaraScanResult
    where
        T: crate::security::BatchScannableItem,
    {
        YaraScanResult {
            target_type: T::item_type().to_string(),
            target_name: item.name().to_string(),
            rule_name: match_info.rule_name.clone(),
            rule_file: rule_name_to_file_name(&match_info.rule_name),
            matched_text: None,
            context: generate_context_message(T::item_type(), &match_info.rule_name),
            rule_metadata: match_info.metadata.clone(),
            owasp_tags: crate::taxonomy::tags_for_yara_rule(&match_info.rule_name),
            phase: None,
            rules_executed: None,
            security_issues_detected: None,
            total_items_scanned: None,
            total_matches: None,
            status: Some("warning".to_string()),
        }
    }
}

impl Scanner for YaraScanner {
    fn name(&self) -> &'static str {
        "yara"
    }

    fn phase(&self) -> ScanPhase {
        self.phase
    }

    #[allow(clippy::too_many_lines)]
    fn run(&self, scan_data: &mut ScanData) -> anyhow::Result<()> {
        match self.phase {
            ScanPhase::PreScan => {
                let stats = self.scanner.stats();
                debug!("Running pre-scan with {} rules", stats.pre_scan_count);

                // Scan all item types using the generic scanner
                let tool_results = self.scan_items_with_yara(&scan_data.tools, ScanPhase::PreScan);
                let prompt_results =
                    self.scan_items_with_yara(&scan_data.prompts, ScanPhase::PreScan);
                let resource_results =
                    self.scan_items_with_yara(&scan_data.resources, ScanPhase::PreScan);

                // Count total matches found
                let total_matches =
                    tool_results.len() + prompt_results.len() + resource_results.len();
                let total_items =
                    scan_data.tools.len() + scan_data.prompts.len() + scan_data.resources.len();

                // Collect triggered rule names from all results and map them to file names for consistency
                let mut triggered_file_names = std::collections::HashSet::new();
                let mut triggered_rules = std::collections::HashSet::new();

                for result in &tool_results {
                    triggered_rules.insert(result.rule_name.clone());
                    // Map YARA rule name back to file name for consistent comparison
                    if let Some(file_name) = rule_name_to_file_name(&result.rule_name) {
                        triggered_file_names.insert(file_name);
                    } else {
                        // Fallback: use the rule name itself if no mapping found
                        triggered_file_names.insert(result.rule_name.clone());
                    }
                }
                for result in &prompt_results {
                    triggered_rules.insert(result.rule_name.clone());
                    if let Some(file_name) = rule_name_to_file_name(&result.rule_name) {
                        triggered_file_names.insert(file_name);
                    } else {
                        triggered_file_names.insert(result.rule_name.clone());
                    }
                }
                for result in &resource_results {
                    triggered_rules.insert(result.rule_name.clone());
                    if let Some(file_name) = rule_name_to_file_name(&result.rule_name) {
                        triggered_file_names.insert(file_name);
                    } else {
                        triggered_file_names.insert(result.rule_name.clone());
                    }
                }

                // Add all results to scan data
                scan_data.yara_results.extend(tool_results);
                scan_data.yara_results.extend(prompt_results);
                scan_data.yara_results.extend(resource_results);

                // Add a summary result
                let summary_result = YaraScanResult {
                    target_type: "summary".to_string(),
                    target_name: "pre-scan".to_string(),
                    rule_name: "YARA_PRE_SCAN_SUMMARY".to_string(),
                    rule_file: None,
                    matched_text: None,
                    context: format!(
                        "Pre-scan completed: {} rules executed on {} items",
                        stats.pre_scan_count, total_items
                    ),
                    rule_metadata: None,
                    owasp_tags: Vec::new(),
                    phase: Some("pre-scan".to_string()),
                    rules_executed: if stats.pre_scan_rules.is_empty() {
                        None
                    } else {
                        Some(
                            stats
                                .pre_scan_rules
                                .iter()
                                .map(|f| format!("{f}:*"))
                                .collect(),
                        )
                    },
                    security_issues_detected: if total_matches > 0 {
                        // Show actual rules that triggered matches with filename:rulename format
                        let triggered_vec: Vec<String> = triggered_rules
                            .into_iter()
                            .map(|rule_name| {
                                // Get the file name for this rule
                                if let Some(file_name) = rule_name_to_file_name(&rule_name) {
                                    format!("{file_name}:{rule_name}")
                                } else {
                                    rule_name
                                }
                            })
                            .collect();
                        debug!("Pre-scan triggered rules: {:?}", triggered_vec);
                        Some(triggered_vec)
                    } else {
                        None
                    },
                    total_items_scanned: Some(total_items),
                    total_matches: Some(total_matches),
                    status: Some(if total_matches == 0 {
                        "passed".to_string()
                    } else {
                        "warning".to_string()
                    }),
                };
                scan_data.yara_results.push(summary_result);
            }
            ScanPhase::PostScan => {
                let stats = self.scanner.stats();
                debug!("Running post-scan with {} rules", stats.post_scan_count);

                // Scan all item types using the generic scanner
                let tool_results = self.scan_items_with_yara(&scan_data.tools, ScanPhase::PostScan);
                let prompt_results =
                    self.scan_items_with_yara(&scan_data.prompts, ScanPhase::PostScan);
                let resource_results =
                    self.scan_items_with_yara(&scan_data.resources, ScanPhase::PostScan);

                // Count total matches found
                let total_matches =
                    tool_results.len() + prompt_results.len() + resource_results.len();
                let total_items =
                    scan_data.tools.len() + scan_data.prompts.len() + scan_data.resources.len();

                // Collect triggered rule names from all results and map them to file names for consistency
                let mut triggered_file_names = std::collections::HashSet::new();
                let mut triggered_rules = std::collections::HashSet::new();

                for result in &tool_results {
                    triggered_rules.insert(result.rule_name.clone());
                    if let Some(file_name) = rule_name_to_file_name(&result.rule_name) {
                        triggered_file_names.insert(file_name);
                    } else {
                        triggered_file_names.insert(result.rule_name.clone());
                    }
                }
                for result in &prompt_results {
                    triggered_rules.insert(result.rule_name.clone());
                    if let Some(file_name) = rule_name_to_file_name(&result.rule_name) {
                        triggered_file_names.insert(file_name);
                    } else {
                        triggered_file_names.insert(result.rule_name.clone());
                    }
                }
                for result in &resource_results {
                    triggered_rules.insert(result.rule_name.clone());
                    if let Some(file_name) = rule_name_to_file_name(&result.rule_name) {
                        triggered_file_names.insert(file_name);
                    } else {
                        triggered_file_names.insert(result.rule_name.clone());
                    }
                }

                // Add all results to scan data
                scan_data.yara_results.extend(tool_results);
                scan_data.yara_results.extend(prompt_results);
                scan_data.yara_results.extend(resource_results);

                // Add a summary result
                let summary_result = YaraScanResult {
                    target_type: "summary".to_string(),
                    target_name: "post-scan".to_string(),
                    rule_name: "YARA_POST_SCAN_SUMMARY".to_string(),
                    rule_file: None,
                    matched_text: None,
                    context: format!(
                        "Post-scan completed: {} rules executed on {} items",
                        stats.post_scan_count, total_items
                    ),
                    rule_metadata: None,
                    owasp_tags: Vec::new(),
                    phase: Some("post-scan".to_string()),
                    rules_executed: if stats.post_scan_rules.is_empty() {
                        None
                    } else {
                        Some(
                            stats
                                .post_scan_rules
                                .iter()
                                .map(|f| format!("{f}:*"))
                                .collect(),
                        )
                    },
                    security_issues_detected: if total_matches > 0 {
                        // Show actual rules that triggered matches with filename:rulename format
                        let triggered_vec: Vec<String> = triggered_rules
                            .into_iter()
                            .map(|rule_name| {
                                // Get the file name for this rule
                                if let Some(file_name) = rule_name_to_file_name(&rule_name) {
                                    format!("{file_name}:{rule_name}")
                                } else {
                                    rule_name
                                }
                            })
                            .collect();
                        debug!("Post-scan triggered rules: {:?}", triggered_vec);
                        Some(triggered_vec)
                    } else {
                        None
                    },
                    total_items_scanned: Some(total_items),
                    total_matches: Some(total_matches),
                    status: Some(if total_matches == 0 {
                        "passed".to_string()
                    } else {
                        "warning".to_string()
                    }),
                };
                scan_data.yara_results.push(summary_result);
            }
        }

        Ok(())
    }

    fn box_clone(&self) -> Box<dyn Scanner> {
        Box::new(Self {
            scanner: self.scanner.clone(),
            phase: self.phase,
        })
    }
}

// MCPScanner struct
pub struct MCPScanner {
    client: Client,
    http_timeout: u64,
    middleware_chain: ScannerChain, // New: pre/post scan hooks
    mcp_client: McpClient,          // Official rmcp SDK client
}

// MCPScanner implementation
impl MCPScanner {
    /// Creates a new `MCPScanner` with the specified HTTP timeout.
    pub fn with_timeout(http_timeout: u64) -> Result<Self> {
        let client = Client::builder()
            .timeout(Duration::from_secs(http_timeout))
            .user_agent(protocol::USER_AGENT)
            .use_preconfigured_tls((*crate::tls::default_tls_config()).clone())
            .build()
            .map_err(|e| anyhow!("Failed to create HTTP client: {}", format_error_chain(&e)))?;

        // Set up middleware chain with dynamic YARA capabilities
        let mut middleware_chain = ScannerChain::new();

        // Fixed rules directory
        let rules_dir = "rules".to_string();

        // Add dynamic YARA pre-scan capability
        if let Ok(pre_cap) = YaraScanner::new(&rules_dir, ScanPhase::PreScan) {
            middleware_chain.add(Box::new(pre_cap));
            debug!("{}", messages::YARA_PRE_SCAN_LOADED);
        } else {
            warn!("{}", messages::YARA_PRE_SCAN_FAILED);
        }

        // Add dynamic YARA post-scan capability
        if let Ok(post_cap) = YaraScanner::new(&rules_dir, ScanPhase::PostScan) {
            middleware_chain.add(Box::new(post_cap));
            debug!("{}", messages::YARA_POST_SCAN_LOADED);
        } else {
            warn!("{}", messages::YARA_POST_SCAN_FAILED);
        }

        // Add cross-origin escalation scanner (pre-scan)
        let cross_origin_scanner = CrossOriginScanner::new(ScanPhase::PreScan);
        middleware_chain.add(Box::new(cross_origin_scanner));
        debug!("Cross-origin scanner loaded");

        Ok(Self {
            client,
            http_timeout,
            middleware_chain,
            mcp_client: McpClient::with_http_timeout(http_timeout),
        })
    }

    /// Run the security-analysis pipeline against pre-fetched MCP data.
    ///
    /// This is the analyze half of `scan_single`, factored out so callers
    /// can drive it directly with data they fetched themselves — useful
    /// when the live MCP server requires upstream credentials the scanner
    /// process doesn't have but the caller does. The HTTP server exposes
    /// this path via `POST /v1/ramparts/analyze`.
    ///
    /// The function runs every analysis stage `scan_single` runs:
    ///   - pre-scan middleware hooks (mutate scan_data; cross-origin
    ///     analysis, etc.)
    ///   - scanner-config load (falls through to defaults if absent)
    ///   - `return_prompts` branch (build LLM prompts and return without
    ///     calling the LLM) OR the security-scan branch (YARA + LLM
    ///     analysis on tools/prompts/resources)
    ///   - post-scan middleware hooks
    ///
    /// `url` is recorded on the result for downstream identification only —
    /// no network call is made with it. Pass an empty string when the
    /// caller has no URL context (e.g. analyzing tool definitions stored
    /// in a database).
    pub async fn analyze_scan_data(
        &self,
        url: &str,
        mut scan_data: ScanData,
        options: &ScanOptions,
    ) -> Result<ScanResult> {
        let timer = Timer::start();
        let mut result = ScanResult::new(url.to_string());

        // === PRE-SCAN HOOKS ===
        self.middleware_chain.run_pre_scan(&mut scan_data);

        result.status = ScanStatus::Success;
        result.server_info.clone_from(&scan_data.server_info);
        result.tools.clone_from(&scan_data.tools);
        result.resources.clone_from(&scan_data.resources);
        result.prompts.clone_from(&scan_data.prompts);
        result.errors.append(&mut scan_data.fetch_errors);

        // Load scanner configuration — fall back to defaults if missing,
        // matching `scan_single`'s behaviour.
        let config_manager = crate::config::ScannerConfigManager::new();
        let scanner_config = match config_manager.load_config() {
            Ok(config) => config,
            Err(e) => {
                warn!("Failed to load scanner config, using defaults: {}", e);
                result.errors.push(format!("Config loading failed: {e}"));
                ScannerConfig::default()
            }
        };

        if options.return_prompts {
            // Build LLM prompts without actually calling the LLM. Same
            // batching logic scan_single uses — kept in one place so the
            // two paths produce identical prompts for the same inputs.
            result.llm_prompts = Some(Self::build_llm_prompts(&scan_data, &scanner_config));
            result.yara_results = std::mem::take(&mut scan_data.yara_results);
        } else {
            // Real security analysis: YARA + LLM batches across tools,
            // prompts, and resources.
            let security_scanner = if scanner_config.security.enabled {
                SecurityScanner::with_config(scanner_config)
            } else {
                SecurityScanner::default()
            };
            let mut security_result = SecurityScanResult::new();

            match security_scanner
                .scan_tools_batch(&scan_data.tools, options.detailed)
                .await
            {
                Ok((tool_issues, analysis_details)) => {
                    security_result.add_tool_issues(tool_issues);
                    for (tool_name, details) in analysis_details {
                        security_result.add_tool_analysis_details(tool_name, details);
                    }
                }
                Err(e) => warn!("Failed to batch scan tools for security issues: {}", e),
            }

            if !scan_data.prompts.is_empty() {
                match security_scanner
                    .scan_prompts_batch(&scan_data.prompts, options.detailed)
                    .await
                {
                    Ok(prompt_issues) => security_result.add_prompt_issues(prompt_issues),
                    Err(e) => {
                        warn!("Failed to batch scan prompts for security issues: {}", e)
                    }
                }
            }

            if !scan_data.resources.is_empty() {
                match security_scanner
                    .scan_resources_batch(&scan_data.resources, options.detailed)
                    .await
                {
                    Ok(resource_issues) => security_result.add_resource_issues(resource_issues),
                    Err(e) => {
                        warn!("Failed to batch scan resources for security issues: {}", e);
                    }
                }
            }

            result.security_issues = Some(security_result);

            // === POST-SCAN HOOKS ===
            self.middleware_chain.run_post_scan(&mut scan_data);
            // Middleware may have appended new yara hits — move into
            // result (no clone needed, scan_data is not used after this).
            result.yara_results = std::mem::take(&mut scan_data.yara_results);
        }

        result.response_time_ms = timer.elapsed_ms();
        debug!("Analysis completed in {}ms", result.response_time_ms);
        Ok(result)
    }

    /// Build the LLM prompts the security scanner would normally send. Used
    /// by both `scan_single` and `analyze_scan_data` when `return_prompts`
    /// is set — extracted so the two paths stay byte-for-byte identical for
    /// the same inputs.
    fn build_llm_prompts(scan_data: &ScanData, scanner_config: &ScannerConfig) -> Vec<LlmPrompt> {
        let mut prompts: Vec<LlmPrompt> = Vec::new();
        let batch_size = scanner_config.scanner.llm_batch_size as usize;
        let security_scanner = SecurityScanner::with_config(scanner_config.clone());

        if !scan_data.tools.is_empty() {
            for (batch_index, chunk) in scan_data.tools.chunks(batch_size).enumerate() {
                let tools_info = chunk
                    .iter()
                    .enumerate()
                    .map(|(i, tool)| tool.format_for_analysis(i))
                    .collect::<String>();
                let prompt_text = SecurityScanner::create_tools_analysis_prompt(&tools_info);
                let item_names = chunk.iter().map(|t| t.name.clone()).collect();
                let request_body = security_scanner.build_llm_request_body(&prompt_text);
                let endpoint = security_scanner.get_endpoint();
                prompts.push(LlmPrompt {
                    target_type: "tool".to_string(),
                    batch_index,
                    prompt: prompt_text,
                    request_body: Some(request_body),
                    endpoint,
                    item_names,
                });
            }
        }
        if !scan_data.prompts.is_empty() {
            for (batch_index, chunk) in scan_data.prompts.chunks(batch_size).enumerate() {
                let prompts_info = chunk
                    .iter()
                    .enumerate()
                    .map(|(i, p)| p.format_for_analysis(i))
                    .collect::<String>();
                let prompt_text = SecurityScanner::create_prompts_analysis_prompt(&prompts_info);
                let item_names = chunk.iter().map(|p| p.name.clone()).collect();
                let request_body = security_scanner.build_llm_request_body(&prompt_text);
                let endpoint = security_scanner.get_endpoint();
                prompts.push(LlmPrompt {
                    target_type: "prompt".to_string(),
                    batch_index,
                    prompt: prompt_text,
                    request_body: Some(request_body),
                    endpoint,
                    item_names,
                });
            }
        }
        if !scan_data.resources.is_empty() {
            for (batch_index, chunk) in scan_data.resources.chunks(batch_size).enumerate() {
                let resources_info = chunk
                    .iter()
                    .enumerate()
                    .map(|(i, r)| r.format_for_analysis(i))
                    .collect::<String>();
                let prompt_text =
                    SecurityScanner::create_resources_analysis_prompt(&resources_info);
                let item_names = chunk.iter().map(|r| r.name.clone()).collect();
                let request_body = security_scanner.build_llm_request_body(&prompt_text);
                let endpoint = security_scanner.get_endpoint();
                prompts.push(LlmPrompt {
                    target_type: "resource".to_string(),
                    batch_index,
                    prompt: prompt_text,
                    request_body: Some(request_body),
                    endpoint,
                    item_names,
                });
            }
        }
        prompts
    }

    /// Scan a single MCP server
    pub async fn scan_single(&self, url: &str, options: ScanOptions) -> Result<ScanResult> {
        let scan_timer = Timer::start();
        let mut result = ScanResult::new(url.to_string());

        debug!("Scanning {}", url);

        // STDIO URLs route through `scan_stdio_url` -> `scan_stdio_server`,
        // which sets `response_time_ms` itself. Don't overwrite it here.
        if url.starts_with("stdio:") {
            return self.scan_stdio_url(url, options).await;
        }

        // Normalize URL with error context for HTTP URLs
        let normalized_url = Self::normalize_url(url);
        result.url.clone_from(&normalized_url);

        // Perform the scan with performance tracking using rmcp SDK
        let scan_result = track_performance("MCP server scan", || async {
            let scan_future = self.perform_scan_with_rmcp(&normalized_url, &options);
            match timeout(Duration::from_secs(options.timeout), scan_future).await {
                Ok(result) => result,
                Err(_) => Err(anyhow!("Scan operation timed out")),
            }
        })
        .await;

        match scan_result {
            Ok(mut scan_data) => {
                // === PRE-SCAN HOOKS ===
                self.middleware_chain.run_pre_scan(&mut scan_data);

                result.status = ScanStatus::Success;
                result.server_info.clone_from(&scan_data.server_info);
                result.tools.clone_from(&scan_data.tools);
                result.resources.clone_from(&scan_data.resources);
                result.prompts.clone_from(&scan_data.prompts);
                result.yara_results.clone_from(&scan_data.yara_results);

                // Add fetch errors to the result
                result.errors.extend(scan_data.fetch_errors.clone());

                // Load scanner configuration
                let config_manager = crate::config::ScannerConfigManager::new();
                let scanner_config = match config_manager.load_config() {
                    Ok(config) => config,
                    Err(e) => {
                        warn!("Failed to load scanner config, using defaults: {}", e);
                        result.errors.push(format!("Config loading failed: {e}"));
                        ScannerConfig::default()
                    }
                };

                // If caller wants prompts back instead of LLM call, skip LLM and populate prompts
                if options.return_prompts {
                    let mut prompts: Vec<LlmPrompt> = Vec::new();
                    // Tools
                    if !scan_data.tools.is_empty() {
                        let batch_size = scanner_config.scanner.llm_batch_size as usize;
                        for (batch_index, chunk) in scan_data.tools.chunks(batch_size).enumerate() {
                            let tools_info = chunk
                                .iter()
                                .enumerate()
                                .map(|(i, tool)| tool.format_for_analysis(i))
                                .collect::<String>();
                            let prompt_text =
                                SecurityScanner::create_tools_analysis_prompt(&tools_info);
                            let item_names = chunk.iter().map(|t| t.name.clone()).collect();
                            let request_body = SecurityScanner::with_config(scanner_config.clone())
                                .build_llm_request_body(&prompt_text);
                            let endpoint =
                                SecurityScanner::with_config(scanner_config.clone()).get_endpoint();
                            prompts.push(LlmPrompt {
                                target_type: "tool".to_string(),
                                batch_index,
                                prompt: prompt_text,
                                request_body: Some(request_body),
                                endpoint,
                                item_names,
                            });
                        }
                    }
                    // Prompts
                    if !scan_data.prompts.is_empty() {
                        let batch_size = scanner_config.scanner.llm_batch_size as usize;
                        for (batch_index, chunk) in scan_data.prompts.chunks(batch_size).enumerate()
                        {
                            let prompts_info = chunk
                                .iter()
                                .enumerate()
                                .map(|(i, p)| p.format_for_analysis(i))
                                .collect::<String>();
                            let prompt_text =
                                SecurityScanner::create_prompts_analysis_prompt(&prompts_info);
                            let item_names = chunk.iter().map(|p| p.name.clone()).collect();
                            let request_body = SecurityScanner::with_config(scanner_config.clone())
                                .build_llm_request_body(&prompt_text);
                            let endpoint =
                                SecurityScanner::with_config(scanner_config.clone()).get_endpoint();
                            prompts.push(LlmPrompt {
                                target_type: "prompt".to_string(),
                                batch_index,
                                prompt: prompt_text,
                                request_body: Some(request_body),
                                endpoint,
                                item_names,
                            });
                        }
                    }
                    // Resources
                    if !scan_data.resources.is_empty() {
                        let batch_size = scanner_config.scanner.llm_batch_size as usize;
                        for (batch_index, chunk) in
                            scan_data.resources.chunks(batch_size).enumerate()
                        {
                            let resources_info = chunk
                                .iter()
                                .enumerate()
                                .map(|(i, r)| r.format_for_analysis(i))
                                .collect::<String>();
                            let prompt_text =
                                SecurityScanner::create_resources_analysis_prompt(&resources_info);
                            let item_names = chunk.iter().map(|r| r.name.clone()).collect();
                            let request_body = SecurityScanner::with_config(scanner_config.clone())
                                .build_llm_request_body(&prompt_text);
                            let endpoint =
                                SecurityScanner::with_config(scanner_config.clone()).get_endpoint();
                            prompts.push(LlmPrompt {
                                target_type: "resource".to_string(),
                                batch_index,
                                prompt: prompt_text,
                                request_body: Some(request_body),
                                endpoint,
                                item_names,
                            });
                        }
                    }
                    result.llm_prompts = Some(prompts);
                } else {
                    // Perform security scanning with configuration
                    let security_scanner = if scanner_config.security.enabled {
                        SecurityScanner::with_config(scanner_config)
                    } else {
                        SecurityScanner::default()
                    };
                    let mut security_result = SecurityScanResult::new();

                    // Always perform the security scan (no enhanced/standard distinction)
                    // Batch scan tools for security issues
                    match security_scanner
                        .scan_tools_batch(&scan_data.tools, options.detailed)
                        .await
                    {
                        Ok((tool_issues, analysis_details)) => {
                            security_result.add_tool_issues(tool_issues);
                            // Store the analysis details for each tool
                            for (tool_name, details) in analysis_details {
                                security_result.add_tool_analysis_details(tool_name, details);
                            }
                        }
                        Err(e) => warn!("Failed to batch scan tools for security issues: {}", e),
                    }

                    // Batch scan prompts for security issues
                    if !scan_data.prompts.is_empty() {
                        match security_scanner
                            .scan_prompts_batch(&scan_data.prompts, options.detailed)
                            .await
                        {
                            Ok(prompt_issues) => security_result.add_prompt_issues(prompt_issues),
                            Err(e) => {
                                warn!("Failed to batch scan prompts for security issues: {}", e)
                            }
                        }
                    }

                    // Batch scan resources for security issues
                    if !scan_data.resources.is_empty() {
                        match security_scanner
                            .scan_resources_batch(&scan_data.resources, options.detailed)
                            .await
                        {
                            Ok(resource_issues) => {
                                security_result.add_resource_issues(resource_issues)
                            }
                            Err(e) => {
                                warn!("Failed to batch scan resources for security issues: {}", e);
                            }
                        }
                    }

                    if !options.return_prompts {
                        result.security_issues = Some(security_result);
                    }

                    // === POST-SCAN HOOKS ===
                    self.middleware_chain.run_post_scan(&mut scan_data);

                    // Update result with any post-scan changes
                    result.yara_results.clone_from(&scan_data.yara_results);

                    debug!("Scan completed in {}ms", scan_timer.elapsed_ms());
                }
            }
            Err(e) => {
                result.status = ScanStatus::Failed(e.to_string());
                result.add_error(error_utils::format_error("Scan operation", &e.to_string()));
                warn!("Scan failed: [\x1b[1m{}\x1b[0m]", e);
            }
        }

        // Record total scan duration on both the success and failure paths so
        // failed scans no longer report `0ms`.
        result.response_time_ms = scan_timer.elapsed_ms();
        Ok(result)
    }

    /// Parse and scan a STDIO URL (format: stdio:command:arg1:arg2... or stdio://command:arg1:arg2...)
    async fn scan_stdio_url(&self, stdio_url: &str, options: ScanOptions) -> Result<ScanResult> {
        // Parse the STDIO URL format: stdio:command:arg1:arg2:... or stdio://command:arg1:arg2:...
        let parts: Vec<&str> = stdio_url.splitn(3, ':').collect();
        if parts.len() < 2 {
            return Err(anyhow!(
                "Invalid STDIO URL format. Expected: stdio:command or stdio:command:args"
            ));
        }

        // Handle both stdio:command and stdio://command formats
        let command = parts[1].trim_start_matches("//");
        let args: Vec<String> = if parts.len() > 2 && !parts[2].is_empty() {
            parts[2]
                .split(':')
                .map(std::string::ToString::to_string)
                .collect()
        } else {
            Vec::new()
        };

        // Create a temporary server config for the STDIO URL. Leaving `name`
        // unset is intentional: the synthetic `STDIO-<command>` placeholder
        // pollutes `to_display_url` output with a useless suffix. We restore
        // the original `stdio_url` on the result below so the user sees the
        // exact string they typed.
        let server_config = MCPServerConfig {
            name: None,
            url: None,
            command: Some(command.to_string()),
            args: Some(args),
            env: None,
            description: Some(format!("STDIO server from URL: {stdio_url}")),
            auth_headers: None,
            options: None,
        };

        let mut result = self.scan_stdio_server(&server_config, options).await?;
        result.url = stdio_url.to_string();
        Ok(result)
    }

    /// Scan a STDIO MCP server using subprocess transport
    async fn scan_stdio_server(
        &self,
        server_config: &MCPServerConfig,
        options: ScanOptions,
    ) -> Result<ScanResult> {
        let scan_timer = Timer::start();
        let command = server_config
            .command
            .as_ref()
            .ok_or_else(|| anyhow!("STDIO server missing command"))?;

        let args = server_config.args.as_deref().unwrap_or(&[]);
        let display_url = server_config.to_display_url();

        debug!("Scanning STDIO MCP server: {}", display_url);

        let mut result = ScanResult::new(display_url.clone());

        // Kick off a parallel OSV.dev lookup for this server's launch package
        // (e.g. `npx -y @scope/pkg@1.2.3` -> npm:@scope/pkg@1.2.3). We don't
        // wait on it inline — the future is awaited after the scan body so
        // the OSV roundtrip overlaps with the actual MCP handshake + tool
        // enumeration. Returns an empty Vec when no recognizable package
        // spec can be parsed from the command (e.g. raw `python3 script.py`).
        let osv_findings_future =
            crate::osv::parse_package_spec_from_command(command, args).map(|spec| {
                debug!("Launching OSV lookup for {}/{}", spec.ecosystem, spec.name);
                crate::osv::query_osv(self.client.clone(), spec)
            });

        // Wrap the entire connect-and-scan pipeline in `options.timeout` so a
        // hung subprocess (no MCP handshake response, deadlocked tool, etc.)
        // can't make the CLI hang forever — same overall budget the HTTP path
        // gets in `scan_single`.
        let scan_result = track_performance("STDIO MCP server scan", || async {
            match timeout(Duration::from_secs(options.timeout), async {
                let session = self
                    .mcp_client
                    .connect_subprocess(command, args, server_config.env.as_ref())
                    .await
                    .map_err(|e| anyhow!("Failed to connect to STDIO server {}: {}", command, e))?;
                let scan_data = self.perform_scan_with_session(&session, &options).await?;
                Ok::<_, anyhow::Error>((session, scan_data))
            })
            .await
            {
                Ok(inner) => inner,
                Err(_) => Err(anyhow!(
                    "STDIO scan operation timed out after {}s",
                    options.timeout
                )),
            }
        })
        .await;

        // Drain the OSV future (no-op if no spec was parseable). Findings
        // append to result.yara_results regardless of whether the main scan
        // succeeded — supply-chain risks are real even if the server is
        // unreachable right now.
        let osv_findings = match osv_findings_future {
            Some(fut) => fut.await,
            None => Vec::new(),
        };

        match scan_result {
            Ok((session, mut scan_data)) => {
                // Apply the same middleware chain as HTTP scanning
                self.middleware_chain.run_pre_scan(&mut scan_data);

                // === SECURITY ANALYSIS ===
                // Load scanner configuration for security analysis
                #[allow(clippy::single_match_else)]
                let scanner_config = match config::ScannerConfigManager::new().load_config() {
                    Ok(config) => config,
                    Err(_) => {
                        debug!("Failed to load scanner config for STDIO security analysis, using defaults");
                        ScannerConfig::default()
                    }
                };

                // Perform security scanning with configuration - same as HTTP flow
                let security_scanner = if scanner_config.security.enabled {
                    SecurityScanner::with_config(scanner_config)
                } else {
                    SecurityScanner::default()
                };
                let mut security_result = SecurityScanResult::new();

                // Batch scan tools for security issues
                match security_scanner
                    .scan_tools_batch(&scan_data.tools, options.detailed)
                    .await
                {
                    Ok((tool_issues, analysis_details)) => {
                        security_result.add_tool_issues(tool_issues);
                        // Store the analysis details for each tool
                        for (tool_name, details) in analysis_details {
                            security_result.add_tool_analysis_details(tool_name, details);
                        }
                    }
                    Err(e) => warn!(
                        "Failed to batch scan STDIO tools for security issues: {}",
                        e
                    ),
                }

                // Batch scan prompts for security issues
                if !scan_data.prompts.is_empty() {
                    match security_scanner
                        .scan_prompts_batch(&scan_data.prompts, options.detailed)
                        .await
                    {
                        Ok(prompt_issues) => security_result.add_prompt_issues(prompt_issues),
                        Err(e) => warn!(
                            "Failed to batch scan STDIO prompts for security issues: {}",
                            e
                        ),
                    }
                }

                // Batch scan resources for security issues
                if !scan_data.resources.is_empty() {
                    match security_scanner
                        .scan_resources_batch(&scan_data.resources, options.detailed)
                        .await
                    {
                        Ok(resource_issues) => security_result.add_resource_issues(resource_issues),
                        Err(e) => {
                            warn!(
                                "Failed to batch scan STDIO resources for security issues: {}",
                                e
                            );
                        }
                    }
                }

                // === POST-SCAN HOOKS ===
                self.middleware_chain.run_post_scan(&mut scan_data);

                // Populate result with scan data
                result.status = ScanStatus::Success;
                result.server_info.clone_from(&session.server_info);
                result.tools = scan_data.tools;
                result.resources = scan_data.resources;
                result.prompts = scan_data.prompts;
                result.yara_results = scan_data.yara_results;
                result.security_issues = Some(security_result);

                debug!("Successfully scanned STDIO server: {}", display_url);
            }
            Err(e) => {
                result.status = ScanStatus::Failed(e.to_string());
                result.add_error(format!("STDIO scan failed: {e}"));
                warn!("STDIO scan failed for {}: {}", display_url, e);
            }
        }

        // Append OSV supply-chain findings (if any) regardless of the main
        // scan's success: a failed handshake doesn't make the dependency
        // any less vulnerable.
        if !osv_findings.is_empty() {
            debug!(
                "OSV reported {} vulnerability finding(s) for {}",
                osv_findings.len(),
                display_url
            );
            result.yara_results.extend(osv_findings);
        }

        result.response_time_ms = scan_timer.elapsed_ms();
        Ok(result)
    }

    /// Scan MCP servers from IDE configuration files
    /// Scan configuration files grouped by IDE
    pub async fn scan_config_by_ide(&self, options: ScanOptions) -> Result<Vec<ScanResult>> {
        self.scan_config_by_ide_inner(MCPConfigManager::new(), options)
            .await
    }

    /// Scan MCP servers discovered by walking a user-supplied root directory
    /// (e.g. a checked-in repo of IDE configs). See ramparts#51.
    pub async fn scan_config_in_root(
        &self,
        root: &Path,
        options: ScanOptions,
    ) -> Result<Vec<ScanResult>> {
        self.scan_config_by_ide_inner(MCPConfigManager::with_root(root), options)
            .await
    }

    async fn scan_config_by_ide_inner(
        &self,
        config_manager: MCPConfigManager,
        options: ScanOptions,
    ) -> Result<Vec<ScanResult>> {
        if !config_manager.has_config_files() {
            return Err(anyhow!("No MCP IDE configuration files found"));
        }

        let config = config_manager.load_config();

        // Debug: Show that we loaded MCP config
        println!(
            "🔍 Loaded MCP configuration with {} servers",
            config.servers.as_ref().map(|s| s.len()).unwrap_or(0)
        );

        // =============================================================
        // Pre-connection static analysis of MCP server definitions
        // - Scan command/args/env with YARA pre-scan rules (if enabled)
        // - Heuristic checks for risky STDIO patterns (always on)
        // - Baseline/diff detection for post-approval swaps
        // =============================================================
        use std::collections::HashMap as StdHashMap;
        let mut server_config_yara: StdHashMap<String, Vec<YaraScanResult>> = StdHashMap::new();

        // Build initial baseline map from disk (best-effort)
        fn get_baseline_path() -> std::path::PathBuf {
            dirs::home_dir()
                .map(|mut p| {
                    p.push(".ramparts");
                    p.push("mcp-baseline.json");
                    p
                })
                .unwrap_or_else(|| std::path::PathBuf::from(".ramparts/mcp-baseline.json"))
        }

        fn compute_server_fingerprint(server: &MCPServerConfig) -> String {
            use std::hash::{Hash, Hasher};
            let mut s = String::new();
            if let Some(name) = &server.name {
                s.push_str(name);
            }
            if let Some(url) = &server.url {
                s.push_str(url);
            }
            if let Some(cmd) = &server.command {
                s.push_str(cmd);
            }
            if let Some(args) = &server.args {
                s.push_str(&args.join(" "));
            }
            if let Some(env) = &server.env {
                let mut kv: Vec<_> = env.iter().collect();
                kv.sort_by(|a, b| a.0.cmp(b.0));
                for (k, v) in kv {
                    s.push_str(k);
                    s.push('=');
                    s.push_str(v);
                }
            }
            let mut hasher = std::collections::hash_map::DefaultHasher::new();
            s.hash(&mut hasher);
            format!("{:016x}", hasher.finish())
        }

        let baseline_path = get_baseline_path();
        let mut baseline_map: StdHashMap<String, String> = StdHashMap::new();
        if baseline_path.exists() {
            if let Ok(content) = std::fs::read_to_string(&baseline_path) {
                if let Ok(map) = serde_json::from_str::<StdHashMap<String, String>>(&content) {
                    baseline_map = map;
                }
            }
        }

        // Prepare YARA rules engine for config scanning (feature-gated)
        #[cfg(feature = "yara-x-scanning")]
        let pre_rules_engine = ThreatRules::new("rules").ok(); // compile once for config scan

        if let Some(ref servers) = config.servers {
            for server in servers {
                let key = server.dedup_key();

                // Heuristic removed in favor of YARA (below). Keep vector for potential YARA additions
                let mut prefindings: Vec<YaraScanResult> = Vec::new();

                // YARA pre-scan on server definition text (if YARA enabled)
                #[cfg(feature = "yara-x-scanning")]
                if let Some(engine) = &pre_rules_engine {
                    let mut text = String::new();
                    if let Some(name) = &server.name {
                        text.push_str(&format!("NAME: {name}\n"));
                    }
                    if let Some(url) = &server.url {
                        text.push_str(&format!("URL: {url}\n"));
                    }
                    if let Some(cmd) = &server.command {
                        text.push_str(&format!("COMMAND: {cmd}\n"));
                    }
                    if let Some(args) = &server.args {
                        text.push_str(&format!("ARGS: {}\n", args.join(" ")));
                    }
                    if let Some(env) = &server.env {
                        // Only include environment VALUES that look non-placeholder and non-trivial,
                        // to avoid false positives from variable NAMES alone
                        let mut kv: Vec<_> = env.iter().collect();
                        kv.sort_by(|a, b| a.0.cmp(b.0));
                        for (_k, v) in kv {
                            let val = v.trim();
                            if val.is_empty() {
                                continue;
                            }
                            // Skip common placeholder syntaxes and trivial booleans
                            let is_placeholder = val.starts_with("${")
                                || val.starts_with("$(")
                                || val.contains("{{")
                                || val.contains('<')
                                || val.eq_ignore_ascii_case("true")
                                || val.eq_ignore_ascii_case("false");
                            if is_placeholder || val.len() < 8 {
                                continue;
                            }
                            text.push_str(&format!("ENV_VALUE:{val} "));
                        }
                        text.push('\n');
                    }
                    if let Some(desc) = &server.description {
                        text.push_str(&format!("DESCRIPTION: {desc}\n"));
                    }
                    let context = format!(
                        "server '{}'",
                        server.name.as_deref().unwrap_or(&server.to_display_url())
                    );
                    let matches = engine.pre_scan(&text, &context);
                    for m in matches {
                        prefindings.push(YaraScanResult {
                            target_type: "server".to_string(),
                            target_name: server
                                .name
                                .clone()
                                .unwrap_or_else(|| server.to_display_url()),
                            rule_name: m.rule_name.clone(),
                            rule_file: rule_name_to_file_name(&m.rule_name),
                            matched_text: None,
                            context: generate_context_message("server", &m.rule_name),
                            rule_metadata: m.metadata.clone(),
                            owasp_tags: crate::taxonomy::tags_for_yara_rule(&m.rule_name),
                            phase: Some("pre-config".to_string()),
                            rules_executed: None,
                            security_issues_detected: None,
                            total_items_scanned: None,
                            total_matches: None,
                            status: Some("warning".to_string()),
                        });
                    }
                }

                // Baseline/diff check (best-effort)
                let fp = compute_server_fingerprint(server);
                match baseline_map.get(&key) {
                    Some(stored) if stored == &fp => { /* unchanged */ }
                    Some(_different) => {
                        prefindings.push(YaraScanResult {
                            target_type: "server".to_string(),
                            target_name: server
                                .name
                                .clone()
                                .unwrap_or_else(|| server.to_display_url()),
                            rule_name: "MCPConfigChanged".to_string(),
                            rule_file: None,
                            matched_text: None,
                            context: "MCP server configuration changed since last baseline"
                                .to_string(),
                            rule_metadata: Some(crate::types::YaraRuleMetadata {
                                name: Some("Baseline Change".to_string()),
                                author: Some("Ramparts".to_string()),
                                date: None,
                                version: None,
                                description: Some(
                                    "Server command/args/env fingerprint differs from baseline."
                                        .to_string(),
                                ),
                                severity: Some("HIGH".to_string()),
                                category: Some("supply-chain".to_string()),
                                confidence: Some("MEDIUM".to_string()),
                                tags: vec!["baseline".to_string()],
                            }),
                            owasp_tags: crate::taxonomy::tags_for_yara_rule("MCPConfigChanged"),
                            phase: Some("pre-config".to_string()),
                            rules_executed: None,
                            security_issues_detected: None,
                            total_items_scanned: None,
                            total_matches: None,
                            status: Some("warning".to_string()),
                        });
                    }
                    None => {
                        // First-run: populate baseline directory/file if missing (best-effort)
                        if !baseline_path.exists() {
                            if let Some(parent) = baseline_path.parent() {
                                let _ = std::fs::create_dir_all(parent);
                            }
                        }
                        baseline_map.insert(key.clone(), fp.clone());
                        // Write baseline silently
                        if let Ok(serialized) = serde_json::to_string_pretty(&baseline_map) {
                            let _ = std::fs::write(&baseline_path, serialized);
                        }
                    }
                }

                if !prefindings.is_empty() {
                    server_config_yara.insert(key, prefindings);
                }
            }
        }

        let server_config_yara = std::sync::Arc::new(server_config_yara);

        let mut results = Vec::new();

        if let Some(ref servers) = config.servers {
            debug!(
                "Found [\x1b[1m{}\x1b[0m] MCP servers to scan",
                servers.len()
            );

            // Parallel scanning implementation using futures
            use futures::future::join_all;

            // Create scanning tasks for parallel execution
            let scan_tasks: Vec<_> = servers
                .iter()
                .map(|server| {
                    let server = server.clone();
                    let config = config.clone();
                    let options = options.clone();
                    // Clone the scanner (shares compiled YARA rules, creates new McpClient)
                    let scanner = self.clone();
                    // Clone shared pre-config findings map into the task
                    let cfg_yara = server_config_yara.clone();

                    tokio::spawn(async move {
                        debug!(
                            "Scanning MCP server: [\x1b[1m{}\x1b[0m] ({})",
                            server.name.as_deref().unwrap_or("unnamed"),
                            server.to_display_url()
                        );

                        // Extract IDE name from description if available
                        let ide_source = server
                            .description
                            .as_ref()
                            .and_then(|desc| {
                                // Look for [IDE:name] pattern in description
                                if let Some(start) = desc.rfind("[IDE:") {
                                    if let Some(end) = desc[start..].find(']') {
                                        let ide_name = &desc[start + 5..start + end];
                                        return Some(ide_name.to_string());
                                    }
                                }
                                None
                            })
                            .unwrap_or_else(|| "IDE Configs".to_string());

                        let server_options =
                            MCPScanner::build_server_options(&options, &config, &server);

                        // Small helper to attach pre-config findings
                        let attach_findings = |res: &mut ScanResult| {
                            if let Some(findings) = cfg_yara.get(&server.dedup_key()) {
                                res.yara_results.extend(findings.clone());
                            }
                        };

                        // Scan the MCP server - HTTP or STDIO
                        let result = if let Some(url) = server.scan_url() {
                            // HTTP server scanning
                            match scanner.scan_single(url, server_options).await {
                                Ok(mut result) => {
                                    result.ide_source = Some(ide_source);
                                    // Append pre-config YARA/heuristic/baseline findings if any
                                    attach_findings(&mut result);
                                    result
                                }
                                Err(e) => {
                                    let mut failed_result = ScanResult::new(url.to_string());
                                    failed_result.status = ScanStatus::Failed(e.to_string());
                                    failed_result.ide_source = Some(ide_source);
                                    attach_findings(&mut failed_result);
                                    failed_result
                                }
                            }
                        } else if server.command.is_some() {
                            // STDIO server scanning
                            match scanner.scan_stdio_server(&server, server_options).await {
                                Ok(mut result) => {
                                    result.ide_source = Some(ide_source);
                                    attach_findings(&mut result);
                                    result
                                }
                                Err(e) => {
                                    let mut failed_result =
                                        ScanResult::new(server.to_display_url());
                                    failed_result.status = ScanStatus::Failed(e.to_string());
                                    failed_result.ide_source = Some(ide_source);
                                    attach_findings(&mut failed_result);
                                    failed_result
                                }
                            }
                        } else {
                            // Invalid server configuration
                            let mut failed_result = ScanResult::new("unknown".to_string());
                            failed_result.status =
                                ScanStatus::Failed("Invalid server configuration".to_string());
                            failed_result.ide_source = Some(ide_source);
                            attach_findings(&mut failed_result);
                            failed_result
                        };

                        result
                    })
                })
                .collect();

            // Execute all scans in parallel and collect results
            println!(
                "🚀 Starting parallel scan of {} servers...",
                scan_tasks.len()
            );

            // Add timeout to prevent tasks from hanging indefinitely
            let scan_results = tokio::time::timeout(
                std::time::Duration::from_secs(300), // 5 minute timeout for all tasks
                join_all(scan_tasks),
            )
            .await
            .unwrap_or_else(|_| {
                warn!("Parallel scan tasks timed out after 5 minutes");
                vec![] // Return empty results if timeout
            });

            // Extract results from join handles
            for task_result in scan_results {
                match task_result {
                    Ok(scan_result) => results.push(scan_result),
                    Err(e) => {
                        // Task panicked or was cancelled
                        let mut failed_result = ScanResult::new("task_failed".to_string());
                        failed_result.status = ScanStatus::Failed(format!("Scan task failed: {e}"));
                        failed_result.ide_source = Some("IDE Configs".to_string());
                        results.push(failed_result);
                    }
                }
            }

            // Clean up the main scanner after all parallel tasks complete
            if let Err(e) = self.mcp_client.cleanup_all_sessions().await {
                warn!("Failed to clean up main scanner sessions after parallel scan: {e}");
            }
        }

        Ok(results)
    }

    fn build_server_options(
        options: &ScanOptions,
        config: &MCPConfig,
        server: &MCPServerConfig,
    ) -> ScanOptions {
        let mut server_options = options.clone();

        // Apply global options from config
        if let Some(global_options) = &config.options {
            if let Some(timeout) = global_options.timeout {
                server_options.timeout = timeout;
            }
            if let Some(http_timeout) = global_options.http_timeout {
                server_options.http_timeout = http_timeout;
            }
            if let Some(format) = &global_options.format {
                server_options.format.clone_from(format);
            }
            if let Some(detailed) = global_options.detailed {
                server_options.detailed = detailed;
            }
        }

        // Apply server-specific options
        if let Some(server_specific_options) = &server.options {
            if let Some(timeout) = server_specific_options.timeout {
                server_options.timeout = timeout;
            }
            if let Some(http_timeout) = server_specific_options.http_timeout {
                server_options.http_timeout = http_timeout;
            }
            if let Some(format) = &server_specific_options.format {
                server_options.format.clone_from(format);
            }
            if let Some(detailed) = server_specific_options.detailed {
                server_options.detailed = detailed;
            }
        }

        // Merge authentication headers
        server_options.auth_headers = Self::build_auth_headers(options, config, server);

        server_options
    }

    fn build_auth_headers(
        options: &ScanOptions,
        config: &MCPConfig,
        server: &MCPServerConfig,
    ) -> Option<HashMap<String, String>> {
        let mut auth_headers = options.auth_headers.clone();

        // Add global auth headers
        if let Some(global_auth_headers) = &config.auth_headers {
            match &mut auth_headers {
                Some(headers) => {
                    for (key, value) in global_auth_headers {
                        headers.insert(key.clone(), value.clone());
                    }
                }
                None => {
                    auth_headers = Some(global_auth_headers.clone());
                }
            }
        }

        // Add server-specific auth headers
        if let Some(server_auth_headers) = &server.auth_headers {
            match &mut auth_headers {
                Some(headers) => {
                    for (key, value) in server_auth_headers {
                        headers.insert(key.clone(), value.clone());
                    }
                }
                None => {
                    auth_headers = Some(server_auth_headers.clone());
                }
            }
        }

        auth_headers
    }

    /// Perform the scan
    /// New rmcp-based scan implementation that replaces all legacy transport code
    async fn perform_scan_with_rmcp(&self, url: &str, options: &ScanOptions) -> Result<ScanData> {
        let mut scan_data = ScanData::new();

        // Connect to MCP server using smart transport selection
        let session = self
            .mcp_client
            .connect_smart(url, options.auth_headers.clone())
            .await?;

        // After connection, some servers need a brief settling period.
        // Instead of a fixed sleep, use a short exponential backoff on the first request.
        async fn fetch_with_backoff<T, F, Fut>(mut op: F, label: &str) -> anyhow::Result<T>
        where
            F: FnMut() -> Fut,
            Fut: std::future::Future<Output = anyhow::Result<T>>,
        {
            use std::time::Duration;
            use tokio::time::sleep;

            let mut delay = Duration::from_millis(100);
            let max_delay = Duration::from_millis(1000);
            let max_attempts = 5;

            for attempt in 1..=max_attempts {
                match op().await {
                    Ok(v) => return Ok(v),
                    Err(e) => {
                        let msg = e.to_string();
                        // Retry on likely initialization/transient errors
                        let retryable = msg.contains("initialization")
                            || msg.contains("before initialization was complete")
                            || msg.contains("transport")
                            || msg.contains("Failed to create MCP service")
                            || msg.contains("connection")
                            || msg.contains("temporarily");

                        if retryable && attempt < max_attempts {
                            tracing::warn!(
                                "{} attempt {} failed: {}. Retrying in {}ms",
                                label,
                                attempt,
                                msg,
                                delay.as_millis()
                            );
                            sleep(delay).await;
                            delay = std::cmp::min(delay * 2, max_delay);
                            continue;
                        }
                        return Err(e);
                    }
                }
            }
            unreachable!("backoff loop should return on success or error");
        }

        debug!("Starting to fetch tools, resources, and prompts after rmcp connection");

        // Get server info from initialization
        if let Some(ref server_info) = session.server_info {
            scan_data.server_info = Some(server_info.clone());
        }

        // Fetch tools, resources, and prompts using rmcp SDK with proper error handling
        let mut fetch_errors = Vec::new();

        scan_data.tools =
            match fetch_with_backoff(|| self.mcp_client.list_tools(&session), "list_tools").await {
                Ok(tools) => {
                    debug!("Successfully fetched {} tools via rmcp", tools.len());
                    tools
                }
                Err(e) => {
                    let error_msg = format!("Failed to fetch tools via rmcp: {e}");
                    warn!("{}", error_msg);
                    fetch_errors.push(error_msg);
                    Vec::new()
                }
            };

        scan_data.resources = match self.mcp_client.list_resources(&session).await {
            Ok(resources) => {
                debug!(
                    "Successfully fetched {} resources via rmcp",
                    resources.len()
                );
                resources
            }
            Err(e) => {
                let error_msg = format!("Failed to fetch resources via rmcp: {e}");
                warn!("{}", error_msg);
                fetch_errors.push(error_msg);
                Vec::new()
            }
        };

        scan_data.prompts = match self.mcp_client.list_prompts(&session).await {
            Ok(prompts) => {
                debug!("Successfully fetched {} prompts via rmcp", prompts.len());
                prompts
            }
            Err(e) => {
                let error_msg = format!("Failed to fetch prompts via rmcp: {e}");
                warn!("{}", error_msg);
                fetch_errors.push(error_msg);
                Vec::new()
            }
        };

        // Store fetch errors in scan_data for later inclusion in final result
        scan_data.fetch_errors = fetch_errors;

        // Apply --only filter: drop categories the user didn't ask for so
        // they don't appear in security analysis or output.
        Self::apply_only_filter(&mut scan_data, options);

        // Clean up the session to prevent session deletion errors
        if let Err(e) = self.mcp_client.cleanup_session(&session).await {
            warn!("Failed to clean up MCP session: {}", e);
        }

        Ok(scan_data)
    }

    /// Drop tools / resources / prompts from `scan_data` based on
    /// `ScanOptions::only`. When `only` is `None`, no-op. When it's `Some`,
    /// any artifact kind not in the list is cleared so downstream YARA, LLM,
    /// and cross-origin steps see nothing for it.
    fn apply_only_filter(scan_data: &mut ScanData, options: &ScanOptions) {
        let Some(kinds) = options.only.as_ref() else {
            return;
        };
        if !kinds.contains(&crate::types::ArtifactKind::Tools) {
            scan_data.tools.clear();
        }
        if !kinds.contains(&crate::types::ArtifactKind::Resources) {
            scan_data.resources.clear();
        }
        if !kinds.contains(&crate::types::ArtifactKind::Prompts) {
            scan_data.prompts.clear();
        }
    }

    /// Perform scan with an existing MCP session (for STDIO transport)
    async fn perform_scan_with_session(
        &self,
        session: &crate::types::MCPSession,
        options: &ScanOptions,
    ) -> Result<ScanData> {
        let mut scan_data = ScanData::new();

        async fn fetch_with_backoff<T, F, Fut>(mut op: F, label: &str) -> anyhow::Result<T>
        where
            F: FnMut() -> Fut,
            Fut: std::future::Future<Output = anyhow::Result<T>>,
        {
            use std::time::Duration;
            use tokio::time::sleep;

            let mut delay = Duration::from_millis(100);
            let max_delay = Duration::from_millis(1000);
            let max_attempts = 5;

            for attempt in 1..=max_attempts {
                match op().await {
                    Ok(v) => return Ok(v),
                    Err(e) => {
                        let msg = e.to_string();
                        let retryable = msg.contains("initialization")
                            || msg.contains("before initialization was complete")
                            || msg.contains("transport")
                            || msg.contains("Failed to create MCP service")
                            || msg.contains("connection")
                            || msg.contains("temporarily");
                        if retryable && attempt < max_attempts {
                            tracing::warn!(
                                "{} attempt {} failed: {}. Retrying in {}ms",
                                label,
                                attempt,
                                msg,
                                delay.as_millis()
                            );
                            sleep(delay).await;
                            delay = std::cmp::min(delay * 2, max_delay);
                            continue;
                        }
                        return Err(e);
                    }
                }
            }
            unreachable!("backoff loop should return on success or error");
        }

        debug!("Starting to fetch tools, resources, and prompts from existing session");

        // Get server info from session
        if let Some(ref server_info) = session.server_info {
            scan_data.server_info = Some(server_info.clone());
        }

        // Fetch tools, resources, and prompts using existing session with proper error handling
        let mut fetch_errors = Vec::new();

        scan_data.tools =
            match fetch_with_backoff(|| self.mcp_client.list_tools(session), "list_tools").await {
                Ok(tools) => {
                    debug!("Successfully fetched {} tools from session", tools.len());
                    tools
                }
                Err(e) => {
                    let error_msg = format!("Failed to fetch tools from session: {e}");
                    warn!("{}", error_msg);
                    fetch_errors.push(error_msg);
                    Vec::new()
                }
            };

        scan_data.resources = match self.mcp_client.list_resources(session).await {
            Ok(resources) => {
                debug!(
                    "Successfully fetched {} resources from session",
                    resources.len()
                );
                resources
            }
            Err(e) => {
                let error_msg = format!("Failed to fetch resources from session: {e}");
                warn!("{}", error_msg);
                fetch_errors.push(error_msg);
                Vec::new()
            }
        };

        scan_data.prompts = match self.mcp_client.list_prompts(session).await {
            Ok(prompts) => {
                debug!(
                    "Successfully fetched {} prompts from session",
                    prompts.len()
                );
                prompts
            }
            Err(e) => {
                let error_msg = format!("Failed to fetch prompts from session: {e}");
                warn!("{}", error_msg);
                fetch_errors.push(error_msg);
                Vec::new()
            }
        };

        // Store fetch errors in scan_data for later inclusion in final result
        scan_data.fetch_errors = fetch_errors;

        // Apply --only filter (same as the HTTP path).
        Self::apply_only_filter(&mut scan_data, options);

        // Clean up the session to prevent session deletion errors
        if let Err(e) = self.mcp_client.cleanup_session(session).await {
            warn!("Failed to clean up MCP session: {}", e);
        }

        Ok(scan_data)
    }

    /// Simple URL normalization for rmcp-based scanning
    fn normalize_url(url: &str) -> String {
        let mut normalized_url = url.to_string();

        // Add http:// if no scheme is provided
        if !normalized_url.contains("://") {
            normalized_url = format!("http://{normalized_url}");
        }

        normalized_url
    }

    /// Connect to an MCP server and return a session
    #[allow(dead_code)] // Future feature - will be used when scheduler is re-enabled
    pub async fn connect_to_server(
        &self,
        url: &str,
        auth_headers: Option<HashMap<String, String>>,
    ) -> Result<crate::types::MCPSession> {
        self.mcp_client.connect_smart(url, auth_headers).await
    }

    /// List tools from an MCP server session
    #[allow(dead_code)] // Future feature - will be used when scheduler is re-enabled
    pub async fn list_tools_from_session(
        &self,
        session: &crate::types::MCPSession,
    ) -> Result<Vec<crate::types::MCPTool>> {
        self.mcp_client.list_tools(session).await
    }
}

// Clone the MCPScanner
impl Clone for MCPScanner {
    fn clone(&self) -> Self {
        Self {
            client: self.client.clone(),
            http_timeout: self.http_timeout,
            middleware_chain: self.middleware_chain.clone(),
            mcp_client: self.mcp_client.clone(),
        }
    }
}

/// Intermediate data produced by the MCP probe step before analysis runs.
///
/// Made `pub` (was `pub(crate)`) and serializable so external callers can
/// drive the analyze-only path via `POST /v1/ramparts/analyze`:
/// they hand us this struct (typically obtained by their own listing of
/// the upstream MCP server) and we run the same security analysis stages
/// the live-scan path uses.
///
/// `#[serde(default)]` keeps the JSON wire format forgiving: clients may
/// omit any collection or `server_info` and the corresponding field
/// defaults to empty / None.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct ScanData {
    #[serde(default)]
    pub server_info: Option<MCPServerInfo>,
    #[serde(default)]
    pub tools: Vec<MCPTool>,
    #[serde(default)]
    pub resources: Vec<MCPResource>,
    #[serde(default)]
    pub prompts: Vec<MCPPrompt>,
    #[serde(default)]
    pub yara_results: Vec<YaraScanResult>,
    #[serde(default)]
    pub fetch_errors: Vec<String>,
}

// Scan data implementation
impl ScanData {
    pub fn new() -> Self {
        Self::default()
    }
}

// Implement Drop for MCPScanner to ensure proper cleanup
impl Drop for MCPScanner {
    fn drop(&mut self) {
        // Disable automatic cleanup in Drop to prevent race conditions in parallel scanning
        // Cleanup is now handled explicitly in scan methods
        debug!("MCPScanner dropped");
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_creates_threat_rules_with_and_without_yara_x() {
        // Use disabled YARA when feature is not available
        #[cfg(feature = "yara-x-scanning")]
        let scanner = ThreatRules::new("rules");
        #[cfg(not(feature = "yara-x-scanning"))]
        let scanner = ThreatRules::with_config("rules", false);

        assert!(
            scanner.is_ok(),
            "ThreatRules creation should succeed even when YARA is disabled"
        );

        let scanner = scanner.expect("Scanner creation should have succeeded");
        let stats = scanner.stats();

        // Should have loaded rules from the pre directory (only when YARA is enabled)
        #[cfg(feature = "yara-x-scanning")]
        assert!(stats.pre_scan_count > 0);
        #[cfg(not(feature = "yara-x-scanning"))]
        assert_eq!(stats.pre_scan_count, 0);

        println!("Loaded {} pre-scan rules", stats.pre_scan_count);
    }

    #[test]
    fn test_creates_yara_capability_with_correct_phase() {
        let scanner = YaraScanner::new("rules", ScanPhase::PreScan);
        assert!(
            scanner.is_ok(),
            "YaraScanner creation should succeed with valid rules directory"
        );

        let scanner_instance = scanner.expect("Scanner creation should have succeeded");
        assert_eq!(scanner_instance.name(), "yara");
        assert_eq!(scanner_instance.phase(), ScanPhase::PreScan);
    }

    #[test]
    fn test_reports_memory_usage_statistics() {
        #[cfg(feature = "yara-x-scanning")]
        let scanner = ThreatRules::new("rules")
            .expect("Should be able to create ThreatRules with rules directory");
        #[cfg(not(feature = "yara-x-scanning"))]
        let scanner = ThreatRules::with_config("rules", false)
            .expect("Should be able to create ThreatRules with YARA disabled");

        let memory_stats = scanner.memory_stats();

        #[cfg(feature = "yara-x-scanning")]
        assert!(memory_stats.pre_scan_count + memory_stats.post_scan_count > 0);
        #[cfg(not(feature = "yara-x-scanning"))]
        assert_eq!(
            memory_stats.pre_scan_count + memory_stats.post_scan_count,
            0
        );

        println!("Memory stats: {memory_stats:?}");
    }

    #[test]
    fn test_shares_rules_efficiently_via_arc_cloning() {
        // Create first scanner
        let scanner1 = ThreatRules::new("rules").unwrap();
        let memory1 = scanner1.memory_stats();

        // Clone should work without deadlocks
        let scanner2 = scanner1.clone();
        let memory2 = scanner2.memory_stats();

        // Both scanners should have same rule counts
        assert_eq!(memory1.pre_scan_count, memory2.pre_scan_count);
        assert_eq!(memory1.post_scan_count, memory2.post_scan_count);

        // Memory counts should match between cloned scanners
        assert_eq!(
            memory1.pre_scan_count + memory1.post_scan_count,
            memory2.pre_scan_count + memory2.post_scan_count
        );

        println!("Cache test passed - cloned scanner has identical memory usage");
    }

    #[test]
    fn test_validates_loaded_rules_successfully() {
        let scanner = ThreatRules::new("rules")
            .expect("Should be able to create ThreatRules with rules directory");
        let validation_result = scanner.validate();

        // Should not have validation errors
        assert!(
            validation_result.is_ok(),
            "YARA rule validation should pass for well-formed rules"
        );
        println!("Rule validation passed");
    }

    #[test]
    #[cfg(feature = "yara-x-scanning")]
    fn test_compiles_and_scans_with_yara_x_rules() {
        // Test actual YARA-X rule compilation from .yar files
        let test_rule = r#"
            rule TestRule {
                meta:
                    name = "Test Rule"
                    description = "A test rule for YARA-X integration"
                    severity = "MEDIUM"
                strings:
                    $test_string = "MALICIOUS_PATTERN"
                    $api_key = /[Aa][Pp][Ii].*[Kk][Ee][Yy]/
                condition:
                    $test_string or $api_key
            }
        "#;

        // Test compilation
        let mut compiler = yara_x::Compiler::new();
        assert!(
            compiler.add_source(test_rule).is_ok(),
            "Rule compilation should succeed"
        );

        let rules = compiler.build();

        // Test scanning with matches
        let mut scanner = yara_x::Scanner::new(&rules);
        let result = scanner.scan(b"This contains MALICIOUS_PATTERN text");
        assert!(result.is_ok(), "Scanning should succeed");

        let scan_results = result.expect("YARA-X scan should have succeeded");
        let matching_rules: Vec<_> = scan_results.matching_rules().collect();
        assert!(
            !matching_rules.is_empty(),
            "Should have matches for test pattern"
        );

        // Test scanning without matches
        let mut scanner2 = yara_x::Scanner::new(&rules);
        let result2 = scanner2.scan(b"Clean text with no malicious content");
        assert!(result2.is_ok(), "Scanning clean text should succeed");

        let scan_results2 = result2.expect("YARA-X scan on clean text should have succeeded");
        let matching_rules2: Vec<_> = scan_results2.matching_rules().collect();
        assert!(
            matching_rules2.is_empty(),
            "Should have no matches for clean text"
        );
    }

    #[test]
    #[cfg(feature = "yara-x-scanning")]
    fn test_extracts_metadata_from_yara_x_matches() {
        // Test metadata extraction from YARA-X rules
        let test_rule = r#"
            rule MetadataTest {
                meta:
                    name = "Metadata Test Rule"
                    author = "Test Author"
                    version = "2.0"
                    severity = "HIGH"
                    confidence = 0.95
                    tags = "test,metadata"
                strings:
                    $test = "test_pattern"
                condition:
                    $test
            }
        "#;

        let mut compiler = yara_x::Compiler::new();
        compiler
            .add_source(test_rule)
            .expect("Test rule should compile successfully with YARA-X");

        let rules = compiler.build();
        let mut scanner = yara_x::Scanner::new(&rules);
        let result = scanner.scan(b"test_pattern");
        assert!(
            result.is_ok(),
            "YARA-X scanning should succeed on test pattern"
        );

        let scan_results = result.expect("YARA-X scan should have succeeded");
        let matching_rules: Vec<_> = scan_results.matching_rules().collect();
        assert!(!matching_rules.is_empty());

        // Test metadata extraction
        let rule = &matching_rules[0];
        assert_eq!(rule.identifier(), "MetadataTest");

        let metadata: std::collections::HashMap<_, _> = rule.metadata().collect();
        assert!(metadata.contains_key("name"));
        assert!(metadata.contains_key("severity"));
        assert!(metadata.contains_key("confidence"));
    }

    #[test]
    #[cfg(feature = "yara-x-scanning")]
    fn test_rejects_malformed_yara_rules() {
        // Test error handling for malformed rules
        let malformed_rule = r#"
            rule MalformedRule {
                invalid_section:
                    this_is_not_valid_yara_syntax = "error"
                condition:
                    undefined_variable
            }
        "#;

        let mut compiler = yara_x::Compiler::new();
        let result = compiler.add_source(malformed_rule);
        assert!(
            result.is_err(),
            "Malformed rule should cause compilation error"
        );
    }

    #[test]
    fn test_loads_rules_from_filesystem() {
        // Test that the real YARA rules can be loaded
        let scanner = ThreatRules::new("rules")
            .expect("Should be able to create ThreatRules with rules directory");
        let stats = scanner.memory_stats();

        // Should have loaded the .yar rule files (only when YARA-X is enabled)
        #[cfg(feature = "yara-x-scanning")]
        {
            assert!(stats.pre_scan_count + stats.post_scan_count > 0);
            assert!(stats.pre_scan_count > 0);
        }

        #[cfg(not(feature = "yara-x-scanning"))]
        {
            // When YARA is disabled, rule counts should be 0
            assert_eq!(stats.pre_scan_count + stats.post_scan_count, 0);
        }

        // Test that the real Rules struct can be loaded (only when YARA-X is available)
        #[cfg(feature = "yara-x-scanning")]
        {
            // Load rules from .yar source file (YARA-X compiles on-the-fly)
            let rule_content = std::fs::read_to_string("rules/pre/secrets_leakage.yar")
                .expect("Should be able to read secrets_leakage.yar test rule file");

            let mut compiler = yara_x::Compiler::new();
            compiler
                .add_source(rule_content.as_str())
                .expect("Should be able to compile secrets_leakage.yar rule");

            let rules = compiler.build();

            // Test that the real scan method works
            let mut scanner = yara_x::Scanner::new(&rules);
            let scan_result = scanner.scan(b"test data");
            assert!(
                scan_result.is_ok(),
                "YARA-X scanning should succeed on test data"
            );

            // Real YARA-X may or may not have matches depending on rule content
            // Just verify we got a valid result (empty or with matches)
        }
    }

    #[test]
    fn test_runs_post_scan_capability_without_errors() {
        // Test that post-scan capability can be created and runs correctly
        let post_scanner = YaraScanner::new("rules", ScanPhase::PostScan);
        assert!(
            post_scanner.is_ok(),
            "YaraScanner creation should succeed for post-scan phase"
        );

        let scanner_instance =
            post_scanner.expect("Post-scan scanner creation should have succeeded");
        assert_eq!(scanner_instance.name(), "yara");
        assert_eq!(scanner_instance.phase(), ScanPhase::PostScan);

        // Create test scan data
        let mut scan_data = ScanData {
            server_info: None,
            tools: vec![],
            resources: vec![],
            prompts: vec![],
            yara_results: vec![],
            fetch_errors: vec![],
        };

        // Run post-scan scanner (should not error even with empty data)
        let result = scanner_instance.run(&mut scan_data);
        assert!(
            result.is_ok(),
            "Post-scan scanner should run successfully on scan data"
        );
        println!("Post-scan scanner test passed");
    }

    #[test]
    fn test_tracks_separate_pre_and_post_scan_statistics() {
        #[cfg(feature = "yara-x-scanning")]
        let scanner = ThreatRules::new("rules")
            .expect("Should be able to create ThreatRules with rules directory");
        #[cfg(not(feature = "yara-x-scanning"))]
        let scanner = ThreatRules::with_config("rules", false)
            .expect("Should be able to create ThreatRules with YARA disabled");

        let stats = scanner.stats();

        println!("Pre-scan rules: {}", stats.pre_scan_count);
        println!("Post-scan rules: {}", stats.post_scan_count);

        // Should have at least some pre-scan rules (only when YARA is enabled)
        #[cfg(feature = "yara-x-scanning")]
        assert!(stats.pre_scan_count > 0);
        #[cfg(not(feature = "yara-x-scanning"))]
        assert_eq!(stats.pre_scan_count, 0);

        // Should have post-scan rules if we created test rule
        if stats.post_scan_count > 0 {
            println!("Post-scan rules detected: {}", stats.post_scan_count);
        } else {
            println!("No post-scan rules found (this is expected if none were created)");
        }
    }

    #[test]
    fn test_collects_rule_names_from_loaded_files() {
        // Test that the dynamic rule names are being collected correctly
        let scanner = ThreatRules::new("rules")
            .expect("Should be able to create ThreatRules with rules directory");
        let stats = scanner.stats();

        // Should have the expected pre-scan rules
        #[cfg(feature = "yara-x-scanning")]
        {
            assert!(!stats.pre_scan_rules.is_empty());
            assert!(stats
                .pre_scan_rules
                .contains(&"command_injection".to_string()));
            assert!(stats.pre_scan_rules.contains(&"path_traversal".to_string()));
            assert!(stats
                .pre_scan_rules
                .contains(&"secrets_leakage".to_string()));
            println!("Pre-scan rules: {:?}", stats.pre_scan_rules);
        }

        #[cfg(not(feature = "yara-x-scanning"))]
        {
            assert!(stats.pre_scan_rules.is_empty());
        }

        // Post-scan rules should be empty for now
        assert!(stats.post_scan_rules.is_empty());
        println!("Post-scan rules: {:?}", stats.post_scan_rules);
    }
}