mcpls-core 0.6.0

Core library for MCP to LSP protocol translation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
//! MCP server implementation using rmcp.
//!
//! This module provides the MCP server that exposes LSP capabilities
//! as MCP tools using the rmcp SDK.

use std::borrow::Cow;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use rmcp::handler::server::router::tool::ToolRouter;
use rmcp::handler::server::wrapper::{Json, Parameters};
use rmcp::model::{
    ErrorCode, Implementation, ListResourcesResult, ReadResourceRequestParams,
    ReadResourceResponse, ReadResourceResult, Resource, ResourceContents,
    ResourceUpdatedNotificationParam, ServerCapabilities, ServerConfig as RmcpServerConfig,
    SubscribeRequestParams, ToolAnnotations, UnsubscribeRequestParams,
};
use rmcp::{ErrorData as McpError, RoleServer, ServerHandler, tool, tool_handler, tool_router};
use schemars::JsonSchema;
use serde::Serialize;
use tokio::sync::Mutex;

use super::handlers::BridgeContext;
use super::tools::{
    CachedDiagnosticsParams, CallHierarchyCallsParams, CodeActionsParams, CompletionsParams,
    DiagnosticsParams, DocumentSymbolsParams, FormatDocumentParams, InlayHintsParams,
    PositionParams, RangeParams, ReferencesParams, RenameParams, ServerLogsParams,
    ServerMessagesParams, WorkspaceSymbolParams,
};
use crate::bridge::resources::{make_uri, parse_uri};
use crate::bridge::{
    DefinitionResult, DiagnosticInfo, DiagnosticsResult, DocumentSymbolsResult, IndexingState,
    NotificationCache, Position, PositionEncoding, ReferencesResult, SubscriptionRegistry,
    Translator, validate_path_against_roots,
};
use crate::config::{McpConfig, ToolPrefix};

/// Built-in `serverInfo.title`, used when `[mcp].title` is not configured.
const DEFAULT_SERVER_TITLE: &str = "MCPLS - MCP to LSP Bridge";

/// Built-in `serverInfo.description`, used when `[mcp].description` is not
/// configured. Sourced from the crate's own `Cargo.toml` `description` field.
const DEFAULT_SERVER_DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");

/// Built-in `RmcpServerConfig.instructions` capability blurb, used when
/// `[mcp].instructions` is not configured. A configured value replaces this
/// text entirely rather than appending to it -- see [`McpConfig::instructions`].
const DEFAULT_INSTRUCTIONS: &str = concat!(
    "Universal MCP to LSP bridge. Exposes Language Server Protocol ",
    "capabilities as MCP tools for semantic code intelligence. ",
    "Supports hover, definition, references, diagnostics, rename, ",
    "completions, symbols, and formatting."
);

/// Byte length of the longest tool name currently registered
/// (`workspace_symbol_search`), used only to keep
/// [`crate::config::MAX_MCP_TOOL_PREFIX_BYTES`] safe (see the compile-time
/// assertion below). Bumping this when a longer tool name is added is
/// always safe on its own; the assertion is what catches the case where
/// that growth would no longer leave enough room for the configured prefix.
const MAX_TOOL_NAME_BYTES: usize = 23;

/// The stricter of the two ceilings a joined `{prefix}_{tool_name}` must fit
/// under: not rmcp's own 128-byte `SHOULD`-level limit (see the second
/// assertion below), but the tighter pattern some LLM client APIs have
/// historically enforced for tool names (`^[a-zA-Z0-9_-]{1,64}$`). This is
/// the actual binding constraint [`crate::config::MAX_MCP_TOOL_PREFIX_BYTES`]'s
/// doc comment promises to stay under -- asserting against it, not just
/// rmcp's 128, is what makes that promise machine-checked.
const CLIENT_TOOL_NAME_BYTE_LIMIT: usize = 64;

/// Ties [`crate::config::MAX_MCP_TOOL_PREFIX_BYTES`] to the longest
/// currently-registered tool name and the stricter client-side tool name
/// length ceiling ([`CLIENT_TOOL_NAME_BYTE_LIMIT`]). If a future tool name
/// grows past what the current prefix budget allows, this fails to
/// *compile* against `MAX_TOOL_NAME_BYTES`, not silently or at runtime
/// against the user-facing, config-breaking `MAX_MCP_TOOL_PREFIX_BYTES`.
const _: () = assert!(
    crate::config::MAX_MCP_TOOL_PREFIX_BYTES + 1 + MAX_TOOL_NAME_BYTES
        <= CLIENT_TOOL_NAME_BYTE_LIMIT,
    "MAX_TOOL_NAME_BYTES has grown too large for the configured MAX_MCP_TOOL_PREFIX_BYTES \
     budget under the stricter client-side tool name length limit"
);

/// Secondary check against rmcp's own tool name length ceiling (128 bytes,
/// `SHOULD`-level per the MCP spec; see `tool_name_validation.rs` in the
/// pinned rmcp version). Kept alongside the stricter assertion above so a
/// future change to [`CLIENT_TOOL_NAME_BYTE_LIMIT`] can't accidentally drop
/// below what rmcp itself requires.
const _: () = assert!(
    crate::config::MAX_MCP_TOOL_PREFIX_BYTES + 1 + MAX_TOOL_NAME_BYTES <= 128,
    "MAX_TOOL_NAME_BYTES has grown too large for the configured MAX_MCP_TOOL_PREFIX_BYTES \
     budget under rmcp's 128-byte tool name limit"
);

/// Response shape for the `get_cached_diagnostics` tool.
///
/// Wraps `DiagnosticsResult` (shared with the pull-model `get_diagnostics`
/// handler) with a flag specific to the cache-only read: whether the file's
/// *diagnostics-route* server (resolved via
/// `Translator::diagnostics_route_id_for_path` from the file's detected
/// language, independent of the cache entry's own `diagnostics_owner`)
/// crashed and was respawned since it last published. `Translator::respawn_if_dead`
/// discards a respawned server's push notifications rather than reconnecting
/// them to a live `diagnostics_pump` (#359), so a cache entry from before
/// the crash can never be refreshed by a later push -- this makes that
/// degradation visible to the caller instead of only appearing in server
/// logs. Deliberately not keyed on `diagnostics_owner`: a respawn clears
/// that ownership record for the crashed server's entries in the same step
/// that marks it degraded, so a flag derived from ownership would always
/// read back `false` for the exact case it exists to catch.
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct CachedDiagnosticsResponse {
    #[serde(flatten)]
    result: DiagnosticsResult,
    /// `true` if the file's diagnostics-route server's push notifications
    /// are known to be dark (see
    /// [`crate::bridge::NotificationCache::is_push_degraded`]); `false` both
    /// when that server is healthy and when no route is configured for the
    /// file's language at all.
    push_notifications_degraded: bool,
    /// `true` if the file's diagnostics-route server has an active signal
    /// indicating its initial workspace-load/indexing phase is still in
    /// progress as of this read -- the returned diagnostics may reflect a
    /// partial index (#445). `false` both when the server is done indexing
    /// (or never reported a readiness signal) and when no route is
    /// configured for the file's language at all.
    indexing_in_progress: bool,
}

/// MCP server that exposes LSP capabilities as tools.
///
/// Deliberately not `Clone` (#478): each HTTP session must get its own
/// [`ResourceSubscriptions`](crate::bridge::ResourceSubscriptions) set via
/// [`Self::for_new_session`], not a shared instance a stray `.clone()` could
/// hand to two sessions at once. `for_new_session` builds a new value field by
/// field instead (each field an `Arc` bump except `subscriptions`), so this
/// costs nothing at the one production call site (`transport::run_http`'s
/// per-session factory closure).
pub struct McplsServer {
    context: Arc<BridgeContext>,

    /// `Arc`-wrapped so building a new session in `for_new_session` is a
    /// cheap `Arc` bump rather than a deep clone of every registered
    /// `ToolRoute`. `#[tool_handler(router = self.tool_router)]`'s generated
    /// `self.tool_router.call(..)` auto-derefs through the `Arc`, so this is
    /// transparent to the macro-generated code.
    tool_router: Arc<ToolRouter<Self>>,
}

/// Whether `meta` carries rmcp's discover-lifecycle keys -- the same test
/// `tower.rs::is_legacy_request` uses to route a request through its
/// stateless per-request HTTP path instead of a durable session (#482). An
/// attached `Mcp-Session-Id` header proves nothing here: rmcp never reads it
/// on that path, so it must not be trusted as a counter-signal.
#[cfg(feature = "transport-http")]
fn request_uses_discover_lifecycle_meta(meta: &rmcp::model::RequestMetaObject) -> bool {
    meta.missing_required_keys(&rmcp::model::ProtocolVersion::V_2026_07_28)
        .is_empty()
}

/// Whether this HTTP-served request must be rejected as effectively
/// stateless (#482): its `_meta` matches [`request_uses_discover_lifecycle_meta`],
/// or it never echoes an `Mcp-Session-Id` header. Gated on the `Parts`
/// extension being present so a non-HTTP transport (stdio) is never affected.
#[cfg(feature = "transport-http")]
fn is_stateless_http_request(
    extensions: &rmcp::model::Extensions,
    meta: &rmcp::model::RequestMetaObject,
) -> bool {
    extensions
        .get::<axum::http::request::Parts>()
        .is_some_and(|parts| {
            request_uses_discover_lifecycle_meta(meta)
                || !parts
                    .headers
                    .contains_key(rmcp::transport::common::http_header::HEADER_SESSION_ID)
        })
}

#[cfg(not(feature = "transport-http"))]
const fn is_stateless_http_request(
    _extensions: &rmcp::model::Extensions,
    _meta: &rmcp::model::RequestMetaObject,
) -> bool {
    false
}

/// Reject a subscription request rmcp served over the stateless per-request
/// HTTP path (#482): the state it would write is dropped the moment the
/// request completes, so this surfaces an explicit error instead of a
/// silent no-op.
fn reject_if_stateless_http(
    context: &rmcp::service::RequestContext<RoleServer>,
) -> Result<(), McpError> {
    if is_stateless_http_request(&context.extensions, &context.meta) {
        return Err(McpError::new(
            ErrorCode(crate::error::STATELESS_SUBSCRIPTION_ERROR_CODE),
            "resource subscriptions require a stateful session; this request was served over \
             the stateless per-request HTTP path, which never persists a subscription past the \
             response that acknowledges it -- retry over a session established via the MCP \
             `initialize` handshake, and without per-request `_meta` protocol negotiation"
                .to_string(),
            None,
        ));
    }
    Ok(())
}

/// Maps an [`crate::error::Error`] onto the wire-level MCP error, via
/// [`crate::error::Error::mcp_error_kind`]'s classification: caller-fault
/// variants become `INVALID_PARAMS`, retryable variants (e.g.
/// `WorkspaceIndexing`, `ServerInitializing`) get their own bespoke code plus
/// a structured `data` payload, and everything else falls back to
/// `INTERNAL_ERROR`.
// By-value `e` matches `Result::map_err`'s `FnOnce(E) -> F`, letting this be
// passed directly as `.map_err(map_bridge_error)` at every call site.
#[allow(clippy::needless_pass_by_value)]
fn map_bridge_error(e: crate::error::Error) -> McpError {
    let message = e.to_string();
    match e.mcp_error_kind() {
        crate::error::McpErrorKind::InvalidParams => McpError::invalid_params(message, None),
        crate::error::McpErrorKind::Internal => McpError::internal_error(message, None),
        crate::error::McpErrorKind::Retryable { code, data } => {
            McpError::new(ErrorCode(code), message, Some(data))
        }
    }
}

/// Map a bridge-layer result to the MCP tool response shape shared by every `#[tool]` handler.
fn to_tool_result<T: serde::Serialize>(
    result: crate::error::Result<T>,
) -> Result<String, McpError> {
    match result {
        Ok(value) => serde_json::to_string(&value)
            .map_err(|e| McpError::internal_error(format!("Serialization error: {e}"), None)),
        Err(e) => Err(map_bridge_error(e)),
    }
}

/// Map a bridge-layer result to a structured MCP tool response (`structuredContent` plus the
/// legacy `content` text block, per the MCP spec's backwards-compat shape).
///
/// The handler's own return type -- not this helper's -- is what the `#[tool]` macro reads to
/// derive `outputSchema`; it must spell `Result<Json<T>, McpError>` literally (no alias) for the
/// macro to detect it. See `Json<T>`'s `IntoCallToolResult` impl, which this helper relies on.
fn to_structured_tool_result<T: Serialize + JsonSchema>(
    result: crate::error::Result<T>,
) -> Result<Json<T>, McpError> {
    match result {
        Ok(value) => Ok(Json(value)),
        Err(e) => Err(map_bridge_error(e)),
    }
}

/// Whether `route_id`'s server is actively indexing; `None` reads `false`.
fn route_indexing_loading(
    cache: &NotificationCache,
    route_id: Option<&crate::config::ServerId>,
) -> bool {
    route_id.is_some_and(|id| cache.indexing_state(id) == IndexingState::Loading)
}

/// Fixed page size for `list_resources` pagination.
///
/// `DocumentTracker`'s configured `max_documents` (0 = unlimited) isn't
/// reachable from here -- it's private to the tracker, and `0` means the
/// document count itself is unbounded anyway -- so this is an independent
/// page-size ceiling, large enough to rarely trigger for typical workspaces
/// but small enough to stay well under stdio transport buffer limits.
const RESOURCE_PAGE_SIZE: usize = 100;

/// Slice `paths` into the page starting at the position `cursor` resumes
/// from, returning the page and the cursor for the next page (`None` once
/// the last page is reached).
///
/// `paths` must already be sorted: the caller's source
/// (`open_document_paths()`) is backed by a `HashMap` with no ordering
/// guarantee, and a stable order is required for a cursor to resume at a
/// reproducible position across calls. The cursor is an index into that
/// order, not a document identity: if a document closes at an index below
/// the cursor between two calls, every later entry shifts down one and the
/// next page silently skips the entry that moved into the cursor's old
/// slot. Low-impact for this use case (a stdio single-session server), but
/// callers pairing pagination with concurrent document open/close should be
/// aware a page can miss an entry rather than duplicate one.
///
/// # Errors
///
/// Returns an error only if `cursor` fails to parse as a `usize`. Any
/// parseable value is accepted as a page-start index, including one that
/// isn't page-aligned (not a value this function itself ever returns via
/// `next_cursor`) or is out of range (e.g. documents were closed between
/// calls) -- an out-of-range cursor is not an error, it yields an empty
/// final page.
fn paginate_resource_paths<'a>(
    paths: &'a [PathBuf],
    cursor: Option<&str>,
    page_size: usize,
) -> Result<(&'a [PathBuf], Option<String>), McpError> {
    debug_assert!(
        page_size > 0,
        "page_size must be non-zero, or next_cursor never advances"
    );

    let start = match cursor {
        Some(c) => c.parse::<usize>().map_err(|_| {
            McpError::invalid_params(format!("invalid pagination cursor: {c}"), None)
        })?,
        None => 0,
    };

    let rest = paths.get(start..).unwrap_or_default();
    let page = &rest[..rest.len().min(page_size)];
    // `start` is client-controlled (parsed straight from the cursor), so the
    // addition must not panic (debug) or silently wrap (release) for a
    // cursor near `usize::MAX`.
    let next_start = start.saturating_add(page_size);
    let next_cursor = (next_start < paths.len()).then(|| next_start.to_string());

    Ok((page, next_cursor))
}

/// `get_diagnostics`'s response shape.
///
/// Wraps `DiagnosticsResult` with an explicit signal that the pull-model
/// diagnostics read may be incomplete: `handle_diagnostics` deliberately
/// stays ungated on workspace-indexing readiness (#445 -- it reads from the
/// notification-cache poll path, not a live whole-workspace LSP request, so
/// blocking it the way `IndexingGate::Required` blocks hover/definition/etc.
/// would be the wrong fix shape for this one call site; see
/// `routing::IndexingGate`'s doc). Instead this flags the result rather than
/// silently returning what can read as "no errors" while the routed server
/// is still loading.
#[derive(serde::Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
struct DiagnosticsResponse {
    #[serde(flatten)]
    result: DiagnosticsResult,
    /// `true` if the file's diagnostics-route server (resolved via
    /// [`Translator::diagnostics_route_id_for_path`]) had an active signal
    /// indicating its initial workspace-load/indexing phase was still in
    /// progress at any point during this read -- the returned diagnostics
    /// may reflect a partial index. Sampled both before and after the pull
    /// request (see `McplsServer::get_diagnostics`), not just at one point
    /// in time, so a server that was `Loading` mid-pull but finished by the
    /// time the pull settled still reads `true` here. `false` both when the
    /// server was done indexing for the whole read (or never reported a
    /// readiness signal) and when no route is configured for the file's
    /// language at all.
    indexing_in_progress: bool,
}

