native-devtools-mcp 0.10.1

MCP server for computer use & browser automation — screenshot, OCR, click, type, find_text, Chrome/Electron CDP, template matching. macOS, Windows & Android.
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
use crate::app_protocol::AppProtocolClient;
use crate::tools::{
    app_protocol as app_tools, find_image, image_cache::ImageCache, input as input_tools,
    load_image, navigation, screenshot, screenshot_cache::ScreenshotCache,
};
use base64::Engine;
use rmcp::model::Content;
use rmcp::{
    handler::server::ServerHandler,
    model::{
        CallToolRequestParam, CallToolResult, ListToolsResult, PaginatedRequestParam,
        ProtocolVersion, ServerCapabilities, ServerInfo, Tool, ToolAnnotations,
    },
    service::{RequestContext, RoleServer},
    Error as McpError,
};
use serde_json::Value;
use std::sync::Arc;
use tokio::sync::RwLock;

use crate::android::AndroidDevice;

/// Serialize a value to pretty-printed JSON, returning a formatted error on failure.
fn to_json_pretty(value: &impl serde::Serialize) -> String {
    serde_json::to_string_pretty(value).unwrap_or_else(|e| format!("Failed to serialize: {}", e))
}

/// Extract a required string field from a JSON value.
fn parse_string_field(args: &Value, field: &str) -> Result<String, McpError> {
    args.get(field)
        .and_then(|v| v.as_str())
        .map(String::from)
        .ok_or_else(|| McpError::invalid_params(format!("missing required param: {}", field), None))
}

/// Extract required `x` and `y` number fields from a JSON value.
fn parse_xy(args: &Value) -> Result<(f64, f64), McpError> {
    let x = args
        .get("x")
        .and_then(|v| v.as_f64())
        .ok_or_else(|| McpError::invalid_params("missing required param: x", None))?;
    let y = args
        .get("y")
        .and_then(|v| v.as_f64())
        .ok_or_else(|| McpError::invalid_params("missing required param: y", None))?;
    Ok((x, y))
}

fn json_to_object(value: Value) -> rmcp::model::JsonObject {
    match value {
        Value::Object(map) => map,
        _ => Default::default(),
    }
}

// ============================================================================
// Tool safety-hint annotations
//
// Each tool is tagged with the MCP `ToolAnnotations` hints
// (readOnlyHint, destructiveHint, idempotentHint, openWorldHint) so clients
// can reason about safety before invoking. These are *hints* per the MCP spec.
// ============================================================================

/// Read-only, idempotent, closed-world (queries: screenshots, snapshots, finds).
fn annotate_read_only() -> ToolAnnotations {
    ToolAnnotations::new()
        .read_only(true)
        .idempotent(true)
        .destructive(false)
        .open_world(false)
}

/// Non-destructive state change on a closed world (clicks, typing, scrolling,
/// focusing, launching a local app, connecting to a local debug server).
fn annotate_state_change() -> ToolAnnotations {
    ToolAnnotations::new()
        .read_only(false)
        .idempotent(false)
        .destructive(false)
        .open_world(false)
}

/// Destructive tool on a closed world (quit app, close tab).
fn annotate_destructive() -> ToolAnnotations {
    ToolAnnotations::new()
        .read_only(false)
        .idempotent(false)
        .destructive(true)
        .open_world(false)
}

/// Non-destructive state change that reaches an open world (e.g. web
/// navigation, arbitrary URL loads).
fn annotate_open_world_state_change() -> ToolAnnotations {
    ToolAnnotations::new()
        .read_only(false)
        .idempotent(false)
        .destructive(false)
        .open_world(true)
}

/// Arbitrary code evaluation in an open world (JS eval in a browser page).
fn annotate_open_world_destructive() -> ToolAnnotations {
    ToolAnnotations::new()
        .read_only(false)
        .idempotent(false)
        .destructive(true)
        .open_world(true)
}

/// Apply the given annotation to every tool in `tools` whose name appears in
/// `names`. Missing names are silently ignored because the conditional tool
/// groups (app_*, android_*, cdp_*, hover, recording) aren't always present.
/// The `test_every_tool_has_annotations` test catches any tool left without
/// an annotation.
fn annotate_tools(tools: &mut [Tool], names: &[&str], annotation: ToolAnnotations) {
    for name in names {
        if let Some(tool) = tools.iter_mut().find(|t| t.name.as_ref() == *name) {
            tool.annotations = Some(annotation.clone());
        }
    }
}

#[derive(Clone)]
pub struct MacOSDevToolsServer {
    app_client: Arc<RwLock<Option<AppProtocolClient>>>,
    screenshot_cache: Arc<RwLock<ScreenshotCache>>,
    image_cache: Arc<RwLock<ImageCache>>,
    android_device: Arc<RwLock<Option<AndroidDevice>>>,
    hover_tracker: Arc<RwLock<Option<crate::tools::hover_tracker::HoverTracker>>>,
    screen_recorder: Arc<RwLock<Option<crate::tools::screen_recorder::ScreenRecorder>>>,
    #[cfg(feature = "cdp")]
    cdp_client: Arc<RwLock<Option<crate::cdp::CdpClient>>>,
    #[cfg(target_os = "macos")]
    ax_session: Arc<crate::tools::ax_session::AxSession>,
}

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

impl MacOSDevToolsServer {
    pub fn new() -> Self {
        Self {
            app_client: Arc::new(RwLock::new(None)),
            screenshot_cache: Arc::new(RwLock::new(ScreenshotCache::default())),
            image_cache: Arc::new(RwLock::new(ImageCache::default())),
            android_device: Arc::new(RwLock::new(None)),
            hover_tracker: Arc::new(RwLock::new(None)),
            screen_recorder: Arc::new(RwLock::new(None)),
            #[cfg(feature = "cdp")]
            cdp_client: Arc::new(RwLock::new(None)),
            #[cfg(target_os = "macos")]
            ax_session: Arc::new(crate::tools::ax_session::AxSession::new()),
        }
    }

    async fn is_connected(&self) -> bool {
        self.app_client.read().await.is_some()
    }

    async fn is_android_connected(&self) -> bool {
        self.android_device.read().await.is_some()
    }

    async fn is_hover_tracking(&self) -> bool {
        self.hover_tracker.read().await.is_some()
    }

    async fn is_recording(&self) -> bool {
        self.screen_recorder.read().await.is_some()
    }

    #[cfg(feature = "cdp")]
    async fn is_cdp_connected(&self) -> bool {
        self.cdp_client.read().await.is_some()
    }

    /// Acquire the android device lock and call `f` with a mutable reference.
    /// Returns a "not connected" error result if no device is connected.
    async fn with_android_device<F>(&self, f: F) -> CallToolResult
    where
        F: FnOnce(&mut AndroidDevice) -> CallToolResult,
    {
        let mut guard = self.android_device.write().await;
        match guard.as_mut() {
            Some(device) => f(device),
            None => CallToolResult::error(vec![Content::text(
                "No Android device connected. Use android_connect first.",
            )]),
        }
    }

    /// Get tools available based on connection state.
    /// Base tools and app_connect are always available.
    /// Other app_* tools are only available when connected.
    ///
    /// CDP tools are always listed (independent of `cdp_connected`) so the
    /// tool surface does not mutate mid-session — clients that prompt-cache
    /// the tool list stay warm. Each CDP tool handler returns a clean
    /// "No CDP connection" error when called without an active connection.
    /// The `cdp_connected` parameter is accepted for API stability but is
    /// no longer used to gate visibility.
    pub fn get_tools(
        app_connected: bool,
        android_connected: bool,
        cdp_connected: bool,
        hover_tracking: bool,
        recording: bool,
    ) -> Vec<Tool> {
        let _ = cdp_connected;
        let mut tools = Self::get_base_tools();
        tools.push(Self::get_app_connect_tool());
        if app_connected {
            tools.extend(Self::get_app_tools());
        }
        tools.extend(Self::get_android_base_tools());
        if android_connected {
            tools.extend(Self::get_android_tools());
        }
        #[cfg(feature = "cdp")]
        {
            tools.push(Self::get_cdp_connect_tool());
            tools.extend(Self::get_cdp_tools());
        }
        tools.extend(Self::get_hover_tracking_tools(hover_tracking));
        tools.extend(Self::get_recording_tools(recording));
        Self::apply_tool_annotations(&mut tools);
        tools
    }

    /// Attach MCP safety-hint annotations (readOnlyHint, destructiveHint,
    /// idempotentHint, openWorldHint) to every tool in the list.
    ///
    /// Classification keys off tool *name* (not description or schema) so
    /// it's stable across schema edits. Tool names absent from `tools`
    /// (conditional groups gated by connection state) are ignored —
    /// `test_every_tool_has_annotations` catches any unclassified tool.
    fn apply_tool_annotations(tools: &mut [Tool]) {
        // Read-only queries: screenshots, snapshots, finds, metadata.
        annotate_tools(
            tools,
            &[
                "take_screenshot",
                "list_windows",
                "list_apps",
                "get_displays",
                "find_text",
                "element_at_point",
                "find_image",
                "probe_app",
                "android_list_devices",
                "app_get_info",
                "app_get_tree",
                "app_query",
                "app_get_element",
                "app_list_windows",
                "app_screenshot",
                "android_screenshot",
                "android_find_text",
                "android_list_apps",
                "android_get_display_info",
                "android_get_current_activity",
            ],
            annotate_read_only(),
        );

        // Non-destructive state changes: clicks, typing, launches, sessions.
        annotate_tools(
            tools,
            &[
                "focus_window",
                "launch_app",
                "click",
                "move_mouse",
                "drag",
                "scroll",
                "type_text",
                "press_key",
                "load_image",
                "app_connect",
                "android_connect",
                "start_hover_tracking",
                "start_recording",
                "app_disconnect",
                "app_click",
                "app_type",
                "app_press_key",
                "app_focus",
                "app_focus_window",
                "android_disconnect",
                "android_click",
                "android_swipe",
                "android_type_text",
                "android_press_key",
                "android_launch_app",
                "get_hover_events",
                "stop_hover_tracking",
                "stop_recording",
            ],
            annotate_state_change(),
        );

        // take_ax_snapshot is state-changing on both platforms: macOS bumps
        // the session generation on every call (invalidating prior uids);
        // Windows advertises the same posture for a uniform client-safety
        // contract even though the underlying UIA read is still pure.
        annotate_tools(tools, &["take_ax_snapshot"], annotate_state_change());

        #[cfg(target_os = "macos")]
        {
            annotate_tools(
                tools,
                &["ax_click", "ax_set_value", "ax_select"],
                annotate_state_change(),
            );
        }

        annotate_tools(tools, &["quit_app"], annotate_destructive());

        #[cfg(feature = "cdp")]
        {
            annotate_tools(
                tools,
                &[
                    "cdp_take_dom_snapshot",
                    "cdp_summarize_page",
                    "cdp_find_elements",
                    "cdp_get_element_context",
                    "cdp_list_pages",
                    "cdp_element_at_point",
                    "cdp_wait_for",
                    "cdp_wait_for_page_change",
                ],
                annotate_read_only(),
            );
            annotate_tools(
                tools,
                &[
                    "cdp_connect",
                    "cdp_disconnect",
                    "cdp_click",
                    "cdp_hover",
                    "cdp_fill",
                    "cdp_press_key",
                    "cdp_handle_dialog",
                    "cdp_type_text",
                    "cdp_select_page",
                ],
                annotate_state_change(),
            );
            annotate_tools(
                tools,
                &["cdp_navigate", "cdp_new_page"],
                annotate_open_world_state_change(),
            );
            annotate_tools(
                tools,
                &["cdp_evaluate_script"],
                annotate_open_world_destructive(),
            );
            annotate_tools(tools, &["cdp_close_page"], annotate_destructive());
        }
    }

