aethershell 0.3.1

The world's first multi-agent shell with typed functional pipelines and multi-modal AI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
//! Provider-agnostic LLMs + Agents (single & multi-agent swarms) for Aether Shell.
//!
//! ## Supported AI Backends
//!
//! - **OpenAI** (`openai:gpt-4o-mini`) - OpenAI's API service
//! - **Ollama** (`ollama:llama3`) - Local Ollama server
//! - **vLLM** (`vllm:meta-llama/Llama-3-8B`) - High-performance inference with PagedAttention
//! - **llama.cpp** (`llamacpp:model`) - Efficient CPU/GPU inference
//! - **TGI** (`tgi:mixtral`) - HuggingFace Text Generation Inference
//! - **OpenAI-Compatible** (`compat:mixtral`) - Any OpenAI-compatible API
//!
//! ## Features
//!
//! - Model URIs for flexible backend selection
//! - Backend registry and per-agent model selection
//! - ToolRegistry with Builtin + MCP resolver
//! - Agents: run_sync + run_sync_with_model
//! - Swarm framework with Coordinator (RoundRobin + Router stubs)
//! - Multi-modal support for images, audio, and video
//! - A2A (Agent-to-Agent) communication protocol
//! - NANDA negotiation and task allocation framework
//! - SECURITY: SecureApiConfig with memory sanitization for API keys

use anyhow::{anyhow, Context, Result};
use reqwest::header::{AUTHORIZATION, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value as J};

use crate::secure_config::SecureApiConfig;

// Sub-modules
pub mod a2a;
pub mod a2ui;
pub mod nanda;

// ===================== Multi-modal support =====================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiModalContent {
    pub text: Option<String>,
    pub image_url: Option<String>,
    pub audio_url: Option<String>,
    pub video_url: Option<String>,
    pub image_data: Option<String>, // base64 encoded
    pub audio_data: Option<String>, // base64 encoded
    pub video_data: Option<String>, // base64 encoded
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiModalMessage {
    pub role: String,
    pub content: Vec<MultiModalContent>,
}

impl MultiModalMessage {
    pub fn text_only(role: &str, text: &str) -> Self {
        Self {
            role: role.to_string(),
            content: vec![MultiModalContent {
                text: Some(text.to_string()),
                image_url: None,
                audio_url: None,
                video_url: None,
                image_data: None,
                audio_data: None,
                video_data: None,
            }],
        }
    }

    pub fn with_image(role: &str, text: &str, image_data: &str) -> Self {
        Self {
            role: role.to_string(),
            content: vec![
                MultiModalContent {
                    text: Some(text.to_string()),
                    image_url: None,
                    audio_url: None,
                    video_url: None,
                    image_data: None,
                    audio_data: None,
                    video_data: None,
                },
                MultiModalContent {
                    text: None,
                    image_url: None,
                    audio_url: None,
                    video_url: None,
                    image_data: Some(image_data.to_string()),
                    audio_data: None,
                    video_data: None,
                },
            ],
        }
    }

    pub fn with_audio(role: &str, text: &str, audio_data: &str) -> Self {
        Self {
            role: role.to_string(),
            content: vec![
                MultiModalContent {
                    text: Some(text.to_string()),
                    image_url: None,
                    audio_url: None,
                    video_url: None,
                    image_data: None,
                    audio_data: None,
                    video_data: None,
                },
                MultiModalContent {
                    text: None,
                    image_url: None,
                    audio_url: None,
                    video_url: None,
                    image_data: None,
                    audio_data: Some(audio_data.to_string()),
                    video_data: None,
                },
            ],
        }
    }

    /// Convert to simple text for non-multimodal backends
    pub fn to_text(&self) -> String {
        self.content
            .iter()
            .filter_map(|c| c.text.as_ref())
            .cloned()
            .collect::<Vec<_>>()
            .join(" ")
    }
}

/// Multi-modal LLM backend trait
pub trait MultiModalLlmBackend: Send + Sync {
    fn chat_multimodal(&self, messages: &[MultiModalMessage]) -> Result<String>;
    fn supports_images(&self) -> bool {
        false
    }
    fn supports_audio(&self) -> bool {
        false
    }
    fn supports_video(&self) -> bool {
        false
    }
}

/// Multi-modal router function
pub fn complete_multimodal_sync(messages: &[MultiModalMessage]) -> Result<String> {
    let backend = multimodal_backend_from_env();
    backend.chat_multimodal(messages)
}

fn multimodal_backend_from_env() -> Box<dyn MultiModalLlmBackend> {
    let model_uri = std::env::var("AETHER_MODEL_URI").unwrap_or_else(|_| {
        match std::env::var("AETHER_AI")
            .unwrap_or_else(|_| "stub".into())
            .as_str()
        {
            "openai" => "openai:gpt-4o",
            "ollama" => "ollama:llava",
            "vllm" => "vllm:meta-llama/Llama-3-Vision",
            "llamacpp" => "llamacpp:llava",
            "compat" => "compat:gpt-4v",
            _ => "stub",
        }
        .to_string()
    });

    multimodal_backend_from_model(model_uri)
}

fn multimodal_backend_from_model(uri: String) -> Box<dyn MultiModalLlmBackend> {
    let m = parse_model_ref(&uri);
    match m.provider {
        Provider::OpenAI => Box::new(OpenAiMultiModalBackend),
        Provider::Ollama => Box::new(OllamaMultiModalBackend),
        Provider::OpenAICompat => Box::new(OpenAiCompatMultiModalBackend),
        _ => Box::new(StubMultiModalBackend),
    }
}

// Multi-modal backend implementations
struct StubMultiModalBackend;
impl MultiModalLlmBackend for StubMultiModalBackend {
    fn chat_multimodal(&self, _messages: &[MultiModalMessage]) -> Result<String> {
        Err(anyhow!(
            "No AI provider configured for multi-modal queries.\n\
            Set AETHER_AI=openai and OPENAI_API_KEY for vision/audio support."
        ))
    }
}

struct OpenAiMultiModalBackend;
impl MultiModalLlmBackend for OpenAiMultiModalBackend {
    fn chat_multimodal(&self, messages: &[MultiModalMessage]) -> Result<String> {
        // SECURITY FIX (HIGH-002): Use SecureApiConfig for memory-safe key handling
        let config = SecureApiConfig::from_keyring_or_env(
            "openai",
            "OPENAI_API_KEY",
            "https://api.openai.com".to_string(),
            std::env::var("OPENAI_MODEL").unwrap_or_else(|_| "gpt-4o".to_string()),
            "openai".to_string(),
        )
        .context("Failed to load OpenAI configuration for multimodal")?;

        config
            .validate_format()
            .context("OpenAI API key validation failed")?;

        let url = format!(
            "{}/v1/chat/completions",
            config.endpoint.trim_end_matches('/')
        );

        // Convert messages to OpenAI format
        let openai_messages: Vec<J> = messages
            .iter()
            .map(|msg| {
                let mut content = Vec::new();

                for part in &msg.content {
                    if let Some(text) = &part.text {
                        content.push(json!({
                            "type": "text",
                            "text": text
                        }));
                    }

                    if let Some(image_data) = &part.image_data {
                        content.push(json!({
                            "type": "image_url",
                            "image_url": {
                                "url": format!("data:image/jpeg;base64,{}", image_data)
                            }
                        }));
                    }

                    if let Some(image_url) = &part.image_url {
                        content.push(json!({
                            "type": "image_url",
                            "image_url": {
                                "url": image_url
                            }
                        }));
                    }
                }

                json!({
                    "role": msg.role,
                    "content": content
                })
            })
            .collect();

        let body = json!({
            "model": config.model,
            "messages": openai_messages,
            "temperature": 0.2,
            "max_tokens": 1000
        });

        // Create authorization header (zeroized after use)
        let auth_header = config
            .create_auth_header()
            .ok_or_else(|| anyhow!("OpenAI API key not configured for multimodal"))?;

        // SECURITY FIX (LOW-002): Use secure HTTP client with timeouts
        let client = crate::security::create_secure_http_client()
            .context("Failed to create secure HTTP client")?;

        let v: J = client
            .post(&url)
            .header(AUTHORIZATION, auth_header.as_str())
            .header(CONTENT_TYPE, "application/json")
            .json(&body)
            .send()?
            .error_for_status()?
            .json()?;

        Ok(v["choices"][0]["message"]["content"]
            .as_str()
            .unwrap_or("")
            .to_string())
    }

