turul-http-mcp-server 0.3.40

HTTP transport layer for Model Context Protocol (MCP) servers
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
//! Streamable HTTP Transport for MCP 2025-11-25
//!
//! This module implements the "Streamable HTTP" transport mechanism introduced
//! in MCP 2025-03-26, which replaces the previous HTTP+SSE approach from 2024-11-05.
//!
//! ## Key Improvements over HTTP+SSE
//! - **Serverless Compatibility**: Enables deployment on AWS Lambda, Google Cloud Run
//! - **Improved Scalability**: Supports chunked transfer encoding and progressive delivery
//! - **Session Management**: Cryptographically secure session IDs for connection tracking
//! - **Enterprise Network Friendly**: No long-lived connections or polling requirements

use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;

use bytes::Bytes;
use futures::Stream;
use http_body::Body;
use http_body_util::{BodyExt, Full};
use hyper::header::{ACCEPT, CONTENT_TYPE};
use hyper::{HeaderMap, Method, Request, Response, StatusCode};
use serde_json::Value;
use tracing::{debug, error, info, warn};
use turul_mcp_session_storage::SessionView;

use crate::ServerConfig;
use crate::protocol::normalize_header_value;

/// MCP Protocol versions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum McpProtocolVersion {
    /// Original protocol without streamable HTTP (2024-11-05)
    V2024_11_05,
    /// Protocol including streamable HTTP (2025-03-26)
    V2025_03_26,
    /// Protocol with structured _meta, cursor, progressToken, and elicitation (2025-06-18)
    V2025_06_18,
    /// Protocol with tasks, icons, URL elicitation, and sampling tools (2025-11-25)
    #[default]
    V2025_11_25,
}

impl McpProtocolVersion {
    /// Parse from header string
    pub fn parse_version(s: &str) -> Option<Self> {
        match s {
            "2024-11-05" => Some(Self::V2024_11_05),
            "2025-03-26" => Some(Self::V2025_03_26),
            "2025-06-18" => Some(Self::V2025_06_18),
            "2025-11-25" => Some(Self::V2025_11_25),
            _ => None,
        }
    }

    /// Convert to string representation
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::V2024_11_05 => "2024-11-05",
            Self::V2025_03_26 => "2025-03-26",
            Self::V2025_06_18 => "2025-06-18",
            Self::V2025_11_25 => "2025-11-25",
        }
    }

    /// Returns whether this version supports streamable HTTP
    pub fn supports_streamable_http(&self) -> bool {
        matches!(
            self,
            Self::V2025_03_26 | Self::V2025_06_18 | Self::V2025_11_25
        )
    }

    /// Returns whether this version supports _meta fields
    pub fn supports_meta_fields(&self) -> bool {
        matches!(self, Self::V2025_06_18 | Self::V2025_11_25)
    }

    /// Returns whether this version supports cursor-based pagination
    pub fn supports_cursors(&self) -> bool {
        matches!(self, Self::V2025_06_18 | Self::V2025_11_25)
    }

    /// Returns whether this version supports progress tokens
    pub fn supports_progress_tokens(&self) -> bool {
        matches!(self, Self::V2025_06_18 | Self::V2025_11_25)
    }

    /// Returns whether this version supports elicitation
    pub fn supports_elicitation(&self) -> bool {
        matches!(self, Self::V2025_06_18 | Self::V2025_11_25)
    }

    /// Returns whether this version supports the task system (experimental)
    pub fn supports_tasks(&self) -> bool {
        matches!(self, Self::V2025_11_25)
    }

    /// Returns whether this version supports icons
    pub fn supports_icons(&self) -> bool {
        matches!(self, Self::V2025_11_25)
    }

    /// Get list of supported features for this version
    pub fn supported_features(&self) -> Vec<&'static str> {
        let mut features = vec![];
        if self.supports_streamable_http() {
            features.push("streamable-http");
        }
        if self.supports_meta_fields() {
            features.push("_meta-fields");
        }
        if self.supports_cursors() {
            features.push("cursor-pagination");
        }
        if self.supports_progress_tokens() {
            features.push("progress-tokens");
        }
        if self.supports_elicitation() {
            features.push("elicitation");
        }
        if self.supports_tasks() {
            features.push("tasks");
        }
        if self.supports_icons() {
            features.push("icons");
        }
        features
    }
}

/// Streamable HTTP request context
#[derive(Debug, Clone)]
pub struct StreamableHttpContext {
    /// Protocol version negotiated
    pub protocol_version: McpProtocolVersion,
    /// Session ID if provided
    pub session_id: Option<String>,
    /// Whether client wants SSE stream (text/event-stream)
    pub wants_sse_stream: bool,
    /// Whether client also accepts JSON (application/json or */*)
    pub accepts_json: bool,
    /// Whether client accepts stream frames (application/json, text/event-stream, or */*)
    pub accepts_stream_frames: bool,
    /// Additional request headers
    pub headers: HashMap<String, String>,
}

impl StreamableHttpContext {
    /// Parse context from HTTP request headers
    pub fn from_request<T>(req: &Request<T>) -> Self {
        let headers = req.headers();

        // Parse protocol version from MCP-Protocol-Version header
        let protocol_version = headers
            .get("MCP-Protocol-Version")
            .and_then(|h| h.to_str().ok())
            .and_then(McpProtocolVersion::parse_version)
            .unwrap_or_default();

        // Extract session ID from Mcp-Session-Id header (note capitalization)
        let session_id = headers
            .get("Mcp-Session-Id")
            .and_then(|h| h.to_str().ok())
            .map(|s| s.to_string());

        // Check Accept header for streaming and JSON support
        let accept_header = headers
            .get(ACCEPT)
            .and_then(|h| h.to_str().ok())
            .map(normalize_header_value)
            .unwrap_or_default();

        let wants_sse_stream = accept_header.contains("text/event-stream");
        let accepts_json =
            accept_header.contains("application/json") || accept_header.contains("*/*");
        let accepts_stream_frames = accepts_json || accept_header.contains("text/event-stream");

        // Collect additional headers for debugging/logging
        let mut header_map = HashMap::new();
        for (name, value) in headers.iter() {
            if let Ok(value_str) = value.to_str() {
                header_map.insert(name.to_string(), value_str.to_string());
            }
        }

        Self {
            protocol_version,
            session_id,
            wants_sse_stream,
            accepts_json,
            accepts_stream_frames,
            headers: header_map,
        }
    }

    /// Whether client wants SSE stream
    pub fn wants_sse_stream(&self) -> bool {
        self.wants_sse_stream
    }

    /// Conservative transport heuristic for SSE vs JSON response framing.
    ///
    /// **Not a spec requirement** — the MCP spec allows either format when
    /// the client accepts both. This is a compatibility-driven default:
    ///
    /// - Client only accepts `text/event-stream` → SSE
    /// - Client only accepts `application/json` → JSON
    /// - Client accepts both → SSE for `tools/call`, `sampling/createMessage`,
    ///   `elicitation/create`; JSON for everything else
    ///
    /// **Limitation (architectural, not fundamental):** the heuristic operates
    /// at method granularity, not per-tool. Every `tools/call` under combined
    /// Accept gets SSE — even simple tools that never call `notify_progress()`.
    /// The transport layer does not currently have per-tool progress metadata;
    /// plumbing that information from the tool registry would allow a finer
    /// decision but is not implemented. Non-streaming `tools/call` responses
    /// pay the SSE framing cost and may hit client/proxy SSE quirks as a
    /// result of this tradeoff.
    pub fn should_use_sse(&self, method: &str) -> bool {
        if !self.wants_sse_stream {
            return false;
        }
        if !self.accepts_json {
            return true;
        }
        // Conservative: assume any tools/call may emit mid-stream events
        matches!(
            method,
            "tools/call" | "sampling/createMessage" | "elicitation/create"
        )
    }

    /// Whether client wants streaming POST responses
    pub fn wants_streaming_post(&self) -> bool {
        self.accepts_stream_frames && self.wants_sse_stream
    }

    /// Check if request is compatible with streamable HTTP
    pub fn is_streamable_compatible(&self) -> bool {
        self.protocol_version.supports_streamable_http() && self.accepts_stream_frames
    }

    /// Validate request for MCP compliance
    pub fn validate(&self, method: &Method) -> std::result::Result<(), String> {
        if !self.accepts_stream_frames {
            return Err(
                "Accept header must include application/json, text/event-stream, or */*"
                    .to_string(),
            );
        }

        if self.wants_sse_stream && !self.protocol_version.supports_streamable_http() {
            return Err(format!(
                "Protocol version {} does not support streamable HTTP",
                self.protocol_version.as_str()
            ));
        }

        // Only enforce session_id for GET requests with SSE streams
        // POST requests will validate session based on the JSON-RPC method (initialize vs others)
        if *method == Method::GET && self.wants_sse_stream && self.session_id.is_none() {
            return Err("Mcp-Session-Id header required for SSE streaming connections".to_string());
        }

        Ok(())
    }

    /// Create response headers for this context
    pub fn response_headers(&self) -> HeaderMap {
        let mut headers = HeaderMap::new();

        // Always include protocol version in response
        headers.insert(
            "MCP-Protocol-Version",
            self.protocol_version.as_str().parse().unwrap(),
        );

        // Include session ID if present
        if let Some(session_id) = &self.session_id {
            headers.insert("Mcp-Session-Id", session_id.parse().unwrap());
        }

        // Add capabilities header showing supported features
        let features = self.protocol_version.supported_features();
        if !features.is_empty() {
            headers.insert("MCP-Capabilities", features.join(",").parse().unwrap());
        }

        headers
    }
}

