agent-harness-rs 0.1.3

Agent loop harness with local and sandbox tool runtimes, context management, and MCP support
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
//! MCP (Model Context Protocol) client + tool runtime.
//!
//! Speaks MCP 2024-11-05 in two transports:
//!
//! - **HTTP POST** (`bootstrap.mcp_servers[].type = "url"`): every request
//!   is a self-contained `POST <url>` with a JSON-RPC 2.0 body. We do
//!   NOT do the SSE streaming variant of the streamable HTTP transport.
//! - **stdio** (`bootstrap.mcp_servers[].type = "stdio"`): spawn the
//!   configured `command` as a child process, exchange newline-delimited
//!   JSON-RPC 2.0 messages over its stdin/stdout. Stderr is forwarded to
//!   the RD tracing log under `target = "harness::mcp::stdio"`. The
//!   process lives for the McpClient's lifetime (one per MCP server);
//!   Drop kills it (best-effort SIGKILL via `tokio::process::Child::kill`).
//!
//! All configuration (command, args, env vars to forward, working dir,
//! HTTP url, timeout) is sourced from bootstrap.yaml — the harness
//! crate never reads `std::env::var` directly.
//!
//! Lifecycle on a session boot:
//!   1. `McpToolRuntime::discover(servers)`
//!      ├─ for each server: `McpClient::new` → `initialize` →
//!      │  `tools/list` → cache the spec list with `{server}__` prefix
//!      └─ unreachable servers log + skip (don't fail the session)
//!   2. AgentLoopHarness sees the MCP tools through the composite
//!      `ToolRuntime` (native + MCP merged via `CompositeToolRuntime`)
//!   3. On invocation: route by tool-name prefix to the right
//!      `McpClient.tools_call`, strip prefix before sending to server
//!
//! Session-id handling: some MCP server implementations issue an
//! `Mcp-Session-Id` header on `initialize` and require it on subsequent
//! requests. `McpClient` records the first non-empty value it sees and
//! replays it; servers that don't issue one stay stateless and that's
//! fine too.

use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, RwLock};
use std::time::Duration;

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

use crate::model::{ImageData, ImageSource, UserAttachment};
use crate::tools::{
    ToolFailure, ToolFailureKind, ToolInvocation, ToolOutcome, ToolRuntime, ToolRuntimeError,
    ToolSpec,
};

/// MCP protocol version we negotiate with the server. The wire format
/// is stable across patches; bump this only when the spec maintainers
/// publish a version that changes the methods we use (`initialize` /
/// `tools/list` / `tools/call`).
pub const MCP_PROTOCOL_VERSION: &str = "2024-11-05";

/// Default per-request timeout. MCP tools can be slow (calling other
/// LLMs / external APIs) but 30 s catches the common deadlocks. Same
/// budget governs HTTP requests and stdio request-response round-trips.
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

/// Transport-agnostic config for a single MCP server. The variant
/// determines whether we POST to a URL or spawn a child process.
#[derive(Debug, Clone)]
pub struct McpServerConfig {
    pub name: String,
    /// Per-call tool invocation timeout (maps to `tool_timeout_sec`).
    /// Default: 30 s for backwards-compat; bootstrap sets it to
    /// the MCP-spec default of 60 s via `with_timeout`.
    pub timeout: Duration,
    /// Time allowed for `initialize` + `tools/list` handshake.
    /// Default: 10 s (OpenAI Codex default).
    pub startup_timeout: Duration,
    /// If `true`, a failure to initialise aborts session boot.
    /// Default: `false` (unreachable servers are silently skipped).
    pub required: bool,
    /// Tool allowlist (short names, no `{server}__` prefix).
    /// Empty = all tools exposed.
    pub enabled_tools: Vec<String>,
    pub transport: McpTransport,
}

/// Transport mechanism for an MCP server.
#[derive(Debug, Clone)]
pub enum McpTransport {
    /// MCP over HTTP POST / SSE. Sessions are tracked via the
    /// `Mcp-Session-Id` header per spec.
    Http {
        url: String,
        /// Static headers sent on every request (e.g. `Authorization`).
        headers: HashMap<String, String>,
    },
    /// MCP over stdio: spawn `command` (with `args`, `env`, `working_dir`),
    /// exchange newline-delimited JSON-RPC 2.0 messages over its
    /// stdin/stdout. The process is owned by the `McpClient`; Drop
    /// kills it.
    ///
    /// `env` is the **full** env passed to the child — anything not in
    /// this map is NOT inherited (we deliberately don't read
    /// `std::env::vars()` so all per-session secrets stay in
    /// bootstrap.yaml). The child still receives a baseline `PATH`
    /// derived from `command` lookup, but no other host env leaks in.
    Stdio {
        command: String,
        args: Vec<String>,
        env: HashMap<String, String>,
        working_dir: Option<String>,
    },
}

impl McpServerConfig {
    /// HTTP-transport convenience constructor. Same name as the v1
    /// `new()` for source compat.
    pub fn new(name: impl Into<String>, url: impl Into<String>) -> Self {
        Self::http(name, url)
    }

    pub fn http(name: impl Into<String>, url: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            timeout: DEFAULT_TIMEOUT,
            startup_timeout: Duration::from_secs(10),
            required: false,
            enabled_tools: Vec::new(),
            transport: McpTransport::Http {
                url: url.into(),
                headers: HashMap::new(),
            },
        }
    }

    /// stdio-transport convenience constructor. `env` / `working_dir`
    /// default to empty; use the struct literal form or chain setters
    /// if you need them.
    pub fn stdio(name: impl Into<String>, command: impl Into<String>, args: Vec<String>) -> Self {
        Self {
            name: name.into(),
            timeout: DEFAULT_TIMEOUT,
            startup_timeout: Duration::from_secs(10),
            required: false,
            enabled_tools: Vec::new(),
            transport: McpTransport::Stdio {
                command: command.into(),
                args,
                env: HashMap::new(),
                working_dir: None,
            },
        }
    }

    /// Per-call tool invocation timeout.
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Time allowed for `initialize` + `tools/list` handshake.
    pub fn with_startup_timeout(mut self, timeout: Duration) -> Self {
        self.startup_timeout = timeout;
        self
    }

    /// If `true`, init failure aborts session boot.
    pub fn with_required(mut self, required: bool) -> Self {
        self.required = required;
        self
    }

    /// Tool allowlist (short names without `{server}__` prefix).
    pub fn with_enabled_tools(mut self, tools: Vec<String>) -> Self {
        self.enabled_tools = tools;
        self
    }

    /// Add a static HTTP request header (HTTP transport only).
    pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        if let McpTransport::Http { headers, .. } = &mut self.transport {
            headers.insert(key.into(), value.into());
        }
        self
    }

    pub fn with_env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        if let McpTransport::Stdio { env, .. } = &mut self.transport {
            env.insert(key.into(), value.into());
        }
        self
    }

    pub fn with_working_dir(mut self, dir: impl Into<String>) -> Self {
        if let McpTransport::Stdio { working_dir, .. } = &mut self.transport {
            *working_dir = Some(dir.into());
        }
        self
    }
}

#[derive(Debug, thiserror::Error)]
pub enum McpError {
    #[error("timeout: {0}")]
    Timeout(String),
    #[error("transport: {0}")]
    Transport(String),
    #[error("HTTP {status}: {body}")]
    Http { status: u16, body: String },
    #[error("decode: {0}")]
    Decode(String),
    #[error("server error code={code} message={message}")]
    Server { code: i64, message: String },
    #[error("missing field {0}")]
    MissingField(&'static str),
}

// ── JSON-RPC 2.0 wire types ─────────────────────────────────────────

#[derive(Debug, Serialize)]
struct McpRequest<'a> {
    jsonrpc: &'static str,
    id: u64,
    method: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    params: Option<Value>,
}

#[derive(Debug, Serialize)]
struct McpNotification<'a> {
    jsonrpc: &'static str,
    method: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    params: Option<Value>,
}

#[derive(Debug, Deserialize)]
struct McpResponse {
    #[allow(dead_code)]
    jsonrpc: String,
    /// `None` for server-initiated notifications (stdio transport). HTTP
    /// transport always carries the response id, but we accept both
    /// shapes from the same parse routine.
    id: Option<u64>,
    result: Option<Value>,
    error: Option<McpResponseError>,
}

