tower-mcp 0.22.2

Tower-native Model Context Protocol (MCP) implementation
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
//! Resources: one fixed URI, or a URI template that routes
//!
//! A resource is content a client reads by URI. Which of the two kinds you
//! build depends on whether the URI is known before the request arrives:
//!
//! - [`Resource`], built with [`ResourceBuilder`], answers for exactly one
//!   URI and appears in `resources/list`.
//! - [`ResourceTemplate`], built with [`ResourceTemplateBuilder`], answers for
//!   a family of URIs described by an RFC 6570 template, appears in
//!   `resources/templates/list`, and hands its handler the variables it
//!   extracted from the request URI.
//!
//! A resource can also be written as a type rather than a closure by
//! implementing [`McpResource`].
//!
//! # How a read finds its handler
//!
//! [`McpRouter`](crate::McpRouter) answers `resources/read` by trying an exact
//! URI match against the registered resources first, then each registered
//! template in registration order, stopping at the first template that
//! matches. Registration order is the only tie-break, so register the narrower
//! pattern first: `db://users/{id}` before `db://{+rest}`, or the second one
//! swallows every read and the first never runs.
//!
//! # Handler forms
//!
//! | builder call | what the handler receives |
//! |---|---|
//! | [`ResourceBuilder::handler`] | nothing |
//! | [`ResourceBuilder::handler_with_context`] | a [`RequestContext`] for progress, cancellation, and client requests |
//! | [`ResourceTemplateBuilder::handler`] | the request URI and the extracted variables |
//!
//! Each returns a [`ReadResourceResult`]. For content already in memory,
//! [`ResourceBuilder::text`] and [`ResourceBuilder::json`] finish the builder
//! without a handler at all. With the `stateless` feature, `mrtr_handler`
//! accepts a handler that can suspend and ask the client for more input
//! (SEP-2322).
//!
//! # A failing resource handler is a failed request
//!
//! Every [`Resource`] handler is wrapped in an error-catching service, because
//! a Tower stack composes only when the inner service cannot fail at the
//! Tower level. The wrapper does not discard the handler's error, though: it
//! converts the handler's own error into a structured JSON-RPC error before
//! any layer sees it, so that error rides through the middleware stack
//! untouched and the router reports it exactly the way it would for a
//! [`ResourceTemplate`] (whose handler is never wrapped at all). Only a
//! genuine failure at the Tower level -- a timeout, a rate limit, anything
//! from a layer rather than from the handler -- is sanitized to a generic
//! `-32603` Internal Error.
//!
//! [`Resource::read`] and [`Resource::read_with_context`] are the one
//! exception: their signatures return [`ReadResourceResult`] rather than a
//! `Result`, so they have nowhere to put an error and still render one as
//! content. The router does not use them; it calls
//! [`Resource::read_outcome_with_context`], which does return a `Result` and
//! is what a client actually sees.
//!
//! # Per-resource middleware
//!
//! Resources are Tower services internally, so any layer composes onto a
//! single resource through `.layer()`. This is the per-resource counterpart to
//! layering the whole router, and the place to put a bound that only one
//! expensive resource needs:
//!
//! ```rust
//! use std::time::Duration;
//! use tower::timeout::TimeoutLayer;
//! use tower_mcp::resource::ResourceBuilder;
//! use tower_mcp::protocol::ReadResourceResult;
//!
//! let resource = ResourceBuilder::new("file:///large-file.txt")
//!     .name("Large File")
//!     .description("A file large enough that a read can hang")
//!     .handler(|| async {
//!         Ok(ReadResourceResult::text("file:///large-file.txt", "content"))
//!     })
//!     .layer(TimeoutLayer::new(Duration::from_secs(30)))
//!     .build();
//! ```
//!
//! # URI templates
//!
//! A template is compiled once, at build time, into a matcher:
//!
//! - `{var}` matches any run of non-slash characters
//! - `{+var}` matches any characters at all, including `/`
//! - `{?a,b}` and `{&a,b}` declare optional query parameters (#1253)
//!
//! [`ResourceTemplate::match_uri`] documents the matching rules and shows each
//! of them running against real URIs.
//!
//! ```rust
//! use tower_mcp::resource::ResourceTemplateBuilder;
//! use tower_mcp::protocol::ReadResourceResult;
//! use std::collections::HashMap;
//!
//! let template = ResourceTemplateBuilder::new("file:///{+path}")
//!     .name("Project Files")
//!     .description("Any file under the project directory")
//!     .handler(|uri: String, vars: HashMap<String, String>| async move {
//!         let path = vars["path"].clone();
//!         Ok(ReadResourceResult::text(uri, format!("contents of {path}")))
//!     });
//!
//! // `{+path}` spans slashes, so nested paths reach the same handler.
//! let vars = template.match_uri("file:///src/lib.rs").unwrap();
//! assert_eq!(vars["path"], "src/lib.rs");
//! ```

use std::collections::HashMap;
use std::convert::Infallible;
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use pin_project_lite::pin_project;
use serde_json::Value;

#[cfg(feature = "stateless")]
use tower::ServiceExt;
use tower::util::BoxCloneService;
use tower_service::Service;

#[cfg(feature = "stateless")]
use tokio::sync::Mutex;

use crate::context::RequestContext;
use crate::error::{Error, JsonRpcError, Result};
use crate::protocol::{
    ContentAnnotations, PromptArgument, ReadResourceResult, RequestOutcome, ResourceContent,
    ResourceDefinition, ResourceTemplateDefinition, ToolIcon,
};

// =============================================================================
// Service Types for Per-Resource Middleware
// =============================================================================

/// What a layer applied with `.layer()` sees for one read.
///
/// Middleware runs on this rather than on the handler's arguments, so a layer
/// can read the URI and the request context without knowing anything about the
/// handler behind it.
#[derive(Debug, Clone)]
pub struct ResourceRequest {
    /// Request context for progress reporting, cancellation, and client requests
    pub ctx: RequestContext,
    /// The URI of the resource being read
    pub uri: String,
}

impl ResourceRequest {
    /// Create a new resource request
    pub fn new(ctx: RequestContext, uri: String) -> Self {
        Self { ctx, uri }
    }
}

/// A boxed, cloneable resource service with `Error = Infallible`.
///
/// This is the internal service type that resources use. The service itself
/// never fails at the Tower level (`Error = Infallible`), which is what lets
/// `.layer()` compose; whether the read succeeded is carried instead in the
/// success value, `Ok(ReadResourceResult)` or `Err(JsonRpcError)`. A
/// structured `JsonRpcError` here is the handler's own error, converted
/// before it reached any layer; [`ResourceCatchError`] only ever produces
/// one itself for a genuine middleware failure (a timeout, a rate limit).
pub type BoxResourceService = BoxCloneService<
    ResourceRequest,
    std::result::Result<ReadResourceResult, JsonRpcError>,
    Infallible,
>;

#[cfg(feature = "stateless")]
type BoxMrtrResourceService = BoxCloneService<
    ResourceRequest,
    std::result::Result<RequestOutcome<ReadResourceResult>, JsonRpcError>,
    Infallible,
>;

/// Catches errors from the inner service and converts them to error results.
///
/// This wrapper ensures that middleware errors (e.g., timeouts, rate limits)
/// and handler errors are converted to `Err(Error)` responses wrapped in
/// `Ok`, rather than propagating as Tower service errors.
#[doc(hidden)]
pub struct ResourceCatchError<S> {
    inner: S,
}

impl<S> ResourceCatchError<S> {
    /// Create a new `ResourceCatchError` wrapping the given service.
    pub fn new(inner: S) -> Self {
        Self { inner }
    }
}

impl<S: Clone> Clone for ResourceCatchError<S> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl<S: fmt::Debug> fmt::Debug for ResourceCatchError<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ResourceCatchError")
            .field("inner", &self.inner)
            .finish()
    }
}

pin_project! {
    /// Future for [`ResourceCatchError`].
    #[doc(hidden)]
    pub struct ResourceCatchErrorFuture<F> {
        #[pin]
        inner: F,
    }
}

impl<F, E> Future for ResourceCatchErrorFuture<F>
where
    F: Future<
        Output = std::result::Result<std::result::Result<ReadResourceResult, JsonRpcError>, E>,
    >,
    E: fmt::Display,
{
    type Output =
        std::result::Result<std::result::Result<ReadResourceResult, JsonRpcError>, Infallible>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.project();
        match this.inner.poll(cx) {
            Poll::Pending => Poll::Pending,
            // The inner service already decided: either the read succeeded,
            // or the handler's own error was already converted to a
            // structured JsonRpcError. Either way, pass it through untouched.
            Poll::Ready(Ok(inner)) => Poll::Ready(Ok(inner)),
            // The inner service itself failed at the Tower level: a genuine
            // middleware failure (timeout, rate limit, ...), not the
            // handler's own error. Sanitize it to a generic Internal Error.
            Poll::Ready(Err(err)) => {
                Poll::Ready(Ok(Err(JsonRpcError::internal_error(err.to_string()))))
            }
        }
    }
}

impl<S> Service<ResourceRequest> for ResourceCatchError<S>
where
    S: Service<ResourceRequest, Response = std::result::Result<ReadResourceResult, JsonRpcError>>
        + Clone
        + Send
        + 'static,
    S::Error: fmt::Display + Send,
    S::Future: Send,
{
    type Response = std::result::Result<ReadResourceResult, JsonRpcError>;
    type Error = Infallible;
    type Future = ResourceCatchErrorFuture<S::Future>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<std::result::Result<(), Self::Error>> {
        // Map any readiness error to Infallible (we catch it on call)
        match self.inner.poll_ready(cx) {
            Poll::Ready(Ok(())) => Poll::Ready(Ok(())),
            Poll::Ready(Err(_)) => Poll::Ready(Ok(())),
            Poll::Pending => Poll::Pending,
        }
    }

    fn call(&mut self, req: ResourceRequest) -> Self::Future {
        let fut = self.inner.call(req);

        ResourceCatchErrorFuture { inner: fut }
    }
}

#[cfg(feature = "stateless")]
#[derive(Clone)]
struct MrtrResourceCatchError<S> {
    inner: S,
}

#[cfg(feature = "stateless")]
impl<S> MrtrResourceCatchError<S> {
    fn new(inner: S) -> Self {
        Self { inner }
    }
}

#[cfg(feature = "stateless")]
impl<S> Service<ResourceRequest> for MrtrResourceCatchError<S>
where
    S: Service<
            ResourceRequest,
            Response = std::result::Result<RequestOutcome<ReadResourceResult>, JsonRpcError>,
        > + Clone
        + Send
        + 'static,
    S::Error: fmt::Display + Send + 'static,
    S::Future: Send + 'static,
{
    type Response = std::result::Result<RequestOutcome<ReadResourceResult>, JsonRpcError>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = std::result::Result<Self::Response, Self::Error>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<std::result::Result<(), Self::Error>> {
        match self.inner.poll_ready(cx) {
            Poll::Ready(Ok(())) | Poll::Ready(Err(_)) => Poll::Ready(Ok(())),
            Poll::Pending => Poll::Pending,
        }
    }

    fn call(&mut self, req: ResourceRequest) -> Self::Future {
        let future = self.inner.call(req);
        Box::pin(async move {
            Ok(match future.await {
                Ok(inner) => inner,
                Err(error) => Err(JsonRpcError::internal_error(error.to_string())),
            })
        })
    }
}