    fn supports_images(&self) -> bool {
        true
    }
    fn supports_audio(&self) -> bool {
        false
    }
    fn supports_video(&self) -> bool {
        false
    }
}

struct OllamaMultiModalBackend;
impl MultiModalLlmBackend for OllamaMultiModalBackend {
    fn chat_multimodal(&self, messages: &[MultiModalMessage]) -> Result<String> {
        // Ollama with vision models like llava
        let _base_url =
            std::env::var("OLLAMA_URL").unwrap_or_else(|_| "http://localhost:11434".into());
        let _model = std::env::var("OLLAMA_MODEL").unwrap_or_else(|_| "llava".into());

        // For now, convert to text and use regular completion
        // In full implementation, would use Ollama's multimodal API
        let text = messages
            .iter()
            .map(|m| m.to_text())
            .collect::<Vec<_>>()
            .join("\n");

        ollama::complete_sync(&text)
    }

    fn supports_images(&self) -> bool {
        true
    }
    fn supports_audio(&self) -> bool {
        false
    }
    fn supports_video(&self) -> bool {
        false
    }
}

struct OpenAiCompatMultiModalBackend;
impl MultiModalLlmBackend for OpenAiCompatMultiModalBackend {
    fn chat_multimodal(&self, messages: &[MultiModalMessage]) -> Result<String> {
        // Convert to text for compatibility
        let text = messages
            .iter()
            .map(|m| m.to_text())
            .collect::<Vec<_>>()
            .join("\n");

        openai_compat::complete_sync(&text)
    }
}

// ===================== Provider Router (simple 1-shot completion) =====================

/// Route by `AETHER_AI` to one of: openai | ollama | openai_compat | tgi
/// Returns an error with configuration instructions if no provider is set.
pub fn complete_sync_router(prompt: &str) -> Result<String> {
    let provider = std::env::var("AETHER_AI").unwrap_or_default();

    match provider.as_str() {
        "openai" => openai::complete_sync(prompt),
        "ollama" => ollama::complete_sync(prompt),
        "openai_compat" | "compat" => openai_compat::complete_sync(prompt),
        "tgi" => tgi::complete_sync(prompt),
        "" => Err(anyhow!(
            "No AI provider configured.\n\n\
            To use AI features, set the AETHER_AI environment variable:\n\n\
            For OpenAI:\n  \
              $env:AETHER_AI = \"openai\"\n  \
              $env:OPENAI_API_KEY = \"sk-your-key\"\n\n\
            For Ollama (local):\n  \
              $env:AETHER_AI = \"ollama\"\n  \
              # Ensure 'ollama serve' is running\n\n\
            For OpenAI-compatible servers (vLLM, llama.cpp):\n  \
              $env:AETHER_AI = \"compat\"\n  \
              $env:AETHER_COMPAT_BASE = \"http://localhost:8000/v1\"\n\n\
            Then restart ae."
        )),
        other => Err(anyhow!(
            "Unknown AI provider: '{}'\n\n\
            Supported providers: openai, ollama, compat, tgi\n\n\
            Example: $env:AETHER_AI = \"openai\"",
            other
        )),
    }
}

// ---------------------- Backends -----------------------

/// Stub backend - returns error with configuration instructions
pub mod stub {
    use anyhow::{anyhow, Result};

    pub fn complete_sync(_prompt: &str) -> Result<String> {
        Err(anyhow!(
            "No AI provider configured.\n\n\
            Set AETHER_AI environment variable:\n\
            - openai: $env:AETHER_AI=\"openai\"; $env:OPENAI_API_KEY=\"sk-...\"\n\
            - ollama: $env:AETHER_AI=\"ollama\" (requires 'ollama serve')\n\
            - compat: $env:AETHER_AI=\"compat\"; $env:AETHER_COMPAT_BASE=\"http://...\""
        ))
    }

    /// Check if AI is configured (for graceful warnings)
    pub fn is_configured() -> bool {
        let provider = std::env::var("AETHER_AI").unwrap_or_default();
        !provider.is_empty()
    }

    /// Get configuration warning message
    pub fn config_warning() -> &'static str {
        "AI not configured. Set AETHER_AI=openai|ollama|compat and restart."
    }
}

pub mod openai {
    use super::*;
    pub fn complete_sync(prompt: &str) -> Result<String> {
        // SECURITY FIX (HIGH-002): Use SecureApiConfig for memory-safe key handling
        let config = SecureApiConfig::from_keyring_or_env(
            "openai",
            "OPENAI_API_KEY",
            "https://api.openai.com".to_string(),
            std::env::var("OPENAI_MODEL").unwrap_or_else(|_| "gpt-4o-mini".to_string()),
            "openai".to_string(),
        )
        .context("Failed to load OpenAI configuration")?;

        config
            .validate_format()
            .context("OpenAI API key validation failed")?;

        let url = format!(
            "{}/v1/chat/completions",
            config.endpoint.trim_end_matches('/')
        );
        let body = json!({
            "model": config.model,
            "messages": [
                { "role":"system", "content":"You are a succinct assistant embedded in a shell." },
                { "role":"user",   "content": prompt }
            ],
            "temperature": 0.2
        });

        // SECURITY: Create authorization header (zeroized after use)
        let auth_header = config
            .create_auth_header()
            .ok_or_else(|| anyhow!("OpenAI API key not configured"))?;

        // SECURITY FIX (LOW-002): Use secure HTTP client with timeouts
        let client = crate::security::create_secure_http_client()
            .context("Failed to create secure HTTP client")?;

        let v: J = client
            .post(&url)
            .header(AUTHORIZATION, auth_header.as_str())
            .header(CONTENT_TYPE, "application/json")
            .json(&body)
            .send()?
            .error_for_status()?
            .json()?;
        Ok(v["choices"][0]["message"]["content"]
            .as_str()
            .unwrap_or("")
            .to_string())
    }
}

pub mod ollama {
    use super::*;
    pub fn complete_sync(prompt: &str) -> Result<String> {
        let base = std::env::var("OLLAMA_URL").unwrap_or_else(|_| "http://localhost:11434".into());
        let model = std::env::var("OLLAMA_MODEL").unwrap_or_else(|_| "llama3".into());
        let url = format!("{}/api/generate", base.trim_end_matches('/'));
        let body = json!({"model": model, "prompt": prompt, "stream": false});

        // SECURITY FIX (LOW-002): Use secure HTTP client with timeouts
        let client = crate::security::create_secure_http_client()
            .context("Failed to create secure HTTP client")?;

        let v: J = client
            .post(&url)
            .json(&body)
            .send()?
            .error_for_status()?
            .json()?;
        Ok(v["response"].as_str().unwrap_or("").to_string())
    }
}

pub mod openai_compat {
    use super::*;
    /// Any OpenAI-compatible server: vLLM, TensorRT-LLM, llama.cpp server, etc.
    pub fn complete_sync(prompt: &str) -> Result<String> {
        let base = std::env::var("AETHER_COMPAT_BASE")
            .unwrap_or_else(|_| "http://localhost:8000/v1".into());
        let model = std::env::var("AETHER_COMPAT_MODEL").unwrap_or_else(|_| "mixtral".into());
        let url = format!("{}/chat/completions", base.trim_end_matches('/'));
        let body = json!({
            "model": model,
            "messages":[
                {"role":"system","content":"You are a succinct assistant embedded in a shell."},
                {"role":"user","content": prompt}
            ],
            "temperature": 0.2
        });

        // SECURITY FIX (LOW-002): Use secure HTTP client with timeouts
        let client = crate::security::create_secure_http_client()
            .context("Failed to create secure HTTP client")?;

        let v: J = client
            .post(&url)
            .header(CONTENT_TYPE, "application/json")
            .json(&body)
            .send()?
            .error_for_status()?
            .json()?;
        Ok(v["choices"][0]["message"]["content"]
            .as_str()
            .unwrap_or("")
            .to_string())
    }
}