/// `read_resource`'s diagnostics payload, distinguishing a file mcpls has no
/// information about (`tracked: false`, always paired with empty
/// `diagnostics`) from one it does -- whether because the file is currently
/// open via `DocumentTracker`, or an LSP server has published diagnostics
/// for it regardless of open state (`tracked: true`; `diagnostics: []` if
/// clean or not yet analyzed).
///
/// `version` is the document version the diagnostics were computed against
/// (the client's staleness signal, mirroring `DiagnosticInfo::version`) --
/// `None` both when untracked and when tracked but nothing has been
/// published yet. `uri` is deliberately omitted: the caller already knows it
/// (it's the resource they requested).
///
/// `push_notifications_degraded` mirrors `get_cached_diagnostics`'s field of
/// the same name (#359): `true` when the file's diagnostics-route server
/// crashed and was respawned, so `subscribe`'s replay and the pump's
/// `notify_resource_updated` calls for it have gone dark until the whole
/// mcpls process restarts, same as this cache-only read.
#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
struct ResourceDiagnosticsResponse {
    tracked: bool,
    version: Option<i32>,
    diagnostics: Vec<lsp_types::Diagnostic>,
    push_notifications_degraded: bool,
    /// Same #445 signal as `get_diagnostics`/`get_cached_diagnostics`: `true`
    /// if the file's diagnostics-route server has an active signal
    /// indicating its initial workspace-load/indexing phase is still in
    /// progress as of this read.
    indexing_in_progress: bool,
}

impl ResourceDiagnosticsResponse {
    fn new(
        tracked: bool,
        entry: Option<&DiagnosticInfo>,
        push_notifications_degraded: bool,
        indexing_in_progress: bool,
    ) -> Self {
        Self {
            tracked,
            version: entry.and_then(|e| e.version),
            diagnostics: entry.map(|e| e.diagnostics.clone()).unwrap_or_default(),
            push_notifications_degraded,
            indexing_in_progress,
        }
    }
}

/// Build `read_resource`'s response for a file. `tracked` is true when the
/// file is currently open via `DocumentTracker` (`document_open`) *or* the
/// diagnostics cache already holds an entry for it (`entry.is_some()`) --
/// not `document_open` alone: an LSP server publishes
/// `textDocument/publishDiagnostics` for whatever it analyzes, including
/// files mcpls never explicitly opened (e.g. one rust-analyzer pulls in
/// transitively), so `document_open` alone could report `tracked: false`
/// while `diagnostics` is still non-empty, contradicting the documented
/// "untracked implies empty diagnostics" contract.
fn build_resource_diagnostics_response(
    document_open: bool,
    entry: Option<&DiagnosticInfo>,
    push_notifications_degraded: bool,
    indexing_in_progress: bool,
) -> ResourceDiagnosticsResponse {
    ResourceDiagnosticsResponse::new(
        document_open || entry.is_some(),
        entry,
        push_notifications_degraded,
        indexing_in_progress,
    )
}

#[tool_router(router = declared_tool_router)]
impl McplsServer {
    /// Create a new MCP server with the given translator, notification cache,
    /// workspace roots, and subscription registry.
    ///
    /// A fresh, empty subscription set is registered into
    /// `subscription_registry` for this instance -- see
    /// [`Self::for_new_session`] for how per-HTTP-session isolation builds on
    /// top of that.
    ///
    /// `project_config_ignored` reports whether a CWD-discovered
    /// `./mcpls.toml` was skipped as untrusted when the active config was
    /// loaded (see [`ServerConfig::project_config_ignored`](crate::config::ServerConfig::project_config_ignored));
    /// `get_info` surfaces it in [`RmcpServerConfig::instructions`]. `mcp` carries
    /// the configured `[mcp]` presentation overrides (see
    /// [`crate::config::McpConfig`]), also read by `get_info`.
    #[must_use]
    pub fn new(
        translator: Arc<Translator>,
        notification_cache: Arc<Mutex<NotificationCache>>,
        workspace_roots: Arc<[PathBuf]>,
        subscription_registry: SubscriptionRegistry,
        project_config_ignored: bool,
        mcp: McpConfig,
    ) -> Self {
        let tool_router = Arc::new(Self::build_tool_router(mcp.tool_prefix.as_ref()));
        let context = Arc::new(BridgeContext::new(
            translator,
            notification_cache,
            workspace_roots,
            subscription_registry,
            project_config_ignored,
            mcp,
        ));
        Self {
            context,
            tool_router,
        }
    }

    /// Build a new server instance for a new HTTP session, giving it its own
    /// isolated [`ResourceSubscriptions`](crate::bridge::ResourceSubscriptions)
    /// set registered into the same [`SubscriptionRegistry`].
    ///
    /// Every other piece of shared state (translator, notification cache,
    /// workspace roots, config) is shared with the original via a cheap `Arc`
    /// clone -- only the subscription set is fresh. Called from the HTTP
    /// transport's service factory (see `transport::run_http`); stdio never
    /// calls this, since it only ever serves the one session `McplsServer::new`
    /// already built.
    ///
    /// "Per session" narrows to "per request" on rmcp's stateless HTTP path --
    /// see [`SubscriptionRegistry`]'s "Known limitation" section for what that
    /// means for the instance (and subscription set) this returns.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    ///
    /// use mcpls_core::bridge::{NotificationCache, SubscriptionRegistry, Translator};
    /// use mcpls_core::config::McpConfig;
    /// use mcpls_core::mcp::McplsServer;
    /// use tokio::sync::Mutex;
    ///
    /// let server = McplsServer::new(
    ///     Arc::new(Translator::new()),
    ///     Arc::new(Mutex::new(NotificationCache::new())),
    ///     Arc::from(Vec::new()),
    ///     SubscriptionRegistry::new(),
    ///     false,
    ///     McpConfig::default(),
    /// );
    /// // One `McplsServer` clone per HTTP session; each gets isolated resource
    /// // subscriptions while still sharing the same LSP-facing state.
    /// let _session = server.for_new_session();
    /// ```
    #[must_use]
    pub fn for_new_session(&self) -> Self {
        let subscriptions = self.context.subscription_registry.register();
        let context = Arc::new(BridgeContext {
            translator: Arc::clone(&self.context.translator),
            notification_cache: Arc::clone(&self.context.notification_cache),
            workspace_roots: Arc::clone(&self.context.workspace_roots),
            subscriptions,
            subscription_registry: self.context.subscription_registry.clone(),
            project_config_ignored: self.context.project_config_ignored,
            mcp: self.context.mcp.clone(),
        });
        Self {
            context,
            tool_router: Arc::clone(&self.tool_router),
        }
    }

    /// This instance's [`SubscriptionRegistry`], so a test exercising the
    /// real HTTP factory/session-close path (`transport.rs`'s integration
    /// tests) can assert on registry state without reaching into private
    /// `BridgeContext` fields cross-module.
    #[cfg(test)]
    pub(crate) fn subscription_registry(&self) -> SubscriptionRegistry {
        self.context.subscription_registry.clone()
    }

    /// Router for every MCP tool, with the read-only classification applied
    /// and, when `prefix` is configured, every tool name rewritten to
    /// `{prefix}_{name}`.
    ///
    /// Every mcpls tool is a read-only LSP query: `rename_symbol`,
    /// `format_document` and `get_code_actions` return a *proposed*
    /// `WorkspaceEdit` and never write to disk. Applying that once here
    /// replaces an identical `annotations(...)` block on all 20 `#[tool]`
    /// attributes. A tool declaring its own annotations keeps them;
    /// `test_tool_annotation_classifications_match_intent` forces a future
    /// mutating tool to write down an explicit classification rather than
    /// inherit this default silently.
    ///
    /// With `prefix: None` this returns byte-for-byte what it always has --
    /// `test_tool_surface_matches_golden_snapshot` pins that as the
    /// backward-compatibility guarantee for the default, unprefixed surface.
    ///
    /// The rename re-keys `router.map` via [`ToolRouter::add_route`] rather
    /// than inserting directly, so it goes through rmcp's own
    /// `validate_and_warn_tool_name` for defence-in-depth. It does **not**
    /// re-key `ToolRouter`'s private `disabled` set -- inert today (mcpls
    /// never calls `disable_route`/`with_disabled`, pinned by
    /// `test_no_route_is_ever_disabled` below), but a latent bug the moment
    /// that changes: any future `disable_route` call must name the
    /// already-prefixed tool name and must run strictly after this rename.
    fn build_tool_router(prefix: Option<&ToolPrefix>) -> ToolRouter<Self> {
        let mut router = Self::declared_tool_router();
        for route in router.map.values_mut() {
            let title = route.attr.title.clone();
            route.attr.annotations.get_or_insert_with(|| {
                ToolAnnotations::from_raw(title, Some(true), Some(false), Some(true), None)
            });
        }
        if let Some(prefix) = prefix {
            debug_assert!(router.map.keys().all(|name| router.has_route(name)));
            let unprefixed = std::mem::take(&mut router.map);
            let entry_count = unprefixed.len();
            for (_, mut route) in unprefixed {
                route.attr.name = Cow::Owned(format!("{prefix}_{}", route.attr.name));
                router.add_route(route);
            }
            debug_assert_eq!(router.map.len(), entry_count);
        }
        router
    }