/// A boxed future for resource handlers
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

/// The shape a resource handler is adapted to internally.
///
/// [`ResourceBuilder`] wraps closures in an implementation of this trait, and
/// the router reads every resource through it. No public constructor accepts
/// an implementation, so a resource written as a type rather than a closure
/// implements [`McpResource`] instead.
pub(crate) trait ResourceHandler: Send + Sync {
    /// Read the resource contents
    fn read(&self) -> BoxFuture<'_, Result<ReadResourceResult>>;

    /// Read the resource with request context for progress/cancellation support
    ///
    /// The default implementation ignores the context and calls `read`.
    /// Override this to receive progress/cancellation context.
    fn read_with_context(&self, _ctx: RequestContext) -> BoxFuture<'_, Result<ReadResourceResult>> {
        self.read()
    }
}

/// Resource handler that may return an SEP-2322 input-required continuation.
#[cfg(feature = "stateless")]
pub(crate) trait MrtrResourceHandler: Send + Sync {
    /// Read a resource attempt with continuation values in the context.
    fn read(
        &self,
        ctx: RequestContext,
    ) -> BoxFuture<'_, Result<RequestOutcome<ReadResourceResult>>>;
}

#[cfg(feature = "stateless")]
struct MrtrResourceHandlerService<H> {
    handler: Arc<H>,
}

#[cfg(feature = "stateless")]
impl<H> MrtrResourceHandlerService<H> {
    fn new(handler: H) -> Self {
        Self {
            handler: Arc::new(handler),
        }
    }
}

#[cfg(feature = "stateless")]
impl<H> Clone for MrtrResourceHandlerService<H> {
    fn clone(&self) -> Self {
        Self {
            handler: self.handler.clone(),
        }
    }
}

#[cfg(feature = "stateless")]
impl<H> Service<ResourceRequest> for MrtrResourceHandlerService<H>
where
    H: MrtrResourceHandler + 'static,
{
    type Response = std::result::Result<RequestOutcome<ReadResourceResult>, JsonRpcError>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = std::result::Result<Self::Response, Infallible>> + Send>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<std::result::Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: ResourceRequest) -> Self::Future {
        let handler = self.handler.clone();
        Box::pin(async move {
            Ok(handler
                .read(req.ctx)
                .await
                .map_err(Error::into_json_rpc_error))
        })
    }
}

#[cfg(feature = "stateless")]
struct ServiceMrtrResourceHandler {
    service: Mutex<BoxMrtrResourceService>,
    uri: String,
}

#[cfg(feature = "stateless")]
impl MrtrResourceHandler for ServiceMrtrResourceHandler {
    fn read(
        &self,
        ctx: RequestContext,
    ) -> BoxFuture<'_, Result<RequestOutcome<ReadResourceResult>>> {
        Box::pin(async move {
            let request = ResourceRequest::new(ctx, self.uri.clone());
            let mut service = self.service.lock().await.clone();
            let outcome = service
                .ready()
                .await
                .expect("MRTR resource service is infallible")
                .call(request)
                .await
                .expect("MRTR resource service is infallible");
            outcome.map_err(Into::into)
        })
    }
}

/// Adapts a `ResourceHandler` to a Tower `Service<ResourceRequest>`.
///
/// This is an internal adapter that bridges the handler abstraction to the
/// service abstraction, enabling middleware composition.
struct ResourceHandlerService<H> {
    handler: Arc<H>,
}

impl<H> ResourceHandlerService<H> {
    fn new(handler: H) -> Self {
        Self {
            handler: Arc::new(handler),
        }
    }
}

impl<H> Clone for ResourceHandlerService<H> {
    fn clone(&self) -> Self {
        Self {
            handler: self.handler.clone(),
        }
    }
}

impl<H> fmt::Debug for ResourceHandlerService<H> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ResourceHandlerService")
            .finish_non_exhaustive()
    }
}

impl<H> Service<ResourceRequest> for ResourceHandlerService<H>
where
    H: ResourceHandler + 'static,
{
    type Response = std::result::Result<ReadResourceResult, JsonRpcError>;
    type Error = Infallible;
    type Future =
        Pin<Box<dyn Future<Output = std::result::Result<Self::Response, Infallible>> + Send>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<std::result::Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: ResourceRequest) -> Self::Future {
        let handler = self.handler.clone();
        Box::pin(async move {
            Ok(handler
                .read_with_context(req.ctx)
                .await
                .map_err(Error::into_json_rpc_error))
        })
    }
}

/// A resource that is ready to register.
///
/// Produced by [`ResourceBuilder`] or [`McpResource::into_resource`] and
/// consumed by [`McpRouter::resource`](crate::McpRouter::resource). The public
/// fields are what `resources/list` advertises; behind them is a Tower service,
/// which is what lets `.layer()` compose middleware onto a single resource.
///
/// That service is wrapped so it cannot fail, so handler and middleware errors
/// come back as content rather than as errors. [`read`](Self::read) shows what
/// that looks like.
pub struct Resource {
    /// Resource URI
    pub uri: String,
    /// Human-readable name
    pub name: String,
    /// Human-readable title for display purposes
    pub title: Option<String>,
    /// Optional description
    pub description: Option<String>,
    /// Optional MIME type
    pub mime_type: Option<String>,
    /// Optional icons for display in user interfaces
    pub icons: Option<Vec<ToolIcon>>,
    /// Optional size in bytes
    pub size: Option<u64>,
    /// Optional annotations (audience, priority hints)
    pub annotations: Option<ContentAnnotations>,
    /// Validated protocol metadata included in `resources/list`.
    pub meta: Option<Value>,
    /// The boxed service that reads the resource
    service: Option<BoxResourceService>,
    #[cfg(feature = "stateless")]
    mrtr_handler: Option<Arc<dyn MrtrResourceHandler>>,
}

impl Clone for Resource {
    fn clone(&self) -> Self {
        Self {
            uri: self.uri.clone(),
            name: self.name.clone(),
            title: self.title.clone(),
            description: self.description.clone(),
            mime_type: self.mime_type.clone(),
            icons: self.icons.clone(),
            size: self.size,
            annotations: self.annotations.clone(),
            meta: self.meta.clone(),
            service: self.service.clone(),
            #[cfg(feature = "stateless")]
            mrtr_handler: self.mrtr_handler.clone(),
        }
    }
}

impl std::fmt::Debug for Resource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Resource")
            .field("uri", &self.uri)
            .field("name", &self.name)
            .field("title", &self.title)
            .field("description", &self.description)
            .field("mime_type", &self.mime_type)
            .field("icons", &self.icons)
            .field("size", &self.size)
            .field("annotations", &self.annotations)
            .field("meta", &self.meta)
            .finish_non_exhaustive()
    }
}

// SAFETY: BoxCloneService is Send + Sync (tower provides unsafe impl Sync),
// and all other fields in Resource are Send + Sync.
unsafe impl Send for Resource {}
unsafe impl Sync for Resource {}

impl Resource {
    /// Create a new resource builder
    pub fn builder(uri: impl Into<String>) -> ResourceBuilder {
        ResourceBuilder::new(uri)
    }

    /// The entry this resource contributes to `resources/list`.
    pub fn definition(&self) -> ResourceDefinition {
        ResourceDefinition {
            uri: self.uri.clone(),
            name: self.name.clone(),
            title: self.title.clone(),
            description: self.description.clone(),
            mime_type: self.mime_type.clone(),
            icons: self.icons.clone(),
            size: self.size,
            annotations: self.annotations.clone(),
            meta: self.meta.clone(),
        }
    }

    /// Attach `_meta` to what `resources/list` publishes for this resource.
    ///
    /// The value is checked here rather than at serialization time, so a key
    /// that does not fit the spec's name grammar is rejected while there is
    /// still a caller to tell.
    ///
    /// ```rust
    /// use serde_json::json;
    /// use tower_mcp::resource::ResourceBuilder;
    ///
    /// let resource = ResourceBuilder::new("docs://usage")
    ///     .name("Usage")
    ///     .text("...")
    ///     .with_meta(json!({ "example.com/generated-at": "2026-08-10" }))
    ///     .expect("valid _meta key");
    ///
    /// assert!(
    ///     resource
    ///         .clone()
    ///         .with_meta(json!({ "/leading-slash": true }))
    ///         .is_err()
    /// );
    /// ```
    pub fn with_meta(
        mut self,
        meta: Value,
    ) -> std::result::Result<Self, crate::protocol::MetaValidationError> {
        crate::protocol::validate_meta_object(&meta)?;
        self.meta = Some(meta);
        Ok(self)
    }