/// Why session validation failed — determines the HTTP status code.
///
/// MCP 2025-11-25 spec: terminated or unknown sessions MUST return 404 Not Found.
/// Missing `Mcp-Session-Id` header (a different code path) stays 401 Unauthorized.
enum SessionValidationError {
    /// Session ID not found or session has been terminated → 404 Not Found
    NotFound(String),
    /// Storage backend error → 500 Internal Server Error
    StorageError(String),
    /// Mandatory notification persistence failed → 500 Internal Server Error
    NotificationFailed(String),
}

impl SessionValidationError {
    fn status_code(&self) -> StatusCode {
        match self {
            Self::NotFound(_) => StatusCode::NOT_FOUND,
            Self::StorageError(_) => StatusCode::INTERNAL_SERVER_ERROR,
            Self::NotificationFailed(_) => StatusCode::INTERNAL_SERVER_ERROR,
        }
    }

    fn message(&self) -> &str {
        match self {
            Self::NotFound(msg) | Self::StorageError(msg) | Self::NotificationFailed(msg) => msg,
        }
    }
}

/// Streamable HTTP response types
pub enum StreamableResponse {
    /// Single JSON response
    Json(Value),
    /// Streaming response with multiple JSON messages
    Stream(Pin<Box<dyn Stream<Item = std::result::Result<Value, String>> + Send>>),
    /// Error response
    Error { status: StatusCode, message: String },
}

impl std::fmt::Debug for StreamableResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Json(value) => f.debug_tuple("Json").field(value).finish(),
            Self::Stream(_) => f.debug_tuple("Stream").field(&"<stream>").finish(),
            Self::Error { status, message } => f
                .debug_struct("Error")
                .field("status", status)
                .field("message", message)
                .finish(),
        }
    }
}

impl StreamableResponse {
    /// Convert to HTTP response
    pub fn into_response(self, context: &StreamableHttpContext) -> Response<Full<Bytes>> {
        let mut response_headers = context.response_headers();

        match self {
            StreamableResponse::Json(json) => {
                response_headers.insert(CONTENT_TYPE, "application/json".parse().unwrap());

                let body = serde_json::to_string(&json)
                    .unwrap_or_else(|_| r#"{"error": "Failed to serialize response"}"#.to_string());

                Response::builder()
                    .status(StatusCode::OK)
                    .body(Full::new(Bytes::from(body)))
                    .unwrap()
            }

            StreamableResponse::Stream(_stream) => {
                // For streaming responses, set appropriate headers
                response_headers.insert(CONTENT_TYPE, "text/event-stream".parse().unwrap());
                response_headers.insert("Cache-Control", "no-cache, no-transform".parse().unwrap());
                response_headers.insert("Connection", "keep-alive".parse().unwrap());

                // TODO: Implement actual streaming body with chunked transfer encoding
                // Should stream JSON messages over HTTP with proper Content-Type: text/event-stream
                // For now, return 202 Accepted to indicate streaming would happen
                Response::builder()
                    .status(StatusCode::ACCEPTED)
                    .body(Full::new(Bytes::from("Streaming response accepted")))
                    .unwrap()
            }

            StreamableResponse::Error { status, message } => {
                response_headers.insert(CONTENT_TYPE, "application/json".parse().unwrap());

                let error_json = serde_json::json!({
                    "error": {
                        "code": status.as_u16(),
                        "message": message
                    }
                });

                let body = serde_json::to_string(&error_json).unwrap_or_else(|_| {
                    r#"{"error": {"code": 500, "message": "Internal server error"}}"#.to_string()
                });

                Response::builder()
                    .status(status)
                    .body(Full::new(Bytes::from(body)))
                    .unwrap()
            }
        }
    }

    /// Convert to HTTP response with UnsyncBoxBody for streaming compatibility
    pub fn into_boxed_response(
        self,
        context: &StreamableHttpContext,
    ) -> Response<http_body_util::combinators::UnsyncBoxBody<Bytes, hyper::Error>> {
        self.into_response(context)
            .map(|body| body.map_err(|never| match never {}).boxed_unsync())
    }
}

/// Streamable HTTP transport handler
#[derive(Clone)]
pub struct StreamableHttpHandler {
    config: Arc<ServerConfig>,
    dispatcher: Arc<turul_mcp_json_rpc_server::JsonRpcDispatcher<turul_mcp_protocol::McpError>>,
    session_storage: Arc<turul_mcp_session_storage::BoxedSessionStorage>,
    stream_manager: Arc<crate::StreamManager>,
    server_capabilities: turul_mcp_protocol::ServerCapabilities,
    pub(crate) middleware_stack: Arc<crate::middleware::MiddlewareStack>,
    tool_fingerprint: Option<String>,
    tool_notifier: Option<Arc<dyn crate::ToolChangeNotifier>>,
}

impl StreamableHttpHandler {
    pub fn new(
        config: Arc<ServerConfig>,
        dispatcher: Arc<turul_mcp_json_rpc_server::JsonRpcDispatcher<turul_mcp_protocol::McpError>>,
        session_storage: Arc<turul_mcp_session_storage::BoxedSessionStorage>,
        stream_manager: Arc<crate::StreamManager>,
        server_capabilities: turul_mcp_protocol::ServerCapabilities,
        middleware_stack: Arc<crate::middleware::MiddlewareStack>,
        tool_fingerprint: Option<String>,
    ) -> Self {
        Self {
            config,
            dispatcher,
            session_storage,
            stream_manager,
            server_capabilities,
            middleware_stack,
            tool_fingerprint,
            tool_notifier: None,
        }
    }

    /// Set the tool change notifier for restart/redeploy fingerprint mismatch notifications.
    pub fn with_tool_notifier(mut self, notifier: Arc<dyn crate::ToolChangeNotifier>) -> Self {
        self.tool_notifier = Some(notifier);
        self
    }

    /// Handle incoming HTTP request with streamable HTTP support
    pub async fn handle_request<T>(
        &self,
        req: Request<T>,
    ) -> Response<http_body_util::combinators::UnsyncBoxBody<Bytes, hyper::Error>>
    where
        T: Body + Send + 'static,
        T::Data: Send,
        T::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
    {
        debug!(
            "Streamable handler request: method={}, uri={}",
            req.method(),
            req.uri()
        );
        // Parse streamable HTTP context from request
        let context = StreamableHttpContext::from_request(&req);

        debug!(
            "Streamable handler entry: method={}, protocol={}, session={:?}, accepts_stream_frames={}, wants_sse_stream={}",
            req.method(),
            context.protocol_version.as_str(),
            context.session_id,
            context.accepts_stream_frames,
            context.wants_sse_stream()
        );

        // Handle OPTIONS preflight before validation (no Accept header required).
        // CORS headers are added by CorsLayer in server.rs — not here.
        if *req.method() == Method::OPTIONS {
            return Response::builder()
                .status(StatusCode::OK)
                .body(Full::new(Bytes::new()))
                .unwrap()
                .map(|body| body.map_err(|never| match never {}).boxed_unsync());
        }

        // Validate request
        if let Err(error) = context.validate(req.method()) {
            warn!("Invalid streamable HTTP request: {}", error);
            return StreamableResponse::Error {
                status: StatusCode::BAD_REQUEST,
                message: error,
            }
            .into_boxed_response(&context);
        }

        // Route based on MCP 2025-11-25 specification
        match *req.method() {
            Method::POST => {
                // ALL client messages (requests, notifications, responses) come via POST
                // Server decides whether to respond with JSON or SSE stream
                self.handle_client_message(req, context).await
            }
            Method::GET => {
                // Optional SSE stream for server-initiated messages
                self.handle_get_sse_notifications(req, context).await
            }
            Method::DELETE => {
                // Optional session cleanup
                self.handle_session_delete(req, context).await
            }
            _ => StreamableResponse::Error {
                status: StatusCode::METHOD_NOT_ALLOWED,
                message: "Method not allowed for this endpoint".to_string(),
            }
            .into_boxed_response(&context),
        }
    }

    /// Handle GET request for long-lived server-initiated notifications (GET SSE)
    ///
    /// This is traditional Server-Sent Events - a long-lived GET connection for
    /// server-initiated notifications unrelated to specific client requests.
    /// NOT used for tool progress (that's POST Streamable HTTP).
    async fn handle_get_sse_notifications<T>(
        &self,
        req: Request<T>,
        context: StreamableHttpContext,
    ) -> Response<http_body_util::combinators::UnsyncBoxBody<Bytes, hyper::Error>>
    where
        T: Body + Send + 'static,
    {
        debug!(
            "Opening streaming connection for session: {:?}",
            context.session_id
        );

        // 1. Validate session exists and is authorized
        let session_id = match context.session_id {
            Some(ref id) => id.clone(),
            None => {
                warn!("Missing session ID for streaming GET request");
                return StreamableResponse::Error {
                    status: StatusCode::BAD_REQUEST,
                    message: "Mcp-Session-Id header required for streaming connection".to_string(),
                }
                .into_boxed_response(&context);
            }
        };

        // Validate session exists (do NOT create if missing)
        match self.validate_session_exists(&session_id).await {
            Ok(_) => {
                debug!(
                    "Session validation successful for streaming GET: {}",
                    session_id
                );
            }
            Err(err) => {
                error!(
                    "Session validation failed for streaming GET {}: {}",
                    session_id,
                    err.message()
                );
                return StreamableResponse::Error {
                    status: err.status_code(),
                    message: format!("Session validation failed: {}", err.message()),
                }
                .into_boxed_response(&context);
            }
        }

        // 2. Create bi-directional stream with chunked transfer encoding
        // For MCP Streamable HTTP, we create a stream that can handle bidirectional JSON-RPC
        // Unlike SSE which is unidirectional server->client, this supports client->server and server->client

        // Extract Last-Event-ID for resumability (if client supports it)
        let last_event_id = req
            .headers()
            .get("Last-Event-ID")
            .and_then(|h| h.to_str().ok())
            .and_then(|s| s.parse::<u64>().ok());

        // Generate unique connection ID for tracking this stream
        let connection_id = uuid::Uuid::now_v7().as_simple().to_string();

        debug!(
            "Creating streamable HTTP connection: session={}, connection={}, last_event_id={:?}",
            session_id, connection_id, last_event_id
        );

        // 3. Return streaming response supporting progressive message delivery
        // ✅ FIXED: Return the actual streaming response from StreamManager
        // This preserves event replay, resumability, and live streaming capabilities
        match self
            .stream_manager
            .handle_sse_connection(session_id.clone(), connection_id.clone(), last_event_id)
            .await
        {
            Ok(mut streaming_response) => {
                debug!(
                    "Streamable HTTP connection established: session={}, connection={}",
                    session_id, connection_id
                );

                // Merge MCP headers from context.response_headers()
                let mcp_headers = context.response_headers();
                for (key, value) in mcp_headers.iter() {
                    streaming_response.headers_mut().insert(key, value.clone());
                }

                // ✅ PRESERVE STREAMING: Return the streaming response with MCP headers
                // This maintains event replay from session storage and live streaming
                streaming_response
            }
            Err(err) => {
                error!("Failed to create streamable HTTP connection: {}", err);
                StreamableResponse::Error {
                    status: StatusCode::INTERNAL_SERVER_ERROR,
                    message: format!("Streaming connection failed: {}", err),
                }
                .into_boxed_response(&context)
            }
        }
    }