#[derive(Debug, Deserialize)]
struct McpResponseError {
    code: i64,
    message: String,
    #[serde(default)]
    #[allow(dead_code)]
    data: Option<Value>,
}

// ── MCP-specific payload types (under JSON-RPC `result`) ────────────

#[derive(Debug, Deserialize)]
struct McpToolDef {
    name: String,
    #[serde(default)]
    description: String,
    #[serde(rename = "inputSchema", default = "default_input_schema")]
    input_schema: Value,
}

fn default_input_schema() -> Value {
    json!({"type": "object", "properties": {}})
}

#[derive(Debug, Deserialize)]
struct McpToolsListResult {
    tools: Vec<McpToolDef>,
}

#[derive(Debug, Deserialize)]
struct McpToolsCallResult {
    #[serde(default)]
    content: Vec<McpContent>,
    #[serde(default, rename = "isError")]
    is_error: bool,
}

/// MCP content block on a `tools/call` response.
///
/// Text variants flow into `ToolOutcome.output`'s `content` string;
/// Image variants are surfaced as `UserAttachment::Image` on
/// `ToolOutcome.attachments` so that providers which support vision
/// in the tool-result slot (Anthropic) can present them to the model.
/// Providers that don't (OpenAI) degrade them to a text placeholder
/// in `chat_message_to_wire`.
///
/// `resource` / other variants we don't yet model are collapsed into
/// bracketed placeholder text — they're rare in practice and the
/// shape varies enough to warrant a dedicated pass when we add them.
#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
enum McpContent {
    #[serde(rename = "text")]
    Text { text: String },
    #[serde(rename = "image")]
    Image {
        #[serde(default, rename = "mimeType")]
        mime_type: String,
        #[serde(default)]
        data: String,
    },
    #[serde(other)]
    Other,
}

// ── McpClient ───────────────────────────────────────────────────────

/// One client per MCP server. Stateless on the wire except for the
/// optional `Mcp-Session-Id` header (HTTP transport only) — see
/// module docstring.
pub struct McpClient {
    name: String,
    timeout: Duration,
    next_id: AtomicU64,
    inner: McpClientInner,
}

enum McpClientInner {
    Http(HttpInner),
    Stdio(StdioInner),
}

struct HttpInner {
    http: reqwest::Client,
    url: String,
    /// Static headers sent on every request (e.g. `Authorization`).
    headers: HashMap<String, String>,
    session_id: Arc<RwLock<Option<String>>>,
}

/// Stdio inner — owns the child process via the writer/reader task
/// pair. `request_tx` is the only way to talk to the child; closing
/// it tears down the pair on Drop.
struct StdioInner {
    request_tx: tokio::sync::mpsc::Sender<StdioRequest>,
    pending_kill: Option<Arc<std::sync::Mutex<Option<tokio::process::Child>>>>,
}

enum StdioRequest {
    /// JSON-RPC request that expects a response. Reply lands on `reply`
    /// via the reader task's id-routing map.
    Call {
        id: u64,
        body: String,
        reply: tokio::sync::oneshot::Sender<Result<Value, McpError>>,
    },
    /// JSON-RPC notification — write + forget.
    Notify { body: String },
}

type PendingReplies =
    Arc<std::sync::Mutex<HashMap<u64, tokio::sync::oneshot::Sender<Result<Value, McpError>>>>>;