    /// Read the resource with a placeholder request context.
    ///
    /// Progress reporting, cancellation, and client requests are inert on the
    /// placeholder context, so this is for tests and for handlers built with
    /// [`ResourceBuilder::handler`], which never see the context anyway. The
    /// transports call [`read_with_context`](Self::read_with_context).
    ///
    /// There is no `Result` here because this method's own signature has no
    /// room for one: it returns [`ReadResourceResult`] outright, so a handler
    /// error is rendered as content instead. That is purely a property of
    /// this convenience method, not of how a resource's error is handled in
    /// general; the protocol-facing path,
    /// [`read_outcome_with_context`](Self::read_outcome_with_context), does
    /// return a `Result` and is what the router and a real client see.
    ///
    /// ```rust
    /// use tower_mcp::error::Error;
    /// use tower_mcp::resource::ResourceBuilder;
    ///
    /// # tokio_test::block_on(async {
    /// let resource = ResourceBuilder::new("db://users")
    ///     .name("Users")
    ///     .handler(|| async { Err(Error::internal("database is offline")) })
    ///     .build();
    ///
    /// let result = resource.read().await;
    /// let text = result.contents[0].text.as_deref().unwrap();
    /// assert!(text.contains("database is offline"), "{text}");
    /// # });
    /// ```
    ///
    /// [`ResourceTemplate::read`] has no such convenience wrapper and always
    /// returns a `Result`.
    pub fn read(&self) -> BoxFuture<'static, ReadResourceResult> {
        let ctx = RequestContext::new(crate::protocol::RequestId::Number(0));
        self.read_with_context(ctx)
    }

    /// Read the resource with the request context the transport built.
    ///
    /// The context carries the request id, progress reporting, the
    /// cancellation token, and the client requester used for sampling and
    /// elicitation. Handlers registered through [`ResourceBuilder::handler`]
    /// ignore it; those registered through
    /// [`ResourceBuilder::handler_with_context`] receive it.
    ///
    /// This signature has no room for an error or for an SEP-2322
    /// continuation, so both are rendered as content, the same way
    /// [`read`](Self::read) does. The router does not call this method: it
    /// calls [`read_outcome_with_context`](Self::read_outcome_with_context),
    /// which returns a `Result` and preserves a continuation instead of
    /// flattening it.
    pub fn read_with_context(&self, ctx: RequestContext) -> BoxFuture<'static, ReadResourceResult> {
        let resource = self.clone();
        let uri = self.uri.clone();
        Box::pin(async move {
            match resource.read_outcome_with_context(ctx).await {
                Ok(RequestOutcome::Complete(result)) => result,
                Ok(RequestOutcome::InputRequired(_)) => ReadResourceResult {
                    contents: vec![ResourceContent {
                        uri,
                        mime_type: Some("text/plain".into()),
                        text: Some(
                            "resource requires additional client input; use read_outcome_with_context"
                                .into(),
                        ),
                        blob: None,
                        meta: None,
                    }],
                    ..ReadResourceResult::default()
                },
                Err(error) => ReadResourceResult {
                    contents: vec![ResourceContent {
                        uri,
                        mime_type: Some("text/plain".into()),
                        text: Some(error.to_string()),
                        blob: None,
                        meta: None,
                    }],
                    ..ReadResourceResult::default()
                },
            }
        })
    }

    /// Read the resource while preserving an SEP-2322 continuation.
    ///
    /// The router calls this so that a handler which needs more input from the
    /// client can say so, instead of having that outcome flattened into
    /// content by [`read_with_context`](Self::read_with_context).
    pub fn read_outcome_with_context(
        &self,
        ctx: RequestContext,
    ) -> BoxFuture<'static, Result<RequestOutcome<ReadResourceResult>>> {
        use tower::ServiceExt;
        #[cfg(feature = "stateless")]
        if let Some(handler) = self.mrtr_handler.clone() {
            return Box::pin(async move { handler.read(ctx).await });
        }
        let service = self
            .service
            .clone()
            .expect("resource must have a complete or MRTR handler");
        let uri = self.uri.clone();
        Box::pin(async move {
            let result = service
                .oneshot(ResourceRequest::new(ctx, uri))
                .await
                .unwrap();
            match result {
                Ok(read_result) => Ok(RequestOutcome::Complete(read_result)),
                Err(json_rpc_err) => Err(json_rpc_err.into()),
            }
        })
    }

    /// Create a resource from a handler (internal helper)
    #[allow(clippy::too_many_arguments)]
    fn from_handler<H: ResourceHandler + 'static>(
        uri: String,
        name: String,
        title: Option<String>,
        description: Option<String>,
        mime_type: Option<String>,
        icons: Option<Vec<ToolIcon>>,
        size: Option<u64>,
        annotations: Option<ContentAnnotations>,
        handler: H,
    ) -> Self {
        let handler_service = ResourceHandlerService::new(handler);
        let catch_error = ResourceCatchError::new(handler_service);
        let service = BoxCloneService::new(catch_error);

        Self {
            uri,
            name,
            title,
            description,
            mime_type,
            icons,
            size,
            annotations,
            meta: None,
            service: Some(service),
            #[cfg(feature = "stateless")]
            mrtr_handler: None,
        }
    }

    #[cfg(feature = "stateless")]
    #[allow(clippy::too_many_arguments)]
    fn from_mrtr_handler<H: MrtrResourceHandler + 'static>(
        uri: String,
        name: String,
        title: Option<String>,
        description: Option<String>,
        mime_type: Option<String>,
        icons: Option<Vec<ToolIcon>>,
        size: Option<u64>,
        annotations: Option<ContentAnnotations>,
        handler: H,
    ) -> Self {
        Self {
            uri,
            name,
            title,
            description,
            mime_type,
            icons,
            size,
            annotations,
            meta: None,
            service: None,
            mrtr_handler: Some(Arc::new(handler)),
        }
    }
}

// =============================================================================
// Builder API
// =============================================================================

/// Builder for a resource served at one fixed URI.
///
/// The URI is the identity clients read by; the name is what a user sees in a
/// picker, and defaults to the URI when it is not set. Finish the chain with a
/// handler and [`build`](ResourceBuilderWithHandler::build), or with
/// [`text`](Self::text) or [`json`](Self::json) for content already in memory,
/// then register the result with
/// [`McpRouter::resource`](crate::McpRouter::resource).
///
/// # Example
///
/// ```rust
/// use tower_mcp::protocol::ReadResourceResult;
/// use tower_mcp::resource::ResourceBuilder;
///
/// # tokio_test::block_on(async {
/// let resource = ResourceBuilder::new("file:///config.json")
///     .name("Configuration")
///     .description("Application configuration file")
///     .mime_type("application/json")
///     .handler(|| async {
///         Ok(ReadResourceResult::text_with_mime(
///             "file:///config.json",
///             r#"{"setting": "value"}"#,
///             "application/json",
///         ))
///     })
///     .build();
///
/// assert_eq!(resource.uri, "file:///config.json");
///
/// let result = resource.read().await;
/// assert_eq!(
///     result.contents[0].text.as_deref(),
///     Some(r#"{"setting": "value"}"#)
/// );
/// # });
/// ```
pub struct ResourceBuilder {
    uri: String,
    name: Option<String>,
    title: Option<String>,
    description: Option<String>,
    mime_type: Option<String>,
    icons: Option<Vec<ToolIcon>>,
    size: Option<u64>,
    annotations: Option<ContentAnnotations>,
}

impl ResourceBuilder {
    /// Start a resource at this URI.
    ///
    /// The URI is the key the router registers under, so registering a second
    /// resource with the same URI replaces the first rather than failing.
    /// [`McpRouter::try_merge`](crate::McpRouter::try_merge) is the way to
    /// find that out instead of discovering it in production.
    pub fn new(uri: impl Into<String>) -> Self {
        Self {
            uri: uri.into(),
            name: None,
            title: None,
            description: None,
            mime_type: None,
            icons: None,
            size: None,
            annotations: None,
        }
    }

    /// Set the name clients list this resource under.
    ///
    /// Defaults to the URI when unset, which is legal and unhelpful in a
    /// picker.
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set the display title, which a client prefers over the name.
    ///
    /// Worth setting when the name has to stay stable for other reasons and
    /// is not what a person should read.
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set the resource description
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Declare the MIME type in `resources/list`.
    ///
    /// A handler sets the type on each content item it returns, and nothing
    /// reconciles the two, so a resource that declares one type and returns
    /// another is simply lying to clients. [`text`](Self::text) and
    /// [`json`](Self::json) cannot drift this way because they build the
    /// content themselves.
    pub fn mime_type(mut self, mime_type: impl Into<String>) -> Self {
        self.mime_type = Some(mime_type.into());
        self
    }

    /// Add an icon for the resource
    pub fn icon(mut self, src: impl Into<String>) -> Self {
        self.icons.get_or_insert_with(Vec::new).push(ToolIcon {
            src: src.into(),
            mime_type: None,
            sizes: None,
            theme: None,
        });
        self
    }

    /// Add an icon with metadata
    pub fn icon_with_meta(
        mut self,
        src: impl Into<String>,
        mime_type: Option<String>,
        sizes: Option<Vec<String>>,
    ) -> Self {
        self.icons.get_or_insert_with(Vec::new).push(ToolIcon {
            src: src.into(),
            mime_type,
            sizes,
            theme: None,
        });
        self
    }

    /// Advertise a size in bytes, so a client can decide before reading.
    ///
    /// A hint only: nothing compares it with what the handler returns.
    pub fn size(mut self, size: u64) -> Self {
        self.size = Some(size);
        self
    }

    /// Set annotations (audience, priority hints) for this resource
    pub fn annotations(mut self, annotations: ContentAnnotations) -> Self {
        self.annotations = Some(annotations);
        self
    }

    /// Set the handler function for reading the resource.
    ///
    /// Returns a [`ResourceBuilderWithHandler`] that can be used to apply
    /// middleware layers via `.layer()` or build the resource directly via `.build()`.
    ///
    /// # Sharing State
    ///
    /// Capture an [`Arc`] in the closure to share state across handler
    /// invocations or with other parts of your application:
    ///
    /// ```rust
    /// use std::sync::Arc;
    /// use tokio::sync::RwLock;
    /// use tower_mcp::resource::ResourceBuilder;
    /// use tower_mcp::protocol::{ReadResourceResult, ResourceContent};
    ///
    /// let db = Arc::new(RwLock::new(vec!["initial".to_string()]));
    ///
    /// let db_clone = Arc::clone(&db);
    /// let resource = ResourceBuilder::new("app://entries")
    ///     .name("Entries")
    ///     .handler(move || {
    ///         let db = Arc::clone(&db_clone);
    ///         async move {
    ///             let entries = db.read().await;
    ///             Ok(ReadResourceResult {
    ///                 contents: vec![ResourceContent {
    ///                     uri: "app://entries".to_string(),
    ///                     mime_type: Some("text/plain".to_string()),
    ///                     text: Some(entries.join("\n")),
    ///                     blob: None,
    ///                     meta: None,
    ///                 }],
    ///                 meta: None,
    ///                 ..Default::default()
    ///             })
    ///         }
    ///     })
    ///     .build();
    /// ```
    ///
    /// [`Arc`]: std::sync::Arc
    pub fn handler<F, Fut>(self, handler: F) -> ResourceBuilderWithHandler<F>
    where
        F: Fn() -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<ReadResourceResult>> + Send + 'static,
    {
        ResourceBuilderWithHandler {
            uri: self.uri,
            name: self.name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            size: self.size,
            annotations: self.annotations,
            handler,
        }
    }

