axon-lang 1.38.1

AXON v1.5.1 — first crates.io publication of the AXON language full-stack runtime. Lexer/parser/type-checker/IR generator (re-exported from axon-frontend) plus the native Rust runtime: typed channels (TypedEventBus with QoS×5, π-calculus mobility, capability extrusion via shield D8 — Fase 13.f.2), Free Monad CPS handlers (Fase 2), lease kernel + reconcile loop (Fase 3+5), Epistemic Security Kernel (ESK Fase 6), Trust Types + ReplayLog (Fase 11.a+11.c), Stateful PEM over WebSocket (Fase 11.d), Ontological Tool Synthesis (Fase 11.e), Mobile Typed Channels (Fase 13). Crate publishes as `axon-lang` to mirror the Python PyPI package; library import remains `use axon::*` so existing call sites keep working unchanged.
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
//! ℰMCP Transducer — Epistemic Model Context Protocol runtime bridge.
//!
//! Implements AXON's ℰMCP (Epistemic MCP) for Ingesta: consuming external MCP
//! servers with epistemic guarantees that plain MCP lacks.
//!
//! Key differentiators from raw MCP:
//!   - **Blame tracking (CT-2/CT-3):** Every MCP call records blame assignment.
//!     If the server returns invalid data → Blame::Server.
//!     If AXON generated bad parameters → Blame::Caller.
//!   - **Epistemic taint:** All data entering via MCP is born as `Uncertainty` (⊥)
//!     in the epistemic lattice. Must be elevated by reasoning before trusted.
//!   - **Effect rows:** MCP tools carry `effects: <network, io, epistemic:speculate>`.
//!   - **Schema validation:** Response validated against output_schema before use.
//!
//! Transport: JSON-RPC 2.0 over HTTP (MCP standard transport).
//! The ℰMCP transducer wraps standard MCP calls with AXON's epistemic envelope.

use serde::{Deserialize, Serialize};
use std::time::Duration;

use crate::tool_executor::ToolResult;
use crate::tool_registry::ToolEntry;

// ── Blame calculus (Findler-Felleisen) ────────────────────────────────────

/// Blame assignment for a tool call failure.
/// Implements the contract-based blame calculus from ℰMCP spec (CT-2/CT-3).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum Blame {
    /// No failure — call succeeded.
    None,
    /// Server violated its contract (bad response, schema mismatch, crash).
    Server,
    /// Caller violated its contract (bad parameters, invalid arguments).
    Caller,
    /// Network/infrastructure failure (timeout, connection refused).
    Network,
}

impl Blame {
    pub fn as_str(&self) -> &'static str {
        match self {
            Blame::None => "none",
            Blame::Server => "server",
            Blame::Caller => "caller",
            Blame::Network => "network",
        }
    }
}

// ── Epistemic taint ──────────────────────────────────────────────────────

/// Epistemic level of data entering via MCP.
/// All MCP-sourced data starts at `Uncertainty` and must be elevated.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum EpistemicTaint {
    /// Data has not been validated — born as ⊥ (Uncertainty).
    Untrusted,
    /// Data passed schema validation but not epistemic reasoning.
    SchemaValidated,
    /// Data elevated by reasoning step (shield or know block).
    Elevated,
}

impl EpistemicTaint {
    pub fn as_str(&self) -> &'static str {
        match self {
            EpistemicTaint::Untrusted => "untrusted",
            EpistemicTaint::SchemaValidated => "schema_validated",
            EpistemicTaint::Elevated => "elevated",
        }
    }
}

// ── MCP call result ──────────────────────────────────────────────────────

/// Result of an ℰMCP tool call — enriched with blame and epistemic metadata.
#[derive(Debug, Clone, Serialize)]
pub struct McpCallResult {
    /// Standard tool result.
    pub tool_name: String,
    pub output: String,
    pub success: bool,
    /// Blame assignment (CT-2/CT-3).
    pub blame: Blame,
    /// Epistemic taint level of the output data.
    pub taint: EpistemicTaint,
    /// MCP server that handled the call.
    pub server: String,
    /// Effect row inferred for this call.
    pub effects: Vec<String>,
}

impl McpCallResult {
    /// Convert to a standard ToolResult (for registry integration).
    pub fn to_tool_result(&self) -> ToolResult {
        ToolResult {
            success: self.success,
            output: self.output.clone(),
            tool_name: self.tool_name.clone(),
        }
    }
}

// ── JSON-RPC 2.0 structures ─────────────────────────────────────────────

/// JSON-RPC 2.0 request (MCP standard transport).
#[derive(Debug, Serialize)]
struct JsonRpcRequest {
    jsonrpc: &'static str,
    method: String,
    params: serde_json::Value,
    id: u64,
}

/// JSON-RPC 2.0 response.
#[derive(Debug, Deserialize)]
struct JsonRpcResponse {
    #[allow(dead_code)]
    jsonrpc: String,
    result: Option<serde_json::Value>,
    error: Option<JsonRpcError>,
    #[allow(dead_code)]
    id: u64,
}

/// JSON-RPC 2.0 error object.
#[derive(Debug, Deserialize)]
struct JsonRpcError {
    code: i64,
    message: String,
}

// ── MCP Client ───────────────────────────────────────────────────────────

/// ℰMCP transducer client — wraps MCP server communication with epistemic
/// guarantees (blame tracking, taint tagging, effect inference).
pub struct McpClient {
    /// MCP server endpoint URL.
    server_url: String,
    /// HTTP timeout.
    timeout: Duration,
    /// Request ID counter.
    next_id: u64,
}

impl McpClient {
    /// Create a new ℰMCP client for the given server.
    pub fn new(server_url: &str, timeout: Duration) -> Self {
        McpClient {
            server_url: server_url.to_string(),
            timeout,
            next_id: 1,
        }
    }

    /// Call a tool on the MCP server with ℰMCP epistemic envelope.
    ///
    /// The call is wrapped with:
    ///   - Blame tracking: server/caller/network fault attribution
    ///   - Taint tagging: output starts as Untrusted, elevated to SchemaValidated if valid
    ///   - Effect inference: all MCP calls carry network + epistemic:speculate effects
    pub fn call_tool(&mut self, tool_name: &str, argument: &str) -> McpCallResult {
        let request_id = self.next_id;
        self.next_id += 1;

        // Build JSON-RPC request for MCP tools/call
        let params = if argument.trim_start().starts_with('{') {
            serde_json::from_str(argument).unwrap_or_else(|_| {
                serde_json::json!({
                    "name": tool_name,
                    "arguments": { "input": argument }
                })
            })
        } else {
            serde_json::json!({
                "name": tool_name,
                "arguments": { "input": argument }
            })
        };

        let rpc_request = JsonRpcRequest {
            jsonrpc: "2.0",
            method: "tools/call".to_string(),
            params,
            id: request_id,
        };

        // Execute the HTTP request to MCP server
        match self.send_rpc(&rpc_request) {
            Ok(response) => self.process_response(tool_name, response),
            Err(e) => McpCallResult {
                tool_name: tool_name.to_string(),
                output: format!("ℰMCP error: {e}"),
                success: false,
                blame: Blame::Network,
                taint: EpistemicTaint::Untrusted,
                server: self.server_url.clone(),
                effects: vec!["network".to_string()],
            },
        }
    }