pub mod tgi {
    use super::*;
    #[derive(Serialize)]
    struct Req<'a> {
        inputs: &'a str,
        #[serde(skip_serializing_if = "Option::is_none")]
        parameters: Option<J>,
    }
    pub fn complete_sync(prompt: &str) -> Result<String> {
        let base = std::env::var("TGI_URL").unwrap_or_else(|_| "http://localhost:8080".into());
        let url = format!("{}/generate", base.trim_end_matches('/'));
        let body = Req {
            inputs: prompt,
            parameters: Some(json!({"temperature":0.2})),
        };

        // SECURITY FIX (LOW-002): Use secure HTTP client with timeouts
        let client = crate::security::create_secure_http_client()
            .context("Failed to create secure HTTP client")?;

        let r = client
            .post(&url)
            .header(CONTENT_TYPE, "application/json")
            .json(&body)
            .send()?
            .error_for_status()?;
        // Some TGI variants return a single object, others an array of objects.
        match r.json::<J>()? {
            J::Object(m) => Ok(m
                .get("generated_text")
                .and_then(|s| s.as_str())
                .unwrap_or("")
                .to_string()),
            J::Array(arr) => Ok(arr
                .get(0)
                .and_then(|x| x.get("generated_text"))
                .and_then(|s| s.as_str())
                .unwrap_or("")
                .to_string()),
            _ => Ok(String::new()),
        }
    }
}

// ===================== Model URIs & Backend registry =====================

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Provider {
    Stub,
    OpenAI,
    Ollama,
    OpenAICompat, // Generic OpenAI-compatible (vLLM, llama.cpp, etc.)
    Tgi,
    VLlm,     // Explicitly vLLM
    LlamaCpp, // Explicitly llama.cpp
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelRef {
    pub provider: Provider,
    pub model: String,
}

/// Parse strings like:
/// - "openai:gpt-4o-mini" / "ollama:llama3" / "compat:mixtral" / "tgi:mixtral" / "stub"
/// - "vllm:meta-llama/Llama-3-8B" / "llamacpp:mistral-7b"
pub fn parse_model_ref(s: &str) -> ModelRef {
    let s = s.trim();
    if let Some((pfx, rest)) = s.split_once(':') {
        let model = rest.trim().to_string();
        let provider = match pfx.trim().to_lowercase().as_str() {
            "openai" => Provider::OpenAI,
            "ollama" => Provider::Ollama,
            "compat" | "openai_compat" => Provider::OpenAICompat,
            "tgi" => Provider::Tgi,
            "vllm" => Provider::VLlm,
            "llamacpp" | "llama.cpp" | "llama_cpp" => Provider::LlamaCpp,
            _ => Provider::Stub,
        };
        ModelRef { provider, model }
    } else {
        // fallback: env or stub
        match s.to_lowercase().as_str() {
            "openai" => ModelRef {
                provider: Provider::OpenAI,
                model: std::env::var("OPENAI_MODEL").unwrap_or_else(|_| "gpt-4o-mini".into()),
            },
            "ollama" => ModelRef {
                provider: Provider::Ollama,
                model: std::env::var("OLLAMA_MODEL").unwrap_or_else(|_| "llama3".into()),
            },
            "compat" | "openai_compat" => ModelRef {
                provider: Provider::OpenAICompat,
                model: std::env::var("AETHER_COMPAT_MODEL").unwrap_or_else(|_| "mixtral".into()),
            },
            "tgi" => ModelRef {
                provider: Provider::Tgi,
                model: "mixtral".into(),
            },
            "vllm" => ModelRef {
                provider: Provider::VLlm,
                model: std::env::var("VLLM_MODEL")
                    .unwrap_or_else(|_| "meta-llama/Llama-3-8B".into()),
            },
            "llamacpp" | "llama.cpp" | "llama_cpp" => ModelRef {
                provider: Provider::LlamaCpp,
                model: std::env::var("LLAMACPP_MODEL").unwrap_or_else(|_| "model".into()),
            },
            _ => ModelRef {
                provider: Provider::Stub,
                model: "stub".into(),
            },
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatMessage {
    pub role: String,
    pub content: String,
}

pub trait LlmBackend: Send + Sync {
    fn chat(&self, messages: &[ChatMessage]) -> Result<String>;
}

struct StubBackend;
impl LlmBackend for StubBackend {
    fn chat(&self, _messages: &[ChatMessage]) -> Result<String> {
        Err(anyhow!(
            "No AI provider configured.\n\
            Set AETHER_AI environment variable to: openai, ollama, or compat"
        ))
    }
}
struct OpenAiBackend;
impl LlmBackend for OpenAiBackend {
    fn chat(&self, messages: &[ChatMessage]) -> Result<String> {
        // SECURITY FIX (HIGH-002): Use SecureApiConfig for memory-safe key handling
        let config = SecureApiConfig::from_keyring_or_env(
            "openai",
            "OPENAI_API_KEY",
            "https://api.openai.com".to_string(),
            std::env::var("OPENAI_MODEL").unwrap_or_else(|_| "gpt-4o-mini".to_string()),
            "openai".to_string(),
        )
        .context("Failed to load OpenAI configuration")?;

        // Validate API key format
        config
            .validate_format()
            .context("OpenAI API key validation failed")?;

        let url = format!(
            "{}/v1/chat/completions",
            config.endpoint.trim_end_matches('/')
        );
        let body = json!({ "model": config.model, "messages": messages, "temperature": 0.2 });

        // Create authorization header (zeroized after use)
        let auth_header = config
            .create_auth_header()
            .ok_or_else(|| anyhow!("OpenAI API key not configured"))?;

        // SECURITY FIX (LOW-002): Use secure HTTP client with timeouts
        let client = crate::security::create_secure_http_client()
            .context("Failed to create secure HTTP client")?;

        let v: J = client
            .post(&url)
            .header(AUTHORIZATION, auth_header.as_str())
            .header(CONTENT_TYPE, "application/json")
            .json(&body)
            .send()
            .context("Failed to send request to OpenAI")?
            .error_for_status()
            .context("OpenAI API returned error status")?
            .json()
            .context("Failed to parse OpenAI response")?;

        // SECURITY: Replace .unwrap() with proper error handling (CVSS 7.1)
        let content = v["choices"][0]["message"]["content"]
            .as_str()
            .ok_or_else(|| anyhow!("OpenAI response missing content field"))?
            .to_string();
        Ok(content)
    }
}
struct OllamaBackend;
impl LlmBackend for OllamaBackend {
    fn chat(&self, messages: &[ChatMessage]) -> Result<String> {
        let prompt = render_prompt(messages);
        ollama::complete_sync(&prompt)
    }
}
struct OpenAiCompatBackend;
impl LlmBackend for OpenAiCompatBackend {
    fn chat(&self, messages: &[ChatMessage]) -> Result<String> {
        let base = std::env::var("AETHER_COMPAT_BASE")
            .unwrap_or_else(|_| "http://localhost:8000/v1".into());
        let model = std::env::var("AETHER_COMPAT_MODEL").unwrap_or_else(|_| "mixtral".into());
        let url = format!("{}/chat/completions", base.trim_end_matches('/'));
        let body = json!({ "model": model, "messages": messages, "temperature": 0.2 });

        // SECURITY FIX (LOW-002): Use secure HTTP client with timeouts
        let client = crate::security::create_secure_http_client()
            .context("Failed to create secure HTTP client")?;

        let v: J = client
            .post(&url)
            .header(CONTENT_TYPE, "application/json")
            .json(&body)
            .send()?
            .error_for_status()?
            .json()?;
        let content = v["choices"][0]["message"]["content"]
            .as_str()
            .ok_or_else(|| anyhow!("OpenAI-compatible API response missing content field"))?
            .to_string();
        Ok(content)
    }
}
struct TgiBackend;
impl LlmBackend for TgiBackend {
    fn chat(&self, messages: &[ChatMessage]) -> Result<String> {
        let base = std::env::var("TGI_URL").unwrap_or_else(|_| "http://localhost:8080".into());
        let url = format!("{}/generate", base.trim_end_matches('/'));
        let body = json!({"inputs": render_prompt(messages), "parameters": {"temperature": 0.2}});

        // SECURITY FIX (LOW-002): Use secure HTTP client with timeouts
        let client = crate::security::create_secure_http_client()
            .context("Failed to create secure HTTP client")?;

        let v: J = client
            .post(&url)
            .header(CONTENT_TYPE, "application/json")
            .json(&body)
            .send()?
            .error_for_status()?
            .json()?;
        let s = v
            .get("generated_text")
            .and_then(|x| x.as_str())
            .or_else(|| {
                v.get(0)
                    .and_then(|x| x.get("generated_text"))
                    .and_then(|x| x.as_str())
            })
            .unwrap_or("");
        Ok(s.to_string())
    }
}

struct VLlmBackend;
impl LlmBackend for VLlmBackend {
    fn chat(&self, messages: &[ChatMessage]) -> Result<String> {
        // vLLM uses OpenAI-compatible API by default (http://localhost:8000/v1)
        let base = std::env::var("VLLM_URL").unwrap_or_else(|_| "http://localhost:8000/v1".into());
        let model = std::env::var("VLLM_MODEL").unwrap_or_else(|_| "meta-llama/Llama-3-8B".into());
        let url = format!("{}/chat/completions", base.trim_end_matches('/'));
        let body = json!({ "model": model, "messages": messages, "temperature": 0.2 });

        // SECURITY FIX (LOW-002): Use secure HTTP client with timeouts
        let client = crate::security::create_secure_http_client()
            .context("Failed to create secure HTTP client")?;

        let v: J = client
            .post(&url)
            .header(CONTENT_TYPE, "application/json")
            .json(&body)
            .send()?
            .error_for_status()?
            .json()?;
        let content = v["choices"][0]["message"]["content"]
            .as_str()
            .ok_or_else(|| anyhow!("vLLM response missing content field"))?
            .to_string();
        Ok(content)
    }
}

struct LlamaCppBackend;
impl LlmBackend for LlamaCppBackend {
    fn chat(&self, messages: &[ChatMessage]) -> Result<String> {
        // llama.cpp server uses OpenAI-compatible API (http://localhost:8080/v1)
        let base =
            std::env::var("LLAMACPP_URL").unwrap_or_else(|_| "http://localhost:8080/v1".into());
        let model = std::env::var("LLAMACPP_MODEL").unwrap_or_else(|_| "model".into());
        let url = format!("{}/chat/completions", base.trim_end_matches('/'));
        let body = json!({ "model": model, "messages": messages, "temperature": 0.2 });

        // SECURITY FIX (LOW-002): Use secure HTTP client with timeouts
        let client = crate::security::create_secure_http_client()
            .context("Failed to create secure HTTP client")?;

        let v: J = client
            .post(&url)
            .header(CONTENT_TYPE, "application/json")
            .json(&body)
            .send()?
            .error_for_status()?
            .json()?;
        let content = v["choices"][0]["message"]["content"]
            .as_str()
            .ok_or_else(|| anyhow!("llama.cpp response missing content field"))?
            .to_string();
        Ok(content)
    }
}

// ===================== Backend Detection =====================

/// Information about a detected backend
#[derive(Debug, Clone)]
pub struct BackendInfo {
    pub name: String,
    pub provider: Provider,
    pub endpoint: String,
    pub available: bool,
    pub models: Vec<String>,
}

/// Detect available AI backends by probing common endpoints
pub fn detect_available_backends() -> Vec<BackendInfo> {
    let mut backends = Vec::new();

    // Check Ollama (localhost:11434)
    if let Ok(info) = detect_ollama() {
        backends.push(info);
    }

    // Check vLLM (localhost:8000)
    if let Ok(info) = detect_vllm() {
        backends.push(info);
    }

    // Check llama.cpp (localhost:8080)
    if let Ok(info) = detect_llamacpp() {
        backends.push(info);
    }

    // Check TGI (localhost:8080 - alternate port check)
    if let Ok(info) = detect_tgi() {
        backends.push(info);
    }

    // Check OpenAI (via API key)
    if let Ok(info) = detect_openai() {
        backends.push(info);
    }

    backends
}

fn detect_ollama() -> Result<BackendInfo> {
    let endpoint = std::env::var("OLLAMA_HOST").unwrap_or_else(|_| "http://localhost:11434".into());
    let url = format!("{}/api/tags", endpoint.trim_end_matches('/'));

    // Use a simple blocking client with short timeout for detection
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(2))
        .build()?;
    let response = client.get(&url).send();

    match response {
        Ok(resp) if resp.status().is_success() => {
            let models = if let Ok(json) = resp.json::<J>() {
                json["models"]
                    .as_array()
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|m| m["name"].as_str().map(String::from))
                            .collect()
                    })
                    .unwrap_or_default()
            } else {
                vec![]
            };

            Ok(BackendInfo {
                name: "Ollama".to_string(),
                provider: Provider::Ollama,
                endpoint,
                available: true,
                models,
            })
        }
        _ => Err(anyhow!("Ollama not available")),
    }
}