    /// Set a handler that receives the [`RequestContext`].
    ///
    /// Take this form when the read runs long enough to report progress
    /// against, has to notice cancellation, or wants to ask the client
    /// something. Everything else can use [`handler`](Self::handler), which
    /// skips the context entirely.
    ///
    /// Reporting progress is unconditional in the handler and free when
    /// unwanted: a client that sent no progress token receives nothing.
    ///
    /// ```rust
    /// use tower_mcp::context::RequestContext;
    /// use tower_mcp::error::Error;
    /// use tower_mcp::protocol::ReadResourceResult;
    /// use tower_mcp::resource::ResourceBuilder;
    ///
    /// let resource = ResourceBuilder::new("logs://today")
    ///     .name("Today's logs")
    ///     .handler_with_context(|ctx: RequestContext| async move {
    ///         let mut pages = Vec::new();
    ///         for page in 0..4 {
    ///             if ctx.is_cancelled() {
    ///                 return Err(Error::internal("read cancelled"));
    ///             }
    ///             ctx.report_progress(f64::from(page), Some(4.0), Some("reading"))
    ///                 .await;
    ///             pages.push(format!("page {page}"));
    ///         }
    ///         Ok(ReadResourceResult::text("logs://today", pages.join("\n")))
    ///     })
    ///     .build();
    ///
    /// # tokio_test::block_on(async {
    /// let result = resource.read().await;
    /// let text = result.contents[0].text.as_deref().unwrap();
    /// assert!(text.contains("page 3"), "{text}");
    /// # });
    /// ```
    ///
    /// Returns a builder that still accepts `.layer()` before `.build()`.
    pub fn handler_with_context<F, Fut>(self, handler: F) -> ResourceBuilderWithContextHandler<F>
    where
        F: Fn(RequestContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<ReadResourceResult>> + Send + 'static,
    {
        ResourceBuilderWithContextHandler {
            uri: self.uri,
            name: self.name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            size: self.size,
            annotations: self.annotations,
            handler,
        }
    }

    /// Set an SEP-2322 resource handler that may return input-required.
    #[cfg(feature = "stateless")]
    pub fn mrtr_handler<F, Fut>(self, handler: F) -> ResourceBuilderWithMrtrHandler<F>
    where
        F: Fn(RequestContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<RequestOutcome<ReadResourceResult>>> + Send + 'static,
    {
        ResourceBuilderWithMrtrHandler {
            uri: self.uri,
            name: self.name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            size: self.size,
            annotations: self.annotations,
            handler,
        }
    }

    /// Finish the builder by serving a fixed string.
    ///
    /// The content is captured once, so this is for text that does not change
    /// while the server runs: instructions, a licence, a schema. Anything read
    /// from disk or a database on demand needs [`handler`](Self::handler).
    ///
    /// ```rust
    /// use tower_mcp::resource::ResourceBuilder;
    ///
    /// # tokio_test::block_on(async {
    /// let resource = ResourceBuilder::new("docs://usage")
    ///     .name("Usage")
    ///     .mime_type("text/markdown")
    ///     .text("# Usage\n\nStart the server, then call `tools/list`.");
    ///
    /// let result = resource.read().await;
    /// assert_eq!(
    ///     result.contents[0].mime_type.as_deref(),
    ///     Some("text/markdown")
    /// );
    /// # });
    /// ```
    pub fn text(self, content: impl Into<String>) -> Resource {
        let uri = self.uri.clone();
        let content = content.into();
        let mime_type = self.mime_type.clone();

        self.handler(move || {
            let uri = uri.clone();
            let content = content.clone();
            let mime_type = mime_type.clone();
            async move {
                Ok(ReadResourceResult {
                    contents: vec![ResourceContent {
                        uri,
                        mime_type,
                        text: Some(content),
                        blob: None,
                        meta: None,
                    }],
                    meta: None,
                    ..Default::default()
                })
            }
        })
        .build()
    }

    /// Finish the builder by serving a fixed JSON value.
    ///
    /// The value is serialized once, at build time, and the MIME type is set
    /// to `application/json` whatever [`mime_type`](Self::mime_type) said
    /// earlier, so the declared type cannot drift from the body.
    ///
    /// ```rust
    /// use serde_json::json;
    /// use tower_mcp::resource::ResourceBuilder;
    ///
    /// # tokio_test::block_on(async {
    /// let resource = ResourceBuilder::new("config://limits")
    ///     .name("Limits")
    ///     .json(json!({ "max_connections": 100 }));
    ///
    /// let result = resource.read().await;
    /// assert_eq!(
    ///     result.contents[0].mime_type.as_deref(),
    ///     Some("application/json")
    /// );
    /// # });
    /// ```
    pub fn json(mut self, value: serde_json::Value) -> Resource {
        let uri = self.uri.clone();
        self.mime_type = Some("application/json".to_string());
        let text = serde_json::to_string_pretty(&value).unwrap_or_else(|_| "{}".to_string());

        self.handler(move || {
            let uri = uri.clone();
            let text = text.clone();
            async move {
                Ok(ReadResourceResult {
                    contents: vec![ResourceContent {
                        uri,
                        mime_type: Some("application/json".to_string()),
                        text: Some(text),
                        blob: None,
                        meta: None,
                    }],
                    meta: None,
                    ..Default::default()
                })
            }
        })
        .build()
    }
}

/// Builder state after handler is specified.
///
/// This builder allows applying middleware layers via `.layer()` or building
/// the resource directly via `.build()`.
pub struct ResourceBuilderWithHandler<F> {
    uri: String,
    name: Option<String>,
    title: Option<String>,
    description: Option<String>,
    mime_type: Option<String>,
    icons: Option<Vec<ToolIcon>>,
    size: Option<u64>,
    annotations: Option<ContentAnnotations>,
    handler: F,
}

/// Builder state after an MRTR handler is specified.
///
/// The multi-round-trip form of [`ResourceBuilderWithHandler`], reached by
/// [`ResourceBuilder::mrtr_handler`]. Same choices from here: apply
/// middleware with `.layer()`, or finish with `.build()`.
#[cfg(feature = "stateless")]
pub struct ResourceBuilderWithMrtrHandler<F> {
    uri: String,
    name: Option<String>,
    title: Option<String>,
    description: Option<String>,
    mime_type: Option<String>,
    icons: Option<Vec<ToolIcon>>,
    size: Option<u64>,
    annotations: Option<ContentAnnotations>,
    handler: F,
}

/// Builder state after an MRTR handler and at least one layer.
///
/// Reached by calling `.layer()` on a
/// [`ResourceBuilderWithMrtrHandler`]. Further `.layer()` calls stack onto
/// `L`, outermost last, and `.build()` finishes.
#[cfg(feature = "stateless")]
pub struct ResourceBuilderWithMrtrLayer<F, L> {
    uri: String,
    name: Option<String>,
    title: Option<String>,
    description: Option<String>,
    mime_type: Option<String>,
    icons: Option<Vec<ToolIcon>>,
    size: Option<u64>,
    annotations: Option<ContentAnnotations>,
    handler: F,
    layer: L,
}

#[cfg(feature = "stateless")]
impl<F, Fut> ResourceBuilderWithMrtrHandler<F>
where
    F: Fn(RequestContext) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<RequestOutcome<ReadResourceResult>>> + Send + 'static,
{
    /// Build the resource with an SEP-2322-aware handler.
    pub fn build(self) -> Resource {
        let name = self.name.unwrap_or_else(|| self.uri.clone());

        Resource::from_mrtr_handler(
            self.uri,
            name,
            self.title,
            self.description,
            self.mime_type,
            self.icons,
            self.size,
            self.annotations,
            MrtrContextHandler {
                handler: self.handler,
            },
        )
    }

    /// Apply a Tower layer to every attempt at this MRTR-capable resource.
    ///
    /// Each retry is an independent request, so the layer runs once per
    /// round. A genuine middleware failure is sanitized to a `-32603`
    /// JSON-RPC error, matching non-MRTR resource middleware; the handler's
    /// own error, if any, rides through untouched.
    pub fn layer<L>(self, layer: L) -> ResourceBuilderWithMrtrLayer<F, L> {
        ResourceBuilderWithMrtrLayer {
            uri: self.uri,
            name: self.name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            size: self.size,
            annotations: self.annotations,
            handler: self.handler,
            layer,
        }
    }
}

#[cfg(feature = "stateless")]
#[allow(private_bounds)]
impl<F, Fut, L> ResourceBuilderWithMrtrLayer<F, L>
where
    F: Fn(RequestContext) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<RequestOutcome<ReadResourceResult>>> + Send + 'static,
    L: tower::Layer<MrtrResourceHandlerService<MrtrContextHandler<F>>>
        + Clone
        + Send
        + Sync
        + 'static,
    L::Service: Service<
            ResourceRequest,
            Response = std::result::Result<RequestOutcome<ReadResourceResult>, JsonRpcError>,
        > + Clone
        + Send
        + 'static,
    <L::Service as Service<ResourceRequest>>::Error: fmt::Display + Send + 'static,
    <L::Service as Service<ResourceRequest>>::Future: Send + 'static,
{
    /// Build the MRTR resource with the applied layer(s).
    pub fn build(self) -> Resource {
        let name = self.name.unwrap_or_else(|| self.uri.clone());
        let handler = MrtrContextHandler {
            handler: self.handler,
        };
        let service = MrtrResourceHandlerService::new(handler);
        let service = self.layer.layer(service);
        let service = BoxCloneService::new(MrtrResourceCatchError::new(service));

        Resource {
            uri: self.uri.clone(),
            name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            size: self.size,
            annotations: self.annotations,
            meta: None,
            service: None,
            mrtr_handler: Some(Arc::new(ServiceMrtrResourceHandler {
                service: Mutex::new(service),
                uri: self.uri,
            })),
        }
    }

    /// Apply an additional Tower layer.
    pub fn layer<L2>(
        self,
        layer: L2,
    ) -> ResourceBuilderWithMrtrLayer<F, tower::layer::util::Stack<L2, L>> {
        ResourceBuilderWithMrtrLayer {
            uri: self.uri,
            name: self.name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            size: self.size,
            annotations: self.annotations,
            handler: self.handler,
            layer: tower::layer::util::Stack::new(layer, self.layer),
        }
    }
}

impl<F, Fut> ResourceBuilderWithHandler<F>
where
    F: Fn() -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<ReadResourceResult>> + Send + 'static,
{
    /// Build the resource without any middleware layers.
    pub fn build(self) -> Resource {
        let name = self.name.unwrap_or_else(|| self.uri.clone());

        Resource::from_handler(
            self.uri,
            name,
            self.title,
            self.description,
            self.mime_type,
            self.icons,
            self.size,
            self.annotations,
            FnHandler {
                handler: self.handler,
            },
        )
    }

    /// Apply a Tower layer (middleware) to this resource.
    ///
    /// The layer wraps the resource's handler service, enabling functionality like
    /// timeouts, rate limiting, and metrics collection at the per-resource level.
    ///
    /// # Example
    ///
    /// ```rust
    /// use std::time::Duration;
    /// use tower::timeout::TimeoutLayer;
    /// use tower_mcp::resource::ResourceBuilder;
    /// use tower_mcp::protocol::{ReadResourceResult, ResourceContent};
    ///
    /// let resource = ResourceBuilder::new("file:///slow.txt")
    ///     .name("Slow Resource")
    ///     .handler(|| async {
    ///         Ok(ReadResourceResult {
    ///             contents: vec![ResourceContent {
    ///                 uri: "file:///slow.txt".to_string(),
    ///                 mime_type: Some("text/plain".to_string()),
    ///                 text: Some("content".to_string()),
    ///                 blob: None,
    ///                 meta: None,
    ///             }],
    ///             meta: None,
    ///             ..Default::default()
    ///         })
    ///     })
    ///     .layer(TimeoutLayer::new(Duration::from_secs(30)))
    ///     .build();
    /// ```
    pub fn layer<L>(self, layer: L) -> ResourceBuilderWithLayer<F, L> {
        ResourceBuilderWithLayer {
            uri: self.uri,
            name: self.name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            size: self.size,
            annotations: self.annotations,
            handler: self.handler,
            layer,
        }
    }
}

/// Builder state after a layer has been applied to the handler.
///
/// This builder allows chaining additional layers and building the final resource.
pub struct ResourceBuilderWithLayer<F, L> {
    uri: String,
    name: Option<String>,
    title: Option<String>,
    description: Option<String>,
    mime_type: Option<String>,
    icons: Option<Vec<ToolIcon>>,
    size: Option<u64>,
    annotations: Option<ContentAnnotations>,
    handler: F,
    layer: L,
}

// Allow private_bounds because these internal types (ResourceHandlerService, FnHandler, etc.)
// are implementation details that users don't interact with directly.
#[allow(private_bounds)]
impl<F, Fut, L> ResourceBuilderWithLayer<F, L>
where
    F: Fn() -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<ReadResourceResult>> + Send + 'static,
    L: tower::Layer<ResourceHandlerService<FnHandler<F>>> + Clone + Send + Sync + 'static,
    L::Service: Service<ResourceRequest, Response = std::result::Result<ReadResourceResult, JsonRpcError>>
        + Clone
        + Send
        + 'static,
    <L::Service as Service<ResourceRequest>>::Error: fmt::Display + Send,
    <L::Service as Service<ResourceRequest>>::Future: Send,
{
    /// Build the resource with the applied layer(s).
    pub fn build(self) -> Resource {
        let name = self.name.unwrap_or_else(|| self.uri.clone());

        let handler_service = ResourceHandlerService::new(FnHandler {
            handler: self.handler,
        });
        let layered = self.layer.layer(handler_service);
        let catch_error = ResourceCatchError::new(layered);
        let service = BoxCloneService::new(catch_error);

        Resource {
            uri: self.uri,
            name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            size: self.size,
            annotations: self.annotations,
            meta: None,
            service: Some(service),
            #[cfg(feature = "stateless")]
            mrtr_handler: None,
        }
    }

    /// Apply an additional Tower layer (middleware).
    ///
    /// Layers are applied in order, with earlier layers wrapping later ones.
    /// This means the first layer added is the outermost middleware.
    pub fn layer<L2>(
        self,
        layer: L2,
    ) -> ResourceBuilderWithLayer<F, tower::layer::util::Stack<L2, L>> {
        ResourceBuilderWithLayer {
            uri: self.uri,
            name: self.name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            size: self.size,
            annotations: self.annotations,
            handler: self.handler,
            layer: tower::layer::util::Stack::new(layer, self.layer),
        }
    }
}

/// Builder state after context-aware handler is specified.
pub struct ResourceBuilderWithContextHandler<F> {
    uri: String,
    name: Option<String>,
    title: Option<String>,
    description: Option<String>,
    mime_type: Option<String>,
    icons: Option<Vec<ToolIcon>>,
    size: Option<u64>,
    annotations: Option<ContentAnnotations>,
    handler: F,
}

impl<F, Fut> ResourceBuilderWithContextHandler<F>
where
    F: Fn(RequestContext) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<ReadResourceResult>> + Send + 'static,
{
    /// Build the resource without any middleware layers.
    pub fn build(self) -> Resource {
        let name = self.name.unwrap_or_else(|| self.uri.clone());

        Resource::from_handler(
            self.uri,
            name,
            self.title,
            self.description,
            self.mime_type,
            self.icons,
            self.size,
            self.annotations,
            ContextAwareHandler {
                handler: self.handler,
            },
        )
    }

    /// Apply a Tower layer (middleware) to this resource.
    ///
    /// Works the same as [`ResourceBuilderWithHandler::layer`].
    pub fn layer<L>(self, layer: L) -> ResourceBuilderWithContextLayer<F, L> {
        ResourceBuilderWithContextLayer {
            uri: self.uri,
            name: self.name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            size: self.size,
            annotations: self.annotations,
            handler: self.handler,
            layer,
        }
    }
}

/// Builder state after a layer has been applied to a context-aware handler.
pub struct ResourceBuilderWithContextLayer<F, L> {
    uri: String,
    name: Option<String>,
    title: Option<String>,
    description: Option<String>,
    mime_type: Option<String>,
    icons: Option<Vec<ToolIcon>>,
    size: Option<u64>,
    annotations: Option<ContentAnnotations>,
    handler: F,
    layer: L,
}

// Allow private_bounds because these internal types are implementation details.
#[allow(private_bounds)]
impl<F, Fut, L> ResourceBuilderWithContextLayer<F, L>
where
    F: Fn(RequestContext) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<ReadResourceResult>> + Send + 'static,
    L: tower::Layer<ResourceHandlerService<ContextAwareHandler<F>>> + Clone + Send + Sync + 'static,
    L::Service: Service<ResourceRequest, Response = std::result::Result<ReadResourceResult, JsonRpcError>>
        + Clone
        + Send
        + 'static,
    <L::Service as Service<ResourceRequest>>::Error: fmt::Display + Send,
    <L::Service as Service<ResourceRequest>>::Future: Send,
{
    /// Build the resource with the applied layer(s).
    pub fn build(self) -> Resource {
        let name = self.name.unwrap_or_else(|| self.uri.clone());

        let handler_service = ResourceHandlerService::new(ContextAwareHandler {
            handler: self.handler,
        });
        let layered = self.layer.layer(handler_service);
        let catch_error = ResourceCatchError::new(layered);
        let service = BoxCloneService::new(catch_error);

        Resource {
            uri: self.uri,
            name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            size: self.size,
            annotations: self.annotations,
            meta: None,
            service: Some(service),
            #[cfg(feature = "stateless")]
            mrtr_handler: None,
        }
    }

    /// Apply an additional Tower layer (middleware).
    pub fn layer<L2>(
        self,
        layer: L2,
    ) -> ResourceBuilderWithContextLayer<F, tower::layer::util::Stack<L2, L>> {
        ResourceBuilderWithContextLayer {
            uri: self.uri,
            name: self.name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            size: self.size,
            annotations: self.annotations,
            handler: self.handler,
            layer: tower::layer::util::Stack::new(layer, self.layer),
        }
    }
}

// =============================================================================
// Handler implementations
// =============================================================================

/// Handler wrapping a function
struct FnHandler<F> {
    handler: F,
}

impl<F, Fut> ResourceHandler for FnHandler<F>
where
    F: Fn() -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<ReadResourceResult>> + Send + 'static,
{
    fn read(&self) -> BoxFuture<'_, Result<ReadResourceResult>> {
        Box::pin((self.handler)())
    }
}

/// Handler that receives request context
struct ContextAwareHandler<F> {
    handler: F,
}

impl<F, Fut> ResourceHandler for ContextAwareHandler<F>
where
    F: Fn(RequestContext) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<ReadResourceResult>> + Send + 'static,
{
    fn read(&self) -> BoxFuture<'_, Result<ReadResourceResult>> {
        let ctx = RequestContext::new(crate::protocol::RequestId::Number(0));
        self.read_with_context(ctx)
    }

