ntoseye 0.20.0

Windows kernel debugger for Linux hosts running Windows under KVM/QEMU
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
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
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
use rmcp::{
    ErrorData as McpError, ServiceExt,
    handler::server::router::tool::ToolRouter,
    handler::server::wrapper::Parameters,
    model::{CallToolResult, Implementation, ProtocolVersion, ServerCapabilities, ServerInfo},
    tool, tool_handler, tool_router,
    transport::stdio,
};
use serde::Deserialize;
use serde_json::Value;
use tokio::sync::{mpsc, oneshot};
use tokio_util::sync::CancellationToken;

use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::time::Duration;

use crate::backend::MemoryOps;
use crate::bugchecks::{analyze_bugcheck, bugcheck_from_dump_info, current_bugcheck};
use crate::dbg_backend::{DebugBackend, WatchpointAccess};
use crate::dmp::{DmpBackend, DmpException, DmpSystemInfo, TriageCrashInfo, UnloadedDriver};
use crate::error::Error;
use crate::expr::Expr;
use crate::gdb::GdbClient;
use crate::guest::ModuleInfo;
use crate::kd::KdBackend;
use crate::memory_backend::MemoryBackend;
use crate::phys::PhysMem;
use crate::session::{ContinueOutcome, RunStatus, Session};
use crate::symbols::{FieldValue, TypeInfo};
use crate::target::{kthread_state_name, wait_reason_name};
use crate::trapframe::read_ktrap_frame_at_or_current;
use crate::triage::TriagePrcbInfo;
use crate::types::VirtAddr;
use crate::unwind::StackFrame;
use crate::view;
use crate::{DEFAULT_KD_SOCKET, resolve_target};

// MCP handlers are async/Send, but `Session` is not. A dedicated actor thread
// owns the session; handlers send closures and await one reply.
/// A unit of work run on the actor thread that owns the (`!Send`) session.
/// Returns JSON on success or a display string on error.
type SessionJob = Box<dyn FnOnce(&mut Session) -> Result<Value, ToolError> + Send>;

/// What the async handlers ask the actor to do.
enum Command {
    /// Run a job against the session and reply with its result.
    Run {
        job: SessionJob,
        reply: oneshot::Sender<Result<Value, ToolError>>,
    },
    /// Clean up the session (remove our breakpoints, resume the VM) and stop the
    /// actor. `ack` fires once cleanup is done so the caller can exit knowing the
    /// guest isn't left frozen with `int3`s installed.
    Shutdown { ack: oneshot::Sender<()> },
    /// A periodic nudge (from the background ticker) for the actor to service the
    /// guest while otherwise idle; absorb wrong-process hits on a shared-page
    /// breakpoint so they don't leave it frozen between tool calls. Carries no
    /// reply; the actor just runs `Session::service_idle`.
    Service,
}

/// How often the background ticker nudges the actor to service the guest (see
/// [`Command::Service`]). Small enough that a wrong-process breakpoint hit is
/// absorbed within a frame, large enough that the idle no-op is negligible.
const SERVICE_TICK: Duration = Duration::from_millis(20);

/// Tear down so the guest isn't left frozen at a stop with our `int3`s patched
/// in: remove breakpoints and leave the VM running (the halt-then-resume detail
/// lives in `Session::cleanup_for_exit`).
fn cleanup_session(ctx: &mut Session) {
    let _ = ctx.cleanup_for_exit();
}

/// Spawn the actor thread that owns the (`!Send`) debugging session. The backend
/// is constructed *on this thread* so the non-`Send` state never crosses a
/// thread boundary. Returns the sender the MCP handlers use to reach it.
fn spawn_session(
    backend: String,
    connect: Option<String>,
    dump: Option<PathBuf>,
) -> anyhow::Result<(mpsc::UnboundedSender<Command>, Arc<AtomicBool>)> {
    let (ready_tx, ready_rx) = std::sync::mpsc::channel::<Result<(), String>>();
    let (tx, mut rx) = mpsc::unbounded_channel::<Command>();

    // Coalesces the background `Service` nudges: the ticker only enqueues one when
    // this is false (and sets it), the actor clears it as it services. Caps queued
    // `Service` commands at one, so a long `wait_for_stop` can't let a burst of
    // them pile up in the unbounded channel and drain all at once afterward.
    let service_pending = Arc::new(AtomicBool::new(false));
    let service_pending_actor = service_pending.clone();

    std::thread::spawn(move || {
        let is_dump = dump.is_some();

        // `connect` takes the per-target instance lock (on this actor thread,
        // where the `!Send` session lives) before building the backend, so a
        // second ntoseye can't attach to the same resource. Dumps and passive
        // memory introspection pass `None` (no lock needed).
        let built = (|| {
            if let Some(dump_path) = &dump {
                let phys = Arc::new(PhysMem::dmp(dump_path)?);
                let info = phys
                    .dmp_info()
                    .expect("dmp_info must be Some for DMP backend")
                    .clone();
                Session::connect(phys, None, || -> Result<Box<dyn DebugBackend>, Error> {
                    Ok(Box::new(DmpBackend::new(&info)))
                })
            } else {
                let target = resolve_target(backend.as_str(), connect.as_deref());
                let phys = Arc::new(PhysMem::kvm()?);
                Session::connect(phys, target.as_deref(), || {
                    let backend: Box<dyn DebugBackend> = match backend.as_str() {
                        "gdb" => Box::new(GdbClient::connect(target.as_deref().unwrap())?),
                        "kd" => Box::new(KdBackend::connect(target.as_deref().unwrap())?),
                        "memory" => Box::new(MemoryBackend::new()),
                        other => {
                            return Err(Error::DebugInfo(format!("unknown backend '{other}'")));
                        }
                    };
                    Ok(backend)
                })
            }
        })()
        .map_err(|e| e.to_string());

        let mut ctx = match built {
            Ok(ctx) => {
                let _ = ready_tx.send(Ok(()));
                ctx
            }
            Err(e) => {
                let _ = ready_tx.send(Err(e));
                return;
            }
        };

        // For live targets the MCP keeps the guest running between tool calls;
        // tools that need a stopped target ask the client to call `interrupt`
        // first. Dumps are always halted — skip the resume.
        if !is_dump && !ctx.backend.is_running() {
            let _ = ctx.backend.continue_execution();
        }

        // Synchronous job loop. `blocking_recv` is valid here because this is a
        // plain std thread with no tokio runtime on it. We clean up on an explicit
        // `Shutdown` (Ctrl+C / client disconnect drives one) and also if the
        // channel closes outright, so the VM is never left frozen.
        loop {
            match rx.blocking_recv() {
                Some(Command::Run { job, reply }) => {
                    // DIAGNOSTIC: every tool call submits exactly one Run job, so
                    // this logs whether the actor runs anything while you believe
                    // the session is idle (e.g. a client auto-probing tools).
                    // Gated on the same env as the kd traces so it interleaves.
                    if std::env::var_os("NTOSEYE_KD_TRACE").is_some() {
                        eprintln!("mcp: actor: running a tool job");
                    }
                    let _ = reply.send(job(&mut ctx));
                }
                Some(Command::Service) => {
                    service_pending_actor.store(false, Ordering::Release);
                    ctx.service_idle();
                }
                Some(Command::Shutdown { ack }) => {
                    cleanup_session(&mut ctx);
                    drop(ctx);
                    let _ = ack.send(());
                    break;
                }
                None => {
                    cleanup_session(&mut ctx);
                    break;
                }
            }
        }
    });

    match ready_rx.recv() {
        Ok(Ok(())) => Ok((tx, service_pending)),
        Ok(Err(e)) => Err(anyhow::anyhow!("failed to attach: {e}")),
        Err(_) => Err(anyhow::anyhow!("session thread exited before attaching")),
    }
}

/// The one shared session slot all transports funnel into. `Opening(gen)`
/// reserves the slot while `open_dump`/`open` builds a session off-thread,
/// so a concurrent open can't race the vacancy check and leak a second
/// actor. The generation counter lets [`OpeningGuard`] detect when its
/// reservation has been cancelled by a concurrent `close`.
enum SessionSlot {
    Vacant,
    Opening(u64),
    Active(mpsc::UnboundedSender<Command>),
}

static OPENING_GENERATION: AtomicU64 = AtomicU64::new(0);

type SharedSession = Arc<std::sync::Mutex<SessionSlot>>;

/// RAII guard that rolls `SessionSlot` back to `Vacant` if the opening future
/// is cancelled (e.g. MCP client timeout). Call [`OpeningGuard::promote`] to
/// install the active sender and defuse the rollback. The generation counter
/// ensures promote/drop only act on the `Opening` this guard created, not one
/// set by a concurrent open after a close race.
struct OpeningGuard {
    session: SharedSession,
    generation: u64,
}

impl OpeningGuard {
    fn claim(session: &SharedSession) -> Result<Self, McpError> {
        let mut guard = session.lock().unwrap();
        match *guard {
            SessionSlot::Vacant => {
                let id = OPENING_GENERATION.fetch_add(1, Ordering::Relaxed);
                *guard = SessionSlot::Opening(id);
                Ok(Self {
                    session: session.clone(),
                    generation: id,
                })
            }
            _ => Err(McpError::invalid_request(
                "a debugger session is already active or opening",
                None,
            )),
        }
    }

    fn promote(self, tx: mpsc::UnboundedSender<Command>) -> Result<(), McpError> {
        let mut guard = self.session.lock().unwrap();
        match *guard {
            SessionSlot::Opening(id) if id == self.generation => {
                *guard = SessionSlot::Active(tx);
                Ok(())
            }
            _ => Err(McpError::internal_error("session open was cancelled", None)),
        }
    }
}

impl Drop for OpeningGuard {
    fn drop(&mut self) {
        let mut guard = self.session.lock().unwrap();
        if matches!(*guard, SessionSlot::Opening(id) if id == self.generation) {
            *guard = SessionSlot::Vacant;
        }
    }
}

struct InterruptResetGuard(Arc<AtomicBool>);

impl Drop for InterruptResetGuard {
    fn drop(&mut self) {
        self.0.store(false, Ordering::Relaxed);
    }
}

#[derive(Clone)]
struct NtoseyeMcp {
    session: SharedSession,
    #[allow(dead_code)] // accessed by the #[tool_router] macro expansion
    tool_router: ToolRouter<Self>,
    /// Flipped on shutdown so an in-flight `wait_for_stop` bails out promptly and
    /// the actor can run cleanup (resume the VM) before exit.
    interrupt: Arc<AtomicBool>,
}

// --- argument schemas ---

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct AddressArgs {
    #[schemars(
        description = "Address as a debugger expression (symbol, register, hex, arithmetic)"
    )]
    address: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct TrapFrameArgs {
    #[schemars(
        description = "Optional trap-frame address as a debugger expression; omit to use the current Windows thread's saved KTHREAD.TrapFrame"
    )]
    address: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct ReadMemoryArgs {
    #[schemars(description = "Start address as a debugger expression")]
    address: String,
    #[schemars(
        range(min = 1, max = 4096),
        description = "Number of bytes to read (range 1-4096)"
    )]
    length: usize,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct ReadCStringArgs {
    #[schemars(
        description = "Address of the NUL-terminated CHAR* string as a debugger expression"
    )]
    address: String,
    #[schemars(
        range(min = 1, max = 4096),
        description = "Maximum bytes to scan for the NUL terminator (default 260, range 1-4096)"
    )]
    max_len: Option<usize>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct DisassembleArgs {
    #[schemars(description = "Start address as a debugger expression")]
    address: String,
    #[schemars(
        range(min = 1, max = 128),
        description = "Number of instructions to decode (range 1-128)"
    )]
    count: usize,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct BacktraceArgs {
    #[schemars(
        range(min = 1, max = 256),
        description = "Maximum number of frames to walk (default 64, range 1-256)"
    )]
    limit: Option<usize>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct TypeArgs {
    #[serde(rename = "type")]
    #[schemars(description = "Type name to look up across loaded modules, e.g. _EPROCESS")]
    type_name: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct SymbolSearchArgs {
    #[schemars(
        description = "Fuzzy symbol query; `module!query` (e.g. `nt!KiSwap`) scopes to one module"
    )]
    query: String,
    #[schemars(range(min = 1, max = 500), description = "Max results (default 50)")]
    limit: Option<usize>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct TypeSearchArgs {
    #[schemars(description = "Fuzzy type-name query, e.g. `_EPROCESS` or `KTHREAD`")]
    query: String,
    #[schemars(range(min = 1, max = 500), description = "Max results (default 50)")]
    limit: Option<usize>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct EnumSearchArgs {
    #[schemars(description = "Fuzzy enum-name query, e.g. `_KWAIT_REASON` or `_POOL_TYPE`")]
    query: String,
    #[schemars(range(min = 1, max = 500), description = "Max results (default 50)")]
    limit: Option<usize>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct ReadStructArgs {
    #[serde(rename = "type")]
    #[schemars(description = "Type name, e.g. _EPROCESS")]
    type_name: String,
    #[schemars(description = "Address of the struct as a debugger expression")]
    address: String,
}

/// Shared paging+filter args for the naturally-bounded enumerations
/// (processes/modules/drivers). `offset`/`limit` window the list like reading a
/// file by line range; `filter` is the ergonomic win for finding one entry.
#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct ListArgs {
    #[schemars(
        description = "Case-insensitive substring matched against the name (find a specific entry instead of paging all)"
    )]
    filter: Option<String>,
    #[schemars(
        description = "0-based index to start at (default 0). To page, pass the previous page's `next_offset`; absent means done."
    )]
    offset: Option<usize>,
    #[schemars(
        range(min = 1, max = 500),
        description = "Window size: how many to return from offset (default 100, range 1-500)"
    )]
    limit: Option<usize>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct ThreadsArgs {
    #[schemars(description = "Only threads belonging to this process id")]
    pid: Option<u64>,
    #[schemars(
        description = "0-based index to start at (default 0). To page, pass the previous page's `next_offset`; absent means done."
    )]
    offset: Option<usize>,
    #[schemars(
        range(min = 1, max = 200),
        description = "Window size (default 50, range 1-200). A live system has 1000+ threads; filter by pid and/or page via offset."
    )]
    limit: Option<usize>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct MemoryMapArgs {
    #[schemars(description = "Process id whose VAD tree to enumerate")]
    pid: u64,
    #[schemars(
        description = "0-based index to start at (default 0). To page, pass the previous page's `next_offset`; absent means done."
    )]
    offset: Option<usize>,
    #[schemars(
        range(min = 1, max = 500),
        description = "Window size (default 200, range 1-500)."
    )]
    limit: Option<usize>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct PidArgs {
    #[schemars(description = "Process id to attach the inspection context to")]
    pid: u64,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct SetBreakpointArgs {
    #[schemars(
        description = "Breakpoint address as a debugger expression, e.g. user32!PostQuitMessage"
    )]
    address: String,
    #[schemars(
        description = "Optional break condition, parsed when installed and re-evaluated each hit; the breakpoint only surfaces when it holds. Uses the normal expression grammar, including comparisons (== != < <= > >=), bitwise operators (~ & ^ | << >>), and short-circuiting !, &&, ||."
    )]
    condition: Option<String>,
}