    /// Validate that a session exists and is not terminated — do NOT create if missing.
    ///
    /// Returns typed errors so callers can map to the correct HTTP status:
    /// - `NotFound` → 404 (MCP spec: terminated sessions MUST return 404)
    /// - `StorageError` → 500
    async fn validate_session_exists(
        &self,
        session_id: &str,
    ) -> std::result::Result<(), SessionValidationError> {
        match self.session_storage.get_session(session_id).await {
            Ok(Some(session_info)) => {
                if session_info.is_terminated() {
                    error!("Session '{}' has been terminated", session_id);
                    return Err(SessionValidationError::NotFound(format!(
                        "Session '{}' has been terminated. Create a new session to continue.",
                        session_id
                    )));
                }
                // Check tool fingerprint — mismatch means tools changed
                if let Some(ref current_fp) = self.tool_fingerprint {
                    if let Some(stored_fp) = session_info.state.get("mcp:tool_fingerprint") {
                        if stored_fp.as_str() != Some(current_fp.as_str()) {
                            // Fingerprint mismatch — tools changed since this session was created.
                            // Session is still valid. Update the stored fingerprint and continue.
                            info!(
                                "Tool fingerprint updated for session '{}' (tools changed since session created)",
                                session_id
                            );
                            let _ = self
                                .session_storage
                                .set_session_state(
                                    session_id,
                                    "mcp:tool_fingerprint",
                                    serde_json::json!(current_fp),
                                )
                                .await;

                            // Emit notifications/tools/list_changed via the notifier
                            // (backed by SessionManager → dispatcher → guaranteed persistence)
                            if let Some(ref notifier) = self.tool_notifier {
                                notifier
                                    .notify_tools_changed(session_id)
                                    .await
                                    .map_err(|e| {
                                        SessionValidationError::NotificationFailed(format!(
                                            "session {}: {}",
                                            session_id, e
                                        ))
                                    })?;
                            }
                        }
                    } else {
                        // Missing fingerprint (pre-feature sessions) — store current fingerprint
                        info!(
                            "Session '{}' has no tool fingerprint, storing current",
                            session_id
                        );
                        let _ = self
                            .session_storage
                            .set_session_state(
                                session_id,
                                "mcp:tool_fingerprint",
                                serde_json::json!(current_fp),
                            )
                            .await;
                    }
                }
                debug!("Session validation successful: {}", session_id);
                Ok(())
            }
            Ok(None) => {
                error!("Session not found: {}", session_id);
                Err(SessionValidationError::NotFound(format!(
                    "Session '{}' not found. Sessions must be created via initialize request first.",
                    session_id
                )))
            }
            Err(err) => {
                error!("Failed to validate session {}: {}", session_id, err);
                Err(SessionValidationError::StorageError(format!(
                    "Session validation failed: {}",
                    err
                )))
            }
        }
    }