    fn read_with_context(&self, ctx: RequestContext) -> BoxFuture<'_, Result<ReadResourceResult>> {
        Box::pin((self.handler)(ctx))
    }
}

#[cfg(feature = "stateless")]
struct MrtrContextHandler<F> {
    handler: F,
}

#[cfg(feature = "stateless")]
impl<F, Fut> MrtrResourceHandler for MrtrContextHandler<F>
where
    F: Fn(RequestContext) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<RequestOutcome<ReadResourceResult>>> + Send + 'static,
{
    fn read(
        &self,
        ctx: RequestContext,
    ) -> BoxFuture<'_, Result<RequestOutcome<ReadResourceResult>>> {
        Box::pin((self.handler)(ctx))
    }
}

// =============================================================================
// Trait-based resource definition
// =============================================================================

/// Define a resource as a type rather than a closure.
///
/// The URI, name, description, and MIME type are associated constants, so they
/// are decided at compile time and the type owns whatever state the read needs.
/// A resource whose identity is only known at runtime belongs in
/// [`ResourceBuilder`] instead. Finish with
/// [`into_resource`](Self::into_resource), which produces the same [`Resource`]
/// the builder does.
///
/// # Example
///
/// ```rust
/// use tower_mcp::{McpResource, ReadResourceResult, ResourceContent, Result};
///
/// struct ConfigResource {
///     config: String,
/// }
///
/// impl McpResource for ConfigResource {
///     const URI: &'static str = "file:///config.json";
///     const NAME: &'static str = "Configuration";
///     const DESCRIPTION: Option<&'static str> = Some("Application configuration");
///     const MIME_TYPE: Option<&'static str> = Some("application/json");
///
///     async fn read(&self) -> Result<ReadResourceResult> {
///         Ok(ReadResourceResult {
///             contents: vec![ResourceContent {
///                 uri: Self::URI.to_string(),
///                 mime_type: Self::MIME_TYPE.map(|s| s.to_string()),
///                 text: Some(self.config.clone()),
///                 blob: None,
///                 meta: None,
///             }],
///             meta: None,
///             ..Default::default()
///         })
///     }
/// }
///
/// let resource = ConfigResource { config: "{}".to_string() }.into_resource();
/// assert_eq!(resource.uri, "file:///config.json");
/// ```
pub trait McpResource: Send + Sync + 'static {
    /// The resource URI.
    const URI: &'static str;
    /// The resource name.
    const NAME: &'static str;
    /// Optional human-readable description.
    const DESCRIPTION: Option<&'static str> = None;
    /// Optional MIME type for the resource content.
    const MIME_TYPE: Option<&'static str> = None;

    /// Read the resource content.
    fn read(&self) -> impl Future<Output = Result<ReadResourceResult>> + Send;

    /// Convert to a Resource instance
    fn into_resource(self) -> Resource
    where
        Self: Sized,
    {
        let resource = Arc::new(self);
        Resource::from_handler(
            Self::URI.to_string(),
            Self::NAME.to_string(),
            None,
            Self::DESCRIPTION.map(|s| s.to_string()),
            Self::MIME_TYPE.map(|s| s.to_string()),
            None,
            None,
            None,
            McpResourceHandler { resource },
        )
    }
}

/// Wrapper to make McpResource implement ResourceHandler
struct McpResourceHandler<T: McpResource> {
    resource: Arc<T>,
}

impl<T: McpResource> ResourceHandler for McpResourceHandler<T> {
    fn read(&self) -> BoxFuture<'_, Result<ReadResourceResult>> {
        let resource = self.resource.clone();
        Box::pin(async move { resource.read().await })
    }
}

// =============================================================================
// Resource Templates
// =============================================================================

/// The shape a resource-template handler is adapted to internally.
///
/// Unlike [`ResourceHandler`], a template handler is told which URI matched
/// and what was extracted from it, because one handler serves many URIs.
/// Templates are built from closures through
/// [`ResourceTemplateBuilder::handler`]; no public constructor accepts an
/// implementation of this trait.
pub(crate) trait ResourceTemplateHandler: Send + Sync {
    /// Read a resource with the given URI variables extracted from the template
    fn read(
        &self,
        uri: &str,
        variables: HashMap<String, String>,
    ) -> BoxFuture<'_, Result<ReadResourceResult>>;
}

/// Resource-template handler that may return an SEP-2322 continuation.
#[cfg(feature = "stateless")]
pub(crate) trait MrtrResourceTemplateHandler: Send + Sync {
    /// Read a matched template resource with retry values in the context.
    fn read(
        &self,
        ctx: RequestContext,
        uri: &str,
        variables: HashMap<String, String>,
    ) -> BoxFuture<'_, Result<RequestOutcome<ReadResourceResult>>>;
}