    /// Get hover information at a position in a file.
    #[tool(
        description = "Type and documentation info at position. Returns signatures, docs, and inferred types for symbols. `positions_degraded: true` on the result means the returned range's `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Hover"
    )]
    async fn get_hover(
        &self,
        Parameters(PositionParams {
            file_path,
            line,
            character,
        }): Parameters<PositionParams>,
    ) -> Result<String, McpError> {
        to_tool_result(
            self.context
                .translator
                .handle_hover(file_path, Position { line, character })
                .await,
        )
    }

    /// Get the definition location of a symbol.
    #[tool(
        description = "Definition location of symbol at position. Returns file path, line, and character where declared. Capped at a fixed maximum for a pathological case; `truncated: true` on the result means more locations exist than are returned. `positions_degraded: true` means some returned `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Go to Definition"
    )]
    async fn get_definition(
        &self,
        Parameters(PositionParams {
            file_path,
            line,
            character,
        }): Parameters<PositionParams>,
    ) -> Result<Json<DefinitionResult>, McpError> {
        to_structured_tool_result(
            self.context
                .translator
                .handle_definition(file_path, Position { line, character })
                .await,
        )
    }

    /// Find all references to a symbol.
    #[tool(
        description = "References to symbol at position, across workspace. Capped at a fixed maximum for an extremely common symbol; `truncated: true` on the result means more references exist than are returned. `positions_degraded: true` means some returned `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Find References"
    )]
    async fn get_references(
        &self,
        Parameters(ReferencesParams {
            position:
                PositionParams {
                    file_path,
                    line,
                    character,
                },
            include_declaration,
        }): Parameters<ReferencesParams>,
    ) -> Result<Json<ReferencesResult>, McpError> {
        to_structured_tool_result(
            self.context
                .translator
                .handle_references(file_path, Position { line, character }, include_declaration)
                .await,
        )
    }

    /// Get diagnostics for a file.
    #[tool(
        description = "Diagnostics for a file. Returns errors, warnings, and hints with severity and location. `indexingInProgress: true` means the routed server was indexing at some point during this read, so results may be incomplete. `positions_degraded: true` means some returned `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Diagnostics"
    )]
    async fn get_diagnostics(
        &self,
        Parameters(DiagnosticsParams { file_path }): Parameters<DiagnosticsParams>,
    ) -> Result<Json<DiagnosticsResponse>, McpError> {
        // Resolved from the validated/canonicalized path (mirrors
        // `read_resource`), not the raw client-supplied path: a symlink
        // whose extension differs from its target must route to the same
        // language `handle_diagnostics`'s own validation resolves, or this
        // could observe the wrong server's (or no server's) indexing state.
        // Best-effort (`.ok()`): an invalid/out-of-workspace path just reads
        // `false` here and fails properly inside `handle_diagnostics` below.
        let route_id =
            validate_path_against_roots(Path::new(&file_path), &self.context.workspace_roots)
                .ok()
                .and_then(|validated_path| {
                    self.context
                        .translator
                        .diagnostics_route_id_for_path(&validated_path)
                });

        // Sampled both before and after the pull request, OR'd:
        // `handle_diagnostics` deliberately never makes an `IndexingGate`
        // decision (#445), so this is the only place indexing readiness is
        // observed for this call. A server that was `Loading` while the
        // pull was computed but finished before a post-only sample would
        // read `false` -- a false negative on exactly the case #445 exists
        // to catch. Sampling only before risks the opposite miss (indexing
        // starts mid-pull). A stale `true` from either sample is harmless:
        // this flag only ever claims "may be incomplete", never "is
        // complete".
        let indexing_before = {
            let cache = self.context.notification_cache.lock().await;
            route_indexing_loading(&cache, route_id.as_ref())
        };

        // Merging push-model (flycheck/clippy) diagnostics into the pull
        // result, including the pull-error-but-cache-has-data fallback, is
        // handled inside handle_diagnostics itself -- see its doc comment.
        let result = self
            .context
            .translator
            .handle_diagnostics(file_path, &self.context.notification_cache)
            .await;

        let indexing_after = {
            let cache = self.context.notification_cache.lock().await;
            route_indexing_loading(&cache, route_id.as_ref())
        };
        let indexing_in_progress = indexing_before || indexing_after;

        to_structured_tool_result(result.map(|result| DiagnosticsResponse {
            result,
            indexing_in_progress,
        }))
    }

    /// Rename a symbol across the workspace.
    // read-only: returns a proposed WorkspaceEdit, does not apply it -- mcpls
    // has no write-back path today; revisit if that changes.
    #[tool(
        description = "Rename symbol across workspace. Returns text edits for all files where symbol is used. A non-empty `dropped` field means some edits were withheld (e.g. out-of-workspace files) -- the rename is then incomplete even though `changes` is non-empty. `positions_degraded: true` means some returned edit ranges' `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Rename Symbol"
    )]
    async fn rename_symbol(
        &self,
        Parameters(RenameParams {
            position:
                PositionParams {
                    file_path,
                    line,
                    character,
                },
            new_name,
        }): Parameters<RenameParams>,
    ) -> Result<String, McpError> {
        to_tool_result(
            self.context
                .translator
                .handle_rename(file_path, Position { line, character }, new_name)
                .await,
        )
    }

    /// Get code completion suggestions.
    #[tool(
        description = "Completion suggestions at position. Returns methods, functions, variables, types, and snippets.",
        title = "Completions"
    )]
    async fn get_completions(
        &self,
        Parameters(CompletionsParams {
            position:
                PositionParams {
                    file_path,
                    line,
                    character,
                },
            trigger,
        }): Parameters<CompletionsParams>,
    ) -> Result<String, McpError> {
        to_tool_result(
            self.context
                .translator
                .handle_completions(file_path, Position { line, character }, trigger)
                .await,
        )
    }

    /// Get all symbols in a document.
    #[tool(
        description = "Symbols in a file. Returns hierarchical outline with functions, classes, structs, and locations. `positions_degraded: true` on the result means some returned `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Document Symbols"
    )]
    async fn get_document_symbols(
        &self,
        Parameters(DocumentSymbolsParams { file_path }): Parameters<DocumentSymbolsParams>,
    ) -> Result<Json<DocumentSymbolsResult>, McpError> {
        to_structured_tool_result(
            self.context
                .translator
                .handle_document_symbols(file_path)
                .await,
        )
    }

    /// Format a document according to language server rules.
    // read-only: returns proposed text edits, does not apply them -- mcpls
    // has no write-back path today; revisit if that changes.
    #[tool(
        description = "Format document with language-specific rules. Returns text edits for indentation, spacing, and style. `positions_degraded: true` on the result means some returned edit ranges' `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Format Document"
    )]
    async fn format_document(
        &self,
        Parameters(FormatDocumentParams {
            file_path,
            tab_size,
            insert_spaces,
        }): Parameters<FormatDocumentParams>,
    ) -> Result<String, McpError> {
        to_tool_result(
            self.context
                .translator
                .handle_format_document(file_path, tab_size, insert_spaces)
                .await,
        )
    }

    /// Search for symbols across the workspace.
    #[tool(
        description = "Search workspace symbols by name. Supports partial matching and fuzzy search. `limit` is capped at a fixed server-side maximum regardless of the value requested; `truncated: true` on the result means more matches exist than are returned. `positions_degraded: true` means some returned `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Workspace Symbol Search"
    )]
    async fn workspace_symbol_search(
        &self,
        Parameters(WorkspaceSymbolParams {
            query,
            kind_filter,
            limit,
        }): Parameters<WorkspaceSymbolParams>,
    ) -> Result<String, McpError> {
        to_tool_result(
            self.context
                .translator
                .handle_workspace_symbol(query, kind_filter, limit)
                .await,
        )
    }

    /// Get code actions for a range.
    // read-only: returns proposed CodeAction edits, does not apply them --
    // mcpls has no write-back path today; revisit if that changes.
    #[tool(
        description = "Code actions for range. Returns quick fixes, refactorings, and source actions with edits. An action's `edit.dropped` field, when non-empty, means some of that edit's changes were withheld (e.g. out-of-workspace files). `positions_degraded: true` on the result means some returned `character` offsets (diagnostic or edit ranges) may be inexact (only possible for a non-UTF-16 server).",
        title = "Code Actions"
    )]
    async fn get_code_actions(
        &self,
        Parameters(CodeActionsParams {
            file_path,
            range:
                RangeParams {
                    start_line,
                    start_character,
                    end_line,
                    end_character,
                },
            kind_filter,
        }): Parameters<CodeActionsParams>,
    ) -> Result<String, McpError> {
        to_tool_result(
            self.context
                .translator
                .handle_code_actions(
                    file_path,
                    Position {
                        line: start_line,
                        character: start_character,
                    },
                    Position {
                        line: end_line,
                        character: end_character,
                    },
                    kind_filter,
                )
                .await,
        )
    }

    /// Prepare call hierarchy at a position.
    #[tool(
        description = "Prepare call hierarchy at position. Returns callable items for incoming/outgoing call analysis. `positions_degraded: true` on the result means some returned `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Prepare Call Hierarchy"
    )]
    async fn prepare_call_hierarchy(
        &self,
        Parameters(PositionParams {
            file_path,
            line,
            character,
        }): Parameters<PositionParams>,
    ) -> Result<String, McpError> {
        to_tool_result(
            self.context
                .translator
                .handle_call_hierarchy_prepare(file_path, Position { line, character })
                .await,
        )
    }

    /// Get incoming calls (callers).
    #[tool(
        description = "Functions calling the specified item. Takes call hierarchy item, returns all callers. `positions_degraded: true` on the result means some returned `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Incoming Calls"
    )]
    async fn get_incoming_calls(
        &self,
        Parameters(CallHierarchyCallsParams { item }): Parameters<CallHierarchyCallsParams>,
    ) -> Result<String, McpError> {
        to_tool_result(self.context.translator.handle_incoming_calls(item).await)
    }

    /// Get outgoing calls (callees).
    #[tool(
        description = "Functions called by the specified item. Takes call hierarchy item, returns all callees. `positions_degraded: true` on the result means some returned `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Outgoing Calls"
    )]
    async fn get_outgoing_calls(
        &self,
        Parameters(CallHierarchyCallsParams { item }): Parameters<CallHierarchyCallsParams>,
    ) -> Result<String, McpError> {
        to_tool_result(self.context.translator.handle_outgoing_calls(item).await)
    }

    /// Get cached diagnostics for a file.
    #[tool(
        description = "Cached diagnostics from server notifications. Faster than the pull-model diagnostics tool, no new analysis. `positions_degraded: true` on the result means some returned `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Cached Diagnostics"
    )]
    async fn get_cached_diagnostics(
        &self,
        Parameters(CachedDiagnosticsParams { file_path }): Parameters<CachedDiagnosticsParams>,
    ) -> Result<String, McpError> {
        let result = match Translator::cached_diagnostics_path_and_uri(
            &self.context.workspace_roots,
            &file_path,
        ) {
            Ok((validated_path, uri)) => {
                // Resolved independently of the cache lookup below: a
                // respawn clears `diagnostics_owner` for this server's
                // entries along with its stale diagnostics (#359), so
                // the degraded flag can't be keyed on ownership -- the
                // routing identity is what stays stable across a
                // respawn.
                let route_id = self
                    .context
                    .translator
                    .diagnostics_route_id_for_path(&validated_path);

                // Lock only long enough for the map lookup + clone: no
                // canonicalize() or Vec mapping while `notification_cache`
                // is held, since `diagnostics_pump` needs the same lock.
                let (diag_info, owner, push_degraded, indexing_in_progress) = {
                    let cache = self.context.notification_cache.lock().await;
                    let owner = cache.diagnostics_owner(&uri).cloned();
                    let push_degraded = route_id
                        .as_ref()
                        .is_some_and(|id| cache.is_push_degraded(id));
                    // #445: same signal `get_diagnostics` surfaces.
                    let indexing_in_progress = route_indexing_loading(&cache, route_id.as_ref());
                    (
                        cache.diagnostics(&uri).cloned(),
                        owner,
                        push_degraded,
                        indexing_in_progress,
                    )
                };
                let encoding = owner.map_or(PositionEncoding::Utf16, |server_id| {
                    self.context.translator.position_encoding_for(&server_id)
                });
                let result = Translator::diagnostics_from_cache_entry(
                    diag_info.as_ref(),
                    encoding,
                    self.context.translator.document_tracker(),
                )
                .await;
                Ok(CachedDiagnosticsResponse {
                    result,
                    push_notifications_degraded: push_degraded,
                    indexing_in_progress,
                })
            }
            Err(e) => Err(e),
        };

        to_tool_result(result)
    }

    /// Get recent LSP server log messages.
    #[tool(
        description = "Recent server log messages. Filter by level (error, warning, info, debug) for debugging.",
        title = "Server Logs"
    )]
    async fn get_server_logs(
        &self,
        Parameters(ServerLogsParams { limit, min_level }): Parameters<ServerLogsParams>,
    ) -> Result<String, McpError> {
        to_tool_result({
            let cache = self.context.notification_cache.lock().await;
            Translator::handle_server_logs(&cache, limit, min_level)
        })
    }

    /// Get recent LSP server messages.
    #[tool(
        description = "Recent server messages (showMessage notifications). User-facing prompts and status updates.",
        title = "Server Messages"
    )]
    async fn get_server_messages(
        &self,
        Parameters(ServerMessagesParams { limit }): Parameters<ServerMessagesParams>,
    ) -> Result<String, McpError> {
        to_tool_result({
            let cache = self.context.notification_cache.lock().await;
            Translator::handle_server_messages(&cache, limit)
        })
    }

    /// Get signature help at a position.
    #[tool(
        description = "Signature help at position. Returns parameter info, active signature/parameter, and documentation while typing a call.",
        title = "Signature Help"
    )]
    async fn get_signature_help(
        &self,
        Parameters(PositionParams {
            file_path,
            line,
            character,
        }): Parameters<PositionParams>,
    ) -> Result<String, McpError> {
        to_tool_result(
            self.context
                .translator
                .handle_signature_help(file_path, Position { line, character })
                .await,
        )
    }

    /// Go to implementation locations.
    #[tool(
        description = "Implementation locations of trait method or interface member at position. Capped at a fixed maximum for an extremely common trait/interface; `truncated: true` on the result means more implementations exist than are returned. `positions_degraded: true` means some returned `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Go to Implementation"
    )]
    async fn go_to_implementation(
        &self,
        Parameters(PositionParams {
            file_path,
            line,
            character,
        }): Parameters<PositionParams>,
    ) -> Result<String, McpError> {
        to_tool_result(
            self.context
                .translator
                .handle_implementation(file_path, Position { line, character })
                .await,
        )
    }

    /// Go to type definition location.
    #[tool(
        description = "Type definition location of expression at position. Distinct from go-to-definition for variable bindings. Capped at a fixed maximum for a pathological case; `truncated: true` on the result means more locations exist than are returned. `positions_degraded: true` means some returned `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Go to Type Definition"
    )]
    async fn go_to_type_definition(
        &self,
        Parameters(PositionParams {
            file_path,
            line,
            character,
        }): Parameters<PositionParams>,
    ) -> Result<String, McpError> {
        to_tool_result(
            self.context
                .translator
                .handle_type_definition(file_path, Position { line, character })
                .await,
        )
    }

    /// Get inlay hints for a range.
    #[tool(
        description = "Inlay hints in range. Returns inferred type/parameter annotations the editor would render inline. `positions_degraded: true` on the result means some returned hint `character` offsets may be inexact (only possible for a non-UTF-16 server).",
        title = "Inlay Hints"
    )]
    async fn get_inlay_hints(
        &self,
        Parameters(InlayHintsParams {
            file_path,
            range:
                RangeParams {
                    start_line,
                    start_character,
                    end_line,
                    end_character,
                },
        }): Parameters<InlayHintsParams>,
    ) -> Result<String, McpError> {
        to_tool_result(
            self.context
                .translator
                .handle_inlay_hints(
                    file_path,
                    Position {
                        line: start_line,
                        character: start_character,
                    },
                    Position {
                        line: end_line,
                        character: end_character,
                    },
                )
                .await,
        )
    }
}

// `list_resources` is synchronous (no `.await`), but `ServerHandler::list_resources`
// requires `async fn`; `#[tool_handler]` also expands other trait methods without
// `.await`, so the lint is suppressed for the whole impl block.
#[allow(clippy::unused_async_trait_impl)]
#[tool_handler(router = self.tool_router)]
impl ServerHandler for McplsServer {
    async fn list_resources(
        &self,
        request: Option<rmcp::model::PaginatedRequestParams>,
        _context: rmcp::service::RequestContext<RoleServer>,
    ) -> Result<ListResourcesResult, McpError> {
        let mut open_paths = self.context.translator.open_document_paths();
        // `open_document_paths()` is backed by a `HashMap`; sort so pagination
        // cursors resume at a stable, deterministic position across calls.
        open_paths.sort();

        let cursor = request.and_then(|r| r.cursor);
        let (page, next_cursor) =
            paginate_resource_paths(&open_paths, cursor.as_deref(), RESOURCE_PAGE_SIZE)?;

        let resources: Vec<_> = page
            .iter()
            .filter_map(|path| {
                let uri = make_uri(path)
                    .inspect_err(|e| {
                        tracing::warn!(
                            "Skipping path in list_resources (make_uri failed): {}: {e}",
                            path.display()
                        );
                    })
                    .ok()?;
                let name = path
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("unknown")
                    .to_string();
                Some(
                    Resource::new(uri, name)
                        .with_mime_type("application/json")
                        .with_description("LSP diagnostics for this file"),
                )
            })
            .collect();

        Ok(ListResourcesResult {
            next_cursor,
            ..ListResourcesResult::with_all_items(resources)
        })
    }

    async fn read_resource(
        &self,
        request: ReadResourceRequestParams,
        _context: rmcp::service::RequestContext<RoleServer>,
    ) -> Result<ReadResourceResponse, McpError> {
        let path =
            parse_uri(&request.uri).map_err(|e| McpError::invalid_params(e.to_string(), None))?;

        // Enforce workspace-root containment — mirrors the guard in every LSP tool.
        // Validated against a lock-free snapshot of workspace_roots (fixed at
        // startup) so this cache-only read never needs to touch `translator` at all.
        let validated_path = validate_path_against_roots(&path, &self.context.workspace_roots)
            .map_err(map_bridge_error)?;

        // Build the URI from the canonicalized path (not the raw input path):
        // it must match what `diagnostics_pump` stores from LSP notifications,
        // which are always keyed by the canonical form.
        let lsp_uri = crate::bridge::path_to_uri(&validated_path).map_err(map_bridge_error)?;

        // Resolved independently of the cache lookup below -- see the same
        // reasoning on `get_cached_diagnostics` (#359): a respawn clears
        // `diagnostics_owner` for the crashed server's entries, so the
        // degraded flag can't be keyed on cache ownership.
        let route_id = self
            .context
            .translator
            .diagnostics_route_id_for_path(&validated_path);

        // Built from a borrow of the cache entry rather than `.cloned()`-ing the
        // whole `DiagnosticInfo` first: `build_resource_diagnostics_response`
        // only ever needs `version` (Copy) and its own clone of `diagnostics`,
        // so cloning the entry up front would clone `diagnostics` twice.
        let response = {
            let cache = self.context.notification_cache.lock().await;
            let push_degraded = route_id
                .as_ref()
                .is_some_and(|id| cache.is_push_degraded(id));
            // #445: same signal `get_diagnostics`/`get_cached_diagnostics`
            // surface.
            let indexing_in_progress = route_indexing_loading(&cache, route_id.as_ref());
            build_resource_diagnostics_response(
                self.context.translator.is_document_open(&validated_path),
                cache.diagnostics(lsp_uri.as_ref()),
                push_degraded,
                indexing_in_progress,
            )
        };

        let json = serde_json::to_string(&response)
            .map_err(|e| McpError::internal_error(format!("Serialization error: {e}"), None))?;

        Ok(ReadResourceResult::new(vec![ResourceContents::text(json, request.uri)]).into())
    }

    /// When cached diagnostics exist, the replay notification is flushed to the client
    /// before this call returns its own response; this is legal per JSON-RPC/MCP, which
    /// permits notifications to interleave with in-flight requests, so a conformant
    /// client must demultiplex by request `id` rather than assume response-before-notification ordering.
    async fn subscribe(
        &self,
        request: SubscribeRequestParams,
        context: rmcp::service::RequestContext<RoleServer>,
    ) -> Result<(), McpError> {
        reject_if_stateless_http(&context)?;

        let path =
            parse_uri(&request.uri).map_err(|e| McpError::invalid_params(e.to_string(), None))?;

        // Enforce workspace-root containment (same invariant as every LSP tool).
        // Validated against a lock-free snapshot of workspace_roots so subscribing
        // never needs to touch `translator` at all (see `read_resource`).
        let validated_path = validate_path_against_roots(&path, &self.context.workspace_roots)
            .map_err(map_bridge_error)?;

        // Track and reply under the canonical resource URI, not the client's raw
        // `request.uri`: `diagnostics_pump` derives `mcp_uri` from the canonical LSP
        // path (see below), so a subscription keyed by a non-canonical but equivalent
        // URI (symlink, macOS /var vs /private/var, ...) would never match its
        // `subs.contains` check and silently stop receiving pushes.
        let canonical_uri =
            make_uri(&validated_path).map_err(|e| McpError::invalid_params(e.to_string(), None))?;

        // Record the subscription *before* checking the cache. This closes the race where
        // a PublishDiagnostics notification lands between the cache check and the
        // subscription being recorded: if diagnostics arrive before this point, the check
        // below catches them; if they arrive after, `diagnostics_pump`'s own
        // `subs.contains` check already sees this URI as subscribed and delivers the
        // update through the normal push path.
        let newly_subscribed = self
            .context
            .subscriptions
            .subscribe(canonical_uri.clone())
            .await
            .map_err(|e| map_bridge_error(e.into()))?;
        if !newly_subscribed {
            tracing::debug!("client re-subscribed to already-subscribed resource {canonical_uri}");
        }

        // Record the raw request URI as an alias of the canonical one so a
        // later `unsubscribe` for the same raw URI still resolves even if
        // canonicalizing it then fails, e.g. because the file was deleted
        // since subscribing (#499). No-op when the two already match.
        self.context
            .subscriptions
            .record_alias(request.uri.clone(), canonical_uri.clone())
            .await;

        // Build the URI from the canonicalized path, matching `read_resource` and
        // what `diagnostics_pump` stores from LSP notifications.
        let lsp_uri = crate::bridge::path_to_uri(&validated_path).map_err(map_bridge_error)?;
        let has_cached_diagnostics = {
            let cache = self.context.notification_cache.lock().await;
            cache.diagnostics(lsp_uri.as_ref()).is_some()
        };

        if has_cached_diagnostics
            && let Err(e) = context
                .peer
                .notify_resource_updated(ResourceUpdatedNotificationParam::new(
                    canonical_uri.clone(),
                ))
                .await
        {
            tracing::warn!("Failed to replay cached diagnostics for {canonical_uri}: {e}");
        }

        Ok(())
    }

    async fn unsubscribe(
        &self,
        request: UnsubscribeRequestParams,
        context: rmcp::service::RequestContext<RoleServer>,
    ) -> Result<(), McpError> {
        reject_if_stateless_http(&context)?;

        // Parse the URI for consistency with subscribe validation.
        let path =
            parse_uri(&request.uri).map_err(|e| McpError::invalid_params(e.to_string(), None))?;

        // Remove under the same canonical URI `subscribe` recorded under. Best-effort
        // fall back to the raw URI if canonicalization fails (e.g. the file was
        // deleted since subscribing) so unsubscribing a stale entry never errors --
        // `ResourceSubscriptions::unsubscribe` then resolves it via the alias
        // `subscribe` recorded for this raw URI (#499).
        let key = validate_path_against_roots(&path, &self.context.workspace_roots)
            .ok()
            .and_then(|validated_path| make_uri(&validated_path).ok())
            .unwrap_or_else(|| request.uri.clone());

        if !self.context.subscriptions.unsubscribe(&key).await {
            tracing::debug!(
                "client unsubscribed from resource with no matching subscription: {key}"
            );
        }
        Ok(())
    }