    /// List available tools on the MCP server.
    pub fn list_tools(&mut self) -> Result<Vec<McpToolInfo>, String> {
        let request_id = self.next_id;
        self.next_id += 1;

        let rpc_request = JsonRpcRequest {
            jsonrpc: "2.0",
            method: "tools/list".to_string(),
            params: serde_json::json!({}),
            id: request_id,
        };

        let response = self.send_rpc(&rpc_request)?;

        match response.result {
            Some(val) => {
                if let Some(tools) = val.get("tools").and_then(|t| t.as_array()) {
                    let infos = tools
                        .iter()
                        .filter_map(|t| {
                            Some(McpToolInfo {
                                name: t.get("name")?.as_str()?.to_string(),
                                description: t
                                    .get("description")
                                    .and_then(|d| d.as_str())
                                    .unwrap_or("")
                                    .to_string(),
                            })
                        })
                        .collect();
                    Ok(infos)
                } else {
                    Ok(Vec::new())
                }
            }
            None => Err(response
                .error
                .map(|e| format!("JSON-RPC error {}: {}", e.code, e.message))
                .unwrap_or_else(|| "empty response".to_string())),
        }
    }

    /// Read a resource from the MCP server.
    pub fn read_resource(&mut self, uri: &str) -> McpCallResult {
        let request_id = self.next_id;
        self.next_id += 1;

        let rpc_request = JsonRpcRequest {
            jsonrpc: "2.0",
            method: "resources/read".to_string(),
            params: serde_json::json!({ "uri": uri }),
            id: request_id,
        };

        match self.send_rpc(&rpc_request) {
            Ok(response) => {
                match response.result {
                    Some(val) => {
                        // Extract text content from MCP resource response
                        let text = val
                            .get("contents")
                            .and_then(|c| c.as_array())
                            .and_then(|arr| arr.first())
                            .and_then(|item| item.get("text"))
                            .and_then(|t| t.as_str())
                            .unwrap_or_else(|| {
                                // Fallback: serialize the entire result
                                ""
                            });

                        let output = if text.is_empty() {
                            serde_json::to_string(&val).unwrap_or_default()
                        } else {
                            text.to_string()
                        };

                        McpCallResult {
                            tool_name: format!("resource:{uri}"),
                            output,
                            success: true,
                            blame: Blame::None,
                            taint: EpistemicTaint::Untrusted, // All MCP data → ⊥
                            server: self.server_url.clone(),
                            effects: vec!["network".to_string(), "io".to_string()],
                        }
                    }
                    None => McpCallResult {
                        tool_name: format!("resource:{uri}"),
                        output: response
                            .error
                            .map(|e| format!("JSON-RPC error {}: {}", e.code, e.message))
                            .unwrap_or_else(|| "empty response".to_string()),
                        success: false,
                        blame: Blame::Server,
                        taint: EpistemicTaint::Untrusted,
                        server: self.server_url.clone(),
                        effects: vec!["network".to_string()],
                    },
                }
            }
            Err(e) => McpCallResult {
                tool_name: format!("resource:{uri}"),
                output: format!("ℰMCP error: {e}"),
                success: false,
                blame: Blame::Network,
                taint: EpistemicTaint::Untrusted,
                server: self.server_url.clone(),
                effects: vec!["network".to_string()],
            },
        }
    }

    // ── Internal ──────────────────────────────────────────────────

    fn send_rpc(&self, request: &JsonRpcRequest) -> Result<JsonRpcResponse, String> {
        let client = reqwest::blocking::Client::builder()
            .timeout(self.timeout)
            .build()
            .map_err(|e| format!("failed to create HTTP client: {e}"))?;

        let body = serde_json::to_string(request)
            .map_err(|e| format!("failed to serialize request: {e}"))?;

        let response = client
            .post(&self.server_url)
            .header("Content-Type", "application/json")
            .header("X-Axon-EMCP", "1.0")
            .body(body)
            .send()
            .map_err(|e| {
                if e.is_timeout() {
                    format!("MCP server timed out after {}s", self.timeout.as_secs())
                } else if e.is_connect() {
                    format!("cannot connect to MCP server at {}", self.server_url)
                } else {
                    format!("MCP request failed: {e}")
                }
            })?;

        let status = response.status();
        let text = response
            .text()
            .map_err(|e| format!("failed to read MCP response: {e}"))?;

        if !status.is_success() {
            return Err(format!("MCP server returned HTTP {}: {}", status.as_u16(), text));
        }

        serde_json::from_str(&text)
            .map_err(|e| format!("invalid JSON-RPC response: {e}"))
    }

    fn process_response(&self, tool_name: &str, response: JsonRpcResponse) -> McpCallResult {
        match response.result {
            Some(val) => {
                // Extract content from MCP tool response
                let output = val
                    .get("content")
                    .and_then(|c| c.as_array())
                    .and_then(|arr| arr.first())
                    .and_then(|item| item.get("text"))
                    .and_then(|t| t.as_str())
                    .map(|s| s.to_string())
                    .unwrap_or_else(|| serde_json::to_string(&val).unwrap_or_default());

                McpCallResult {
                    tool_name: tool_name.to_string(),
                    output,
                    success: true,
                    blame: Blame::None,
                    taint: EpistemicTaint::Untrusted, // Born as ⊥ — must be elevated
                    server: self.server_url.clone(),
                    effects: vec![
                        "network".to_string(),
                        "epistemic:speculate".to_string(),
                    ],
                }
            }
            None => {
                let (blame, msg) = match response.error {
                    Some(e) => {
                        // JSON-RPC error codes: -32600..-32603 are protocol errors (caller)
                        // Server-defined errors are positive or other negative codes
                        let b = if (-32603..=-32600).contains(&e.code) {
                            Blame::Caller
                        } else {
                            Blame::Server
                        };
                        (b, format!("JSON-RPC error {}: {}", e.code, e.message))
                    }
                    None => (Blame::Server, "empty response from MCP server".to_string()),
                };

                McpCallResult {
                    tool_name: tool_name.to_string(),
                    output: msg,
                    success: false,
                    blame,
                    taint: EpistemicTaint::Untrusted,
                    server: self.server_url.clone(),
                    effects: vec!["network".to_string()],
                }
            }
        }
    }
}