impl McpClient {
    pub fn new(config: McpServerConfig) -> Result<Self, McpError> {
        let name = config.name.clone();
        let timeout = config.timeout;
        let inner = match config.transport {
            McpTransport::Http { url, headers } => {
                let http = reqwest::Client::builder()
                    .timeout(timeout)
                    .build()
                    .map_err(|e| McpError::Transport(e.to_string()))?;
                McpClientInner::Http(HttpInner {
                    http,
                    url,
                    headers,
                    session_id: Arc::new(RwLock::new(None)),
                })
            }
            McpTransport::Stdio {
                command,
                args,
                env,
                working_dir,
            } => spawn_stdio(&name, command, args, env, working_dir)?,
        };
        Ok(Self {
            name,
            timeout,
            next_id: AtomicU64::new(1),
            inner,
        })
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    /// Run the spec-required initialization handshake. Captures the
    /// session id (if any) and notifies the server that we're ready.
    pub async fn initialize(&self) -> Result<(), McpError> {
        let params = json!({
            "protocolVersion": MCP_PROTOCOL_VERSION,
            "capabilities": {},
            "clientInfo": {
                "name": "agentmatrix-runtime-driver",
                "version": env!("CARGO_PKG_VERSION"),
            }
        });
        let _ = self.call("initialize", Some(params)).await?;
        // notifications/initialized has no `id` and expects no response —
        // we fire-and-forget. Errors get logged but don't fail boot.
        if let Err(e) = self.notify("notifications/initialized", None).await {
            tracing::warn!(
                target: "harness::mcp",
                server = %self.name,
                error = %e,
                "notifications/initialized fire-and-forget failed; continuing"
            );
        }
        Ok(())
    }

    /// Pull the server's tool advertisement. Each `ToolSpec` returned
    /// has the **unprefixed** name; `McpToolRuntime::discover` prefixes
    /// with `{server_name}__` to avoid collisions.
    pub async fn tools_list(&self) -> Result<Vec<ToolSpec>, McpError> {
        let value = self.call("tools/list", None).await?;
        let result: McpToolsListResult = serde_json::from_value(value)
            .map_err(|e| McpError::Decode(format!("tools/list result: {e}")))?;
        Ok(result
            .tools
            .into_iter()
            .map(|t| ToolSpec {
                name: t.name,
                description: t.description,
                input_schema: t.input_schema,
            })
            .collect())
    }

    /// Invoke an MCP tool. `name` is the **unprefixed** MCP-side tool
    /// name (caller has already stripped the `{server}__` prefix).
    /// Returns a `ToolOutcome` shaped the same way `SandboxToolRuntime`
    /// returns its outcomes — the harness layer doesn't care which
    /// runtime served the call.
    pub async fn tools_call(&self, name: &str, arguments: Value) -> Result<ToolOutcome, McpError> {
        let params = json!({
            "name": name,
            "arguments": arguments,
        });
        let value = self.call("tools/call", Some(params)).await?;
        let result: McpToolsCallResult = serde_json::from_value(value)
            .map_err(|e| McpError::Decode(format!("tools/call result: {e}")))?;

        // Split MCP content blocks into a text channel (goes into
        // the tool_result content string) and an image channel
        // (goes into `attachments`, preserved for the next
        // user-turn projection on vision-capable providers).
        let mut text_parts: Vec<String> = Vec::new();
        let mut attachments: Vec<UserAttachment> = Vec::new();
        for c in result.content {
            match c {
                McpContent::Text { text } => text_parts.push(text),
                McpContent::Image { mime_type, data } => {
                    if data.is_empty() {
                        // Server sent an image block but no payload —
                        // surface a placeholder so the model still
                        // knows something visual was returned.
                        text_parts.push(format!("[image {mime_type} returned with empty data]"));
                    } else {
                        attachments.push(UserAttachment::Image(ImageSource {
                            media_type: if mime_type.is_empty() {
                                "image/png".to_string()
                            } else {
                                mime_type
                            },
                            data: ImageData::Base64(data),
                        }));
                    }
                }
                McpContent::Other => {
                    text_parts.push("[non-text MCP content elided]".into());
                }
            }
        }
        let content_str = text_parts.join("\n");

        let output = if result.is_error {
            Err(ToolFailure::new(
                ToolFailureKind::Runtime,
                if content_str.is_empty() {
                    format!("MCP tool {name} reported error")
                } else {
                    format!("MCP tool {name} error: {content_str}")
                },
            ))
        } else {
            // Wrap text content in a JSON object so it slots into the
            // same `tool_result.content` shape native tools produce.
            Ok(json!({"content": content_str}))
        };
        Ok(ToolOutcome {
            output,
            attachments,
        })
    }

    /// Internal: send a request, parse the JSON-RPC envelope, return
    /// the `result` field (or the `error` mapped to `McpError::Server`).
    async fn call(&self, method: &str, params: Option<Value>) -> Result<Value, McpError> {
        let id = self.next_id.fetch_add(1, Ordering::SeqCst);
        let body = McpRequest {
            jsonrpc: "2.0",
            id,
            method,
            params,
        };
        match &self.inner {
            McpClientInner::Http(http) => self.call_http(http, id, &body).await,
            McpClientInner::Stdio(stdio) => self.call_stdio(stdio, id, &body).await,
        }
    }

    /// Fire-and-forget notification (JSON-RPC has no `id`, no response).
    async fn notify(&self, method: &str, params: Option<Value>) -> Result<(), McpError> {
        let body = McpNotification {
            jsonrpc: "2.0",
            method,
            params,
        };
        match &self.inner {
            McpClientInner::Http(http) => self.notify_http(http, &body).await,
            McpClientInner::Stdio(stdio) => self.notify_stdio(stdio, &body).await,
        }
    }

    async fn call_http(
        &self,
        http: &HttpInner,
        id: u64,
        body: &McpRequest<'_>,
    ) -> Result<Value, McpError> {
        // Advertise both response shapes so servers can pick. MCP SDK's
        // default streamable-HTTP transport will reply with
        // `text/event-stream` if it sees this header — we MUST then
        // parse SSE rather than `resp.text()` (which would block until
        // the stream is fully drained but, more importantly, the bytes
        // we get back are SSE framing, not JSON).
        let mut req = http
            .http
            .post(&http.url)
            .header("Accept", "application/json, text/event-stream")
            .json(body);
        // Inject static headers (e.g. Authorization) configured in bootstrap.
        for (k, v) in &http.headers {
            req = req.header(k.as_str(), v.as_str());
        }
        if let Some(sid) = cached_session_id(&http.session_id) {
            req = req.header("Mcp-Session-Id", sid);
        }
        let resp = req.send().await.map_err(|e| {
            if e.is_timeout() {
                McpError::Timeout(e.to_string())
            } else {
                McpError::Transport(e.to_string())
            }
        })?;

        // Server may issue a session id on initialize; capture the first
        // non-empty one we see. Subsequent calls replay it.
        if let Some(sid) = resp
            .headers()
            .get("mcp-session-id")
            .and_then(|v| v.to_str().ok())
        {
            if !sid.is_empty() {
                if let Ok(mut guard) = http.session_id.write() {
                    if guard.is_none() {
                        *guard = Some(sid.to_string());
                    }
                }
            }
        }

        let status = resp.status();
        if !status.is_success() {
            let body_text = resp.text().await.unwrap_or_default();
            return Err(McpError::Http {
                status: status.as_u16(),
                body: body_text.chars().take(512).collect(),
            });
        }

        // Branch on Content-Type. SSE responses can carry multiple
        // `message` events (server-side notifications + our response);
        // we drain them all and pick the JSON-RPC envelope whose `id`
        // matches what we sent.
        let content_type = resp
            .headers()
            .get(reqwest::header::CONTENT_TYPE)
            .and_then(|v| v.to_str().ok())
            .map(|s| s.to_ascii_lowercase())
            .unwrap_or_default();

        if content_type.starts_with("text/event-stream") {
            parse_mcp_sse_response(resp, id, &self.name).await
        } else {
            let body_text = resp.text().await.unwrap_or_default();
            let parsed: McpResponse = serde_json::from_str(&body_text)
                .map_err(|e| McpError::Decode(format!("response body: {e}; raw={body_text}")))?;
            if let Some(err) = parsed.error {
                return Err(McpError::Server {
                    code: err.code,
                    message: err.message,
                });
            }
            parsed.result.ok_or(McpError::MissingField("result"))
        }
    }

    async fn notify_http(
        &self,
        http: &HttpInner,
        body: &McpNotification<'_>,
    ) -> Result<(), McpError> {
        // Notifications still advertise SSE; some servers reply with
        // `202 Accepted` + an SSE stream that carries no envelope (it
        // was just an ACK). We send + drop the response body.
        let mut req = http
            .http
            .post(&http.url)
            .header("Accept", "application/json, text/event-stream")
            .json(body);
        for (k, v) in &http.headers {
            req = req.header(k.as_str(), v.as_str());
        }
        if let Some(sid) = cached_session_id(&http.session_id) {
            req = req.header("Mcp-Session-Id", sid);
        }
        req.send()
            .await
            .map_err(|e| McpError::Transport(e.to_string()))?;
        Ok(())
    }

    async fn call_stdio(
        &self,
        stdio: &StdioInner,
        id: u64,
        body: &McpRequest<'_>,
    ) -> Result<Value, McpError> {
        let line = serde_json::to_string(body)
            .map_err(|e| McpError::Decode(format!("encode request: {e}")))?;
        let (reply_tx, reply_rx) = tokio::sync::oneshot::channel();
        stdio
            .request_tx
            .send(StdioRequest::Call {
                id,
                body: line,
                reply: reply_tx,
            })
            .await
            .map_err(|_| McpError::Transport("stdio worker gone".into()))?;
        match tokio::time::timeout(self.timeout, reply_rx).await {
            Ok(Ok(result)) => result,
            Ok(Err(_)) => Err(McpError::Transport(
                "stdio reply channel closed before response".into(),
            )),
            Err(_) => Err(McpError::Timeout(format!(
                "stdio request timed out after {:?}",
                self.timeout
            ))),
        }
    }

    async fn notify_stdio(
        &self,
        stdio: &StdioInner,
        body: &McpNotification<'_>,
    ) -> Result<(), McpError> {
        let line = serde_json::to_string(body)
            .map_err(|e| McpError::Decode(format!("encode notification: {e}")))?;
        stdio
            .request_tx
            .send(StdioRequest::Notify { body: line })
            .await
            .map_err(|_| McpError::Transport("stdio worker gone".into()))?;
        Ok(())
    }
}

fn cached_session_id(slot: &Arc<RwLock<Option<String>>>) -> Option<String> {
    slot.read().ok().and_then(|g| g.clone())
}

/// Drain a `text/event-stream` response from an MCP server and return
/// the JSON-RPC envelope whose `id` matches `expected_id`. Server-side
/// notifications (no `id`) and unrelated responses (other `id`s) are
/// logged at debug + discarded — the streamable-HTTP transport may
/// interleave them with our response.
///
/// Fails if the stream ends without an id-matching envelope, or with an
/// `error` field on the matched envelope.
async fn parse_mcp_sse_response(
    resp: reqwest::Response,
    expected_id: u64,
    server_name: &str,
) -> Result<Value, McpError> {
    use eventsource_stream::Eventsource;
    use futures::StreamExt;

    let mut events = resp.bytes_stream().eventsource();
    while let Some(ev) = events.next().await {
        let ev = ev.map_err(|e| McpError::Transport(format!("SSE transport error: {e}")))?;
        // MCP streamable-HTTP uses default event name (`message`) for
        // JSON-RPC envelopes. We accept either an empty event name or
        // explicit `message`; anything else (ping, retry, custom) is
        // ignored.
        if !ev.event.is_empty() && ev.event != "message" {
            tracing::debug!(
                target: "harness::mcp",
                server = %server_name,
                event = %ev.event,
                "ignoring non-message SSE event"
            );
            continue;
        }
        let trimmed = ev.data.trim();
        if trimmed.is_empty() {
            continue;
        }
        let parsed: McpResponse = match serde_json::from_str(trimmed) {
            Ok(v) => v,
            Err(e) => {
                tracing::warn!(
                    target: "harness::mcp",
                    server = %server_name,
                    error = %e,
                    "SSE event body is not a JSON-RPC envelope; skipping"
                );
                continue;
            }
        };
        // Server-initiated notification (no id): we don't implement
        // sampling / elicitation yet, so drop and keep draining.
        let Some(rid) = parsed.id else {
            tracing::debug!(
                target: "harness::mcp",
                server = %server_name,
                "ignoring server-initiated notification mid-SSE stream"
            );
            continue;
        };
        if rid != expected_id {
            // Stale / unrelated reply (shouldn't really happen on a
            // request-scoped POST stream, but defend anyway).
            tracing::debug!(
                target: "harness::mcp",
                server = %server_name,
                rid,
                expected_id,
                "ignoring SSE response with mismatched id"
            );
            continue;
        }
        if let Some(err) = parsed.error {
            return Err(McpError::Server {
                code: err.code,
                message: err.message,
            });
        }
        return parsed.result.ok_or(McpError::MissingField("result"));
    }
    Err(McpError::Transport(format!(
        "SSE stream closed without a JSON-RPC response matching id={expected_id}"
    )))
}

/// Drop wires teardown for stdio inners: signal the worker to stop +
/// best-effort kill on the child if it's still alive.
impl Drop for McpClient {
    fn drop(&mut self) {
        if let McpClientInner::Stdio(stdio) = &mut self.inner {
            // Closing the sender lets the writer task observe EOF and
            // exit, which closes the child's stdin; most well-behaved
            // MCP servers exit on EOF. As a safety net, kill the child
            // explicitly so a misbehaved server can't leak processes.
            if let Some(child_slot) = stdio.pending_kill.take() {
                if let Ok(mut guard) = child_slot.lock() {
                    if let Some(mut child) = guard.take() {
                        let _ = child.start_kill();
                    }
                }
            }
        }
    }
}

// ── stdio transport ─────────────────────────────────────────────────

/// Spawn the configured MCP server as a child process, wire up a
/// writer task (drains an mpsc of outbound requests/notifications to
/// the child's stdin) and a reader task (parses newline-delimited
/// JSON-RPC messages from the child's stdout, routes responses to
/// their `oneshot` waiters by `id`). Stderr is drained into RD
/// tracing under `target = "harness::mcp::stdio"`.
///
/// Returns an `McpClientInner::Stdio` ready for `call`/`notify`.
fn spawn_stdio(
    name: &str,
    command: String,
    args: Vec<String>,
    env: HashMap<String, String>,
    working_dir: Option<String>,
) -> Result<McpClientInner, McpError> {
    use std::process::Stdio;
    use tokio::io::{AsyncBufReadExt, BufReader};
    use tokio::process::Command;

    let mut cmd = Command::new(&command);
    cmd.args(&args)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        // env_clear FIRST so nothing host-side leaks in. The MCP server
        // sees exactly what bootstrap.yaml said and nothing more.
        .env_clear();
    for (k, v) in &env {
        cmd.env(k, v);
    }
    // Keep the child env sealed, but provide PATH by default so common
    // stdio launchers like `npx` can resolve their own subprocesses.
    if !env.contains_key("PATH") {
        if let Some(path) = std::env::var_os("PATH") {
            cmd.env("PATH", path);
        }
    }
    if let Some(dir) = working_dir.as_deref() {
        cmd.current_dir(dir);
    }
    cmd.kill_on_drop(true);

    let mut child = cmd
        .spawn()
        .map_err(|e| McpError::Transport(format!("stdio spawn {command:?}: {e}")))?;

    let stdin = child
        .stdin
        .take()
        .ok_or_else(|| McpError::Transport("stdio child has no stdin".into()))?;
    let stdout = child
        .stdout
        .take()
        .ok_or_else(|| McpError::Transport("stdio child has no stdout".into()))?;
    let stderr = child.stderr.take();

    // Shared map from outbound request id → oneshot reply slot. The
    // writer task installs entries; the reader task pops them.
    let pending: PendingReplies = Arc::new(std::sync::Mutex::new(HashMap::new()));

    // Writer task: drain outbound mpsc → child stdin.
    let (request_tx, mut request_rx) = tokio::sync::mpsc::channel::<StdioRequest>(32);
    {
        let pending = pending.clone();
        let server_name = name.to_string();
        tokio::spawn(async move {
            let mut stdin = stdin;
            while let Some(req) = request_rx.recv().await {
                match req {
                    StdioRequest::Call { id, body, reply } => {
                        // Register pending FIRST so a quick reply can't
                        // race the writer.
                        if let Ok(mut guard) = pending.lock() {
                            guard.insert(id, reply);
                        }
                        if let Err(e) = write_stdio_line(&mut stdin, &body).await {
                            // Pop the waiter we just registered + return
                            // the error so the caller doesn't hang.
                            if let Ok(mut guard) = pending.lock() {
                                if let Some(slot) = guard.remove(&id) {
                                    let _ = slot.send(Err(McpError::Transport(format!(
                                        "stdio write failed: {e}"
                                    ))));
                                }
                            }
                            tracing::warn!(
                                target: "harness::mcp::stdio",
                                server = %server_name,
                                error = %e,
                                "stdio writer terminated"
                            );
                            break;
                        }
                    }
                    StdioRequest::Notify { body } => {
                        if let Err(e) = write_stdio_line(&mut stdin, &body).await {
                            tracing::warn!(
                                target: "harness::mcp::stdio",
                                server = %server_name,
                                error = %e,
                                "stdio writer terminated during notify"
                            );
                            break;
                        }
                    }
                }
            }
            // Channel closed → drop stdin so child sees EOF and exits.
            // (Explicit drop for documentation; happens implicitly too.)
            drop(stdin);
        });
    }

    // Reader task: parse newline-delimited JSON-RPC → route by id.
    {
        let pending = pending.clone();
        let server_name = name.to_string();
        tokio::spawn(async move {
            let mut reader = BufReader::new(stdout);
            let mut line = String::new();
            loop {
                line.clear();
                match reader.read_line(&mut line).await {
                    Ok(0) => {
                        // EOF — server closed stdout. Fail all pending.
                        if let Ok(mut guard) = pending.lock() {
                            for (_, slot) in guard.drain() {
                                let _ = slot.send(Err(McpError::Transport(
                                    "stdio server closed stdout".into(),
                                )));
                            }
                        }
                        break;
                    }
                    Ok(_) => {
                        let trimmed = line.trim();
                        if trimmed.is_empty() {
                            continue;
                        }
                        let parsed: McpResponse = match serde_json::from_str(trimmed) {
                            Ok(v) => v,
                            Err(e) => {
                                tracing::warn!(
                                    target: "harness::mcp::stdio",
                                    server = %server_name,
                                    error = %e,
                                    line = %trimmed.chars().take(256).collect::<String>(),
                                    "stdio reader could not parse JSON-RPC envelope"
                                );
                                continue;
                            }
                        };
                        // We only care about responses (have `id`).
                        // Server-initiated notifications without `id`
                        // are ignored — we don't implement sampling /
                        // elicitation yet.
                        let Some(id) = parsed.id else {
                            tracing::debug!(
                                target: "harness::mcp::stdio",
                                server = %server_name,
                                "ignoring server-initiated notification"
                            );
                            continue;
                        };
                        let slot = if let Ok(mut guard) = pending.lock() {
                            guard.remove(&id)
                        } else {
                            None
                        };
                        if let Some(slot) = slot {
                            let result = if let Some(err) = parsed.error {
                                Err(McpError::Server {
                                    code: err.code,
                                    message: err.message,
                                })
                            } else {
                                parsed.result.ok_or(McpError::MissingField("result"))
                            };
                            let _ = slot.send(result);
                        } else {
                            tracing::debug!(
                                target: "harness::mcp::stdio",
                                server = %server_name,
                                id,
                                "stdio reply for unknown id (timeout already fired?)"
                            );
                        }
                    }
                    Err(e) => {
                        tracing::warn!(
                            target: "harness::mcp::stdio",
                            server = %server_name,
                            error = %e,
                            "stdio reader I/O error"
                        );
                        break;
                    }
                }
            }
        });
    }

    // Stderr drain — best-effort tracing. Don't wait on this task; if
    // the child's stderr is huge we still let the reader/writer
    // dominate scheduling.
    if let Some(stderr) = stderr {
        let server_name = name.to_string();
        tokio::spawn(async move {
            let mut reader = BufReader::new(stderr);
            let mut line = String::new();
            loop {
                line.clear();
                match reader.read_line(&mut line).await {
                    Ok(0) | Err(_) => break,
                    Ok(_) => {
                        let trimmed = line.trim_end();
                        if !trimmed.is_empty() {
                            tracing::debug!(
                                target: "harness::mcp::stdio",
                                server = %server_name,
                                stderr = %trimmed,
                            );
                        }
                    }
                }
            }
        });
    }

    // Park the child so Drop on McpClient can kill it. We DON'T
    // .await it — exit code is best-effort observed via the reader's
    // EOF detection.
    let child_slot = Arc::new(std::sync::Mutex::new(Some(child)));

    Ok(McpClientInner::Stdio(StdioInner {
        request_tx,
        pending_kill: Some(child_slot),
    }))
}

async fn write_stdio_line<W: tokio::io::AsyncWrite + Unpin>(
    stdin: &mut W,
    body: &str,
) -> std::io::Result<()> {
    use tokio::io::AsyncWriteExt;
    stdin.write_all(body.as_bytes()).await?;
    stdin.write_all(b"\n").await?;
    stdin.flush().await
}

// ── McpToolRuntime ──────────────────────────────────────────────────

/// ToolRuntime that fronts one or more MCP servers. Built once at
/// session boot via `discover()`. Cheap to clone (Arc internally).
#[derive(Clone)]
pub struct McpToolRuntime {
    inner: Arc<McpToolRuntimeInner>,
}

struct McpToolRuntimeInner {
    clients: Vec<McpClient>,
    specs: Vec<ToolSpec>,
    tool_to_client: HashMap<String, usize>,
}

impl McpToolRuntime {
    /// Tool-name prefix separator between the server name and the
    /// upstream tool name. Chosen as `__` because it's invalid in most
    /// MCP server names already and unlikely to collide. The agent loop
    /// receives the prefixed name; `tools_call` strips it before
    /// forwarding to the server.
    pub const NAME_SEPARATOR: &'static str = "__";