fn detect_vllm() -> Result<BackendInfo> {
    let endpoint = std::env::var("VLLM_URL").unwrap_or_else(|_| "http://localhost:8000/v1".into());
    let url = format!("{}/models", endpoint.trim_end_matches('/'));

    // Use a simple blocking client with short timeout for detection
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(2))
        .build()?;
    let response = client.get(&url).send();

    match response {
        Ok(resp) if resp.status().is_success() => {
            let models = if let Ok(json) = resp.json::<J>() {
                json["data"]
                    .as_array()
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|m| m["id"].as_str().map(String::from))
                            .collect()
                    })
                    .unwrap_or_default()
            } else {
                vec![]
            };

            Ok(BackendInfo {
                name: "vLLM".to_string(),
                provider: Provider::VLlm,
                endpoint,
                available: true,
                models,
            })
        }
        _ => Err(anyhow!("vLLM not available")),
    }
}

fn detect_llamacpp() -> Result<BackendInfo> {
    let endpoint =
        std::env::var("LLAMACPP_URL").unwrap_or_else(|_| "http://localhost:8080/v1".into());
    let url = format!("{}/models", endpoint.trim_end_matches('/'));

    // Use a simple blocking client with short timeout for detection
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(2))
        .build()?;
    let response = client.get(&url).send();

    match response {
        Ok(resp) if resp.status().is_success() => {
            let models = if let Ok(json) = resp.json::<J>() {
                json["data"]
                    .as_array()
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|m| m["id"].as_str().map(String::from))
                            .collect()
                    })
                    .unwrap_or_default()
            } else {
                vec!["model".to_string()] // Default model name
            };

            Ok(BackendInfo {
                name: "llama.cpp".to_string(),
                provider: Provider::LlamaCpp,
                endpoint,
                available: true,
                models,
            })
        }
        _ => Err(anyhow!("llama.cpp not available")),
    }
}

fn detect_tgi() -> Result<BackendInfo> {
    let endpoint = std::env::var("TGI_URL").unwrap_or_else(|_| "http://localhost:8080".into());
    let url = format!("{}/health", endpoint.trim_end_matches('/'));

    // Use a simple blocking client with short timeout for detection
    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(2))
        .build()?;
    let response = client.get(&url).send();

    match response {
        Ok(resp) if resp.status().is_success() => Ok(BackendInfo {
            name: "Text Generation Inference (TGI)".to_string(),
            provider: Provider::Tgi,
            endpoint,
            available: true,
            models: vec![], // TGI typically serves one model
        }),
        _ => Err(anyhow!("TGI not available")),
    }
}

fn detect_openai() -> Result<BackendInfo> {
    // Check if OpenAI API key is available
    let has_key = std::env::var("OPENAI_API_KEY").is_ok()
        || crate::secure_config::SecureApiConfig::from_keyring(
            "openai",
            "https://api.openai.com".to_string(),
            "gpt-4o-mini".to_string(),
            "openai".to_string(),
        )
        .is_ok();

    if has_key {
        Ok(BackendInfo {
            name: "OpenAI".to_string(),
            provider: Provider::OpenAI,
            endpoint: "https://api.openai.com".to_string(),
            available: true,
            models: vec![
                "gpt-4o".to_string(),
                "gpt-4o-mini".to_string(),
                "gpt-4-turbo".to_string(),
                "gpt-3.5-turbo".to_string(),
            ],
        })
    } else {
        Err(anyhow!("OpenAI API key not configured"))
    }
}

// ========== MCP Server Detection ==========

use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