/// Information about a tool available on an MCP server.
#[derive(Debug, Clone)]
pub struct McpToolInfo {
    pub name: String,
    pub description: String,
}

// ── Registry dispatch ────────────────────────────────────────────────────

/// Dispatch an MCP tool call via the ℰMCP transducer.
///
/// Creates a transient McpClient for the server URL in `entry.runtime`,
/// calls the tool, and returns the result with blame/taint metadata.
pub fn dispatch_mcp(entry: &ToolEntry, argument: &str) -> ToolResult {
    let server_url = entry.runtime.trim();

    if server_url.is_empty() {
        return ToolResult {
            success: false,
            output: format!(
                "ℰMCP tool '{}': no server URL. Set runtime: \"http://...\" in tool definition.",
                entry.name
            ),
            tool_name: entry.name.clone(),
        };
    }

    if !server_url.starts_with("http://") && !server_url.starts_with("https://") {
        return ToolResult {
            success: false,
            output: format!(
                "ℰMCP tool '{}': invalid server URL '{}'. Must start with http:// or https://.",
                entry.name, server_url
            ),
            tool_name: entry.name.clone(),
        };
    }

    let timeout = crate::http_tool::parse_timeout_pub(&entry.timeout)
        .unwrap_or(Duration::from_secs(30));

    let mut client = McpClient::new(server_url, timeout);
    let mcp_result = client.call_tool(&entry.name, argument);

    // Enrich output with blame metadata for tracing
    if !mcp_result.success {
        ToolResult {
            success: false,
            output: format!(
                "{} [blame={}]",
                mcp_result.output,
                mcp_result.blame.as_str()
            ),
            tool_name: entry.name.clone(),
        }
    } else {
        mcp_result.to_tool_result()
    }
}

// ── Blame tracker ────────────────────────────────────────────────────────

/// Accumulates blame records across MCP tool calls during execution.
#[derive(Debug, Default)]
pub struct BlameTracker {
    records: Vec<BlameRecord>,
}

/// A single blame record.
#[derive(Debug, Clone, Serialize)]
pub struct BlameRecord {
    pub tool_name: String,
    pub server: String,
    pub blame: Blame,
    pub taint: EpistemicTaint,
    pub message: String,
}

impl BlameTracker {
    pub fn new() -> Self {
        BlameTracker { records: Vec::new() }
    }

    /// Record a blame event from an MCP call result.
    pub fn record(&mut self, result: &McpCallResult) {
        self.records.push(BlameRecord {
            tool_name: result.tool_name.clone(),
            server: result.server.clone(),
            blame: result.blame,
            taint: result.taint,
            message: if result.success {
                "ok".to_string()
            } else {
                result.output.clone()
            },
        });
    }

    /// Total number of blame records.
    pub fn total(&self) -> usize {
        self.records.len()
    }

    /// Count of server-blamed failures.
    pub fn server_faults(&self) -> usize {
        self.records.iter().filter(|r| r.blame == Blame::Server).count()
    }

    /// Count of caller-blamed failures.
    pub fn caller_faults(&self) -> usize {
        self.records.iter().filter(|r| r.blame == Blame::Caller).count()
    }

    /// Count of network failures.
    pub fn network_faults(&self) -> usize {
        self.records.iter().filter(|r| r.blame == Blame::Network).count()
    }

    /// All records.
    pub fn records(&self) -> &[BlameRecord] {
        &self.records
    }
}

// ════════════════════════════════════════════════════════════════════════════
//  §Fase 34.f — McpStreamingTool: Tool::stream() over MCP JSON-RPC 2.0
//  partial responses (`notifications/message` / `notifications/progress`)
//  + best-effort `notifications/cancelled` on cancel.
// ════════════════════════════════════════════════════════════════════════════

use async_trait::async_trait;
use bytes::Bytes;
use futures::StreamExt;

use crate::backends::sse_streaming::LineBuffer;
use crate::tool_trait::{Tool, ToolChunk, ToolContext, ToolFinishReason, ToolStream};

/// MCP tool with first-class streaming surface (Fase 34.f).
///
/// MCP's wire format is JSON-RPC 2.0 over HTTP. Servers that
/// support partial-response streaming emit a sequence of JSON-RPC
/// envelopes — one per line — over a `application/x-ndjson` (or
/// `application/jsonl`) response body. Each envelope is either:
///
/// - A **notification** (no `id`): `method == "notifications/message"`
///   or `method == "notifications/progress"`. The notification's
///   `params.data` / `params.text` / `params.message` field becomes a
///   `ToolChunk::intermediate` delta.
/// - A **response** (has `id`): `result` is the final tool payload;
///   the `result.content[0].text` field becomes the terminator's
///   delta + the stream closes with `ToolFinishReason::Stop`. An
///   `error` envelope closes the stream with `ToolFinishReason::Error`.
///
/// Servers that **don't** support streaming respond with a single
/// JSON-RPC response (Content-Type `application/json`). The
/// streaming tool gracefully falls back to D9 single-chunk wrap:
/// the materialized `result.content[0].text` becomes one intermediate
/// `ToolChunk` followed by a `Stop` terminator — byte-equal to the
/// legacy [`dispatch_mcp`] output.
///
/// # Cancel discipline (D5)
///
/// `ctx.cancel` is polled between every `bytes_stream().next().await`
/// boundary. When fired, the streaming task:
///
/// 1. Drops the in-flight reqwest response (closing the connection).
/// 2. Best-effort fires a JSON-RPC `notifications/cancelled`
///    notification (POST + fire-and-forget; no await on response) so
///    MCP servers that support cooperative cancellation can clean up.
/// 3. Emits a `ToolFinishReason::Cancelled` terminator.
///
/// # Error discipline
///
/// Every failure surface — URL invalid / client build / connect /
/// timeout / non-2xx status / JSON-RPC error envelope / mid-stream
/// byte error / unparseable line — is captured as a
/// `ToolFinishReason::Error { message }` terminator chunk.
///
/// # Epistemic taint
///
/// All MCP-sourced data is born `Untrusted` (⊥) per the ℰMCP charter.
/// The streaming tool preserves this discipline — each chunk's delta
/// reaches the dispatcher untrusted; downstream `shield`/`know`
/// blocks elevate the taint via reasoning. Taint metadata is not
/// embedded in the `ToolChunk` (the dispatcher's audit row + ℰMCP
/// [`BlameTracker`] are the durable trail).
pub struct McpStreamingTool {
    name: String,
    server_url: String,
    timeout: Duration,
}