    fn get_info(&self) -> RmcpServerConfig {
        let mut implementation = Implementation::new("mcpls", env!("CARGO_PKG_VERSION"));
        implementation.title = Some(
            self.context
                .mcp
                .title
                .clone()
                .unwrap_or_else(|| DEFAULT_SERVER_TITLE.to_string()),
        );
        implementation.description = Some(
            self.context
                .mcp
                .description
                .clone()
                .unwrap_or_else(|| DEFAULT_SERVER_DESCRIPTION.to_string()),
        );
        implementation.website_url = Some("https://github.com/bug-ops/mcpls".to_string());

        let capabilities = ServerCapabilities::builder()
            .enable_tools()
            .enable_resources()
            .enable_resources_subscribe()
            .build();
        let mut server_info = RmcpServerConfig::new(capabilities);
        server_info.server_info = implementation;
        let mut instructions = self
            .context
            .mcp
            .instructions
            .clone()
            .unwrap_or_else(|| DEFAULT_INSTRUCTIONS.to_string());

        if self.context.project_config_ignored {
            instructions.push_str(
                " NOTE: a project-local mcpls.toml was found in the current directory but \
                 ignored as untrusted; the server is running on built-in defaults or a global \
                 config instead. If this repository is trusted, restart mcpls with \
                 --trust-project-config (or MCPLS_TRUST_PROJECT_CONFIG=true) to load it.",
            );
        }
        server_info.instructions = Some(instructions);

        server_info
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::bridge::ResourceSubscriptions;

    fn create_test_server() -> McplsServer {
        create_test_server_with_ignored_flag(false)
    }

    fn create_test_server_with_ignored_flag(project_config_ignored: bool) -> McplsServer {
        create_test_server_with_mcp_config(project_config_ignored, McpConfig::default())
    }

    fn create_test_server_with_mcp_config(
        project_config_ignored: bool,
        mcp: McpConfig,
    ) -> McplsServer {
        let workspace_roots: Arc<[PathBuf]> = Arc::from(Vec::new());
        create_test_server_with_workspace_roots(project_config_ignored, mcp, workspace_roots)
    }

    /// Like [`create_test_server_with_mcp_config`], for tests that exercise a
    /// path-taking tool (e.g. `get_cached_diagnostics`) and so need a real
    /// workspace root -- an empty one now makes `validate_path_against_roots`
    /// fail closed with `Error::NoWorkspaceRoots`.
    fn create_test_server_with_workspace_roots(
        project_config_ignored: bool,
        mcp: McpConfig,
        workspace_roots: Arc<[PathBuf]>,
    ) -> McplsServer {
        let translator = Arc::new(Translator::new());
        let notification_cache = Arc::new(Mutex::new(NotificationCache::new()));
        McplsServer::new(
            translator,
            notification_cache,
            workspace_roots,
            SubscriptionRegistry::new(),
            project_config_ignored,
            mcp,
        )
    }

    /// A server with no LSP servers configured, backed by a real file under
    /// a real workspace root -- so a path-taking tool call clears the
    /// workspace-roots gate and exercises the "no server configured for this
    /// language" handler error downstream of it, rather than stopping at the
    /// gate itself with an unrelated `NoWorkspaceRoots`/`FileIo` error.
    fn create_test_server_with_real_file() -> (McplsServer, tempfile::TempDir, PathBuf) {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let test_file = temp_dir.path().join("file.rs");
        std::fs::write(&test_file, "fn main() {}").unwrap();
        let server = create_test_server_with_workspace_roots(
            false,
            McpConfig::default(),
            Arc::from([temp_dir.path().to_path_buf()]),
        );
        (server, temp_dir, test_file)
    }

    /// #424: `Error::WorkspaceIndexing` must map onto the dedicated
    /// `WORKSPACE_INDEXING_ERROR_CODE`, not the generic `INTERNAL_ERROR`
    /// every other variant gets, and must carry `server_id`/`elapsed_secs`
    /// in `data` so a client can act on them mechanically.
    #[test]
    fn test_map_bridge_error_workspace_indexing_uses_dedicated_error_code() {
        let err = crate::error::Error::WorkspaceIndexing {
            server_id: crate::config::ServerId::from("rust"),
            elapsed_secs: 30,
        };
        let mcp_err = map_bridge_error(err);

        assert_eq!(
            mcp_err.code,
            ErrorCode(crate::error::WORKSPACE_INDEXING_ERROR_CODE)
        );
        let data = mcp_err.data.unwrap();
        assert_eq!(data["serverId"], "rust");
        assert_eq!(data["elapsedSecs"], 30);
    }

    /// #479: `Error::ServerInitializing` must be distinguishable on the wire
    /// from a crash (`INTERNAL_ERROR`) and from `WorkspaceIndexing`, since
    /// both are retryable but for different reasons.
    #[test]
    fn test_map_bridge_error_server_initializing_uses_dedicated_error_code() {
        let err = crate::error::Error::ServerInitializing {
            server_id: crate::config::ServerId::from("python"),
        };
        let mcp_err = map_bridge_error(err);

        assert_eq!(
            mcp_err.code,
            ErrorCode(crate::error::SERVER_INITIALIZING_ERROR_CODE)
        );
        assert_ne!(
            mcp_err.code,
            ErrorCode(crate::error::WORKSPACE_INDEXING_ERROR_CODE)
        );
        let data = mcp_err.data.unwrap();
        assert_eq!(data["serverId"], "python");
    }

    /// Counterpart to the above: a variant with no explicit classification
    /// (neither caller-fault nor retryable) must still map onto the generic
    /// `INTERNAL_ERROR` code, unchanged.
    #[test]
    fn test_map_bridge_error_other_variant_uses_internal_error_code() {
        let err = crate::error::Error::NoServerForLanguage("python".to_string());
        let mcp_err = map_bridge_error(err);

        assert_eq!(mcp_err.code, ErrorCode::INTERNAL_ERROR);
        assert!(mcp_err.data.is_none());
    }

    /// #479: caller-fault variants that used to fall through to
    /// `INTERNAL_ERROR` (`-32603`) must now map onto `INVALID_PARAMS`
    /// (`-32602`), matching how the resource handlers already classify a
    /// `PathOutsideWorkspace` rejection.
    #[test]
    fn test_map_bridge_error_caller_fault_variants_use_invalid_params() {
        let caller_fault_errors = vec![
            crate::error::Error::InvalidToolParams("bad params".to_string()),
            crate::error::Error::PathOutsideWorkspace(PathBuf::from("/etc/passwd")),
            crate::error::Error::NotARegularFile(PathBuf::from("/dev/null")),
            crate::error::Error::InvalidUri("not a uri".to_string()),
            crate::error::Error::DocumentNotFound(PathBuf::from("/missing.rs")),
            crate::error::Error::FileSizeLimitExceeded { size: 100, max: 10 },
        ];

        for err in caller_fault_errors {
            let debug = format!("{err:?}");
            let mcp_err = map_bridge_error(err);
            assert_eq!(
                mcp_err.code,
                ErrorCode::INVALID_PARAMS,
                "expected {debug} to map onto INVALID_PARAMS"
            );
        }
    }

    /// #479 follow-up: `WorkspaceServersInitializing` (the no-single-server
    /// counterpart of `ServerInitializing`, see its doc comment) must be
    /// retryable too, sharing `SERVER_INITIALIZING_ERROR_CODE` since it's the
    /// same underlying condition.
    #[test]
    fn test_map_bridge_error_workspace_servers_initializing_is_retryable() {
        let err = crate::error::Error::WorkspaceServersInitializing;
        let mcp_err = map_bridge_error(err);

        assert_eq!(
            mcp_err.code,
            ErrorCode(crate::error::SERVER_INITIALIZING_ERROR_CODE)
        );
    }

    #[tokio::test]
    async fn test_server_info() {
        let server = create_test_server();
        let info = server.get_info();

        assert!(info.capabilities.tools.is_some());
        assert_eq!(info.server_info.name, "mcpls");
        assert!(info.instructions.is_some());
    }

    #[tokio::test]
    async fn test_server_info_omits_ignore_notice_when_not_ignored() {
        let server = create_test_server_with_ignored_flag(false);
        let info = server.get_info();

        assert!(!info.instructions.unwrap().contains("ignored as untrusted"));
    }

    #[tokio::test]
    async fn test_server_info_surfaces_ignored_project_config() {
        let server = create_test_server_with_ignored_flag(true);
        let info = server.get_info();

        let instructions = info.instructions.unwrap();
        assert!(instructions.contains("ignored as untrusted"));
        assert!(instructions.contains("--trust-project-config"));
    }

    #[tokio::test]
    async fn test_get_info_default_mcp_config_uses_built_in_text() {
        let server = create_test_server_with_mcp_config(false, McpConfig::default());
        let info = server.get_info();

        assert_eq!(
            info.server_info.title.as_deref(),
            Some(DEFAULT_SERVER_TITLE)
        );
        assert_eq!(
            info.server_info.description.as_deref(),
            Some(DEFAULT_SERVER_DESCRIPTION)
        );
        assert_eq!(info.instructions.as_deref(), Some(DEFAULT_INSTRUCTIONS));
    }

    #[tokio::test]
    async fn test_get_info_reflects_configured_mcp_fields() {
        let mcp = McpConfig {
            title: Some("Custom Title".to_string()),
            description: Some("Custom description".to_string()),
            instructions: Some("Custom instructions.".to_string()),
            tool_prefix: None,
        };
        let server = create_test_server_with_mcp_config(false, mcp);
        let info = server.get_info();

        assert_eq!(info.server_info.title.as_deref(), Some("Custom Title"));
        assert_eq!(
            info.server_info.description.as_deref(),
            Some("Custom description")
        );
        assert_eq!(info.instructions.as_deref(), Some("Custom instructions."));
    }

    /// Configured `instructions` replace the built-in blurb, but the
    /// untrusted-project-config NOTE must still be appended afterward --
    /// including when `instructions` sits exactly at
    /// `MAX_MCP_INSTRUCTIONS_BYTES`, proving the NOTE is outside the user's
    /// budget rather than truncated to make room for it.
    #[tokio::test]
    async fn test_get_info_appends_ignore_notice_after_configured_instructions_at_cap() {
        let instructions = "a".repeat(crate::config::MAX_MCP_INSTRUCTIONS_BYTES);
        let mcp = McpConfig {
            title: None,
            description: None,
            instructions: Some(instructions.clone()),
            tool_prefix: None,
        };
        let server = create_test_server_with_mcp_config(true, mcp);
        let info = server.get_info();

        let returned_instructions = info.instructions.unwrap();
        assert!(returned_instructions.starts_with(&instructions));
        assert!(returned_instructions.contains("ignored as untrusted"));
    }

    #[tokio::test]
    async fn test_hover_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(PositionParams {
            file_path: test_file.to_str().unwrap().to_string(),
            line: 1,
            character: 1,
        });

        // No LSP server is registered for any language on this test server,
        // so this fails downstream of the workspace-roots gate with
        // `Error::NoServerForLanguage`/`NoServerConfigured`.
        let result = server.get_hover(params).await;
        assert!(result.is_err());
    }

    /// #417: the fail-closed `Error::NoWorkspaceRoots` path must propagate
    /// correctly through a `#[tool]` handler's full error-mapping chain
    /// (`to_tool_result`/`McpError::internal_error`), not just through the
    /// lower-level `Translator::validate_path`/`validate_path_against_roots`
    /// unit tests -- `create_test_server()` here deliberately keeps the
    /// empty roots that `create_test_server_with_real_file()` (used by the
    /// rest of this test group) sets up a real root to avoid.
    #[tokio::test]
    async fn test_hover_tool_with_params_no_workspace_roots() {
        let server = create_test_server();
        let params = Parameters(PositionParams {
            file_path: "/test/file.rs".to_string(),
            line: 1,
            character: 1,
        });

        let result = server.get_hover(params).await;
        let err = result.unwrap_err();
        assert!(
            err.message.contains("no workspace roots configured"),
            "expected the NoWorkspaceRoots error to propagate through the tool handler, got: {}",
            err.message
        );
    }

    #[tokio::test]
    async fn test_definition_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(PositionParams {
            file_path: test_file.to_str().unwrap().to_string(),
            line: 10,
            character: 5,
        });