    /// Connect to every configured server, handshake, list tools.
    /// Unreachable / misbehaving servers log a warning and are skipped —
    /// a single down dependency shouldn't fail the whole session.
    /// Returns the runtime even if 0 servers came up (specs() will just
    /// be empty); caller decides whether that's acceptable.
    pub async fn discover(servers: Vec<McpServerConfig>) -> Self {
        let mut clients: Vec<McpClient> = Vec::with_capacity(servers.len());
        let mut specs: Vec<ToolSpec> = Vec::new();
        let mut tool_to_client: HashMap<String, usize> = HashMap::new();

        for config in servers {
            let server_name = config.name.clone();
            let required = config.required;
            // Extract enabled_tools before config is consumed by McpClient::new.
            let enabled_tools: std::collections::HashSet<String> =
                config.enabled_tools.iter().cloned().collect();
            let client = match McpClient::new(config) {
                Ok(c) => c,
                Err(e) => {
                    tracing::warn!(
                        target: "harness::mcp",
                        server = %server_name,
                        error = %e,
                        "McpClient::new failed; skipping server"
                    );
                    continue;
                }
            };
            if let Err(e) = client.initialize().await {
                if required {
                    tracing::error!(
                        target: "harness::mcp",
                        server = %server_name,
                        error = %e,
                        "required MCP server failed to initialize; session boot will fail"
                    );
                    // `discover` returns a partial runtime; caller checks
                    // server_count() and aborts if required server is absent.
                    // Flag the absence via a sentinel rather than panicking.
                } else {
                    tracing::warn!(
                        target: "harness::mcp",
                        server = %server_name,
                        error = %e,
                        "MCP initialize failed; skipping server"
                    );
                }
                continue;
            }
            let server_specs = match client.tools_list().await {
                Ok(s) => s,
                Err(e) => {
                    tracing::warn!(
                        target: "harness::mcp",
                        server = %server_name,
                        error = %e,
                        "MCP tools/list failed; skipping server"
                    );
                    continue;
                }
            };
            let client_idx = clients.len();
            for mut spec in server_specs {
                let original = spec.name.clone();
                // Apply enabled_tools allowlist: skip tools not in the list
                // (empty set = all tools allowed).
                if !enabled_tools.is_empty() && !enabled_tools.contains(&original) {
                    continue;
                }
                // Prefix server name onto the tool to avoid collisions
                // when multiple MCP servers offer same-named tools.
                spec.name = format!("{server_name}{}{original}", Self::NAME_SEPARATOR);
                if tool_to_client.contains_key(&spec.name) {
                    // Two servers with the same name → operator misconfigured.
                    tracing::warn!(
                        target: "harness::mcp",
                        tool = %spec.name,
                        "duplicate MCP tool name after prefixing; later registration wins"
                    );
                }
                tool_to_client.insert(spec.name.clone(), client_idx);
                specs.push(spec);
            }
            clients.push(client);
        }

        Self {
            inner: Arc::new(McpToolRuntimeInner {
                clients,
                specs,
                tool_to_client,
            }),
        }
    }