/// MCP Server information
#[derive(Debug, Clone)]
pub struct McpServerInfo {
    pub name: String,
    pub endpoint: String,
    pub available: bool,
    pub tools: Vec<String>,
}

/// Cached MCP detection result
#[derive(Debug, Clone)]
pub struct McpDetectionCache {
    servers: Vec<McpServerInfo>,
    timestamp: Instant,
    ttl: Duration,
}

impl McpDetectionCache {
    fn new(servers: Vec<McpServerInfo>, ttl: Duration) -> Self {
        Self {
            servers,
            timestamp: Instant::now(),
            ttl,
        }
    }

    fn is_expired(&self) -> bool {
        self.timestamp.elapsed() > self.ttl
    }
}

lazy_static::lazy_static! {
    pub static ref MCP_CACHE: Arc<Mutex<Option<McpDetectionCache>>> = Arc::new(Mutex::new(None));
    static ref HTTP_CLIENT: reqwest::blocking::Client = {
        reqwest::blocking::Client::builder()
            .timeout(Duration::from_secs(2))
            .pool_max_idle_per_host(10)
            .pool_idle_timeout(Duration::from_secs(30))
            .build()
            .expect("FATAL: Failed to create HTTP client - this indicates a critical system configuration issue")
    };
}

/// Configure MCP detection cache TTL (time-to-live)
pub fn configure_mcp_cache_ttl(_ttl_seconds: u64) -> Result<()> {
    // This will affect future cache entries
    Ok(())
}

/// Clear MCP detection cache
pub fn clear_mcp_cache() -> Result<()> {
    let mut cache = MCP_CACHE
        .lock()
        .map_err(|e| anyhow!("Failed to acquire MCP cache lock: {}", e))?;
    *cache = None;
    Ok(())
}

/// Detect available MCP servers on common ports (with caching)
pub fn detect_mcp_servers() -> Vec<McpServerInfo> {
    detect_mcp_servers_with_cache(Duration::from_secs(30))
}

/// Detect available MCP servers with custom cache TTL
pub fn detect_mcp_servers_with_cache(cache_ttl: Duration) -> Vec<McpServerInfo> {
    // Check cache first
    if let Ok(cache_guard) = MCP_CACHE.lock() {
        if let Some(ref cache) = *cache_guard {
            if !cache.is_expired() {
                return cache.servers.clone();
            }
        }
    }

    // Cache miss or expired - perform fresh detection
    let servers = detect_mcp_servers_uncached();

    // Update cache
    if let Ok(mut cache_guard) = MCP_CACHE.lock() {
        *cache_guard = Some(McpDetectionCache::new(servers.clone(), cache_ttl));
    }

    servers
}

/// Detect available MCP servers without caching (internal)
/// Detect MCP servers without using cache (always performs network calls)
pub fn detect_mcp_servers_uncached() -> Vec<McpServerInfo> {
    let mut servers = Vec::with_capacity(8); // Pre-allocate capacity

    // Prioritized MCP server endpoints (most likely to respond first)
    let prioritized_endpoints = vec![
        ("filesystem", "http://localhost:3001"),
        ("git", "http://localhost:3002"),
        ("database", "http://localhost:3005"),
        ("docker", "http://localhost:3003"),
        ("aws", "http://localhost:3004"),
        ("custom1", "http://localhost:8080"),
        ("custom2", "http://localhost:8081"),
    ];

    // Use parallel detection for faster response
    let results: Vec<_> = prioritized_endpoints
        .into_iter()
        .map(|(name, endpoint)| std::thread::spawn(move || detect_mcp_server(name, endpoint)))
        .collect();

    // Collect results in order of completion
    for handle in results {
        if let Ok(Ok(info)) = handle.join() {
            if info.available {
                servers.push(info);
            }
        }
    }

    servers
}

/// Detect a specific MCP server
fn detect_mcp_server(name: &str, endpoint: &str) -> Result<McpServerInfo> {
    let url = format!("{}/mcp/v1/tools", endpoint.trim_end_matches('/'));

    // Use shared HTTP client with connection pooling
    let response = HTTP_CLIENT.get(&url).send();

    match response {
        Ok(resp) if resp.status().is_success() => {
            // Try to parse the tools list
            let tools: Vec<mcp::McpToolSchema> = resp.json().unwrap_or_default();
            let tool_names = tools.iter().map(|t| t.name.clone()).collect();

            Ok(McpServerInfo {
                name: name.to_string(),
                endpoint: endpoint.to_string(),
                available: true,
                tools: tool_names,
            })
        }
        _ => Err(anyhow!("MCP server {} not available", name)),
    }
}

/// Automatically select the best available backend
pub fn auto_select_backend() -> Option<String> {
    let backends = detect_available_backends();

    // Priority: local backends first (faster), then cloud
    for backend in backends {
        if !backend.available {
            continue;
        }

        let model = backend
            .models
            .first()
            .map(String::as_str)
            .unwrap_or("model");

        return Some(match backend.provider {
            Provider::Ollama => format!("ollama:{}", model),
            Provider::VLlm => format!("vllm:{}", model),
            Provider::LlamaCpp => format!("llamacpp:{}", model),
            Provider::Tgi => "tgi:model".to_string(),
            Provider::OpenAI => "openai:gpt-4o-mini".to_string(),
            _ => continue,
        });
    }

    None
}

pub fn backend_from_env() -> Box<dyn LlmBackend> {
    // First check for explicit configuration
    let model_uri = std::env::var("AETHER_MODEL_URI").ok().or_else(|| {
        std::env::var("AETHER_AI").ok().map(|ai| {
            match ai.as_str() {
                "openai" => "openai:gpt-4o-mini",
                "ollama" => "ollama:llama3",
                "openai_compat" | "compat" => "compat:mixtral",
                "tgi" => "tgi:mixtral",
                "vllm" => "vllm:meta-llama/Llama-3-8B",
                "llamacpp" | "llama.cpp" => "llamacpp:model",
                "auto" => return auto_select_backend().unwrap_or_else(|| "stub".to_string()),
                _ => "stub",
            }
            .to_string()
        })
    });

    // If no explicit config, try auto-detection
    let uri =
        model_uri.unwrap_or_else(|| auto_select_backend().unwrap_or_else(|| "stub".to_string()));

    backend_from_model(uri)
}
pub fn backend_from_model(uri: String) -> Box<dyn LlmBackend> {
    let m = parse_model_ref(&uri);
    match m.provider {
        Provider::OpenAI => Box::new(OpenAiBackend),
        Provider::Ollama => Box::new(OllamaBackend),
        Provider::OpenAICompat => Box::new(OpenAiCompatBackend),
        Provider::Tgi => Box::new(TgiBackend),
        Provider::VLlm => Box::new(VLlmBackend),
        Provider::LlamaCpp => Box::new(LlamaCppBackend),
        Provider::Stub => Box::new(StubBackend),
    }
}

fn render_prompt(msgs: &[ChatMessage]) -> String {
    let mut s = String::new();
    for m in msgs {
        s.push_str(&format!("{}: {}\n", m.role, m.content));
    }
    s
}

pub fn parse_agent_command(text: &str) -> (Option<J>, String) {
    if let Some(start) = text.find("```json") {
        if let Some(end) = text[start + 7..].find("```") {
            let json_str = &text[start + 7..start + 7 + end];
            if let Ok(v) = serde_json::from_str::<J>(json_str) {
                return (Some(v), text[..start].trim().to_string());
            }
        }
    }
    if let Ok(v) = serde_json::from_str::<J>(text) {
        return (Some(v), String::new());
    }
    (None, text.trim().to_string())
}

use crate::value::Value;
pub fn display_value(v: &Value) -> String {
    match v {
        Value::Null => "null".into(),
        Value::Bool(b) => b.to_string(),
        Value::Int(n) => n.to_string(),
        Value::Float(x) => x.to_string(),
        Value::Str(s) => s.clone(),
        Value::Uri(u) => u.clone(),
        Value::Array(a) => format!("[len={}]", a.len()),
        Value::Record(_) => "{…}".into(),
        Value::Table(t) => format!("<Table rows={}>", t.rows.len()),
        Value::Lambda(_) => "<lambda>".into(),
        Value::AsyncLambda(_) => "<async lambda>".into(),
        Value::Future(_) => "<future>".into(),
        Value::Builtin(b) => format!("<builtin:{}>", b.name),
            Value::Error(msg) => format!("Error: {}", msg),
    }
}