impl McpStreamingTool {
    /// Construct from a registry [`ToolEntry`]. Validates the server
    /// URL + extracts the timeout. Returns `Err` with adopter-facing
    /// diagnostic when the URL is missing or has an invalid scheme.
    pub fn from_entry(entry: &ToolEntry) -> Result<Self, String> {
        let url = entry.runtime.trim();
        if url.is_empty() {
            return Err(format!(
                "ℰMCP tool '{}': no server URL. Set runtime: \"http://...\" in tool definition.",
                entry.name
            ));
        }
        if !url.starts_with("http://") && !url.starts_with("https://") {
            return Err(format!(
                "ℰMCP tool '{}': invalid server URL '{}'. Must start with http:// or https://.",
                entry.name, url
            ));
        }
        let timeout =
            crate::http_tool::parse_timeout_pub(&entry.timeout).unwrap_or(Duration::from_secs(30));
        Ok(Self {
            name: entry.name.clone(),
            server_url: url.to_string(),
            timeout,
        })
    }

    /// Public new() ctor for tests + adopters who construct directly
    /// without a registry entry.
    pub fn new(name: String, server_url: String, timeout: Duration) -> Self {
        Self {
            name,
            server_url,
            timeout,
        }
    }
}

/// Build the JSON-RPC 2.0 `tools/call` request body. Mirrors the
/// legacy [`McpClient::call_tool`] argument-handling discipline:
/// JSON-shaped arguments pass through; plain strings wrap as
/// `{ input: <args> }`.
fn build_mcp_request_body(tool_name: &str, args: &str, request_id: u64) -> String {
    let params = if args.trim_start().starts_with('{') {
        serde_json::from_str::<serde_json::Value>(args).unwrap_or_else(|_| {
            serde_json::json!({
                "name": tool_name,
                "arguments": { "input": args }
            })
        })
    } else {
        serde_json::json!({
            "name": tool_name,
            "arguments": { "input": args }
        })
    };
    serde_json::json!({
        "jsonrpc": "2.0",
        "method": "tools/call",
        "params": params,
        "id": request_id,
    })
    .to_string()
}

/// Classify an MCP response Content-Type as streaming vs single.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum McpFramingMode {
    /// `application/x-ndjson` / `application/jsonl` — line-delimited
    /// JSON-RPC envelopes. Drain per-line + parse each envelope.
    NdjsonStream,
    /// `application/json` or anything else — single JSON-RPC
    /// response. D9 fallback: parse full body, emit `result.content`
    /// as 1 chunk + `Stop` terminator (byte-equal to
    /// [`dispatch_mcp`]).
    SingleResponse,
}

fn classify_mcp_framing(content_type: &str) -> McpFramingMode {
    let lc = content_type.to_ascii_lowercase();
    if lc.contains("application/x-ndjson") || lc.contains("application/jsonl") {
        McpFramingMode::NdjsonStream
    } else {
        McpFramingMode::SingleResponse
    }
}

/// Parsed JSON-RPC 2.0 envelope read from the streaming body. The
/// public ToolStream surface is downstream of this enum.
enum McpEnvelope {
    /// `method == "notifications/message"` / `"notifications/progress"`.
    /// The extracted human-readable text (params.data / params.text /
    /// params.message — the first non-empty wins). Empty notifications
    /// (no text payload) emit empty deltas which the dispatcher
    /// already skips per D4.
    Notification { delta: String },
    /// JSON-RPC response with a `result` field. The extracted
    /// `result.content[0].text` (or stringified result if shape
    /// differs).
    Result { delta: String },
    /// JSON-RPC error envelope. Carries code + message + blame slug.
    Error { message: String },
}

/// Parse one line of the MCP NDJSON body into an envelope. Returns
/// `None` for blank lines (per NDJSON spec) + for lines we don't
/// recognize (defensive — drop unknown envelopes rather than fail
/// the whole stream).
fn parse_mcp_envelope(line: &str) -> Option<McpEnvelope> {
    if line.trim().is_empty() {
        return None;
    }
    let value: serde_json::Value = serde_json::from_str(line).ok()?;

    // Error envelope wins precedence (a response can have both
    // result==null + error set; we honor the error path).
    if let Some(err) = value.get("error") {
        let code = err.get("code").and_then(|c| c.as_i64()).unwrap_or(0);
        let message = err
            .get("message")
            .and_then(|m| m.as_str())
            .unwrap_or("unknown JSON-RPC error");
        let blame = if (-32603..=-32600).contains(&code) {
            "caller"
        } else {
            "server"
        };
        return Some(McpEnvelope::Error {
            message: format!("JSON-RPC error {code}: {message} [blame={blame}]"),
        });
    }

    // Notification: has `method` field, no `id`.
    if let Some(method) = value.get("method").and_then(|m| m.as_str()) {
        if method == "notifications/message" || method == "notifications/progress" {
            let delta = extract_notification_text(value.get("params"));
            return Some(McpEnvelope::Notification { delta });
        }
        // Unknown method — drop defensively.
        return None;
    }

    // Response envelope: has `id` + `result`.
    if let Some(result) = value.get("result") {
        let delta = extract_result_text(result);
        return Some(McpEnvelope::Result { delta });
    }
    None
}

/// Extract the human-readable text from a notification's `params`.
/// First non-empty of: `data`, `text`, `message`. Falls back to
/// `serde_json` stringification of the params if no canonical field
/// matches (preserves the payload for downstream debugging).
fn extract_notification_text(params: Option<&serde_json::Value>) -> String {
    let Some(p) = params else { return String::new() };
    for key in ["data", "text", "message"] {
        if let Some(val) = p.get(key).and_then(|v| v.as_str()) {
            if !val.is_empty() {
                return val.to_string();
            }
        }
    }
    serde_json::to_string(p).unwrap_or_default()
}

/// Extract `result.content[0].text` from a JSON-RPC response result.
/// Falls back to serialized result on shape mismatch.
fn extract_result_text(result: &serde_json::Value) -> String {
    result
        .get("content")
        .and_then(|c| c.as_array())
        .and_then(|arr| arr.first())
        .and_then(|item| item.get("text"))
        .and_then(|t| t.as_str())
        .map(|s| s.to_string())
        .unwrap_or_else(|| serde_json::to_string(result).unwrap_or_default())
}