#[derive(Clone, Copy, Debug, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "snake_case")]
enum WatchpointAccessArg {
    Write,
    ReadWrite,
}

impl From<WatchpointAccessArg> for WatchpointAccess {
    fn from(access: WatchpointAccessArg) -> Self {
        match access {
            WatchpointAccessArg::Write => Self::Write,
            WatchpointAccessArg::ReadWrite => Self::ReadWrite,
        }
    }
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct SetWatchpointArgs {
    #[schemars(description = "Data address to watch as a debugger expression")]
    address: String,
    #[schemars(
        description = "Access to watch: write, or read_write (x86 cannot trap reads without also trapping writes)"
    )]
    access: WatchpointAccessArg,
    #[schemars(
        range(min = 1, max = 8),
        description = "Watched width in bytes: 1, 2, 4, or 8; address must be naturally aligned"
    )]
    length: u8,
    #[schemars(
        description = "Optional condition, parsed when installed and re-evaluated after the watch fires. Uses the normal expression grammar, including comparisons, bitwise operators, and short-circuiting !, &&, ||."
    )]
    condition: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct BreakpointIdArgs {
    #[schemars(description = "Breakpoint id")]
    id: u32,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct SearchArgs {
    #[schemars(description = "Start address as a debugger expression")]
    start: String,
    #[schemars(
        description = "Byte pattern to find, as contiguous lowercase hex, e.g. \"48895c24\""
    )]
    pattern: String,
    #[schemars(
        range(min = 1, max = 1048576),
        description = "How many bytes to scan from start (range 1-1048576)"
    )]
    length: usize,
    #[schemars(
        description = "0-based index into the match list to start at (default 0). To page, pass the previous page's `next_offset`; absent means done."
    )]
    offset: Option<usize>,
    #[schemars(
        range(min = 1, max = 500),
        description = "Max matches to return from offset (default 50, range 1-500). A short pattern over a dense region can match millions of times."
    )]
    limit: Option<usize>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct WalkListArgs {
    #[schemars(
        description = "List-head address as a debugger expression, e.g. PsLoadedModuleList"
    )]
    head: String,
    #[schemars(description = "Record struct type the list links into, e.g. _LDR_DATA_TABLE_ENTRY")]
    record_type: String,
    #[schemars(
        description = "Name of the _LIST_ENTRY field inside the record, e.g. InLoadOrderLinks"
    )]
    link_field: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct WriteMemoryArgs {
    #[schemars(description = "Start address as a debugger expression")]
    address: String,
    #[schemars(
        description = "Bytes to write as contiguous lowercase hex (same form read_memory returns), e.g. \"90909090\". 1-4096 bytes."
    )]
    hex: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct ThreadIdArgs {
    #[schemars(description = "Thread/vCPU id to select as the current inspection thread")]
    thread: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct SetRegisterArgs {
    #[schemars(description = "Register name, e.g. rip, rax, rflags")]
    name: String,
    #[schemars(
        description = "New value as a debugger expression (symbol, register, hex, arithmetic)"
    )]
    value: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct OpenDumpArgs {
    #[schemars(description = "Absolute path to a Windows kernel crash dump (.dmp) file")]
    path: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct OpenArgs {
    #[schemars(
        description = "Backend to use: \"kd\" (KD over Unix socket), \"gdb\" (GDB remote stub), or \"memory\" (physical memory only, no debug transport)"
    )]
    backend: String,
    #[schemars(
        description = "Connection target: Unix socket path for kd (default /tmp/ntoseye-kd.sock), host:port for gdb (default 127.0.0.1:1234). Ignored for memory backend."
    )]
    connect: Option<String>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct EvalArgs {
    #[schemars(
        description = "Debugger expression to evaluate (symbol name, hex address, register, arithmetic, or poi())"
    )]
    expression: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct ReadPointerArgs {
    #[schemars(description = "Address expression to read a pointer-sized (8-byte) value from")]
    address: String,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct WaitArgs {
    #[schemars(
        range(min = 0, max = 20000),
        description = "How long to wait for a stop before returning {stop:\"running\"} (default 10000, max 20000; 0 means use the default). Bounded by design: poll by calling again while it returns running. There is no indefinite wait; your MCP client times out the request on its own, and a long wait blocks every other tool on the single debugger session."
    )]
    timeout_ms: Option<u64>,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
struct DebugLogArgs {
    #[schemars(
        description = "Resume cursor: pass the previous call's `next_seq` to get only lines captured since then. Default 0 returns from the oldest retained line."
    )]
    since_seq: Option<u64>,
}

const LIST_DEFAULT_LIMIT: usize = 100;
const LIST_MAX_LIMIT: usize = 500;
const THREADS_DEFAULT_LIMIT: usize = 50;
const THREADS_MAX_LIMIT: usize = 200;
const MEMORY_MAP_DEFAULT_LIMIT: usize = 200;
const MEMORY_MAP_MAX_LIMIT: usize = 500;
const READ_MEMORY_MAX_LENGTH: usize = 4096;
const READ_C_STRING_MAX_LENGTH: usize = 4096;
const READ_C_STRING_DEFAULT_LENGTH: usize = 260; // MAX_PATH, the common case
const WRITE_MEMORY_MAX_LENGTH: usize = 4096;
const SEARCH_MAX_LENGTH: usize = 1 << 20; // 1 MiB scanned per call
const SEARCH_DEFAULT_LIMIT: usize = 50;
const SEARCH_MAX_LIMIT: usize = 500;

const DISASSEMBLE_MAX_COUNT: usize = 128;
const BACKTRACE_DEFAULT_LIMIT: usize = 64;
const BACKTRACE_MAX_LIMIT: usize = 256;
const CONTINUE_DEFAULT_TIMEOUT_MS: u64 = 10_000;
// Capped well under common MCP client request timeouts (e.g. oh-my-pi defaults to
// 30s) so a wait returns {stop:"running"} and frees the single-session actor
// before the client gives up. No indefinite wait is offered over MCP.
const CONTINUE_MAX_TIMEOUT_MS: u64 = 20_000;

/// A tool failure carrying enough structure to map to the right MCP error class.
/// Keeps guest memory faults distinguishable from bad arguments and internal bugs.
enum ToolError {
    /// A bad argument value (JSON-RPC invalid params).
    Params(String),
    /// Well-formed but not valid in the current state (invalid request).
    Request(String),
    /// A guest memory access fault (unmapped page, partial read, ...).
    Memory(String),
    /// Anything else (internal error).
    Internal(String),
}

impl From<Error> for ToolError {
    fn from(e: Error) -> Self {
        let msg = e.to_string();
        match e {
            Error::BadVirtualAddress(_)
            | Error::AddressNotInDump(_)
            | Error::BadPhysicalAddress(_)
            | Error::PartialRead(_)
            | Error::PartialWrite(_)
            | Error::BufferNotEnough
            | Error::InvalidRange => ToolError::Memory(msg),
            _ => ToolError::Internal(msg),
        }
    }
}

impl ToolError {
    fn into_mcp(self) -> McpError {
        match self {
            ToolError::Params(m) => McpError::invalid_params(m, None),
            ToolError::Request(m) => McpError::invalid_request(m, None),
            // No dedicated JSON-RPC code for a guest fault; surface it as an
            // invalid request tagged with `kind` so a client can tell "this
            // address isn't readable" apart from an internal bug.
            ToolError::Memory(m) => {
                McpError::invalid_request(m, Some(serde_json::json!({ "kind": "memory_access" })))
            }
            ToolError::Internal(m) => McpError::internal_error(m, None),
        }
    }
}

fn invalid_params(message: impl Into<String>) -> McpError {
    McpError::invalid_params(message.into(), None)
}

fn invalid_request(message: impl Into<String>) -> ToolError {
    ToolError::Request(message.into())
}

/// Turn a kernel-enumeration failure into an actionable directive when the kernel
/// lists aren't up yet (early boot / mid-rediscovery): the process and module
/// lists don't exist, so the raw "bad virtual address" is noise. Only rewrites
/// when the target is known-incoherent, so a genuine bad address still surfaces
/// as itself (classified by `ToolError::from`).
fn enumeration_error(ctx: &Session, e: Error) -> ToolError {
    if !ctx.kernel_coherent() {
        invalid_request(
            "guest is rebooting or at early boot: kernel process/module lists are not up yet, \
             so enumeration is unavailable. resume, then wait_for_stop until status reports \
             coherent:true, then retry.",
        )
    } else {
        ToolError::from(e)
    }
}

fn required_range(name: &str, value: usize, max: usize) -> Result<usize, McpError> {
    if value == 0 || value > max {
        Err(invalid_params(format!("{name} must be in range 1..={max}")))
    } else {
        Ok(value)
    }
}

fn optional_limit(
    name: &str,
    value: Option<usize>,
    default: usize,
    max: usize,
) -> Result<usize, McpError> {
    match value {
        Some(v) => required_range(name, v, max),
        None => Ok(default),
    }
}

fn optional_timeout_ms(value: Option<u64>) -> Result<u64, McpError> {
    // 0 or omitted means "use the default". There is deliberately no indefinite
    // wait over MCP: the client owns the request timeout, and a long wait pins the
    // single-session actor, so callers poll instead (a bounded wait returns
    // {stop:"running"} and the caller calls again).
    let ms = match value {
        None | Some(0) => CONTINUE_DEFAULT_TIMEOUT_MS,
        Some(ms) => ms,
    };
    if ms > CONTINUE_MAX_TIMEOUT_MS {
        Err(invalid_params(format!(
            "timeout_ms must be 0 (use default) or in range 1..={CONTINUE_MAX_TIMEOUT_MS}"
        )))
    } else {
        Ok(ms)
    }
}

fn require_halted(ctx: &mut Session, tool: &str) -> Result<(), ToolError> {
    // A stop the servicer already caught (e.g. an early-boot break) leaves the VM
    // physically halted while `is_running()` still reads stale-true. Ingest it
    // first so a genuinely halted target isn't turned away with "call interrupt";
    // a debugger-noise break-in is absorbed (resume), so only a VM that is truly
    // running, or stopped on a real, inspectable site, reaches the guard.
    ctx.settle_pending_stop().map_err(ToolError::from)?;
    // This op runs against the (already-rebuilt) current kernel, so the host has
    // engaged the target post-reboot: finish any memory-completable rediscovery
    // and stop deferring the reboot notification, so a later `wait_for_stop`
    // doesn't flush a stale `target_reloaded` into the middle of the agent's flow
    // (e.g. right after it set a kernel breakpoint). Matches what `status` does.
    ctx.try_finish_rediscovery_from_memory();
    ctx.clear_deferred_reload_surface();
    if ctx.backend.is_running() {
        Err(invalid_request(format!(
            "{tool} requires the VM to be halted; call interrupt first (resume afterwards with resume)"
        )))
    } else {
        Ok(())
    }
}

/// Render a [`ContinueOutcome`] as JSON, enriching breakpoint/exception stops with
/// the current process and resolved symbol from `ctx`.
fn continue_outcome_json(ctx: &Session, outcome: ContinueOutcome) -> Value {
    let process = ctx
        .target
        .current_process_info
        .as_ref()
        .map(|p| serde_json::json!({ "pid": p.pid, "name": p.name }));
    let symbol_at = |rip: u64| ctx.target.closest_symbol_current_context(VirtAddr(rip));
    match outcome {
        ContinueOutcome::Breakpoint {
            id,
            address,
            symbol,
            temporary,
            rip,
            condition_error,
        } => {
            let bp = ctx.breakpoint(id);
            let watch_access = bp.and_then(|bp| bp.watch_access_name());
            serde_json::json!({
                "stop": if watch_access.is_some() { "watchpoint" } else { "breakpoint" },
                "id": id,
                "address": hex(address),
                "symbol": symbol.or_else(|| symbol_at(rip)),
                "temporary": temporary,
                "rip": hex(rip),
                "process": process,
                "watch_access": watch_access,
                "watch_length": bp.and_then(|bp| bp.watch_length()),
                "condition_error": condition_error,
            })
        }
        ContinueOutcome::Bugcheck { rip, info } => {
            let analysis = info
                .map(|i| analyze_bugcheck(&ctx.target, &i))
                .or_else(|| current_bugcheck(&ctx.target))
                .or_else(|| bugcheck_from_dump_info(&ctx.target));
            serde_json::json!({
                "stop": "bugcheck",
                "rip": rip.map(hex),
                "bugcheck": analysis.as_ref().map(|a| view::to_json(&view::bugcheck(a))),
            })
        }
        ContinueOutcome::Stopped {
            rip,
            exception_code,
        } => serde_json::json!({
            "stop": "exception",
            "rip": hex(rip),
            "exception_code": exception_code,
            "symbol": symbol_at(rip),
            "process": process,
        }),
        ContinueOutcome::Step { rip } => serde_json::json!({
            "stop": "step",
            "rip": hex(rip),
            "symbol": symbol_at(rip),
            "process": process,
        }),
        ContinueOutcome::TargetReloaded {
            kernel_base,
            coherent,
        } => {
            let note = if coherent {
                "The guest rebooted and debugger state is now fully rebuilt against \
                 the new kernel: the loaded-module list is available and the system \
                 is introspectable. The VM is halted at an internal KD break-in (an \
                 arbitrary landing site, not a meaningful location). Every prior \
                 address (eprocess, ethread/kthread, module base, dtb) from before \
                 the reboot is invalid; re-enumerate (processes/threads/modules) \
                 before acting."
            } else {
                "The guest rebooted and the VM is halted at the earliest post-reboot \
                 stop, before kernel initialization: kernel symbols are loaded, but \
                 the loaded-module list does not exist yet, so process, thread, and \
                 module enumeration are UNAVAILABLE at this stop and most reads will \
                 fail or mislead. Every prior address (eprocess, ethread/kthread, \
                 module base, dtb) is now invalid; do not reuse results from before \
                 the reload. Use this stop to debug early boot (e.g. set breakpoints \
                 on init paths; hits are reported even while the system is coming \
                 up). Otherwise to reach a usable system: resume, then poll \
                 wait_for_stop, and only enumerate once status reports \
                 coherent:true (rediscovery finishes silently as the system comes \
                 up; this is the only reload stop reported). Enumerating while \
                 coherent:false returns a directive error, not data."
            };
            serde_json::json!({
                "stop": "target_reloaded",
                "kernel_base": kernel_base.map(hex),
                "coherent": coherent,
                "note": note,
            })
        }
        ContinueOutcome::Running => serde_json::json!({ "stop": "running" }),
        ContinueOutcome::Halted { rip } => serde_json::json!({
            "stop": "halted",
            // Not a new event; the VM was already parked here, so don't treat it
            // like a fresh breakpoint/exception stop.
            "event": false,
            "rip": hex(rip),
            "symbol": symbol_at(rip),
            "process": process,
            "coherent": ctx.kernel_coherent(),
        }),
    }
}