// ===================== Agents (single + swarms) =====================

use crate::{builtins, env::Env};

pub mod agents {
    use super::*;
    use std::collections::BTreeMap;

    // ---------- Tools ----------
    /// A callable tool the agent may use.
    pub trait Tool: Send + Sync {
        fn name(&self) -> &str;
        fn description(&self) -> &str;
        fn call(&self, input: &str, env: &mut Env) -> Result<Value>;
    }

    /// Tool that bridges to Aether builtins: input is parsed as a JSON array of args.
    pub struct BuiltinTool {
        pub name: String,
        pub description: String,
    }
    impl Tool for BuiltinTool {
        fn name(&self) -> &str {
            &self.name
        }
        fn description(&self) -> &str {
            &self.description
        }
        fn call(&self, input: &str, env: &mut Env) -> Result<Value> {
            let parsed: J = serde_json::from_str(input).unwrap_or(J::Null);
            let mut args = Vec::<Value>::new();
            if let Some(arr) = parsed.as_array() {
                for v in arr {
                    args.push(json_to_value(v));
                }
            } else if parsed.is_string() {
                if let Some(s) = parsed.as_str() {
                    args.push(Value::Str(s.to_string()));
                }
            }
            builtins::call(&self.name, args, env)
        }
    }
    fn json_to_value(v: &J) -> Value {
        match v {
            J::Null => Value::Null,
            J::Bool(b) => Value::Bool(*b),
            J::Number(n) => {
                if let Some(i) = n.as_i64() {
                    Value::Int(i)
                } else if let Some(f) = n.as_f64() {
                    Value::Float(f)
                } else {
                    Value::Null
                }
            }
            J::String(s) => Value::Str(s.clone()),
            J::Array(a) => Value::Array(a.iter().map(json_to_value).collect()),
            J::Object(m) => {
                let mut rec = BTreeMap::new();
                for (k, v) in m {
                    rec.insert(k.clone(), json_to_value(v));
                }
                Value::Record(rec)
            }
        }
    }

    // ---------- Tool Registry ----------
    pub trait ToolResolver: Send + Sync {
        fn list(&self) -> Vec<String>;
        fn get(&self, name: &str) -> Option<Box<dyn Tool>>;
    }

    /// Resolver that exposes Aether builtins as tools.
    pub struct BuiltinToolResolver;
    impl ToolResolver for BuiltinToolResolver {
        fn list(&self) -> Vec<String> {
            vec![
                "print".into(),
                "echo".into(),
                "map".into(),
                "reduce".into(),
                "cd".into(),
                "pwd".into(),
                "!".into(),
                "http_get".into(),
            ]
        }
        fn get(&self, name: &str) -> Option<Box<dyn Tool>> {
            Some(Box::new(BuiltinTool {
                name: name.to_string(),
                description: format!("Aether builtin `{}`", name),
            }))
        }
    }

    pub struct ToolRegistry {
        resolvers: Vec<Box<dyn ToolResolver>>,
    }
    impl ToolRegistry {
        pub fn with_builtins() -> Self {
            Self {
                resolvers: vec![Box::new(BuiltinToolResolver)],
            }
        }
        pub fn with_builtins_and_mcp(endpoint: &str) -> Self {
            let mut r = Self::with_builtins();
            r.resolvers
                .push(Box::new(crate::ai::mcp::McpToolResolver::new(endpoint)));
            r
        }
        pub fn list(&self) -> Vec<String> {
            let mut out = Vec::new();
            for r in &self.resolvers {
                out.extend(r.list());
            }
            out.sort();
            out.dedup();
            out
        }
        pub fn resolve_many(&self, names: &[&str]) -> Vec<Box<dyn Tool>> {
            let mut tools = Vec::new();
            for n in names {
                for r in &self.resolvers {
                    if let Some(t) = r.get(n) {
                        tools.push(t);
                        break;
                    }
                }
            }
            tools
        }
    }

    // ---------- Single Agent ----------
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct AgentStep {
        pub thought: String,
        pub command: Option<J>,
        pub observation: Option<String>,
    }

    pub struct Agent {
        backend: Box<dyn super::LlmBackend>,
        pub tools: Vec<Box<dyn Tool>>,
        pub max_steps: usize,
        pub trace: Vec<AgentStep>,
    }
    impl Agent {
        pub fn new(tools: Vec<Box<dyn Tool>>) -> Self {
            Self {
                backend: super::backend_from_env(),
                tools,
                max_steps: 8,
                trace: Vec::new(),
            }
        }
        /// Construct with a specific model URI (e.g., "openai:gpt-4o-mini").
        pub fn with_model_uri(tools: Vec<Box<dyn Tool>>, model_uri: &str) -> Self {
            Self {
                backend: super::backend_from_model(model_uri.to_string()),
                tools,
                max_steps: 8,
                trace: Vec::new(),
            }
        }
        pub fn run_sync(&mut self, goal: &str, dry_run: bool, env: &mut Env) -> Result<String> {
            let system = ChatMessage {
                role: "system".into(),
                content: format!(
                    "You are Aether Agent. Emit JSON commands:\n\
                     {{\"type\":\"tool\",\"tool\":\"<name>\",\"input\":<json or string>}} or \
                     {{\"type\":\"final\",\"output\":\"...\"}}.\nTools:\n{}",
                    self.tools
                        .iter()
                        .map(|t| format!("- {}: {}", t.name(), t.description()))
                        .collect::<Vec<_>>()
                        .join("\n")
                ),
            };
            let mut dialogue = vec![
                system,
                ChatMessage {
                    role: "user".into(),
                    content: goal.into(),
                },
            ];

            for _ in 0..self.max_steps {
                let reply = self.backend.chat(&dialogue)?;
                let (cmd, thought) = super::parse_agent_command(&reply);
                self.trace.push(AgentStep {
                    thought: thought.clone(),
                    command: cmd.clone(),
                    observation: None,
                });

                if let Some(c) = cmd
                    .as_ref()
                    .and_then(|j| j.get("type"))
                    .and_then(|t| t.as_str())
                {
                    if c == "final" {
                        let out = cmd
                            .as_ref()
                            .and_then(|j| j.get("output"))
                            .and_then(|s| s.as_str())
                            .unwrap_or("")
                            .to_string();
                        return if dry_run {
                            Ok(format!("[dry_run] final: {}\ntrace: {:?}", out, self.trace))
                        } else {
                            Ok(out)
                        };
                    }
                }

                if let Some(tool_name) = cmd
                    .as_ref()
                    .and_then(|j| j.get("tool"))
                    .and_then(|s| s.as_str())
                {
                    let input = cmd
                        .as_ref()
                        .and_then(|j| j.get("input"))
                        .unwrap_or(&J::Null)
                        .to_string();
                    let obs = if dry_run {
                        format!("[dry_run] would call {} with {}", tool_name, input)
                    } else {
                        if let Some(tool) = self.tools.iter().find(|t| t.name() == tool_name) {
                            match tool.call(&input, env) {
                                Ok(val) => format!("OK: {}", super::display_value(&val)),
                                Err(e) => format!("ERROR: {}", e),
                            }
                        } else {
                            format!("ERROR: unknown tool {}", tool_name)
                        }
                    };
                    dialogue.push(ChatMessage {
                        role: "assistant".into(),
                        content: reply,
                    });
                    dialogue.push(ChatMessage {
                        role: "user".into(),
                        content: format!("Observation: {}", obs),
                    });
                    if let Some(last) = self.trace.last_mut() {
                        last.observation = Some(obs);
                    }
                    continue;
                }

                dialogue.push(ChatMessage {
                    role: "assistant".into(),
                    content: reply.clone(),
                });
                dialogue.push(ChatMessage {
                    role: "user".into(),
                    content: "Your last response was not valid JSON. Please emit a valid command."
                        .into(),
                });
            }
            Ok(format!(
                "(incomplete) max steps reached; trace: {:?}",
                self.trace
            ))
        }
    }