/// A resource whose URI is a pattern rather than a constant.
///
/// The pattern is compiled once, when the handler is attached, and every
/// `resources/read` that does not name a registered [`Resource`] is offered to
/// each template in registration order. The first match wins, and the
/// variables it extracted are passed to the handler together with the URI that
/// produced them, so one handler serves a whole family of URIs: a filesystem
/// subtree, a table of records, a paged listing.
///
/// [`match_uri`](Self::match_uri) documents the matching rules and shows them
/// running.
///
/// # Router authorization
///
/// Registering a template on an [`McpRouter`](crate::McpRouter) lets a
/// [`ResourceTemplateFilter`](crate::ResourceTemplateFilter) control both its
/// definition in `resources/templates/list` and reads resolved through it. For
/// a read, [`CapabilityFilterContext::target`](crate::CapabilityFilterContext::target)
/// is the concrete URI rather than the URI pattern, so a policy can expose a
/// template while protecting particular members of the resource family. The
/// router performs this check before either an ordinary or an MRTR handler is
/// invoked.
///
/// A router with a [`ResourceFilter`](crate::ResourceFilter) but no resource
/// template filter fails closed for templates. Configure
/// [`McpRouter::resource_template_filter`](crate::McpRouter::resource_template_filter)
/// explicitly when such a server intends to expose templates. With neither
/// filter configured, templates retain their default public behavior.
///
/// [`McpRouter::disable_resource`](crate::McpRouter::disable_resource) is
/// evaluated against the concrete requested URI before template matching. It
/// therefore blocks that URI without hiding the template definition or
/// disabling sibling URIs that the same template serves.
///
/// # Example
///
/// ```rust
/// use std::collections::HashMap;
/// use serde_json::json;
/// use tower_mcp::protocol::ReadResourceResult;
/// use tower_mcp::resource::ResourceTemplateBuilder;
///
/// let template = ResourceTemplateBuilder::new("db://users/{id}")
///     .name("User Records")
///     .description("One user record per id")
///     .handler(|uri: String, vars: HashMap<String, String>| async move {
///         let id = vars["id"].clone();
///         Ok(ReadResourceResult::json(uri, &json!({ "id": id })))
///     });
///
/// # tokio_test::block_on(async {
/// let vars = template.match_uri("db://users/42").expect("42 is one segment");
/// assert_eq!(vars["id"], "42");
///
/// let result = template.read("db://users/42", vars).await.unwrap();
/// assert!(result.contents[0].text.as_deref().unwrap().contains("42"));
/// # });
///
/// // `{id}` stops at a slash, so a deeper URI is left for another template.
/// assert!(template.match_uri("db://users/42/posts").is_none());
/// ```
pub struct ResourceTemplate {
    /// The URI template pattern (e.g., `file:///{path}`)
    pub uri_template: String,
    /// Human-readable name
    pub name: String,
    /// Human-readable title for display purposes
    pub title: Option<String>,
    /// Optional description
    pub description: Option<String>,
    /// Optional MIME type hint
    pub mime_type: Option<String>,
    /// Optional icons for display in user interfaces
    pub icons: Option<Vec<ToolIcon>>,
    /// Optional annotations (audience, priority hints)
    pub annotations: Option<ContentAnnotations>,
    /// Arguments this template accepts for URI expansion, as declared by
    /// [`ResourceTemplateBuilder::argument`]. Empty unless declared (#1282).
    pub arguments: Vec<PromptArgument>,
    /// Protocol-level `_meta`, set by [`with_meta`](Self::with_meta).
    meta: Option<Value>,
    /// Compiled regex for matching URIs
    pattern: regex::Regex,
    /// Variables declared by a form-style query expression, empty when the
    /// template has none (#1253).
    query_variables: Vec<String>,
    /// Variable names in order of appearance
    variables: Vec<String>,
    /// Handler for reading matched resources
    handler: Option<Arc<dyn ResourceTemplateHandler>>,
    #[cfg(feature = "stateless")]
    mrtr_handler: Option<Arc<dyn MrtrResourceTemplateHandler>>,
}

impl Clone for ResourceTemplate {
    fn clone(&self) -> Self {
        Self {
            uri_template: self.uri_template.clone(),
            name: self.name.clone(),
            title: self.title.clone(),
            description: self.description.clone(),
            mime_type: self.mime_type.clone(),
            icons: self.icons.clone(),
            annotations: self.annotations.clone(),
            arguments: self.arguments.clone(),
            meta: self.meta.clone(),
            pattern: self.pattern.clone(),
            query_variables: self.query_variables.clone(),
            variables: self.variables.clone(),
            handler: self.handler.clone(),
            #[cfg(feature = "stateless")]
            mrtr_handler: self.mrtr_handler.clone(),
        }
    }
}

impl std::fmt::Debug for ResourceTemplate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ResourceTemplate")
            .field("uri_template", &self.uri_template)
            .field("name", &self.name)
            .field("title", &self.title)
            .field("description", &self.description)
            .field("mime_type", &self.mime_type)
            .field("icons", &self.icons)
            .field("variables", &self.variables)
            .finish_non_exhaustive()
    }
}

impl ResourceTemplate {
    /// Create a new resource template builder
    pub fn builder(uri_template: impl Into<String>) -> ResourceTemplateBuilder {
        ResourceTemplateBuilder::new(uri_template)
    }

    /// The entry this template contributes to `resources/templates/list`.
    ///
    /// It advertises the pattern rather than deriving arguments from it: a
    /// client expands the pattern itself, and `arguments` carries only what
    /// [`ResourceTemplateBuilder::argument`] declared, which is nothing
    /// unless the author said otherwise (#1282).
    pub fn definition(&self) -> ResourceTemplateDefinition {
        ResourceTemplateDefinition {
            uri_template: self.uri_template.clone(),
            name: self.name.clone(),
            title: self.title.clone(),
            description: self.description.clone(),
            mime_type: self.mime_type.clone(),
            icons: self.icons.clone(),
            annotations: self.annotations.clone(),
            arguments: self.arguments.clone(),
            meta: self.meta.clone(),
        }
    }

    /// Attach `_meta` to what `resources/templates/list` publishes for this
    /// template.
    ///
    /// The counterpart of [`Resource::with_meta`], and validated the same
    /// way: keys must satisfy the protocol's `_meta` rules, so a bad key is
    /// rejected here rather than serialized onto the wire (#1282).
    ///
    /// ```rust
    /// use serde_json::json;
    /// use tower_mcp::ResourceTemplate;
    ///
    /// # fn template() -> ResourceTemplate {
    /// ResourceTemplate::builder("file:///{path}")
    ///     .name("files")
    ///     .handler(|_uri: String, _vars: std::collections::HashMap<String, String>| async move {
    ///         Ok(tower_mcp::protocol::ReadResourceResult::text("file:///a", ""))
    ///     })
    /// # }
    /// let tagged = template()
    ///     .with_meta(json!({ "example.com/audience": "internal" }))
    ///     .expect("a valid _meta key");
    /// assert!(tagged.definition().meta.is_some());
    ///
    /// // A leading slash is not a valid key, and is refused rather than sent.
    /// assert!(template().with_meta(json!({ "/nope": true })).is_err());
    /// ```
    pub fn with_meta(
        mut self,
        meta: Value,
    ) -> std::result::Result<Self, crate::protocol::MetaValidationError> {
        crate::protocol::validate_meta_object(&meta)?;
        self.meta = Some(meta);
        Ok(self)
    }

    /// Match a URI against this template and extract its variables.
    ///
    /// `None` means this template does not serve that URI, which is the
    /// router's signal to try the next one. The whole URI must match, not a
    /// prefix of it.
    ///
    /// # Path expansion
    ///
    /// `{var}` matches a run of non-slash characters and `{+var}` matches
    /// anything at all. That single difference decides whether a template
    /// routes one path segment or an entire subtree:
    ///
    /// ```rust
    /// use std::collections::HashMap;
    /// use tower_mcp::protocol::ReadResourceResult;
    /// use tower_mcp::resource::{ResourceTemplate, ResourceTemplateBuilder};
    ///
    /// fn template(pattern: &str) -> ResourceTemplate {
    ///     ResourceTemplateBuilder::new(pattern).name("example").handler(
    ///         |uri: String, _vars: HashMap<String, String>| async move {
    ///             Ok(ReadResourceResult::text(uri, "body"))
    ///         },
    ///     )
    /// }
    ///
    /// let segment = template("db://users/{id}");
    /// assert_eq!(segment.match_uri("db://users/42").unwrap()["id"], "42");
    /// assert!(segment.match_uri("db://users/42/posts").is_none());
    ///
    /// let subtree = template("file:///{+path}");
    /// assert_eq!(
    ///     subtree.match_uri("file:///src/lib.rs").unwrap()["path"],
    ///     "src/lib.rs"
    /// );
    ///
    /// // Both forms require something to capture: neither matches empty.
    /// assert!(segment.match_uri("db://users/").is_none());
    /// ```
    ///
    /// # Query expansion
    ///
    /// `{?a,b}`, and the `{&a,b}` continuation form, declare query parameters
    /// (#1253). They are matched against the URI's query string rather than
    /// compiled into the pattern, because a regex cannot reasonably express
    /// "any subset of these, in any order".
    ///
    /// RFC 6570 defines expansion rather than matching, so the routing policy
    /// is this crate's:
    ///
    /// - every declared variable is optional, and the bare URI still matches
    /// - order is not significant
    /// - present with no value is an empty string, and distinct from absent
    /// - undeclared keys are ignored rather than rejected, so a caller
    ///   appending a tracking parameter cannot break routing
    /// - the first occurrence of a repeated key wins
    /// - values are percent-decoded and `+` reads as a space; a malformed
    ///   escape is left as written rather than dropped, so a bad value still
    ///   reaches the handler instead of vanishing
    ///
    /// Path captures are handed over exactly as matched. That asymmetry is
    /// deliberate: query strings are form-encoded by convention, and path
    /// captures were never decoded, so decoding them now would silently change
    /// what existing handlers receive.
    ///
    /// ```rust
    /// # use std::collections::HashMap;
    /// # use tower_mcp::protocol::ReadResourceResult;
    /// # use tower_mcp::resource::{ResourceTemplate, ResourceTemplateBuilder};
    /// # fn template(pattern: &str) -> ResourceTemplate {
    /// #     ResourceTemplateBuilder::new(pattern).name("example").handler(
    /// #         |uri: String, _vars: HashMap<String, String>| async move {
    /// #             Ok(ReadResourceResult::text(uri, "body"))
    /// #         },
    /// #     )
    /// # }
    /// let threads = template("agent://threads{?cursor,limit}");
    ///
    /// // Nothing supplied still routes here, with no variables.
    /// assert!(threads.match_uri("agent://threads").unwrap().is_empty());
    ///
    /// // A subset in any order, each variable under its own name.
    /// let vars = threads.match_uri("agent://threads?limit=20").unwrap();
    /// assert_eq!(vars["limit"], "20");
    /// assert!(!vars.contains_key("cursor"));
    ///
    /// // Present-but-empty is a different fact from absent.
    /// let vars = threads.match_uri("agent://threads?cursor=").unwrap();
    /// assert_eq!(vars["cursor"], "");
    ///
    /// // An undeclared key is ignored; a repeated key keeps the first.
    /// let vars = threads
    ///     .match_uri("agent://threads?utm=ad&cursor=one&cursor=two")
    ///     .unwrap();
    /// assert_eq!(vars["cursor"], "one");
    /// assert!(!vars.contains_key("utm"));
    ///
    /// // Query values are decoded; the path capture beside them is not.
    /// let files = template("file:///{name}{?rev}");
    /// let vars = files.match_uri("file:///a%20b?rev=a+b").unwrap();
    /// assert_eq!(vars["name"], "a%20b");
    /// assert_eq!(vars["rev"], "a b");
    ///
    /// // A template that declares no query expression never splits on `?`,
    /// // so a literal `?` in the pattern keeps matching literally.
    /// let literal = template("http://example.com/api?query={q}");
    /// assert_eq!(
    ///     literal.match_uri("http://example.com/api?query=hello").unwrap()["q"],
    ///     "hello"
    /// );
    /// ```
    pub fn match_uri(&self, uri: &str) -> Option<HashMap<String, String>> {
        // Only a template that declares a query expression splits the URI.
        // Without one the pattern matches the whole string, including any
        // literal `?`, exactly as it did before query support existed.
        let (path, query) = if self.query_variables.is_empty() {
            (uri, None)
        } else {
            match uri.split_once('?') {
                Some((path, query)) => (path, Some(query)),
                None => (uri, None),
            }
        };

        let mut matched: HashMap<String, String> = self.pattern.captures(path).map(|caps| {
            self.variables
                .iter()
                .enumerate()
                .filter_map(|(i, name)| {
                    caps.get(i + 1)
                        .map(|m| (name.clone(), m.as_str().to_string()))
                })
                .collect()
        })?;

        if let Some(query) = query {
            matched.extend(extract_query_variables(query, &self.query_variables));
        }
        Some(matched)
    }