    /// Number of MCP servers that successfully came up (initialize + tools/list ok).
    pub fn server_count(&self) -> usize {
        self.inner.clients.len()
    }
}

#[async_trait]
impl ToolRuntime for McpToolRuntime {
    fn specs(&self) -> Vec<ToolSpec> {
        self.inner.specs.clone()
    }

    async fn invoke(&self, invocation: ToolInvocation) -> Result<ToolOutcome, ToolRuntimeError> {
        let Some(&idx) = self.inner.tool_to_client.get(&invocation.name) else {
            return Err(ToolRuntimeError::UnknownTool(invocation.name));
        };
        let client = &self.inner.clients[idx];
        // Strip the `{server}__` prefix before sending to the MCP server.
        let original_name = invocation
            .name
            .split_once(Self::NAME_SEPARATOR)
            .map(|(_, name)| name)
            .unwrap_or(invocation.name.as_str())
            .to_string();
        client
            .tools_call(&original_name, invocation.input)
            .await
            .map_err(mcp_error_to_tool_runtime_error)
    }
}

fn mcp_error_to_tool_runtime_error(err: McpError) -> ToolRuntimeError {
    if matches!(err, McpError::Timeout(_)) {
        return ToolRuntimeError::Timeout(format!("MCP: {err}"));
    }
    // MCP errors are mostly transport-level / protocol-level — bucket
    // them all as `Runtime` (the catch-all). Caller (agent_loop) will
    // surface this as `NativeHarnessError::ToolRuntime` → eventually
    // `sandbox_failed_error` on the wire, which conveys "MCP side
    // broke" close enough. We don't add Timeout etc explicitly because
    // reqwest's timeout already surfaces as Transport.
    ToolRuntimeError::Runtime(format!("MCP: {err}"))
}

// ── CompositeToolRuntime ────────────────────────────────────────────

/// Layer two ToolRuntimes into one. Used to combine native tools
/// (`SandboxToolRuntime`) and MCP tools (`McpToolRuntime`) into a
/// single thing the agent loop's generic `R: ToolRuntime` can consume.
///
/// Dispatch policy: try `primary` first; if it reports `UnknownTool`,
/// fall back to `secondary`. Native tools live in primary by convention
/// (cheaper to call, no network hop).
#[derive(Clone)]
pub struct CompositeToolRuntime {
    primary: Arc<dyn ToolRuntime>,
    secondary: Arc<dyn ToolRuntime>,
}

impl CompositeToolRuntime {
    pub fn new(primary: Arc<dyn ToolRuntime>, secondary: Arc<dyn ToolRuntime>) -> Self {
        Self { primary, secondary }
    }
}

#[async_trait]
impl ToolRuntime for CompositeToolRuntime {
    fn specs(&self) -> Vec<ToolSpec> {
        let mut combined = self.primary.specs();
        combined.extend(self.secondary.specs());
        combined
    }

    async fn invoke(&self, invocation: ToolInvocation) -> Result<ToolOutcome, ToolRuntimeError> {
        match self.primary.invoke(invocation.clone()).await {
            Err(ToolRuntimeError::UnknownTool(_)) => self.secondary.invoke(invocation).await,
            other => other,
        }
    }

    async fn invoke_cancellable(
        &self,
        invocation: ToolInvocation,
        cancel: Option<&tokio_util::sync::CancellationToken>,
    ) -> Result<ToolOutcome, ToolRuntimeError> {
        match self
            .primary
            .invoke_cancellable(invocation.clone(), cancel)
            .await
        {
            Err(ToolRuntimeError::UnknownTool(_)) => {
                self.secondary.invoke_cancellable(invocation, cancel).await
            }
            other => other,
        }
    }
}

// Blanket impl so `Arc<dyn ToolRuntime>` itself satisfies ToolRuntime —
// lets the agent loop's `R: ToolRuntime + Clone` accept an Arc of a
// trait object directly. Forwards to the inner concrete impl.
#[async_trait]
impl ToolRuntime for Arc<dyn ToolRuntime> {
    fn specs(&self) -> Vec<ToolSpec> {
        (**self).specs()
    }