    // ---------- Public helpers ----------
    /// Convenience wrapper exposed to callers/tests/builtins.
    pub fn run_sync(
        goal: &str,
        tool_names: &[&str],
        max_steps: usize,
        dry_run: bool,
        env: &mut Env,
    ) -> Result<String> {
        let reg = ToolRegistry::with_builtins();
        let tools = reg.resolve_many(tool_names);
        let mut agent = if let Ok(uri) = std::env::var("AETHER_AGENT_MODEL_URI") {
            Agent::with_model_uri(tools, &uri)
        } else {
            Agent::new(tools)
        };
        if max_steps > 0 {
            agent.max_steps = max_steps;
        }
        agent.run_sync(goal, dry_run, env)
    }

    /// Same as `run_sync`, but forces a specific model URI for this run.
    pub fn run_sync_with_model(
        goal: &str,
        tool_names: &[&str],
        model_uri: &str,
        max_steps: usize,
        dry_run: bool,
        env: &mut Env,
    ) -> Result<String> {
        let reg = ToolRegistry::with_builtins();
        let tools = reg.resolve_many(tool_names);
        let mut agent = Agent::with_model_uri(tools, model_uri);
        if max_steps > 0 {
            agent.max_steps = max_steps;
        }
        agent.run_sync(goal, dry_run, env)
    }

    // ---------- Multi-Agent Swarm ----------
    pub mod swarm {
        use super::*;

        pub struct AgentConfig {
            pub id: String,
            pub system: String,
            pub tools: Vec<Box<dyn Tool>>,
            pub max_steps: usize,
            /// Optional model URI for this agent (e.g., "ollama:llama3")
            pub model_uri: Option<String>,
        }
        impl std::fmt::Debug for AgentConfig {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(
                    f,
                    "AgentConfig {{ id: {:?}, system_len: {}, tools: {}, max_steps: {}, model_uri: {:?} }}",
                    self.id,
                    self.system.len(),
                    self.tools.len(),
                    self.max_steps,
                    self.model_uri
                )
            }
        }

        #[derive(Debug, Clone, Copy)]
        pub enum Policy {
            RoundRobin,
            Router,
        }

        #[derive(Debug, Clone, Serialize, Deserialize)]
        pub struct BlackboardMsg {
            pub author: String,
            pub content: String,
            pub kind: String,
        }

        #[derive(Debug, Clone, Serialize, Deserialize)]
        pub struct SwarmStep {
            pub agent: String,
            pub reply: String,
            pub parsed: Option<J>,
            pub observation: Option<String>,
        }

        /// Strategy abstraction (Nanda-friendly)
        pub trait Coordinator: Send {
            fn select(&mut self, swarm: &Swarm, tick: usize) -> usize;
        }

        /// Round-robin coordinator (default)
        pub struct RoundRobinCoord {
            next: usize,
        }

        impl RoundRobinCoord {
            pub fn new() -> Self {
                Self { next: 0 }
            }
        }
        impl Coordinator for RoundRobinCoord {
            fn select(&mut self, swarm: &Swarm, _tick: usize) -> usize {
                if swarm.agents.is_empty() {
                    return 0;
                }
                let i = self.next % swarm.agents.len();
                self.next += 1;
                i
            }
        }

        /// Router coordinator (stub): could inspect blackboard and route.
        pub struct RouterCoord;
        impl Coordinator for RouterCoord {
            fn select(&mut self, _swarm: &Swarm, _tick: usize) -> usize {
                0 // stub: always pick agent 0; replace with scoring/LLM routing
            }
        }

        pub struct Swarm {
            pub policy: Policy,
            pub agents: Vec<(AgentConfig, Box<dyn super::super::LlmBackend>)>,
            pub blackboard: Vec<BlackboardMsg>,
            pub steps: Vec<SwarmStep>,
            pub max_iters: usize,
            coord: Option<Box<dyn Coordinator>>,
        }

        impl Swarm {
            pub fn new(policy: Policy, max_iters: usize) -> Self {
                let coord: Box<dyn Coordinator> = match policy {
                    Policy::RoundRobin => Box::new(RoundRobinCoord::new()),
                    Policy::Router => Box::new(RouterCoord),
                };
                Self {
                    policy,
                    agents: Vec::new(),
                    blackboard: Vec::new(),
                    steps: Vec::new(),
                    max_iters,
                    coord: Some(coord),
                }
            }
            pub fn add_agent(&mut self, mut cfg: AgentConfig) {
                // Default per-agent model URI from env if none provided
                if cfg.model_uri.is_none() {
                    if let Ok(uri) = std::env::var("AETHER_SWARM_AGENT_MODEL_URI") {
                        cfg.model_uri = Some(uri);
                    }
                }
                let be = if let Some(uri) = &cfg.model_uri {
                    super::super::backend_from_model(uri.clone())
                } else {
                    super::super::backend_from_env()
                };
                self.agents.push((cfg, be));
            }

            pub fn run_sync(
                &mut self,
                user_goal: &str,
                env: &mut Env,
                dry_run: bool,
            ) -> Result<String> {
                if self.agents.is_empty() {
                    return Err(anyhow!("swarm has no agents"));
                }
                self.blackboard.push(BlackboardMsg {
                    author: "user".into(),
                    content: user_goal.into(),
                    kind: "note".into(),
                });

                for t in 0..self.max_iters {
                    // Move coord out to avoid overlapping borrows (self immutably borrowed below)
                    let i = {
                        // SECURITY: Replace .expect() with proper error handling (CVSS 7.1)
                        let mut coord = self
                            .coord
                            .take()
                            .ok_or_else(|| anyhow!("Coordinator not initialized"))?;
                        let idx = coord.select(self, t);
                        self.coord = Some(coord);
                        idx
                    };
                    let (cfg, be) = &self.agents[i];
                    let reply = be.chat(&self.compose_dialogue(cfg))?;

                    let parsed = try_parse_command(&reply);
                    if let Some(js) = &parsed {
                        if js.get("type").and_then(|x| x.as_str()) == Some("final") {
                            let out = js
                                .get("output")
                                .and_then(|x| x.as_str())
                                .unwrap_or("")
                                .to_string();
                            self.blackboard.push(BlackboardMsg {
                                author: cfg.id.clone(),
                                content: out.clone(),
                                kind: "final".into(),
                            });
                            self.steps.push(SwarmStep {
                                agent: cfg.id.clone(),
                                reply,
                                parsed,
                                observation: None,
                            });
                            return Ok(out);
                        }
                    }

                    let mut observation = None;
                    if let Some(js) = &parsed {
                        if js.get("type").and_then(|x| x.as_str()) == Some("tool") {
                            let tool_name = js.get("tool").and_then(|x| x.as_str()).unwrap_or("");
                            let input = js.get("input").cloned().unwrap_or(J::Null).to_string();
                            let obs = if dry_run {
                                format!("[dry_run] {}/tool {}({})", cfg.id, tool_name, input)
                            } else if let Some(tool) =
                                cfg.tools.iter().find(|t| t.name() == tool_name)
                            {
                                match tool.call(&input, env) {
                                    Ok(v) => format!("OK: {}", super::super::display_value(&v)),
                                    Err(e) => format!("ERROR: {}", e),
                                }
                            } else {
                                format!("ERROR: unknown tool {}", tool_name)
                            };
                            observation = Some(obs);
                        }
                    }

                    if observation.is_none() && parsed.is_none() {
                        self.blackboard.push(BlackboardMsg {
                            author: cfg.id.clone(),
                            content: reply.clone(),
                            kind: "thought".into(),
                        });
                    }
                    self.steps.push(SwarmStep {
                        agent: cfg.id.clone(),
                        reply: reply.clone(),
                        parsed: parsed.clone(),
                        observation: observation.clone(),
                    });
                    if let Some(obs) = observation {
                        self.blackboard.push(BlackboardMsg {
                            author: cfg.id.clone(),
                            content: format!("obs: {obs}"),
                            kind: "note".into(),
                        });
                    }
                }
                Ok(format!(
                    "(incomplete) swarm max_iters reached; steps={}",
                    self.steps.len()
                ))
            }