    /// Read one URI through this template's handler.
    ///
    /// `variables` is what [`match_uri`](Self::match_uri) returned for `uri`.
    /// Nothing re-derives or checks it here, so a map taken from a different
    /// URI is passed to the handler as given.
    ///
    /// Unlike [`Resource::read`], this returns a `Result`. Template handlers
    /// are not wrapped in an error-catching service, so an `Err` stays an
    /// error and the router turns it into a JSON-RPC error rather than into
    /// resource content.
    ///
    /// A template built with `mrtr_handler` has no plain handler to call and
    /// reports that as an error here; use
    /// [`read_outcome_with_context`](Self::read_outcome_with_context).
    ///
    /// This is a direct application call and does not pass through an
    /// [`McpRouter`](crate::McpRouter), so it does not evaluate the router's
    /// resource-template filter. Callers using a template outside the router
    /// are responsible for their own authorization.
    pub fn read(
        &self,
        uri: &str,
        variables: HashMap<String, String>,
    ) -> BoxFuture<'_, Result<ReadResourceResult>> {
        match &self.handler {
            Some(handler) => handler.read(uri, variables),
            None => Box::pin(async {
                Err(Error::invalid_params(
                    "MRTR resource template requires read_outcome_with_context",
                ))
            }),
        }
    }

    /// Read a matched resource while preserving an SEP-2322 continuation.
    ///
    /// Calling this method directly does not evaluate an
    /// [`McpRouter`](crate::McpRouter) resource-template filter. The router
    /// performs that authorization before invoking this method; other callers
    /// must enforce their own policy.
    pub fn read_outcome_with_context(
        &self,
        ctx: RequestContext,
        uri: &str,
        variables: HashMap<String, String>,
    ) -> BoxFuture<'_, Result<RequestOutcome<ReadResourceResult>>> {
        let _ = &ctx;
        #[cfg(feature = "stateless")]
        if let Some(handler) = &self.mrtr_handler {
            return handler.read(ctx, uri, variables);
        }
        match &self.handler {
            Some(handler) => {
                let handler = handler.clone();
                let uri = uri.to_string();
                Box::pin(async move {
                    handler
                        .read(&uri, variables)
                        .await
                        .map(RequestOutcome::Complete)
                })
            }
            None => Box::pin(async {
                Err(Error::invalid_params(
                    "resource template has neither a complete nor MRTR handler",
                ))
            }),
        }
    }
}

/// Builder for a [`ResourceTemplate`].
///
/// The chain ends at [`handler`](Self::handler), which compiles the pattern
/// and returns the template itself; there is no separate `build` step. The
/// name defaults to the pattern when it is not set.
///
/// # Example
///
/// A paged listing, where the page cursor is a declared query variable and the
/// handler has to cope with its absence because every query variable is
/// optional:
///
/// ```rust
/// use std::collections::HashMap;
/// use tower_mcp::protocol::ReadResourceResult;
/// use tower_mcp::resource::ResourceTemplateBuilder;
///
/// let template = ResourceTemplateBuilder::new("agent://threads{?cursor,limit}")
///     .name("Threads")
///     .description("Conversation threads, newest first")
///     .mime_type("application/json")
///     .handler(|uri: String, vars: HashMap<String, String>| async move {
///         let cursor = vars.get("cursor").map(String::as_str).unwrap_or("start");
///         let limit: usize = vars
///             .get("limit")
///             .and_then(|value| value.parse().ok())
///             .unwrap_or(50);
///         Ok(ReadResourceResult::text(
///             uri,
///             format!("{limit} threads from {cursor}"),
///         ))
///     });
///
/// # tokio_test::block_on(async {
/// let vars = template.match_uri("agent://threads").unwrap();
/// let result = template.read("agent://threads", vars).await.unwrap();
/// assert_eq!(
///     result.contents[0].text.as_deref(),
///     Some("50 threads from start")
/// );
/// # });
/// ```
pub struct ResourceTemplateBuilder {
    uri_template: String,
    name: Option<String>,
    title: Option<String>,
    description: Option<String>,
    mime_type: Option<String>,
    icons: Option<Vec<ToolIcon>>,
    annotations: Option<ContentAnnotations>,
    arguments: Vec<PromptArgument>,
}

impl ResourceTemplateBuilder {
    /// Start a template from an RFC 6570 URI pattern.
    ///
    /// The pattern is not compiled until a handler is attached, so a mistake
    /// in it surfaces at [`handler`](Self::handler) (a panic) or
    /// [`try_handler`](Self::try_handler) (an error), not here.
    ///
    /// # Supported expansions
    ///
    /// | form | matches | example |
    /// |---|---|---|
    /// | `{var}` | any run of non-slash characters | `db://users/{id}` matches `db://users/123` |
    /// | `{+var}` | any characters, slashes included | `file:///{+path}` matches `file:///src/lib.rs` |
    /// | `{?a,b}`, `{&a,b}` | optional query parameters (#1253) | `agent://threads{?cursor,limit}` matches `agent://threads` and `agent://threads?limit=20` |
    ///
    /// Everything else in the pattern is literal, including a `?` in a
    /// template that declares no query expression.
    /// [`ResourceTemplate::match_uri`] documents the matching rules in full,
    /// with the query routing policy this crate settled on where RFC 6570
    /// defines expansion but not matching.
    ///
    /// # Rejected at build time
    ///
    /// A query expression describes the query string, so nothing can follow
    /// it: trailing text, a second query expression, and an empty variable
    /// name are all reported rather than left to mismatch silently at
    /// request time.
    pub fn new(uri_template: impl Into<String>) -> Self {
        Self {
            uri_template: uri_template.into(),
            name: None,
            title: None,
            description: None,
            mime_type: None,
            icons: None,
            annotations: None,
            arguments: Vec::new(),
        }
    }