    async fn invoke(&self, invocation: ToolInvocation) -> Result<ToolOutcome, ToolRuntimeError> {
        (**self).invoke(invocation).await
    }

    async fn invoke_cancellable(
        &self,
        invocation: ToolInvocation,
        cancel: Option<&tokio_util::sync::CancellationToken>,
    ) -> Result<ToolOutcome, ToolRuntimeError> {
        (**self).invoke_cancellable(invocation, cancel).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::TcpListener;

    /// Spin up a one-shot mock HTTP server that responds to MCP requests
    /// with scripted JSON bodies. Returns the URL the client should
    /// POST to. Server task ends after `expected_requests` requests.
    /// Cheaper than wiremock; covers our minimal needs.
    async fn spawn_mock_mcp_server(
        scripted_responses: Vec<String>,
    ) -> (String, tokio::task::JoinHandle<()>) {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let url = format!("http://{addr}/mcp");

        let handle = tokio::spawn(async move {
            let mut remaining = scripted_responses.into_iter();
            while let Some(response_body) = remaining.next() {
                let (mut stream, _) = listener.accept().await.unwrap();
                // Drain request: read until \r\n\r\n then Content-Length
                // bytes. Hand-rolled because we don't need full HTTP parsing.
                let mut buf = Vec::with_capacity(2048);
                let mut header_end = 0;
                loop {
                    let mut tmp = [0u8; 1024];
                    let n = stream.read(&mut tmp).await.unwrap();
                    if n == 0 {
                        break;
                    }
                    buf.extend_from_slice(&tmp[..n]);
                    if let Some(pos) = find_header_end(&buf) {
                        header_end = pos + 4;
                        break;
                    }
                }
                let headers = std::str::from_utf8(&buf[..header_end.saturating_sub(4)])
                    .unwrap()
                    .to_lowercase();
                let mut content_length = 0usize;
                for line in headers.lines() {
                    if let Some(v) = line.strip_prefix("content-length:") {
                        content_length = v.trim().parse().unwrap_or(0);
                    }
                }
                let mut already_read = buf.len() - header_end;
                while already_read < content_length {
                    let mut tmp = [0u8; 1024];
                    let n = stream.read(&mut tmp).await.unwrap();
                    if n == 0 {
                        break;
                    }
                    buf.extend_from_slice(&tmp[..n]);
                    already_read += n;
                }

                // Reply with the scripted JSON body.
                let response = format!(
                    "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
                    response_body.len(),
                    response_body
                );
                stream.write_all(response.as_bytes()).await.unwrap();
                stream.flush().await.unwrap();
                let _ = stream.shutdown().await;
            }
        });
        (url, handle)
    }

    fn find_header_end(buf: &[u8]) -> Option<usize> {
        buf.windows(4).position(|w| w == b"\r\n\r\n")
    }

    /// SSE variant: for each request, the mock replies with a
    /// `text/event-stream` response whose body is each scripted entry
    /// (a list of SSE events) concatenated. Each "entry" is itself a
    /// `Vec<String>` of SSE event blocks (each ending with `\n\n`) so
    /// a single response can carry multiple events — exercising the
    /// "discard server-initiated notifications between expected
    /// envelopes" code path. Connection close terminates the stream.
    async fn spawn_mock_mcp_sse_server(
        scripted_responses: Vec<Vec<String>>,
    ) -> (String, tokio::task::JoinHandle<()>) {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let url = format!("http://{addr}/mcp");

        let handle = tokio::spawn(async move {
            let mut remaining = scripted_responses.into_iter();
            while let Some(events) = remaining.next() {
                let (mut stream, _) = listener.accept().await.unwrap();
                let mut buf = Vec::with_capacity(2048);
                let mut header_end = 0;
                loop {
                    let mut tmp = [0u8; 1024];
                    let n = stream.read(&mut tmp).await.unwrap();
                    if n == 0 {
                        break;
                    }
                    buf.extend_from_slice(&tmp[..n]);
                    if let Some(pos) = find_header_end(&buf) {
                        header_end = pos + 4;
                        break;
                    }
                }
                let headers = std::str::from_utf8(&buf[..header_end.saturating_sub(4)])
                    .unwrap()
                    .to_lowercase();
                let mut content_length = 0usize;
                for line in headers.lines() {
                    if let Some(v) = line.strip_prefix("content-length:") {
                        content_length = v.trim().parse().unwrap_or(0);
                    }
                }
                let mut already_read = buf.len() - header_end;
                while already_read < content_length {
                    let mut tmp = [0u8; 1024];
                    let n = stream.read(&mut tmp).await.unwrap();
                    if n == 0 {
                        break;
                    }
                    buf.extend_from_slice(&tmp[..n]);
                    already_read += n;
                }

                // We send the response WITHOUT Content-Length and with
                // Connection: close — closing the TCP stream signals
                // end-of-stream to the client's SSE parser. Each event
                // is already terminated by its own `\n\n`.
                let body: String = events.concat();
                let header_block = "HTTP/1.1 200 OK\r\n\
                    Content-Type: text/event-stream\r\n\
                    Cache-Control: no-cache\r\n\
                    Connection: close\r\n\r\n";
                stream.write_all(header_block.as_bytes()).await.unwrap();
                stream.write_all(body.as_bytes()).await.unwrap();
                stream.flush().await.unwrap();
                let _ = stream.shutdown().await;
            }
        });
        (url, handle)
    }

    /// Build one SSE `message`-event block from a JSON-RPC body.
    fn sse_event(body: &str) -> String {
        format!("event: message\ndata: {body}\n\n")
    }

    fn jsonrpc_result(id: u64, result: Value) -> String {
        json!({"jsonrpc": "2.0", "id": id, "result": result}).to_string()
    }

    #[tokio::test]
    async fn mcp_client_initializes_lists_and_calls_a_tool() {
        // Scripted responses for: initialize, notifications/initialized
        // (notification gets no response but the test server still
        // returns OK), tools/list, tools/call.
        let (url, _server) = spawn_mock_mcp_server(vec![
            jsonrpc_result(1, json!({"protocolVersion": "2024-11-05", "capabilities": {}})),
            // Notification — server returns empty 200 with empty body
            // is technically OK; we send a benign empty JSON-RPC anyway
            // so the mock parses cleanly.
            json!({}).to_string(),
            jsonrpc_result(
                3,
                json!({
                    "tools": [{
                        "name": "echo",
                        "description": "echo back input",
                        "inputSchema": {"type": "object", "properties": {"text": {"type": "string"}}}
                    }]
                }),
            ),
            jsonrpc_result(
                4,
                json!({
                    "content": [{"type": "text", "text": "hello back"}],
                    "isError": false
                }),
            ),
        ])
        .await;
        let client = McpClient::new(McpServerConfig::new("fs", url)).unwrap();
        client.initialize().await.expect("init");
        let specs = client.tools_list().await.expect("list");
        assert_eq!(specs.len(), 1);
        assert_eq!(specs[0].name, "echo");
        let outcome = client
            .tools_call("echo", json!({"text": "hi"}))
            .await
            .expect("call");
        let v = outcome.output.expect("ok output");
        assert_eq!(v["content"], "hello back");
    }

    #[tokio::test]
    async fn mcp_client_extracts_image_content_into_attachments() {
        // MCP servers that return an `image` content block (e.g. a
        // screenshot tool) should surface the bytes as
        // ToolOutcome.attachments — NOT degrade them to placeholder
        // text. Anthropic projection then puts the image into the
        // tool_result block array; OpenAI projection appends a
        // placeholder note in the tool-role string.
        let (url, _server) = spawn_mock_mcp_server(vec![
            jsonrpc_result(
                1,
                json!({"protocolVersion": "2024-11-05", "capabilities": {}}),
            ),
            json!({}).to_string(),
            jsonrpc_result(
                3,
                json!({
                    "content": [
                        {"type": "text", "text": "captured"},
                        {"type": "image", "mimeType": "image/png", "data": "PNGBYTES"}
                    ],
                    "isError": false
                }),
            ),
        ])
        .await;
        let client = McpClient::new(McpServerConfig::new("screen", url)).unwrap();
        client.initialize().await.unwrap();
        let outcome = client
            .tools_call("screenshot", json!({}))
            .await
            .expect("call");
        let v = outcome.output.expect("ok output");
        // Text channel: only the actual text block — image isn't
        // smuggled in as a stringified payload.
        assert_eq!(v["content"], "captured");
        // Attachment channel: image bytes preserved verbatim.
        assert_eq!(outcome.attachments.len(), 1);
        let UserAttachment::Image(src) = &outcome.attachments[0];
        assert_eq!(src.media_type, "image/png");
        match &src.data {
            ImageData::Base64(b) => assert_eq!(b, "PNGBYTES"),
            ImageData::Url(_) => panic!("expected base64, got url"),
        }
    }

    #[tokio::test]
    async fn mcp_client_surfaces_tool_error_as_tool_failure() {
        let (url, _server) = spawn_mock_mcp_server(vec![
            jsonrpc_result(
                1,
                json!({"protocolVersion": "2024-11-05", "capabilities": {}}),
            ),
            json!({}).to_string(),
            jsonrpc_result(
                3,
                json!({
                    "content": [{"type": "text", "text": "file not found"}],
                    "isError": true
                }),
            ),
        ])
        .await;
        let client = McpClient::new(McpServerConfig::new("fs", url)).unwrap();
        client.initialize().await.unwrap();
        let outcome = client
            .tools_call("read", json!({"path": "/none"}))
            .await
            .unwrap();
        let failure = outcome.output.expect_err("expected ToolFailure");
        assert_eq!(failure.kind, ToolFailureKind::Runtime);
        assert!(failure.message.contains("file not found"));
    }

    #[tokio::test]
    async fn mcp_client_surfaces_jsonrpc_error_as_mcp_server_error() {
        let (url, _server) = spawn_mock_mcp_server(vec![
            jsonrpc_result(
                1,
                json!({"protocolVersion": "2024-11-05", "capabilities": {}}),
            ),
            json!({}).to_string(),
            json!({
                "jsonrpc": "2.0",
                "id": 3,
                "error": {"code": -32601, "message": "method not found"}
            })
            .to_string(),
        ])
        .await;
        let client = McpClient::new(McpServerConfig::new("fs", url)).unwrap();
        client.initialize().await.unwrap();
        let err = client.tools_list().await.unwrap_err();
        match err {
            McpError::Server { code, message } => {
                assert_eq!(code, -32601);
                assert!(message.contains("method not found"));
            }
            other => panic!("expected Server error, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn mcp_tool_runtime_prefixes_tool_names_and_routes_calls() {
        let (url, _server) = spawn_mock_mcp_server(vec![
            jsonrpc_result(
                1,
                json!({"protocolVersion": "2024-11-05", "capabilities": {}}),
            ),
            json!({}).to_string(),
            jsonrpc_result(
                3,
                json!({
                    "tools": [{
                        "name": "echo",
                        "description": "echo",
                        "inputSchema": {"type": "object"}
                    }]
                }),
            ),
            jsonrpc_result(
                4,
                json!({"content": [{"type": "text", "text": "routed"}], "isError": false}),
            ),
        ])
        .await;
        let rt = McpToolRuntime::discover(vec![McpServerConfig::new("fs", url)]).await;
        assert_eq!(rt.server_count(), 1);
        let specs = rt.specs();
        // Tool name carries the `fs__` prefix.
        assert_eq!(specs.len(), 1);
        assert_eq!(specs[0].name, "fs__echo");

        // invoke with prefixed name — runtime strips prefix on the wire.
        let outcome = rt
            .invoke(ToolInvocation {
                id: "tc1".into(),
                name: "fs__echo".into(),
                input: json!({"text": "x"}),
            })
            .await
            .unwrap();
        assert_eq!(outcome.output.unwrap()["content"], "routed");
    }

    #[tokio::test]
    async fn mcp_tool_runtime_unknown_tool_returns_runtime_error() {
        // No server configured — runtime is empty. invoke on any tool
        // surfaces UnknownTool, which is the contract used by
        // CompositeToolRuntime's fallback logic.
        let rt = McpToolRuntime::discover(vec![]).await;
        let err = rt
            .invoke(ToolInvocation {
                id: "tc".into(),
                name: "nope__whatever".into(),
                input: json!({}),
            })
            .await
            .unwrap_err();
        assert!(matches!(err, ToolRuntimeError::UnknownTool(ref s) if s == "nope__whatever"));
    }

    #[derive(Clone, Default)]
    struct FakeNativeRuntime {
        names: Vec<&'static str>,
    }

    #[async_trait]
    impl ToolRuntime for FakeNativeRuntime {
        fn specs(&self) -> Vec<ToolSpec> {
            self.names
                .iter()
                .map(|n| ToolSpec {
                    name: n.to_string(),
                    description: "fake".into(),
                    input_schema: json!({"type": "object"}),
                })
                .collect()
        }
        async fn invoke(&self, inv: ToolInvocation) -> Result<ToolOutcome, ToolRuntimeError> {
            if self.names.contains(&inv.name.as_str()) {
                Ok(ToolOutcome {
                    output: Ok(json!({"served_by": "native", "name": inv.name})),
                    attachments: vec![],
                })
            } else {
                Err(ToolRuntimeError::UnknownTool(inv.name))
            }
        }
    }

    #[derive(Clone, Default)]
    struct FakeMcpRuntime {
        names: Vec<&'static str>,
    }

    #[async_trait]
    impl ToolRuntime for FakeMcpRuntime {
        fn specs(&self) -> Vec<ToolSpec> {
            self.names
                .iter()
                .map(|n| ToolSpec {
                    name: n.to_string(),
                    description: "mcp".into(),
                    input_schema: json!({"type": "object"}),
                })
                .collect()
        }
        async fn invoke(&self, inv: ToolInvocation) -> Result<ToolOutcome, ToolRuntimeError> {
            if self.names.contains(&inv.name.as_str()) {
                Ok(ToolOutcome {
                    output: Ok(json!({"served_by": "mcp", "name": inv.name})),
                    attachments: vec![],
                })
            } else {
                Err(ToolRuntimeError::UnknownTool(inv.name))
            }
        }
    }

    #[tokio::test]
    async fn composite_runtime_merges_specs_and_falls_back_to_secondary() {
        let native = Arc::new(FakeNativeRuntime {
            names: vec!["bash", "read"],
        }) as Arc<dyn ToolRuntime>;
        let mcp = Arc::new(FakeMcpRuntime {
            names: vec!["fs__list", "git__diff"],
        }) as Arc<dyn ToolRuntime>;
        let composite = CompositeToolRuntime::new(native, mcp);

        // specs union (in primary-then-secondary order)
        let names: Vec<String> = composite.specs().into_iter().map(|s| s.name).collect();
        assert_eq!(names, vec!["bash", "read", "fs__list", "git__diff"]);

        // primary serves "bash"
        let outcome = composite
            .invoke(ToolInvocation {
                id: "tc".into(),
                name: "bash".into(),
                input: json!({}),
            })
            .await
            .unwrap();
        assert_eq!(outcome.output.unwrap()["served_by"], "native");

        // primary returns UnknownTool → fall back to secondary
        let outcome = composite
            .invoke(ToolInvocation {
                id: "tc".into(),
                name: "fs__list".into(),
                input: json!({}),
            })
            .await
            .unwrap();
        assert_eq!(outcome.output.unwrap()["served_by"], "mcp");

        // Neither knows → final UnknownTool
        let err = composite
            .invoke(ToolInvocation {
                id: "tc".into(),
                name: "ghost".into(),
                input: json!({}),
            })
            .await
            .unwrap_err();
        assert!(matches!(err, ToolRuntimeError::UnknownTool(_)));
    }

    /// Build a tiny stdio MCP server in pure shell: stays in a loop
    /// reading newline-delimited JSON-RPC from stdin and writing
    /// pre-canned responses based on the request's `method`. Used to
    /// drive the stdio transport without a real Python/Node MCP
    /// implementation on the test host.
    ///
    /// The script handles:
    /// - `initialize` → returns `{protocolVersion, capabilities:{}}`
    /// - `tools/list` → returns one fake tool `echo`
    /// - `tools/call` → returns text content `"stdio-routed"`
    /// - `notifications/initialized` (no id) → consumed silently
    ///
    /// Returns the absolute path to the temp script file.
    fn write_mock_stdio_server(dir: &std::path::Path) -> std::path::PathBuf {
        let path = dir.join("mock-mcp-stdio.sh");
        // The id needs to be parsed back from the inbound request so we
        // echo it in the response — use sed to extract.
        let body = r#"#!/usr/bin/env bash
set -u
while IFS= read -r line; do
  # Pull out method + id (best-effort sed; the test driver only sends
  # well-formed JSON so we don't need a real parser).
  method=$(echo "$line" | sed -n 's/.*"method"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')
  id=$(echo "$line" | sed -n 's/.*"id"[[:space:]]*:[[:space:]]*\([0-9]*\).*/\1/p')
  case "$method" in
    initialize)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"protocolVersion":"2024-11-05","capabilities":{}}}\n' "$id"
      ;;
    tools/list)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"tools":[{"name":"echo","description":"d","inputSchema":{"type":"object"}}]}}\n' "$id"
      ;;
    tools/call)
      printf '{"jsonrpc":"2.0","id":%s,"result":{"content":[{"type":"text","text":"stdio-routed"}],"isError":false}}\n' "$id"
      ;;
    notifications/initialized)
      # No response for notifications.
      ;;
    *)
      printf '{"jsonrpc":"2.0","id":%s,"error":{"code":-32601,"message":"method not found"}}\n' "$id"
      ;;
  esac