// --- value decoding helpers (run on the actor thread) ---

/// Resolve an address-expression argument against the live session.
fn eval_addr(ctx: &Session, expr: &str) -> Result<u64, ToolError> {
    Expr::eval(expr, &ctx.target)
        .map(|v| v.0)
        .map_err(|e| ToolError::Params(e.to_string()))
}

/// Lowercase contiguous hex for a byte slice.
fn hex_of(slice: &[u8]) -> String {
    slice.iter().map(|b| format!("{b:02x}")).collect()
}

/// Decode contiguous hex (the form `hex_of`/`read_memory` emit) back to bytes.
/// Rejects odd length and non-hex digits as invalid params.
fn bytes_of_hex(s: &str) -> Result<Vec<u8>, ToolError> {
    let s = s.strip_prefix("0x").unwrap_or(s);
    if s.len() % 2 != 0 {
        return Err(invalid_request("hex must have an even number of digits"));
    }
    (0..s.len())
        .step_by(2)
        .map(|i| {
            u8::from_str_radix(&s[i..i + 2], 16)
                .map_err(|_| invalid_request("hex contains non-hex digits"))
        })
        .collect()
}

/// Format an address/value as a `0x` hex string. JSON numbers are decimal-only,
/// so addresses (which debugger users always read in hex) are emitted as strings.
fn hex(v: u64) -> String {
    format!("{v:#x}")
}

/// Decode a struct's fields the same way the Python SDK does: scalars (1/2/4/8
/// bytes) and pointers become ints, bitfields are masked to their value, and
/// larger aggregates become a hex string. Nested-struct fields the PDB reports
/// with size 0 are omitted; read those separately with their own type.
fn decode_struct(info: &TypeInfo, buf: &[u8]) -> Value {
    let mut map = serde_json::Map::new();
    for (name, value) in info.decode_fields(buf) {
        // A struct dump renders every scalar in hex (WinDbg `dt` convention):
        // type-stable and predictable, so the same field never flips between
        // decimal and hex across instances. Most 8-byte fields are addresses
        // anyway (ULONG_PTR/ULONGLONG the PDB types as ints, e.g.
        // _LOADER_PARAMETER_BLOCK.KernelStack/Prcb/Process); counts, sizes and
        // versions read as hex too, which a consumer converts trivially.
        // Aggregates stay contiguous bytes.
        let v = match value {
            FieldValue::Int(n) | FieldValue::Bitfield(n) | FieldValue::Pointer(n) => {
                Value::from(hex(n))
            }
            FieldValue::Bytes(b) => Value::from(hex_of(&b)),
        };
        map.insert(name, v);
    }
    Value::Object(map)
}

/// Build a paged list envelope: `{total, offset, returned, has_more, next_offset?, <key>: [...]}`.
/// `next_offset` is the offset of the next page, present only when more pages remain.
/// `total` is the count after filtering; `items` is the windowed slice.
fn paged(total: usize, offset: usize, key: &str, items: Vec<Value>) -> Value {
    let returned = items.len();
    let end = offset.saturating_add(returned);
    let mut obj = serde_json::json!({
        "total": total,
        "offset": offset,
        "returned": returned,
        "has_more": end < total,
    });
    if end < total {
        obj["next_offset"] = end.into();
    }
    obj[key] = Value::Array(items);
    obj
}

/// Wrap a JSON value as a successful tool result (pretty-printed text content).
// Structured tool result. rmcp's `structured()` also embeds a compact-text copy
// in `content`, so clients that ignore structuredContent still get JSON.
fn json_result(v: Value) -> Result<CallToolResult, McpError> {
    Ok(CallToolResult::structured(v))
}

fn run_status_json(s: RunStatus) -> Value {
    let process = s.process.map(|(pid, name, eprocess)| {
        serde_json::json!({ "pid": pid, "name": name, "eprocess": hex(eprocess) })
    });
    serde_json::json!({
        "running": s.running,
        "current_thread": s.current_thread,
        "rip": s.rip.map(hex),
        "symbol": s.symbol,
        "process": process,
        "coherent": s.coherent,
        "kernel_base": hex(s.kernel_base),
    })
}

fn status_json(ctx: &mut Session) -> Value {
    run_status_json(ctx.run_status())
}

fn frame_json(f: &StackFrame) -> Value {
    serde_json::json!({
        "ip": hex(f.ip),
        "sp": hex(f.sp),
        "symbol": f.symbol,
        "source": f.source.as_str(),
    })
}

fn module_json(m: &ModuleInfo) -> Value {
    serde_json::json!({
        "name": m.name,
        "short_name": m.short_name,
        "base": hex(m.base_address.0),
        "end": hex(m.end_address().0),
        "size": m.size,
    })
}

fn exception_json(exc: &DmpException) -> Value {
    serde_json::json!({
        "code": exc.code,
        "code_hex": hex(exc.code as u64),
        "code_name": exception_code_name(exc.code),
        "flags": exc.flags,
        "address": hex(exc.address),
        "parameters": exc.parameters.iter().map(|p| hex(*p)).collect::<Vec<_>>(),
    })
}

fn exception_code_name(code: u32) -> &'static str {
    match code {
        0xC0000005 => "STATUS_ACCESS_VIOLATION",
        0xC000001D => "STATUS_ILLEGAL_INSTRUCTION",
        0xC0000094 => "STATUS_INTEGER_DIVIDE_BY_ZERO",
        0xC0000095 => "STATUS_INTEGER_OVERFLOW",
        0xC0000096 => "STATUS_PRIVILEGED_INSTRUCTION",
        0xC00000FD => "STATUS_STACK_OVERFLOW",
        0xC0000006 => "STATUS_IN_PAGE_ERROR",
        0x80000003 => "STATUS_BREAKPOINT",
        0x80000004 => "STATUS_SINGLE_STEP",
        0xC000008E => "STATUS_FLOAT_DIVIDE_BY_ZERO",
        0xC0000090 => "STATUS_FLOAT_INVALID_OPERATION",
        0xC0000091 => "STATUS_FLOAT_OVERFLOW",
        0xC000008D => "STATUS_FLOAT_DENORMAL_OPERAND",
        0xC0000092 => "STATUS_FLOAT_STACK_CHECK",
        0xC0000093 => "STATUS_FLOAT_UNDERFLOW",
        0xC000008F => "STATUS_FLOAT_INEXACT_RESULT",
        _ => "unknown",
    }
}

fn system_info_json(info: &DmpSystemInfo) -> Value {
    let product = match info.product_type {
        1 => "Workstation",
        2 => "DomainController",
        3 => "Server",
        _ => "Unknown",
    };
    let system_time = if info.system_time != 0 {
        filetime_to_iso(info.system_time as u64)
    } else {
        None
    };
    let machine = match info.machine_image_type {
        0x014c => "I386",
        0x8664 => "AMD64",
        0xAA64 => "ARM64",
        _ => "Unknown",
    };
    serde_json::json!({
        "major_version": info.major_version,
        "build": info.minor_version,
        "service_pack_build": info.service_pack_build,
        "machine_image_type": hex(info.machine_image_type as u64),
        "machine": machine,
        "system_time": system_time,
        "system_up_time_secs": if info.system_up_time != 0 {
            Some(info.system_up_time / 10_000_000)
        } else {
            None
        },
        "product_type": product,
        "suite_mask": hex(info.suite_mask as u64),
    })
}

/// Crash context extracted from the triage EPROCESS/ETHREAD snapshots,
/// shared by `open_dump` and `triage`.
fn crash_context_json(ci: &TriageCrashInfo) -> Value {
    let mut v = serde_json::json!({
        "process_name": ci.process_name,
        "process_id": ci.process_id,
        "thread_id": ci.thread_id,
    });
    let obj = v.as_object_mut().unwrap();
    if let Some(ppid) = ci.parent_process_id {
        obj.insert("parent_process_id".into(), serde_json::json!(ppid));
    }
    if let Some(es) = ci.exit_status {
        obj.insert("exit_status".into(), hex(es as u64).into());
    }
    if let Some(ct) = ci.create_time {
        obj.insert("create_time".into(), filetime_to_iso(ct).into());
    }
    if let Some(es) = ci.thread_exit_status {
        obj.insert("thread_exit_status".into(), hex(es as u64).into());
    }
    v
}

/// Crashing processor's PRCB summary, shared by `open_dump` and `triage`.
fn prcb_json(p: &TriagePrcbInfo) -> Value {
    serde_json::json!({
        "current_thread": hex(p.current_thread),
        "processor_number": p.processor_number,
        "mhz": p.mhz,
        "cpu_type": p.cpu_type,
        "vendor_string": p.vendor_string,
    })
}

fn filetime_to_iso(ft: u64) -> Option<String> {
    // Windows FILETIME: 100-ns intervals since 1601-01-01 UTC.
    // Unix epoch is 11644473600 seconds after that.
    const EPOCH_DIFF: i64 = 11_644_473_600;
    let secs = (ft / 10_000_000) as i64 - EPOCH_DIFF;
    if secs < 0 || secs > 253_402_300_799 {
        return None;
    }
    let s = secs % 60;
    let total_m = secs / 60;
    let m = total_m % 60;
    let total_h = total_m / 60;
    let h = total_h % 24;
    let days = total_h / 24;
    // Approximate date from days since epoch (good enough for display)
    let (y, mo, d) = days_to_ymd(days);
    Some(format!("{y:04}-{mo:02}-{d:02}T{h:02}:{m:02}:{s:02}Z"))
}

fn days_to_ymd(mut days: i64) -> (i64, i64, i64) {
    // Civil days since 1970-01-01 to (year, month, day)
    days += 719_468;
    let era = if days >= 0 { days } else { days - 146_096 } / 146_097;
    let doe = days - era * 146_097;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146_096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = if m <= 2 { y + 1 } else { y };
    (y, m, d)
}

fn unloaded_driver_json(d: &UnloadedDriver) -> Value {
    serde_json::json!({
        "name": d.name,
        "start_address": hex(d.start_address),
        "end_address": hex(d.end_address),
    })
}

fn read_virt_bytes(ctx: &mut Session, addr: u64, buf: &mut [u8]) -> Result<(), ToolError> {
    let process = ctx.target.current_process()?;
    process
        .memory()
        .read_bytes(VirtAddr(addr), buf)
        .map_err(ToolError::from)?;
    ctx.breakpoints
        .mask_breakpoint_bytes(VirtAddr(addr), buf, process.dtb());
    Ok(())
}

#[tool_router]
impl NtoseyeMcp {
    fn new(session: SharedSession, interrupt: Arc<AtomicBool>) -> Self {
        let tool_router = Self::tool_router();
        Self {
            session,
            tool_router,
            interrupt,
        }
    }

    /// Ship a job to the session actor and await its JSON reply.
    async fn run<F>(&self, job: F) -> Result<Value, McpError>
    where
        F: FnOnce(&mut Session) -> Result<Value, ToolError> + Send + 'static,
    {
        let tx = {
            let guard = self.session.lock().unwrap();
            match &*guard {
                SessionSlot::Active(tx) => tx.clone(),
                SessionSlot::Opening(_) => {
                    return Err(McpError::invalid_request(
                        "a debugger session is still opening; retry shortly",
                        None,
                    ));
                }
                SessionSlot::Vacant => {
                    return Err(McpError::invalid_request(
                        "no debugger session is active; call open_dump or open to attach",
                        None,
                    ));
                }
            }
        };
        let (reply_tx, reply_rx) = oneshot::channel();
        tx.send(Command::Run {
            job: Box::new(job),
            reply: reply_tx,
        })
        .map_err(|_| McpError::internal_error("debugger session is gone", None))?;
        reply_rx
            .await
            .map_err(|_| McpError::internal_error("debugger session dropped the request", None))?
            .map_err(ToolError::into_mcp)
    }