        let result = server.get_definition(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_references_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(ReferencesParams {
            position: PositionParams {
                file_path: test_file.to_str().unwrap().to_string(),
                line: 10,
                character: 5,
            },
            include_declaration: false,
        });

        let result = server.get_references(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_diagnostics_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(DiagnosticsParams {
            file_path: test_file.to_str().unwrap().to_string(),
        });

        let result = server.get_diagnostics(params).await;
        assert!(result.is_err());
    }

    /// #445: `get_diagnostics` must flag `indexingInProgress: true` when the
    /// file's diagnostics-route server has an active `Loading` signal as of
    /// this read, since `handle_diagnostics` itself deliberately stays
    /// ungated (see `routing::IndexingGate`'s doc) -- this is the only place
    /// that signal reaches the caller.
    #[tokio::test]
    async fn test_get_diagnostics_flags_indexing_in_progress() {
        use std::collections::HashMap;
        use std::fs;

        use tempfile::TempDir;
        use tokio::io::BufReader;

        use crate::config::{ServerId, ToolRouter};
        use crate::test_lsp::{fake_lsp_client, read_framed_message, write_response};

        let server_id = ServerId::from("rust");
        let dir = TempDir::new().unwrap();
        let mut translator = Translator::new()
            .with_router(ToolRouter::catch_all([(
                server_id.clone(),
                "rust".to_string(),
            )]))
            .with_extensions(HashMap::from([("rs".to_string(), "rust".to_string())]));
        translator.set_workspace_roots(vec![dir.path().to_path_buf()]);
        let translator = Arc::new(translator);
        let (client, mut fake_server) = fake_lsp_client();
        translator.register_client(server_id.clone(), client);

        let notification_cache = Arc::new(Mutex::new(NotificationCache::new()));
        notification_cache.lock().await.observe_indexing_signal(
            &server_id,
            "experimental/serverStatus",
            Some(&serde_json::json!({"quiescent": false})),
        );

        let path = dir.path().join("lib.rs");
        fs::write(&path, "fn main() {}").unwrap();
        let path_str = path.to_string_lossy().to_string();

        let mcp_server = McplsServer::new(
            Arc::clone(&translator),
            Arc::clone(&notification_cache),
            Arc::from(vec![dir.path().to_path_buf()]),
            SubscriptionRegistry::new(),
            false,
            McpConfig::default(),
        );

        let call = {
            let params = Parameters(DiagnosticsParams {
                file_path: path_str,
            });
            tokio::spawn(async move { mcp_server.get_diagnostics(params).await })
        };

        let mut wire = BufReader::new(&mut fake_server.write_stdout);
        let opened = read_framed_message(&mut wire).await;
        assert_eq!(opened["method"], "textDocument/didOpen");
        let diag_request = read_framed_message(&mut wire).await;
        assert_eq!(diag_request["method"], "textDocument/diagnostic");
        write_response(
            &mut fake_server.read_half_stdin,
            &diag_request["id"],
            serde_json::json!({"kind": "full", "items": []}),
        )
        .await;

        let result = call.await.unwrap().unwrap();
        assert!(result.0.indexing_in_progress);
        assert!(result.0.result.diagnostics.is_empty());
    }

    /// Counterpart to the above: once the server has no active `Loading`
    /// signal, `indexingInProgress` must read back `false`.
    #[tokio::test]
    async fn test_get_diagnostics_indexing_in_progress_false_when_ready() {
        use std::collections::HashMap;
        use std::fs;

        use tempfile::TempDir;
        use tokio::io::BufReader;

        use crate::config::{ServerId, ToolRouter};
        use crate::test_lsp::{fake_lsp_client, read_framed_message, write_response};

        let server_id = ServerId::from("rust");
        let dir = TempDir::new().unwrap();
        let mut translator = Translator::new()
            .with_router(ToolRouter::catch_all([(
                server_id.clone(),
                "rust".to_string(),
            )]))
            .with_extensions(HashMap::from([("rs".to_string(), "rust".to_string())]));
        translator.set_workspace_roots(vec![dir.path().to_path_buf()]);
        let translator = Arc::new(translator);
        let (client, mut fake_server) = fake_lsp_client();
        translator.register_client(server_id.clone(), client);

        let notification_cache = Arc::new(Mutex::new(NotificationCache::new()));

        let path = dir.path().join("lib.rs");
        fs::write(&path, "fn main() {}").unwrap();
        let path_str = path.to_string_lossy().to_string();

        let mcp_server = McplsServer::new(
            Arc::clone(&translator),
            Arc::clone(&notification_cache),
            Arc::from(vec![dir.path().to_path_buf()]),
            SubscriptionRegistry::new(),
            false,
            McpConfig::default(),
        );

        let call = {
            let params = Parameters(DiagnosticsParams {
                file_path: path_str,
            });
            tokio::spawn(async move { mcp_server.get_diagnostics(params).await })
        };

        let mut wire = BufReader::new(&mut fake_server.write_stdout);
        let opened = read_framed_message(&mut wire).await;
        assert_eq!(opened["method"], "textDocument/didOpen");
        let diag_request = read_framed_message(&mut wire).await;
        assert_eq!(diag_request["method"], "textDocument/diagnostic");
        write_response(
            &mut fake_server.read_half_stdin,
            &diag_request["id"],
            serde_json::json!({"kind": "full", "items": []}),
        )
        .await;

        let result = call.await.unwrap().unwrap();
        assert!(!result.0.indexing_in_progress);
    }

    /// S2 regression: the server is `Loading` when the pull *starts* but
    /// transitions to `Ready` before the pull *settles* -- sampling only
    /// after the pull (the original, buggy shape) would read `false` here,
    /// the exact false negative #445 exists to close. `indexingInProgress`
    /// must still read `true`, proving the "sample before" half of the OR
    /// actually does its job.
    #[tokio::test]
    async fn test_get_diagnostics_flags_indexing_in_progress_even_if_finished_mid_pull() {
        use std::collections::HashMap;
        use std::fs;

        use tempfile::TempDir;
        use tokio::io::BufReader;

        use crate::config::{ServerId, ToolRouter};
        use crate::test_lsp::{fake_lsp_client, read_framed_message, write_response};

        let server_id = ServerId::from("rust");
        let dir = TempDir::new().unwrap();
        let mut translator = Translator::new()
            .with_router(ToolRouter::catch_all([(
                server_id.clone(),
                "rust".to_string(),
            )]))
            .with_extensions(HashMap::from([("rs".to_string(), "rust".to_string())]));
        translator.set_workspace_roots(vec![dir.path().to_path_buf()]);
        let translator = Arc::new(translator);
        let (client, mut fake_server) = fake_lsp_client();
        translator.register_client(server_id.clone(), client);

        let notification_cache = Arc::new(Mutex::new(NotificationCache::new()));
        notification_cache.lock().await.observe_indexing_signal(
            &server_id,
            "experimental/serverStatus",
            Some(&serde_json::json!({"quiescent": false})),
        );

        let path = dir.path().join("lib.rs");
        fs::write(&path, "fn main() {}").unwrap();
        let path_str = path.to_string_lossy().to_string();

        let mcp_server = McplsServer::new(
            Arc::clone(&translator),
            Arc::clone(&notification_cache),
            Arc::from(vec![dir.path().to_path_buf()]),
            SubscriptionRegistry::new(),
            false,
            McpConfig::default(),
        );

        let call = {
            let params = Parameters(DiagnosticsParams {
                file_path: path_str,
            });
            tokio::spawn(async move { mcp_server.get_diagnostics(params).await })
        };

        let mut wire = BufReader::new(&mut fake_server.write_stdout);
        let opened = read_framed_message(&mut wire).await;
        assert_eq!(opened["method"], "textDocument/didOpen");
        let diag_request = read_framed_message(&mut wire).await;
        assert_eq!(diag_request["method"], "textDocument/diagnostic");

        // Indexing finishes while the pull request is in flight, before the
        // response is written -- the "before" sample already ran, so this
        // must not erase the signal.
        notification_cache.lock().await.observe_indexing_signal(
            &server_id,
            "experimental/serverStatus",
            Some(&serde_json::json!({"quiescent": true})),
        );

        write_response(
            &mut fake_server.read_half_stdin,
            &diag_request["id"],
            serde_json::json!({"kind": "full", "items": []}),
        )
        .await;

        let result = call.await.unwrap().unwrap();
        assert!(
            result.0.indexing_in_progress,
            "the pre-pull sample must still catch a server that finished indexing mid-pull"
        );
    }

    /// M2 regression: `get_diagnostics` must resolve the indexing-signal
    /// route from the *canonicalized* path, not the raw client-supplied one
    /// -- a symlink whose extension differs from its target (here `.txt` ->
    /// `.rs`) must still route to the same server the actual pull request
    /// canonicalizes and routes to internally, or the raw-path lookup would
    /// silently resolve no route at all (`plaintext` has none configured)
    /// and always read `indexingInProgress: false` regardless of the real
    /// server's state.
    #[cfg(unix)]
    #[tokio::test]
    async fn test_get_diagnostics_resolves_indexing_route_through_symlink() {
        use std::collections::HashMap;
        use std::fs;
        use std::os::unix::fs::symlink;

        use tempfile::TempDir;
        use tokio::io::BufReader;

        use crate::config::{ServerId, ToolRouter};
        use crate::test_lsp::{fake_lsp_client, read_framed_message, write_response};

        let server_id = ServerId::from("rust");
        let dir = TempDir::new().unwrap();
        let mut translator = Translator::new()
            .with_router(ToolRouter::catch_all([(
                server_id.clone(),
                "rust".to_string(),
            )]))
            .with_extensions(HashMap::from([("rs".to_string(), "rust".to_string())]));
        translator.set_workspace_roots(vec![dir.path().to_path_buf()]);
        let translator = Arc::new(translator);
        let (client, mut fake_server) = fake_lsp_client();
        translator.register_client(server_id.clone(), client);

        let notification_cache = Arc::new(Mutex::new(NotificationCache::new()));
        notification_cache.lock().await.observe_indexing_signal(
            &server_id,
            "experimental/serverStatus",
            Some(&serde_json::json!({"quiescent": false})),
        );

        let target = dir.path().join("target.rs");
        fs::write(&target, "fn main() {}").unwrap();
        let link = dir.path().join("link.txt");
        symlink(&target, &link).unwrap();
        let path_str = link.to_string_lossy().to_string();

        let mcp_server = McplsServer::new(
            Arc::clone(&translator),
            Arc::clone(&notification_cache),
            Arc::from(vec![dir.path().to_path_buf()]),
            SubscriptionRegistry::new(),
            false,
            McpConfig::default(),
        );

        let call = {
            let params = Parameters(DiagnosticsParams {
                file_path: path_str,
            });
            tokio::spawn(async move { mcp_server.get_diagnostics(params).await })
        };

        let mut wire = BufReader::new(&mut fake_server.write_stdout);
        let opened = read_framed_message(&mut wire).await;
        assert_eq!(opened["method"], "textDocument/didOpen");
        let diag_request = read_framed_message(&mut wire).await;
        assert_eq!(diag_request["method"], "textDocument/diagnostic");
        write_response(
            &mut fake_server.read_half_stdin,
            &diag_request["id"],
            serde_json::json!({"kind": "full", "items": []}),
        )
        .await;

        let result = call.await.unwrap().unwrap();
        assert!(
            result.0.indexing_in_progress,
            "route resolution must follow the symlink to its .rs target, not \
             stop at the .txt extension of the raw client path"
        );
    }

    #[tokio::test]
    async fn test_rename_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(RenameParams {
            position: PositionParams {
                file_path: test_file.to_str().unwrap().to_string(),
                line: 10,
                character: 5,
            },
            new_name: "new_name".to_string(),
        });

        let result = server.rename_symbol(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_completions_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(CompletionsParams {
            position: PositionParams {
                file_path: test_file.to_str().unwrap().to_string(),
                line: 10,
                character: 5,
            },
            trigger: None,
        });

        let result = server.get_completions(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_document_symbols_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(DocumentSymbolsParams {
            file_path: test_file.to_str().unwrap().to_string(),
        });

        let result = server.get_document_symbols(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_format_document_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(FormatDocumentParams {
            file_path: test_file.to_str().unwrap().to_string(),
            tab_size: 4,
            insert_spaces: true,
        });

        let result = server.format_document(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_workspace_symbol_search_tool_with_params() {
        let server = create_test_server();
        let params = Parameters(WorkspaceSymbolParams {
            query: "User".to_string(),
            kind_filter: None,
            limit: 100,
        });
        let result = server.workspace_symbol_search(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_code_actions_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(CodeActionsParams {
            file_path: test_file.to_str().unwrap().to_string(),
            range: RangeParams {
                start_line: 10,
                start_character: 5,
                end_line: 10,
                end_character: 15,
            },
            kind_filter: None,
        });
        let result = server.get_code_actions(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_prepare_call_hierarchy_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(PositionParams {
            file_path: test_file.to_str().unwrap().to_string(),
            line: 10,
            character: 5,
        });
        let result = server.prepare_call_hierarchy(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_incoming_calls_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let uri = url::Url::from_file_path(&test_file).unwrap().to_string();
        let item = serde_json::json!({
            "name": "test_function",
            "kind": 12,
            "uri": uri,
            "range": {
                "start": {"line": 0, "character": 0},
                "end": {"line": 0, "character": 10}
            },
            "selectionRange": {
                "start": {"line": 0, "character": 0},
                "end": {"line": 0, "character": 10}
            }
        });
        let params = Parameters(CallHierarchyCallsParams { item });
        let result = server.get_incoming_calls(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_outgoing_calls_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let uri = url::Url::from_file_path(&test_file).unwrap().to_string();
        let item = serde_json::json!({
            "name": "test_function",
            "kind": 12,
            "uri": uri,
            "range": {
                "start": {"line": 0, "character": 0},
                "end": {"line": 0, "character": 10}
            },
            "selectionRange": {
                "start": {"line": 0, "character": 0},
                "end": {"line": 0, "character": 10}
            }
        });
        let params = Parameters(CallHierarchyCallsParams { item });
        let result = server.get_outgoing_calls(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_cached_diagnostics_tool_with_params() {
        use std::fs;

        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");
        fs::write(&test_file, "fn main() {}").unwrap();

        let server = create_test_server_with_workspace_roots(
            false,
            McpConfig::default(),
            Arc::from([temp_dir.path().to_path_buf()]),
        );

        let params = Parameters(CachedDiagnosticsParams {
            file_path: test_file.to_str().unwrap().to_string(),
        });

        let result = server.get_cached_diagnostics(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert!(parsed.get("diagnostics").is_some());
    }

    /// `get_cached_diagnostics` end-to-end: a cache entry stored under the
    /// canonical URI (as `diagnostics_pump` would store it) must be found when
    /// requested via a textually non-canonical path -- proving `cached_diagnostics_uri`
    /// still canonicalizes correctly after the lock-scope split, and that
    /// `diagnostics_from_cache_entry` correctly maps a populated entry through
    /// the actual tool call (not just the unit-level helpers directly).
    #[tokio::test]
    async fn test_cached_diagnostics_tool_finds_entry_via_noncanonical_path() {
        use std::fs;

        use tempfile::TempDir;
        use url::Url;

        let temp_dir = TempDir::new().unwrap();
        let subdir = temp_dir.path().join("sub");
        fs::create_dir(&subdir).unwrap();
        let test_file = subdir.join("test.rs");
        fs::write(&test_file, "fn main() {}").unwrap();

        let server = create_test_server_with_workspace_roots(
            false,
            McpConfig::default(),
            Arc::from([temp_dir.path().to_path_buf()]),
        );

        let canonical_path = test_file.canonicalize().unwrap();
        let uri: lsp_types::Uri =
            lsp_types::Uri::from(Url::from_file_path(&canonical_path).unwrap().as_str());
        let diagnostic = lsp_types::Diagnostic {
            range: lsp_types::Range {
                start: lsp_types::Position {
                    line: 0,
                    character: 0,
                },
                end: lsp_types::Position {
                    line: 0,
                    character: 1,
                },
            },
            severity: Some(lsp_types::DiagnosticSeverity::Error),
            code: None,
            code_description: None,
            source: None,
            message: "cached error".to_string().into(),
            related_information: None,
            tags: None,
            data: None,
        };
        {
            let mut cache = server.context.notification_cache.lock().await;
            cache.store_diagnostics(
                &crate::config::ServerId::from("rust"),
                &uri,
                Some(1),
                vec![diagnostic],
            );
        }

        // Textually distinct from `test_file`, but canonicalizes to the same path.
        let noncanonical = subdir.join("..").join("sub").join("test.rs");
        let params = Parameters(CachedDiagnosticsParams {
            file_path: noncanonical.to_str().unwrap().to_string(),
        });

        let result = server.get_cached_diagnostics(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        let diagnostics = parsed.get("diagnostics").unwrap().as_array().unwrap();
        assert_eq!(diagnostics.len(), 1);
        assert_eq!(diagnostics[0].get("message").unwrap(), "cached error");
    }

    /// #290 gap: a cache-only read must resolve the *owner* server's
    /// negotiated encoding, not silently assume UTF-16. Registers the
    /// publishing server as UTF-8 and stores a diagnostic over a real
    /// multibyte line ("héllo") so a UTF-16 assumption would produce a
    /// visibly different (wrong) column: LSP byte offset 3 is MCP column 3
    /// under the registered server's UTF-8 encoding, but would read as raw
    /// column 4 (unconverted passthrough) under the UTF-16 default tested in
    /// `test_cached_diagnostics_tool_no_owner_falls_back_to_utf16` below.
    #[tokio::test]
    async fn test_cached_diagnostics_tool_uses_registered_owner_encoding() {
        use std::fs;

        use tempfile::TempDir;
        use url::Url;

        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");
        fs::write(&test_file, "héllo").unwrap();

        let server = create_test_server_with_workspace_roots(
            false,
            McpConfig::default(),
            Arc::from([temp_dir.path().to_path_buf()]),
        );
        let owner = crate::config::ServerId::from("rust");
        server.context.translator.register_server(
            owner.clone(),
            crate::lsp::LspServer::new_for_test_with_encoding(
                lsp_types::ServerCapabilities::default(),
                lsp_types::PositionEncodingKind::UTF8,
            ),
        );

        let canonical_path = test_file.canonicalize().unwrap();
        let uri: lsp_types::Uri =
            lsp_types::Uri::from(Url::from_file_path(&canonical_path).unwrap().as_str());
        let diagnostic = lsp_types::Diagnostic {
            range: lsp_types::Range {
                start: lsp_types::Position {
                    line: 0,
                    character: 0,
                },
                end: lsp_types::Position {
                    line: 0,
                    character: 3,
                },
            },
            severity: Some(lsp_types::DiagnosticSeverity::Error),
            code: None,
            code_description: None,
            source: None,
            message: "multibyte range".to_string().into(),
            related_information: None,
            tags: None,
            data: None,
        };
        {
            let mut cache = server.context.notification_cache.lock().await;
            cache.store_diagnostics(&owner, &uri, Some(1), vec![diagnostic]);
        }

        let params = Parameters(CachedDiagnosticsParams {
            file_path: test_file.to_str().unwrap().to_string(),
        });
        let result = server.get_cached_diagnostics(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        let diagnostics = parsed.get("diagnostics").unwrap().as_array().unwrap();
        assert_eq!(
            diagnostics[0]["range"]["end"]["character"], 3,
            "byte offset 3 on \"héllo\" is UTF-16 column 3 when converted against the \
             registered UTF-8 owner"
        );
    }

    /// Companion to the test above: when no server is registered under the
    /// cached entry's owner id (or no owner is tracked at all),
    /// `get_cached_diagnostics` must fall back to UTF-16 -- a raw,
    /// unconverted passthrough -- rather than panicking or guessing.
    #[tokio::test]
    async fn test_cached_diagnostics_tool_no_owner_falls_back_to_utf16() {
        use std::fs;

        use tempfile::TempDir;
        use url::Url;

        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");
        fs::write(&test_file, "héllo").unwrap();

        let server = create_test_server_with_workspace_roots(
            false,
            McpConfig::default(),
            Arc::from([temp_dir.path().to_path_buf()]),
        );
        // Deliberately not registered with `translator.register_server`.
        let owner = crate::config::ServerId::from("rust");

        let canonical_path = test_file.canonicalize().unwrap();
        let uri: lsp_types::Uri =
            lsp_types::Uri::from(Url::from_file_path(&canonical_path).unwrap().as_str());
        let diagnostic = lsp_types::Diagnostic {
            range: lsp_types::Range {
                start: lsp_types::Position {
                    line: 0,
                    character: 0,
                },
                end: lsp_types::Position {
                    line: 0,
                    character: 3,
                },
            },
            severity: Some(lsp_types::DiagnosticSeverity::Error),
            code: None,
            code_description: None,
            source: None,
            message: "multibyte range".to_string().into(),
            related_information: None,
            tags: None,
            data: None,
        };
        {
            let mut cache = server.context.notification_cache.lock().await;
            cache.store_diagnostics(&owner, &uri, Some(1), vec![diagnostic]);
        }

        let params = Parameters(CachedDiagnosticsParams {
            file_path: test_file.to_str().unwrap().to_string(),
        });
        let result = server.get_cached_diagnostics(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        let diagnostics = parsed.get("diagnostics").unwrap().as_array().unwrap();
        assert_eq!(
            diagnostics[0]["range"]["end"]["character"], 4,
            "with no registered owner, must fall back to UTF-16 (raw passthrough: \
             character + 1), not the UTF-8-correct column"
        );
    }

    /// #359: `get_cached_diagnostics` must surface `pushNotificationsDegraded`
    /// when the cached entry's owning server was marked degraded (respawned
    /// with its push notifications discarded), so a caller can tell the
    /// result may be stale rather than treating it as current.
    #[tokio::test]
    async fn test_cached_diagnostics_tool_flags_push_degraded_owner() {
        use std::collections::HashMap;
        use std::fs;

        use tempfile::TempDir;
        use url::Url;

        use crate::config::{ServerId, ToolRouter};

        // A router + extension map is required: the degraded flag is keyed
        // on `Translator::diagnostics_route_id_for_path` (the file's
        // *routed* server, resolved from its detected language), not on
        // `NotificationCache::diagnostics_owner` -- see #359's C1 fix. This
        // is a fast unit test of that wiring alone; the slower
        // `..._after_real_respawn` test below covers the full path through
        // an actual crash + respawn.
        let owner = ServerId::from("rust");
        let notification_cache = Arc::new(Mutex::new(NotificationCache::new()));
        let translator = Arc::new(
            Translator::new()
                .with_router(ToolRouter::catch_all([(owner.clone(), "rust".to_string())]))
                .with_extensions(HashMap::from([("rs".to_string(), "rust".to_string())])),
        );
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");
        fs::write(&test_file, "fn main() {}").unwrap();

        let server = McplsServer::new(
            translator,
            Arc::clone(&notification_cache),
            Arc::from([temp_dir.path().to_path_buf()]),
            SubscriptionRegistry::new(),
            false,
            McpConfig::default(),
        );

        let canonical_path = test_file.canonicalize().unwrap();
        let uri: lsp_types::Uri =
            lsp_types::Uri::from(Url::from_file_path(&canonical_path).unwrap().as_str());
        {
            let mut cache = notification_cache.lock().await;
            cache.store_diagnostics(&owner, &uri, Some(1), vec![]);
            cache.mark_push_degraded(&owner);
        }

        let params = Parameters(CachedDiagnosticsParams {
            file_path: test_file.to_str().unwrap().to_string(),
        });
        let result = server.get_cached_diagnostics(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert_eq!(parsed.get("pushNotificationsDegraded").unwrap(), true);
    }

    /// #445: `get_cached_diagnostics` must surface the same
    /// `indexingInProgress` signal `get_diagnostics` does -- a cache-only
    /// read is exactly as vulnerable to reflecting a partial index as the
    /// pull-model one.
    #[tokio::test]
    async fn test_cached_diagnostics_tool_flags_indexing_in_progress() {
        use std::collections::HashMap;
        use std::fs;

        use tempfile::TempDir;

        use crate::config::{ServerId, ToolRouter};

        let owner = ServerId::from("rust");
        let notification_cache = Arc::new(Mutex::new(NotificationCache::new()));
        notification_cache.lock().await.observe_indexing_signal(
            &owner,
            "experimental/serverStatus",
            Some(&serde_json::json!({"quiescent": false})),
        );
        let translator = Arc::new(
            Translator::new()
                .with_router(ToolRouter::catch_all([(owner.clone(), "rust".to_string())]))
                .with_extensions(HashMap::from([("rs".to_string(), "rust".to_string())])),
        );
        let temp_dir = TempDir::new().unwrap();
        let server = McplsServer::new(
            translator,
            Arc::clone(&notification_cache),
            Arc::from(vec![temp_dir.path().to_path_buf()]),
            SubscriptionRegistry::new(),
            false,
            McpConfig::default(),
        );

        let test_file = temp_dir.path().join("test.rs");
        fs::write(&test_file, "fn main() {}").unwrap();

        let params = Parameters(CachedDiagnosticsParams {
            file_path: test_file.to_str().unwrap().to_string(),
        });
        let result = server.get_cached_diagnostics(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert_eq!(parsed.get("indexingInProgress").unwrap(), true);
    }

    /// M2 counterpart for `get_cached_diagnostics`: route resolution must
    /// follow a symlink to its target's extension (`.txt` -> `.rs`), not
    /// stop at the raw client path's extension, or this would silently
    /// resolve no route (`plaintext` has none configured) and always read
    /// `indexingInProgress: false` regardless of the real server's state.
    #[cfg(unix)]
    #[tokio::test]
    async fn test_cached_diagnostics_tool_resolves_indexing_route_through_symlink() {
        use std::collections::HashMap;
        use std::fs;
        use std::os::unix::fs::symlink;

        use tempfile::TempDir;

        use crate::config::{ServerId, ToolRouter};

        let owner = ServerId::from("rust");
        let notification_cache = Arc::new(Mutex::new(NotificationCache::new()));
        notification_cache.lock().await.observe_indexing_signal(
            &owner,
            "experimental/serverStatus",
            Some(&serde_json::json!({"quiescent": false})),
        );
        let translator = Arc::new(
            Translator::new()
                .with_router(ToolRouter::catch_all([(owner.clone(), "rust".to_string())]))
                .with_extensions(HashMap::from([("rs".to_string(), "rust".to_string())])),
        );
        let temp_dir = TempDir::new().unwrap();
        let server = McplsServer::new(
            translator,
            Arc::clone(&notification_cache),
            Arc::from(vec![temp_dir.path().to_path_buf()]),
            SubscriptionRegistry::new(),
            false,
            McpConfig::default(),
        );

        let target = temp_dir.path().join("target.rs");
        fs::write(&target, "fn main() {}").unwrap();
        let link = temp_dir.path().join("link.txt");
        symlink(&target, &link).unwrap();

        let params = Parameters(CachedDiagnosticsParams {
            file_path: link.to_str().unwrap().to_string(),
        });
        let result = server.get_cached_diagnostics(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert_eq!(
            parsed.get("indexingInProgress").unwrap(),
            true,
            "route resolution must follow the symlink to its .rs target, not \
             stop at the .txt extension of the raw client path"
        );
    }

    /// #359 regression: drives the *actual* `respawn_if_dead` path (not a
    /// hand-ordered `store_diagnostics`-then-`mark_push_degraded` call
    /// sequence, which a real respawn never produces, since
    /// `clear_server_diagnostics` removes `diagnostics_owner` for the
    /// crashed server before `mark_push_degraded` runs) and asserts through
    /// the real `get_cached_diagnostics` MCP handler that
    /// `pushNotificationsDegraded` comes back `true` afterward -- proving
    /// the flag is actually reachable in production, keyed on the stable
    /// routing identity rather than the cleared per-URI ownership map.
    ///
    /// `#[cfg(unix)]`: the fake LSP server is a hand-written `sh` script, no
    /// equivalent on Windows -- mirrors the gating on the respawn tests in
    /// `bridge::translator::respawn`.
    #[cfg(unix)]
    #[tokio::test]
    async fn test_cached_diagnostics_tool_flags_push_degraded_after_real_respawn() {
        use std::collections::HashMap;
        use std::fs;

        use tempfile::TempDir;

        use crate::config::{LspServerConfig, ServerId, ToolRouter};
        use crate::lsp::{LspServer, ServerInitConfig};
        use crate::test_lsp::with_read_preamble;

        let dir = TempDir::new().unwrap();
        let script_path = dir.path().join("crash_after_init.sh");
        // Answers the `initialize` handshake, then exits ~0.3s later --
        // stands in for "was alive, then crashed" without a real language
        // server binary (same shape as `respawn::tests::respawn_tests`'
        // `write_crash_after_init_script`, duplicated here since that
        // module's test helpers are private to it).
        fs::write(
            &script_path,
            with_read_preamble(
                r#"body='{"jsonrpc":"2.0","id":1,"result":{"capabilities":{}}}'
printf 'Content-Length: %d\r\n\r\n%s' ${#body} "$body"
sleep 0.3
"#,
            ),
        )
        .unwrap();

        let id = ServerId::from("rust");
        let config = ServerInitConfig {
            server_config: LspServerConfig {
                language_id: "rust".to_string(),
                command: "sh".to_string(),
                args: vec![script_path.to_string_lossy().to_string()],
                env: HashMap::new(),
                file_patterns: vec![],
                initialization_options: None,
                timeout_seconds: 5,
                request_timeout_seconds: 5,
                heuristics: None,
                name: Some("rust".to_string()),
                handles: None,
                indexing: crate::bridge::IndexingPolicy::Auto,
            },
            workspace_roots: vec![],
            initialization_options: None,
            position_encodings: vec!["utf-8".to_string(), "utf-16".to_string()],
            notification_tx: None,
        };

        let seed = LspServer::spawn(config.clone()).await.unwrap();

        let notification_cache = Arc::new(Mutex::new(NotificationCache::new()));
        let translator = Arc::new(
            Translator::new()
                .with_router(ToolRouter::catch_all([(id.clone(), "rust".to_string())]))
                .with_notification_cache(Arc::clone(&notification_cache))
                .with_extensions(HashMap::from([("rs".to_string(), "rust".to_string())])),
        );
        translator.register_client(id.clone(), seed.client().clone());
        translator.register_server(id.clone(), seed);
        translator.register_server_config(id.clone(), config);

        let server = McplsServer::new(
            Arc::clone(&translator),
            Arc::clone(&notification_cache),
            Arc::from([dir.path().to_path_buf()]),
            SubscriptionRegistry::new(),
            false,
            McpConfig::default(),
        );

        // Drive real respawn attempts (a no-op while the seed is still
        // alive) until one actually observes the crash and marks the
        // diagnostics-route server degraded -- bounded so a broken script
        // fails the test instead of hanging it.
        let deadline = tokio::time::Instant::now() + tokio::time::Duration::from_secs(2);
        loop {
            let _ = translator.respawn_if_dead(&id).await;
            if notification_cache.lock().await.is_push_degraded(&id) {
                break;
            }
            assert!(
                tokio::time::Instant::now() < deadline,
                "server was never observed dead and respawned within the deadline"
            );
            tokio::time::sleep(tokio::time::Duration::from_millis(20)).await;
        }

        let test_file = dir.path().join("main.rs");
        fs::write(&test_file, "fn main() {}").unwrap();
        let params = Parameters(CachedDiagnosticsParams {
            file_path: test_file.to_str().unwrap().to_string(),
        });
        let result = server.get_cached_diagnostics(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert_eq!(
            parsed.get("pushNotificationsDegraded").unwrap(),
            true,
            "a real respawn must be observable through the actual MCP handler, got: {parsed}"
        );
    }

    /// Companion to the test above: an entry from a server that was never
    /// marked degraded must report `pushNotificationsDegraded: false`.
    #[tokio::test]
    async fn test_cached_diagnostics_tool_reports_not_degraded_by_default() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");
        std::fs::write(&test_file, "fn main() {}").unwrap();

        let server = create_test_server_with_workspace_roots(
            false,
            McpConfig::default(),
            Arc::from([temp_dir.path().to_path_buf()]),
        );

        let params = Parameters(CachedDiagnosticsParams {
            file_path: test_file.to_str().unwrap().to_string(),
        });
        let result = server.get_cached_diagnostics(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert_eq!(parsed.get("pushNotificationsDegraded").unwrap(), false);
    }

    #[tokio::test]
    async fn test_cached_diagnostics_tool_nonexistent_file() {
        #[cfg(windows)]
        let root = PathBuf::from(r"C:\");
        #[cfg(not(windows))]
        let root = PathBuf::from("/");
        let server =
            create_test_server_with_workspace_roots(false, McpConfig::default(), Arc::from([root]));
        let params = Parameters(CachedDiagnosticsParams {
            file_path: "/nonexistent/file.rs".to_string(),
        });

        let result = server.get_cached_diagnostics(params).await;
        let err = result.unwrap_err();
        assert!(
            err.message.contains("file I/O error"),
            "expected a file I/O error for a nonexistent path, got: {}",
            err.message
        );
    }

    #[tokio::test]
    async fn test_server_logs_tool_with_default_params() {
        let server = create_test_server();
        let params = Parameters(ServerLogsParams {
            limit: 50,
            min_level: None,
        });

        let result = server.get_server_logs(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert!(parsed.get("logs").is_some());
    }

    #[tokio::test]
    async fn test_server_logs_tool_with_error_level() {
        let server = create_test_server();
        let params = Parameters(ServerLogsParams {
            limit: 10,
            min_level: Some("error".to_string()),
        });

        let result = server.get_server_logs(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        let logs = parsed.get("logs").unwrap().as_array().unwrap();
        assert_eq!(logs.len(), 0);
    }

    #[tokio::test]
    async fn test_server_logs_tool_with_warning_level() {
        let server = create_test_server();
        let params = Parameters(ServerLogsParams {
            limit: 100,
            min_level: Some("warning".to_string()),
        });

        let result = server.get_server_logs(params).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_server_logs_tool_with_info_level() {
        let server = create_test_server();
        let params = Parameters(ServerLogsParams {
            limit: 50,
            min_level: Some("info".to_string()),
        });

        let result = server.get_server_logs(params).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_server_logs_tool_with_debug_level() {
        let server = create_test_server();
        let params = Parameters(ServerLogsParams {
            limit: 20,
            min_level: Some("debug".to_string()),
        });

        let result = server.get_server_logs(params).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_server_logs_tool_with_invalid_level() {
        let server = create_test_server();
        let params = Parameters(ServerLogsParams {
            limit: 10,
            min_level: Some("invalid_level".to_string()),
        });

        let result = server.get_server_logs(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_server_logs_tool_with_zero_limit() {
        let server = create_test_server();
        let params = Parameters(ServerLogsParams {
            limit: 0,
            min_level: None,
        });

        let result = server.get_server_logs(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        let logs = parsed.get("logs").unwrap().as_array().unwrap();
        assert_eq!(logs.len(), 0);
    }

    #[tokio::test]
    async fn test_server_messages_tool_with_default_params() {
        let server = create_test_server();
        let params = Parameters(ServerMessagesParams { limit: 20 });

        let result = server.get_server_messages(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert!(parsed.get("messages").is_some());
    }

    #[tokio::test]
    async fn test_server_messages_tool_with_custom_limit() {
        let server = create_test_server();
        let params = Parameters(ServerMessagesParams { limit: 5 });

        let result = server.get_server_messages(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        let messages = parsed.get("messages").unwrap().as_array().unwrap();
        assert_eq!(messages.len(), 0);
    }

    #[tokio::test]
    async fn test_server_messages_tool_with_zero_limit() {
        let server = create_test_server();
        let params = Parameters(ServerMessagesParams { limit: 0 });

        let result = server.get_server_messages(params).await;
        assert!(result.is_ok());

        let json_str = result.unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        let messages = parsed.get("messages").unwrap().as_array().unwrap();
        assert_eq!(messages.len(), 0);
    }

    #[tokio::test]
    async fn test_server_messages_tool_with_large_limit() {
        let server = create_test_server();
        let params = Parameters(ServerMessagesParams { limit: 1000 });

        let result = server.get_server_messages(params).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_signature_help_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(PositionParams {
            file_path: test_file.to_str().unwrap().to_string(),
            line: 10,
            character: 5,
        });

        let result = server.get_signature_help(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_go_to_implementation_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(PositionParams {
            file_path: test_file.to_str().unwrap().to_string(),
            line: 10,
            character: 5,
        });

        let result = server.go_to_implementation(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_go_to_type_definition_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(PositionParams {
            file_path: test_file.to_str().unwrap().to_string(),
            line: 10,
            character: 5,
        });

        let result = server.go_to_type_definition(params).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_get_inlay_hints_tool_with_params() {
        let (server, _temp_dir, test_file) = create_test_server_with_real_file();
        let params = Parameters(InlayHintsParams {
            file_path: test_file.to_str().unwrap().to_string(),
            range: RangeParams {
                start_line: 1,
                start_character: 1,
                end_line: 10,
                end_character: 1,
            },
        });

        let result = server.get_inlay_hints(params).await;
        assert!(result.is_err());
    }

    // ------------------------------------------------------------------
    // Tool annotation tests
    // ------------------------------------------------------------------

    /// Every registered tool must carry `ToolAnnotations` (plus the current-spec
    /// `Tool.title`) so MCP clients can decide when to skip confirmation dialogs
    /// (read-only tools) or must prompt the user (destructive tools) without
    /// invoking the tool first. Sourced from `build_tool_router(None).list_all()`
    /// (not a hand-written list of tool names). This test alone does not catch a
    /// future *mutating* tool that omits `annotations(...)`: `build_tool_router()`'s
    /// central pass (see its doc comment) blanket-labels any such tool
    /// read-only rather than leaving it `None`, so the hint assertions above
    /// always pass. `test_tool_annotation_classifications_match_intent` below
    /// forces a new mutating tool to write down an explicit classification,
    /// though it does not verify that classification is truthful.
    #[test]
    fn test_all_tools_carry_annotations() {
        let tools = McplsServer::build_tool_router(None).list_all();
        assert!(!tools.is_empty(), "no tools registered");

        for tool in &tools {
            assert!(
                tool.title.is_some(),
                "tool `{}` is missing a top-level title",
                tool.name
            );
            let annotations = tool
                .annotations
                .as_ref()
                .unwrap_or_else(|| panic!("tool `{}` is missing annotations", tool.name));
            assert!(
                annotations.title.is_some(),
                "tool `{}` is missing an annotations title",
                tool.name
            );
            assert!(
                annotations.read_only_hint.is_some(),
                "tool `{}` is missing read_only_hint",
                tool.name
            );
            assert!(
                annotations.destructive_hint.is_some(),
                "tool `{}` is missing destructive_hint",
                tool.name
            );
            assert!(
                annotations.idempotent_hint.is_some(),
                "tool `{}` is missing idempotent_hint",
                tool.name
            );
        }
    }

    /// Value-level regression guard for every tool's `(read_only, destructive,
    /// idempotent)` classification, sourced from the live `tool_router` (not
    /// per-tool `*_tool_attr()` calls) so the expected-tool table itself is
    /// checked against the actual registered count.
    #[test]
    fn test_tool_annotation_classifications_match_intent() {
        let tools = McplsServer::build_tool_router(None).list_all();
        let by_name: std::collections::HashMap<&str, &rmcp::model::ToolAnnotations> = tools
            .iter()
            .map(|tool| {
                (
                    tool.name.as_ref(),
                    tool.annotations
                        .as_ref()
                        .unwrap_or_else(|| panic!("tool `{}` is missing annotations", tool.name)),
                )
            })
            .collect();

        // (tool name, read_only_hint, destructive_hint, idempotent_hint)
        let expected: &[(&str, bool, bool, bool)] = &[
            ("get_hover", true, false, true),
            ("get_definition", true, false, true),
            ("get_references", true, false, true),
            ("get_diagnostics", true, false, true),
            ("rename_symbol", true, false, true),
            ("get_completions", true, false, true),
            ("get_document_symbols", true, false, true),
            ("format_document", true, false, true),
            ("workspace_symbol_search", true, false, true),
            ("get_code_actions", true, false, true),
            ("prepare_call_hierarchy", true, false, true),
            ("get_incoming_calls", true, false, true),
            ("get_outgoing_calls", true, false, true),
            ("get_cached_diagnostics", true, false, true),
            ("get_server_logs", true, false, true),
            ("get_server_messages", true, false, true),
            ("get_signature_help", true, false, true),
            ("go_to_implementation", true, false, true),
            ("go_to_type_definition", true, false, true),
            ("get_inlay_hints", true, false, true),
        ];

        assert_eq!(
            expected.len(),
            tools.len(),
            "expected-classification table is out of sync with the registered tool count"
        );

        for (name, read_only, destructive, idempotent) in expected {
            let annotations = by_name
                .get(name)
                .unwrap_or_else(|| panic!("tool `{name}` not found in tool_router"));
            assert_eq!(
                annotations.read_only_hint,
                Some(*read_only),
                "tool `{name}` read_only_hint mismatch"
            );
            assert_eq!(
                annotations.destructive_hint,
                Some(*destructive),
                "tool `{name}` destructive_hint mismatch"
            );
            assert_eq!(
                annotations.idempotent_hint,
                Some(*idempotent),
                "tool `{name}` idempotent_hint mismatch"
            );
        }
    }

    // ------------------------------------------------------------------
    // Resource handler tests (logic-level, avoiding rmcp::service::RequestContext
    // which requires a live Peer with private fields)
    // ------------------------------------------------------------------

    /// `list_resources` returns an empty vec for a fresh translator with no open documents.
    #[tokio::test]
    async fn test_list_resources_returns_empty_when_no_open_documents() {
        let server = create_test_server();
        let empty = server.context.translator.open_document_paths().is_empty();
        assert!(empty);
    }

    // ------------------------------------------------------------------
    // `paginate_resource_paths` (pagination logic behind `list_resources`)
    // ------------------------------------------------------------------

    fn paths(n: usize) -> Vec<PathBuf> {
        (0..n)
            .map(|i| PathBuf::from(format!("/f{i:04}.rs")))
            .collect()
    }

    #[test]
    fn test_paginate_first_page_under_page_size_has_no_next_cursor() {
        let p = paths(5);
        let (page, next_cursor) = paginate_resource_paths(&p, None, 100).unwrap();
        assert_eq!(page.len(), 5);
        assert!(next_cursor.is_none());
    }

    #[test]
    fn test_paginate_splits_across_pages_when_over_page_size() {
        let p = paths(250);

        let (page1, cursor1) = paginate_resource_paths(&p, None, 100).unwrap();
        assert_eq!(page1.len(), 100);
        assert_eq!(page1.first(), p.first());
        assert_eq!(cursor1.as_deref(), Some("100"));

        let (page2, cursor2) = paginate_resource_paths(&p, cursor1.as_deref(), 100).unwrap();
        assert_eq!(page2.len(), 100);
        assert_eq!(page2.first(), Some(&p[100]));
        assert_eq!(cursor2.as_deref(), Some("200"));

        let (page3, cursor3) = paginate_resource_paths(&p, cursor2.as_deref(), 100).unwrap();
        assert_eq!(page3.len(), 50);
        assert_eq!(page3.first(), Some(&p[200]));
        assert!(cursor3.is_none());
    }

    #[test]
    fn test_paginate_rejects_malformed_cursor() {
        let p = paths(5);
        let result = paginate_resource_paths(&p, Some("not-a-number"), 100);
        assert!(result.is_err());
    }

    #[test]
    fn test_paginate_out_of_range_cursor_yields_empty_page_not_error() {
        let p = paths(5);
        let (page, next_cursor) = paginate_resource_paths(&p, Some("9999"), 100).unwrap();
        assert!(page.is_empty());
        assert!(next_cursor.is_none());
    }

    /// Regression for a client-controlled cursor near `usize::MAX`: `start + page_size`
    /// must not panic (debug) or silently wrap to a bogus cursor (release).
    #[test]
    fn test_paginate_cursor_near_usize_max_does_not_overflow() {
        let p = paths(5);
        let cursor = usize::MAX.to_string();
        let (page, next_cursor) = paginate_resource_paths(&p, Some(&cursor), 100).unwrap();
        assert!(page.is_empty());
        assert!(next_cursor.is_none());
    }

    /// `list_resources` overrides `next_cursor` via struct-update syntax on top of
    /// `ListResourcesResult::with_all_items` (which always sets it to `None`) --
    /// confirm the explicit field wins and survives serialization under its
    /// wire name (`nextCursor`, camelCase per `rmcp`'s `paginated_result!`).
    #[test]
    fn test_list_resources_result_next_cursor_survives_struct_update_override() {
        let result = ListResourcesResult {
            next_cursor: Some("100".to_string()),
            ..ListResourcesResult::with_all_items(Vec::new())
        };
        assert_eq!(result.next_cursor.as_deref(), Some("100"));

        let json = serde_json::to_value(&result).unwrap();
        assert_eq!(json.get("nextCursor").unwrap(), "100");
    }

    // ------------------------------------------------------------------
    // `ResourceDiagnosticsResponse` (tracked-vs-untracked shape behind
    // `read_resource`)
    // ------------------------------------------------------------------

    fn sample_diagnostic_info(diagnostics: Vec<lsp_types::Diagnostic>) -> DiagnosticInfo {
        use url::Url;

        let uri: lsp_types::Uri =
            lsp_types::Uri::from(Url::parse("file:///sample.rs").unwrap().as_str());
        DiagnosticInfo {
            uri,
            version: Some(1),
            diagnostics,
        }
    }

    #[test]
    fn test_resource_diagnostics_response_untracked_is_not_tracked_and_empty() {
        let response = ResourceDiagnosticsResponse::new(false, None, false, false);
        assert!(!response.tracked);
        assert!(response.version.is_none());
        assert!(response.diagnostics.is_empty());

        // #132's contract is the wire shape, not the Rust struct -- assert the JSON directly.
        let json = serde_json::to_value(&response).unwrap();
        assert_eq!(json["tracked"], false);
        assert!(json["version"].is_null());
        assert_eq!(json["diagnostics"], serde_json::json!([]));
    }

    #[test]
    fn test_resource_diagnostics_response_tracked_but_no_cache_entry_is_clean() {
        let response = ResourceDiagnosticsResponse::new(true, None, false, false);
        assert!(response.tracked);
        assert!(response.version.is_none());
        assert!(response.diagnostics.is_empty());

        let json = serde_json::to_value(&response).unwrap();
        assert_eq!(json["tracked"], true);
        assert!(json["version"].is_null());
        assert_eq!(json["diagnostics"], serde_json::json!([]));
    }

    #[test]
    fn test_resource_diagnostics_response_tracked_with_diagnostics() {
        let entry = sample_diagnostic_info(vec![lsp_types::Diagnostic {
            range: lsp_types::Range {
                start: lsp_types::Position {
                    line: 0,
                    character: 0,
                },
                end: lsp_types::Position {
                    line: 0,
                    character: 1,
                },
            },
            severity: Some(lsp_types::DiagnosticSeverity::Error),
            code: None,
            code_description: None,
            source: None,
            message: "boom".to_string().into(),
            related_information: None,
            tags: None,
            data: None,
        }]);
        let response = ResourceDiagnosticsResponse::new(true, Some(&entry), false, false);
        assert!(response.tracked);
        assert_eq!(response.version, Some(1));
        assert_eq!(response.diagnostics.len(), 1);
        assert_eq!(response.diagnostics[0].message, "boom".into());

        let json = serde_json::to_value(&response).unwrap();
        assert_eq!(json["tracked"], true);
        assert_eq!(json["version"], 1);
        assert_eq!(json["diagnostics"][0]["message"], "boom");
    }

    /// A path `read_resource` never opened reports `is_document_open() == false`
    /// -- one of the two inputs `build_resource_diagnostics_response` ORs together.
    #[tokio::test]
    async fn test_read_resource_untracked_path_is_not_open() {
        let server = create_test_server();
        let tracked = server
            .context
            .translator
            .is_document_open(std::path::Path::new("/never/opened.rs"));
        assert!(!tracked);
    }

    #[test]
    fn test_build_resource_diagnostics_response_neither_open_nor_cached_is_untracked() {
        let response = build_resource_diagnostics_response(false, None, false, false);
        assert!(!response.tracked);
        assert!(response.diagnostics.is_empty());
    }

    #[test]
    fn test_build_resource_diagnostics_response_open_but_uncached_is_tracked() {
        let response = build_resource_diagnostics_response(true, None, false, false);
        assert!(response.tracked);
        assert!(response.diagnostics.is_empty());
    }

    /// Regression: an LSP server can publish diagnostics for a file mcpls never
    /// explicitly opened via `DocumentTracker` (e.g. one rust-analyzer analyzes
    /// transitively). `tracked` must still be `true` here -- deriving it from
    /// `document_open` alone would report `tracked: false` while `diagnostics`
    /// is non-empty, contradicting the documented "untracked implies empty
    /// diagnostics" contract.
    #[test]
    fn test_build_resource_diagnostics_response_cached_but_unopened_is_tracked() {
        let entry = sample_diagnostic_info(vec![lsp_types::Diagnostic {
            range: lsp_types::Range {
                start: lsp_types::Position {
                    line: 0,
                    character: 0,
                },
                end: lsp_types::Position {
                    line: 0,
                    character: 1,
                },
            },
            severity: Some(lsp_types::DiagnosticSeverity::Warning),
            code: None,
            code_description: None,
            source: None,
            message: "transitively analyzed".to_string().into(),
            related_information: None,
            tags: None,
            data: None,
        }]);

        let response = build_resource_diagnostics_response(false, Some(&entry), false, false);
        assert!(
            response.tracked,
            "a cached diagnostics entry must make the response tracked, \
             even for a file that was never explicitly opened"
        );
        assert_eq!(response.diagnostics.len(), 1);
    }

    /// #359 S2: `read_resource`'s response carries the same
    /// `pushNotificationsDegraded` signal as `get_cached_diagnostics`, since
    /// both serve the same cache and go dark the same way after a respawn.
    #[test]
    fn test_build_resource_diagnostics_response_flags_push_degraded() {
        let response = build_resource_diagnostics_response(false, None, true, false);
        assert!(response.push_notifications_degraded);

        let json = serde_json::to_value(&response).unwrap();
        assert_eq!(json["pushNotificationsDegraded"], true);
    }

    /// #445 counterpart to the push-degraded test above: `read_resource`'s
    /// response carries the same `indexingInProgress` signal
    /// `get_diagnostics`/`get_cached_diagnostics` surface.
    #[test]
    fn test_build_resource_diagnostics_response_flags_indexing_in_progress() {
        let response = build_resource_diagnostics_response(false, None, false, true);
        assert!(response.indexing_in_progress);

        let json = serde_json::to_value(&response).unwrap();
        assert_eq!(json["indexingInProgress"], true);
    }

    /// `parse_uri` rejects `file://` scheme — ensures `read_resource` would return an error.
    #[test]
    fn test_read_resource_rejects_file_scheme() {
        let result = parse_uri("file:///some/file.rs");
        assert!(result.is_err());
    }

    /// `parse_uri` rejects `https://` scheme.
    #[test]
    fn test_subscribe_rejects_https_scheme() {
        let result = parse_uri("https://evil.com/file.rs");
        assert!(result.is_err());
    }

    /// A request with no `http::request::Parts` extension at all (e.g. served
    /// over stdio) is never mistaken for a stateless HTTP request, even with
    /// an empty `_meta`.
    #[test]
    fn test_is_stateless_http_request_false_without_http_extension() {
        let extensions = rmcp::model::Extensions::new();
        let meta = rmcp::model::RequestMetaObject::new();
        assert!(!super::is_stateless_http_request(&extensions, &meta));
    }

    /// #482 regression: a stdio request (no `Parts` extension) whose `_meta`
    /// carries discover-lifecycle keys must still be allowed through -- see
    /// [`super::is_stateless_http_request`]'s docs for why.
    #[test]
    fn test_is_stateless_http_request_false_without_http_extension_even_with_discover_meta() {
        let extensions = rmcp::model::Extensions::new();
        let meta = rmcp::model::RequestMetaObject::with_client_context(
            rmcp::model::ProtocolVersion::V_2025_03_26,
            rmcp::model::Implementation::default(),
            rmcp::model::ClientCapabilities::default(),
        );
        assert!(!super::is_stateless_http_request(&extensions, &meta));
    }

    /// #482: an HTTP-served request that never echoes `Mcp-Session-Id` is
    /// detected as stateless -- the secondary, unioned signal (see
    /// [`super::request_uses_discover_lifecycle_meta`] for the primary,
    /// exhaustive one).
    #[cfg(feature = "transport-http")]
    #[test]
    fn test_is_stateless_http_request_true_without_session_header() {
        let (parts, ()) = axum::http::Request::builder()
            .body(())
            .unwrap()
            .into_parts();
        let mut extensions = rmcp::model::Extensions::new();
        extensions.insert(parts);
        let meta = rmcp::model::RequestMetaObject::new();
        assert!(super::is_stateless_http_request(&extensions, &meta));
    }

    /// A request that echoes an `Mcp-Session-Id` header and carries no
    /// discover-lifecycle `_meta` is not flagged -- the ordinary legacy
    /// session case.
    #[cfg(feature = "transport-http")]
    #[test]
    fn test_is_stateless_http_request_false_with_session_header_and_no_discover_meta() {
        let (mut parts, ()) = axum::http::Request::builder()
            .body(())
            .unwrap()
            .into_parts();
        parts.headers.insert(
            rmcp::transport::common::http_header::HEADER_SESSION_ID,
            axum::http::HeaderValue::from_static("test-session-id"),
        );
        let mut extensions = rmcp::model::Extensions::new();
        extensions.insert(parts);
        let meta = rmcp::model::RequestMetaObject::new();
        assert!(!super::is_stateless_http_request(&extensions, &meta));
    }

    /// A request that echoes an `Mcp-Session-Id` header is still flagged if
    /// its `_meta` carries discover-lifecycle keys -- proving the session
    /// header alone is *not* a sufficient counter-signal: rmcp never
    /// validates that header on the stateless branch #482 targets, so a
    /// request can carry one (fabricated, stale, or even genuinely live)
    /// while still being served statelessly.
    #[cfg(feature = "transport-http")]
    #[test]
    fn test_is_stateless_http_request_true_with_session_header_and_discover_meta() {
        let (mut parts, ()) = axum::http::Request::builder()
            .body(())
            .unwrap()
            .into_parts();
        parts.headers.insert(
            rmcp::transport::common::http_header::HEADER_SESSION_ID,
            axum::http::HeaderValue::from_static("test-session-id"),
        );
        let mut extensions = rmcp::model::Extensions::new();
        extensions.insert(parts);
        let meta = rmcp::model::RequestMetaObject::with_client_context(
            rmcp::model::ProtocolVersion::V_2025_03_26,
            rmcp::model::Implementation::default(),
            rmcp::model::ClientCapabilities::default(),
        );
        assert!(super::is_stateless_http_request(&extensions, &meta));
    }

    /// #482 primary signal: `_meta` carrying both discover-lifecycle keys
    /// (`protocolVersion` + `clientCapabilities`) is detected regardless of
    /// the declared protocol version's value -- mirroring rmcp's own
    /// `missing_required_keys`, which only checks presence.
    #[cfg(feature = "transport-http")]
    #[test]
    fn test_request_uses_discover_lifecycle_meta_true_with_both_keys_present() {
        let meta = rmcp::model::RequestMetaObject::with_client_context(
            rmcp::model::ProtocolVersion::V_2025_03_26,
            rmcp::model::Implementation::default(),
            rmcp::model::ClientCapabilities::default(),
        );
        assert!(super::request_uses_discover_lifecycle_meta(&meta));
    }

    /// Only one of the two required keys present is not enough -- matching
    /// rmcp's own `missing_required_keys`, which requires both.
    #[cfg(feature = "transport-http")]
    #[test]
    fn test_request_uses_discover_lifecycle_meta_false_with_only_one_key() {
        let mut meta = rmcp::model::RequestMetaObject::new();
        meta.set_protocol_version(rmcp::model::ProtocolVersion::V_2025_03_26);
        assert!(!super::request_uses_discover_lifecycle_meta(&meta));
    }

    /// Empty `_meta` (typical for a legacy session's ordinary request, which
    /// relies on the session's own handshake state instead of per-request
    /// metadata) is not flagged.
    #[cfg(feature = "transport-http")]
    #[test]
    fn test_request_uses_discover_lifecycle_meta_false_when_empty() {
        let meta = rmcp::model::RequestMetaObject::new();
        assert!(!super::request_uses_discover_lifecycle_meta(&meta));
    }

    /// Regression test for `read_resource`'s canonical-path fix: a path reached
    /// through a symlink must resolve, via `validate_path_against_roots`, to the
    /// same URI as its canonical (symlink-resolved) form -- matching what
    /// `diagnostics_pump` stores from LSP notifications. Building `lsp_uri` from
    /// the raw (symlinked) path (the pre-fix behavior) would produce a
    /// mismatched cache key and always miss.
    ///
    /// Uses a real symlink rather than `..` segments: `path_to_uri` re-parses
    /// the URI string through `url::Url::parse` (for RFC 3986 char encoding),
    /// which normalizes away `..` segments regardless of platform -- so a path
    /// differing only by `..` produces the same URI as its canonical form with
    /// or without the fix. Only an actual symlink resolution (which happens in
    /// `canonicalize()`, not in URI string normalization) creates a real
    /// raw-vs-canonical difference. Unix-only: creating symlinks on Windows CI
    /// runners typically requires elevated privileges / Developer Mode.
    #[test]
    #[cfg(unix)]
    fn test_read_resource_canonical_path_matches_pump_cache_key() {
        use std::fs;
        use std::os::unix::fs::symlink;

        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        // Canonicalize the base up front so any symlink-iness already present
        // in the OS temp directory itself (e.g. macOS's `/tmp` -> `/private/tmp`)
        // doesn't leak into the comparison -- the only symlink under test is
        // `link_dir`.
        let base = temp_dir.path().canonicalize().unwrap();
        let real_dir = base.join("real");
        fs::create_dir(&real_dir).unwrap();
        let test_file = real_dir.join("test.rs");
        fs::write(&test_file, "fn main() {}").unwrap();

        let link_dir = base.join("link");
        symlink(&real_dir, &link_dir).unwrap();
        let noncanonical = link_dir.join("test.rs");
        assert_ne!(noncanonical, test_file);

        let validated = validate_path_against_roots(&noncanonical, &[base]).unwrap();
        assert_eq!(validated, test_file.canonicalize().unwrap());

        let uri_from_raw_path = crate::bridge::path_to_uri(&noncanonical).unwrap();
        let uri_from_validated_path = crate::bridge::path_to_uri(&validated).unwrap();
        assert_ne!(
            uri_from_raw_path, uri_from_validated_path,
            "raw and canonical paths must differ here, otherwise this test can't \
             detect a regression back to keying off the raw path"
        );
    }

    /// `validate_path` rejects a non-existent path (canonicalize fails).
    #[tokio::test]
    async fn test_validate_path_rejects_nonexistent_path() {
        use std::path::Path;

        use crate::error::Error;

        let mut translator = Translator::new();
        #[cfg(windows)]
        translator.set_workspace_roots(vec![PathBuf::from(r"C:\")]);
        #[cfg(not(windows))]
        translator.set_workspace_roots(vec![PathBuf::from("/")]);
        let result = translator.validate_path(Path::new("/this/path/does/not/exist/at/all.rs"));
        assert!(matches!(result, Err(Error::FileIo { .. })));
    }

    /// #479 regression: `read_resource`/`subscribe` must still return
    /// `INVALID_PARAMS` (`-32602`), not `INTERNAL_ERROR` (`-32603`), for a
    /// client-supplied path that doesn't exist. Exercised at the same
    /// logic level as the rest of this test group (constructing a live
    /// `rmcp::service::RequestContext` isn't possible in a unit test, see
    /// the note above "Resource handler tests"): `validate_path_against_roots`
    /// is the exact call both handlers make, and `map_bridge_error` is the
    /// exact function both now pipe its `Err` through.
    #[test]
    fn test_read_resource_nonexistent_path_maps_to_invalid_params() {
        let temp_dir = tempfile::TempDir::new().unwrap();
        let roots = [temp_dir.path().to_path_buf()];
        let missing = temp_dir.path().join("does-not-exist.rs");

        let result = validate_path_against_roots(&missing, &roots);
        assert!(matches!(result, Err(crate::error::Error::FileIo { .. })));

        let mcp_err = map_bridge_error(result.unwrap_err());
        assert_eq!(mcp_err.code, ErrorCode::INVALID_PARAMS);
    }

    /// #496 site 2 regression: a `SubscriptionError::LimitReached`, routed
    /// through `map_bridge_error` the same way `subscribe`'s handler now
    /// does, must classify as `INTERNAL_ERROR` (like `DocumentLimitExceeded`),
    /// not `INVALID_PARAMS` -- it fires on aggregate per-session tracker
    /// state, not this request's params, so it can succeed unchanged once
    /// other subscriptions are dropped.
    #[test]
    fn test_subscription_limit_reached_maps_to_internal_error() {
        let err: crate::error::Error =
            crate::bridge::resources::SubscriptionError::LimitReached.into();
        let mcp_err = map_bridge_error(err);
        assert_eq!(mcp_err.code, ErrorCode::INTERNAL_ERROR);
    }

    /// #499 regression: `unsubscribe`'s best-effort fallback to the raw
    /// request URI must still resolve to the entry `subscribe` created, even
    /// when canonicalizing the raw URI now fails because the file was
    /// deleted since subscribing. Without the alias recorded by
    /// `record_alias` at subscribe time, this fallback used to leak a capped
    /// subscription slot forever.
    #[tokio::test]
    #[cfg(unix)]
    async fn test_unsubscribe_resolves_stale_deleted_file_via_alias() {
        use std::fs;
        use std::os::unix::fs::symlink;

        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let base = temp_dir.path().canonicalize().unwrap();
        let real_dir = base.join("real");
        fs::create_dir(&real_dir).unwrap();
        let test_file = real_dir.join("test.rs");
        fs::write(&test_file, "fn main() {}").unwrap();

        let link_dir = base.join("link");
        symlink(&real_dir, &link_dir).unwrap();
        let noncanonical = link_dir.join("test.rs");

        let roots = std::slice::from_ref(&base);
        let validated = validate_path_against_roots(&noncanonical, roots).unwrap();
        let raw_uri = make_uri(&noncanonical).unwrap();
        let canonical_uri = make_uri(&validated).unwrap();
        assert_ne!(raw_uri, canonical_uri);

        let subscriptions = ResourceSubscriptions::new();
        subscriptions
            .subscribe(canonical_uri.clone())
            .await
            .unwrap();
        subscriptions
            .record_alias(raw_uri.clone(), canonical_uri.clone())
            .await;

        // Delete the file (through the real path, not the symlink) so
        // canonicalizing the symlinked path at unsubscribe time fails.
        fs::remove_file(&test_file).unwrap();
        assert!(validate_path_against_roots(&noncanonical, roots).is_err());

        // Mirrors `unsubscribe`'s handler: falls back to the raw URI once
        // canonicalization fails.
        let key = raw_uri;
        assert!(subscriptions.unsubscribe(&key).await);
        assert!(!subscriptions.contains(&canonical_uri).await);
    }

    /// subscribe cap enforced: after `MAX_SUBSCRIPTIONS` entries, the next call returns `Err`.
    #[tokio::test]
    async fn test_subscription_cap_enforced_in_handler_context() {
        use crate::bridge::resources::MAX_SUBSCRIPTIONS;

        let subscriptions = Arc::new(ResourceSubscriptions::new());
        for i in 0..MAX_SUBSCRIPTIONS {
            subscriptions
                .subscribe(format!("lsp-diagnostics:///file{i}.rs"))
                .await
                .unwrap();
        }
        let over = subscriptions
            .subscribe("lsp-diagnostics:///overflow.rs".to_string())
            .await;
        assert!(over.is_err());
    }

    /// unsubscribing a URI that was never subscribed is a no-op (returns `false`, not an error).
    #[tokio::test]
    async fn test_unsubscribe_nonexistent_is_noop() {
        let subscriptions = Arc::new(ResourceSubscriptions::new());
        let removed = subscriptions
            .unsubscribe("lsp-diagnostics:///nonexistent.rs")
            .await;
        assert!(!removed);
    }

    /// `for_new_session` gives each HTTP session its own subscription set
    /// (#478): subscribing on one clone must not be visible to another, and
    /// unsubscribing from one clone must not affect another's entries.
    #[tokio::test]
    async fn test_for_new_session_isolates_subscriptions() {
        let server = create_test_server();
        let session_a = server.for_new_session();
        let session_b = server.for_new_session();

        session_a
            .context
            .subscriptions
            .subscribe("lsp-diagnostics:///a.rs".to_string())
            .await
            .unwrap();

        assert!(
            session_a
                .context
                .subscriptions
                .contains("lsp-diagnostics:///a.rs")
                .await
        );
        assert!(
            !session_b
                .context
                .subscriptions
                .contains("lsp-diagnostics:///a.rs")
                .await
        );
        assert!(
            !server
                .context
                .subscriptions
                .contains("lsp-diagnostics:///a.rs")
                .await
        );

        // Cross-session unsubscribe must not remove another session's entry.
        session_b
            .context
            .subscriptions
            .unsubscribe("lsp-diagnostics:///a.rs")
            .await;
        assert!(
            session_a
                .context
                .subscriptions
                .contains("lsp-diagnostics:///a.rs")
                .await
        );
    }

    /// A session's subscriptions are reclaimed from the shared registry as
    /// soon as its `McplsServer` clone is dropped, without any explicit
    /// close-time bookkeeping (#478).
    #[tokio::test]
    async fn test_dropped_session_subscriptions_are_reclaimed() {
        let server = create_test_server();
        let registry = server.context.subscription_registry.clone();
        {
            let session = server.for_new_session();
            session
                .context
                .subscriptions
                .subscribe("lsp-diagnostics:///a.rs".to_string())
                .await
                .unwrap();
            assert!(registry.any_contains("lsp-diagnostics:///a.rs").await);
        }
        assert!(!registry.any_contains("lsp-diagnostics:///a.rs").await);
    }

    /// Server capabilities advertise resources support.
    #[tokio::test]
    async fn test_server_capabilities_include_resources() {
        let server = create_test_server();
        let info = server.get_info();
        assert!(info.capabilities.resources.is_some());
    }

    /// Dump the current tool surface to stdout so it can be captured into
    /// `tool_surface.json`. Not part of the regular suite.
    #[test]
    #[ignore = "run manually to (re)generate tool_surface.json"]
    fn dump_tool_surface() {
        let tools = McplsServer::build_tool_router(None).list_all();
        println!("{}", serde_json::to_string_pretty(&tools).unwrap());
    }

    /// Pins the client-visible tool surface (name, description, title,
    /// annotations, input schema) exposed by `build_tool_router(None).list_all()`.
    /// `serde_json::Value` comparison, not string comparison, so key
    /// order/whitespace drift doesn't cause false failures -- only an actual
    /// change to what an MCP client sees does.
    #[test]
    fn test_tool_surface_matches_golden_snapshot() {
        let tools = McplsServer::build_tool_router(None).list_all();
        let actual = serde_json::to_value(&tools).unwrap();
        let expected: serde_json::Value =
            serde_json::from_str(include_str!("tool_surface.json")).unwrap();
        assert_eq!(
            actual, expected,
            "client-visible tool surface changed -- update tool_surface.json only if the \
             change is intentional"
        );
    }

    /// Structured output (`outputSchema`) is advertised for exactly the tools migrated to
    /// `Result<Json<T>, McpError>` handler signatures, and only those. Driven off a literal
    /// expected-name set (not derived from the router) so both a future migration and an
    /// accidental scope change fail loudly here instead of only showing up as an opaque diff in
    /// `test_tool_surface_matches_golden_snapshot`.
    #[test]
    fn test_output_schema_present_only_for_structured_tools() {
        const STRUCTURED_TOOLS: &[&str] = &[
            "get_diagnostics",
            "get_definition",
            "get_references",
            "get_document_symbols",
        ];

        let tools = McplsServer::build_tool_router(None).list_all();
        assert!(!tools.is_empty(), "no tools registered");

        for tool in &tools {
            let expects_schema = STRUCTURED_TOOLS.contains(&tool.name.as_ref());
            assert_eq!(
                tool.output_schema.is_some(),
                expects_schema,
                "tool `{}`: expected output_schema.is_some() == {expects_schema}",
                tool.name
            );
        }
    }

    // ------------------------------------------------------------------
    // tool_prefix tests
    // ------------------------------------------------------------------

    /// Pins that `ToolRouter::disable_route`/`with_disabled` are unreachable
    /// today, which is what makes `build_tool_router`'s rename pass safe to
    /// skip re-keying the private `disabled` set (see its doc comment). If
    /// this ever fails, a `disable_route` call was added somewhere and the
    /// rename pass needs to be updated to preserve disabled state across
    /// the rekey.
    #[test]
    fn test_no_route_is_ever_disabled() {
        let router = McplsServer::build_tool_router(None);
        for name in router.map.keys() {
            assert!(
                !router.is_disabled(name),
                "route {name} is unexpectedly disabled"
            );
        }
    }

    /// Every currently-registered tool name must fit within
    /// `MAX_TOOL_NAME_BYTES`, the constant the `MAX_MCP_TOOL_PREFIX_BYTES`
    /// safety margin is derived from (see the compile-time assertion next
    /// to it). A future tool name that grows past this fails here, with a
    /// clear pointer to `MAX_TOOL_NAME_BYTES`, rather than surfacing as a
    /// confusing prefix-length failure far away in `config`.
    #[test]
    fn test_registered_tool_names_fit_max_tool_name_bytes() {
        let router = McplsServer::build_tool_router(None);
        for tool in router.list_all() {
            assert!(
                tool.name.len() <= MAX_TOOL_NAME_BYTES,
                "tool `{}` is {} bytes, exceeding MAX_TOOL_NAME_BYTES ({MAX_TOOL_NAME_BYTES}); \
                 bump MAX_TOOL_NAME_BYTES and re-check its compile-time assertion against \
                 MAX_MCP_TOOL_PREFIX_BYTES",
                tool.name,
                tool.name.len()
            );
        }
    }

    /// A configured prefix rewrites only `name` -- every other field
    /// (description, title, annotations, input schema, output schema) stays
    /// identical to the unprefixed golden snapshot, and no route is gained
    /// or lost.
    #[test]
    fn test_build_tool_router_with_prefix_renames_only_name() {
        let prefix: ToolPrefix = "optics".parse().unwrap();
        let unprefixed = McplsServer::build_tool_router(None).list_all();
        let prefixed = McplsServer::build_tool_router(Some(&prefix)).list_all();

        assert_eq!(unprefixed.len(), prefixed.len());
        for (before, after) in unprefixed.iter().zip(prefixed.iter()) {
            assert_eq!(after.name, format!("optics_{}", before.name));
            assert_eq!(after.description, before.description);
            assert_eq!(after.title, before.title);
            assert_eq!(after.annotations, before.annotations);
            assert_eq!(after.input_schema, before.input_schema);
            assert_eq!(after.output_schema, before.output_schema);
        }
    }

    /// The map key for every route must equal its own `attr.name` --
    /// `ToolRouter::call` dispatches by looking up `map` with the
    /// requested name, so a mismatch here would silently break routing to
    /// unreachable dead entries under their pre-rename key.
    #[test]
    fn test_build_tool_router_map_keys_match_route_names() {
        let prefix: ToolPrefix = "optics".parse().unwrap();
        let router = McplsServer::build_tool_router(Some(&prefix));
        for (key, route) in &router.map {
            assert_eq!(key.as_ref(), route.attr.name.as_ref());
        }
    }

    /// The single test proving the prefix threads end-to-end from the
    /// public `McplsServer::new` constructor, not just from calling
    /// `build_tool_router` directly -- every other test above calls
    /// `build_tool_router` in isolation, so this is the one that would
    /// catch a wiring mistake in `new` (e.g. forgetting to read
    /// `mcp.tool_prefix` before `mcp` is moved into `BridgeContext::new`).
    #[tokio::test]
    async fn test_new_threads_configured_tool_prefix_end_to_end() {
        let mcp = McpConfig {
            tool_prefix: Some("p".parse().unwrap()),
            ..McpConfig::default()
        };
        let server = create_test_server_with_mcp_config(false, mcp);

        assert!(server.get_tool("p_get_hover").is_some());
        assert!(server.get_tool("get_hover").is_none());
    }
}