done
"#;
        std::fs::write(&path, body).unwrap();
        // chmod +x via std::fs metadata.
        use std::os::unix::fs::PermissionsExt;
        let mut perms = std::fs::metadata(&path).unwrap().permissions();
        perms.set_mode(0o755);
        std::fs::set_permissions(&path, perms).unwrap();
        path
    }

    #[tokio::test]
    async fn mcp_stdio_initialize_lists_and_calls_a_tool() {
        // End-to-end stdio path: spawn a shell-based MCP server, run
        // initialize → tools/list → tools/call, assert routing works
        // without ever touching HTTP. This is the cheapest way to prove
        // the writer/reader/id-routing wiring is correct.
        // Use the OS temp dir directly — pulls no new dev-dep, fine for
        // a sandboxed CI worker. We pick a per-test filename so parallel
        // test runs don't clobber each other.
        let tmp = std::env::temp_dir().join(format!(
            "rd-mock-mcp-stdio-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&tmp).unwrap();
        let script = write_mock_stdio_server(&tmp);

        let config =
            McpServerConfig::stdio("local-fs", script.to_string_lossy().into_owned(), vec![])
                .with_timeout(Duration::from_secs(5));
        let client = McpClient::new(config).expect("spawn");
        client.initialize().await.expect("init over stdio");
        let specs = client.tools_list().await.expect("list over stdio");
        assert_eq!(specs.len(), 1);
        assert_eq!(specs[0].name, "echo");

        let outcome = client
            .tools_call("echo", json!({"text": "hi"}))
            .await
            .expect("call over stdio");
        let v = outcome.output.expect("ok output");
        assert_eq!(v["content"], "stdio-routed");
    }

    #[tokio::test]
    async fn mcp_stdio_returns_transport_error_when_command_missing() {
        // Spawning a nonexistent command must surface a Transport
        // error eagerly from `McpClient::new`, not hang later on
        // initialize. This is the failure mode operators hit when a
        // bootstrap.yaml references an MCP server whose CLI isn't
        // installed on the RD host.
        let config = McpServerConfig::stdio(
            "nope",
            "/definitely/not/a/real/binary-xyz".to_string(),
            vec![],
        );
        let err = match McpClient::new(config) {
            Ok(_) => panic!("must fail to spawn"),
            Err(e) => e,
        };
        match err {
            McpError::Transport(msg) => {
                assert!(msg.contains("stdio spawn"), "got: {msg}");
            }
            other => panic!("expected Transport, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn mcp_stdio_call_times_out_when_server_doesnt_reply() {
        // `sleep` never reads stdin and never writes stdout, so the
        // reader task can't route anything → call must hit the
        // per-request timeout cleanly (not hang).
        let config = McpServerConfig::stdio("silent", "sleep".to_string(), vec!["30".to_string()])
            .with_timeout(Duration::from_millis(250));
        let client = McpClient::new(config).expect("spawn cat");
        let err = client.initialize().await.expect_err("must time out");
        match err {
            McpError::Timeout(msg) => {
                assert!(msg.contains("timed out"), "got: {msg}");
            }
            other => panic!("expected Timeout, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn mcp_http_sse_response_yields_jsonrpc_result() {
        // Streamable-HTTP transport: server replies with
        // `text/event-stream`. The client must detect Content-Type and
        // drain the SSE stream looking for the id-matching envelope.
        // initialize → notifications/initialized → tools/list →
        // tools/call. Each request gets one SSE message in response
        // (notifications get an empty stream the server will just
        // close after).
        // id sequence: only `call()` calls increment `next_id`;
        // `notify()` (notifications/initialized) does not. So:
        //   initialize → 1, tools/list → 2, tools/call → 3.
        let (url, _server) = spawn_mock_mcp_sse_server(vec![
            // initialize
            vec![sse_event(&jsonrpc_result(
                1,
                json!({"protocolVersion": "2024-11-05", "capabilities": {}}),
            ))],
            // notifications/initialized — no envelope, just close
            vec![],
            // tools/list
            vec![sse_event(&jsonrpc_result(
                2,
                json!({
                    "tools": [{
                        "name": "echo",
                        "description": "d",
                        "inputSchema": {"type": "object"}
                    }]
                }),
            ))],
            // tools/call
            vec![sse_event(&jsonrpc_result(
                3,
                json!({
                    "content": [{"type": "text", "text": "sse-routed"}],
                    "isError": false
                }),
            ))],
        ])
        .await;
        let client = McpClient::new(McpServerConfig::http("sse-fs", url)).expect("build client");
        client.initialize().await.expect("init over sse");
        let specs = client.tools_list().await.expect("list over sse");
        assert_eq!(specs.len(), 1);
        let outcome = client
            .tools_call("echo", json!({}))
            .await
            .expect("call over sse");
        assert_eq!(outcome.output.unwrap()["content"], "sse-routed");
    }

    #[tokio::test]
    async fn mcp_http_sse_response_skips_server_notifications() {
        // SSE stream may interleave server-initiated notifications
        // (no `id`) before the actual response. The parser must drop
        // them and keep draining until it finds the id match.
        let server_notification =
            r#"{"jsonrpc":"2.0","method":"notifications/progress","params":{"percent":42}}"#;
        let unrelated = r#"{"jsonrpc":"2.0","id":999,"result":{"unrelated":true}}"#;
        let (url, _server) = spawn_mock_mcp_sse_server(vec![
            // initialize — single notification + unrelated id + real
            // response. Ordering matters: parser must skip the first
            // two and consume the third.
            vec![
                sse_event(server_notification),
                sse_event(unrelated),
                sse_event(&jsonrpc_result(
                    1,
                    json!({"protocolVersion": "2024-11-05", "capabilities": {}}),
                )),
            ],
            // notifications/initialized
            vec![],
        ])
        .await;
        let client = McpClient::new(McpServerConfig::http("sse-noisy", url)).expect("build client");
        client
            .initialize()
            .await
            .expect("must skip notifications and find id match");
    }

    #[tokio::test]
    async fn mcp_http_sse_response_propagates_jsonrpc_error() {
        // An SSE envelope with `error` field maps to McpError::Server,
        // not Transport — same as the JSON path.
        let (url, _server) = spawn_mock_mcp_sse_server(vec![vec![sse_event(
            &json!({
                "jsonrpc": "2.0",
                "id": 1,
                "error": {"code": -32601, "message": "method not found"}
            })
            .to_string(),
        )]])
        .await;
        let client = McpClient::new(McpServerConfig::http("sse-err", url)).expect("build");
        let err = client.initialize().await.unwrap_err();
        match err {
            McpError::Server { code, message } => {
                assert_eq!(code, -32601);
                assert!(message.contains("method not found"));
            }
            other => panic!("expected Server, got {other:?}"),
        }
    }
}