    /// Handle POST request with JSON response (legacy compatibility)
    #[allow(dead_code)]
    async fn handle_json_post<T>(
        &self,
        req: Request<T>,
        context: StreamableHttpContext,
    ) -> Response<http_body_util::combinators::UnsyncBoxBody<Bytes, hyper::Error>>
    where
        T: Body + Send + 'static,
    {
        debug!("Handling JSON POST (non-streaming/legacy)");

        // 1. Parse JSON-RPC request(s) from request body (legacy clients don't require sessions)

        // Check content type
        let content_type = req
            .headers()
            .get(CONTENT_TYPE)
            .and_then(|ct| ct.to_str().ok())
            .map(normalize_header_value)
            .unwrap_or_default();

        if !content_type.starts_with("application/json") {
            warn!("Invalid content type for legacy POST: {}", content_type);
            return StreamableResponse::Error {
                status: StatusCode::BAD_REQUEST,
                message: "Content-Type must be application/json".to_string(),
            }
            .into_boxed_response(&context);
        }

        // Read request body
        let body_bytes = match req.into_body().collect().await {
            Ok(collected) => collected.to_bytes(),
            Err(_err) => {
                error!("Failed to read legacy POST request body");
                return StreamableResponse::Error {
                    status: StatusCode::BAD_REQUEST,
                    message: "Failed to read request body".to_string(),
                }
                .into_boxed_response(&context);
            }
        };

        // Check body size
        if body_bytes.len() > self.config.max_body_size {
            warn!(
                "Legacy POST request body too large: {} bytes",
                body_bytes.len()
            );
            return StreamableResponse::Error {
                status: StatusCode::PAYLOAD_TOO_LARGE,
                message: "Request body too large".to_string(),
            }
            .into_boxed_response(&context);
        }

        // Parse as UTF-8
        let body_str = match std::str::from_utf8(&body_bytes) {
            Ok(s) => s,
            Err(err) => {
                error!("Invalid UTF-8 in legacy POST request body: {}", err);
                return StreamableResponse::Error {
                    status: StatusCode::BAD_REQUEST,
                    message: "Request body must be valid UTF-8".to_string(),
                }
                .into_boxed_response(&context);
            }
        };

        debug!("Received legacy POST JSON-RPC request: {}", body_str);

        // Parse JSON-RPC message
        use turul_mcp_json_rpc_server::dispatch::{
            JsonRpcMessage, JsonRpcMessageResult, parse_json_rpc_message,
        };

        let message = match parse_json_rpc_message(body_str) {
            Ok(msg) => msg,
            Err(rpc_err) => {
                error!("JSON-RPC parse error in legacy POST: {}", rpc_err);
                let error_json =
                    serde_json::to_string(&rpc_err).unwrap_or_else(|_| "{}".to_string());
                return Response::builder()
                    .status(StatusCode::OK) // JSON-RPC parse errors still use 200 OK
                    .header(CONTENT_TYPE, "application/json")
                    .header("MCP-Protocol-Version", context.protocol_version.as_str())
                    .body(Full::new(Bytes::from(error_json)))
                    .unwrap()
                    .map(|body| body.map_err(|never| match never {}).boxed_unsync());
            }
        };

        // 2. Process via dispatcher (no session context for legacy clients)
        // Legacy clients (MCP 2024-11-05) don't use sessions, so no session context
        let message_result = match message {
            JsonRpcMessage::Request(request) => {
                debug!(
                    "Processing legacy POST JSON-RPC request: method={}",
                    request.method
                );

                // Special handling for initialize requests - legacy clients can create sessions too
                let response = if request.method == "initialize" {
                    debug!("Handling legacy initialize request - creating new session");

                    // Let session storage create the session and generate the ID
                    match self
                        .session_storage
                        .create_session(self.server_capabilities.clone())
                        .await
                    {
                        Ok(session_info) => {
                            debug!(
                                "Created new session for legacy client: {}",
                                session_info.session_id
                            );

                            // Create session context for initialize response
                            use crate::notification_bridge::StreamManagerNotificationBroadcaster;
                            use turul_mcp_json_rpc_server::r#async::SessionContext;

                            let broadcaster = Arc::new(StreamManagerNotificationBroadcaster::new(
                                Arc::clone(&self.stream_manager),
                            ));
                            let broadcaster_any =
                                Arc::new(broadcaster) as Arc<dyn std::any::Any + Send + Sync>;

                            let session_context = SessionContext {
                                session_id: session_info.session_id.clone(),
                                metadata: std::collections::HashMap::new(),
                                broadcaster: Some(broadcaster_any),
                                timestamp: chrono::Utc::now().timestamp_millis() as u64,
                                extensions: std::collections::HashMap::new(),
                            };

                            self.dispatcher
                                .handle_request_with_context(request, session_context)
                                .await
                        }
                        Err(err) => {
                            error!("Failed to create session during legacy initialize: {}", err);
                            let error_msg = format!("Session creation failed: {}", err);
                            turul_mcp_json_rpc_server::JsonRpcMessage::error(
                                turul_mcp_json_rpc_server::JsonRpcError::internal_error(
                                    Some(request.id),
                                    Some(error_msg),
                                ),
                            )
                        }
                    }
                } else {
                    // For non-initialize requests, process without session context (legacy mode)
                    self.dispatcher.handle_request(request).await
                };

                // Convert JsonRpcMessage to JsonRpcMessageResult
                match response {
                    turul_mcp_json_rpc_server::JsonRpcMessage::Response(resp) => {
                        JsonRpcMessageResult::Response(resp)
                    }
                    turul_mcp_json_rpc_server::JsonRpcMessage::Error(err) => {
                        JsonRpcMessageResult::Error(err)
                    }
                }
            }
            JsonRpcMessage::Notification(notification) => {
                debug!(
                    "Processing legacy POST JSON-RPC notification: method={}",
                    notification.method
                );

                // Process notification without session context (legacy mode)
                let result = self
                    .dispatcher
                    .handle_notification_with_context(notification, None)
                    .await;

                if let Err(err) = result {
                    error!("Legacy POST notification handling error: {}", err);
                }
                JsonRpcMessageResult::NoResponse
            }
        };

        // 3. Return single JSON response (no streaming) - legacy compatibility
        match message_result {
            JsonRpcMessageResult::Response(response) => {
                let response_json = serde_json::to_string(&response)
                    .unwrap_or_else(|_| r#"{"error": "Failed to serialize response"}"#.to_string());

                Response::builder()
                    .status(StatusCode::OK)
                    .header(CONTENT_TYPE, "application/json")
                    .header("MCP-Protocol-Version", context.protocol_version.as_str())
                    .body(Full::new(Bytes::from(response_json)))
                    .unwrap()
                    .map(|body| body.map_err(|never| match never {}).boxed_unsync())
            }
            JsonRpcMessageResult::Error(error) => {
                let error_json = serde_json::to_string(&error)
                    .unwrap_or_else(|_| r#"{"error": "Internal error"}"#.to_string());

                Response::builder()
                    .status(StatusCode::OK) // JSON-RPC errors still return 200 OK
                    .header(CONTENT_TYPE, "application/json")
                    .header("MCP-Protocol-Version", context.protocol_version.as_str())
                    .body(Full::new(Bytes::from(error_json)))
                    .unwrap()
                    .map(|body| body.map_err(|never| match never {}).boxed_unsync())
            }
            JsonRpcMessageResult::NoResponse => {
                // Notifications return 202 Accepted per MCP spec
                Response::builder()
                    .status(StatusCode::ACCEPTED)
                    .header("MCP-Protocol-Version", context.protocol_version.as_str())
                    .body(Full::new(Bytes::new()))
                    .unwrap()
                    .map(|body| body.map_err(|never| match never {}).boxed_unsync())
            }
        }
    }

    /// Handle DELETE request for session cleanup
    async fn handle_session_delete<T>(
        &self,
        _req: Request<T>,
        context: StreamableHttpContext,
    ) -> Response<http_body_util::combinators::UnsyncBoxBody<Bytes, hyper::Error>>
    where
        T: Body + Send + 'static,
    {
        if let Some(session_id) = &context.session_id {
            debug!("Deleting session: {}", session_id);

            // Implement proper session cleanup for Streamable HTTP
            // 1. Close any active streaming connections for this session
            let closed_connections = self
                .stream_manager
                .close_session_connections(session_id)
                .await;
            debug!(
                "Closed {} streaming connections for session: {}",
                closed_connections, session_id
            );

            // 2. Mark session as terminated instead of immediate deletion (for proper lifecycle management)
            match self.session_storage.get_session(session_id).await {
                Ok(Some(mut session_info)) => {
                    // Mark session as terminated in state
                    session_info
                        .state
                        .insert("terminated".to_string(), serde_json::Value::Bool(true));
                    session_info.state.insert(
                        "terminated_at".to_string(),
                        serde_json::Value::Number(serde_json::Number::from(
                            chrono::Utc::now().timestamp_millis(),
                        )),
                    );
                    session_info.touch();

                    // 3. Update session with termination markers
                    match self.session_storage.update_session(session_info).await {
                        Ok(()) => {
                            debug!(
                                "Session {} marked as terminated (TTL will handle cleanup)",
                                session_id
                            );

                            // Return success response with proper headers
                            Response::builder()
                                .status(StatusCode::OK)
                                .header(CONTENT_TYPE, "application/json")
                                .header("MCP-Protocol-Version", context.protocol_version.as_str())
                                .header("Mcp-Session-Id", session_id)
                                .body(Full::new(Bytes::from(
                                    serde_json::to_string(&serde_json::json!({
                                        "status": "session_terminated",
                                        "session_id": session_id,
                                        "closed_connections": closed_connections,
                                        "message": "Session marked for cleanup"
                                    }))
                                    .unwrap_or_else(|_| {
                                        r#"{"status":"session_terminated"}"#.to_string()
                                    }),
                                )))
                                .unwrap()
                                .map(|body| body.map_err(|never| match never {}).boxed_unsync())
                        }
                        Err(err) => {
                            error!(
                                "Error marking session {} as terminated: {}",
                                session_id, err
                            );
                            // Fallback to deletion if update fails
                            match self.session_storage.delete_session(session_id).await {
                                Ok(_) => {
                                    debug!("Session {} deleted as fallback", session_id);
                                    Response::builder()
                                        .status(StatusCode::OK)
                                        .header(CONTENT_TYPE, "application/json")
                                        .header(
                                            "MCP-Protocol-Version",
                                            context.protocol_version.as_str(),
                                        )
                                        .body(Full::new(Bytes::from(
                                            serde_json::to_string(&serde_json::json!({
                                                "status": "session_deleted",
                                                "session_id": session_id,
                                                "closed_connections": closed_connections,
                                                "message": "Session removed"
                                            }))
                                            .unwrap_or_else(|_| {
                                                r#"{"status":"session_deleted"}"#.to_string()
                                            }),
                                        )))
                                        .unwrap()
                                        .map(|body| {
                                            body.map_err(|never| match never {}).boxed_unsync()
                                        })
                                }
                                Err(delete_err) => {
                                    error!(
                                        "Error deleting session {} as fallback: {}",
                                        session_id, delete_err
                                    );
                                    StreamableResponse::Error {
                                        status: StatusCode::INTERNAL_SERVER_ERROR,
                                        message: "Session termination error".to_string(),
                                    }
                                    .into_boxed_response(&context)
                                }
                            }
                        }
                    }
                }
                Ok(None) => {
                    // Session not found
                    Response::builder()
                        .status(StatusCode::NOT_FOUND)
                        .header(CONTENT_TYPE, "application/json")
                        .header("MCP-Protocol-Version", context.protocol_version.as_str())
                        .body(Full::new(Bytes::from(
                            serde_json::to_string(&serde_json::json!({
                                "status": "session_not_found",
                                "session_id": session_id,
                                "message": "Session not found"
                            }))
                            .unwrap_or_else(|_| r#"{"status":"session_not_found"}"#.to_string()),
                        )))
                        .unwrap()
                        .map(|body| body.map_err(|never| match never {}).boxed_unsync())
                }
                Err(err) => {
                    error!(
                        "Error retrieving session {} for termination: {}",
                        session_id, err
                    );
                    StreamableResponse::Error {
                        status: StatusCode::INTERNAL_SERVER_ERROR,
                        message: "Session lookup error".to_string(),
                    }
                    .into_boxed_response(&context)
                }
            }
        } else {
            StreamableResponse::Error {
                status: StatusCode::BAD_REQUEST,
                message: "Mcp-Session-Id header required for session deletion".to_string(),
            }
            .into_boxed_response(&context)
        }
    }

    /// Handle POST with tool progress notifications (POST Streamable HTTP)
    ///
    /// Implements MCP 2025-11-25 Streamable HTTP where POST requests receive
    /// chunked responses containing both progress notifications AND final result.
    /// This is request-response, NOT a long-lived connection (that's GET SSE).
    ///
    /// Uses hyper::Body::channel() for chunked transfer encoding with background
    /// task forwarding (works for long-running servers, BROKEN in Lambda).
    async fn handle_post_streamable_http<T>(
        &self,
        req: Request<T>,
        mut context: StreamableHttpContext,
    ) -> Response<http_body_util::combinators::UnsyncBoxBody<Bytes, hyper::Error>>
    where
        T: Body + Send + 'static,
    {
        debug!("Streaming handler called - using true streaming POST");

        // Parse request body (still need to collect for JSON-RPC parsing)
        let body_bytes = match req.into_body().collect().await {
            Ok(collected) => collected.to_bytes(),
            Err(_err) => {
                error!("Failed to read streaming POST request body");
                return StreamableResponse::Error {
                    status: StatusCode::BAD_REQUEST,
                    message: "Failed to read request body".to_string(),
                }
                .into_boxed_response(&context);
            }
        };

        // Check body size
        if body_bytes.len() > self.config.max_body_size {
            warn!(
                "Streaming POST request body too large: {} bytes",
                body_bytes.len()
            );
            return StreamableResponse::Error {
                status: StatusCode::PAYLOAD_TOO_LARGE,
                message: "Request body too large".to_string(),
            }
            .into_boxed_response(&context);
        }

        // Parse as UTF-8
        let body_str = match std::str::from_utf8(&body_bytes) {
            Ok(s) => s,
            Err(err) => {
                error!("Invalid UTF-8 in streaming POST request body: {}", err);
                return StreamableResponse::Error {
                    status: StatusCode::BAD_REQUEST,
                    message: "Request body must be valid UTF-8".to_string(),
                }
                .into_boxed_response(&context);
            }
        };

        debug!("Streaming POST received JSON-RPC request: {}", body_str);

        // Parse JSON-RPC message
        use turul_mcp_json_rpc_server::dispatch::{JsonRpcMessage, parse_json_rpc_message};
        use turul_mcp_json_rpc_server::error::JsonRpcErrorObject;

        let message = match parse_json_rpc_message(body_str) {
            Ok(msg) => msg,
            Err(rpc_err) => {
                error!("JSON-RPC parse error in streaming POST: {}", rpc_err);
                let error_json =
                    serde_json::to_string(&rpc_err).unwrap_or_else(|_| "{}".to_string());

                // Return error with MCP headers (no session header for parse errors)
                return Response::builder()
                    .status(StatusCode::OK) // JSON-RPC parse errors still use 200 OK
                    .header(CONTENT_TYPE, "application/json")
                    .header("MCP-Protocol-Version", context.protocol_version.as_str())
                    .body(
                        Full::new(Bytes::from(error_json))
                            .map_err(|never| match never {})
                            .boxed_unsync(),
                    )
                    .unwrap();
            }
        };

        // Handle sessionless ping (pre-init ping support per MCP 2025-11-25)
        // Clients are permitted to send pings before initialization completes.
        // When allowed by config, dispatch through the shared middleware + dispatch pipeline
        // with session=None, so rate-limiting middleware can still block abuse.
        let is_sessionless_ping = match &message {
            JsonRpcMessage::Request(req) => req.method == "ping",
            JsonRpcMessage::Notification(notif) => notif.method == "ping",
        } && context.session_id.is_none()
            && self.config.allow_unauthenticated_ping;

        if is_sessionless_ping {
            return match message {
                JsonRpcMessage::Request(request) => {
                    // Sessionless ping request: shared dispatch path
                    let (response, _) = self
                        .run_middleware_and_dispatch(request, context.headers.clone(), None, None)
                        .await;
                    let response_value =
                        serde_json::to_value(&response).unwrap_or(serde_json::json!({}));
                    StreamableResponse::Json(response_value).into_boxed_response(&context)
                }
                JsonRpcMessage::Notification(notification) => {
                    // Sessionless ping notification: dispatch and return 202 Accepted
                    let dispatcher = Arc::clone(&self.dispatcher);
                    tokio::spawn(async move {
                        if let Err(e) = dispatcher
                            .handle_notification_with_context(notification, None)
                            .await
                        {
                            error!("Failed to process sessionless ping notification: {}", e);
                        }
                    });

                    Response::builder()
                        .status(StatusCode::ACCEPTED)
                        .header("MCP-Protocol-Version", context.protocol_version.as_str())
                        .body(
                            Full::new(Bytes::new())
                                .map_err(|never| match never {})
                                .boxed_unsync(),
                        )
                        .unwrap()
                }
            };
        }

        // --- Pre-session auth phase (D4) ---
        // Extract Bearer token using hardened parser (D6)
        let bearer_token = context
            .headers
            .get("authorization")
            .and_then(|v| extract_bearer_token(v));

        // Run pre-session middleware if any are registered
        let pre_session_extensions = if self.middleware_stack.has_pre_session_middleware() {
            let method_name = match &message {
                JsonRpcMessage::Request(req) => req.method.as_str(),
                JsonRpcMessage::Notification(notif) => notif.method.as_str(),
            };
            let mut pre_ctx = crate::middleware::RequestContext::new(method_name, None);
            if let Some(ref token) = bearer_token {
                pre_ctx.set_bearer_token(token.clone());
            }
            // Copy headers to metadata, excluding Bearer authorization (D5)
            for (k, v) in &context.headers {
                if k.eq_ignore_ascii_case("authorization") && is_bearer_scheme(v) {
                    continue;
                }
                pre_ctx.add_metadata(k.clone(), serde_json::json!(v));
            }
            match self
                .middleware_stack
                .execute_before_session(&mut pre_ctx)
                .await
            {
                Ok(()) => Some(pre_ctx.take_extensions()),
                Err(crate::middleware::MiddlewareError::HttpChallenge {
                    status,
                    www_authenticate,
                    body,
                }) => {
                    return build_http_challenge_response(
                        status,
                        &www_authenticate,
                        body.as_deref(),
                        &context,
                    );
                }
                Err(other_err) => {
                    // Non-challenge pre-session errors → JSON-RPC error
                    if let JsonRpcMessage::Request(ref req) = message {
                        let response =
                            Self::map_middleware_error_to_jsonrpc(other_err, req.id.clone());
                        let response_value =
                            serde_json::to_value(&response).unwrap_or(serde_json::json!({}));
                        return StreamableResponse::Json(response_value)
                            .into_boxed_response(&context);
                    } else {
                        // Notification — can't return JSON-RPC error, just reject
                        return Response::builder()
                            .status(StatusCode::FORBIDDEN)
                            .body(
                                Full::new(Bytes::from(other_err.to_string()))
                                    .map_err(|never| match never {})
                                    .boxed_unsync(),
                            )
                            .unwrap();
                    }
                }
            }
        } else {
            None
        };

        // Validate session requirements based on method
        let session_id = match &message {
            JsonRpcMessage::Request(req) if req.method == "initialize" => {
                // Initialize can create session if none exists
                if let Some(existing_id) = &context.session_id {
                    // Validate existing session for initialize
                    if let Err(err) = self.validate_session_exists(existing_id).await {
                        warn!(
                            "Invalid session ID {} during initialize: {}",
                            existing_id,
                            err.message()
                        );
                        return StreamableResponse::Error {
                            status: err.status_code(),
                            message: format!("Invalid or expired session: {}", err.message()),
                        }
                        .into_boxed_response(&context);
                    }
                    existing_id.clone()
                } else {
                    // Create new session for initialize
                    match self
                        .session_storage
                        .create_session(self.server_capabilities.clone())
                        .await
                    {
                        Ok(session_info) => {
                            debug!(
                                "Created new session for initialize: {}",
                                session_info.session_id
                            );
                            context.session_id = Some(session_info.session_id.clone());
                            session_info.session_id
                        }
                        Err(err) => {
                            error!("Failed to create session during initialize: {}", err);
                            return StreamableResponse::Error {
                                status: StatusCode::INTERNAL_SERVER_ERROR,
                                message: "Failed to create session".to_string(),
                            }
                            .into_boxed_response(&context);
                        }
                    }
                }
            }
            JsonRpcMessage::Request(_) | JsonRpcMessage::Notification(_) => {
                // All other methods REQUIRE session ID
                if let Some(existing_id) = &context.session_id {
                    // Validate existing session
                    if let Err(err) = self.validate_session_exists(existing_id).await {
                        warn!("Invalid session ID {}: {}", existing_id, err.message());
                        return StreamableResponse::Error {
                            status: err.status_code(),
                            message: format!("Invalid or expired session: {}", err.message()),
                        }
                        .into_boxed_response(&context);
                    }
                    existing_id.clone()
                } else {
                    // Return 401 for missing header — this is NOT a stale session (404),
                    // it's "no session ID provided at all"
                    let method_name = match &message {
                        JsonRpcMessage::Request(req) => &req.method,
                        JsonRpcMessage::Notification(notif) => &notif.method,
                    };
                    let request_id = match &message {
                        JsonRpcMessage::Request(req) => Some(req.id.clone()),
                        JsonRpcMessage::Notification(_) => None,
                    };

                    warn!("Missing session ID for method: {}", method_name);

                    let error_response = turul_mcp_json_rpc_server::JsonRpcError::new(
                        request_id,
                        JsonRpcErrorObject::server_error(
                            -32001,
                            "Missing Mcp-Session-Id header. Call initialize first.",
                            None::<serde_json::Value>,
                        ),
                    );

                    let error_json =
                        serde_json::to_string(&error_response).unwrap_or_else(|_| "{}".to_string());

                    return Response::builder()
                        .status(StatusCode::UNAUTHORIZED)
                        .header(CONTENT_TYPE, "application/json")
                        .header("MCP-Protocol-Version", context.protocol_version.as_str())
                        .body(
                            Full::new(Bytes::from(error_json))
                                .map_err(|never| match never {})
                                .boxed_unsync(),
                        )
                        .unwrap();
                }
            }
        };

        debug!("Processing streaming request with session: {}", session_id);

        // Create streaming response using hyper::Body::channel()
        match message {
            JsonRpcMessage::Request(request) => {
                debug!(
                    "Processing streaming JSON-RPC request: method={}",
                    request.method
                );
                self.create_streaming_response(
                    request,
                    session_id,
                    context,
                    pre_session_extensions.clone(),
                )
                .await
            }
            JsonRpcMessage::Notification(notification) => {
                debug!(
                    "Processing streaming JSON-RPC notification: method={}",
                    notification.method
                );

                // Create session context with notification broadcaster for notifications
                use crate::notification_bridge::StreamManagerNotificationBroadcaster;
                use turul_mcp_json_rpc_server::SessionContext;

                let broadcaster = Arc::new(StreamManagerNotificationBroadcaster::new(Arc::clone(
                    &self.stream_manager,
                )));
                let broadcaster_any = Arc::new(broadcaster) as Arc<dyn std::any::Any + Send + Sync>;

                let session_context = SessionContext {
                    session_id: session_id.clone(),
                    metadata: std::collections::HashMap::new(),
                    broadcaster: Some(broadcaster_any),
                    timestamp: chrono::Utc::now().timestamp_millis() as u64,
                    extensions: std::collections::HashMap::new(),
                };

                // Process notification through dispatcher (notifications don't return responses)
                let dispatcher = Arc::clone(&self.dispatcher);
                let notification_clone = notification.clone();

                // notifications/initialized MUST be processed synchronously before
                // returning 202 — otherwise the next request (tools/list) can race
                // ahead of the is_initialized state write. Other notifications are
                // fire-and-forget and can be processed asynchronously.
                //
                // Per MCP spec, notifications always return 202 even on failure.
                // If processing fails, we log the error — the next request will
                // fail with "session not initialized" which points operators to
                // the initialization failure in logs.
                if notification_clone.method == "notifications/initialized" {
                    if let Err(e) = dispatcher
                        .handle_notification_with_context(notification_clone, Some(session_context))
                        .await
                    {
                        error!(
                            "Failed to process notifications/initialized: {}. \
                             Session will remain uninitialized — subsequent requests will fail.",
                            e
                        );
                    }
                } else {
                    tokio::spawn(async move {
                        if let Err(e) = dispatcher
                            .handle_notification_with_context(
                                notification_clone,
                                Some(session_context),
                            )
                            .await
                        {
                            error!("Failed to process notification: {}", e);
                        }
                    });
                }

                // Return 202 Accepted with MCP headers
                Response::builder()
                    .status(StatusCode::ACCEPTED)
                    .header("MCP-Protocol-Version", context.protocol_version.as_str())
                    .header("Mcp-Session-Id", &session_id)
                    .body(
                        Full::new(Bytes::new())
                            .map_err(|never| match never {})
                            .boxed_unsync(),
                    )
                    .unwrap()
            }
        }
    }

    /// Create a streaming response using hyper::Body::channel()
    /// This enables true progressive responses with Transfer-Encoding: chunked
    async fn create_streaming_response(
        &self,
        request: turul_mcp_json_rpc_server::JsonRpcRequest,
        session_id: String,
        context: StreamableHttpContext,
        pre_session_extensions: Option<HashMap<String, serde_json::Value>>,
    ) -> Response<http_body_util::combinators::UnsyncBoxBody<Bytes, hyper::Error>> {
        debug!(
            "Creating streaming response for method: {}, session: {}",
            request.method, session_id
        );
        // Create channel for streaming response
        use http_body_util::StreamBody;
        use tokio_stream::StreamExt;
        use tokio_stream::wrappers::UnboundedReceiverStream; // Add StreamExt for map method

        let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<Result<bytes::Bytes, hyper::Error>>();
        let body_stream =
            UnboundedReceiverStream::new(rx).map(|item| item.map(http_body::Frame::data));
        let body = StreamBody::new(body_stream);

        // Create session context with notification broadcaster (same pattern as SessionMcpHandler)
        use crate::notification_bridge::{
            SharedNotificationBroadcaster, StreamManagerNotificationBroadcaster,
        };
        use turul_mcp_json_rpc_server::SessionContext;

        let broadcaster: SharedNotificationBroadcaster = Arc::new(
            StreamManagerNotificationBroadcaster::new(Arc::clone(&self.stream_manager)),
        );
        let broadcaster_any = Arc::new(broadcaster) as Arc<dyn std::any::Any + Send + Sync>;

        let session_context = SessionContext {
            session_id: session_id.clone(),
            metadata: std::collections::HashMap::new(),
            broadcaster: Some(broadcaster_any),
            timestamp: chrono::Utc::now().timestamp_millis() as u64,
            extensions: std::collections::HashMap::new(),
        };

        // Register streaming POST connection with StreamManager for progress events
        // Transport policy: prefer JSON for non-streaming methods when client accepts both
        let wants_sse = context.should_use_sse(&request.method);
        let connection_id = format!("post-{}", uuid::Uuid::now_v7().as_simple());

        // Progress forwarding only for SSE clients
        let (shutdown_tx, completion_rx) = if wants_sse {
            // Create shutdown signal for progress task (critical for no-progress-events case)
            let (shutdown_tx, mut shutdown_rx) = tokio::sync::oneshot::channel::<()>();
            let (completion_tx, completion_rx) = tokio::sync::oneshot::channel::<()>();
            let (progress_tx, mut progress_rx) = tokio::sync::mpsc::channel(100);

            // Register with StreamManager to receive progress events
            let registration_result = self
                .stream_manager
                .register_streaming_connection(&session_id, connection_id.clone(), progress_tx)
                .await;

            if let Err(e) = registration_result {
                error!("Failed to register POST streaming connection: {}", e);
                // Continue without streaming - will still work as regular POST
                (None, None)
            } else {
                debug!(
                    "Registered SSE streaming connection for session: {}",
                    session_id
                );

                // Spawn task to forward progress events to HTTP response
                let sender_clone = tx.clone();
                let session_id_clone = session_id.clone();
                let connection_id_clone = connection_id.clone();
                let stream_manager_clone = Arc::clone(&self.stream_manager);

                tokio::spawn(async move {
                    debug!(
                        "Starting progress forwarding task for session: {}",
                        session_id_clone
                    );

                    // No replay needed: the POST SSE connection is registered BEFORE
                    // dispatch, so all events emitted during request execution are
                    // delivered live via progress_tx. Replay would double-deliver them.
                    // (GET SSE resumability uses Last-Event-ID separately.)

                    // CRITICAL: Use select to handle both progress events AND explicit shutdown
                    loop {
                        debug!(
                            "🔍 Progress task entering select loop for session: {}",
                            session_id_clone
                        );
                        tokio::select! {
                            // Handle progress events if they arrive
                            maybe_event = progress_rx.recv() => {
                                debug!("🔍 Progress task: progress_rx.recv() branch fired for session: {}", session_id_clone);
                                match maybe_event {
                                    Some(sse_event) => {
                                        debug!("🔍 Forwarding progress event to POST response: session={}, event={:?}", session_id_clone, sse_event.event_type);

                                        // Convert SSE event to fully-formatted SSE chunk with event metadata
                                        let sse_chunk = sse_event.format();

                                        if let Err(e) = sender_clone.send(Ok(Bytes::from(sse_chunk))) {
                                            error!("Failed to send progress event to POST response: {}", e);
                                            break;
                                        }
                                    }
                                    None => {
                                        // Progress channel closed naturally
                                        debug!("🔍 Progress channel closed naturally for session: {}", session_id_clone);
                                        break;
                                    }
                                }
                            }
                            // Handle explicit shutdown signal from main task
                            _ = &mut shutdown_rx => {
                                debug!("🔍 Progress task: shutdown_rx branch fired! Received explicit shutdown signal for session: {}", session_id_clone);
                                break;
                            }
                        }
                    }

                    // Clean up: Unregister from StreamManager to close progress_tx
                    debug!(
                        "Progress task unregistering connection for session: {}",
                        session_id_clone
                    );
                    stream_manager_clone
                        .unregister_connection(&session_id_clone, &connection_id_clone)
                        .await;

                    // CRITICAL: Drop the sender to ensure stream can close
                    debug!(
                        "🔍 Progress task: dropping sender_clone for session: {}",
                        session_id_clone
                    );
                    drop(sender_clone);

                    // Signal completion to main task
                    debug!(
                        "🔍 Progress task: signaling completion for session: {}",
                        session_id_clone
                    );
                    if completion_tx.send(()).is_err() {
                        debug!(
                            "🔍 Progress task: main task already dropped completion_rx for session: {}",
                            session_id_clone
                        );
                    }

                    debug!(
                        "🔍 Progress forwarding task completed for session: {}",
                        session_id_clone
                    );
                });

                // Return shutdown_tx and completion_rx for later use
                (Some(shutdown_tx), Some(completion_rx))
            }
        } else {
            // No SSE, no shutdown signal needed
            (None, None)
        };

        // Spawn task to handle streaming dispatch
        let request_id = request.id.clone();
        let sender = tx; // Rename for clarity

        // Capture headers for middleware (clone before move into spawn)
        let headers = context.headers.clone();
        let self_clone = self.clone();

        tokio::spawn(async move {
            debug!(
                "Spawning streaming task for request ID: {:?}, wants_sse: {}",
                request_id, wants_sse
            );

            // Process actual request through middleware pipeline
            // Injection is applied immediately inside run_middleware_and_dispatch
            let (response, _) = self_clone
                .run_middleware_and_dispatch(
                    request,
                    headers,
                    Some(session_context),
                    pre_session_extensions,
                )
                .await;

            // Send final result - format depends on client type
            if wants_sse {
                // For SSE clients, send as streaming frame with SSE framing
                let final_frame = match response {
                    turul_mcp_json_rpc_server::JsonRpcMessage::Response(resp) => {
                        turul_mcp_json_rpc_server::JsonRpcFrame::FinalResult {
                            request_id: request_id.clone(),
                            result: match resp.result {
                                turul_mcp_json_rpc_server::response::ResponseResult::Success(
                                    val,
                                ) => val,
                                turul_mcp_json_rpc_server::response::ResponseResult::Null => {
                                    serde_json::Value::Null
                                }
                            },
                        }
                    }
                    turul_mcp_json_rpc_server::JsonRpcMessage::Error(err) => {
                        turul_mcp_json_rpc_server::JsonRpcFrame::Error {
                            request_id: request_id.clone(),
                            error: turul_mcp_json_rpc_server::error::JsonRpcErrorObject {
                                code: err.error.code,
                                message: err.error.message,
                                data: err.error.data,
                            },
                        }
                    }
                };

                let final_json = final_frame.to_json();
                // SSE framing: data: {json}\n\n
                let final_chunk =
                    format!("data: {}\n\n", serde_json::to_string(&final_json).unwrap());

                if let Err(err) = sender.send(Ok(Bytes::from(final_chunk))) {
                    error!("Failed to send SSE final chunk: {}", err);
                }

                // CRITICAL: Send explicit shutdown signal to progress forwarding task (SSE only)
                // This breaks it out of the progress_rx.recv().await loop immediately
                if let Some(shutdown_tx) = shutdown_tx {
                    debug!(
                        "🔍 Main task sending shutdown signal to progress task for request: {:?}",
                        request_id
                    );
                    match shutdown_tx.send(()) {
                        Ok(()) => {
                            debug!(
                                "🔍 Main task: shutdown signal sent successfully for request: {:?}",
                                request_id
                            );

                            // CRITICAL: Wait for progress task to complete and drop its sender_clone
                            // This ensures both senders are dropped before the stream tries to close
                            if let Some(completion_rx) = completion_rx {
                                match tokio::time::timeout(
                                    tokio::time::Duration::from_millis(100),
                                    completion_rx,
                                )
                                .await
                                {
                                    Ok(Ok(())) => {
                                        debug!(
                                            "🔍 Main task: progress task completed successfully for request: {:?}",
                                            request_id
                                        );
                                    }
                                    Ok(Err(_)) => {
                                        debug!(
                                            "🔍 Main task: progress task completion signal dropped for request: {:?}",
                                            request_id
                                        );
                                    }
                                    Err(_) => {
                                        debug!(
                                            "🔍 Main task: progress task completion timeout for request: {:?}",
                                            request_id
                                        );
                                    }
                                }
                            }
                        }
                        Err(_) => {
                            debug!(
                                "🔍 Main task: progress task already completed (shutdown_rx dropped) for request: {:?}",
                                request_id
                            );
                        }
                    }
                } else {
                    debug!(
                        "🔍 Main task: no shutdown_tx available (not SSE client) for request: {:?}",
                        request_id
                    );
                }
            } else {
                // For JSON-only clients, send as regular JSON-RPC response (no streaming frames)
                let final_json = serde_json::to_string(&response).unwrap();

                if let Err(err) = sender.send(Ok(Bytes::from(final_json))) {
                    error!("Failed to send final JSON response: {}", err);
                }
            }

            debug!(
                "🔍 Main task: streaming task completed for request ID: {:?}",
                request_id
            );

            // CRITICAL: Drop the sender to close the stream and signal completion to client
            debug!(
                "🔍 Main task: dropping main sender for request ID: {:?}",
                request_id
            );
            drop(sender);
        });

        // Build response with MCP headers merged from context
        // Content-Type must match the framing decision made by wants_sse above
        let content_type = if wants_sse {
            "text/event-stream"
        } else {
            "application/json"
        };

        let mut response = Response::builder()
            .status(StatusCode::OK)
            .header(CONTENT_TYPE, content_type)
            .header("Transfer-Encoding", "chunked") // Key: Enable chunked encoding!
            .header("Cache-Control", "no-cache")
            .body(http_body_util::BodyExt::boxed_unsync(body))
            .unwrap();

        // Merge MCP headers from context.response_headers()
        let mcp_headers = context.response_headers();
        for (key, value) in mcp_headers.iter() {
            response.headers_mut().insert(key, value.clone());
        }

        response
    }

    /// Handle POST with buffered response (fallback for legacy clients)
    #[allow(dead_code)]
    async fn handle_buffered_post<T>(
        &self,
        _req: Request<T>,
        context: StreamableHttpContext,
        session_id: String,
    ) -> Response<http_body_util::combinators::UnsyncBoxBody<Bytes, hyper::Error>>
    where
        T: Body + Send + 'static,
    {
        debug!(
            "Using buffered POST for legacy client, session: {}",
            session_id
        );

        // Use the existing logic (simplified version)
        // TODO: Extract common logic into helper method

        Response::builder()
            .status(StatusCode::OK)
            .header(CONTENT_TYPE, "application/json")
            .header("MCP-Protocol-Version", context.protocol_version.as_str())
            .header("Mcp-Session-Id", &session_id)
            .body(
                Full::new(Bytes::from(
                    r#"{"jsonrpc":"2.0","id":1,"result":"buffered"}"#,
                ))
                .map_err(|never| match never {})
                .boxed_unsync(),
            )
            .unwrap()
    }

    /// Handle POST request - unified handler for all client messages (MCP 2025-11-25 compliant)
    /// Processes JSON-RPC requests, notifications, and responses
    /// Server decides whether to respond with JSON or SSE stream based on message type
    async fn handle_client_message<T>(
        &self,
        req: Request<T>,
        context: StreamableHttpContext,
    ) -> Response<http_body_util::combinators::UnsyncBoxBody<Bytes, hyper::Error>>
    where
        T: Body + Send + 'static,
    {
        debug!("Handling client message via POST (MCP 2025-11-25)");

        // Reject POST if accepts_stream_frames is false
        // Per MCP spec: "Include Accept header with application/json and text/event-stream"
        if !context.accepts_stream_frames {
            warn!("Client POST missing application/json in Accept header");
            return StreamableResponse::Error {
                status: StatusCode::BAD_REQUEST,
                message: "Accept header must include application/json, text/event-stream, or */*"
                    .to_string(),
            }
            .into_boxed_response(&context);
        }

        // Check content type
        let content_type = req
            .headers()
            .get(CONTENT_TYPE)
            .and_then(|ct| ct.to_str().ok())
            .map(normalize_header_value)
            .unwrap_or_default();
        if !content_type.starts_with("application/json") {
            warn!("Invalid content type for POST: {}", content_type);
            return StreamableResponse::Error {
                status: StatusCode::BAD_REQUEST,
                message: "Content-Type must be application/json".to_string(),
            }
            .into_boxed_response(&context);
        }

        // Use streaming for all POST requests, but adapt based on client needs
        // For simple JSON clients, streaming will send only the final result (no progress frames)
        debug!("Using streaming POST handler for all requests");
        return self.handle_post_streamable_http(req, context).await;
    }

    /// Run middleware stack around dispatcher call
    ///
    /// This helper:
    /// 1. Fast-paths when middleware stack is empty
    /// 2. Normalizes headers to lowercase String keys
    /// 3. Builds RequestContext with method + headers
    /// 4. Executes before_dispatch
    /// 5. Applies or stashes SessionInjection
    /// 6. Calls dispatcher
    /// 7. Executes after_dispatch
    ///
    /// Returns (JsonRpcMessage, Option<SessionInjection>) where the injection
    /// is Some when session was None (initialize case) and needs to be applied
    /// after session creation.
    async fn run_middleware_and_dispatch(
        &self,
        request: turul_mcp_json_rpc_server::JsonRpcRequest,
        headers: HashMap<String, String>,
        session: Option<turul_mcp_json_rpc_server::SessionContext>,
        pre_session_extensions: Option<HashMap<String, serde_json::Value>>,
    ) -> (
        turul_mcp_json_rpc_server::JsonRpcMessage,
        Option<crate::middleware::SessionInjection>,
    ) {
        // Fast path: if middleware stack is empty, dispatch directly
        if self.middleware_stack.is_empty() {
            let result = if let Some(session_ctx) = session {
                self.dispatcher
                    .handle_request_with_context(request, session_ctx)
                    .await
            } else {
                self.dispatcher.handle_request(request).await
            };
            return (result, None);
        }

        // Normalize headers: lowercase String keys
        let normalized_headers: HashMap<String, String> = headers
            .iter()
            .map(|(k, v)| (k.to_lowercase(), v.clone()))
            .collect();

        // Build RequestContext with method and headers
        let method = request.method.clone();

        // Convert params to Option<Value>
        let params = request.params.clone().map(|p| match p {
            turul_mcp_json_rpc_server::RequestParams::Object(map) => {
                serde_json::Value::Object(map.into_iter().collect())
            }
            turul_mcp_json_rpc_server::RequestParams::Array(arr) => serde_json::Value::Array(arr),
        });
        let mut ctx = crate::middleware::RequestContext::new(&method, params);

        // Seed extensions from pre-session phase (auth claims etc.)
        if let Some(ext) = pre_session_extensions {
            for (k, v) in ext {
                ctx.set_extension(k, v);
            }
        }

        for (k, v) in normalized_headers {
            // D5: Bearer scheme NEVER enters metadata, even if token is malformed
            if k == "authorization" && is_bearer_scheme(&v) {
                continue;
            }
            ctx.add_metadata(k, serde_json::json!(v));
        }

        // Create SessionView adapter if session is available
        let session_view = session.as_ref().map(|s| {
            crate::middleware::StorageBackedSessionView::new(
                s.session_id.clone(),
                Arc::clone(&self.session_storage),
            )
        });

        // Execute before_dispatch with SessionView (None if sessionless)
        let injection = match self
            .middleware_stack
            .execute_before(
                &mut ctx,
                session_view.as_ref().map(|v| v as &dyn SessionView),
            )
            .await
        {
            Ok(inj) => inj,
            Err(err) => {
                return (Self::map_middleware_error_to_jsonrpc(err, request.id), None);
            }
        };

        // Apply injection to session storage (only if session exists)
        if !injection.is_empty()
            && let Some(ref sv) = session_view
        {
            for (key, value) in injection.state() {
                if let Err(e) = sv.set_state(key, value.clone()).await {
                    tracing::warn!("Failed to apply injection state '{}': {}", key, e);
                }
            }
            for (key, value) in injection.metadata() {
                if let Err(e) = sv.set_metadata(key, value.clone()).await {
                    tracing::warn!("Failed to apply injection metadata '{}': {}", key, e);
                }
            }
        }

        // Thread extensions from RequestContext → JSON-RPC SessionContext (D3 canonical flow)
        let session = session.map(|mut s| {
            s.extensions = ctx.extensions().clone();
            s
        });

        // Save request ID before dispatch consumes the request
        let request_id = request.id.clone();

        // Dispatch the request
        let result = if let Some(session_ctx) = session {
            self.dispatcher
                .handle_request_with_context(request, session_ctx)
                .await
        } else {
            self.dispatcher.handle_request(request).await
        };

        // Execute after_dispatch
        let mut dispatcher_result = match &result {
            turul_mcp_json_rpc_server::JsonRpcMessage::Response(resp) => match &resp.result {
                turul_mcp_json_rpc_server::response::ResponseResult::Success(val) => {
                    crate::middleware::DispatcherResult::Success(val.clone())
                }
                turul_mcp_json_rpc_server::response::ResponseResult::Null => {
                    crate::middleware::DispatcherResult::Success(serde_json::Value::Null)
                }
            },
            turul_mcp_json_rpc_server::JsonRpcMessage::Error(err) => {
                crate::middleware::DispatcherResult::Error(err.error.message.clone())
            }
        };

        match self
            .middleware_stack
            .execute_after(&ctx, &mut dispatcher_result)
            .await
        {
            Ok(()) => {
                let result = Self::apply_dispatcher_result(result, dispatcher_result);
                (result, None)
            }
            Err(middleware_err) => (
                Self::map_middleware_error_to_jsonrpc(middleware_err, request_id),
                None,
            ),
        }
    }

    /// Apply potentially-mutated `DispatcherResult` back into the `JsonRpcMessage`.
    ///
    /// Handles all four mutation paths per the middleware contract:
    /// - Success → Success: value mutated in place
    /// - Success → Error: middleware rejected response (INTERNAL_ERROR -32603)
    /// - Error → Success: middleware recovered (only when error has request ID)
    /// - Error → Error: error message mutated
    fn apply_dispatcher_result(
        result: turul_mcp_json_rpc_server::JsonRpcMessage,
        dispatcher_result: crate::middleware::DispatcherResult,
    ) -> turul_mcp_json_rpc_server::JsonRpcMessage {
        match dispatcher_result {
            crate::middleware::DispatcherResult::Success(val) => match result {
                turul_mcp_json_rpc_server::JsonRpcMessage::Response(mut resp) => {
                    resp.result = turul_mcp_json_rpc_server::response::ResponseResult::Success(val);
                    turul_mcp_json_rpc_server::JsonRpcMessage::Response(resp)
                }
                turul_mcp_json_rpc_server::JsonRpcMessage::Error(err) => {
                    // Error→Success recovery: only when error has a request ID
                    match err.id {
                        Some(id) => turul_mcp_json_rpc_server::JsonRpcMessage::Response(
                            turul_mcp_json_rpc_server::response::JsonRpcResponse::success(id, val),
                        ),
                        None => turul_mcp_json_rpc_server::JsonRpcMessage::Error(err),
                    }
                }
            },
            crate::middleware::DispatcherResult::Error(msg) => match result {
                turul_mcp_json_rpc_server::JsonRpcMessage::Response(resp) => {
                    turul_mcp_json_rpc_server::JsonRpcMessage::Error(
                        turul_mcp_json_rpc_server::error::JsonRpcError::new(
                            Some(resp.id),
                            turul_mcp_json_rpc_server::error::JsonRpcErrorObject::internal_error(
                                Some(msg),
                            ),
                        ),
                    )
                }
                turul_mcp_json_rpc_server::JsonRpcMessage::Error(mut err) => {
                    err.error.message = msg;
                    turul_mcp_json_rpc_server::JsonRpcMessage::Error(err)
                }
            },
        }
    }

    /// Map MiddlewareError to JSON-RPC error with semantic error codes
    fn map_middleware_error_to_jsonrpc(
        err: crate::middleware::MiddlewareError,
        request_id: turul_mcp_json_rpc_server::RequestId,
    ) -> turul_mcp_json_rpc_server::JsonRpcMessage {
        use crate::middleware::MiddlewareError;
        use crate::middleware::error::error_codes;

        let (code, message, data) = match err {
            MiddlewareError::Unauthenticated(msg) => (error_codes::UNAUTHENTICATED, msg, None),
            MiddlewareError::Unauthorized(msg) => (error_codes::UNAUTHORIZED, msg, None),
            MiddlewareError::RateLimitExceeded {
                message,
                retry_after,
            } => {
                let data = retry_after.map(|s| serde_json::json!({"retryAfter": s}));
                (error_codes::RATE_LIMIT_EXCEEDED, message, data)
            }
            MiddlewareError::InvalidRequest(msg) => (error_codes::INVALID_REQUEST, msg, None),
            MiddlewareError::Internal(msg) => (error_codes::INTERNAL_ERROR, msg, None),
            MiddlewareError::Custom { message, .. } => (error_codes::INTERNAL_ERROR, message, None),
            MiddlewareError::HttpChallenge { .. } => {
                unreachable!(
                    "HttpChallenge must be caught at transport level before JSON-RPC dispatch"
                )
            }
        };

        let error_obj = if let Some(d) = data {
            turul_mcp_json_rpc_server::error::JsonRpcErrorObject::server_error(
                code,
                &message,
                Some(d),
            )
        } else {
            turul_mcp_json_rpc_server::error::JsonRpcErrorObject::server_error(
                code,
                &message,
                None::<serde_json::Value>,
            )
        };

        turul_mcp_json_rpc_server::JsonRpcMessage::Error(
            turul_mcp_json_rpc_server::JsonRpcError::new(Some(request_id), error_obj),
        )
    }
}

use crate::middleware::bearer::{extract_bearer_token, is_bearer_scheme};

/// Build an HTTP challenge response (401/403 with WWW-Authenticate header).
///
/// Returns a raw HTTP response — never enters the JSON-RPC layer.
fn build_http_challenge_response(
    status: u16,
    www_authenticate: &str,
    body: Option<&str>,
    context: &StreamableHttpContext,
) -> Response<http_body_util::combinators::UnsyncBoxBody<Bytes, hyper::Error>> {
    let status_code = StatusCode::from_u16(status).unwrap_or(StatusCode::UNAUTHORIZED);
    let body_bytes = body.unwrap_or("").to_string();

    Response::builder()
        .status(status_code)
        .header("WWW-Authenticate", www_authenticate)
        .header("Cache-Control", "no-store")
        .header("Content-Type", "application/json")
        .header("MCP-Protocol-Version", context.protocol_version.as_str())
        .body(
            http_body_util::Full::new(Bytes::from(body_bytes))
                .map_err(|never| match never {})
                .boxed_unsync(),
        )
        .unwrap()
}

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

    #[test]
    fn test_version_parsing() {
        assert_eq!(
            McpProtocolVersion::parse_version("2024-11-05"),
            Some(McpProtocolVersion::V2024_11_05)
        );
        assert_eq!(
            McpProtocolVersion::parse_version("2025-03-26"),
            Some(McpProtocolVersion::V2025_03_26)
        );
        assert_eq!(
            McpProtocolVersion::parse_version("2025-06-18"),
            Some(McpProtocolVersion::V2025_06_18)
        );
        assert_eq!(McpProtocolVersion::parse_version("invalid"), None);
    }

    #[test]
    fn test_version_capabilities() {
        let v1 = McpProtocolVersion::V2024_11_05;
        assert!(!v1.supports_streamable_http());
        assert!(!v1.supports_meta_fields());

        let v2 = McpProtocolVersion::V2025_03_26;
        assert!(v2.supports_streamable_http());
        assert!(!v2.supports_meta_fields());

        let v3 = McpProtocolVersion::V2025_06_18;
        assert!(v3.supports_streamable_http());
        assert!(v3.supports_meta_fields());
        assert!(v3.supports_cursors());
        assert!(v3.supports_progress_tokens());
        assert!(v3.supports_elicitation());
    }

    #[test]
    fn test_context_validation() {
        let mut context = StreamableHttpContext {
            protocol_version: McpProtocolVersion::V2025_06_18,
            session_id: Some("test-session".to_string()),
            wants_sse_stream: true,
            accepts_json: true,
            accepts_stream_frames: true,
            headers: HashMap::new(),
        };

        // POST with session should be valid
        assert!(context.validate(&Method::POST).is_ok());
        // GET with session should be valid
        assert!(context.validate(&Method::GET).is_ok());

        // Test invalid cases
        context.accepts_stream_frames = false;
        assert!(context.validate(&Method::POST).is_err());

        context.accepts_stream_frames = true;
        context.protocol_version = McpProtocolVersion::V2024_11_05;
        context.wants_sse_stream = true;
        assert!(context.validate(&Method::POST).is_err());

        context.protocol_version = McpProtocolVersion::V2025_06_18;
        context.session_id = None;
        // POST without session should be OK (for initialize)
        assert!(context.validate(&Method::POST).is_ok());
        // GET without session should fail
        assert!(context.validate(&Method::GET).is_err());
    }

    // Bearer extraction tests are in middleware::bearer::tests
    // Transport-level integration tests:

    #[test]
    fn test_non_bearer_preserved_in_metadata() {
        // T5: Non-bearer auth headers pass through, Bearer excluded
        let mut ctx = crate::middleware::RequestContext::new("test/method", None);

        let headers = vec![
            (
                "authorization".to_string(),
                "Basic dXNlcjpwYXNz".to_string(),
            ),
            ("x-custom".to_string(), "value".to_string()),
        ];

        for (k, v) in &headers {
            if k == "authorization" && is_bearer_scheme(v) {
                continue;
            }
            ctx.add_metadata(k.clone(), serde_json::json!(v));
        }

        assert!(ctx.metadata().contains_key("authorization"));
        assert!(ctx.metadata().contains_key("x-custom"));

        // Bearer exclusion
        let mut ctx2 = crate::middleware::RequestContext::new("test/method", None);
        let bearer_headers = vec![
            ("authorization".to_string(), "Bearer abc123".to_string()),
            ("x-custom".to_string(), "value".to_string()),
        ];

        for (k, v) in &bearer_headers {
            if k == "authorization" && is_bearer_scheme(v) {
                continue;
            }
            ctx2.add_metadata(k.clone(), serde_json::json!(v));
        }

        assert!(!ctx2.metadata().contains_key("authorization"));
        assert!(ctx2.metadata().contains_key("x-custom"));
    }

    #[test]
    fn test_malformed_bearer_excluded_from_metadata() {
        // T47: Even malformed Bearer tokens are excluded from metadata
        let mut ctx = crate::middleware::RequestContext::new("test/method", None);

        let headers = vec![("authorization".to_string(), "Bearer ".to_string())];

        for (k, v) in &headers {
            if k == "authorization" && is_bearer_scheme(v) {
                continue;
            }
            ctx.add_metadata(k.clone(), serde_json::json!(v));
        }

        assert!(!ctx.metadata().contains_key("authorization"));
    }
}