/// Best-effort fire-and-forget `notifications/cancelled` POST. Used
/// by the streaming task when cancel fires mid-stream. We don't
/// wait on the response — MCP servers that support cooperative
/// cancellation will honor it; servers that don't see only a
/// closed connection. No-op if the URL is unreachable.
fn fire_cancel_notification(server_url: String, request_id: u64, name: String) {
    tokio::spawn(async move {
        let body = serde_json::json!({
            "jsonrpc": "2.0",
            "method": "notifications/cancelled",
            "params": { "requestId": request_id },
        })
        .to_string();
        if let Ok(client) = reqwest::Client::builder()
            .timeout(Duration::from_millis(500))
            .build()
        {
            let _ = client
                .post(&server_url)
                .header("Content-Type", "application/json")
                .header("X-Axon-EMCP", "1.0")
                .header("X-Axon-Tool", name)
                .body(body)
                .send()
                .await;
        }
    });
}

#[async_trait]
impl Tool for McpStreamingTool {
    async fn execute(&self, args: String, _ctx: ToolContext) -> ToolResult {
        // Synchronous path delegates to legacy `dispatch_mcp` via
        // spawn_blocking so reqwest::blocking::Client doesn't panic
        // inside the async runtime. Output is byte-equal to
        // dispatch_mcp (D9 backwards-compat).
        let entry = ToolEntry {
            name: self.name.clone(),
            provider: "mcp".to_string(),
            timeout: format!("{}s", self.timeout.as_secs()),
            runtime: self.server_url.clone(),
            sandbox: None,
            max_results: None,
            output_schema: String::new(),
            effect_row: Vec::new(),
            source: crate::tool_registry::ToolSource::Program,
            is_streaming: false,
        };
        let args_owned = args;
        match tokio::task::spawn_blocking(move || dispatch_mcp(&entry, &args_owned)).await {
            Ok(result) => result,
            Err(e) => ToolResult {
                success: false,
                output: format!("ℰMCP tool '{}': blocking task join failed: {e}", self.name),
                tool_name: self.name.clone(),
            },
        }
    }

    async fn stream(&self, args: String, ctx: ToolContext) -> ToolStream {
        let server_url = self.server_url.clone();
        let name = self.name.clone();
        let timeout = self.timeout;
        let cancel = ctx.cancel.clone();
        // request_id is monotonic per-invocation; the cancel
        // notification uses it for correlation. We start at the
        // ToolContext's trace_id so the audit trail can link the
        // MCP correlation back to the trace. (ID collisions between
        // distinct McpStreamingTool invocations are accepted — MCP
        // servers correlate per-connection.)
        let request_id = ctx.trace_id.max(1);
        let body = build_mcp_request_body(&name, &args, request_id);

        let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<ToolChunk>();

        tokio::spawn(async move {
            let send_terminator = |reason: ToolFinishReason| {
                let _ = tx.send(ToolChunk::terminator("", reason));
            };

            // Pre-flight cancel.
            if cancel.is_cancelled() {
                fire_cancel_notification(server_url.clone(), request_id, name.clone());
                send_terminator(ToolFinishReason::Cancelled);
                return;
            }

            // 1. Build async client.
            let client = match reqwest::Client::builder().timeout(timeout).build() {
                Ok(c) => c,
                Err(e) => {
                    send_terminator(ToolFinishReason::Error {
                        message: format!(
                            "ℰMCP tool '{name}': failed to build async client: {e}"
                        ),
                    });
                    return;
                }
            };

            // 2. Issue request.
            let response = match client
                .post(&server_url)
                .header("Content-Type", "application/json")
                .header("X-Axon-EMCP", "1.0")
                .header("Accept", "application/x-ndjson, application/json")
                .body(body)
                .send()
                .await
            {
                Ok(r) => r,
                Err(e) => {
                    let message = if e.is_timeout() {
                        format!(
                            "ℰMCP tool '{name}': server timed out after {}s",
                            timeout.as_secs()
                        )
                    } else if e.is_connect() {
                        format!("ℰMCP tool '{name}': cannot connect to MCP server at {server_url}")
                    } else {
                        format!("ℰMCP tool '{name}': MCP request failed: {e}")
                    };
                    send_terminator(ToolFinishReason::Error { message });
                    return;
                }
            };

            // 3. Non-2xx → error terminator with status + truncated body.
            let status = response.status();
            if !status.is_success() {
                let body_text = response.text().await.unwrap_or_default();
                let truncated = if body_text.len() > 200 {
                    format!("{}...", &body_text[..200])
                } else {
                    body_text
                };
                send_terminator(ToolFinishReason::Error {
                    message: format!(
                        "ℰMCP server '{name}' returned HTTP {}: {}",
                        status.as_u16(),
                        truncated
                    ),
                });
                return;
            }

            // 4. Read Content-Type → classify framing.
            let content_type = response
                .headers()
                .get(reqwest::header::CONTENT_TYPE)
                .and_then(|v| v.to_str().ok())
                .unwrap_or("")
                .to_string();
            let framing = classify_mcp_framing(&content_type);

            // 5. Drain per framing mode.
            let mut byte_stream = response.bytes_stream();
            let drain_result = match framing {
                McpFramingMode::NdjsonStream => {
                    drain_mcp_ndjson(&mut byte_stream, &cancel, &tx).await
                }
                McpFramingMode::SingleResponse => {
                    drain_mcp_single(&mut byte_stream, &cancel, &tx).await
                }
            };

            match drain_result {
                McpDrainOutcome::Completed => send_terminator(ToolFinishReason::Stop),
                McpDrainOutcome::CompletedWith(reason) => send_terminator(reason),
                McpDrainOutcome::Cancelled => {
                    fire_cancel_notification(server_url.clone(), request_id, name.clone());
                    send_terminator(ToolFinishReason::Cancelled);
                }
                McpDrainOutcome::Error(message) => {
                    send_terminator(ToolFinishReason::Error { message })
                }
            }
        });

        Box::pin(futures::stream::unfold(rx, |mut rx| async move {
            rx.recv().await.map(|chunk| (chunk, rx))
        }))
    }

    fn is_streaming(&self) -> bool {
        true
    }
}

/// Per-drain outcome. The `CompletedWith` variant lets a drain
/// helper carry a JSON-RPC error all the way back to the spawned
/// task without losing the typed surface.
enum McpDrainOutcome {
    /// Stream ended naturally → `Stop` terminator.
    Completed,
    /// Stream ended with an explicit reason — used when the
    /// JSON-RPC body returns an error envelope (we want to keep
    /// the typed `ToolFinishReason::Error` surface intact instead
    /// of stringifying through `Error(message)`).
    CompletedWith(ToolFinishReason),
    /// Cancel flag observed mid-drain.
    Cancelled,
    /// Stream byte error or unrecoverable parse error.
    Error(String),
}