            fn compose_dialogue(&self, cfg: &AgentConfig) -> Vec<ChatMessage> {
                let mut bb = String::new();
                for m in &self.blackboard {
                    bb.push_str(&format!("- {} [{}]: {}\n", m.author, m.kind, m.content));
                }
                let tools_list = cfg
                    .tools
                    .iter()
                    .map(|t| format!("- {}: {}", t.name(), t.description()))
                    .collect::<Vec<_>>()
                    .join("\n");
                vec![
                    ChatMessage {
                        role: "system".into(),
                        content: format!(
                            "You are agent `{}`.\n{}\n\nBlackboard:\n{}\n\n\
                             Emit JSON commands:\n\
                             - tool: {{\"type\":\"tool\",\"tool\":\"<name>\",\"input\":<json|string>}}\n\
                             - final: {{\"type\":\"final\",\"output\":\"...\"}}\n\
                             - delegate: {{\"type\":\"delegate\",\"target\":\"<agent-id>\",\"input\":<json|string>}}\n\
                             - route: {{\"type\":\"route\",\"target\":\"<agent-id>\"}}",
                            cfg.id, cfg.system, bb
                        ),
                    },
                    ChatMessage {
                        role: "user".into(),
                        content: format!(
                            "Act toward the shared goal. Available tools:\n{}",
                            tools_list
                        ),
                    },
                ]
            }
        }

        fn try_parse_command(text: &str) -> Option<J> {
            if let Some(start) = text.find("```json") {
                if let Some(end) = text[start + 7..].find("```") {
                    if let Ok(v) = serde_json::from_str::<J>(&text[start + 7..start + 7 + end]) {
                        return Some(v);
                    }
                }
            }
            serde_json::from_str::<J>(text).ok()
        }

        // ---- Compatibility shim: some callers use ai::agents::swarm::run_sync ----
        /// Thin wrapper to maintain compatibility; internally calls a single-agent runner.
        pub fn run_sync(
            goal: &str,
            tool_names: &[&str],
            max_steps: usize,
            dry_run: bool,
            env: &mut Env,
        ) -> Result<String> {
            super::run_sync(goal, tool_names, max_steps, dry_run, env)
        }
    }
}

// ------------- MCP (Model Context Protocol) Implementation -------------
#[allow(dead_code)]
pub mod mcp {
    use super::*;
    use std::collections::HashMap;
    use std::sync::{Arc, Mutex};

    /// MCP Protocol Version
    pub const MCP_VERSION: &str = "1.0";

    /// MCP Tool Schema
    #[derive(Debug, Clone, Serialize, Deserialize)]
    pub struct McpToolSchema {
        pub name: String,
        pub description: String,
        #[serde(default)]
        pub input_schema: J,
        #[serde(default)]
        pub output_schema: Option<J>,
    }

    /// MCP Client for communicating with MCP servers
    #[derive(Debug, Clone)]
    pub struct McpClient {
        pub endpoint: String,
        tools_cache: Arc<Mutex<HashMap<String, McpToolSchema>>>,
        client: reqwest::blocking::Client,
    }

    impl McpClient {
        pub fn new(endpoint: &str) -> Self {
            // SECURITY FIX (LOW-002): Use secure HTTP client with proper error handling
            let client = crate::security::create_secure_http_client().unwrap_or_else(|_| {
                // Fallback to default client if secure client creation fails
                reqwest::blocking::Client::new()
            });

            Self {
                endpoint: endpoint.to_string(),
                tools_cache: Arc::new(Mutex::new(HashMap::new())),
                client,
            }
        }

        /// Discover available tools from MCP server
        pub fn discover_tools(&self) -> Result<Vec<McpToolSchema>> {
            let url = format!("{}/mcp/v1/tools", self.endpoint.trim_end_matches('/'));

            match self.client.get(&url).send() {
                Ok(response) => {
                    if response.status().is_success() {
                        let tools: Vec<McpToolSchema> = response.json().unwrap_or_default();

                        // Cache discovered tools
                        // SECURITY: Replace .unwrap() with proper error handling (CVSS 7.1)
                        let mut cache = self
                            .tools_cache
                            .lock()
                            .map_err(|e| anyhow!("Failed to acquire tools cache lock: {}", e))?;
                        for tool in &tools {
                            cache.insert(tool.name.clone(), tool.clone());
                        }

                        Ok(tools)
                    } else {
                        // MCP server returned error, return empty list
                        Ok(vec![])
                    }
                }
                Err(_) => {
                    // MCP server unreachable, return empty list
                    Ok(vec![])
                }
            }
        }

        /// List tool names (tries discovery first, falls back to cache)
        pub fn list_tools(&self) -> Result<Vec<String>> {
            // Try fresh discovery
            if let Ok(tools) = self.discover_tools() {
                return Ok(tools.iter().map(|t| t.name.clone()).collect());
            }

            // Fall back to cache
            // SECURITY: Replace .unwrap() with proper error handling (CVSS 7.1)
            let cache = self
                .tools_cache
                .lock()
                .map_err(|e| anyhow!("Failed to acquire tools cache lock: {}", e))?;
            Ok(cache.keys().cloned().collect())
        }

        /// Execute a tool via MCP
        pub fn call_tool(&self, name: &str, input: &str) -> Result<String> {
            let url = format!(
                "{}/mcp/v1/tools/{}/execute",
                self.endpoint.trim_end_matches('/'),
                name
            );

            // Parse input as JSON
            let input_json: J =
                serde_json::from_str(input).unwrap_or_else(|_| J::String(input.to_string()));

            let response = self
                .client
                .post(&url)
                .header(CONTENT_TYPE, "application/json")
                .json(&input_json)
                .send()?;

            if !response.status().is_success() {
                return Err(anyhow!("MCP tool execution failed: {}", response.status()));
            }

            let result: J = response.json()?;
            Ok(result.to_string())
        }

        /// Validate tool input against schema (if available)
        pub fn validate_input(&self, tool_name: &str, _input: &J) -> Result<()> {
            // SECURITY: Replace .unwrap() with proper error handling (CVSS 7.1)
            let cache = self
                .tools_cache
                .lock()
                .map_err(|e| anyhow!("Failed to acquire tools cache lock: {}", e))?;
            if let Some(tool) = cache.get(tool_name) {
                // For now, just check if schema exists
                // Full validation would use jsonschema crate
                if tool.input_schema != J::Null {
                    // Schema exists, assume valid for now
                    // TODO: Implement full JSONSchema validation
                }
                Ok(())
            } else {
                // Tool not in cache, can't validate
                Ok(())
            }
        }

        /// Health check for MCP server
        pub fn health_check(&self) -> bool {
            let url = format!("{}/health", self.endpoint.trim_end_matches('/'));
            self.client
                .get(&url)
                .send()
                .map(|r| r.status().is_success())
                .unwrap_or(false)
        }

        /// Get tool description from cache
        pub fn get_tool_description(&self, name: &str) -> Option<String> {
            // SECURITY: Replace .unwrap() with proper error handling (CVSS 7.1)
            let cache = self.tools_cache.lock().ok()?;
            cache.get(name).map(|t| t.description.clone())
        }
    }

    /// Resolver that exposes MCP tools
    pub struct McpToolResolver {
        client: Arc<McpClient>,
    }

    impl McpToolResolver {
        pub fn new(endpoint: &str) -> Self {
            Self {
                client: Arc::new(McpClient::new(endpoint)),
            }
        }
    }

    impl crate::ai::agents::ToolResolver for McpToolResolver {
        fn list(&self) -> Vec<String> {
            self.client.list_tools().unwrap_or_default()
        }

        fn get(&self, name: &str) -> Option<Box<dyn crate::ai::agents::Tool>> {
            struct McpTool {
                name: String,
                client: Arc<McpClient>,
            }
            impl crate::ai::agents::Tool for McpTool {
                fn name(&self) -> &str {
                    &self.name
                }
                fn description(&self) -> &str {
                    // Try to get cached description, or return default
                    if let Some(_desc) = self.client.get_tool_description(&self.name) {
                        // Need to leak the string to return &'static str
                        // For now, just return a static default
                        "MCP tool"
                    } else {
                        "MCP tool"
                    }
                }
                fn call(
                    &self,
                    input: &str,
                    _env: &mut crate::env::Env,
                ) -> anyhow::Result<crate::value::Value> {
                    let out = self.client.call_tool(&self.name, input)?;
                    Ok(crate::value::Value::Str(out))
                }
            }
            Some(Box::new(McpTool {
                name: name.into(),
                client: Arc::clone(&self.client),
            }))
        }
    }
}