    /// Tools that are always available (system tools, CGEvent tools, etc.)
    fn get_base_tools() -> Vec<Tool> {
        #[allow(unused_mut)]
        let mut tools = vec![
            Tool::new(
                "take_screenshot",
                "Capture a screenshot of the screen, a specific window, or a region. Returns a base64-encoded image, JSON metadata for coordinate conversion, and OCR text annotations including clickable coordinates.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "mode": {
                            "type": "string",
                            "enum": ["screen", "window", "region"],
                            "description": "Capture mode (default: 'window'). Prefer 'window' with app_name for focused screenshots. Only use 'screen' when you need to see multiple windows or the desktop.",
                            "default": "window"
                        },
                        "window_id": {
                            "type": "integer",
                            "description": "Window ID to capture (required for mode='window')"
                        },
                        "app_name": {
                            "type": "string",
                            "description": "Application name to capture (for mode='window', alternative to window_id)"
                        },
                        "x": {
                            "type": "number",
                            "description": "X coordinate of region (required for mode='region')"
                        },
                        "y": {
                            "type": "number",
                            "description": "Y coordinate of region (required for mode='region')"
                        },
                        "width": {
                            "type": "number",
                            "description": "Width of region (required for mode='region')"
                        },
                        "height": {
                            "type": "number",
                            "description": "Height of region (required for mode='region')"
                        },
                        "include_ocr": {
                            "type": "boolean",
                            "description": "Include OCR text detection with clickable coordinates (default: true)",
                            "default": true
                        }
                    }
                }))),
            ),
            Tool::new(
                "list_windows",
                "List all visible windows on screen with their IDs, titles, app names, and bounds.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "app_name": {
                            "type": "string",
                            "description": "Filter windows by application name (optional)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "list_apps",
                "List all running applications with their names, bundle IDs, and PIDs.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "app_name": {
                            "type": "string",
                            "description": "Filter by application name (case-insensitive substring match)"
                        },
                        "user_apps_only": {
                            "type": "boolean",
                            "description": "Only return user-facing apps (excludes system agents, helpers, and daemons)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "focus_window",
                "Bring a window or application to the front. Specify window_id, app_name, or pid.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "window_id": {
                            "type": "integer",
                            "description": "Window ID to focus"
                        },
                        "app_name": {
                            "type": "string",
                            "description": "Application name to focus"
                        },
                        "pid": {
                            "type": "integer",
                            "description": "Process ID to focus"
                        }
                    }
                }))),
            ),
            Tool::new(
                "launch_app",
                "Launch an application by name. On macOS, finds apps in /Applications and other standard locations. If the app is already running and no args are provided, brings it to the front. If args are provided and the app is already running, returns an error — use quit_app first.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["app_name"],
                    "properties": {
                        "app_name": {
                            "type": "string",
                            "description": "Application name to launch (e.g., 'Calculator', 'Safari')"
                        },
                        "args": {
                            "type": "array",
                            "items": { "type": "string" },
                            "description": "CLI arguments to pass to the app (e.g., ['--remote-debugging-port=9222']). Only applied on fresh launch — if the app is already running, returns an error."
                        },
                        "background": {
                            "type": "boolean",
                            "description": "If true, launch without bringing the app to the foreground (uses `open -g` on macOS). Recommended when the next action will use CDP or AX dispatch, which are focus-preserving."
                        }
                    }
                }))),
            ),
            Tool::new(
                "quit_app",
                "Quit a running application by name. Graceful by default (app can save state). Use force=true to kill immediately.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["app_name"],
                    "properties": {
                        "app_name": {
                            "type": "string",
                            "description": "Application name to quit (e.g., 'Calculator', 'Safari')"
                        },
                        "force": {
                            "type": "boolean",
                            "description": "Force kill instead of graceful quit (default: false)"
                        }
                    }
                }))),
            ),
            // System-level input tools (CGEvent on macOS, SendInput on Windows)
            Tool::new(
                "click",
                "Click at screen coordinates. Pass exactly one coordinate variant — the runtime \
                 rejects mixes. Variants: \
                 (1) 'screenshot-pixels' (PREFERRED after take_screenshot) — screenshot_x, \
                 screenshot_y, screenshot_origin_x, screenshot_origin_y, screenshot_scale from \
                 take_screenshot metadata; \
                 (2) 'screen' — absolute screen x, y (use with find_text results); \
                 (3) 'window-relative' — window_x, window_y, window_id from list_windows; \
                 (4) 'screenshot-pixels-legacy' (DEPRECATED) — screenshot_x, screenshot_y, \
                 screenshot_window_id. \
                 Works with any app (egui, Electron, etc.). Requires Accessibility permission on macOS.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "x": {
                            "type": "number",
                            "description": "[screen variant] Absolute screen X coordinate. Use with find_text results."
                        },
                        "y": {
                            "type": "number",
                            "description": "[screen variant] Absolute screen Y coordinate. Use with find_text results."
                        },
                        "window_x": {
                            "type": "number",
                            "description": "[window-relative variant] X relative to window top-left. Pair with window_y and window_id."
                        },
                        "window_y": {
                            "type": "number",
                            "description": "[window-relative variant] Y relative to window top-left. Pair with window_x and window_id."
                        },
                        "window_id": {
                            "type": "integer",
                            "description": "[window-relative variant] Target window ID (from list_windows)."
                        },
                        "screenshot_x": {
                            "type": "number",
                            "description": "[screenshot-pixels / screenshot-pixels-legacy] X pixel inside the screenshot image."
                        },
                        "screenshot_y": {
                            "type": "number",
                            "description": "[screenshot-pixels / screenshot-pixels-legacy] Y pixel inside the screenshot image."
                        },
                        "screenshot_origin_x": {
                            "type": "number",
                            "description": "[screenshot-pixels, PREFERRED] screenshot_origin_x from take_screenshot metadata."
                        },
                        "screenshot_origin_y": {
                            "type": "number",
                            "description": "[screenshot-pixels, PREFERRED] screenshot_origin_y from take_screenshot metadata."
                        },
                        "screenshot_scale": {
                            "type": "number",
                            "description": "[screenshot-pixels, PREFERRED] screenshot_scale from take_screenshot metadata."
                        },
                        "screenshot_window_id": {
                            "type": "integer",
                            "description": "[screenshot-pixels-legacy, DEPRECATED] Window ID the screenshot was taken from. Prefer screenshot_origin_x/y + screenshot_scale."
                        },
                        "button": {
                            "type": "string",
                            "enum": ["left", "right", "center"],
                            "description": "Mouse button (default: left)"
                        },
                        "click_count": {
                            "type": "integer",
                            "description": "Number of clicks (1=single, 2=double)",
                            "default": 1
                        }
                    }
                }))),
            ),
            Tool::new(
                "move_mouse",
                "Move mouse cursor to screen coordinates. Requires Accessibility permission on macOS.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["x", "y"],
                    "properties": {
                        "x": {
                            "type": "number",
                            "description": "Screen X coordinate"
                        },
                        "y": {
                            "type": "number",
                            "description": "Screen Y coordinate"
                        }
                    }
                }))),
            ),
            Tool::new(
                "drag",
                "Drag from one point to another. Requires Accessibility permission on macOS.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["start_x", "start_y", "end_x", "end_y"],
                    "properties": {
                        "start_x": {
                            "type": "number",
                            "description": "Start X coordinate"
                        },
                        "start_y": {
                            "type": "number",
                            "description": "Start Y coordinate"
                        },
                        "end_x": {
                            "type": "number",
                            "description": "End X coordinate"
                        },
                        "end_y": {
                            "type": "number",
                            "description": "End Y coordinate"
                        },
                        "button": {
                            "type": "string",
                            "enum": ["left", "right", "center"],
                            "description": "Mouse button (default: left)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "scroll",
                "Scroll at a position. Requires Accessibility permission on macOS.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["x", "y", "delta_y"],
                    "properties": {
                        "x": {
                            "type": "number",
                            "description": "Screen X coordinate to scroll at"
                        },
                        "y": {
                            "type": "number",
                            "description": "Screen Y coordinate to scroll at"
                        },
                        "delta_x": {
                            "type": "integer",
                            "description": "Horizontal scroll amount (positive=right)",
                            "default": 0
                        },
                        "delta_y": {
                            "type": "integer",
                            "description": "Vertical scroll amount (negative=up, positive=down)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "type_text",
                "Type text at the current cursor position. Works with any app. Requires Accessibility permission on macOS.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["text"],
                    "properties": {
                        "text": {
                            "type": "string",
                            "description": "Text to type"
                        }
                    }
                }))),
            ),
            Tool::new(
                "press_key",
                "Press a key combination. Works with any app. Requires Accessibility permission on macOS.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["key"],
                    "properties": {
                        "key": {
                            "type": "string",
                            "description": "Key to press (e.g., 'return', 'tab', 'escape', 'a', 'f1', 'left', 'up')"
                        },
                        "modifiers": {
                            "type": "array",
                            "items": { "type": "string" },
                            "description": "Modifier keys: 'shift', 'control', 'option', 'command'",
                            "default": []
                        }
                    }
                }))),
            ),
            Tool::new(
                "get_displays",
                "Get information about all connected displays including bounds, scale factors, and resolution.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ),
            Tool::new(
                "find_text",
                "PREFERRED for clicking buttons/labels by name. Finds text on screen using the platform accessibility API (macOS Accessibility, Windows UI Automation) with OCR fallback, and returns screen coordinates ready for the click tool. Use this instead of visually estimating coordinates from screenshots. Can be scoped to a specific app window for faster, more precise results. Note: accessibility results use semantic element names (e.g., 'All Clear' instead of 'AC', 'Subtract' instead of '\u{2212}'), so search by meaning rather than displayed symbol. When no matches are found, the response includes an available_elements array listing all UI element names in the target window — use this to find the correct name and retry. Requires macOS 10.15+ or Windows 10 1903+.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["text"],
                    "properties": {
                        "text": {
                            "type": "string",
                            "description": "Text to search for (case-insensitive substring match). Matches against accessibility element names first (e.g., 'All Clear', 'Subtract'), then falls back to OCR on visible text."
                        },
                        "app_name": {
                            "type": "string",
                            "description": "Application name to scope the search to a specific app's window (e.g., 'Calculator'). Faster and avoids false matches from other windows."
                        },
                        "window_id": {
                            "type": "integer",
                            "description": "Window ID to scope the search to a specific window"
                        },
                        "display_id": {
                            "type": "integer",
                            "description": "Display ID to search on. Use get_displays to list available displays. If omitted, searches the main display. Ignored when window_id or app_name is provided."
                        },
                        "uses_language_correction": {
                            "type": "boolean",
                            "description": "Enable language correction for better word accuracy in OCR fallback. Default is false, which is better for UI automation (buttons, labels, single characters). Has no effect when results come from the accessibility API."
                        }
                    }
                }))),
            ),
            Tool::new(
                "element_at_point",
                "Given screen coordinates (x, y), return the accessibility element at that point (name, role, label, value, bounds, pid, app_name). Optional app_name param to scope the lookup to a specific application for faster, more precise results.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["x", "y"],
                    "properties": {
                        "x": {
                            "type": "number",
                            "description": "Screen X coordinate"
                        },
                        "y": {
                            "type": "number",
                            "description": "Screen Y coordinate"
                        },
                        "app_name": {
                            "type": "string",
                            "description": "Application name to scope the lookup to a specific app (e.g., 'Calculator'). Faster and avoids ambiguity at window edges."
                        }
                    }
                }))),
            ),
            Tool::new(
                "find_image",
                "Find a template image within a screenshot using template matching. Returns precise click coordinates for non-text UI elements like icons and shapes. Use screenshot_id from take_screenshot or provide screenshot_image_base64. Use template_id from load_image or provide template_image_base64.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "screenshot_id": {
                            "type": "string",
                            "description": "Screenshot ID from a previous take_screenshot call (preferred)"
                        },
                        "screenshot_image_base64": {
                            "type": "string",
                            "description": "Base64-encoded screenshot image (used if no screenshot_id)"
                        },
                        "template_id": {
                            "type": "string",
                            "description": "Image ID from a previous load_image call (preferred over template_image_base64)"
                        },
                        "template_image_base64": {
                            "type": "string",
                            "description": "Base64-encoded template image to find (used if no template_id)"
                        },
                        "mask_id": {
                            "type": "string",
                            "description": "Image ID from a previous load_image call for the mask"
                        },
                        "mask_image_base64": {
                            "type": "string",
                            "description": "Base64-encoded mask image (optional; white=match, black=ignore)"
                        },
                        "mode": {
                            "type": "string",
                            "enum": ["fast", "accurate"],
                            "description": "Matching mode: 'fast' (default) for quick searches, 'accurate' for thorough matching",
                            "default": "fast"
                        },
                        "threshold": {
                            "type": "number",
                            "description": "Minimum match score 0.0-1.0 (default: 0.75)"
                        },
                        "max_results": {
                            "type": "integer",
                            "description": "Maximum matches to return (default: 3 fast, 5 accurate)"
                        },
                        "scales": {
                            "type": "object",
                            "description": "Scale search range {min, max, step}",
                            "properties": {
                                "min": { "type": "number", "default": 0.8 },
                                "max": { "type": "number", "default": 1.2 },
                                "step": { "type": "number", "default": 0.1 }
                            }
                        },
                        "search_region": {
                            "type": "object",
                            "description": "Limit search to region {x, y, w, h} in screenshot pixels",
                            "properties": {
                                "x": { "type": "integer" },
                                "y": { "type": "integer" },
                                "w": { "type": "integer" },
                                "h": { "type": "integer" }
                            }
                        },
                        "stride": {
                            "type": "integer",
                            "description": "Search step size (default: 2 fast, 1 accurate)"
                        },
                        "rotations": {
                            "type": "array",
                            "items": { "type": "number" },
                            "description": "Rotations to try in degrees (only 0, 90, 180, 270 supported)"
                        },
                        "return_screen_coords": {
                            "type": "boolean",
                            "description": "Include screen coordinates for clicking (default: true)",
                            "default": true
                        }
                    }
                }))),
            ),
            Tool::new(
                "load_image",
                "Load an image from a local file path and cache it for use with find_image. Returns an image_id that can be passed to find_image as template_id or mask_id. This avoids manually base64-encoding images.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["path"],
                    "properties": {
                        "path": {
                            "type": "string",
                            "description": "Local filesystem path to the image file"
                        },
                        "id_prefix": {
                            "type": "string",
                            "description": "Optional prefix for the generated ID (e.g., 'template', 'mask')"
                        },
                        "max_width": {
                            "type": "integer",
                            "description": "Maximum width to downscale to (maintains aspect ratio)"
                        },
                        "max_height": {
                            "type": "integer",
                            "description": "Maximum height to downscale to (maintains aspect ratio)"
                        },
                        "as_mask": {
                            "type": "boolean",
                            "description": "If true, convert to single-channel grayscale mask",
                            "default": false
                        },
                        "return_base64": {
                            "type": "boolean",
                            "description": "If true, include base64-encoded image data in response",
                            "default": false
                        }
                    }
                }))),
            ),
            Tool::new(
                "take_ax_snapshot",
                "Take an accessibility tree snapshot of an application. Returns a \
                 structured text representation with unique element IDs, roles, names, \
                 state attributes, and (on macOS) per-element bounding boxes. Works for \
                 any app without requiring a debug port. \
                 macOS note: this tool mutates server state — each call bumps a \
                 monotonic generation and invalidates every prior uid for ax_click / \
                 ax_set_value / ax_select consumption. Snapshot IDs on macOS look like \
                 'a42g3' (generation-tagged); Windows IDs remain bare 'a42'. \
                 Re-snapshot immediately before any ax_click / ax_set_value / ax_select \
                 call.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "app_name": {
                            "type": "string",
                            "description": "Application name (defaults to frontmost app if omitted)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "probe_app",
                "Probe an application to determine its type (Native, ElectronApp, or ChromeBrowser). Works whether the app is running or not. Use this to decide between native automation (take_ax_snapshot, click, find_text) and CDP-based tools (cdp_connect, cdp_find_elements, cdp_take_dom_snapshot).",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["app_name"],
                    "properties": {
                        "app_name": {
                            "type": "string",
                            "description": "Application name to probe (e.g., 'Signal', 'Google Chrome', 'Safari')"
                        }
                    }
                }))),
            ),
        ];
        #[cfg(target_os = "macos")]
        {
            tools.push(Self::get_ax_click_tool());
            tools.push(Self::get_ax_set_value_tool());
            tools.push(Self::get_ax_select_tool());
        }
        tools
    }

    #[cfg(target_os = "macos")]
    fn get_ax_click_tool() -> Tool {
        Tool::new(
            "ax_click",
            "macOS only. Dispatch AXPress against a UI element identified by its uid \
             from the most recent take_ax_snapshot (e.g. \"a42g3\"). The 'g<gen>' \
             suffix is a generation tag — any fresh take_ax_snapshot invalidates all \
             prior uids, so always snapshot immediately before the uid is used. Does \
             not move the mouse cursor and does not steal focus from the frontmost \
             app. On failure the tool returns an error whose JSON body includes \
             { error: { code, message, fallback: { x, y } | null } }; when fallback is \
             populated you can retry via click(x, y).",
            Arc::new(json_to_object(serde_json::json!({
                "type": "object",
                "required": ["uid"],
                "properties": {
                    "uid": {
                        "type": "string",
                        "description": "Element uid from the most recent take_ax_snapshot, e.g. \"a42g3\". Must match the current snapshot generation."
                    }
                }
            }))),
        )
    }

    #[cfg(target_os = "macos")]
    fn get_ax_set_value_tool() -> Tool {
        Tool::new(
            "ax_set_value",
            "macOS only. Write to an element's kAXValueAttribute — value assignment, \
             not key-event typing. Use for AXTextField / AXTextArea / AXSearchField and \
             similar text widgets. Does NOT fire keydown/keyup, does NOT participate in \
             IME/composition, does NOT populate the app's undo stack, and will not work \
             on rich editors that refuse AXValue writes. On not_dispatchable, the caller \
             should fall back to a two-step sequence: click(fallback.x, fallback.y) to \
             focus, then type_text(text) for key-event input.",
            Arc::new(json_to_object(serde_json::json!({
                "type": "object",
                "required": ["uid", "text"],
                "properties": {
                    "uid": {
                        "type": "string",
                        "description": "Element uid from the most recent take_ax_snapshot, e.g. \"a42g3\"."
                    },
                    "text": {
                        "type": "string",
                        "description": "Text to assign via kAXValueAttribute."
                    }
                }
            }))),
        )
    }

    #[cfg(target_os = "macos")]
    fn get_ax_select_tool() -> Tool {
        Tool::new(
            "ax_select",
            "macOS only. Select a row inside an NSOutlineView / NSTableView (sidebars, \
             rule lists, file browsers) by writing AXSelectedRows on the enclosing \
             outline or table. Accepts a uid pointing at the row, a cell inside the row, \
             or any descendant — the tool walks up to the enclosing AXRow then the \
             enclosing AXOutline / AXTable. Does not move the cursor and does not steal \
             focus. Use ax_select instead of ax_click for row targets: rows typically \
             refuse AXPress (returning not_dispatchable or AX error -25205), and a \
             coordinate click steals focus. On failure the tool returns { error: { code, \
             message, fallback: { x, y } | null } } with codes snapshot_expired, \
             uid_not_found, no_row_ancestor, no_outline_container, not_dispatchable, or \
             ax_error. The fallback centre falls back from the row bbox to the \
             originally-targeted element bbox so the caller can still click(x, y) if \
             desired.",
            Arc::new(json_to_object(serde_json::json!({
                "type": "object",
                "required": ["uid"],
                "properties": {
                    "uid": {
                        "type": "string",
                        "description": "Element uid from the most recent take_ax_snapshot. Points at the row itself, a cell within the row, or any descendant."
                    }
                }
            }))),
        )
    }

    /// The app_connect tool - always available to initiate connections
    fn get_app_connect_tool() -> Tool {
        Tool::new(
            "app_connect",
            "Connect to an app's debug server via WebSocket. The app must have AppDebugKit embedded.",
            Arc::new(json_to_object(serde_json::json!({
                "type": "object",
                "required": ["url"],
                "properties": {
                    "url": {
                        "type": "string",
                        "description": "WebSocket URL (e.g., ws://127.0.0.1:9222)"
                    },
                    "expected_bundle_id": {
                        "type": "string",
                        "description": "Expected bundle ID (e.g., com.example.MyApp). Connection fails if mismatch."
                    },
                    "expected_app_name": {
                        "type": "string",
                        "description": "Expected app name (case-insensitive). Connection fails if mismatch."
                    }
                }
            }))),
        )
    }

    /// App debug tools - only available when connected to an app
    fn get_app_tools() -> Vec<Tool> {
        vec![
            Tool::new(
                "app_disconnect",
                "Disconnect from the app's debug server.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ),
            Tool::new(
                "app_get_info",
                "Get runtime info from the connected app (name, bundle ID, version, etc.).",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ),
            Tool::new(
                "app_get_tree",
                "Get the view hierarchy from the connected app.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "depth": {
                            "type": "integer",
                            "description": "Max depth to traverse (-1 for unlimited)",
                            "default": 5
                        },
                        "root_id": {
                            "type": "string",
                            "description": "Element ID to start from (optional, defaults to key window)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "app_query",
                "Find elements matching a CSS-like selector in the connected app.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["selector"],
                    "properties": {
                        "selector": {
                            "type": "string",
                            "description": "CSS-like selector (#id, .ClassName, [prop=value])"
                        },
                        "all": {
                            "type": "boolean",
                            "description": "Return all matches (default: first only)",
                            "default": false
                        }
                    }
                }))),
            ),
            Tool::new(
                "app_get_element",
                "Get detailed information about an element by ID.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["element_id"],
                    "properties": {
                        "element_id": {
                            "type": "string",
                            "description": "Element ID to get details for"
                        }
                    }
                }))),
            ),
            Tool::new(
                "app_click",
                "Click an element in the connected app by ID.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["element_id"],
                    "properties": {
                        "element_id": {
                            "type": "string",
                            "description": "Element ID to click"
                        },
                        "click_count": {
                            "type": "integer",
                            "description": "Number of clicks (1 for single, 2 for double)",
                            "default": 1
                        }
                    }
                }))),
            ),
            Tool::new(
                "app_type",
                "Type text into an element in the connected app.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["text"],
                    "properties": {
                        "text": {
                            "type": "string",
                            "description": "Text to type"
                        },
                        "element_id": {
                            "type": "string",
                            "description": "Element ID to type into (uses focused element if omitted)"
                        },
                        "clear_first": {
                            "type": "boolean",
                            "description": "Clear existing text first",
                            "default": false
                        }
                    }
                }))),
            ),
            Tool::new(
                "app_press_key",
                "Press a key or key combination in the connected app.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["key"],
                    "properties": {
                        "key": {
                            "type": "string",
                            "description": "Key to press (e.g., 'Return', 'Tab', 'Escape')"
                        },
                        "modifiers": {
                            "type": "array",
                            "items": { "type": "string" },
                            "description": "Modifier keys: 'shift', 'control', 'option', 'command'",
                            "default": []
                        }
                    }
                }))),
            ),
            Tool::new(
                "app_focus",
                "Focus an element in the connected app (make it first responder).",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["element_id"],
                    "properties": {
                        "element_id": {
                            "type": "string",
                            "description": "Element ID to focus"
                        }
                    }
                }))),
            ),
            Tool::new(
                "app_screenshot",
                "Take a screenshot of an element or window in the connected app.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "element_id": {
                            "type": "string",
                            "description": "Element ID to capture (whole window if omitted)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "app_list_windows",
                "List all windows in the connected app.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ),
            Tool::new(
                "app_focus_window",
                "Focus a window in the connected app (make it key and main window).",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["window_id"],
                    "properties": {
                        "window_id": {
                            "type": "string",
                            "description": "Window ID to focus (e.g., 'window-1')"
                        }
                    }
                }))),
            ),
        ]
    }

    /// Android tools that are always available (device discovery and connection)
    fn get_android_base_tools() -> Vec<Tool> {
        vec![
            Tool::new(
                "android_list_devices",
                "List all Android devices connected via ADB.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ),
            Tool::new(
                "android_connect",
                "Connect to an Android device by its serial number. Use android_list_devices to find available devices.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["serial"],
                    "properties": {
                        "serial": {
                            "type": "string",
                            "description": "Device serial number (e.g., 'emulator-5554' or a USB device serial)"
                        }
                    }
                }))),
            ),
        ]
    }

    /// Android tools available only when a device is connected
    fn get_android_tools() -> Vec<Tool> {
        vec![
            Tool::new(
                "android_disconnect",
                "Disconnect from the current Android device.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ),
            Tool::new(
                "android_screenshot",
                "Take a screenshot of the Android device screen. Returns a base64-encoded JPEG image.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "file_path": {
                            "type": "string",
                            "description": "Optional file path to save the screenshot PNG to instead of returning it inline."
                        }
                    }
                }))),
            ),
            Tool::new(
                "android_click",
                "Tap at screen coordinates on the Android device.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["x", "y"],
                    "properties": {
                        "x": {
                            "type": "number",
                            "description": "Screen X coordinate"
                        },
                        "y": {
                            "type": "number",
                            "description": "Screen Y coordinate"
                        }
                    }
                }))),
            ),
            Tool::new(
                "android_swipe",
                "Swipe from one point to another on the Android device.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["start_x", "start_y", "end_x", "end_y"],
                    "properties": {
                        "start_x": {
                            "type": "number",
                            "description": "Start X coordinate"
                        },
                        "start_y": {
                            "type": "number",
                            "description": "Start Y coordinate"
                        },
                        "end_x": {
                            "type": "number",
                            "description": "End X coordinate"
                        },
                        "end_y": {
                            "type": "number",
                            "description": "End Y coordinate"
                        },
                        "duration_ms": {
                            "type": "integer",
                            "description": "Duration of the swipe in milliseconds (optional, default is instant)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "android_type_text",
                "Type text on the Android device. Special characters are automatically escaped.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["text"],
                    "properties": {
                        "text": {
                            "type": "string",
                            "description": "Text to type"
                        }
                    }
                }))),
            ),
            Tool::new(
                "android_press_key",
                "Press a key on the Android device by keycode name (e.g., 'KEYCODE_HOME') or numeric code.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["key"],
                    "properties": {
                        "key": {
                            "type": "string",
                            "description": "Key to press (e.g., 'KEYCODE_HOME', 'KEYCODE_BACK', 'KEYCODE_ENTER', or a numeric keycode)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "android_find_text",
                "Find UI elements on the Android device screen that match the given text. Uses uiautomator to dump the view hierarchy and search for matching elements. Returns coordinates for clicking. When no matches are found, the response includes an available_elements array listing all UI element names on screen — use this to find the correct name and retry.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["text"],
                    "properties": {
                        "text": {
                            "type": "string",
                            "description": "Text to search for (case-insensitive substring match against text and content-desc attributes)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "android_list_apps",
                "List installed apps on the Android device.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "user_apps_only": {
                            "type": "boolean",
                            "description": "Only return user-installed (third-party) apps. Default is false (all packages)."
                        }
                    }
                }))),
            ),
            Tool::new(
                "android_launch_app",
                "Launch an app on the Android device by its package name.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["package_name"],
                    "properties": {
                        "package_name": {
                            "type": "string",
                            "description": "Package name to launch (e.g., 'com.android.settings')"
                        }
                    }
                }))),
            ),
            Tool::new(
                "android_get_display_info",
                "Get display information (size and density) from the Android device.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ),
            Tool::new(
                "android_get_current_activity",
                "Get the currently resumed activity on the Android device.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ),
        ]
    }

    /// Hover tracking tools. `start_hover_tracking` is always visible.
    /// `get_hover_events` and `stop_hover_tracking` only appear while tracking is active.
    fn get_hover_tracking_tools(tracking_active: bool) -> Vec<Tool> {
        let mut tools = vec![Tool::new(
            "start_hover_tracking",
            "Start tracking hover state changes. Polls cursor position and accessibility element at configurable intervals, recording transitions. Use get_hover_events to retrieve recorded events, and stop_hover_tracking to end the session. Only one tracking session can be active at a time.",
            Arc::new(json_to_object(serde_json::json!({
                "type": "object",
                "properties": {
                    "app_name": {
                        "type": "string",
                        "description": "Scope element lookup to a specific application (e.g., 'Safari'). Faster and avoids ambiguity."
                    },
                    "poll_interval_ms": {
                        "type": "integer",
                        "description": "Polling interval in milliseconds (default: 100)",
                        "default": 100
                    },
                    "max_duration_ms": {
                        "type": "integer",
                        "description": "Auto-stop after this many milliseconds (default: 60000 = 60s)",
                        "default": 60000
                    },
                    "min_dwell_ms": {
                        "type": "integer",
                        "description": "Minimum time (ms) cursor must stay on a new element before recording a transition. Filters out pass-through elements during fast mouse movement. 0 = record every change immediately. (default: 300)",
                        "default": 300
                    }
                }
            }))),
        )];
        if tracking_active {
            tools.push(Tool::new(
                "get_hover_events",
                "Retrieve and drain buffered hover events since the last call. Returns a JSON array of transition events, each with cursor position, element info, timestamp, and dwell time. Events are consumed — subsequent calls return only new events.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ));
            tools.push(Tool::new(
                "stop_hover_tracking",
                "Stop hover tracking and return any remaining buffered events. Ends the background polling task.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ));
        }
        tools
    }

    /// Screen recording tools. `start_recording` always visible,
    /// `stop_recording` only while recording is active.
    fn get_recording_tools(recording_active: bool) -> Vec<Tool> {
        let mut tools = vec![Tool::new(
            "start_recording",
            "Start recording the frontmost app's window at ~5fps. Writes timestamped JPEG frames to the specified output directory. Use stop_recording to end the session and get the frame list.",
            Arc::new(json_to_object(serde_json::json!({
                "type": "object",
                "properties": {
                    "output_dir": {
                        "type": "string",
                        "description": "Directory to write JPEG frames to (created if needed)"
                    },
                    "fps": {
                        "type": "integer",
                        "description": "Frames per second (default: 5)",
                        "default": 5
                    },
                    "max_duration_ms": {
                        "type": "integer",
                        "description": "Auto-stop after this many milliseconds (default: 60000 = 1 min)",
                        "default": 60000
                    }
                },
                "required": ["output_dir"]
            }))),
        )];
        if recording_active {
            tools.push(Tool::new(
                "stop_recording",
                "Stop screen recording and return all frame metadata as a JSON array.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ));
        }
        tools
    }

    #[cfg(feature = "cdp")]
    fn get_cdp_connect_tool() -> Tool {
        Tool::new(
            "cdp_connect",
            "Connect to a Chrome or Electron app via its remote debugging port. The app must be launched with --remote-debugging-port=PORT and --user-data-dir=PATH (Chrome 136+ requires a non-default profile for the debug port to open). After connecting, use cdp_summarize_page for page inventory, cdp_find_elements for targeted discovery, and cdp_get_element_context to expand an ambiguous match.",
            Arc::new(json_to_object(serde_json::json!({
                "type": "object",
                "required": ["port"],
                "properties": {
                    "port": {
                        "type": "integer",
                        "description": "The remote debugging port number"
                    }
                }
            }))),
        )
    }

    #[cfg(feature = "cdp")]
    const UID_DESC: &'static str =
        "Element UID from cdp_take_dom_snapshot or cdp_find_elements (d-prefixed)";

    fn get_cdp_tools() -> Vec<Tool> {
        vec![
            Tool::new(
                "cdp_disconnect",
                "Disconnect from the Chrome/Electron app. CDP tools remain listed but will return a 'not connected' error until cdp_connect is called again.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ),
            Tool::new(
                "cdp_take_dom_snapshot",
                "Take a full DOM snapshot of the selected browser page. Returns all interactive elements with UIDs prefixed 'd' (e.g., d1, d2). Use when you need the complete page structure — captures contenteditable editors, placeholder inputs, and custom widgets. For targeted lookups, prefer cdp_find_elements instead. UIDs are valid for cdp_click, cdp_fill, and other action tools.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "max_nodes": {
                            "type": "integer",
                            "description": "Maximum number of nodes to return (default: 500)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_summarize_page",
                "Return a compact page summary: URL, title, current page generation, and an inventory of interactive elements grouped by role with a few sample labels. Does not return element UIDs and does not overwrite the current DOM UID snapshot. Use this for orientation before issuing targeted cdp_find_elements queries.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ),
            Tool::new(
                "cdp_find_elements",
                "PREFERRED discovery tool. Search the live DOM for interactive elements matching a text query across labels, visible text, values, placeholders, titles, alt text, and test ids. Returns UIDs prefixed 'd' (e.g., d1, d2), match provenance, visible/accessibility text evidence, parent context for disambiguation, viewport geometry, warnings, and a page-level inventory grouped by role. Always try this first — it gives focused results without flooding context. Use cdp_take_dom_snapshot only if you need the full page structure. UIDs are valid for cdp_click, cdp_fill, and other action tools.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["query"],
                    "properties": {
                        "query": {
                            "type": "string",
                            "description": "Text to search for across element labels, visible text, values, placeholders, titles, alt text, and test ids"
                        },
                        "role": {
                            "type": "string",
                            "description": "Optional role filter (e.g., 'textbox', 'button')"
                        },
                        "max_results": {
                            "type": "integer",
                            "description": "Maximum matches to return (default: 10)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_get_element_context",
                "Expand a UID returned by the most recent cdp_find_elements or cdp_take_dom_snapshot call. Returns the stored match evidence, nearby snapshot matches, and bounded live DOM context around the element (ancestors, siblings, and children). Use this when targeted search returns multiple plausible matches or the match needs local context before acting.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["uid"],
                    "properties": {
                        "uid": {
                            "type": "string",
                            "description": (Self::UID_DESC)
                        },
                        "ancestor_depth": {
                            "type": "integer",
                            "description": "Maximum ancestor levels to include (default: 3, max: 8)"
                        },
                        "sibling_limit": {
                            "type": "integer",
                            "description": "Number of preceding/following siblings to include around the element (default: 2, max: 10)"
                        },
                        "child_limit": {
                            "type": "integer",
                            "description": "Maximum direct children to summarize (default: 8, max: 50)"
                        },
                        "max_chars": {
                            "type": "integer",
                            "description": "Maximum text characters per summarized node (default: 240, range: 40-1000)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_evaluate_script",
                "Evaluate a JavaScript function in the selected browser page. Arbitrary script execution may mutate page or external state and is approval-gated by clients; prefer cdp_find_elements/cdp_take_dom_snapshot for discovery and typed cdp_* action tools for UI changes. Returns the response as JSON. Example without arguments: '() => document.title'. Example with element arguments: pass UIDs from cdp_take_dom_snapshot or cdp_find_elements via args to reference DOM elements, e.g., '(el) => el.innerText' with args=[{uid: 'd5'}].",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["function"],
                    "properties": {
                        "function": {
                            "type": "string",
                            "description": "JavaScript function to evaluate (e.g., '() => document.title' or '(el) => el.innerText')"
                        },
                        "args": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "uid": { "type": "string", "description": (Self::UID_DESC) }
                                }
                            },
                            "description": "Optional element arguments from snapshot UIDs"
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_click",
                "Click a DOM element by its UID from a cdp_take_dom_snapshot or cdp_find_elements result. Scrolls the element into view automatically and clicks its center. More reliable than coordinate-based clicking for web content.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["uid"],
                    "properties": {
                        "uid": {
                            "type": "string",
                            "description": (Self::UID_DESC)
                        },
                        "dbl_click": {
                            "type": "boolean",
                            "description": "Double-click instead of single click (default: false)"
                        },
                        "include_snapshot": {
                            "type": "boolean",
                            "description": "Appends a DOM snapshot (d-prefixed UIDs) to the response (default: false)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_list_pages",
                "List all open pages (tabs) in the connected browser. Returns page indices and URLs. The currently selected page is marked with *. Use cdp_select_page to switch between pages.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {}
                }))),
            ),
            Tool::new(
                "cdp_select_page",
                "Select a browser page (tab) by index as context for subsequent CDP operations. Call cdp_list_pages first to see available pages and their indices.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["page_idx"],
                    "properties": {
                        "page_idx": {
                            "type": "integer",
                            "description": "Page index from cdp_list_pages"
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_hover",
                "Hover over the provided element.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["uid"],
                    "properties": {
                        "uid": {
                            "type": "string",
                            "description": (Self::UID_DESC)
                        },
                        "include_snapshot": {
                            "type": "boolean",
                            "description": "Appends a DOM snapshot (d-prefixed UIDs) to the response (default: false)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_fill",
                "Type text into an input, text area, rich contenteditable editor, or select an option from a <select> element. Rich editors are focused inside the page and receive CDP keyboard insertion so framework state updates.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["uid", "value"],
                    "properties": {
                        "uid": {
                            "type": "string",
                            "description": (Self::UID_DESC)
                        },
                        "value": {
                            "type": "string",
                            "description": "The value to fill in"
                        },
                        "include_snapshot": {
                            "type": "boolean",
                            "description": "Appends a DOM snapshot (d-prefixed UIDs) to the response (default: false)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_press_key",
                "Press a key or key combination. Use this when other input methods like cdp_fill cannot be used (e.g., keyboard shortcuts, navigation keys, or special key combinations).",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["key"],
                    "properties": {
                        "key": {
                            "type": "string",
                            "description": "A key or combination (e.g., 'Enter', 'Control+A', 'Control++', 'Control+Shift+R'). Modifiers: Control, Shift, Alt, Meta"
                        },
                        "include_snapshot": {
                            "type": "boolean",
                            "description": "Appends a DOM snapshot (d-prefixed UIDs) to the response (default: false)"
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_handle_dialog",
                "If a browser dialog was opened, use this command to handle it.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["action"],
                    "properties": {
                        "action": {
                            "type": "string",
                            "enum": ["accept", "dismiss"],
                            "description": "Whether to accept or dismiss the dialog"
                        },
                        "prompt_text": {
                            "type": "string",
                            "description": "Optional text to enter into a prompt dialog before accepting"
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_navigate",
                "Navigate the currently selected page to a URL, or go back, forward, or reload.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "url": {
                            "type": "string",
                            "description": "Target URL (required when type is 'url')"
                        },
                        "type": {
                            "type": "string",
                            "enum": ["url", "back", "forward", "reload"],
                            "description": "Navigation type. Default: 'url'"
                        },
                        "timeout": {
                            "type": "integer",
                            "description": "Maximum wait time in milliseconds for page load (default: 10000). If the page takes longer, navigation is assumed successful."
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_new_page",
                "Create a new page (tab) and navigate it to the given URL. The new page becomes the selected page.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["url"],
                    "properties": {
                        "url": {
                            "type": "string",
                            "description": "URL to load in the new page"
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_close_page",
                "Close a page (tab) by its index. The last open page cannot be closed.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["page_idx"],
                    "properties": {
                        "page_idx": {
                            "type": "integer",
                            "description": "The index of the page to close. Call cdp_list_pages to list pages."
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_wait_for",
                "Wait for the specified text to appear on the selected page. Resolves when any value appears.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["text"],
                    "properties": {
                        "text": {
                            "type": "array",
                            "items": { "type": "string" },
                            "minItems": 1,
                            "description": "Non-empty list of texts. Resolves when any value appears on the page."
                        },
                        "timeout": {
                            "type": "integer",
                            "description": "Maximum wait time in milliseconds (default: 10000)"
                        },
                        "include_snapshot": {
                            "type": "boolean",
                            "description": "Appends a DOM snapshot (d-prefixed UIDs) to the response after the text appears (default: false). When false, only a short 'text appeared after Xms' line is returned."
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_wait_for_page_change",
                "Wait until the selected page or a scoped DOM element has a semantic visible-text/editor-value change. Use this for unknown incoming messages, replies, notifications, or page updates after you have chosen the best scope_uid with cdp_find_elements/cdp_get_element_context. This blocks inside one tool call and returns compact before/after deltas.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "properties": {
                        "scope_uid": {
                            "type": "string",
                            "description": "Optional d-prefixed uid to watch. Prefer a stable container such as a message list, inbox list, thread, panel, or editor. Omit only when a page-level wait is intentional."
                        },
                        "condition": {
                            "type": "string",
                            "description": "Coarse condition label for the wait, e.g. semantic_delta, new_visible_text, item_count_changed. Currently evaluated as semantic_delta and echoed in the result."
                        },
                        "goal": {
                            "type": "string",
                            "description": "Short natural-language goal to echo in the result so the next model turn can judge relevance, e.g. new incoming message in Note to Self."
                        },
                        "timeout": {
                            "type": "integer",
                            "description": "Maximum wait time in milliseconds (default: 55000, capped at 55000 to stay within the Clickweave MCP client timeout)"
                        },
                        "poll_interval_ms": {
                            "type": "integer",
                            "description": "Backup polling interval in milliseconds for changes MutationObserver cannot see (default: 500, clamped to 100-5000)"
                        },
                        "stable_ms": {
                            "type": "integer",
                            "description": "Debounce/stability window after a wake-up before comparing semantic text (default: 500, clamped to 100-2000)"
                        },
                        "include_snapshot": {
                            "type": "boolean",
                            "description": "Appends a compact DOM snapshot after the wait returns (default: false). The normal response already includes before/after text tails and deltas."
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_type_text",
                "Type text using keyboard into a previously focused input. Use cdp_fill for form fields; use this for character-by-character keyboard input.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["text"],
                    "properties": {
                        "text": {
                            "type": "string",
                            "description": "The text to type"
                        },
                        "submit_key": {
                            "type": "string",
                            "description": "Optional key to press after typing (e.g., 'Enter', 'Tab', 'Escape')"
                        }
                    }
                }))),
            ),
            Tool::new(
                "cdp_element_at_point",
                "Given screen coordinates (x, y) in points, hit-test the DOM element at that \
                 position. Always returns the element's backend_node_id. If a current DOM \
                 snapshot already contains that node, the response also includes its d-prefixed \
                 UID, role, and name; otherwise uid is null and a note points at \
                 cdp_take_dom_snapshot / cdp_find_elements. Requires an active CDP connection. \
                 Coordinates use the same screen-point system as element_at_point and click.",
                Arc::new(json_to_object(serde_json::json!({
                    "type": "object",
                    "required": ["x", "y"],
                    "properties": {
                        "x": {
                            "type": "number",
                            "description": "Screen X coordinate in points"
                        },
                        "y": {
                            "type": "number",
                            "description": "Screen Y coordinate in points"
                        }
                    }
                }))),
            ),
        ]
    }
}

impl ServerHandler for MacOSDevToolsServer {
    fn get_info(&self) -> ServerInfo {
        let mut instructions = String::from(
            "Native DevTools MCP server for automating desktop apps (macOS/Windows) and Android devices.\n\n\
             WHICH TOOLS TO USE:\n\
             - Desktop apps (coordinate-based, cross-platform): no prefix (click, find_text, take_screenshot, type_text, etc.). Moves the cursor; steals focus.\n",
        );

        #[cfg(target_os = "macos")]
        {
            instructions.push_str(
                "- Desktop apps (element-precise, macOS only): ax_* (ax_click, ax_set_value, ax_select) — focus-preserving dispatch against uids from take_ax_snapshot.\n",
            );
        }

        instructions.push_str(
            "- Android devices: android_* (android_click, android_find_text, etc.)\n\
             - App debug protocol: app_* — only when given a WebSocket URL to connect to.\n\
             NEVER mix these — desktop tools do not work on Android and vice versa.\n\n\
             == DESKTOP (macOS/Windows) ==\n\n\
             CLICKING BY TEXT (PREFERRED): Use find_text to locate UI elements by name, \
             then click at the returned coordinates.\n\
             Example: find_text(text='Submit') → click(x=..., y=...).\n\n\
             CLICKING BY VISUAL POSITION: Use take_screenshot with include_ocr=true. \
             The OCR results include screen coordinates you can click directly. \
             For positions not covered by OCR, use the screenshot metadata \
             (origin_x, origin_y, scale) to convert pixel positions.\n\n\
             Always call focus_window before clicking to ensure the target window receives input.\n\n\
             Screenshot best practice: Use take_screenshot with app_name (e.g., app_name='Code') \
             to capture a specific window. Avoid mode='screen' unless you need to see multiple windows.\n\n",
        );

        #[cfg(target_os = "macos")]
        {
            instructions.push_str(
                "ELEMENT-PRECISE AUTOMATION (macOS, PREFERRED for native apps): \
                 Call take_ax_snapshot(app_name='...') to get a tree of elements tagged \
                 with generation-stamped uids like 'a42g3'. Then pick the dispatch \
                 primitive that matches the target: ax_click(uid) dispatches AXPress for \
                 buttons, menu items, and anything pressable; ax_set_value(uid, text) \
                 writes kAXValueAttribute on text fields; ax_select(uid) writes \
                 AXSelectedRows for NSOutlineView / NSTableView row selection (sidebars, \
                 rule lists, file browsers) — rows typically refuse AXPress so ax_click \
                 returns not_dispatchable or AX error -25205 against them. IMPORTANT: any \
                 fresh take_ax_snapshot invalidates all prior uids — snapshot immediately \
                 before each ax_click / ax_set_value / ax_select call. ax_set_value is \
                 value assignment, not keystrokes: no IME, no undo-stack entry. If a call \
                 fails with not_dispatchable and returns a fallback {x, y}, retry via \
                 click(x, y) (plus type_text(text) for ax_set_value).\n\n",
            );
        }

        instructions.push_str(
            "App debug protocol (app_* tools): For element-level precision in apps with an embedded \
             debug server. Use app_connect with a WebSocket URL first, then app_click, app_type, etc.\n\n\
             == ANDROID ==\n\n\
             All Android tools require connecting to a device first:\n\
             1. android_list_devices — find available devices and their serial numbers\n\
             2. android_connect(serial='...') — connect (this unlocks all other android_* tools)\n\
             To switch devices, call android_disconnect first, then android_connect to the new device.\n\n\
             CLICKING BY TEXT (PREFERRED): Use android_find_text to search the accessibility tree, \
             then android_click at the returned coordinates.\n\
             Example: android_find_text(text='Settings') → android_click(x=..., y=...).\n\n\
             CLICKING BY VISUAL POSITION: Use android_screenshot to see the screen, \
             then android_click at the desired coordinates.\n\
             Note: android_screenshot has no OCR — always prefer android_find_text for text elements.\n\n\
             Android coordinates are absolute screen pixels — no scale conversion needed.\n\
             Use android_press_key with Android keycodes (e.g., 'KEYCODE_BACK', 'KEYCODE_HOME').",
        );

        // `ServerInfo` and `Implementation` are `#[non_exhaustive]` in rmcp 1.x,
        // so they can't be built with struct literals from outside the crate.
        // Start from the defaults and override the fields we care about.
        let mut info = ServerInfo::default();
        info.protocol_version = ProtocolVersion::V_2024_11_05;
        info.capabilities = ServerCapabilities::builder()
            .enable_tools()
            .enable_tool_list_changed()
            .build();
        info.server_info.name = "native-devtools-mcp".to_string();
        info.server_info.version = env!("CARGO_PKG_VERSION").to_string();
        info.instructions = Some(instructions);
        info
    }

    async fn list_tools(
        &self,
        _request: Option<PaginatedRequestParam>,
        _context: RequestContext<RoleServer>,
    ) -> Result<ListToolsResult, McpError> {
        let connected = self.is_connected().await;
        #[cfg(feature = "cdp")]
        let cdp_connected = self.is_cdp_connected().await;
        #[cfg(not(feature = "cdp"))]
        let cdp_connected = false;
        Ok(ListToolsResult {
            tools: Self::get_tools(
                connected,
                self.is_android_connected().await,
                cdp_connected,
                self.is_hover_tracking().await,
                self.is_recording().await,
            ),
            next_cursor: None,
            ..Default::default()
        })
    }

    async fn call_tool(
        &self,
        request: CallToolRequestParam,
        context: RequestContext<RoleServer>,
    ) -> Result<CallToolResult, McpError> {
        let args = request
            .arguments
            .map(Value::Object)
            .unwrap_or(Value::Object(Default::default()));

        match request.name.as_ref() {
            "take_screenshot" => {
                let params: screenshot::TakeScreenshotParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(screenshot::take_screenshot(params, Some(self.screenshot_cache.clone())).await)
            }
            "list_windows" => {
                let params: navigation::ListWindowsParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(navigation::list_windows(params))
            }
            "list_apps" => {
                let params: navigation::ListAppsParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(navigation::list_apps(params))
            }
            "focus_window" => {
                let params: navigation::FocusWindowParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(navigation::focus_window(params))
            }
            "launch_app" => {
                let params: navigation::LaunchAppParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(navigation::launch_app(params))
            }
            "quit_app" => {
                let params: navigation::QuitAppParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(navigation::quit_app(params))
            }
            // App Debug Protocol tools
            "app_connect" => {
                let params: app_tools::AppConnectParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                let result =
                    app_tools::app_connect(params, self.app_client.clone(), context.peer).await;
                Ok(result)
            }
            "app_disconnect" => {
                let result = app_tools::app_disconnect(self.app_client.clone(), context.peer).await;
                Ok(result)
            }
            "app_get_info" => Ok(app_tools::app_get_info(self.app_client.clone()).await),
            "app_get_tree" => {
                let params: app_tools::AppGetTreeParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(app_tools::app_get_tree(params, self.app_client.clone()).await)
            }
            "app_query" => {
                let params: app_tools::AppQueryParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(app_tools::app_query(params, self.app_client.clone()).await)
            }
            "app_get_element" => {
                let params: app_tools::AppGetElementParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(app_tools::app_get_element(params, self.app_client.clone()).await)
            }
            "app_click" => {
                let params: app_tools::AppClickParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(app_tools::app_click(params, self.app_client.clone()).await)
            }
            "app_type" => {
                let params: app_tools::AppTypeParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(app_tools::app_type(params, self.app_client.clone()).await)
            }
            "app_press_key" => {
                let params: app_tools::AppPressKeyParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(app_tools::app_press_key(params, self.app_client.clone()).await)
            }
            "app_focus" => {
                let params: app_tools::AppFocusParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(app_tools::app_focus(params, self.app_client.clone()).await)
            }
            "app_screenshot" => {
                let params: app_tools::AppScreenshotParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(app_tools::app_screenshot(params, self.app_client.clone()).await)
            }
            "app_list_windows" => Ok(app_tools::app_list_windows(self.app_client.clone()).await),
            "app_focus_window" => {
                let params: app_tools::AppFocusWindowParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(app_tools::app_focus_window(params, self.app_client.clone()).await)
            }
            // System-level input tools
            "click" => {
                let params: input_tools::ClickParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(input_tools::click(params).await)
            }
            "move_mouse" => {
                let params: input_tools::MoveMouseParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(input_tools::move_mouse(params).await)
            }
            "drag" => {
                let params: input_tools::DragParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(input_tools::drag(params).await)
            }
            "scroll" => {
                let params: input_tools::ScrollParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(input_tools::scroll(params).await)
            }
            "type_text" => {
                let params: input_tools::TypeTextParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(input_tools::type_text(params).await)
            }
            "press_key" => {
                let params: input_tools::PressKeyParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(input_tools::press_key(params).await)
            }
            "get_displays" => {
                let params: input_tools::GetDisplaysParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(input_tools::get_displays(params))
            }
            "find_text" => {
                let params: input_tools::FindTextParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(input_tools::find_text(params))
            }
            "element_at_point" => {
                let params: input_tools::ElementAtPointParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(input_tools::element_at_point(params))
            }
            "find_image" => {
                let params: find_image::FindImageParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(find_image::find_image(
                    params,
                    self.screenshot_cache.clone(),
                    self.image_cache.clone(),
                )
                .await)
            }
            "load_image" => {
                let params: load_image::LoadImageParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(load_image::load_image(params, self.image_cache.clone()).await)
            }
            "take_ax_snapshot" => {
                let params: crate::tools::ax_snapshot::TakeAxSnapshotParams =
                    serde_json::from_value(args)
                        .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                #[cfg(target_os = "macos")]
                {
                    // Native macOS path: walk the AX tree, capture retained
                    // AXRef handles, swap them into the session (generation
                    // bump), format with the assigned generation so uids are
                    // stamped `a<N>g<gen>`.
                    let (nodes, refs) =
                        match crate::macos::ax::collect_ax_tree_indexed(params.app_name.as_deref())
                        {
                            Ok(v) => v,
                            Err(e) => return Ok(CallToolResult::error(vec![Content::text(e)])),
                        };
                    let generation = self.ax_session.create_snapshot(refs).await;
                    let snapshot =
                        crate::tools::ax_snapshot::format_snapshot(&nodes, Some(generation));
                    Ok(CallToolResult::success(vec![Content::text(snapshot)]))
                }
                #[cfg(not(target_os = "macos"))]
                {
                    // Windows UIA path: unchanged — no session, uids stay bare `a<N>`.
                    match crate::tools::ax_snapshot::take_ax_snapshot(params) {
                        Ok(snapshot) => Ok(CallToolResult::success(vec![Content::text(snapshot)])),
                        Err(e) => Ok(CallToolResult::error(vec![Content::text(e)])),
                    }
                }
            }
            #[cfg(target_os = "macos")]
            "ax_click" => {
                let params: crate::tools::ax_click::AxClickParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(crate::tools::ax_click::ax_click(params, self.ax_session.clone()).await)
            }
            #[cfg(target_os = "macos")]
            "ax_set_value" => {
                let params: crate::tools::ax_set_value::AxSetValueParams =
                    serde_json::from_value(args)
                        .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(crate::tools::ax_set_value::ax_set_value(params, self.ax_session.clone()).await)
            }
            #[cfg(target_os = "macos")]
            "ax_select" => {
                let params: crate::tools::ax_select::AxSelectParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(crate::tools::ax_select::ax_select(params, self.ax_session.clone()).await)
            }
            "probe_app" => {
                let params: crate::tools::probe_app::ProbeAppParams = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(crate::tools::probe_app::probe_app(params))
            }
            // Android tools
            "android_list_devices" => match crate::android::device::list_devices() {
                Ok(devices) => Ok(CallToolResult::success(vec![Content::text(
                    to_json_pretty(&devices),
                )])),
                Err(e) => Ok(CallToolResult::error(vec![Content::text(e)])),
            },
            "android_connect" => {
                let serial = parse_string_field(&args, "serial")?;
                match AndroidDevice::connect(&serial) {
                    Ok(device) => {
                        let msg = format!(
                            "Connected to Android device '{}'. Android tools (android_*) are now available.",
                            device.serial
                        );
                        *self.android_device.write().await = Some(device);
                        let _ = context.peer.notify_tool_list_changed().await;
                        Ok(CallToolResult::success(vec![Content::text(msg)]))
                    }
                    Err(e) => Ok(CallToolResult::error(vec![Content::text(e)])),
                }
            }
            "android_disconnect" => {
                if self.android_device.write().await.take().is_some() {
                    let _ = context.peer.notify_tool_list_changed().await;
                    Ok(CallToolResult::success(vec![Content::text(
                        "Disconnected from Android device. Android tools (android_*) are no longer available.",
                    )]))
                } else {
                    Ok(CallToolResult::error(vec![Content::text(
                        "No Android device connected.",
                    )]))
                }
            }
            "android_screenshot" => {
                let file_path = args
                    .get("file_path")
                    .and_then(|v| v.as_str())
                    .map(String::from);
                Ok(self
                    .with_android_device(|device| {
                        let shot = match crate::android::screenshot::capture(device) {
                            Ok(s) => s,
                            Err(e) => return CallToolResult::error(vec![Content::text(e)]),
                        };
                        if let Some(ref path) = file_path {
                            return match std::fs::write(path, &shot.png_data) {
                                Ok(()) => CallToolResult::success(vec![Content::text(format!(
                                    "Screenshot saved to {} ({}x{})",
                                    path, shot.width, shot.height
                                ))]),
                                Err(e) => CallToolResult::error(vec![Content::text(format!(
                                    "Failed to save screenshot: {}",
                                    e
                                ))]),
                            };
                        }
                        let (image_data, mime_type) = match screenshot::png_to_jpeg(&shot.png_data)
                        {
                            Ok(jpeg_data) => (jpeg_data, "image/jpeg"),
                            Err(e) => {
                                tracing::warn!("JPEG conversion failed, using PNG: {}", e);
                                (shot.png_data, "image/png")
                            }
                        };
                        let base64_data =
                            base64::engine::general_purpose::STANDARD.encode(&image_data);
                        let mut contents = vec![Content::image(base64_data, mime_type)];
                        contents.push(Content::text(to_json_pretty(&serde_json::json!({
                            "width": shot.width,
                            "height": shot.height,
                            "scale": 1.0,
                            "device": device.serial,
                        }))));
                        CallToolResult::success(contents)
                    })
                    .await)
            }
            "android_click" => {
                let (x, y) = parse_xy(&args)?;
                Ok(self
                    .with_android_device(|device| {
                        match crate::android::input::click(device, x, y) {
                            Ok(()) => CallToolResult::success(vec![Content::text(format!(
                                "Tapped at ({:.0}, {:.0})",
                                x, y
                            ))]),
                            Err(e) => CallToolResult::error(vec![Content::text(e)]),
                        }
                    })
                    .await)
            }
            "android_swipe" => {
                #[derive(serde::Deserialize)]
                struct Params {
                    start_x: f64,
                    start_y: f64,
                    end_x: f64,
                    end_y: f64,
                    duration_ms: Option<u32>,
                }
                let p: Params = serde_json::from_value(args)
                    .map_err(|e| McpError::invalid_params(e.to_string(), None))?;
                Ok(self
                    .with_android_device(|device| {
                        match crate::android::input::swipe(
                            device,
                            p.start_x,
                            p.start_y,
                            p.end_x,
                            p.end_y,
                            p.duration_ms,
                        ) {
                            Ok(()) => CallToolResult::success(vec![Content::text(format!(
                                "Swiped from ({:.0}, {:.0}) to ({:.0}, {:.0})",
                                p.start_x, p.start_y, p.end_x, p.end_y
                            ))]),
                            Err(e) => CallToolResult::error(vec![Content::text(e)]),
                        }
                    })
                    .await)
            }
            "android_type_text" => {
                let text = parse_string_field(&args, "text")?;
                let len = text.len();
                Ok(self
                    .with_android_device(|device| {
                        match crate::android::input::type_text(device, &text) {
                            Ok(()) => CallToolResult::success(vec![Content::text(format!(
                                "Typed {} characters",
                                len
                            ))]),
                            Err(e) => CallToolResult::error(vec![Content::text(e)]),
                        }
                    })
                    .await)
            }
            "android_press_key" => {
                let key = parse_string_field(&args, "key")?;
                Ok(self
                    .with_android_device(|device| {
                        match crate::android::input::press_key(device, &key) {
                            Ok(()) => CallToolResult::success(vec![Content::text(format!(
                                "Pressed key: {}",
                                key
                            ))]),
                            Err(e) => CallToolResult::error(vec![Content::text(e)]),
                        }
                    })
                    .await)
            }
            "android_find_text" => {
                let text = parse_string_field(&args, "text")?;
                Ok(self
                    .with_android_device(|device| {
                        match crate::android::ui_automator::find_text(device, &text) {
                            Ok(result) => {
                                let mut content =
                                    vec![Content::text(to_json_pretty(&result.matches))];
                                if result.matches.is_empty() {
                                    content.push(Content::text(
                                        input_tools::build_no_matches_hint(
                                            &text,
                                            &result.available_elements,
                                        ),
                                    ));
                                }
                                CallToolResult::success(content)
                            }
                            Err(e) => CallToolResult::error(vec![Content::text(e)]),
                        }
                    })
                    .await)
            }
            "android_list_apps" => {
                let user_apps_only = args
                    .get("user_apps_only")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false);
                Ok(self
                    .with_android_device(|device| {
                        match crate::android::navigation::list_apps(device, user_apps_only) {
                            Ok(apps) => {
                                CallToolResult::success(vec![Content::text(to_json_pretty(&apps))])
                            }
                            Err(e) => CallToolResult::error(vec![Content::text(e)]),
                        }
                    })
                    .await)
            }
            "android_launch_app" => {
                let package_name = parse_string_field(&args, "package_name")?;
                Ok(self
                    .with_android_device(|device| {
                        match crate::android::navigation::launch_app(device, &package_name) {
                            Ok(()) => CallToolResult::success(vec![Content::text(format!(
                                "Launched {}",
                                package_name
                            ))]),
                            Err(e) => CallToolResult::error(vec![Content::text(e)]),
                        }
                    })
                    .await)
            }
            "android_get_display_info" => Ok(self
                .with_android_device(|device| {
                    match crate::android::navigation::get_display_info(device) {
                        Ok(info) => {
                            CallToolResult::success(vec![Content::text(to_json_pretty(&info))])
                        }
                        Err(e) => CallToolResult::error(vec![Content::text(e)]),
                    }
                })
                .await),
            "android_get_current_activity" => Ok(self
                .with_android_device(
                    |device| match crate::android::navigation::get_current_activity(device) {
                        Ok(activity) => {
                            CallToolResult::success(vec![Content::text(to_json_pretty(&activity))])
                        }
                        Err(e) => CallToolResult::error(vec![Content::text(e)]),
                    },
                )
                .await),
            "start_hover_tracking" => {
                // Auto-clean finished tracker (e.g. from max duration timeout)
                let already_active = {
                    let guard = self.hover_tracker.read().await;
                    match guard.as_ref() {
                        Some(t) if t.is_finished() => false, // will clean up below
                        Some(_) => true,
                        None => false,
                    }
                };
                if already_active {
                    return Ok(CallToolResult::error(vec![Content::text(
                        "Hover tracking is already active. Use stop_hover_tracking to end the current session first.",
                    )]));
                }
                // Clean up any finished tracker before starting a new one
                if self.hover_tracker.read().await.is_some() {
                    self.hover_tracker.write().await.take();
                }

                let app_name = args
                    .get("app_name")
                    .and_then(|v| v.as_str())
                    .map(String::from);
                let poll_interval_ms = args
                    .get("poll_interval_ms")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(100)
                    .clamp(10, 10_000) as u32;
                let max_duration_ms = args
                    .get("max_duration_ms")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(60000)
                    .clamp(100, u32::MAX as u64) as u32;
                let min_dwell_ms = args
                    .get("min_dwell_ms")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(300)
                    .clamp(0, 10_000) as u32;

                let events = Arc::new(std::sync::Mutex::new(Vec::new()));
                let cancel = tokio_util::sync::CancellationToken::new();

                let task_handle = crate::tools::hover_tracker::start_polling(
                    events.clone(),
                    cancel.clone(),
                    app_name.clone(),
                    poll_interval_ms,
                    max_duration_ms,
                    min_dwell_ms,
                );

                let tracker =
                    crate::tools::hover_tracker::HoverTracker::new(events, task_handle, cancel);
                *self.hover_tracker.write().await = Some(tracker);
                let _ = context.peer.notify_tool_list_changed().await;

                let msg = format!(
                    "Hover tracking started (poll: {}ms, max: {}ms, dwell: {}ms{}). Use get_hover_events to read transitions, stop_hover_tracking to end.",
                    poll_interval_ms,
                    max_duration_ms,
                    min_dwell_ms,
                    app_name.map_or(String::new(), |a| format!(", app: {}", a)),
                );
                Ok(CallToolResult::success(vec![Content::text(msg)]))
            }
            "get_hover_events" => {
                // Single lock: check auto-stop and drain events together
                let result = {
                    let guard = self.hover_tracker.read().await;
                    guard.as_ref().map(|t| {
                        let auto_stopped = t.is_finished();
                        let events = t.drain_events();
                        (auto_stopped, events)
                    })
                };

                match result {
                    Some((auto_stopped, events)) => {
                        let json = to_json_pretty(&events);

                        if auto_stopped {
                            self.hover_tracker.write().await.take();
                            let _ = context.peer.notify_tool_list_changed().await;
                        }

                        // Always return the JSON array for consistent parsing.
                        // The timeout sentinel event (with timeout: true) signals
                        // auto-stop within the event stream itself.
                        Ok(CallToolResult::success(vec![Content::text(json)]))
                    }
                    None => Ok(CallToolResult::error(vec![Content::text(
                        "No hover tracking session is active. Use start_hover_tracking first.",
                    )])),
                }
            }
            "stop_hover_tracking" => {
                let tracker = self.hover_tracker.write().await.take();
                match tracker {
                    Some(tracker) => {
                        let events = tracker.cancel_and_drain().await;
                        let _ = context.peer.notify_tool_list_changed().await;
                        // Return raw JSON array for consistent parsing with get_hover_events
                        Ok(CallToolResult::success(vec![Content::text(
                            to_json_pretty(&events),
                        )]))
                    }
                    None => Ok(CallToolResult::error(vec![Content::text(
                        "No hover tracking session is active.",
                    )])),
                }
            }
            "start_recording" => {
                // Check if a previous recording auto-stopped (max_duration elapsed).
                // If so, drain remaining frames (log count) and clear stale state.
                {
                    let guard = self.screen_recorder.read().await;
                    if let Some(recorder) = guard.as_ref() {
                        if recorder.is_finished() {
                            let stale_count = recorder.drain_frames().len();
                            if stale_count > 0 {
                                tracing::warn!(
                                    "Discarding {stale_count} frames from auto-stopped recording \
                                     (stop_recording was not called)"
                                );
                            }
                            drop(guard);
                            self.screen_recorder.write().await.take();
                            let _ = context.peer.notify_tool_list_changed().await;
                        } else {
                            return Ok(CallToolResult::error(vec![Content::text(
                                "Recording is already active. Use stop_recording to end the current session first.",
                            )]));
                        }
                    }
                }

                let output_dir = parse_string_field(&args, "output_dir")?;
                let fps = args.get("fps").and_then(|v| v.as_u64()).unwrap_or(5) as u32;
                let max_duration_ms = args
                    .get("max_duration_ms")
                    .and_then(|v| v.as_u64())
                    .unwrap_or(60_000)
                    .clamp(1_000, u32::MAX as u64) as u32;

                let output_path = std::path::PathBuf::from(&output_dir);
                if let Err(e) = std::fs::create_dir_all(&output_path) {
                    return Ok(CallToolResult::error(vec![Content::text(format!(
                        "Failed to create output directory: {e}"
                    ))]));
                }
                // Probe-write to fail fast if the directory is not writable.
                match tempfile::tempfile_in(&output_path) {
                    Ok(_) => {} // drops and deletes automatically
                    Err(e) => {
                        return Ok(CallToolResult::error(vec![Content::text(format!(
                            "Output directory is not writable: {e}"
                        ))]));
                    }
                }

                let frames = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
                let cancel = tokio_util::sync::CancellationToken::new();

                let task_handle = crate::tools::screen_recorder::start_recording(
                    frames.clone(),
                    cancel.clone(),
                    output_path,
                    fps,
                    max_duration_ms,
                );

                let recorder =
                    crate::tools::screen_recorder::ScreenRecorder::new(frames, task_handle, cancel);
                *self.screen_recorder.write().await = Some(recorder);
                let _ = context.peer.notify_tool_list_changed().await;

                Ok(CallToolResult::success(vec![Content::text(format!(
                    "Recording started ({fps}fps, max: {max_duration_ms}ms, dir: {output_dir}). Use stop_recording to end.",
                ))]))
            }
            "stop_recording" => {
                let recorder = self.screen_recorder.write().await.take();
                match recorder {
                    Some(recorder) => {
                        let frames = recorder.cancel_and_drain().await;
                        let _ = context.peer.notify_tool_list_changed().await;
                        Ok(CallToolResult::success(vec![Content::text(
                            to_json_pretty(&frames),
                        )]))
                    }
                    None => Ok(CallToolResult::error(vec![Content::text(
                        "No recording session is active.",
                    )])),
                }
            }
            #[cfg(feature = "cdp")]
            "cdp_connect" => {
                let port_num = args.get("port").and_then(|v| v.as_u64()).ok_or_else(|| {
                    McpError::invalid_params("missing required param: port", None)
                })?;
                if port_num > 65535 {
                    return Ok(CallToolResult::error(vec![Content::text(format!(
                        "Invalid port: {}. Port must be 0-65535.",
                        port_num
                    ))]));
                }
                let port = port_num as u16;
                match crate::cdp::CdpClient::connect(port).await {
                    Ok(client) => {
                        let page_info = if let Some(page) = client.selected_page.as_ref() {
                            let url = crate::cdp::page_url(page).await;
                            format!("Selected page: {}", url)
                        } else {
                            "No pages found".to_string()
                        };
                        *self.cdp_client.write().await = Some(client);
                        // Tool list does not change on CDP connect/disconnect — CDP
                        // tools are always listed so prompt caches remain stable.
                        Ok(CallToolResult::success(vec![Content::text(format!(
                            "Connected to Chrome/Electron on port {}. CDP tool calls will now succeed.\n{}",
                            port, page_info
                        ))]))
                    }
                    Err(e) => Ok(CallToolResult::error(vec![Content::text(e)])),
                }
            }
            #[cfg(feature = "cdp")]
            "cdp_disconnect" => {
                if let Some(client) = self.cdp_client.write().await.take() {
                    client.disconnect();
                    // Tool list is unchanged on disconnect — CDP tools remain
                    // listed and will return "not connected" errors until
                    // cdp_connect succeeds again.
                    Ok(CallToolResult::success(vec![Content::text(
                        "Disconnected from Chrome/Electron. CDP tool calls will return a 'not connected' error until cdp_connect is called again.",
                    )]))
                } else {
                    // Use the canonical "not connected" message shared by every
                    // CDP tool handler so clients see one stable error shape.
                    Ok(CallToolResult::error(vec![Content::text(
                        "No CDP connection. Use cdp_connect first.",
                    )]))
                }
            }
            #[cfg(feature = "cdp")]
            "cdp_take_dom_snapshot" => {
                let max_nodes = args
                    .get("max_nodes")
                    .and_then(|v| v.as_u64())
                    .map(|n| n as u32);
                Ok(
                    crate::cdp::tools::cdp_take_dom_snapshot(max_nodes, self.cdp_client.clone())
                        .await,
                )
            }
            #[cfg(feature = "cdp")]
            "cdp_summarize_page" => {
                Ok(crate::cdp::tools::cdp_summarize_page(self.cdp_client.clone()).await)
            }
            #[cfg(feature = "cdp")]
            "cdp_find_elements" => {
                let query = parse_string_field(&args, "query")?;
                let role = args.get("role").and_then(|v| v.as_str()).map(String::from);
                let max_results = args
                    .get("max_results")
                    .and_then(|v| v.as_u64())
                    .map(|n| n as u32);
                Ok(crate::cdp::tools::cdp_find_elements(
                    query,
                    role,
                    max_results,
                    self.cdp_client.clone(),
                )
                .await)
            }
            #[cfg(feature = "cdp")]
            "cdp_get_element_context" => {
                let uid = parse_string_field(&args, "uid")?;
                let ancestor_depth = args
                    .get("ancestor_depth")
                    .and_then(|v| v.as_u64())
                    .map(|n| n as u32);
                let sibling_limit = args
                    .get("sibling_limit")
                    .and_then(|v| v.as_u64())
                    .map(|n| n as u32);
                let child_limit = args
                    .get("child_limit")
                    .and_then(|v| v.as_u64())
                    .map(|n| n as u32);
                let max_chars = args
                    .get("max_chars")
                    .and_then(|v| v.as_u64())
                    .map(|n| n as u32);
                Ok(crate::cdp::tools::cdp_get_element_context(
                    uid,
                    ancestor_depth,
                    sibling_limit,
                    child_limit,
                    max_chars,
                    self.cdp_client.clone(),
                )
                .await)
            }
            #[cfg(feature = "cdp")]
            "cdp_evaluate_script" => {
                let function = parse_string_field(&args, "function")?;
                let script_args = args.get("args").and_then(|v| v.as_array()).cloned();
                Ok(crate::cdp::tools::cdp_evaluate_script(
                    function,
                    script_args,
                    self.cdp_client.clone(),
                )
                .await)
            }
            #[cfg(feature = "cdp")]
            "cdp_click" => {
                let uid = parse_string_field(&args, "uid")?;
                let dbl_click = args
                    .get("dbl_click")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false);
                let include_snapshot = args
                    .get("include_snapshot")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false);
                Ok(crate::cdp::tools::cdp_click(
                    uid,
                    dbl_click,
                    include_snapshot,
                    self.cdp_client.clone(),
                )
                .await)
            }
            #[cfg(feature = "cdp")]
            "cdp_list_pages" => {
                Ok(crate::cdp::tools::cdp_list_pages(self.cdp_client.clone()).await)
            }
            #[cfg(feature = "cdp")]
            "cdp_select_page" => {
                let page_idx = args
                    .get("page_idx")
                    .and_then(|v| v.as_u64())
                    .map(|p| p as usize)
                    .ok_or_else(|| {
                        McpError::invalid_params("missing required param: page_idx", None)
                    })?;
                Ok(crate::cdp::tools::cdp_select_page(page_idx, self.cdp_client.clone()).await)
            }
            #[cfg(feature = "cdp")]
            "cdp_hover" => {
                let uid = parse_string_field(&args, "uid")?;
                let include_snapshot = args
                    .get("include_snapshot")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false);
                Ok(
                    crate::cdp::tools::cdp_hover(uid, include_snapshot, self.cdp_client.clone())
                        .await,
                )
            }
            #[cfg(feature = "cdp")]
            "cdp_fill" => {
                let uid = parse_string_field(&args, "uid")?;
                let value = parse_string_field(&args, "value")?;
                let include_snapshot = args
                    .get("include_snapshot")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false);
                Ok(crate::cdp::tools::cdp_fill(
                    uid,
                    value,
                    include_snapshot,
                    self.cdp_client.clone(),
                )
                .await)
            }
            #[cfg(feature = "cdp")]
            "cdp_press_key" => {
                let key = parse_string_field(&args, "key")?;
                let include_snapshot = args
                    .get("include_snapshot")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false);
                Ok(
                    crate::cdp::tools::cdp_press_key(
                        key,
                        include_snapshot,
                        self.cdp_client.clone(),
                    )
                    .await,
                )
            }
            #[cfg(feature = "cdp")]
            "cdp_handle_dialog" => {
                let action = parse_string_field(&args, "action")?;
                let prompt_text = args
                    .get("prompt_text")
                    .and_then(|v| v.as_str())
                    .map(String::from);
                Ok(crate::cdp::tools::cdp_handle_dialog(
                    action,
                    prompt_text,
                    self.cdp_client.clone(),
                )
                .await)
            }
            #[cfg(feature = "cdp")]
            "cdp_navigate" => {
                let url = args.get("url").and_then(|v| v.as_str()).map(String::from);
                let nav_type = args.get("type").and_then(|v| v.as_str()).map(String::from);
                let timeout = args.get("timeout").and_then(|v| v.as_u64());
                Ok(
                    crate::cdp::tools::cdp_navigate(
                        url,
                        nav_type,
                        timeout,
                        self.cdp_client.clone(),
                    )
                    .await,
                )
            }
            #[cfg(feature = "cdp")]
            "cdp_new_page" => {
                let url = parse_string_field(&args, "url")?;
                Ok(crate::cdp::tools::cdp_new_page(url, self.cdp_client.clone()).await)
            }
            #[cfg(feature = "cdp")]
            "cdp_close_page" => {
                let page_idx = args
                    .get("page_idx")
                    .and_then(|v| v.as_u64())
                    .map(|p| p as usize)
                    .ok_or_else(|| {
                        McpError::invalid_params("missing required param: page_idx", None)
                    })?;
                Ok(crate::cdp::tools::cdp_close_page(page_idx, self.cdp_client.clone()).await)
            }
            #[cfg(feature = "cdp")]
            "cdp_wait_for" => {
                let texts: Vec<String> = args
                    .get("text")
                    .and_then(|v| v.as_array())
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|v| v.as_str().map(String::from))
                            .collect()
                    })
                    .unwrap_or_default();
                if texts.is_empty() {
                    return Err(McpError::invalid_params(
                        "missing required param: text (array of strings)",
                        None,
                    ));
                }
                let timeout = args.get("timeout").and_then(|v| v.as_u64());
                let include_snapshot = args
                    .get("include_snapshot")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false);
                Ok(crate::cdp::tools::cdp_wait_for(
                    texts,
                    timeout,
                    include_snapshot,
                    self.cdp_client.clone(),
                )
                .await)
            }
            #[cfg(feature = "cdp")]
            "cdp_wait_for_page_change" => {
                let scope_uid = args
                    .get("scope_uid")
                    .and_then(|v| v.as_str())
                    .map(String::from);
                let condition = args
                    .get("condition")
                    .and_then(|v| v.as_str())
                    .map(String::from);
                let goal = args.get("goal").and_then(|v| v.as_str()).map(String::from);
                let timeout = args.get("timeout").and_then(|v| v.as_u64());
                let poll_interval_ms = args.get("poll_interval_ms").and_then(|v| v.as_u64());
                let stable_ms = args.get("stable_ms").and_then(|v| v.as_u64());
                let include_snapshot = args
                    .get("include_snapshot")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false);
                Ok(crate::cdp::tools::cdp_wait_for_page_change(
                    scope_uid,
                    condition,
                    goal,
                    timeout,
                    poll_interval_ms,
                    stable_ms,
                    include_snapshot,
                    self.cdp_client.clone(),
                )
                .await)
            }
            #[cfg(feature = "cdp")]
            "cdp_type_text" => {
                let text = parse_string_field(&args, "text")?;
                let submit_key = args
                    .get("submit_key")
                    .and_then(|v| v.as_str())
                    .map(String::from);
                Ok(
                    crate::cdp::tools::cdp_type_text(text, submit_key, self.cdp_client.clone())
                        .await,
                )
            }
            #[cfg(feature = "cdp")]
            "cdp_element_at_point" => {
                let (x, y) = parse_xy(&args)?;
                Ok(crate::cdp::tools::cdp_element_at_point(x, y, self.cdp_client.clone()).await)
            }
            _ => Err(McpError::invalid_params(
                format!("Unknown tool: {}", request.name),
                None,
            )),
        }
    }
}