    /// Set the human-readable name for this template
    pub fn name(mut self, name: impl Into<String>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set a human-readable title for the template
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set the description for this template
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Set the MIME type hint for resources from this template
    pub fn mime_type(mut self, mime_type: impl Into<String>) -> Self {
        self.mime_type = Some(mime_type.into());
        self
    }

    /// Add an icon for the template
    pub fn icon(mut self, src: impl Into<String>) -> Self {
        self.icons.get_or_insert_with(Vec::new).push(ToolIcon {
            src: src.into(),
            mime_type: None,
            sizes: None,
            theme: None,
        });
        self
    }

    /// Add an icon with metadata
    pub fn icon_with_meta(
        mut self,
        src: impl Into<String>,
        mime_type: Option<String>,
        sizes: Option<Vec<String>>,
    ) -> Self {
        self.icons.get_or_insert_with(Vec::new).push(ToolIcon {
            src: src.into(),
            mime_type,
            sizes,
            theme: None,
        });
        self
    }

    /// Set annotations (audience, priority hints) for this resource template
    pub fn annotations(mut self, annotations: ContentAnnotations) -> Self {
        self.annotations = Some(annotations);
        self
    }

    /// Declare an argument this template accepts for URI expansion.
    ///
    /// Arguments are advertised on `resources/templates/list`, and are for the
    /// client's benefit rather than the router's: matching a URI still comes
    /// from the compiled pattern, so declaring an argument neither adds a
    /// variable nor validates one. Nothing is derived from the pattern
    /// automatically, because a variable name alone carries no description and
    /// no honest answer about whether it is required (#1282).
    ///
    /// ```rust
    /// use tower_mcp::ResourceTemplate;
    ///
    /// let template = ResourceTemplate::builder("db://{table}{?limit}")
    ///     .name("rows")
    ///     .argument("table", Some("Table to read from"), true)
    ///     .argument("limit", Some("Maximum rows to return"), false)
    ///     .handler(|_uri: String, _vars: std::collections::HashMap<String, String>| async move {
    ///         Ok(tower_mcp::protocol::ReadResourceResult::text("db://t", "[]"))
    ///     });
    ///
    /// let definition = template.definition();
    /// assert_eq!(definition.arguments.len(), 2);
    /// assert!(definition.arguments[0].required);
    /// assert!(!definition.arguments[1].required);
    /// ```
    pub fn argument(
        mut self,
        name: impl Into<String>,
        description: Option<impl Into<String>>,
        required: bool,
    ) -> Self {
        self.arguments.push(PromptArgument {
            name: name.into(),
            description: description.map(Into::into),
            required,
        });
        self
    }

    /// Attach the handler, compile the pattern, and return the template.
    ///
    /// The handler is called with the URI that was requested and the variables
    /// [`ResourceTemplate::match_uri`] pulled out of it. Only variables that
    /// actually matched are present: path variables always are, query
    /// variables only when the request supplied them.
    ///
    /// ```rust
    /// use std::collections::HashMap;
    /// use tower_mcp::protocol::ReadResourceResult;
    /// use tower_mcp::resource::ResourceTemplateBuilder;
    ///
    /// let template = ResourceTemplateBuilder::new("api://v1/{collection}/{id}")
    ///     .name("API records")
    ///     .handler(|uri: String, vars: HashMap<String, String>| async move {
    ///         let body = format!("{} #{}", vars["collection"], vars["id"]);
    ///         Ok(ReadResourceResult::text(uri, body))
    ///     });
    ///
    /// # tokio_test::block_on(async {
    /// let uri = "api://v1/posts/456";
    /// let vars = template.match_uri(uri).unwrap();
    /// let result = template.read(uri, vars).await.unwrap();
    /// assert_eq!(result.contents[0].text.as_deref(), Some("posts #456"));
    /// # });
    /// ```
    ///
    /// Indexing the map, as above, is safe for a path variable the pattern
    /// declares and a panic for anything else. Use `get` for query variables.
    ///
    /// # Panics
    ///
    /// Panics if the URI template is not a valid pattern. That is the right
    /// trade for a template written as a literal, since it fails on the first
    /// run rather than on the first request. For a pattern that comes from
    /// configuration or user input, use [`try_handler`](Self::try_handler).
    pub fn handler<F, Fut>(self, handler: F) -> ResourceTemplate
    where
        F: Fn(String, HashMap<String, String>) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<ReadResourceResult>> + Send + 'static,
    {
        self.try_handler(handler).unwrap_or_else(|e| {
            panic!("Invalid URI template: {e}");
        })
    }

    /// The fallible form of [`handler`](Self::handler).
    ///
    /// Reach for this when the pattern is not a literal in the source: a
    /// plugin manifest, a config file, an argument. A bad pattern then
    /// disables one template with a reportable error instead of taking down
    /// the server that loaded it.
    ///
    /// ```rust
    /// use std::collections::HashMap;
    /// use tower_mcp::protocol::ReadResourceResult;
    /// use tower_mcp::resource::ResourceTemplateBuilder;
    ///
    /// fn load(pattern: &str) -> Result<(), String> {
    ///     ResourceTemplateBuilder::new(pattern)
    ///         .name("configured")
    ///         .try_handler(|uri: String, _vars: HashMap<String, String>| async move {
    ///             Ok(ReadResourceResult::text(uri, "body"))
    ///         })
    ///         .map(|_template| ())
    ///         .map_err(|error| error.to_string())
    /// }
    ///
    /// assert!(load("agent://threads{?cursor}").is_ok());
    ///
    /// // A query expression describes the end of the URI, so nothing may
    /// // follow it.
    /// let error = load("agent://threads{?cursor}/latest").unwrap_err();
    /// assert!(error.contains("after its query expression"), "{error}");
    /// ```
    pub fn try_handler<F, Fut>(self, handler: F) -> std::result::Result<ResourceTemplate, Error>
    where
        F: Fn(String, HashMap<String, String>) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<ReadResourceResult>> + Send + 'static,
    {
        let CompiledTemplate {
            pattern,
            variables,
            query_variables,
        } = compile_uri_template(&self.uri_template)?;
        let name = self.name.unwrap_or_else(|| self.uri_template.clone());

        Ok(ResourceTemplate {
            uri_template: self.uri_template,
            name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            annotations: self.annotations,
            arguments: self.arguments,
            meta: None,
            pattern,
            query_variables,
            variables,
            handler: Some(Arc::new(FnTemplateHandler { handler })),
            #[cfg(feature = "stateless")]
            mrtr_handler: None,
        })
    }

    /// Set an SEP-2322-aware template handler that may require client input.
    ///
    /// # Panics
    ///
    /// Panics if the URI template produces an invalid regex pattern. Use
    /// [`try_mrtr_handler`](Self::try_mrtr_handler) for a fallible variant.
    #[cfg(feature = "stateless")]
    pub fn mrtr_handler<F, Fut>(self, handler: F) -> ResourceTemplate
    where
        F: Fn(RequestContext, String, HashMap<String, String>) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<RequestOutcome<ReadResourceResult>>> + Send + 'static,
    {
        self.try_mrtr_handler(handler)
            .unwrap_or_else(|error| panic!("Invalid URI template: {error}"))
    }

    /// Fallible variant of [`mrtr_handler`](Self::mrtr_handler).
    #[cfg(feature = "stateless")]
    pub fn try_mrtr_handler<F, Fut>(
        self,
        handler: F,
    ) -> std::result::Result<ResourceTemplate, Error>
    where
        F: Fn(RequestContext, String, HashMap<String, String>) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<RequestOutcome<ReadResourceResult>>> + Send + 'static,
    {
        let CompiledTemplate {
            pattern,
            variables,
            query_variables,
        } = compile_uri_template(&self.uri_template)?;
        let name = self.name.unwrap_or_else(|| self.uri_template.clone());

        Ok(ResourceTemplate {
            uri_template: self.uri_template,
            name,
            title: self.title,
            description: self.description,
            mime_type: self.mime_type,
            icons: self.icons,
            annotations: self.annotations,
            arguments: self.arguments,
            meta: None,
            pattern,
            query_variables,
            variables,
            handler: None,
            mrtr_handler: Some(Arc::new(MrtrFnTemplateHandler { handler })),
        })
    }
}

/// Handler wrapping a function for templates
struct FnTemplateHandler<F> {
    handler: F,
}

impl<F, Fut> ResourceTemplateHandler for FnTemplateHandler<F>
where
    F: Fn(String, HashMap<String, String>) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<ReadResourceResult>> + Send + 'static,
{
    fn read(
        &self,
        uri: &str,
        variables: HashMap<String, String>,
    ) -> BoxFuture<'_, Result<ReadResourceResult>> {
        let uri = uri.to_string();
        Box::pin((self.handler)(uri, variables))
    }
}

#[cfg(feature = "stateless")]
struct MrtrFnTemplateHandler<F> {
    handler: F,
}

#[cfg(feature = "stateless")]
impl<F, Fut> MrtrResourceTemplateHandler for MrtrFnTemplateHandler<F>
where
    F: Fn(RequestContext, String, HashMap<String, String>) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<RequestOutcome<ReadResourceResult>>> + Send + 'static,
{
    fn read(
        &self,
        ctx: RequestContext,
        uri: &str,
        variables: HashMap<String, String>,
    ) -> BoxFuture<'_, Result<RequestOutcome<ReadResourceResult>>> {
        Box::pin((self.handler)(ctx, uri.to_string(), variables))
    }
}

/// A URI template compiled for matching.
struct CompiledTemplate {
    /// Matches the part of a URI before any query string.
    pattern: regex::Regex,
    /// Path variables, in capture order.
    variables: Vec<String>,
    /// Variables declared by a form-style query expression, if any.
    ///
    /// Empty for a template without one, which is what keeps such templates
    /// matching a literal `?` exactly as before.
    query_variables: Vec<String>,
}

/// Compile a URI template into a regex pattern and extract variable names.
///
/// Supports:
/// - `{var}`, simple expansion, matching any characters except `/`
/// - `{+var}`, reserved expansion, matching any characters
/// - `{?a,b}` and `{&a,b}`, form-style query expansion (RFC 6570 section
///   3.2.8), matched against the URI's query string rather than the regex
///
/// A query expression must be the last thing in the template, since it
/// describes the query string and nothing can follow that (#1253).
fn compile_uri_template(template: &str) -> std::result::Result<CompiledTemplate, Error> {
    let mut pattern = String::from("^");
    let mut variables = Vec::new();
    let mut query_variables: Vec<String> = Vec::new();

    let mut chars = template.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '{' {
            // Form-style query expansion describes the query string, so it is
            // matched separately rather than compiled into the regex.
            if matches!(chars.peek(), Some('?') | Some('&')) {
                chars.next();
                let body: String = chars.by_ref().take_while(|&c| c != '}').collect();
                if !query_variables.is_empty() {
                    return Err(Error::Internal(format!(
                        "URI template '{template}' declares more than one query expression"
                    )));
                }
                for name in body.split(',') {
                    let name = name.trim();
                    if name.is_empty() {
                        return Err(Error::Internal(format!(
                            "URI template '{template}' has an empty query variable name"
                        )));
                    }
                    query_variables.push(name.to_string());
                }
                if chars.peek().is_some() {
                    return Err(Error::Internal(format!(
                        "URI template '{template}' has text after its query expression, \
                         which describes the end of the URI"
                    )));
                }
                continue;
            }

            // Check for + prefix (reserved expansion)
            let is_reserved = chars.peek() == Some(&'+');
            if is_reserved {
                chars.next();
            }

            // Collect variable name
            let var_name: String = chars.by_ref().take_while(|&c| c != '}').collect();
            variables.push(var_name);

            // Choose pattern based on expansion type
            if is_reserved {
                // Reserved expansion - match anything
                pattern.push_str("(.+)");
            } else {
                // Simple expansion - match non-slash characters
                pattern.push_str("([^/]+)");
            }
        } else {
            // Escape regex special characters
            match c {
                '.' | '+' | '*' | '?' | '^' | '$' | '(' | ')' | '[' | ']' | '{' | '}' | '|'
                | '\\' => {
                    pattern.push('\\');
                    pattern.push(c);
                }
                _ => pattern.push(c),
            }
        }
    }

    pattern.push('$');

    let regex = regex::Regex::new(&pattern)
        .map_err(|e| Error::Internal(format!("Invalid URI template '{}': {}", template, e)))?;

    Ok(CompiledTemplate {
        pattern: regex,
        variables,
        query_variables,
    })
}

/// Percent-decode a query component, treating `+` as a space.
///
/// Kept local rather than pulling in a decoder: resource templates are core,
/// and the crate's only percent-encoding dependency is optional and enabled
/// by an unrelated feature.
///
/// A malformed escape is left as written rather than dropped, so a bad value
/// still reaches the handler instead of silently vanishing from the map.
fn decode_query_component(value: &str) -> String {
    let bytes = value.as_bytes();
    let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        match bytes[i] {
            b'+' => {
                out.push(b' ');
                i += 1;
            }
            // Decoded from the byte array rather than by slicing the
            // string. `&value[i + 1..i + 3]` panics when either index lands
            // inside a multibyte character, which a client can trigger with
            // something as ordinary as `?q=%a\u{e9}`. Indexing bytes cannot
            // cross a boundary, and any byte that is not an ASCII hex digit
            // fails `to_digit` and falls through to the malformed path.
            b'%' if i + 2 < bytes.len() => {
                let high = (bytes[i + 1] as char).to_digit(16);
                let low = (bytes[i + 2] as char).to_digit(16);
                match (high, low) {
                    (Some(high), Some(low)) => {
                        out.push((high * 16 + low) as u8);
                        i += 3;
                    }
                    _ => {
                        out.push(bytes[i]);
                        i += 1;
                    }
                }
            }
            byte => {
                out.push(byte);
                i += 1;
            }
        }
    }
    String::from_utf8(out).unwrap_or_else(|_| value.to_string())
}

/// Pull the declared variables out of a URI's query string.
///
/// Routing policy, which RFC 6570 does not specify for matching:
///
/// - every declared variable is optional, so a URI with none still matches
/// - order does not matter
/// - a variable present with no value maps to an empty string, which is
///   distinct from being absent
/// - keys that were not declared are ignored rather than rejected, so an
///   added tracking parameter does not break routing
/// - the first occurrence of a repeated key wins
fn extract_query_variables(query: &str, declared: &[String]) -> HashMap<String, String> {
    let mut found = HashMap::new();
    for pair in query.split('&').filter(|p| !p.is_empty()) {
        let (raw_key, raw_value) = match pair.split_once('=') {
            Some((key, value)) => (key, value),
            None => (pair, ""),
        };
        let key = decode_query_component(raw_key);
        if !declared.contains(&key) {
            continue;
        }
        found
            .entry(key)
            .or_insert_with(|| decode_query_component(raw_value));
    }
    found
}

#[cfg(test)]
mod query_expansion_tests;

#[cfg(test)]
mod tests;