    #[tool(
        description = "List running processes (optional name filter; paged via offset/limit) as {total, offset, returned, has_more, next_offset?, processes:[{pid, name, eprocess, dtb}]}"
    )]
    async fn processes(
        &self,
        Parameters(ListArgs {
            filter,
            offset,
            limit,
        }): Parameters<ListArgs>,
    ) -> Result<CallToolResult, McpError> {
        let offset = offset.unwrap_or(0);
        let limit = optional_limit("limit", limit, LIST_DEFAULT_LIMIT, LIST_MAX_LIMIT)?;
        let v = self
            .run(move |ctx| {
                let matched = ctx
                    .target
                    .matching_processes(filter.as_deref())
                    .map_err(|e| enumeration_error(ctx, e))?;
                let total = matched.len();
                let items: Vec<Value> = matched
                    .iter()
                    .skip(offset)
                    .take(limit)
                    .map(|p| {
                        serde_json::json!({
                            "pid": p.pid,
                            "name": p.name,
                            "eprocess": hex(p.eprocess_va.0),
                            "dtb": hex(p.dtb),
                        })
                    })
                    .collect();
                Ok(paged(total, offset, "processes", items))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "List loaded kernel modules (optional name filter; paged via offset/limit) as {total, offset, returned, has_more, next_offset?, modules:[{name, short_name, base, end, size}]}"
    )]
    async fn kernel_modules(
        &self,
        Parameters(ListArgs {
            filter,
            offset,
            limit,
        }): Parameters<ListArgs>,
    ) -> Result<CallToolResult, McpError> {
        let offset = offset.unwrap_or(0);
        let limit = optional_limit("limit", limit, LIST_DEFAULT_LIMIT, LIST_MAX_LIMIT)?;
        let v = self
            .run(move |ctx| {
                let mods = ctx
                    .target
                    .kernel_modules()
                    .map_err(|e| enumeration_error(ctx, e))?;
                let f = filter.as_deref().map(str::to_ascii_lowercase);
                let matched: Vec<_> = mods
                    .iter()
                    .filter(|m| {
                        f.as_deref().is_none_or(|f| {
                            m.name.to_ascii_lowercase().contains(f)
                                || m.short_name.to_ascii_lowercase().contains(f)
                        })
                    })
                    .collect();
                let total = matched.len();
                let items: Vec<Value> = matched
                    .iter()
                    .skip(offset)
                    .take(limit)
                    .map(|m| module_json(m))
                    .collect();
                Ok(paged(total, offset, "modules", items))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "List driver objects (optional name filter; paged via offset/limit) as {total, offset, returned, has_more, next_offset?, drivers:[{name, object, driver_start, driver_size, device_object, driver_unload}]}"
    )]
    async fn driver_objects(
        &self,
        Parameters(ListArgs {
            filter,
            offset,
            limit,
        }): Parameters<ListArgs>,
    ) -> Result<CallToolResult, McpError> {
        let offset = offset.unwrap_or(0);
        let limit = optional_limit("limit", limit, LIST_DEFAULT_LIMIT, LIST_MAX_LIMIT)?;
        let v = self
            .run(move |ctx| {
                let drivers = ctx
                    .target
                    .enumerate_driver_objects()
                    .map_err(|e| enumeration_error(ctx, e))?;
                let f = filter.as_deref().map(str::to_ascii_lowercase);
                let matched: Vec<_> = drivers
                    .iter()
                    .filter(|d| {
                        f.as_deref()
                            .is_none_or(|f| d.name.to_ascii_lowercase().contains(f))
                    })
                    .collect();
                let total = matched.len();
                let items: Vec<Value> = matched
                    .iter()
                    .skip(offset)
                    .take(limit)
                    .map(|d| {
                        serde_json::json!({
                            "name": d.name,
                            "object": hex(d.object.0),
                            "driver_start": hex(d.driver_start.0),
                            "driver_size": d.driver_size,
                            "device_object": hex(d.device_object.0),
                            "driver_unload": hex(d.driver_unload.0),
                        })
                    })
                    .collect();
                Ok(paged(total, offset, "drivers", items))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "List Windows threads (filter by pid; paged via offset/limit) as {total, offset, returned, has_more, next_offset?, threads:[{tid, pid, process_name, ethread, kthread, eprocess, state, state_name, wait_reason, wait_reason_name, active}]}. `active` is the vCPU id currently running the thread (only resolved when the VM is halted), else null. A live system has 1000+ threads; pass pid to scope to one process."
    )]
    async fn threads(
        &self,
        Parameters(ThreadsArgs { pid, offset, limit }): Parameters<ThreadsArgs>,
    ) -> Result<CallToolResult, McpError> {
        let offset = offset.unwrap_or(0);
        let limit = optional_limit("limit", limit, THREADS_DEFAULT_LIMIT, THREADS_MAX_LIMIT)?;
        let v = self
            .run(move |ctx| {
                let (threads, active) = ctx
                    .windows_threads()
                    .map_err(|e| enumeration_error(ctx, e))?;
                let matched: Vec<_> = threads
                    .iter()
                    .filter(|t| pid.is_none_or(|p| t.pid == Some(p)))
                    .collect();
                let total = matched.len();
                let items: Vec<Value> = matched
                    .iter()
                    .skip(offset)
                    .take(limit)
                    .map(|t| {
                        serde_json::json!({
                            "tid": t.tid,
                            "pid": t.pid,
                            "process_name": t.process_name,
                            "ethread": hex(t.ethread.0),
                            "kthread": hex(t.kthread.0),
                            "eprocess": t.eprocess.map(|a| hex(a.0)),
                            "state": t.state,
                            "state_name": t.state.map(kthread_state_name),
                            "wait_reason": t.wait_reason,
                            "wait_reason_name": t.wait_reason.map(wait_reason_name),
                            "active": active.get(&t.ethread.0),
                        })
                    })
                    .collect();
                Ok(paged(total, offset, "threads", items))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Inspect every vCPU as {vcpus:[{id, rip, context, symbol, error}]}: the address space each is executing in (\"kernel\", a process name, or \"unknown\") and the nearest symbol. Requires the VM halted (call interrupt first, or be stopped at a breakpoint)."
    )]
    async fn vcpus(&self) -> Result<CallToolResult, McpError> {
        let v = self
            .run(|ctx| {
                require_halted(ctx, "vcpus")?;
                let vcpus = ctx.vcpus().map_err(ToolError::from)?;
                let items: Vec<Value> = vcpus
                    .into_iter()
                    .map(|v| {
                        serde_json::json!({
                            "id": v.id,
                            "rip": v.rip.map(hex),
                            "context": v.context,
                            "symbol": v.symbol,
                            "error": v.error,
                        })
                    })
                    .collect();
                Ok(serde_json::json!({ "vcpus": items }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Read all registers of the current thread as a {name: value} object. Requires the VM halted (call interrupt first, or be stopped at a breakpoint)."
    )]
    async fn registers(&self) -> Result<CallToolResult, McpError> {
        let v = self
            .run(|ctx| {
                require_halted(ctx, "registers")?;
                let regs = ctx.backend.read_registers().map_err(ToolError::from)?;
                let map: serde_json::Map<String, Value> = ctx
                    .register_map
                    .to_hashmap(&regs)
                    .into_iter()
                    .map(|(name, value)| (name, Value::from(hex(value))))
                    .collect();
                Ok(Value::Object(map))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Read guest virtual memory in the current address space (attached process or kernel); length must be 1-4096. Returns {address, length, hex} (hex is contiguous bytes)"
    )]
    async fn read_memory(
        &self,
        Parameters(ReadMemoryArgs { address, length }): Parameters<ReadMemoryArgs>,
    ) -> Result<CallToolResult, McpError> {
        let length = required_range("length", length, READ_MEMORY_MAX_LENGTH)?;
        let v = self
            .run(move |ctx| {
                let addr = eval_addr(ctx, &address)?;
                let len = length;
                let mut buf = vec![0u8; len];
                read_virt_bytes(ctx, addr, &mut buf)?;
                Ok(serde_json::json!({
                    "address": hex(addr),
                    "length": len,
                    "hex": hex_of(&buf),
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Disassemble 1-128 instructions at an address in the current address space; returns {instructions:[{ip, hex, asm, comment}]}"
    )]
    async fn disassemble(
        &self,
        Parameters(DisassembleArgs { address, count }): Parameters<DisassembleArgs>,
    ) -> Result<CallToolResult, McpError> {
        let count = required_range("count", count, DISASSEMBLE_MAX_COUNT)?;
        let v = self
            .run(move |ctx| {
                let addr = eval_addr(ctx, &address)?;
                let rows = ctx
                    .disassemble(VirtAddr(addr), count)
                    .map_err(ToolError::from)?;
                let arr: Vec<Value> = rows
                    .iter()
                    .map(|r| {
                        serde_json::json!({
                            "ip": hex(r.ip),
                            "hex": r.hex,
                            "asm": r.asm(),
                            "comment": r.comment,
                        })
                    })
                    .collect();
                Ok(serde_json::json!({ "instructions": arr }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Walk the current thread's call stack; limit default 64, range 1-256. Returns {frames:[{ip, sp, symbol, source}]} (source: current/unwind/scan). Requires the VM halted (call interrupt first, or be stopped at a breakpoint)."
    )]
    async fn backtrace(
        &self,
        Parameters(BacktraceArgs { limit }): Parameters<BacktraceArgs>,
    ) -> Result<CallToolResult, McpError> {
        let limit = optional_limit("limit", limit, BACKTRACE_DEFAULT_LIMIT, BACKTRACE_MAX_LIMIT)?;
        let v = self
            .run(move |ctx| {
                require_halted(ctx, "backtrace")?;
                let trace = ctx.backtrace(limit).map_err(ToolError::from)?;
                let arr: Vec<Value> = trace.frames.iter().map(frame_json).collect();
                Ok(serde_json::json!({ "frames": arr }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Walk the page tables for an address; returns {address, levels:[{level, address, value, pfn, present, large_page, writable, user, nx, flags}]}"
    )]
    async fn pte_walk(
        &self,
        Parameters(AddressArgs { address }): Parameters<AddressArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let addr = eval_addr(ctx, &address)?;
                let t = ctx
                    .target
                    .pte_traverse(VirtAddr(addr))
                    .map_err(ToolError::from)?;
                let mut levels = vec![
                    view::to_json(&view::pte_level(&t.pxe)),
                    view::to_json(&view::pte_level(&t.ppe)),
                ];
                if let Some(pde) = &t.pde {
                    levels.push(view::to_json(&view::pte_level(pde)));
                }
                if let Some(pte) = &t.pte {
                    levels.push(view::to_json(&view::pte_level(pte)));
                }
                Ok(serde_json::json!({
                    "address": hex(t.address.0),
                    "dtb": hex(t.dtb),
                    "levels": levels,
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Inspect an _IRP at an address (expression) in the current address space. Returns {address, type, size, stack_count, current_location, pending_returned, requestor_mode, io_status, user_event, user_buffer, mdl_address, thread, current_stack:{address, major_function, major_function_name, minor_function, device_object, file_object, completion_routine, context}|null}. current_stack is null when CurrentLocation is out of range or the slot is unreadable."
    )]
    async fn inspect_irp(
        &self,
        Parameters(AddressArgs { address }): Parameters<AddressArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let addr = eval_addr(ctx, &address)?;
                let irp = ctx
                    .target
                    .inspect_irp(VirtAddr(addr))
                    .map_err(ToolError::from)?;
                Ok(view::to_json(&view::irp(&irp)))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Inspect a _DRIVER_OBJECT at an address (expression). Accepts a pointer to one. Returns {object, via_pointer, name, driver_start, driver_size, driver_section, driver_unload, devices:[{device, device_type, flags, characteristics, attached, next}], dispatch:[{index, name, routine, symbol}]} where dispatch is the 28-entry IRP_MJ_* table."
    )]
    async fn inspect_driver_object(
        &self,
        Parameters(AddressArgs { address }): Parameters<AddressArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let addr = eval_addr(ctx, &address)?;
                let d = ctx
                    .target
                    .inspect_driver_object(VirtAddr(addr))
                    .map_err(ToolError::from)?;
                Ok(view::to_json(&view::driver_object(&ctx.target, &d)))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Inspect a _DEVICE_OBJECT at an address (expression). Accepts a pointer to one. Returns {object, via_pointer, device_type, flags, characteristics, driver_object, attached_device, next_device, current_irp, device_extension, attached_stack:[{device, driver_object, device_type, flags}]}."
    )]
    async fn inspect_device_object(
        &self,
        Parameters(AddressArgs { address }): Parameters<AddressArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let addr = eval_addr(ctx, &address)?;
                let d = ctx
                    .target
                    .inspect_device_object(VirtAddr(addr))
                    .map_err(ToolError::from)?;
                Ok(view::to_json(&view::device_object(&d)))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Inspect an executive _OBJECT_HEADER for an address (expression), accepting either the object body or the header. Returns {input, mode, header, body, pointer_count, handle_count, type_index, type_object, type_name, info_mask, name_info, name}. mode is 'body' or 'header'; type_name/name are null when unresolved."
    )]
    async fn inspect_object_header(
        &self,
        Parameters(AddressArgs { address }): Parameters<AddressArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let addr = eval_addr(ctx, &address)?;
                let o = ctx
                    .target
                    .inspect_object_header(VirtAddr(addr))
                    .map_err(ToolError::from)?;
                Ok(view::to_json(&view::object_header(&o)))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Enumerate process/thread/image notification callbacks (Psp*NotifyRoutine arrays). `filter` is a case-insensitive substring matched against the resolved routine symbol. Paged via offset/limit. Returns {total, offset, returned, has_more, next_offset?, callbacks:[{kind, index, function, symbol, block, raw, context}]}."
    )]
    async fn notify_callbacks(
        &self,
        Parameters(ListArgs {
            filter,
            offset,
            limit,
        }): Parameters<ListArgs>,
    ) -> Result<CallToolResult, McpError> {
        let offset = offset.unwrap_or(0);
        let limit = optional_limit("limit", limit, LIST_DEFAULT_LIMIT, LIST_MAX_LIMIT)?;
        let v = self
            .run(move |ctx| {
                let dtb = ctx.target.kernel_dtb();
                let cbs = ctx
                    .target
                    .enumerate_notify_callbacks()
                    .map_err(ToolError::from)?;
                let f = filter.as_deref().map(str::to_ascii_lowercase);
                let rows: Vec<Value> = cbs
                    .iter()
                    .map(|c| {
                        let symbol = ctx
                            .target
                            .symbols
                            .format_closest_symbol_for_address(dtb, c.function);
                        (c, symbol)
                    })
                    .filter(|(_, symbol)| {
                        f.as_deref().is_none_or(|f| {
                            symbol
                                .as_deref()
                                .is_some_and(|s| s.to_ascii_lowercase().contains(f))
                        })
                    })
                    .map(|(c, symbol)| view::to_json(&view::notify_callback(c, symbol)))
                    .collect();
                let total = rows.len();
                let items: Vec<Value> = rows.into_iter().skip(offset).take(limit).collect();
                Ok(paged(total, offset, "callbacks", items))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Dump the kernel SSDT (KiServiceTable) and, when initialized, the win32k shadow table. Returns {tables:[{label, base, limit, entries:[{index, target, symbol, module}]}]}. An entry whose module differs from the expected owner (ntoskrnl/win32k) is a candidate hook."
    )]
    async fn ssdt(&self) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let tables = ctx.target.dump_ssdt().map_err(ToolError::from)?;
                let tables: Vec<Value> = tables
                    .iter()
                    .map(|t| view::to_json(&view::ssdt_table(t)))
                    .collect();
                Ok(serde_json::json!({ "tables": tables }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Discover in-flight IRPs from each thread's _ETHREAD.IrpList and each device's _DEVICE_OBJECT.CurrentIrp. `filter` scopes processes (pid or name substring) and driver names; a numeric/pid filter skips the device sweep. Paged via offset/limit. Returns {total, offset, returned, has_more, next_offset?, irps:[{irp, source, stack_count, current_location, pid, tid, ethread, state, wait_reason, driver, device}]}."
    )]
    async fn discover_irps(
        &self,
        Parameters(ListArgs {
            filter,
            offset,
            limit,
        }): Parameters<ListArgs>,
    ) -> Result<CallToolResult, McpError> {
        let offset = offset.unwrap_or(0);
        let limit = optional_limit("limit", limit, LIST_DEFAULT_LIMIT, LIST_MAX_LIMIT)?;
        let v = self
            .run(move |ctx| {
                let hits = ctx
                    .target
                    .discover_irps(filter.as_deref())
                    .map_err(ToolError::from)?;
                let total = hits.len();
                let items: Vec<Value> = hits
                    .iter()
                    .skip(offset)
                    .take(limit)
                    .map(|h| view::to_json(&view::irp_hit(h)))
                    .collect();
                Ok(paged(total, offset, "irps", items))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Describe what an address belongs to (complements pte_walk's how-it's-mapped with where-it-lives). Returns {address, dtb, kind, module:{name, base, size, offset}|null, section|null, va_type|null, region:{start, end, protection, vad_type, private_memory, commit_charge, details}|null}. kind is kernel-module/user-image/kernel-region/private/mapped/unknown; section is the PE section for a module hit; va_type is the MM region name (e.g. KernelStacks, PagedPool, SystemPtes) for a kernel address; region is the VAD entry for a process address."
    )]
    async fn describe_address(
        &self,
        Parameters(AddressArgs { address }): Parameters<AddressArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let addr = eval_addr(ctx, &address)?;
                let d = ctx
                    .target
                    .describe_address(VirtAddr(addr))
                    .map_err(ToolError::from)?;
                Ok(view::to_json(&view::address_description(&d)))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Resolve the nearest symbol to an address as module!name+0x..; returns {address, symbol}"
    )]
    async fn closest_symbol(
        &self,
        Parameters(AddressArgs { address }): Parameters<AddressArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let addr = eval_addr(ctx, &address)?;
                let symbol = ctx.target.closest_symbol_current_context(VirtAddr(addr));
                Ok(serde_json::json!({ "address": hex(addr), "symbol": symbol }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Get a struct/class type's layout; returns {name, size, fields:[{name, offset, size, type}]} sorted by offset. Use enum_values for PDB enums."
    )]
    async fn type_layout(
        &self,
        Parameters(TypeArgs { type_name }): Parameters<TypeArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let dtb = ctx.target.current_dtb();
                let info = ctx
                    .target
                    .symbols
                    .find_type_across_modules(dtb, &type_name)
                    .ok_or_else(|| {
                        ToolError::Request(
                            ctx.target.symbols.unresolved_type_message(dtb, &type_name),
                        )
                    })?;
                let fields: Vec<Value> = {
                    let mut fields: Vec<_> = info.fields.iter().collect();
                    fields.sort_by_key(|(_, f)| f.offset);
                    fields
                }
                .into_iter()
                .map(|(n, f)| {
                    serde_json::json!({
                        "name": n,
                        "offset": hex(f.offset as u64),
                        "size": f.size,
                        "type": format!("{}", f.type_data),
                    })
                })
                .collect();
                Ok(serde_json::json!({
                    "name": type_name,
                    "size": info.size,
                    "fields": fields,
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Fuzzy-search symbols by name (use `module!query` to scope to one module). Returns {query, total, symbols:[{name, address, module}]}; address/module are null when a name doesn't resolve in the current context."
    )]
    async fn search_symbols(
        &self,
        Parameters(SymbolSearchArgs { query, limit }): Parameters<SymbolSearchArgs>,
    ) -> Result<CallToolResult, McpError> {
        let limit = optional_limit("limit", limit, 50, 500)?;
        let v = self
            .run(move |ctx| {
                let dtb = ctx.target.current_dtb();
                let (module, names) = match query.split_once('!') {
                    Some((m, q)) => (
                        Some(m.to_string()),
                        ctx.target
                            .symbols
                            .search_symbols_in_module(dtb, m, q, limit),
                    ),
                    None => (
                        None,
                        ctx.target.current_symbol_index().search(&query, limit),
                    ),
                };
                let symbols: Vec<Value> = names
                    .iter()
                    .map(|name| {
                        let lookup = match &module {
                            Some(m) => format!("{m}!{name}"),
                            None => name.clone(),
                        };
                        let resolved = ctx.target.symbols.find_symbol_with_module(dtb, &lookup);
                        serde_json::json!({
                            "name": name,
                            "address": resolved.as_ref().map(|(a, _)| hex(a.0)),
                            "module": resolved.map(|(_, m)| m),
                        })
                    })
                    .collect();
                Ok(serde_json::json!({
                    "query": query,
                    "total": symbols.len(),
                    "symbols": symbols,
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Fuzzy-search struct/class type names across loaded modules. Returns {query, total, types:[name]}; feed a name to type_layout for its fields. Use search_enums for PDB enums."
    )]
    async fn search_types(
        &self,
        Parameters(TypeSearchArgs { query, limit }): Parameters<TypeSearchArgs>,
    ) -> Result<CallToolResult, McpError> {
        let limit = optional_limit("limit", limit, 50, 500)?;
        let v = self
            .run(move |ctx| {
                let names = ctx.target.current_types_index().search(&query, limit);
                Ok(serde_json::json!({
                    "query": query,
                    "total": names.len(),
                    "types": names,
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Fuzzy-search PDB enum names across loaded modules. Returns {query, total, enums:[name]}; feed a name to enum_values."
    )]
    async fn search_enums(
        &self,
        Parameters(EnumSearchArgs { query, limit }): Parameters<EnumSearchArgs>,
    ) -> Result<CallToolResult, McpError> {
        let limit = optional_limit("limit", limit, 50, 500)?;
        let v = self
            .run(move |ctx| {
                let names = ctx.target.current_enums_index().search(&query, limit);
                Ok(serde_json::json!({
                    "query": query,
                    "total": names.len(),
                    "enums": names,
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "List a PDB enum's variants (in declaration order) as {name, values:[{name, value}]}. Enums aren't structs, so use this rather than type_layout (e.g. _MI_SYSTEM_VA_TYPE, _KWAIT_REASON, _POOL_TYPE)."
    )]
    async fn enum_values(
        &self,
        Parameters(TypeArgs { type_name }): Parameters<TypeArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let variants = ctx
                    .target
                    .symbols
                    .find_enum_across_modules(ctx.target.current_dtb(), &type_name)
                    .ok_or_else(|| ToolError::Request(format!("unknown enum: {type_name}")))?;
                let values: Vec<Value> = variants
                    .into_iter()
                    .map(|(name, value)| serde_json::json!({ "name": name, "value": value }))
                    .collect();
                Ok(serde_json::json!({ "name": type_name, "values": values }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Read a typed struct at an address in the current address space; returns {type, address, fields:{name: value}}. Nested structs are omitted; read them separately at their own address."
    )]
    async fn read_struct(
        &self,
        Parameters(ReadStructArgs { type_name, address }): Parameters<ReadStructArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let addr = eval_addr(ctx, &address)?;
                let dtb = ctx.target.current_dtb();
                let info = ctx
                    .target
                    .symbols
                    .find_type_across_modules(dtb, &type_name)
                    .ok_or_else(|| {
                        ToolError::Request(
                            ctx.target.symbols.unresolved_type_message(dtb, &type_name),
                        )
                    })?;
                let mut buf = vec![0u8; info.size];
                read_virt_bytes(ctx, addr, &mut buf)?;
                Ok(serde_json::json!({
                    "type": type_name,
                    "address": hex(addr),
                    "fields": decode_struct(&info, &buf),
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Enumerate a process's virtual address map (VAD tree) by pid, paged via offset/limit; returns {total, offset, returned, has_more, next_offset?, regions:[{start, end, size, protection, vad_type, private, commit, details}]}"
    )]
    async fn memory_map(
        &self,
        Parameters(MemoryMapArgs { pid, offset, limit }): Parameters<MemoryMapArgs>,
    ) -> Result<CallToolResult, McpError> {
        let offset = offset.unwrap_or(0);
        let limit = optional_limit(
            "limit",
            limit,
            MEMORY_MAP_DEFAULT_LIMIT,
            MEMORY_MAP_MAX_LIMIT,
        )?;
        let v = self
            .run(move |ctx| {
                let process = ctx
                    .target
                    .guest()?
                    .enumerate_processes()
                    .map_err(|e| enumeration_error(ctx, e))?
                    .into_iter()
                    .find(|p| p.pid == pid)
                    .ok_or_else(|| ToolError::Request(format!("no process with pid {pid}")))?;
                let all = ctx
                    .target
                    .enumerate_vad_regions_for_process_info(&process)
                    .map_err(ToolError::from)?;
                let total = all.len();
                let items: Vec<Value> = all
                    .iter()
                    .skip(offset)
                    .take(limit)
                    .map(|r| {
                        serde_json::json!({
                            "start": hex(r.start.0),
                            "end": hex(r.end.0),
                            "size": r.size(),
                            "protection": r.protection,
                            "vad_type": r.vad_type,
                            "private": r.private_memory,
                            "commit": r.commit_charge,
                            "details": r.details,
                        })
                    })
                    .collect();
                Ok(paged(total, offset, "regions", items))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Current inspection context: {dtb, current_thread, process:{pid, name, eprocess}|null}"
    )]
    async fn context(&self) -> Result<CallToolResult, McpError> {
        let v = self
            .run(|ctx| {
                let process = ctx.target.current_process_info.as_ref().map(|p| {
                    serde_json::json!({
                        "pid": p.pid,
                        "name": p.name,
                        "eprocess": hex(p.eprocess_va.0),
                    })
                });
                Ok(serde_json::json!({
                    "dtb": hex(ctx.target.current_dtb()),
                    "current_thread": ctx.current_thread,
                    "process": process,
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Analyze the current bugcheck (BSOD): {code, code_hex, name, description, driver, args:[{index, value, description}], fault:{ip, symbol, driver}, trap_frames:[{address, rip_symbol, frame:{rax..r11, rip, rsp, cs, ss, eflags, error_code, previous_mode, previous_irql}|null, error:string|null}], source}, or {} if the guest is not bugchecking"
    )]
    async fn bugcheck(&self) -> Result<CallToolResult, McpError> {
        let v = self
            .run(|ctx| {
                Ok(
                    match current_bugcheck(&ctx.target)
                        .or_else(|| bugcheck_from_dump_info(&ctx.target))
                    {
                        Some(analysis) => view::to_json(&view::bugcheck(&analysis)),
                        None => serde_json::json!({}),
                    },
                )
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Decode an x64 _KTRAP_FRAME at an address, or the current Windows thread's saved trap frame when address is omitted. Returns {address, rip_symbol, frame:{rax..r11, rip, rsp, cs, ss, eflags, error_code, previous_mode, previous_irql}}."
    )]
    async fn inspect_trap_frame(
        &self,
        Parameters(TrapFrameArgs { address }): Parameters<TrapFrameArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let address = address
                    .as_deref()
                    .map(|address| eval_addr(ctx, address).map(VirtAddr))
                    .transpose()?;
                let frame = read_ktrap_frame_at_or_current(&ctx.target, address)
                    .map_err(ToolError::from)?;
                let rip_symbol = ctx
                    .target
                    .closest_symbol_current_context(VirtAddr(frame.rip));
                Ok(view::to_json(&view::trap_frame(&frame, rip_symbol)))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Read captured guest debug output (DbgPrint, e.g. \"DriverEntry failed 0x...\"). Snapshot+cursor: pass since_seq (the prior call's next_seq) to poll only new lines; returns {lines:[{seq, timestamp_ms, text}], next_seq, dropped}. dropped=true means older lines were evicted before you read them (poll more often). Output is captured only while the target runs, so resume to let it accumulate; an empty result is not proof the guest is silent. Empty on backends without a debug stream."
    )]
    async fn debug_log(
        &self,
        Parameters(DebugLogArgs { since_seq }): Parameters<DebugLogArgs>,
    ) -> Result<CallToolResult, McpError> {
        let since = since_seq.unwrap_or(0);
        let v = self
            .run(move |ctx| {
                let page = ctx.read_debug_output(since);
                let lines: Vec<Value> = page
                    .lines
                    .iter()
                    .map(|l| {
                        serde_json::json!({
                            "seq": l.seq,
                            "timestamp_ms": l.timestamp_ms,
                            "text": l.text,
                        })
                    })
                    .collect();
                Ok(serde_json::json!({
                    "lines": lines,
                    "next_seq": page.next_seq,
                    "dropped": page.dropped,
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "List the backend's capability matrix - which debug operations the current transport (kd/gdb/memory/dump) supports - as {capabilities:[{capability, label, supported}]}. Check before a state-changing op (e.g. usermode breakpoints)."
    )]
    async fn capabilities(&self) -> Result<CallToolResult, McpError> {
        let v = self
            .run(|ctx| {
                let arr: Vec<Value> = ctx
                    .capabilities()
                    .iter()
                    .map(|c| {
                        serde_json::json!({
                            "capability": format!("{:?}", c.capability),
                            "label": c.capability.label(),
                            "supported": c.supported,
                        })
                    })
                    .collect();
                Ok(serde_json::json!({ "capabilities": arr }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "List loaded modules for the current inspection scope: the attached process's user-mode modules when attached (attach_process), otherwise the kernel module list. Optional name filter; paged via offset/limit. Returns {total, offset, returned, has_more, next_offset?, modules:[{name, short_name, base, end, size}]}. Use kernel_modules to list kernel modules regardless of attach state."
    )]
    async fn modules(
        &self,
        Parameters(ListArgs {
            filter,
            offset,
            limit,
        }): Parameters<ListArgs>,
    ) -> Result<CallToolResult, McpError> {
        let offset = offset.unwrap_or(0);
        let limit = optional_limit("limit", limit, LIST_DEFAULT_LIMIT, LIST_MAX_LIMIT)?;
        let v = self
            .run(move |ctx| {
                let mods = ctx
                    .target
                    .modules()
                    .map_err(|e| enumeration_error(ctx, e))?;
                let f = filter.as_deref().map(str::to_ascii_lowercase);
                let matched: Vec<_> = mods
                    .iter()
                    .filter(|m| {
                        f.as_deref().is_none_or(|f| {
                            m.name.to_ascii_lowercase().contains(f)
                                || m.short_name.to_ascii_lowercase().contains(f)
                        })
                    })
                    .collect();
                let total = matched.len();
                let items: Vec<Value> = matched
                    .iter()
                    .skip(offset)
                    .take(limit)
                    .map(|m| module_json(m))
                    .collect();
                Ok(paged(total, offset, "modules", items))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Search guest memory in the current address space: scan `length` bytes from `start` for a byte `pattern` (contiguous lowercase hex). Returns {start, length, total, offset, returned, has_more, next_offset?, matches:[{address, offset, symbol, kind, module, section, va_type, region}]}; `total` is the full match count and `matches` is the offset/limit window."
    )]
    async fn search(
        &self,
        Parameters(SearchArgs {
            start,
            pattern,
            length,
            offset,
            limit,
        }): Parameters<SearchArgs>,
    ) -> Result<CallToolResult, McpError> {
        let length = required_range("length", length, SEARCH_MAX_LENGTH)?;
        let offset = offset.unwrap_or(0);
        let limit = optional_limit("limit", limit, SEARCH_DEFAULT_LIMIT, SEARCH_MAX_LIMIT)?;
        let v = self
            .run(move |ctx| {
                let start_addr = eval_addr(ctx, &start)?;
                let needle = bytes_of_hex(&pattern)?;
                if needle.is_empty() {
                    return Err(invalid_request("pattern must be at least one byte"));
                }
                let matches = ctx
                    .target
                    .search(VirtAddr(start_addr), &needle, length)
                    .map_err(ToolError::from)?;
                let total = matches.len();
                let page: Vec<u64> = matches.into_iter().skip(offset).take(limit).collect();
                let items: Vec<Value> = ctx
                    .target
                    .describe_search_matches(VirtAddr(start_addr), &page)
                    .map_err(ToolError::from)?
                    .iter()
                    .map(|m| view::to_json(&view::memory_search_match(m)))
                    .collect();
                let mut out = paged(total, offset, "matches", items);
                out["start"] = Value::from(hex(start_addr));
                out["length"] = Value::from(length);
                Ok(out)
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Walk an intrusive _LIST_ENTRY list in the current address space. Given the list-head address, the record struct type, and the name of the _LIST_ENTRY field embedded in the record, returns each record's base address: {head, record_type, total, records:[addr...]}. Bounded to 1000 records. E.g. head=PsLoadedModuleList, record_type=_LDR_DATA_TABLE_ENTRY, link_field=InLoadOrderLinks."
    )]
    async fn walk_list(
        &self,
        Parameters(WalkListArgs {
            head,
            record_type,
            link_field,
        }): Parameters<WalkListArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let head_addr = eval_addr(ctx, &head)?;
                let dtb = ctx.target.current_dtb();
                let info = ctx
                    .target
                    .symbols
                    .find_type_across_modules(dtb, &record_type)
                    .ok_or_else(|| invalid_request(format!("unknown type: {record_type}")))?;
                let link_offset = info.field_offset(&link_field).map_err(ToolError::from)?;
                let records = ctx
                    .target
                    .walk_list(VirtAddr(head_addr), link_offset)
                    .map_err(ToolError::from)?;
                Ok(serde_json::json!({
                    "head": hex(head_addr),
                    "record_type": record_type,
                    "total": records.len(),
                    "records": records.into_iter().map(hex).collect::<Vec<_>>(),
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Decode the _UNICODE_STRING at an address in the current address space to a string. Returns {address, value}."
    )]
    async fn read_unicode_string(
        &self,
        Parameters(AddressArgs { address }): Parameters<AddressArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let addr = eval_addr(ctx, &address)?;
                let value = ctx
                    .target
                    .read_unicode_string(VirtAddr(addr))
                    .map_err(ToolError::from)?;
                Ok(serde_json::json!({ "address": hex(addr), "value": value }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Read a NUL-terminated CHAR* (ASCII/UTF-8) string at an address in the current address space, up to max_len bytes (default 260). Returns {address, value}. The CHAR* counterpart to read_unicode_string; use it for ARC paths, boot LoadOptions, and other plain C strings."
    )]
    async fn read_c_string(
        &self,
        Parameters(ReadCStringArgs { address, max_len }): Parameters<ReadCStringArgs>,
    ) -> Result<CallToolResult, McpError> {
        let max_len = optional_limit(
            "max_len",
            max_len,
            READ_C_STRING_DEFAULT_LENGTH,
            READ_C_STRING_MAX_LENGTH,
        )?;
        let v = self
            .run(move |ctx| {
                let addr = eval_addr(ctx, &address)?;
                let value = ctx
                    .target
                    .read_c_string(VirtAddr(addr), max_len)
                    .map_err(ToolError::from)?;
                Ok(serde_json::json!({ "address": hex(addr), "value": value }))
            })
            .await?;
        json_result(v)
    }

    // --- context / run-control (state-changing) ---

    #[tool(
        description = "Switch the inspection context to a process by pid (scopes memory reads and breakpoints to it and loads its module symbols); returns {pid, name}"
    )]
    async fn attach_process(
        &self,
        Parameters(PidArgs { pid }): Parameters<PidArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let report = ctx.target.attach(pid).map_err(ToolError::from)?;
                Ok(serde_json::json!({ "pid": pid, "name": report.name }))
            })
            .await?;
        json_result(v)
    }

    #[tool(description = "Return the inspection context to the default kernel scope")]
    async fn detach(&self) -> Result<CallToolResult, McpError> {
        let v = self
            .run(|ctx| {
                ctx.target.detach();
                Ok(serde_json::json!({ "detached": true }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Set a code breakpoint at an address (scoped to the attached process, or kernel-wide if not attached), with an optional break condition. Requires the VM halted (call interrupt first, or be stopped at a breakpoint); resume then wait_for_stop to run to a hit. Returns {id, address, condition}"
    )]
    async fn set_breakpoint(
        &self,
        Parameters(SetBreakpointArgs { address, condition }): Parameters<SetBreakpointArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                require_halted(ctx, "set_breakpoint")?;
                let addr = eval_addr(ctx, &address)?;
                let id = ctx
                    .add_breakpoint_with_condition(VirtAddr(addr), condition.clone())
                    .map_err(ToolError::from)?;
                Ok(serde_json::json!({ "id": id, "address": hex(addr), "condition": condition }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Watch a data address globally across guest address spaces for write or read/write access. length is 1, 2, 4, or 8 bytes and the address must be naturally aligned. Requires the VM halted and a backend with data-watch support (currently KD). Returns {id, address, access, length, condition}."
    )]
    async fn set_watchpoint(
        &self,
        Parameters(SetWatchpointArgs {
            address,
            access,
            length,
            condition,
        }): Parameters<SetWatchpointArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                require_halted(ctx, "set_watchpoint")?;
                let addr = eval_addr(ctx, &address)?;
                let access = WatchpointAccess::from(access);
                let symbol = ctx.target.closest_symbol_current_context(VirtAddr(addr));
                let id = ctx
                    .add_watchpoint_with_symbol_condition(
                        VirtAddr(addr),
                        access,
                        length,
                        symbol,
                        condition.clone(),
                    )
                    .map_err(ToolError::from)?;
                Ok(serde_json::json!({
                    "id": id,
                    "address": hex(addr),
                    "access": access.name(),
                    "length": length,
                    "condition": condition,
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Remove a breakpoint or watchpoint by id. Requires the VM halted (call interrupt first, or be stopped at a breakpoint/watchpoint)."
    )]
    async fn clear_breakpoint(
        &self,
        Parameters(BreakpointIdArgs { id }): Parameters<BreakpointIdArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                require_halted(ctx, "clear_breakpoint")?;
                ctx.remove_breakpoint(id).map_err(ToolError::from)?;
                Ok(serde_json::json!({ "cleared": id }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Re-arm a disabled breakpoint or watchpoint by id. Requires the VM halted (call interrupt first, or be stopped at one)."
    )]
    async fn enable_breakpoint(
        &self,
        Parameters(BreakpointIdArgs { id }): Parameters<BreakpointIdArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                require_halted(ctx, "enable_breakpoint")?;
                ctx.enable_breakpoint(id).map_err(ToolError::from)?;
                Ok(serde_json::json!({ "id": id, "enabled": true }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Disable a breakpoint or watchpoint by id without forgetting it, so it can be re-enabled later. Requires the VM halted."
    )]
    async fn disable_breakpoint(
        &self,
        Parameters(BreakpointIdArgs { id }): Parameters<BreakpointIdArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                require_halted(ctx, "disable_breakpoint")?;
                ctx.disable_breakpoint(id).map_err(ToolError::from)?;
                Ok(serde_json::json!({ "id": id, "enabled": false }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "List code breakpoints and data watchpoints as {id, address, enabled, symbol, scope, condition, temporary, watch_access, watch_length} objects. watch_access/watch_length are null for code breakpoints."
    )]
    async fn list_breakpoints(&self) -> Result<CallToolResult, McpError> {
        let v = self
            .run(|ctx| {
                let arr: Vec<Value> = ctx
                    .list_breakpoints()
                    .iter()
                    .map(|b| view::to_json(&view::breakpoint(b)))
                    .collect();
                Ok(serde_json::json!({ "breakpoints": arr }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Resume the VM (go). Non-blocking: returns immediately as {running:true, already_running:bool}. To wait for the next stop, call wait_for_stop; to see where it is now without resuming, call status."
    )]
    async fn resume(&self) -> Result<CallToolResult, McpError> {
        let v = self
            .run(|ctx| {
                // Drain any stop the servicer caught so a real halt that already
                // surfaced is reflected as `already_running:false` and the resume
                // below actually advances past it, rather than the stale running
                // value reporting `already_running:true` and doing nothing. A
                // debugger-noise break-in is absorbed (already running, no-op).
                ctx.settle_pending_stop().map_err(ToolError::from)?;
                let already_running = ctx.backend.is_running();
                if !already_running {
                    ctx.resume().map_err(ToolError::from)?;
                }
                Ok(serde_json::json!({ "running": true, "already_running": already_running }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Wait up to timeout_ms for the next stop WITHOUT resuming (default 10000, max 20000; 0 = default). Returns {stop:\"breakpoint\"|\"watchpoint\"|\"exception\"|\"bugcheck\"|\"target_reloaded\"} with context, {stop:\"running\"} if the wait elapsed (call again to keep waiting; no stops lost between calls), or {stop:\"halted\"} immediately if the VM is already parked with nothing pending. Breakpoint/watchpoint stops include condition_error when condition evaluation failed; the stop is surfaced rather than skipped. Does not resume; call resume to advance. Use short timeouts and poll; there is no indefinite wait."
    )]
    async fn wait_for_stop(
        &self,
        Parameters(WaitArgs { timeout_ms }): Parameters<WaitArgs>,
        ct: CancellationToken,
    ) -> Result<CallToolResult, McpError> {
        let timeout_ms = optional_timeout_ms(timeout_ms)?;
        // Per-request cancel flag the actor's wait loop polls. Set when the
        // client cancels/times out this request (`ct`) or the server is shutting
        // down (`self.interrupt`), so an in-flight wait returns and frees the
        // single-threaded actor instead of pinning it for the whole timeout.
        let cancel = Arc::new(AtomicBool::new(false));
        let watcher = {
            let cancel = cancel.clone();
            let shutdown = self.interrupt.clone();
            tokio::spawn(async move {
                loop {
                    if shutdown.load(Ordering::Relaxed) {
                        cancel.store(true, Ordering::Relaxed);
                        return;
                    }
                    tokio::select! {
                        _ = ct.cancelled() => {
                            cancel.store(true, Ordering::Relaxed);
                            return;
                        }
                        _ = tokio::time::sleep(Duration::from_millis(200)) => {}
                    }
                }
            })
        };
        let result = self
            .run(move |ctx| {
                // A stop the background `service_idle` caught and parked while the
                // host was idle (e.g. a breakpoint that fired between tool calls)
                // is the proper event for this wait; return it before waiting for
                // a new one.
                if let Some(parked) = ctx.take_parked_stop() {
                    return Ok(continue_outcome_json(ctx, parked));
                }
                // Always bounded over MCP (see optional_timeout_ms): the caller
                // polls by calling again rather than waiting indefinitely. Never
                // resumes, so a held stop (e.g. the early-boot reload) surfaces in
                // place instead of being run past.
                let timeout = Some(Duration::from_millis(timeout_ms));
                let outcome = ctx
                    .wait_for_stop_bounded(timeout, &cancel)
                    .map_err(ToolError::from)?;
                Ok(continue_outcome_json(ctx, outcome))
            })
            .await;
        watcher.abort();
        let v = result?;
        json_result(v)
    }

    #[tool(
        description = "Read-only run-control state (where am I): {running, current_thread, rip, symbol, process:{pid,name,eprocess}|null, coherent}. rip/symbol are null while running. coherent=false means the guest rebooted and rediscovery is still in progress, so process/module enumeration is not yet meaningful; resume + wait_for_stop to advance rather than reading stale state."
    )]
    async fn status(&self) -> Result<CallToolResult, McpError> {
        let v = self.run(|ctx| Ok(status_json(ctx))).await?;
        json_result(v)
    }

    #[tool(
        description = "Pause a running VM (e.g. before reading registers/backtrace); returns {already_halted, rip}. If already halted, no action is taken. Resume with resume."
    )]
    async fn interrupt(&self) -> Result<CallToolResult, McpError> {
        let v = self
            .run(|ctx| {
                // A stop the servicer already caught means the VM is halted now;
                // ingest it so `already_halted` is truthful at a real stop and we
                // don't send a redundant break-in over it (a debugger-noise break-in
                // is absorbed, so already_halted is false and we interrupt for real).
                ctx.settle_pending_stop().map_err(ToolError::from)?;
                let already_halted = !ctx.backend.is_running();
                let event_rip = if already_halted {
                    None
                } else {
                    // Route through the context so the selected thread tracks
                    // the halt (rather than calling the backend raw).
                    ctx.interrupt().map_err(ToolError::from)?.program_counter
                };
                let rip = event_rip.or_else(|| {
                    ctx.backend
                        .read_registers()
                        .ok()
                        .and_then(|r| ctx.register_map.read_u64("rip", &r).ok())
                });
                Ok(serde_json::json!({
                    "already_halted": already_halted,
                    "rip": rip.map(hex),
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Single-step one instruction on the current thread. Requires the VM halted (call interrupt first, or be stopped at a breakpoint). Returns {rip, symbol}"
    )]
    async fn step(&self) -> Result<CallToolResult, McpError> {
        let v = self
            .run(|ctx| {
                require_halted(ctx, "step")?;
                ctx.step().map_err(ToolError::from)?;
                let regs = ctx.backend.read_registers().map_err(ToolError::from)?;
                let rip = ctx.register_map.read_u64("rip", &regs).unwrap_or(0);
                let symbol = ctx.target.closest_symbol_current_context(VirtAddr(rip));
                Ok(serde_json::json!({ "rip": hex(rip), "symbol": symbol }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Step over the current instruction on the current thread: if it's a call, run to its return site, otherwise single-step. Requires the VM halted (call interrupt first, or be stopped at a breakpoint). Returns the same shape as wait_for_stop ({stop:\"step\"} on completion, or breakpoint/bugcheck/exception if one is hit en route)."
    )]
    async fn step_over(&self) -> Result<CallToolResult, McpError> {
        let cancel = self.interrupt.clone();
        let v = self
            .run(move |ctx| {
                require_halted(ctx, "step_over")?;
                let outcome = ctx.step_over(&cancel).map_err(ToolError::from)?;
                Ok(continue_outcome_json(ctx, outcome))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Step out of the current function: run to the caller's return address. Requires the VM halted (call interrupt first, or be stopped at a breakpoint). Returns the same shape as wait_for_stop ({stop:\"step\"} on completion, or breakpoint/bugcheck/exception if one is hit en route)."
    )]
    async fn step_out(&self) -> Result<CallToolResult, McpError> {
        let cancel = self.interrupt.clone();
        let v = self
            .run(move |ctx| {
                require_halted(ctx, "step_out")?;
                let outcome = ctx.step_out(&cancel).map_err(ToolError::from)?;
                Ok(continue_outcome_json(ctx, outcome))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Select the current inspection thread (a vCPU id, as listed by vcpus or in threads' `active` field) so registers/backtrace/step operate on it. Returns {current_thread}."
    )]
    async fn set_current_thread(
        &self,
        Parameters(ThreadIdArgs { thread }): Parameters<ThreadIdArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                ctx.set_current_thread(&thread).map_err(ToolError::from)?;
                Ok(serde_json::json!({ "current_thread": thread }))
            })
            .await?;
        json_result(v)
    }

    // --- guest writes (state-changing) ---

    #[tool(
        description = "Write bytes to guest virtual memory (scoped to the attached process, or kernel-wide if not attached). `hex` is contiguous lowercase hex, 1-4096 bytes (the form read_memory returns). Works while the guest runs; if the guest may be concurrently touching the same bytes, interrupt first to avoid a torn write. Returns {address, length}."
    )]
    async fn write_memory(
        &self,
        Parameters(WriteMemoryArgs { address, hex: data }): Parameters<WriteMemoryArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let bytes = bytes_of_hex(&data)?;
                if bytes.is_empty() || bytes.len() > WRITE_MEMORY_MAX_LENGTH {
                    return Err(invalid_request(format!(
                        "hex must decode to 1..={WRITE_MEMORY_MAX_LENGTH} bytes"
                    )));
                }
                let addr = eval_addr(ctx, &address)?;
                ctx.target
                    .current_process()?
                    .memory()
                    .write_bytes(VirtAddr(addr), &bytes)
                    .map_err(ToolError::from)?;
                Ok(serde_json::json!({
                    "address": hex(addr),
                    "length": bytes.len(),
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Set a single register on the current thread. `value` is a debugger expression. Requires the VM halted (call interrupt first, or be stopped at a breakpoint). Returns {name, value}."
    )]
    async fn set_register(
        &self,
        Parameters(SetRegisterArgs { name, value }): Parameters<SetRegisterArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                require_halted(ctx, "set_register")?;
                let value = eval_addr(ctx, &value)?;
                ctx.write_register(&name, value).map_err(ToolError::from)?;
                Ok(serde_json::json!({ "name": name, "value": hex(value) }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Open a Windows kernel crash dump (.dmp) file for offline analysis. Must be called before any other tool when the server was started without --dump/--connect. Only one session can be active at a time. Returns {status, path, processors, bugcheck, exception:{code, code_hex, code_name, flags, address, parameters}, system_info:{major_version, build, service_pack_build, machine_image_type, machine, system_time, system_up_time_secs, product_type, suite_mask}, crash_context, prcb, broken_driver, triage_overflowed}."
    )]
    async fn open_dump(
        &self,
        Parameters(OpenDumpArgs { path }): Parameters<OpenDumpArgs>,
    ) -> Result<CallToolResult, McpError> {
        let opening = OpeningGuard::claim(&self.session)?;

        let dump_path = PathBuf::from(&path);
        let (tx, _service_pending) = tokio::task::spawn_blocking(move || {
            spawn_session(String::new(), None, Some(dump_path))
        })
        .await
        .map_err(|e| McpError::internal_error(format!("spawn_blocking failed: {e}"), None))?
        .map_err(|e| McpError::internal_error(format!("failed to open dump: {e}"), None))?;

        opening.promote(tx)?;

        let v = self
            .run(move |ctx| {
                let processors = ctx.backend.thread_list().map(|t| t.len()).unwrap_or(1);
                let bc =
                    current_bugcheck(&ctx.target).or_else(|| bugcheck_from_dump_info(&ctx.target));
                let dmp = ctx.target.phys.dmp_info();
                let crash_context = ctx.backend.triage_crash_info().map(crash_context_json);
                let prcb = dmp.and_then(|d| d.triage_prcb_info.as_ref()).map(prcb_json);
                Ok(serde_json::json!({
                    "status": "opened",
                    "path": path,
                    "processors": processors,
                    "bugcheck": bc.map(|a| view::to_json(&view::bugcheck(&a))),
                    "exception": dmp.and_then(|d| d.exception.as_ref().map(exception_json)),
                    "system_info": dmp.and_then(|d| d.system_info.as_ref().map(system_info_json)),
                    "crash_context": crash_context,
                    "prcb": prcb,
                    "broken_driver": dmp.and_then(|d| d.broken_driver.clone()),
                    "triage_overflowed": dmp.map(|d| d.triage_overflowed),
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Connect to a live Windows VM. Supported backends: \"kd\" (KD over Unix socket, default /tmp/ntoseye-kd.sock), \"gdb\" (GDB remote stub, default 127.0.0.1:1234), \"memory\" (physical memory only, no debug transport). Must be called before any other tool when the server was started without --backend/--connect/--dump. Only one session can be active at a time."
    )]
    async fn open(
        &self,
        Parameters(OpenArgs { backend, connect }): Parameters<OpenArgs>,
    ) -> Result<CallToolResult, McpError> {
        match backend.as_str() {
            "kd" | "gdb" => {}
            "memory" => {
                if connect.is_some() {
                    return Err(invalid_params(
                        "memory backend does not use 'connect'".to_string(),
                    ));
                }
            }
            other => {
                return Err(invalid_params(format!(
                    "unknown backend \"{other}\": must be \"kd\", \"gdb\", or \"memory\""
                )));
            }
        }

        let opening = OpeningGuard::claim(&self.session)?;

        let backend_name = backend.clone();
        let connect_str = connect.clone();
        let (tx, service_pending) =
            tokio::task::spawn_blocking(move || spawn_session(backend_name, connect_str, None))
                .await
                .map_err(|e| McpError::internal_error(format!("spawn_blocking failed: {e}"), None))?
                .map_err(|e| {
                    McpError::internal_error(format!("failed to connect ({backend}): {e}"), None)
                })?;

        let tx_for_ticker = tx.clone();
        opening.promote(tx)?;
        if backend != "memory" {
            spawn_service_ticker(tx_for_ticker, service_pending);
        }

        let v = self
            .run(move |ctx| {
                let processors = ctx.backend.thread_list().map(|t| t.len()).unwrap_or(1);
                Ok(serde_json::json!({
                    "status": "connected",
                    "backend": backend,
                    "connect": connect,
                    "processors": processors,
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "One-shot overview for crash/debug triage: bundles status, bugcheck analysis, exception record, system info, backtrace, loaded/unloaded kernel modules into a single response. backtrace is null when the VM is running. Returns {status, bugcheck, exception, system_info, backtrace, modules, modules_total, unloaded_drivers, crash_context, prcb, broken_driver, triage_overflowed}."
    )]
    async fn triage(&self) -> Result<CallToolResult, McpError> {
        let v = self
            .run(|ctx| {
                let s = ctx.run_status();
                let running = s.running;
                let status = run_status_json(s);

                let bc = current_bugcheck(&ctx.target)
                    .or_else(|| bugcheck_from_dump_info(&ctx.target))
                    .map(|a| view::to_json(&view::bugcheck(&a)));

                let bt = if !running {
                    ctx.backtrace(BACKTRACE_DEFAULT_LIMIT)
                        .ok()
                        .map(|trace| Value::Array(trace.frames.iter().map(frame_json).collect()))
                } else {
                    None
                };

                let all_mods = ctx.target.kernel_modules().unwrap_or_default();
                let modules_total = all_mods.len();
                let mods: Vec<Value> = all_mods.iter().take(200).map(module_json).collect();

                let dmp = ctx.target.phys.dmp_info();
                let exception = dmp.and_then(|d| d.exception.as_ref().map(exception_json));
                let system_info = dmp.and_then(|d| d.system_info.as_ref().map(system_info_json));
                let unloaded: Vec<Value> = dmp
                    .map(|d| {
                        d.unloaded_drivers
                            .iter()
                            .map(unloaded_driver_json)
                            .collect()
                    })
                    .unwrap_or_default();

                let crash_context = ctx.backend.triage_crash_info().map(crash_context_json);
                let prcb = dmp.and_then(|d| d.triage_prcb_info.as_ref()).map(prcb_json);

                Ok(serde_json::json!({
                    "status": status,
                    "bugcheck": bc,
                    "exception": exception,
                    "system_info": system_info,
                    "backtrace": bt,
                    "modules": mods,
                    "modules_total": modules_total,
                    "unloaded_drivers": unloaded,
                    "crash_context": crash_context,
                    "prcb": prcb,
                    "broken_driver": dmp.and_then(|d| d.broken_driver.clone()),
                    "triage_overflowed": dmp.map(|d| d.triage_overflowed),
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Evaluate a debugger expression (symbol, hex address, register, arithmetic, poi()) and return its numeric result. Returns {expression, value}."
    )]
    async fn eval(
        &self,
        Parameters(EvalArgs { expression }): Parameters<EvalArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let result = eval_addr(ctx, &expression)?;
                Ok(serde_json::json!({
                    "expression": expression,
                    "value": hex(result),
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Read a pointer-sized (8-byte little-endian) value at the given address. Shorthand for read_memory + byte reversal. Returns {address, value}."
    )]
    async fn read_pointer(
        &self,
        Parameters(ReadPointerArgs { address }): Parameters<ReadPointerArgs>,
    ) -> Result<CallToolResult, McpError> {
        let v = self
            .run(move |ctx| {
                let addr = eval_addr(ctx, &address)?;
                let mut buf = [0u8; 8];
                read_virt_bytes(ctx, addr, &mut buf)?;
                let val = u64::from_le_bytes(buf);
                Ok(serde_json::json!({
                    "address": hex(addr),
                    "value": hex(val),
                }))
            })
            .await?;
        json_result(v)
    }

    #[tool(
        description = "Close the active debugger session so a new one can be opened with open_dump or open. Returns {status:\"closed\"} on clean shutdown, {status:\"closed\", warning:\"...\"} if the shutdown timed out. Cancels an in-progress open if one is pending."
    )]
    async fn close(&self) -> Result<CallToolResult, McpError> {
        let tx = {
            let mut guard = self.session.lock().unwrap();
            match &*guard {
                SessionSlot::Active(tx) => tx.clone(),
                SessionSlot::Opening(_) => {
                    *guard = SessionSlot::Vacant;
                    return json_result(serde_json::json!({
                        "status": "closed",
                        "note": "cancelled a pending open; the old connection may take a moment to release — retry if the next open reports AlreadyRunning",
                    }));
                }
                SessionSlot::Vacant => {
                    return Err(McpError::invalid_request(
                        "no debugger session is active; nothing to close",
                        None,
                    ));
                }
            }
        };

        self.interrupt.store(true, Ordering::Relaxed);
        let _interrupt_guard = InterruptResetGuard(self.interrupt.clone());

        let (ack_tx, ack_rx) = oneshot::channel();
        let clean = if tx.send(Command::Shutdown { ack: ack_tx }).is_ok() {
            tokio::time::timeout(Duration::from_secs(5), ack_rx)
                .await
                .is_ok_and(|r| r.is_ok())
        } else {
            true
        };
        drop(tx);

        if clean {
            // Vacate only after the actor has acked (and released its
            // InstanceGuard), so a subsequent open() can acquire the same
            // target.
            *self.session.lock().unwrap() = SessionSlot::Vacant;
            drop(_interrupt_guard);
            json_result(serde_json::json!({ "status": "closed" }))
        } else {
            // The actor is still running (and still holds the InstanceGuard),
            // so leave the slot as Active — vacating now would let a
            // concurrent open() past OpeningGuard::claim only to fail inside
            // acquire_instance_guard with AlreadyRunning.
            drop(_interrupt_guard);
            json_result(serde_json::json!({
                "status": "pending",
                "warning": "shutdown timed out; the session is still closing — retry close shortly",
            }))
        }
    }
}

#[tool_handler]
impl rmcp::ServerHandler for NtoseyeMcp {
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
            .with_protocol_version(ProtocolVersion::LATEST)
            .with_server_info(Implementation::new(
                env!("CARGO_PKG_NAME"),
                env!("CARGO_PKG_VERSION"),
            ))
            .with_instructions(
                "ntoseye: introspect and control a live Windows kernel running under \
                 KVM/QEMU. Read-only tools for process/thread/module/driver \
                 enumeration, memory and struct reads, disassembly, backtraces, \
                 trap-frame decoding, page-table walks, and symbol/type lookup. \
                 State-changing tools: attach_process/detach (scope the context \
                 to a process), set_breakpoint/set_watchpoint and shared \
                 clear/enable/disable/list breakpoint lifecycle, run-control \
                 (resume, wait_for_stop, interrupt, step, step_over, step_out, \
                 set_current_thread), and guest writes \
                 (write_memory, set_register). Run-control is split: resume goes \
                 (non-blocking), wait_for_stop polls for the next stop without \
                 resuming, status reports where it is now, interrupt pauses. \
                 Typical flow for a user-mode breakpoint: attach_process(pid) → \
                 interrupt → set_breakpoint(expr) → resume → wait_for_stop(timeout) \
                 (poll until stop:\"breakpoint\"). \
                 The guest runs freely by default (memory/process/struct reads work \
                 live). registers/backtrace need the CPU halted: call interrupt \
                 first (or be stopped at a breakpoint/watchpoint), inspect, then \
                 resume to let the guest run again. Stop-point mutation, step, \
                 and set_register require the VM halted; write_memory works live. \
                 After a reboot, \
                 status reports coherent:false until rediscovery finishes; \
                 wait_for_stop for it rather than enumerating stale state. \
                 Tools that take an address accept a debugger expression (symbol, \
                 register, hex, arithmetic). Addresses in tool output are 0x hex \
                 strings (JSON has no hex numbers); ids/pids/sizes stay decimal.",
            )
    }
}

fn is_loopback_http_bind(addr: &str) -> bool {
    if let Ok(socket) = addr.parse::<std::net::SocketAddr>() {
        return socket.ip().is_loopback();
    }

    let Some((host, _port)) = addr.rsplit_once(':') else {
        return false;
    };
    let host = host.trim_start_matches('[').trim_end_matches(']');
    host.eq_ignore_ascii_case("localhost")
        || host
            .parse::<std::net::IpAddr>()
            .map(|ip| ip.is_loopback())
            .unwrap_or(false)
}

/// Whether a browser `Origin` header (`scheme://host[:port]`, no path) names a
/// loopback host. Used to gate cross-origin access to the loopback HTTP bind so a
/// website the user merely visits can't reach the debugger via 127.0.0.1.
fn is_loopback_origin(origin: &str) -> bool {
    let Some((_scheme, rest)) = origin.split_once("://") else {
        return false;
    };
    // host[:port]; a bracketed IPv6 literal ([::1]:port) keeps the colons inside
    // the brackets, so peel those off before splitting on the port colon.
    let host = if let Some(after_bracket) = rest.strip_prefix('[') {
        match after_bracket.split_once(']') {
            Some((host, _port)) => host,
            None => return false,
        }
    } else {
        rest.split(':').next().unwrap_or(rest)
    };
    host.eq_ignore_ascii_case("localhost")
        || host
            .parse::<std::net::IpAddr>()
            .map(|ip| ip.is_loopback())
            .unwrap_or(false)
}

fn check_http_bind_policy(addr: &str, unsafe_http: bool) -> anyhow::Result<()> {
    if is_loopback_http_bind(addr) {
        return Ok(());
    }
    if unsafe_http {
        eprintln!(
            "ntoseye-mcp: warning: HTTP bind {addr} is not loopback; debugger control tools are reachable by clients that can access this address"
        );
        Ok(())
    } else {
        Err(anyhow::anyhow!(
            "refusing non-loopback MCP HTTP bind {addr}; use 127.0.0.1:PORT for local browser clients or pass --unsafe-http to expose debugger control tools on the network"
        ))
    }
}

/// Attach (on a dedicated thread) and serve the MCP protocol until the client
/// disconnects. Synchronous entry point; it owns its own tokio runtime, so the
/// rest of the binary stays runtime-free.
///
/// `http` selects the transport: `None` serves over **stdio** (the client
/// launches this binary as a subprocess, e.g. Claude Desktop / Claude Code), while
/// `Some(addr)` serves the **Streamable HTTP** transport on `addr` (for web
/// clients like llama.cpp's webui that connect over the network and can't spawn a
/// subprocess). HTTP binds are loopback-only unless `unsafe_http` is set. Both
/// transports drive the same single session actor.
pub fn run(
    backend: String,
    connect: Option<String>,
    dump: Option<PathBuf>,
    http: Option<String>,
    unsafe_http: bool,
) -> anyhow::Result<()> {
    if let Some(addr) = http.as_deref() {
        check_http_bind_policy(addr, unsafe_http)?;
    }

    // The stdio transport speaks MCP on stdout, so all logging goes to stderr.
    let session: SharedSession = Arc::new(std::sync::Mutex::new(SessionSlot::Vacant));
    let eager = dump.is_some() || connect.is_some() || backend == "memory";

    if eager {
        let is_dump = dump.is_some();
        let needs_ticker = !is_dump && backend != "memory";
        let label = if is_dump { "dump" } else { &backend };
        eprintln!("ntoseye-mcp: attaching ({label})...");
        let (tx, service_pending) = spawn_session(backend, connect, dump)?;
        *session.lock().unwrap() = SessionSlot::Active(tx.clone());
        if needs_ticker {
            spawn_service_ticker(tx, service_pending);
        }
    } else {
        if backend == "kd" {
            eprintln!(
                "ntoseye-mcp: note: pass --connect {default_kd} to auto-attach at startup, \
                 or use the 'open' tool after launch",
                default_kd = DEFAULT_KD_SOCKET,
            );
        } else {
            eprintln!(
                "ntoseye-mcp: note: --backend {backend} has no effect without --connect; \
                 use the 'open' tool to attach, or pass --connect to auto-attach at startup"
            );
        }
        eprintln!("ntoseye-mcp: starting without a session (use open_dump or open to attach)");
    }

    // Shared with the handlers so shutdown can interrupt an in-flight
    // `wait_for_stop` (otherwise the actor stays busy and never reaches
    // cleanup, leaving the VM frozen).
    let interrupt = Arc::new(AtomicBool::new(false));
    let interrupt_for_signal = interrupt.clone();
    // Slot kept aside so we can drive teardown of whichever session is active
    // by then (eager or opened later via open_dump/open).
    let session_for_shutdown = session.clone();

    let runtime = tokio::runtime::Runtime::new()?;
    let result = runtime.block_on(async move {
        let serve = async {
            match http {
                Some(addr) => {
                    eprintln!("ntoseye-mcp: serving Streamable HTTP at http://{addr}/mcp");
                    serve_http(session, addr, unsafe_http, interrupt).await
                }
                None => {
                    eprintln!("ntoseye-mcp: serving over stdio");
                    let service = NtoseyeMcp::new(session, interrupt).serve(stdio()).await?;
                    service.waiting().await?;
                    Ok(())
                }
            }
        };

        // Serve until the client disconnects (or the server errors), or until
        // Ctrl+C; either way fall through to teardown.
        let result = tokio::select! {
            r = serve => r,
            _ = tokio::signal::ctrl_c() => {
                eprintln!("ntoseye-mcp: interrupted");
                Ok(())
            }
        };

        // Ask the actor to remove our breakpoints and resume the VM before we
        // exit, so Ctrl+C doesn't leave a live guest frozen with int3s
        // installed (a no-op for dumps). Set the interrupt first so any
        // in-flight wait returns and the actor is free to process the Shutdown.
        eprintln!("ntoseye-mcp: cleaning up...");
        interrupt_for_signal.store(true, Ordering::Relaxed);
        // Clone the sender out so the slot's mutex isn't held across the await
        let shutdown_tx = match &*session_for_shutdown.lock().unwrap() {
            SessionSlot::Active(tx) => Some(tx.clone()),
            _ => None,
        };
        if let Some(tx) = shutdown_tx {
            let (ack_tx, ack_rx) = oneshot::channel();
            if tx.send(Command::Shutdown { ack: ack_tx }).is_ok() {
                let _ = tokio::time::timeout(std::time::Duration::from_secs(5), ack_rx).await;
            }
        }
        result
    });
    runtime.shutdown_background();
    result
}

/// Background servicing ticker: periodically nudge the actor to service the
/// guest while idle (see [`Command::Service`]), so a wrong-process hit on a
/// shared-page breakpoint doesn't leave it frozen between tool calls.
/// `service_pending` keeps at most one `Service` queued even if the actor is
/// busy in a long wait; the thread exits once the actor's channel closes
/// (send fails).
fn spawn_service_ticker(tx: mpsc::UnboundedSender<Command>, service_pending: Arc<AtomicBool>) {
    std::thread::spawn(move || {
        loop {
            std::thread::sleep(SERVICE_TICK);
            if service_pending.swap(true, Ordering::AcqRel) {
                continue;
            }
            if tx.send(Command::Service).is_err() {
                break;
            }
        }
    });
}

/// Serve the Streamable HTTP transport on `addr`, mounting the MCP service at
/// `/mcp`. Every HTTP session gets a clone of the handler (cheap; it holds only
/// the actor's channel sender), so all connections funnel to the one live
/// debugger session.
async fn serve_http(
    session: SharedSession,
    addr: String,
    unsafe_http: bool,
    interrupt: Arc<AtomicBool>,
) -> anyhow::Result<()> {
    use rmcp::transport::StreamableHttpService;
    use rmcp::transport::streamable_http_server::session::local::LocalSessionManager;
    use tower_http::cors::{AllowOrigin, Any, CorsLayer};

    let template = NtoseyeMcp::new(session, interrupt);
    let service = StreamableHttpService::new(
        move || Ok(template.clone()),
        LocalSessionManager::default().into(),
        Default::default(),
    );

    // Methods/headers stay permissive for the Streamable HTTP handshake. Origin
    // is the exposure that matters: loopback binds trust only loopback browser
    // origins, while `--unsafe-http` widens it to any origin.
    let allow_origin = if unsafe_http {
        AllowOrigin::any()
    } else {
        AllowOrigin::predicate(|origin, _parts| origin.to_str().is_ok_and(is_loopback_origin))
    };
    let cors = CorsLayer::new()
        .allow_origin(allow_origin)
        .allow_methods(Any)
        .allow_headers(Any)
        .expose_headers(Any);

    let router = axum::Router::new()
        .nest_service("/mcp", service)
        .layer(cors);
    let listener = tokio::net::TcpListener::bind(&addr).await?;
    axum::serve(listener, router).await?;
    Ok(())
}

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

    fn empty_mcp() -> NtoseyeMcp {
        NtoseyeMcp::new(
            Arc::new(std::sync::Mutex::new(SessionSlot::Vacant)),
            Arc::new(AtomicBool::new(false)),
        )
    }

    fn fake_active_mcp() -> NtoseyeMcp {
        let (tx, _rx) = mpsc::unbounded_channel::<Command>();
        NtoseyeMcp::new(
            Arc::new(std::sync::Mutex::new(SessionSlot::Active(tx))),
            Arc::new(AtomicBool::new(false)),
        )
    }

    #[test]
    fn structured_trap_frame_and_watchpoint_tools_are_registered() {
        let mcp = empty_mcp();
        for name in ["inspect_trap_frame", "set_watchpoint", "list_breakpoints"] {
            assert!(
                mcp.tool_router.get(name).is_some(),
                "missing MCP tool {name}"
            );
        }
    }

    #[test]
    fn loopback_origins_are_trusted() {
        assert!(is_loopback_origin("http://localhost"));
        assert!(is_loopback_origin("http://localhost:8080"));
        assert!(is_loopback_origin("http://127.0.0.1:3000"));
        assert!(is_loopback_origin("https://127.0.0.1"));
        assert!(is_loopback_origin("http://[::1]:9000"));
        assert!(is_loopback_origin("http://LOCALHOST:1234"));
    }

    #[test]
    fn non_loopback_origins_are_rejected() {
        assert!(!is_loopback_origin("http://meow.example.com"));
        assert!(!is_loopback_origin("https://meow.test:443"));
        assert!(!is_loopback_origin("http://10.0.0.5:8080"));
        // `null` (sandboxed iframes, file://) is not a loopback host.
        assert!(!is_loopback_origin("null"));
        // Malformed / no scheme.
        assert!(!is_loopback_origin("127.0.0.1"));
        assert!(!is_loopback_origin(""));
    }

    #[tokio::test]
    async fn open_dump_rejects_when_session_active() {
        let mcp = fake_active_mcp();
        let err = mcp
            .open_dump(Parameters(OpenDumpArgs {
                path: "/tmp/fake.dmp".into(),
            }))
            .await
            .unwrap_err();
        assert!(
            err.message.contains("already active"),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test]
    async fn open_rejects_when_session_active() {
        let mcp = fake_active_mcp();
        let err = mcp
            .open(Parameters(OpenArgs {
                backend: "kd".into(),
                connect: None,
            }))
            .await
            .unwrap_err();
        assert!(
            err.message.contains("already active"),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test]
    async fn open_rejects_unknown_backend() {
        let mcp = empty_mcp();
        let err = mcp
            .open(Parameters(OpenArgs {
                backend: "nope".into(),
                connect: None,
            }))
            .await
            .unwrap_err();
        assert!(
            err.message.contains("unknown backend"),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test]
    async fn open_memory_rejects_connect() {
        let mcp = empty_mcp();
        let err = mcp
            .open(Parameters(OpenArgs {
                backend: "memory".into(),
                connect: Some("127.0.0.1:1234".into()),
            }))
            .await
            .unwrap_err();
        assert!(
            err.message.contains("does not use"),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test]
    async fn open_dump_nonexistent_file() {
        let mcp = empty_mcp();
        let err = mcp
            .open_dump(Parameters(OpenDumpArgs {
                path: "/tmp/ntoseye-test-does-not-exist.dmp".into(),
            }))
            .await
            .unwrap_err();
        assert!(
            err.message.contains("failed to open dump"),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test]
    async fn open_kd_nonexistent_socket() {
        let mcp = empty_mcp();
        let err = mcp
            .open(Parameters(OpenArgs {
                backend: "kd".into(),
                connect: Some("/tmp/ntoseye-test-does-not-exist.sock".into()),
            }))
            .await
            .unwrap_err();
        assert!(
            err.message.contains("failed to connect"),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test]
    async fn open_kd_default_socket() {
        let mcp = empty_mcp();
        // Use a nonexistent socket so the test doesn't block on a real KD
        // handshake when /tmp/ntoseye-kd.sock exists.
        let err = mcp
            .open(Parameters(OpenArgs {
                backend: "kd".into(),
                connect: Some("/tmp/ntoseye-test-default-does-not-exist.sock".into()),
            }))
            .await
            .unwrap_err();
        assert!(
            err.message.contains("failed to connect"),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test]
    async fn close_when_no_session() {
        let mcp = empty_mcp();
        let err = mcp.close().await.unwrap_err();
        assert!(
            err.message.contains("no debugger session"),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test]
    async fn close_active_session() {
        let (tx, mut rx) = mpsc::unbounded_channel::<Command>();
        let mcp = NtoseyeMcp::new(
            Arc::new(std::sync::Mutex::new(SessionSlot::Active(tx))),
            Arc::new(AtomicBool::new(false)),
        );

        // Simulate the actor: drain commands and ack shutdown.
        tokio::spawn(async move {
            while let Some(cmd) = rx.recv().await {
                if let Command::Shutdown { ack } = cmd {
                    let _ = ack.send(());
                    break;
                }
            }
        });

        let result = mcp.close().await.expect("close should succeed");
        let text = result
            .content
            .first()
            .and_then(|c| c.as_text())
            .map(|t| t.text.as_str())
            .unwrap_or("");
        assert!(text.contains("closed"), "unexpected result: {text}");
        assert!(
            matches!(&*mcp.session.lock().unwrap(), SessionSlot::Vacant),
            "session should be vacant after close"
        );
        assert!(!mcp.interrupt.load(Ordering::Relaxed));
    }

    #[tokio::test]
    async fn close_then_reopen_allowed() {
        let (tx, mut rx) = mpsc::unbounded_channel::<Command>();
        let mcp = NtoseyeMcp::new(
            Arc::new(std::sync::Mutex::new(SessionSlot::Active(tx))),
            Arc::new(AtomicBool::new(false)),
        );

        tokio::spawn(async move {
            while let Some(cmd) = rx.recv().await {
                if let Command::Shutdown { ack } = cmd {
                    let _ = ack.send(());
                    break;
                }
            }
        });

        mcp.close().await.expect("close should succeed");

        // After close, open should no longer reject with "already active".
        // Use a nonexistent socket so the test doesn't block on a real KD
        // handshake when /tmp/ntoseye-kd.sock exists.
        let err = mcp
            .open(Parameters(OpenArgs {
                backend: "kd".into(),
                connect: Some("/tmp/ntoseye-test-close-reopen-does-not-exist.sock".into()),
            }))
            .await
            .unwrap_err();
        assert!(
            !err.message.contains("already active"),
            "session should not be active after close: {err:?}"
        );
    }

    #[tokio::test]
    async fn close_cancels_opening_session() {
        let mcp = NtoseyeMcp::new(
            Arc::new(std::sync::Mutex::new(SessionSlot::Opening(0))),
            Arc::new(AtomicBool::new(false)),
        );

        let result = mcp.close().await.expect("close of Opening should succeed");
        let text = result
            .content
            .first()
            .and_then(|c| c.as_text())
            .map(|t| t.text.as_str())
            .unwrap_or("");
        assert!(text.contains("closed"), "unexpected result: {text}");
        assert!(
            matches!(&*mcp.session.lock().unwrap(), SessionSlot::Vacant),
            "session should be vacant after close"
        );
    }

    #[tokio::test]
    async fn triage_requires_session() {
        let mcp = empty_mcp();
        let err = mcp.triage().await.unwrap_err();
        assert!(
            err.message.contains("no debugger session"),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test]
    async fn eval_requires_session() {
        let mcp = empty_mcp();
        let err = mcp
            .eval(Parameters(EvalArgs {
                expression: "0x1234".into(),
            }))
            .await
            .unwrap_err();
        assert!(
            err.message.contains("no debugger session"),
            "unexpected error: {err:?}"
        );
    }

    #[tokio::test]
    async fn read_pointer_requires_session() {
        let mcp = empty_mcp();
        let err = mcp
            .read_pointer(Parameters(ReadPointerArgs {
                address: "0x1234".into(),
            }))
            .await
            .unwrap_err();
        assert!(
            err.message.contains("no debugger session"),
            "unexpected error: {err:?}"
        );
    }
}