/// Drain MCP NDJSON-streaming body. Parse each LF-delimited line as
/// a JSON-RPC envelope; emit notifications as intermediate chunks,
/// stop on response/error envelopes.
async fn drain_mcp_ndjson<S>(
    byte_stream: &mut S,
    cancel: &crate::cancel_token::CancellationFlag,
    tx: &tokio::sync::mpsc::UnboundedSender<ToolChunk>,
) -> McpDrainOutcome
where
    S: futures::Stream<Item = reqwest::Result<Bytes>> + Unpin + Send,
{
    let mut line_buf = LineBuffer::new();
    loop {
        if cancel.is_cancelled() {
            return McpDrainOutcome::Cancelled;
        }
        match byte_stream.next().await {
            None => break,
            Some(Err(e)) => {
                return McpDrainOutcome::Error(format!("MCP stream chunk error: {e}"))
            }
            Some(Ok(bytes)) => {
                let lines = line_buf.push(&bytes);
                for line in lines {
                    if let Some(env) = parse_mcp_envelope(&line) {
                        match env {
                            McpEnvelope::Notification { delta } => {
                                if !delta.is_empty()
                                    && tx.send(ToolChunk::intermediate(delta)).is_err()
                                {
                                    return McpDrainOutcome::Cancelled;
                                }
                            }
                            McpEnvelope::Result { delta } => {
                                if !delta.is_empty() {
                                    let _ = tx.send(ToolChunk::intermediate(delta));
                                }
                                return McpDrainOutcome::Completed;
                            }
                            McpEnvelope::Error { message } => {
                                return McpDrainOutcome::CompletedWith(
                                    ToolFinishReason::Error { message },
                                );
                            }
                        }
                    }
                }
            }
        }
    }
    if let Some(line) = line_buf.flush() {
        if let Some(env) = parse_mcp_envelope(&line) {
            match env {
                McpEnvelope::Notification { delta } => {
                    if !delta.is_empty() {
                        let _ = tx.send(ToolChunk::intermediate(delta));
                    }
                }
                McpEnvelope::Result { delta } => {
                    if !delta.is_empty() {
                        let _ = tx.send(ToolChunk::intermediate(delta));
                    }
                    return McpDrainOutcome::Completed;
                }
                McpEnvelope::Error { message } => {
                    return McpDrainOutcome::CompletedWith(ToolFinishReason::Error {
                        message,
                    });
                }
            }
        }
    }
    McpDrainOutcome::Completed
}

/// Drain MCP single-response (non-streaming) body. Parse the full
/// body as one JSON-RPC envelope; D9 backwards-compat: emit
/// `result.content[0].text` as 1 chunk + `Stop` terminator (matches
/// [`dispatch_mcp`] byte-equal); on `error`, emit `Error` terminator.
async fn drain_mcp_single<S>(
    byte_stream: &mut S,
    cancel: &crate::cancel_token::CancellationFlag,
    tx: &tokio::sync::mpsc::UnboundedSender<ToolChunk>,
) -> McpDrainOutcome
where
    S: futures::Stream<Item = reqwest::Result<Bytes>> + Unpin + Send,
{
    let mut acc: Vec<u8> = Vec::new();
    loop {
        if cancel.is_cancelled() {
            return McpDrainOutcome::Cancelled;
        }
        match byte_stream.next().await {
            None => break,
            Some(Err(e)) => {
                return McpDrainOutcome::Error(format!("MCP body chunk error: {e}"))
            }
            Some(Ok(bytes)) => acc.extend_from_slice(&bytes),
        }
    }
    let body_text = String::from_utf8_lossy(&acc).into_owned();
    if body_text.trim().is_empty() {
        return McpDrainOutcome::Completed;
    }
    let value: serde_json::Value = match serde_json::from_str(&body_text) {
        Ok(v) => v,
        Err(e) => {
            return McpDrainOutcome::Error(format!(
                "ℰMCP server returned unparseable JSON-RPC response: {e}"
            ));
        }
    };
    if let Some(err) = value.get("error") {
        let code = err.get("code").and_then(|c| c.as_i64()).unwrap_or(0);
        let message = err
            .get("message")
            .and_then(|m| m.as_str())
            .unwrap_or("unknown JSON-RPC error");
        let blame = if (-32603..=-32600).contains(&code) {
            "caller"
        } else {
            "server"
        };
        return McpDrainOutcome::CompletedWith(ToolFinishReason::Error {
            message: format!("JSON-RPC error {code}: {message} [blame={blame}]"),
        });
    }
    if let Some(result) = value.get("result") {
        let delta = extract_result_text(result);
        if !delta.is_empty() {
            let _ = tx.send(ToolChunk::intermediate(delta));
        }
        return McpDrainOutcome::Completed;
    }
    McpDrainOutcome::Error("ℰMCP response has neither result nor error".to_string())
}

// ── Tests ─────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tool_registry::{ToolEntry, ToolSource};

    fn make_mcp_entry(name: &str, url: &str, timeout: &str) -> ToolEntry {
        ToolEntry {
            name: name.to_string(),
            provider: "mcp".to_string(),
            timeout: timeout.to_string(),
            runtime: url.to_string(),
            sandbox: None,
            max_results: None,
            output_schema: "JSON".to_string(),
            effect_row: vec!["network".to_string(), "epistemic:speculate".to_string()],
            source: ToolSource::Program,
            // §Fase 34.c — MCP tools default to non-streaming; effect_row
            // carries `network` + `epistemic:speculate` but no `stream:`
            // prefix. MCP streaming via partial-response notifications
            // lands in Fase 34.f.
            is_streaming: false,
        }
    }

    // ── Blame ─────────────────────────────────────────────────────

    #[test]
    fn blame_variants() {
        assert_eq!(Blame::None.as_str(), "none");
        assert_eq!(Blame::Server.as_str(), "server");
        assert_eq!(Blame::Caller.as_str(), "caller");
        assert_eq!(Blame::Network.as_str(), "network");
    }

    // ── Epistemic taint ───────────────────────────────────────────

    #[test]
    fn taint_levels() {
        assert_eq!(EpistemicTaint::Untrusted.as_str(), "untrusted");
        assert_eq!(EpistemicTaint::SchemaValidated.as_str(), "schema_validated");
        assert_eq!(EpistemicTaint::Elevated.as_str(), "elevated");
    }

    #[test]
    fn mcp_data_born_untrusted() {
        // All MCP-sourced data must start as Untrusted (⊥)
        let result = McpCallResult {
            tool_name: "DataTool".into(),
            output: "some data".into(),
            success: true,
            blame: Blame::None,
            taint: EpistemicTaint::Untrusted,
            server: "http://localhost:3000".into(),
            effects: vec!["network".into()],
        };
        assert_eq!(result.taint, EpistemicTaint::Untrusted);
    }

    // ── McpCallResult conversion ──────────────────────────────────

    #[test]
    fn mcp_result_to_tool_result() {
        let mcp = McpCallResult {
            tool_name: "TestTool".into(),
            output: "hello".into(),
            success: true,
            blame: Blame::None,
            taint: EpistemicTaint::Untrusted,
            server: "http://localhost".into(),
            effects: vec![],
        };
        let tr = mcp.to_tool_result();
        assert!(tr.success);
        assert_eq!(tr.output, "hello");
        assert_eq!(tr.tool_name, "TestTool");
    }

    // ── Dispatch validation ───────────────────────────────────────

    #[test]
    fn dispatch_empty_url_fails() {
        let entry = make_mcp_entry("McpTool", "", "5s");
        let result = dispatch_mcp(&entry, "arg");
        assert!(!result.success);
        assert!(result.output.contains("no server URL"));
    }

    #[test]
    fn dispatch_invalid_scheme_fails() {
        let entry = make_mcp_entry("McpTool", "ws://localhost:3000", "5s");
        let result = dispatch_mcp(&entry, "arg");
        assert!(!result.success);
        assert!(result.output.contains("invalid server URL"));
    }

    #[test]
    fn dispatch_connection_refused() {
        let entry = make_mcp_entry("McpTool", "http://127.0.0.1:1/mcp", "2s");
        let result = dispatch_mcp(&entry, "test");
        assert!(!result.success);
        assert!(result.output.contains("blame=network"));
    }

    // ── Blame tracker ─────────────────────────────────────────────

    #[test]
    fn blame_tracker_accumulates() {
        let mut tracker = BlameTracker::new();

        tracker.record(&McpCallResult {
            tool_name: "A".into(),
            output: "ok".into(),
            success: true,
            blame: Blame::None,
            taint: EpistemicTaint::Untrusted,
            server: "s1".into(),
            effects: vec![],
        });

        tracker.record(&McpCallResult {
            tool_name: "B".into(),
            output: "server error".into(),
            success: false,
            blame: Blame::Server,
            taint: EpistemicTaint::Untrusted,
            server: "s1".into(),
            effects: vec![],
        });

        tracker.record(&McpCallResult {
            tool_name: "C".into(),
            output: "bad params".into(),
            success: false,
            blame: Blame::Caller,
            taint: EpistemicTaint::Untrusted,
            server: "s2".into(),
            effects: vec![],
        });

        tracker.record(&McpCallResult {
            tool_name: "D".into(),
            output: "timeout".into(),
            success: false,
            blame: Blame::Network,
            taint: EpistemicTaint::Untrusted,
            server: "s1".into(),
            effects: vec![],
        });

        assert_eq!(tracker.total(), 4);
        assert_eq!(tracker.server_faults(), 1);
        assert_eq!(tracker.caller_faults(), 1);
        assert_eq!(tracker.network_faults(), 1);
    }

    // ── McpClient construction ────────────────────────────────────

    #[test]
    fn mcp_client_creates() {
        let client = McpClient::new("http://localhost:3000", Duration::from_secs(10));
        assert_eq!(client.server_url, "http://localhost:3000");
        assert_eq!(client.timeout, Duration::from_secs(10));
        assert_eq!(client.next_id, 1);
    }

    // ── JSON-RPC response processing ──────────────────────────────

    #[test]
    fn process_success_response() {
        let client = McpClient::new("http://localhost", Duration::from_secs(5));
        let response = JsonRpcResponse {
            jsonrpc: "2.0".into(),
            result: Some(serde_json::json!({
                "content": [{"type": "text", "text": "result data"}]
            })),
            error: None,
            id: 1,
        };

        let result = client.process_response("TestTool", response);
        assert!(result.success);
        assert_eq!(result.output, "result data");
        assert_eq!(result.blame, Blame::None);
        assert_eq!(result.taint, EpistemicTaint::Untrusted); // Still untrusted!
    }

    #[test]
    fn process_server_error_response() {
        let client = McpClient::new("http://localhost", Duration::from_secs(5));
        let response = JsonRpcResponse {
            jsonrpc: "2.0".into(),
            result: None,
            error: Some(JsonRpcError {
                code: -32000,
                message: "internal server error".into(),
            }),
            id: 1,
        };

        let result = client.process_response("TestTool", response);
        assert!(!result.success);
        assert_eq!(result.blame, Blame::Server);
        assert!(result.output.contains("-32000"));
    }

    #[test]
    fn process_caller_error_response() {
        let client = McpClient::new("http://localhost", Duration::from_secs(5));
        // -32602 = Invalid params (JSON-RPC standard)
        let response = JsonRpcResponse {
            jsonrpc: "2.0".into(),
            result: None,
            error: Some(JsonRpcError {
                code: -32602,
                message: "invalid params".into(),
            }),
            id: 1,
        };

        let result = client.process_response("TestTool", response);
        assert!(!result.success);
        assert_eq!(result.blame, Blame::Caller);
    }

    // ── Effect inference ──────────────────────────────────────────

    #[test]
    fn mcp_calls_carry_epistemic_effects() {
        let client = McpClient::new("http://localhost", Duration::from_secs(5));
        let response = JsonRpcResponse {
            jsonrpc: "2.0".into(),
            result: Some(serde_json::json!({"content": [{"text": "data"}]})),
            error: None,
            id: 1,
        };

        let result = client.process_response("Tool", response);
        assert!(result.effects.contains(&"network".to_string()));
        assert!(result.effects.contains(&"epistemic:speculate".to_string()));
    }

    // ── Serialization ─────────────────────────────────────────────

    #[test]
    fn blame_serializes() {
        let json = serde_json::to_string(&Blame::Server).unwrap();
        assert_eq!(json, "\"Server\"");
    }

    #[test]
    fn mcp_call_result_serializes() {
        let result = McpCallResult {
            tool_name: "T".into(),
            output: "out".into(),
            success: true,
            blame: Blame::None,
            taint: EpistemicTaint::Untrusted,
            server: "http://s".into(),
            effects: vec!["network".into()],
        };
        let json = serde_json::to_string(&result).unwrap();
        assert!(json.contains("\"blame\":\"None\""));
        assert!(json.contains("\"taint\":\"Untrusted\""));
    }

    // ════════════════════════════════════════════════════════════════
    //  §Fase 34.f — McpStreamingTool lib unit tests
    // ════════════════════════════════════════════════════════════════

    #[test]
    fn mcp_streaming_tool_from_entry_accepts_valid_http_url() {
        let entry = make_mcp_entry("McpTool", "http://localhost:3000/mcp", "10s");
        let t = McpStreamingTool::from_entry(&entry).expect("ok");
        assert_eq!(t.name, "McpTool");
        assert_eq!(t.server_url, "http://localhost:3000/mcp");
        assert_eq!(t.timeout, Duration::from_secs(10));
        assert!(t.is_streaming());
    }

    #[test]
    fn mcp_streaming_tool_from_entry_accepts_https_url() {
        let entry = make_mcp_entry("McpTool", "https://api.example.com/mcp", "5s");
        let t = McpStreamingTool::from_entry(&entry).expect("ok");
        assert!(t.server_url.starts_with("https://"));
    }

    #[test]
    fn mcp_streaming_tool_from_entry_rejects_empty_url() {
        let entry = make_mcp_entry("McpTool", "", "10s");
        let err = McpStreamingTool::from_entry(&entry).err().unwrap();
        assert!(err.contains("no server URL"));
    }

    #[test]
    fn mcp_streaming_tool_from_entry_rejects_invalid_scheme() {
        let entry = make_mcp_entry("McpTool", "ws://localhost:3000", "10s");
        let err = McpStreamingTool::from_entry(&entry).err().unwrap();
        assert!(err.contains("invalid server URL"));
    }

    #[test]
    fn mcp_streaming_tool_default_timeout_when_empty() {
        let entry = make_mcp_entry("McpTool", "http://localhost", "");
        let t = McpStreamingTool::from_entry(&entry).expect("ok");
        assert_eq!(t.timeout, Duration::from_secs(30));
    }

    // ─── Framing classification ─────────────────────────────────────

    #[test]
    fn classify_mcp_framing_ndjson() {
        assert_eq!(
            classify_mcp_framing("application/x-ndjson"),
            McpFramingMode::NdjsonStream
        );
        assert_eq!(
            classify_mcp_framing("application/x-ndjson; charset=utf-8"),
            McpFramingMode::NdjsonStream
        );
        assert_eq!(
            classify_mcp_framing("application/jsonl"),
            McpFramingMode::NdjsonStream
        );
    }

    #[test]
    fn classify_mcp_framing_single_default() {
        assert_eq!(
            classify_mcp_framing("application/json"),
            McpFramingMode::SingleResponse
        );
        assert_eq!(
            classify_mcp_framing("text/plain"),
            McpFramingMode::SingleResponse
        );
        assert_eq!(classify_mcp_framing(""), McpFramingMode::SingleResponse);
    }

    // ─── Envelope parsing ───────────────────────────────────────────

    #[test]
    fn parse_envelope_notification_message_data() {
        let line = r#"{"jsonrpc":"2.0","method":"notifications/message","params":{"data":"partial-1"}}"#;
        match parse_mcp_envelope(line) {
            Some(McpEnvelope::Notification { delta }) => assert_eq!(delta, "partial-1"),
            other => panic!("expected Notification, got {}", envelope_label(&other)),
        }
    }

    #[test]
    fn parse_envelope_notification_progress_text() {
        let line = r#"{"jsonrpc":"2.0","method":"notifications/progress","params":{"text":"50% done"}}"#;
        match parse_mcp_envelope(line) {
            Some(McpEnvelope::Notification { delta }) => assert_eq!(delta, "50% done"),
            other => panic!("expected Notification, got {}", envelope_label(&other)),
        }
    }

    #[test]
    fn parse_envelope_notification_message_field_fallback() {
        // `message` key as third-priority fallback.
        let line = r#"{"jsonrpc":"2.0","method":"notifications/message","params":{"message":"hi"}}"#;
        match parse_mcp_envelope(line) {
            Some(McpEnvelope::Notification { delta }) => assert_eq!(delta, "hi"),
            other => panic!("expected Notification, got {}", envelope_label(&other)),
        }
    }

    #[test]
    fn parse_envelope_response_with_content() {
        let line = r#"{"jsonrpc":"2.0","result":{"content":[{"type":"text","text":"final answer"}]},"id":1}"#;
        match parse_mcp_envelope(line) {
            Some(McpEnvelope::Result { delta }) => assert_eq!(delta, "final answer"),
            other => panic!("expected Result, got {}", envelope_label(&other)),
        }
    }

    #[test]
    fn parse_envelope_error_server_blame() {
        let line = r#"{"jsonrpc":"2.0","error":{"code":-32000,"message":"internal"},"id":1}"#;
        match parse_mcp_envelope(line) {
            Some(McpEnvelope::Error { message }) => {
                assert!(message.contains("-32000"));
                assert!(message.contains("blame=server"));
            }
            other => panic!("expected Error, got {}", envelope_label(&other)),
        }
    }

    #[test]
    fn parse_envelope_error_caller_blame_invalid_params() {
        let line = r#"{"jsonrpc":"2.0","error":{"code":-32602,"message":"bad params"},"id":1}"#;
        match parse_mcp_envelope(line) {
            Some(McpEnvelope::Error { message }) => {
                assert!(message.contains("blame=caller"));
            }
            other => panic!("expected Error, got {}", envelope_label(&other)),
        }
    }

    #[test]
    fn parse_envelope_blank_line_returns_none() {
        assert!(parse_mcp_envelope("").is_none());
        assert!(parse_mcp_envelope("   ").is_none());
    }

    #[test]
    fn parse_envelope_unknown_method_returns_none() {
        let line = r#"{"jsonrpc":"2.0","method":"notifications/heartbeat","params":{}}"#;
        assert!(parse_mcp_envelope(line).is_none());
    }

    #[test]
    fn parse_envelope_malformed_json_returns_none() {
        assert!(parse_mcp_envelope("not json").is_none());
        assert!(parse_mcp_envelope("{").is_none());
    }

    #[test]
    fn build_mcp_request_body_json_args_passthrough() {
        let body = build_mcp_request_body("Tool", r#"{"name":"Tool","arguments":{"x":1}}"#, 42);
        let v: serde_json::Value = serde_json::from_str(&body).unwrap();
        assert_eq!(v["jsonrpc"], "2.0");
        assert_eq!(v["method"], "tools/call");
        assert_eq!(v["id"], 42);
        assert_eq!(v["params"]["arguments"]["x"], 1);
    }

    #[test]
    fn build_mcp_request_body_plain_args_wrapped() {
        let body = build_mcp_request_body("Tool", "search query", 1);
        let v: serde_json::Value = serde_json::from_str(&body).unwrap();
        assert_eq!(v["params"]["name"], "Tool");
        assert_eq!(v["params"]["arguments"]["input"], "search query");
    }

    /// Helper to name McpEnvelope variants in test failure messages
    /// without exposing them via Debug.
    fn envelope_label(env: &Option<McpEnvelope>) -> &'static str {
        match env {
            None => "None",
            Some(McpEnvelope::Notification { .. }) => "Notification",
            Some(McpEnvelope::Result { .. }) => "Result",
            Some(McpEnvelope::Error { .. }) => "Error",
        }
    }
}