faucet-cli 1.13.1

Config-driven CLI runner for faucet-stream pipelines (YAML / JSON, Meltano-style)
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
//! MCP tool definitions + in-process dispatch (issue #420).
//!
//! Every tool is a thin shape-adapter over an existing faucet capability —
//! `registry` (list / schema), `init_template` (scaffold), `expand` /
//! `topology` (validate), `preview`, and `run_from_yaml_str` (the one gated
//! mutating tool). No pipeline logic is re-implemented here.

use super::McpContext;
use crate::config::PipelineConfig;
use crate::mcp::protocol::{ToolDef, tool_error, tool_text};
use serde_json::{Value, json};

/// Hard cap on `preview` rows regardless of the requested limit — an MCP
/// client must never trigger a full extract of a large source.
const PREVIEW_MAX: usize = 100;

/// Build the advertised tool list for `tools/list`, honoring the mutation gate.
pub fn tool_defs(ctx: &McpContext) -> Vec<ToolDef> {
    let mut defs = vec![
        ToolDef {
            name: "list_connectors",
            description: "List all compiled-in sources, sinks, transforms, and state stores, each with a one-line description and (for connectors) a conformance tier.",
            input_schema: json!({
                "type": "object",
                "properties": {
                    "kind": { "type": "string", "enum": ["source", "sink", "transform", "state", "all"], "description": "Filter to one category (default: all)." }
                }
            }),
        },
        ToolDef {
            name: "get_connector_schema",
            description: "Return the JSON Schema for a connector or transform's config block.",
            input_schema: json!({
                "type": "object",
                "properties": {
                    "kind": { "type": "string", "enum": ["source", "sink", "transform"] },
                    "name": { "type": "string", "description": "Connector/transform name, e.g. 'rest' or 'keys_case'." }
                },
                "required": ["kind", "name"]
            }),
        },
        ToolDef {
            name: "scaffold_config",
            description: "Generate a commented YAML pipeline skeleton for a source→sink pair (read-only: returns text, writes nothing).",
            input_schema: json!({
                "type": "object",
                "properties": {
                    "source": { "type": "string", "description": "Source connector kind." },
                    "sink": { "type": "string", "description": "Sink connector kind." },
                    "name": { "type": "string", "description": "Optional pipeline name." }
                },
                "required": ["source", "sink"]
            }),
        },
    ];
    // `validate_config` and `preview` act on a caller-supplied config: they
    // resolve `${env:}`/`${file:}`/`${secret:}` server-side and (for `preview`)
    // build the described connector and return its records. That is a strictly
    // higher capability than schema introspection, so it is separately gated
    // (#456 C4) and not advertised when the caller may not use it.
    if ctx.allow_config_execution {
        defs.push(ToolDef {
            name: "validate_config",
            description: "Fully validate a pipeline YAML/JSON config (structure, templates, matrix/topology graph). Returns a per-node report or the validation error.",
            input_schema: json!({
                "type": "object",
                "properties": {
                    "config": { "type": "string", "description": "The pipeline config document (YAML or JSON)." }
                },
                "required": ["config"]
            }),
        });
        defs.push(ToolDef {
            name: "preview",
            description: "Fetch a bounded sample of records from a config's first source (source side only; downstream sinks are not run). Capped at 100 rows.",
            input_schema: json!({
                "type": "object",
                "properties": {
                    "config": { "type": "string", "description": "The pipeline config document (YAML or JSON)." },
                    "limit": { "type": "integer", "description": "Max rows to return (1–100, default 10)." }
                },
                "required": ["config"]
            }),
        });
    }
    if ctx.allow_mutations {
        defs.push(ToolDef {
            name: "run_pipeline",
            description: "Run a pipeline from an inline config. MUTATING — gated behind --allow-mutations. Pass dry_run:true to validate+preview only.",
            input_schema: json!({
                "type": "object",
                "properties": {
                    "config": { "type": "string" },
                    "dry_run": { "type": "boolean", "description": "If true, validate + preview only; do not write to any sink." },
                    "selection": { "type": "object", "description": "Run only some matrix rows / streams: { select: [ids], only: [globs], skip: [ids|globs], tags: [..], status: [available|draft|archived], include_parents: off|eligible|all }. Omit to run every row." }
                },
                "required": ["config"]
            }),
        });
    }
    #[cfg(feature = "templates")]
    if ctx.templates.is_some() {
        defs.push(ToolDef {
            name: "list_templates",
            description: "List registered pipeline templates (newest version of each, plus its release status) with the typed params each one takes.",
            input_schema: json!({ "type": "object", "properties": {} }),
        });
        defs.push(ToolDef {
            name: "get_template",
            description: "Show one registered pipeline template: its declared params, stored config body, and available versions.",
            input_schema: json!({
                "type": "object",
                "properties": {
                    "id": { "type": "string", "description": "Template id." },
                    "version": { "description": "Version: a number, or a named channel. Derived: \"stable\" (the launched version — the default), \"previous\", \"newest\". Assignable: \"dev\", \"test\", \"staging\", \"pre-prod\", \"canary\", \"prod\". Note \"latest\" is deliberately not a channel — use \"stable\" for the current release or \"newest\" for the highest version number.", "oneOf": [{ "type": "integer" }, { "type": "string" }] }
                },
                "required": ["id"]
            }),
        });
        defs.push(ToolDef {
            name: "list_template_rows",
            description: "List a registered template's selectable rows — the streams of a source template (write resolution and guarantees against `sink` when given) or the matrix rows of a pipeline — with status, tags, hierarchy, read/write facts, the params each uses, and the last run's state. With `selection`, also resolve what a run with it would execute (run_set, pulled-in ancestors, blocked rows, the error it would return).",
            input_schema: json!({
                "type": "object",
                "properties": {
                    "id": { "type": "string" },
                    "version": { "description": "Version: a number or a channel. Default \"stable\".", "oneOf": [{ "type": "integer" }, { "type": "string" }] },
                    "sink": { "type": "string", "description": "For a source template: the registered sink template to resolve against." },
                    "sink_version": { "oneOf": [{ "type": "integer" }, { "type": "string" }] },
                    "selection": { "type": "object", "description": "Run only some matrix rows / streams: { select: [ids], only: [globs], skip: [ids|globs], tags: [..], status: [available|draft|archived], include_parents: off|eligible|all }. Omit to run every row." }
                },
                "required": ["id"]
            }),
        });
        if ctx.allow_mutations && ctx.allow_template_admin {
            defs.push(ToolDef {
                name: "register_template",
                description: "Register a template document as a new version: `kind: source-template` (a system + its streams), `kind: sink-template` (a destination), or `kind: pipeline` (a complete config). Params are declared with `params:`. MUTATING — gated behind --allow-mutations.",
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "config": { "type": "string", "description": "The pipeline config document (YAML or JSON), stored verbatim." },
                        "id": { "type": "string", "description": "Template id. Derived from the config's `name:` when omitted." },
                        "description": { "type": "string" },
                        "tags": { "type": "array", "items": { "type": "string" }, "description": "Named environment channels to point at the new version (dev/test/staging/pre-prod/canary/prod). Derived channels (stable/previous/newest) are rejected." },
                        "launch": { "type": "boolean", "description": "Make the new version live immediately. Off by default: a register is inert, so a new build never moves existing callers until it is launched." }
                    },
                    "required": ["config"]
                }),
            });
            defs.push(ToolDef {
                name: "launch_template",
                description: "Make a template version live — what unpinned runs will use. MUTATING. This is the only action that moves existing callers; registering a build does not.",
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "id": { "type": "string" },
                        "version": { "description": "Version to launch: a number, or a channel whose current target to copy. Defaults to \"newest\".", "oneOf": [{ "type": "integer" }, { "type": "string" }] }
                    },
                    "required": ["id"]
                }),
            });
            defs.push(ToolDef {
                name: "rollback_template",
                description: "Re-launch a template's previously launched version. MUTATING.",
                input_schema: json!({
                    "type": "object",
                    "properties": { "id": { "type": "string" } },
                    "required": ["id"]
                }),
            });
            defs.push(ToolDef {
                name: "deprecate_template",
                description: "Retire a template (or revive it with undo:true). MUTATING. A deprecated template keeps serving existing callers but every trigger warns.",
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "id": { "type": "string" },
                        "reason": { "type": "string" },
                        "undo": { "type": "boolean" }
                    },
                    "required": ["id"]
                }),
            });
        }
        if ctx.allow_mutations {
            defs.push(ToolDef {
                name: "run_template",
                description: "Run a registered template with the given params: a source-template composed with `sink` (a registered sink-template), or a complete `pipeline` template. MUTATING — gated behind --allow-mutations. Pass dry_run:true to materialize + validate only.",
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "id": { "type": "string" },
                        "version": { "description": "Version: a number, or a named channel. Derived: \"stable\" (the launched version — the default), \"previous\", \"newest\". Assignable: \"dev\", \"test\", \"staging\", \"pre-prod\", \"canary\", \"prod\". Note \"latest\" is deliberately not a channel — use \"stable\" for the current release or \"newest\" for the highest version number.", "oneOf": [{ "type": "integer" }, { "type": "string" }] },
                        "params": { "type": "object", "description": "Values for the template's declared params." },
                        "sink": { "type": "string", "description": "For a source-template: the registered sink-template to compose in (required for a source template; a `pipeline` template takes none)." },
                        "sink_version": { "description": "Version of the sink template: a number or a channel. Default \"stable\".", "oneOf": [{ "type": "integer" }, { "type": "string" }] },
                        "overlay": { "description": "Deployment overlay for a composed run: a registered `kind: deployment` id, or an inline mapping of operational blocks (state, dlq, notifications, sla, resilience, execution, delivery, schedule, streams).", "oneOf": [{ "type": "string" }, { "type": "object" }] },
                        "overlay_version": { "description": "Version of a registered overlay: a number or a channel. Default \"stable\".", "oneOf": [{ "type": "integer" }, { "type": "string" }] },
                        "env": { "type": "object", "description": "Per-run overrides for ${env:VAR} resolution." },
                        "dry_run": { "type": "boolean", "description": "If true, materialize + validate only; do not write to any sink." },
                        "selection": { "type": "object", "description": "Run only some matrix rows / streams: { select: [ids], only: [globs], skip: [ids|globs], tags: [..], status: [available|draft|archived], include_parents: off|eligible|all }. Omit to run every row." }
                    },
                    "required": ["id"]
                }),
            });
        }
    }
    if ctx.changes.is_some() {
        defs.push(ToolDef {
            name: "propose_run",
            description: "Propose a pipeline run as a change request (#703) instead of running it: the server plans it (resolved rows, delivery guarantee, policy verdict, impact) and stores it pending; an approver runs it. Use this when the server requires approval, or whenever a human should review first. Returns the pending request (id, plan summary, approvals needed).",
            input_schema: json!({
                "type": "object",
                "properties": {
                    "config": { "type": "string", "description": "The pipeline config (YAML or JSON)." },
                    "config_format": { "type": "string", "enum": ["yaml", "json"], "description": "Default yaml." },
                    "name": { "type": "string", "description": "Run name." },
                    "reason": { "type": "string", "description": "Why — shown to the approvers." },
                    "budget": { "type": "object", "description": "Ceilings the approved run must honour: max_records, max_bytes, max_duration_secs, allowed_sinks.", "properties": {
                        "max_records": { "type": "integer" }, "max_bytes": { "type": "integer" },
                        "max_duration_secs": { "type": "integer" }, "allowed_sinks": { "type": "array", "items": { "type": "string" } } } },
                    "labels": { "type": "object", "additionalProperties": { "type": "string" } },
                    "timeout_secs": { "type": "integer" },
                    "clock": { "type": "string", "description": "RFC 3339 / YYYY-MM-DD run clock for ${now.*}." },
                    "selection": { "type": "object", "description": "Run only some matrix rows / streams: { select: [ids], only: [globs], skip: [ids|globs], tags: [..], status: [available|draft|archived], include_parents: off|eligible|all }. Omit to run every row." }
                },
                "required": ["config", "reason"]
            }),
        });
        defs.push(ToolDef {
            name: "propose_template",
            description: "Propose a template change as a change request (#703): `action: register` files a new template version (the document is validated and planned, nothing is stored until approved); `action: launch` proposes making a version live. An approver executes it. Returns the pending request.",
            input_schema: json!({
                "type": "object",
                "properties": {
                    "action": { "type": "string", "enum": ["register", "launch"], "description": "Default register." },
                    "config": { "type": "string", "description": "register: the template document (YAML or JSON)." },
                    "id": { "type": "string", "description": "register: explicit template id (derived from name: when omitted). launch: the template id (required)." },
                    "description": { "type": "string", "description": "register: template description." },
                    "launch": { "type": "boolean", "description": "register: also launch the new version once registered." },
                    "version": { "description": "launch: the version to make live (number or channel; default newest).", "oneOf": [{ "type": "integer" }, { "type": "string" }] },
                    "reason": { "type": "string", "description": "Why — shown to the approvers." }
                },
                "required": ["reason"]
            }),
        });
    }
    defs
}

/// Dispatch a `tools/call`. Returns the MCP `tools/call` result envelope
/// (`content` + `isError`). A tool-level failure is `tool_error(..)`, not a
/// JSON-RPC protocol error.
pub async fn call_tool(ctx: &McpContext, name: &str, args: &Value) -> Value {
    let result: Result<String, String> = match name {
        "list_connectors" => list_connectors(args),
        "get_connector_schema" => get_connector_schema(args),
        "scaffold_config" => scaffold_config(args),
        // Gated at call time as well as in the advertised list: an agent can
        // always name a tool it was never offered.
        "validate_config" => {
            if !ctx.allow_config_execution {
                Err(CONFIG_EXEC_GATE.to_string())
            } else {
                validate_config(ctx, args).await
            }
        }
        "preview" => {
            if !ctx.allow_config_execution {
                Err(CONFIG_EXEC_GATE.to_string())
            } else {
                preview(ctx, args).await
            }
        }
        "run_pipeline" => {
            if !ctx.allow_mutations {
                Err("run_pipeline is disabled; start the MCP server with --allow-mutations to enable mutating tools".to_string())
            } else if ctx.changes.as_ref().is_some_and(|c| {
                c.state
                    .requires_approval(crate::serve::changes::ChangeKind::Run)
            }) {
                Err("this server requires an approved change request for runs (--require-approval run); use propose_run instead".to_string())
            } else {
                run_pipeline(ctx, args).await
            }
        }
        "propose_run" => match &ctx.changes {
            Some(p) => propose_run(p, args).await,
            None => Err(
                "propose_run is only available on a server transport (faucet serve --mcp)"
                    .to_string(),
            ),
        },
        "propose_template" => match &ctx.changes {
            Some(p) => propose_template(p, args).await,
            None => Err(
                "propose_template is only available on a server transport (faucet serve --mcp)"
                    .to_string(),
            ),
        },
        #[cfg(feature = "templates")]
        "list_templates" => list_templates(ctx).await,
        #[cfg(feature = "templates")]
        "get_template" => get_template(ctx, args).await,
        #[cfg(feature = "templates")]
        "list_template_rows" => list_template_rows(ctx, args).await,
        #[cfg(feature = "templates")]
        "register_template" => {
            if !ctx.allow_mutations {
                Err(MUTATION_GATE.to_string())
            } else if !ctx.allow_template_admin {
                Err(TEMPLATE_ADMIN_GATE.to_string())
            } else {
                register_template(ctx, args).await
            }
        }
        #[cfg(feature = "templates")]
        "launch_template" => {
            if !ctx.allow_mutations {
                Err(MUTATION_GATE.to_string())
            } else if !ctx.allow_template_admin {
                Err(TEMPLATE_ADMIN_GATE.to_string())
            } else {
                launch_template(ctx, args).await
            }
        }
        #[cfg(feature = "templates")]
        "rollback_template" => {
            if !ctx.allow_mutations {
                Err(MUTATION_GATE.to_string())
            } else if !ctx.allow_template_admin {
                Err(TEMPLATE_ADMIN_GATE.to_string())
            } else {
                rollback_template(ctx, args).await
            }
        }
        #[cfg(feature = "templates")]
        "deprecate_template" => {
            if !ctx.allow_mutations {
                Err(MUTATION_GATE.to_string())
            } else if !ctx.allow_template_admin {
                Err(TEMPLATE_ADMIN_GATE.to_string())
            } else {
                deprecate_template(ctx, args).await
            }
        }
        #[cfg(feature = "templates")]
        "run_template" => {
            if !ctx.allow_mutations {
                Err(MUTATION_GATE.to_string())
            } else {
                run_template(ctx, args).await
            }
        }
        other => Err(format!("unknown tool '{other}'")),
    };
    match result {
        Ok(text) => tool_text(text),
        // Redact any resolved secret material that reached an error string.
        Err(msg) => tool_error(crate::secrets::registry::redact(&msg)),
    }
}

/// The MCP-facing rendering of a change request: everything an agent needs
/// to tell the human what to approve, without the plan rows' bulk.
fn render_change(change: &crate::serve::changes::ChangeRequest) -> Result<String, String> {
    let plan = change.plan.as_ref().map(|p| &p.summary);
    let value = json!({
        "change_id": change.id,
        "kind": change.kind,
        "status": change.status,
        "requester": change.requester,
        "reason": change.reason,
        "required_approvals": change.required_approvals,
        "approvals": change.approvals.len(),
        "expires_at": change.expires_at,
        "plan": plan,
        "budget": change.budget,
        "next": format!(
            "an approver runs it with POST /v1/changes/{}/approve (or the console's Changes page)",
            change.id
        ),
    });
    serde_json::to_string_pretty(&value).map_err(|e| e.to_string())
}

async fn propose_run(p: &crate::mcp::ChangeProposer, args: &Value) -> Result<String, String> {
    let config = str_arg(args, "config")?;
    let reason = str_arg(args, "reason")?;
    let mut payload = json!({
        "config": config,
        "config_format": args.get("config_format").cloned().unwrap_or(json!("yaml")),
    });
    for key in ["name", "labels", "timeout_secs", "clock", "selection"] {
        if let Some(v) = args.get(key).filter(|v| !v.is_null()) {
            payload[key] = v.clone();
        }
    }
    let budget = match args.get("budget").filter(|v| !v.is_null()) {
        Some(v) => Some(serde_json::from_value(v.clone()).map_err(|e| format!("budget: {e}"))?),
        None => None,
    };
    let change = crate::serve::changes::create(
        &p.state,
        &p.actor,
        crate::serve::changes::NewChange {
            kind: crate::serve::changes::ChangeKind::Run,
            payload,
            reason: Some(reason.to_string()),
            budget,
        },
    )
    .await
    .map_err(|e| e.to_string())?;
    render_change(&change)
}

async fn propose_template(p: &crate::mcp::ChangeProposer, args: &Value) -> Result<String, String> {
    let reason = str_arg(args, "reason")?;
    let action = args
        .get("action")
        .and_then(Value::as_str)
        .unwrap_or("register");
    let (kind, payload) = match action {
        "register" => {
            let config = str_arg(args, "config")?;
            let mut payload = json!({ "config": config });
            for key in ["id", "description", "launch"] {
                if let Some(v) = args.get(key).filter(|v| !v.is_null()) {
                    payload[key] = v.clone();
                }
            }
            (crate::serve::changes::ChangeKind::TemplateRegister, payload)
        }
        "launch" => {
            let id = str_arg(args, "id")?;
            let mut payload = json!({ "id": id });
            if let Some(v) = args.get("version").filter(|v| !v.is_null()) {
                payload["version"] = v.clone();
            }
            (crate::serve::changes::ChangeKind::TemplateLaunch, payload)
        }
        other => {
            return Err(format!(
                "unknown action `{other}` (expected register or launch)"
            ));
        }
    };
    let change = crate::serve::changes::create(
        &p.state,
        &p.actor,
        crate::serve::changes::NewChange {
            kind,
            payload,
            reason: Some(reason.to_string()),
            budget: None,
        },
    )
    .await
    .map_err(|e| e.to_string())?;
    render_change(&change)
}

fn str_arg<'a>(args: &'a Value, key: &str) -> Result<&'a str, String> {
    args.get(key)
        .and_then(Value::as_str)
        .ok_or_else(|| format!("missing required string argument '{key}'"))
}

fn tier_of(kind: &str, is_source: bool) -> &'static str {
    crate::conformance::tier_for(kind, is_source).as_str()
}

fn list_connectors(args: &Value) -> Result<String, String> {
    let filter = args.get("kind").and_then(Value::as_str).unwrap_or("all");
    let want = |c: &str| filter == "all" || filter == c;

    let mut out = json!({});
    let obj = out.as_object_mut().unwrap();
    if want("source") {
        let sources: Vec<Value> = crate::registry::source_descriptions()
            .into_iter()
            .map(|(name, desc)| json!({ "name": name, "description": desc, "tier": tier_of(name, true) }))
            .collect();
        obj.insert("sources".into(), json!(sources));
    }
    if want("sink") {
        let sinks: Vec<Value> = crate::registry::sink_descriptions()
            .into_iter()
            .map(|(name, desc)| json!({ "name": name, "description": desc, "tier": tier_of(name, false) }))
            .collect();
        obj.insert("sinks".into(), json!(sinks));
    }
    if want("transform") {
        let transforms: Vec<Value> = crate::transforms::transform_descriptions()
            .into_iter()
            .map(|(name, desc)| json!({ "name": name, "description": desc }))
            .collect();
        obj.insert("transforms".into(), json!(transforms));
    }
    if want("state") {
        obj.insert(
            "state_stores".into(),
            json!(crate::state::available_state_kinds()),
        );
    }
    Ok(pretty(&out))
}

fn get_connector_schema(args: &Value) -> Result<String, String> {
    let kind = str_arg(args, "kind")?;
    let name = str_arg(args, "name")?;
    let schema = match kind {
        "source" => crate::registry::source_schema(name),
        "sink" => crate::registry::sink_schema(name),
        "transform" => crate::transforms::transform_schema(name),
        other => return Err(format!("kind must be source|sink|transform, got '{other}'")),
    }
    .map_err(|e| e.to_string())?;
    Ok(pretty(&schema))
}

fn scaffold_config(args: &Value) -> Result<String, String> {
    let source = str_arg(args, "source")?;
    let sink = str_arg(args, "sink")?;
    let name = args
        .get("name")
        .and_then(Value::as_str)
        .unwrap_or("pipeline");

    let src_schema = crate::registry::source_schema(source).map_err(|e| e.to_string())?;
    let sink_schema = crate::registry::sink_schema(sink).map_err(|e| e.to_string())?;
    let src_yaml = crate::init_template::schema_to_yaml_template(&src_schema, 6);
    let sink_yaml = crate::init_template::schema_to_yaml_template(&sink_schema, 6);

    Ok(format!(
        "version: 1\nname: {name}\npipeline:\n  source:\n    type: {source}\n    config:\n{src_yaml}\n  sink:\n    type: {sink}\n    config:\n{sink_yaml}"
    ))
}

/// Parse an inline config document, mirroring the file-load pipeline: resolve
/// `${env:}` / `${file:}` / `${secret:}` per scalar, bind `${param.*}`, then take
/// the typed path. YAML is a JSON superset, so one parser handles both wire
/// formats.
///
/// `mode` decides what an unsupplied `required` param means: `Placeholder` for
/// the read-only introspection tools (a parameterized config still validates),
/// `Strict` where the config is about to actually run.
fn parse_config_with(text: &str, mode: crate::params::BindMode) -> Result<PipelineConfig, String> {
    let mut doc: Value = serde_yaml::from_str(text).map_err(|e| e.to_string())?;
    crate::interpolate::interpolate_value(&mut doc).map_err(|e| e.to_string())?;
    crate::params::bind_document(&mut doc, &Default::default(), mode).map_err(|e| e.to_string())?;
    PipelineConfig::from_value(doc).map_err(|e| e.to_string())
}

/// Read-only introspection: a config whose required params arrive later still
/// validates, against type-shaped placeholders.
fn parse_config(text: &str) -> Result<PipelineConfig, String> {
    parse_config_with(text, crate::params::BindMode::Placeholder)
}

async fn validate_config(ctx: &McpContext, args: &Value) -> Result<String, String> {
    let text = str_arg(args, "config")?;
    let cfg = parse_config(text)?;

    if crate::topology::is_topology(&cfg) {
        let topo = crate::topology::build_topology(&cfg, &ctx.auth)
            .await
            .map_err(|e| e.to_string())?;
        return Ok(pretty(&json!({
            "valid": true,
            "mode": "topology",
            "nodes": topo.nodes().iter().map(|n| json!({"id": n.id, "kind": n.kind.kind_str()})).collect::<Vec<_>>(),
            "edges": topo.edges().len(),
        })));
    }

    let nodes = crate::expand::expand(&cfg).map_err(|e| e.to_string())?;
    let rows: Vec<Value> = nodes
        .iter()
        .map(|n| {
            json!({
                "id": n.id,
                "source": n.source.kind,
                "sink": n.sink.kind,
                "transforms": n.transforms.len(),
            })
        })
        .collect();
    Ok(pretty(&json!({
        "valid": true,
        "mode": "matrix",
        "name": cfg.name,
        "rows": rows,
    })))
}

async fn preview(ctx: &McpContext, args: &Value) -> Result<String, String> {
    use faucet_core::stage::{apply_stages, compile_stage};

    let text = str_arg(args, "config")?;
    let limit = args
        .get("limit")
        .and_then(Value::as_u64)
        .map(|n| (n as usize).clamp(1, PREVIEW_MAX))
        .unwrap_or(10);

    let cfg = parse_config(text)?;
    if crate::topology::is_topology(&cfg) {
        return crate::topology::preview_to_string(&cfg, &ctx.auth, limit)
            .await
            .map_err(|e| e.to_string());
    }

    let nodes = crate::expand::expand(&cfg).map_err(|e| e.to_string())?;
    let first_root = nodes
        .iter()
        .find(|n| matches!(n.role, crate::expand::NodeRole::Root))
        .ok_or_else(|| "no root row to preview".to_string())?;

    let source = crate::registry::build_source(
        &first_root.source.kind,
        first_root.source.config.clone(),
        &ctx.auth,
        None,
    )
    .await
    .map_err(|e| e.to_string())?;
    let stages =
        crate::transforms::compile_transforms(&first_root.transforms).map_err(|e| e.to_string())?;
    let records = source.fetch_all().await.map_err(|e| e.to_string())?;
    let records: Vec<Value> = if stages.is_empty() {
        records
    } else {
        let compiled = stages
            .iter()
            .map(compile_stage)
            .collect::<Result<Vec<_>, _>>()
            .map_err(|e| e.to_string())?;
        let mut out = Vec::with_capacity(records.len());
        for r in records {
            out.extend(apply_stages(r, &compiled).map_err(|e| e.to_string())?);
        }
        out
    };
    let limited: Vec<Value> = records.into_iter().take(limit).collect();
    Ok(pretty(
        &json!({ "row": first_root.id, "count": limited.len(), "records": limited }),
    ))
}

/// The optional `selection` argument (#741).
fn selection_arg(args: &Value) -> Result<Option<crate::select::SelectionRequest>, String> {
    match args.get("selection") {
        None | Some(Value::Null) => Ok(None),
        Some(v) => serde_json::from_value(v.clone())
            .map(Some)
            .map_err(|e| format!("selection: {e}")),
    }
}

async fn run_pipeline(ctx: &McpContext, args: &Value) -> Result<String, String> {
    let text = str_arg(args, "config")?;
    let selection = selection_arg(args)?;
    let dry_run = args
        .get("dry_run")
        .and_then(Value::as_bool)
        .unwrap_or(false);

    if dry_run {
        let mut report = validate_config(ctx, args).await?;
        report.push_str("\n\n-- preview --\n");
        report.push_str(
            &preview(ctx, args)
                .await
                .unwrap_or_else(|e| format!("preview skipped: {e}")),
        );
        return Ok(report);
    }

    let summary = crate::run_from_yaml_str_selected(text, selection.as_ref())
        .await
        .map_err(|e| e.to_string())?;
    let failed = summary.failure_count();
    let total: usize = summary.invocations.iter().map(|i| i.records_written).sum();
    let doc = json!({
        "selection": selection.as_ref().map(|s| s.canonical()),
        "invocations": summary.invocations.len(),
        "ok": summary.invocations.len() - failed,
        "failed": failed,
        "records_written": total,
    });
    if failed > 0 {
        return Err(format!(
            "pipeline had {failed} failed invocation(s): {}",
            pretty(&doc)
        ));
    }
    Ok(pretty(&doc))
}

fn pretty(v: &Value) -> String {
    serde_json::to_string_pretty(v).unwrap_or_else(|_| v.to_string())
}

// ── Pipeline template tools (#444) ──────────────────────────────────────────

/// Shared refusal text for a mutating template tool on a read-only server.
#[cfg(feature = "templates")]
const MUTATION_GATE: &str =
    "this tool is disabled; start the MCP server with --allow-mutations to enable mutating tools";

/// Refusal text for the template-lifecycle tools when the caller is not an admin.
const TEMPLATE_ADMIN_GATE: &str = "managing templates (register, launch, roll back, deprecate) \
     is admin-only; an operator can run registered templates with run_template";

/// Refusal text for the config-executing tools when the caller lacks the scope.
/// Phrased for the HTTP transport, which is the only place the gate closes.
const CONFIG_EXEC_GATE: &str = "this tool acts on a config you supply — it resolves \
     ${env:}/${file:}/${secret:} on the server and builds the connectors you name — so it \
     requires the same scope as POST /v1/doctor (role `operator` or `admin`), not a read-only \
     token";

/// The wired template registry, or a tool error explaining how to wire one.
#[cfg(feature = "templates")]
fn template_store(ctx: &McpContext) -> Result<&crate::templates::TemplateStore, String> {
    ctx.templates.as_ref().ok_or_else(|| {
        "no pipeline-template registry is configured — start `faucet mcp --template-store \
         <url>`, or use the /mcp route of a `faucet serve --mcp` whose --history backend holds \
         the registry"
            .to_string()
    })
}

#[cfg(feature = "templates")]
async fn list_templates(ctx: &McpContext) -> Result<String, String> {
    let store = template_store(ctx)?;
    let templates = crate::templates::list_with_state(store)
        .await
        .map_err(|e| e.to_string())?;
    Ok(pretty(&json!({
        "count": templates.len(),
        "templates": templates,
    })))
}

/// Read the optional `version` argument: a number, or one of the closed set of
/// named channels. Absent = `stable` (the *launched* version), so an agent that
/// never mentions versions rides releases rather than picking up every new
/// registration.
#[cfg(feature = "templates")]
fn version_arg(args: &Value) -> Result<crate::serve::history::templates::VersionSelector, String> {
    use crate::serve::history::templates::VersionSelector;
    match args.get("version") {
        None | Some(Value::Null) => Ok(VersionSelector::default()),
        Some(v) => serde_json::from_value::<VersionSelector>(v.clone()).map_err(|e| e.to_string()),
    }
}

/// Resolve the `version` argument against the registry (every channel, derived or
/// assigned, needs a lookup — nothing falls back to "the newest build").
#[cfg(feature = "templates")]
async fn resolved_version_arg(
    store: &crate::templates::TemplateStore,
    id: &str,
    args: &Value,
) -> Result<u32, String> {
    crate::templates::resolve_version(store, id, version_arg(args)?)
        .await
        .map_err(|e| e.to_string())
}

#[cfg(feature = "templates")]
async fn get_template(ctx: &McpContext, args: &Value) -> Result<String, String> {
    let store = template_store(ctx)?;
    let id = str_arg(args, "id")?;
    let version = resolved_version_arg(store, id, args).await?;
    let record = store
        .template_get(id, Some(version))
        .await
        .map_err(|e| e.to_string())?
        .ok_or_else(|| format!("no pipeline template '{id}'"))?;
    let launches = store
        .template_launches(id)
        .await
        .map_err(|e| e.to_string())?;
    let state = crate::templates::template_state(store, id)
        .await
        .map_err(|e| e.to_string())?;
    Ok(pretty(&json!({
        "template": record,
        "state": state,
        "is_stable": state.stable == Some(record.version),
        "launches": launches,
    })))
}

/// Read the optional `tags` array, validating each against the closed channel set.
#[cfg(feature = "templates")]
fn tags_arg(args: &Value) -> Result<Vec<crate::serve::history::templates::VersionChannel>, String> {
    use crate::serve::history::templates::VersionChannel;
    let Some(list) = args.get("tags").and_then(Value::as_array) else {
        return Ok(Vec::new());
    };
    list.iter()
        .map(|v| {
            v.as_str()
                .ok_or_else(|| "each `tags` entry must be a channel name".to_string())
                .and_then(|s| VersionChannel::parse(s).map_err(|e| e.to_string()))
        })
        .collect()
}

#[cfg(feature = "templates")]
async fn register_template(ctx: &McpContext, args: &Value) -> Result<String, String> {
    use crate::templates::RegisterRequest;
    let store = template_store(ctx)?;
    let config = str_arg(args, "config")?;
    let record = crate::templates::register(
        store,
        RegisterRequest {
            id: args.get("id").and_then(Value::as_str).map(str::to_string),
            body: config.to_string(),
            // MCP always hands over an inline document; YAML parses JSON too.
            format: crate::serve::load::ConfigFormat::Yaml,
            description: args
                .get("description")
                .and_then(Value::as_str)
                .map(str::to_string),
            tags: tags_arg(args)?,
            launch: args.get("launch").and_then(Value::as_bool).unwrap_or(false),
            created_by: Some("mcp".to_string()),
        },
    )
    .await
    .map_err(|e| e.to_string())?;
    Ok(pretty(&json!({
        "registered": record.summary(),
    })))
}

#[cfg(feature = "templates")]
async fn launch_template(ctx: &McpContext, args: &Value) -> Result<String, String> {
    use crate::serve::history::templates::VersionSelector;
    let store = template_store(ctx)?;
    let id = str_arg(args, "id")?;
    let target = match args.get("version") {
        None | Some(Value::Null) => VersionSelector::newest(),
        Some(_) => version_arg(args)?,
    };
    let outcome = crate::templates::launch(store, id, target, Some("mcp"))
        .await
        .map_err(|e| e.to_string())?;
    Ok(pretty(&json!({
        "id": id,
        "version": outcome.version,
        "replaced": outcome.replaced,
        "already_launched": outcome.already_launched,
        "first_launch": outcome.first_launch,
    })))
}

#[cfg(feature = "templates")]
async fn rollback_template(ctx: &McpContext, args: &Value) -> Result<String, String> {
    let store = template_store(ctx)?;
    let id = str_arg(args, "id")?;
    let outcome = crate::templates::rollback(store, id, Some("mcp"))
        .await
        .map_err(|e| e.to_string())?;
    Ok(pretty(&json!({
        "id": id,
        "version": outcome.version,
        "replaced": outcome.replaced,
    })))
}

#[cfg(feature = "templates")]
async fn deprecate_template(ctx: &McpContext, args: &Value) -> Result<String, String> {
    let store = template_store(ctx)?;
    let id = str_arg(args, "id")?;
    let undo = args.get("undo").and_then(Value::as_bool).unwrap_or(false);
    let reason = args
        .get("reason")
        .and_then(Value::as_str)
        .map(str::to_string);
    let status = crate::templates::set_deprecated(store, id, reason, Some("mcp"), !undo)
        .await
        .map_err(|e| e.to_string())?;
    Ok(pretty(&json!({ "id": id, "status": status.as_str() })))
}

#[cfg(feature = "templates")]
async fn list_template_rows(ctx: &McpContext, args: &Value) -> Result<String, String> {
    let store = template_store(ctx)?;
    let id = str_arg(args, "id")?;
    let version = resolved_version_arg(store, id, args).await?;
    let sink = args.get("sink").and_then(Value::as_str);
    let sink_version = match sink {
        Some(sid) => {
            let sel = match args.get("sink_version") {
                None | Some(Value::Null) => Default::default(),
                Some(v) => serde_json::from_value(v.clone()).map_err(|e| e.to_string())?,
            };
            Some(
                crate::templates::resolve_version(store, sid, sel)
                    .await
                    .map_err(|e| e.to_string())?,
            )
        }
        None => None,
    };
    let selection = selection_arg(args)?;
    let report = crate::templates::rows::list_rows(
        store,
        crate::templates::rows::RowsQuery {
            id,
            version,
            sink: sink.zip(sink_version),
            overlay: None,
            selection: selection.as_ref(),
            state: true,
        },
    )
    .await
    .map_err(|e| e.to_string())?;
    serde_json::to_string_pretty(&report).map_err(|e| e.to_string())
}

#[cfg(feature = "templates")]
async fn run_template(ctx: &McpContext, args: &Value) -> Result<String, String> {
    let store = template_store(ctx)?;
    let id = str_arg(args, "id")?;
    let version = resolved_version_arg(store, id, args).await?;
    let deprecated = crate::templates::deprecation_warning(
        &crate::templates::template_state(store, id)
            .await
            .map_err(|e| e.to_string())?,
        version,
    );
    let dry_run = args
        .get("dry_run")
        .and_then(Value::as_bool)
        .unwrap_or(false);
    let supplied: crate::params::SuppliedParams = args
        .get("params")
        .and_then(Value::as_object)
        .map(|m| m.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
        .unwrap_or_default();
    let env: std::collections::BTreeMap<String, String> = args
        .get("env")
        .and_then(Value::as_object)
        .map(|m| {
            m.iter()
                .filter_map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_string())))
                .collect()
        })
        .unwrap_or_default();

    let sink = crate::templates::SinkChoice {
        id: args.get("sink").and_then(Value::as_str).map(str::to_string),
        version: match args.get("sink_version") {
            None | Some(Value::Null) => Default::default(),
            Some(v) => serde_json::from_value(v.clone()).map_err(|e| e.to_string())?,
        },
        overlay: match args.get("overlay") {
            None | Some(Value::Null) => None,
            Some(Value::String(id)) => Some(crate::templates::OverlayChoice::Registered {
                id: id.clone(),
                version: match args.get("overlay_version") {
                    None | Some(Value::Null) => Default::default(),
                    Some(v) => serde_json::from_value(v.clone()).map_err(|e| e.to_string())?,
                },
            }),
            Some(v @ Value::Object(_)) => Some(crate::templates::OverlayChoice::Inline(v.clone())),
            Some(_) => return Err("`overlay` must be a deployment id or a mapping".into()),
        },
    };
    let selection = selection_arg(args)?;
    let materialized = crate::templates::materialize_for_run_selected(
        store,
        id,
        version,
        &sink,
        &supplied,
        &env,
        // The MCP tool runs the pipeline in this process; nothing is persisted.
        crate::templates::Materialize::Local,
        selection.as_ref(),
    )
    .await
    .map_err(|e| e.to_string())?;

    if dry_run {
        // Validate the materialized config without touching a sink, and never
        // echo the body — a secret param value would be in it.
        let cfg = parse_config_with(&materialized.body, crate::params::BindMode::Strict)?;
        let mut nodes = crate::expand::expand(&cfg).map_err(|e| e.to_string())?;
        if let Some(sel) = &materialized.selection {
            nodes = sel.apply(&cfg, nodes).map_err(|e| e.to_string())?;
        }
        let run_set: Vec<String> = nodes.iter().map(|n| n.id.clone()).collect();
        let rows = nodes.len();
        return Ok(pretty(&json!({
            "template_id": materialized.template_id,
            "template_version": materialized.version,
            "sink_template": materialized.sink_id,
            "sink_template_version": materialized.sink_version,
            "streams": materialized.streams,
            "overlay": materialized.overlay_id,
            "overlay_version": materialized.overlay_version,
            "overlay_contributes": materialized.overlay_contributes,
            "warnings": materialized.warnings,
            "params": materialized.params_redacted,
            "rows": rows,
            "run_set": run_set,
            "selection": selection.as_ref().map(|s| s.canonical()),
            "dry_run": true,
            "deprecated": deprecated,
        })));
    }

    // The materialized body is JSON, which `run_from_yaml_str` parses (YAML is a
    // JSON superset) and takes through the ordinary run path.
    let summary =
        crate::run_from_yaml_str_selected(&materialized.body, materialized.selection.as_ref())
            .await
            .map_err(|e| e.to_string())?;
    let failed = summary.failure_count();
    let total: usize = summary.invocations.iter().map(|i| i.records_written).sum();
    let doc = json!({
        "template_id": materialized.template_id,
        "template_version": materialized.version,
        "sink_template": materialized.sink_id,
        "sink_template_version": materialized.sink_version,
        "overlay": materialized.overlay_id,
        "overlay_version": materialized.overlay_version,
        "params": materialized.params_redacted,
        "selection": selection.as_ref().map(|s| s.canonical()),
        "invocations": summary.invocations.len(),
        "ok": summary.invocations.len() - failed,
        "failed": failed,
        "records_written": total,
        "deprecated": deprecated,
    });
    if failed > 0 {
        return Err(format!(
            "template run had {failed} failed invocation(s): {}",
            pretty(&doc)
        ));
    }
    Ok(pretty(&doc))
}

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

    fn ctx(allow: bool) -> McpContext {
        McpContext::new(
            crate::auth_catalog::build_auth_catalog(None).unwrap(),
            allow,
        )
    }

    #[tokio::test]
    async fn propose_tools_file_change_requests_on_a_server() {
        let dir = tempfile::tempdir().unwrap();
        let csv = dir.path().join("in.csv");
        std::fs::write(&csv, "id\n1\n").unwrap();
        let config = format!(
            "version: 1\nname: mcp-chg\npipeline:\n  source:\n    type: csv\n    config:\n      path: {}\n  sink:\n    type: jsonl\n    config:\n      path: {}\n",
            csv.display(),
            dir.path().join("out.jsonl").display()
        );
        for tool in ["propose_run", "propose_template"] {
            let out = call_tool(&ctx(true), tool, &json!({})).await;
            assert_eq!(out["isError"], true);
            assert!(
                out["content"][0]["text"]
                    .as_str()
                    .unwrap()
                    .contains("server transport")
            );
        }
        let proposer = crate::mcp::ChangeProposer {
            state: crate::serve::test_support::test_state(),
            actor: crate::serve::rbac::AuthContext::system("agent"),
        };
        let c = ctx(true).with_changes(proposer);
        assert!(format!("{:?}", c.changes.as_ref().unwrap()).contains("system:agent"));

        let out = call_tool(
            &c,
            "propose_run",
            &json!({"config": config, "reason": "nightly", "name": "n", "labels": null,
                    "budget": {"max_records": 10}}),
        )
        .await;
        assert_eq!(out["isError"], false, "{out}");
        assert!(
            out["content"][0]["text"]
                .as_str()
                .unwrap()
                .contains("pending")
        );
        let bad = call_tool(
            &c,
            "propose_run",
            &json!({"config": config, "reason": "r", "budget": {"max_records": "x"}}),
        )
        .await;
        assert!(
            bad["content"][0]["text"]
                .as_str()
                .unwrap()
                .contains("budget")
        );
        let bad = call_tool(&c, "propose_run", &json!({"config": "{", "reason": "r"})).await;
        assert_eq!(bad["isError"], true);

        let bad = call_tool(
            &c,
            "propose_template",
            &json!({"reason": "r", "action": "burn"}),
        )
        .await;
        assert!(
            bad["content"][0]["text"]
                .as_str()
                .unwrap()
                .contains("unknown action")
        );
        let reg = call_tool(
            &c,
            "propose_template",
            &json!({"reason": "r", "config": config, "id": "mcp-chg", "launch": null}),
        )
        .await;
        #[cfg(feature = "templates")]
        assert_eq!(reg["isError"], false, "{reg}");
        #[cfg(not(feature = "templates"))]
        assert_eq!(reg["isError"], true);
        let launch = call_tool(
            &c,
            "propose_template",
            &json!({"reason": "r", "action": "launch", "id": "ghost", "version": 1}),
        )
        .await;
        assert!(launch["content"][0]["text"].is_string());
        let missing = call_tool(
            &c,
            "propose_template",
            &json!({"reason": "r", "action": "launch"}),
        )
        .await;
        assert_eq!(missing["isError"], true);
    }

    #[test]
    fn tool_defs_gate_mutations() {
        let ro = tool_defs(&ctx(false));
        assert!(ro.iter().all(|t| t.name != "run_pipeline"));
        let rw = tool_defs(&ctx(true));
        assert!(rw.iter().any(|t| t.name == "run_pipeline"));
    }

    #[tokio::test]
    async fn list_connectors_includes_sources_and_tier() {
        let out = call_tool(&ctx(false), "list_connectors", &json!({})).await;
        assert_eq!(out["isError"], false);
        let text = out["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("\"sources\""));
        assert!(text.contains("\"tier\""));
    }

    #[tokio::test]
    async fn list_connectors_filter_kind() {
        let out = call_tool(
            &ctx(false),
            "list_connectors",
            &json!({"kind": "transform"}),
        )
        .await;
        let text = out["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("\"transforms\""));
        assert!(!text.contains("\"sources\""));
    }

    #[tokio::test]
    async fn get_connector_schema_unknown_is_tool_error() {
        let out = call_tool(
            &ctx(false),
            "get_connector_schema",
            &json!({"kind":"source","name":"nope"}),
        )
        .await;
        assert_eq!(out["isError"], true);
    }

    #[tokio::test]
    async fn unknown_tool_errors() {
        let out = call_tool(&ctx(false), "does_not_exist", &json!({})).await;
        assert_eq!(out["isError"], true);
    }

    #[tokio::test]
    async fn run_pipeline_blocked_without_mutations() {
        let out = call_tool(&ctx(false), "run_pipeline", &json!({"config":"version: 1"})).await;
        assert_eq!(out["isError"], true);
        assert!(
            out["content"][0]["text"]
                .as_str()
                .unwrap()
                .contains("--allow-mutations")
        );
    }

    // ── handler coverage: scaffold / validate / preview / run ────────────────

    fn csv_config(dir: &std::path::Path) -> String {
        let csv = dir.join("in.csv");
        std::fs::write(&csv, "id,name\n1,alice\n2,bob\n").unwrap();
        let out = dir.join("out.jsonl");
        format!(
            "version: 1\nname: t\npipeline:\n  source:\n    type: csv\n    config:\n      path: {}\n  sink:\n    type: jsonl\n    config:\n      path: {}\n",
            csv.display(),
            out.display()
        )
    }

    fn topology_config(dir: &std::path::Path) -> String {
        let csv = dir.join("in.csv");
        std::fs::write(&csv, "id,name\n1,alice\n").unwrap();
        let out = dir.join("out.jsonl");
        format!(
            "version: 1\nname: t\npipeline:\n  sources:\n    s: {{ type: csv, config: {{ path: {} }} }}\n  sinks:\n    o: {{ type: jsonl, config: {{ path: {} }} }}\n  nodes:\n    src: {{ kind: source, ref: s }}\n    w: {{ kind: sink, ref: o }}\n  edges:\n    - {{ from: src, to: w }}\n",
            csv.display(),
            out.display()
        )
    }

    #[tokio::test]
    async fn scaffold_config_emits_yaml() {
        let out = call_tool(
            &ctx(false),
            "scaffold_config",
            &json!({"source":"csv","sink":"jsonl","name":"demo"}),
        )
        .await;
        assert_eq!(out["isError"], false);
        let text = out["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("name: demo"));
        assert!(text.contains("type: csv"));
        assert!(text.contains("type: jsonl"));
    }

    #[tokio::test]
    async fn scaffold_config_missing_arg_errors() {
        let out = call_tool(&ctx(false), "scaffold_config", &json!({"source":"csv"})).await;
        assert_eq!(out["isError"], true);
        assert!(out["content"][0]["text"].as_str().unwrap().contains("sink"));
    }

    #[tokio::test]
    async fn validate_config_matrix_ok() {
        let dir = tempfile::tempdir().unwrap();
        let out = call_tool(
            &ctx(false),
            "validate_config",
            &json!({ "config": csv_config(dir.path()) }),
        )
        .await;
        assert_eq!(out["isError"], false);
        let text = out["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("\"mode\": \"matrix\""));
        assert!(text.contains("\"valid\": true"));
    }

    #[tokio::test]
    async fn validate_config_topology_ok() {
        let dir = tempfile::tempdir().unwrap();
        let out = call_tool(
            &ctx(false),
            "validate_config",
            &json!({ "config": topology_config(dir.path()) }),
        )
        .await;
        assert_eq!(out["isError"], false);
        assert!(
            out["content"][0]["text"]
                .as_str()
                .unwrap()
                .contains("\"mode\": \"topology\"")
        );
    }

    #[tokio::test]
    async fn validate_config_bad_yaml_errors() {
        let out = call_tool(
            &ctx(false),
            "validate_config",
            &json!({ "config": "this: is: not: valid: yaml:" }),
        )
        .await;
        assert_eq!(out["isError"], true);
    }

    #[tokio::test]
    async fn preview_matrix_returns_records() {
        let dir = tempfile::tempdir().unwrap();
        let out = call_tool(
            &ctx(false),
            "preview",
            &json!({ "config": csv_config(dir.path()), "limit": 1 }),
        )
        .await;
        assert_eq!(out["isError"], false);
        let text = out["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("\"count\": 1"));
        assert!(text.contains("alice"));
    }

    #[tokio::test]
    async fn preview_topology_returns_sources() {
        let dir = tempfile::tempdir().unwrap();
        let out = call_tool(
            &ctx(false),
            "preview",
            &json!({ "config": topology_config(dir.path()) }),
        )
        .await;
        assert_eq!(out["isError"], false);
        assert!(
            out["content"][0]["text"]
                .as_str()
                .unwrap()
                .contains("\"sources\"")
        );
    }

    #[tokio::test]
    async fn run_pipeline_dry_run_validates_and_previews() {
        let dir = tempfile::tempdir().unwrap();
        let out = call_tool(
            &ctx(true),
            "run_pipeline",
            &json!({ "config": csv_config(dir.path()), "dry_run": true }),
        )
        .await;
        assert_eq!(out["isError"], false);
        let text = out["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("-- preview --"));
    }

    #[tokio::test]
    async fn run_pipeline_real_writes_sink() {
        let dir = tempfile::tempdir().unwrap();
        let cfg = csv_config(dir.path());
        let out = call_tool(&ctx(true), "run_pipeline", &json!({ "config": cfg })).await;
        assert_eq!(out["isError"], false, "{}", out["content"][0]["text"]);
        assert!(
            out["content"][0]["text"]
                .as_str()
                .unwrap()
                .contains("\"records_written\": 2")
        );
        assert_eq!(
            std::fs::read_to_string(dir.path().join("out.jsonl"))
                .unwrap()
                .lines()
                .count(),
            2
        );
    }

    #[tokio::test]
    async fn get_connector_schema_transform_ok() {
        let out = call_tool(
            &ctx(false),
            "get_connector_schema",
            &json!({"kind":"transform","name":"keys_case"}),
        )
        .await;
        assert_eq!(out["isError"], false);
    }

    #[tokio::test]
    async fn get_connector_schema_bad_kind_errors() {
        let out = call_tool(
            &ctx(false),
            "get_connector_schema",
            &json!({"kind":"weird","name":"x"}),
        )
        .await;
        assert_eq!(out["isError"], true);
    }

    #[tokio::test]
    async fn validate_config_accepts_a_parameterized_config() {
        // Read-only introspection binds required params to placeholders, so a
        // template-shaped config still validates (#444).
        let dir = tempfile::tempdir().unwrap();
        let cfg = format!(
            "version: 1\nname: t\nparams:\n  tag: {{ required: true }}\npipeline:\n  source:\n    type: csv\n    config:\n      path: {}\n  sink:\n    type: jsonl\n    config:\n      path: {}\n",
            dir.path().join("in-${param.tag}.csv").display(),
            dir.path().join("out.jsonl").display()
        );
        let out = call_tool(&ctx(false), "validate_config", &json!({ "config": cfg })).await;
        assert_eq!(out["isError"], false, "{}", out["content"][0]["text"]);
    }

    // ── Pipeline template tools (#444) ──────────────────────────────────────

    #[cfg(feature = "templates")]
    mod templates {
        use super::*;
        use std::sync::Arc;
        use std::time::Duration;

        fn tpl_ctx(allow: bool) -> McpContext {
            let store = Arc::new(crate::serve::history::memory::MemoryHistory::new(
                Duration::from_secs(60),
            )) as crate::templates::TemplateStore;
            McpContext::new(
                crate::auth_catalog::build_auth_catalog(None).unwrap(),
                allow,
            )
            .with_templates(store)
        }

        fn body(dir: &std::path::Path) -> String {
            let csv = dir.join("in.csv");
            std::fs::write(&csv, "id,name\n1,alice\n2,bob\n").unwrap();
            format!(
                "version: 1\nname: mcp-tpl\nparams:\n  tag: {{ required: true }}\npipeline:\n  source:\n    type: csv\n    config:\n      path: {}\n  sink:\n    type: jsonl\n    config:\n      path: {}\n",
                csv.display(),
                dir.join("out-${param.tag}.jsonl").display()
            )
        }

        fn hub_pair(dir: &std::path::Path) -> (String, String) {
            std::fs::write(dir.join("orders.csv"), "id,total\n1,10\n").unwrap();
            let source = format!(
                "kind: source-template\nname: acme-exports\ndescription: Acme exports\nparams:\n  data_dir: {{ type: string, default: {} }}\nsource:\n  type: csv\n  config:\n    path: \"${{param.data_dir}}/orders.csv\"\nstreams:\n  - {{ name: orders, primary_keys: [id], write: [overwrite, upsert] }}\n",
                dir.display()
            );
            let sink = format!(
                "kind: sink-template\nname: local-jsonl\ndescription: Local files\nparams:\n  out_dir: {{ type: string, default: {} }}\nsink:\n  type: jsonl\n  config: {{ append: false }}\nper_stream:\n  path: \"${{param.out_dir}}/${{source}}/${{stream}}.jsonl\"\nwrite_mode_aliases: {{ overwrite: append }}\n",
                dir.display()
            );
            (source, sink)
        }

        #[tokio::test]
        async fn rows_and_selection_through_the_template_tools() {
            let dir = tempfile::tempdir().unwrap();
            let ctx = tpl_ctx(true);
            std::fs::write(dir.path().join("orders.csv"), "id,total\n1,10\n").unwrap();
            std::fs::write(dir.path().join("items.csv"), "id,sku\n1,a\n").unwrap();
            let source = format!(
                "kind: source-template\nname: shop\nparams:\n  data_dir: {{ type: string, default: {d} }}\nsource:\n  type: csv\n  config:\n    path: \"${{param.data_dir}}/orders.csv\"\nstreams:\n  - {{ name: orders, write: append }}\n  - {{ name: items, source: {{ config: {{ path: \"${{param.data_dir}}/items.csv\" }} }}, write: append }}\n",
                d = dir.path().display()
            );
            let (_, sink) = hub_pair(dir.path());
            for config in [source, sink] {
                let out = call_tool(
                    &ctx,
                    "register_template",
                    &json!({"config": config, "launch": true}),
                )
                .await;
                assert_eq!(out["isError"], false, "{out}");
            }
            let out = call_tool(
                &ctx,
                "list_template_rows",
                &json!({"id": "shop", "sink": "local-jsonl", "sink_version": "stable", "selection": {"select": ["items"]}}),
            )
            .await;
            assert_eq!(out["isError"], false, "{out}");
            let doc: Value =
                serde_json::from_str(out["content"][0]["text"].as_str().unwrap()).unwrap();
            assert_eq!(doc["run_set"], json!(["items"]));
            assert_eq!(doc["rows"].as_array().unwrap().len(), 2);
            let bad = call_tool(
                &ctx,
                "list_template_rows",
                &json!({"id": "shop", "selection": {"rows": []}}),
            )
            .await;
            assert_eq!(bad["isError"], true);

            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id": "shop", "sink": "local-jsonl", "dry_run": true, "selection": {"select": ["items"]}}),
            )
            .await;
            assert_eq!(out["isError"], false, "{out}");
            let doc: Value =
                serde_json::from_str(out["content"][0]["text"].as_str().unwrap()).unwrap();
            assert_eq!(doc["run_set"], json!(["items"]));
            assert_eq!(doc["selection"], "select=items");
            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id": "shop", "sink": "local-jsonl", "selection": {"select": ["items"]}}),
            )
            .await;
            assert_eq!(out["isError"], false, "{out}");
            assert!(dir.path().join("shop/items.jsonl").exists());
            assert!(!dir.path().join("shop/orders.jsonl").exists());
        }

        #[tokio::test]
        async fn run_pipeline_honours_a_selection() {
            let dir = tempfile::tempdir().unwrap();
            std::fs::write(dir.path().join("in.csv"), "id\n1\n").unwrap();
            let config = format!(
                "version: 1\nname: m\npipeline:\n  source: {{ type: csv, config: {{ path: {d}/in.csv }} }}\n  sink: {{ type: jsonl, config: {{ path: {d}/x.jsonl }} }}\nmatrix:\n  - {{ id: a, sink: {{ config: {{ path: {d}/a.jsonl }} }} }}\n  - {{ id: b, sink: {{ config: {{ path: {d}/b.jsonl }} }} }}\n",
                d = dir.path().display()
            );
            let out = call_tool(
                &ctx(true),
                "run_pipeline",
                &json!({"config": config, "selection": {"select": ["b"]}}),
            )
            .await;
            assert_eq!(out["isError"], false, "{out}");
            assert!(dir.path().join("b.jsonl").exists());
            assert!(!dir.path().join("a.jsonl").exists());
            let out = call_tool(
                &ctx(true),
                "run_pipeline",
                &json!({"config": config, "selection": {"select": ["zz"]}}),
            )
            .await;
            assert_eq!(out["isError"], true);
            let out = call_tool(
                &ctx(true),
                "run_pipeline",
                &json!({"config": config, "selection": "b"}),
            )
            .await;
            assert_eq!(out["isError"], true);
        }

        #[tokio::test]
        async fn run_template_composes_a_source_with_a_sink() {
            let dir = tempfile::tempdir().unwrap();
            let ctx = tpl_ctx(true);
            let (source, sink) = hub_pair(dir.path());
            for config in [source, sink] {
                let out = call_tool(
                    &ctx,
                    "register_template",
                    &json!({"config": config, "launch": true}),
                )
                .await;
                assert_eq!(out["isError"], false, "{out}");
            }
            // The list carries each template's kind.
            let out = call_tool(&ctx, "list_templates", &json!({})).await;
            let text = out["content"][0]["text"].as_str().unwrap();
            assert!(
                text.contains("source-template") && text.contains("sink-template"),
                "{text}"
            );

            // A source template needs a sink …
            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id": "acme-exports", "dry_run": true}),
            )
            .await;
            assert_eq!(out["isError"], true);
            assert!(out["content"][0]["text"].as_str().unwrap().contains("sink"));

            // … and composes with one; the dry run reports both halves and the plan.
            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id": "acme-exports", "sink": "local-jsonl", "sink_version": "stable", "dry_run": true}),
            )
            .await;
            assert_eq!(out["isError"], false, "{out}");
            let doc: Value =
                serde_json::from_str(out["content"][0]["text"].as_str().unwrap()).unwrap();
            assert_eq!(doc["sink_template"], json!("local-jsonl"));
            assert_eq!(doc["sink_template_version"], json!(1));
            assert_eq!(doc["streams"][0]["stream"], json!("orders"));
            assert_eq!(doc["rows"], json!(1));

            // A real run writes the stream file under <out_dir>/<source>/.
            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id": "acme-exports", "sink": "local-jsonl"}),
            )
            .await;
            assert_eq!(out["isError"], false, "{out}");
            let written =
                std::fs::read_to_string(dir.path().join("acme-exports/orders.jsonl")).unwrap();
            assert_eq!(written.lines().count(), 1);

            // A malformed sink version is a tool error, not a panic.
            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id": "acme-exports", "sink": "local-jsonl", "sink_version": "latest", "dry_run": true}),
            )
            .await;
            assert_eq!(out["isError"], true);

            // #679: an inline overlay is applied and reported in the dry run; a
            // registered one resolves by id; a malformed one is a tool error.
            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id": "acme-exports", "sink": "local-jsonl", "dry_run": true,
                        "overlay": {"execution": {"max_concurrent": 1}}}),
            )
            .await;
            assert_eq!(out["isError"], false, "{out}");
            let doc: Value =
                serde_json::from_str(out["content"][0]["text"].as_str().unwrap()).unwrap();
            assert_eq!(doc["overlay"], json!("inline"));
            assert_eq!(doc["overlay_contributes"], json!(["execution"]));
            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id": "acme-exports", "sink": "local-jsonl", "dry_run": true,
                        "overlay": "nope", "overlay_version": "stable"}),
            )
            .await;
            assert_eq!(out["isError"], true, "an unknown registered overlay");
            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id": "acme-exports", "sink": "local-jsonl", "dry_run": true, "overlay": 3}),
            )
            .await;
            assert_eq!(out["isError"], true);
            assert!(
                out["content"][0]["text"]
                    .as_str()
                    .unwrap()
                    .contains("deployment id or a mapping")
            );
        }

        #[tokio::test]
        async fn tools_are_hidden_without_a_store() {
            let names: Vec<&str> = tool_defs(&ctx(true)).iter().map(|t| t.name).collect();
            for t in ["list_templates", "register_template", "launch_template"] {
                assert!(!names.contains(&t), "{t} must be hidden: {names:?}");
            }
            // Calling one anyway is a clear tool error, not a panic.
            let out = call_tool(&ctx(true), "list_templates", &json!({})).await;
            assert_eq!(out["isError"], true);
            assert!(
                out["content"][0]["text"]
                    .as_str()
                    .unwrap()
                    .contains("--template-store")
            );
        }

        #[tokio::test]
        async fn read_tools_are_ungated_and_write_tools_are_gated() {
            let ro: Vec<&str> = tool_defs(&tpl_ctx(false)).iter().map(|t| t.name).collect();
            for t in ["list_templates", "get_template"] {
                assert!(ro.contains(&t), "{t} should be read-only: {ro:?}");
            }
            let mutating = [
                "register_template",
                "run_template",
                "launch_template",
                "rollback_template",
                "deprecate_template",
            ];
            for t in mutating {
                assert!(!ro.contains(&t), "{t} must be gated: {ro:?}");
            }
            let rw: Vec<&str> = tool_defs(&tpl_ctx(true)).iter().map(|t| t.name).collect();
            for t in mutating {
                assert!(rw.contains(&t), "{t} should appear with mutations: {rw:?}");
                let out = call_tool(&tpl_ctx(false), t, &json!({"id":"x","config":"y"})).await;
                assert_eq!(out["isError"], true, "{t} must be gated");
                assert!(
                    out["content"][0]["text"]
                        .as_str()
                        .unwrap()
                        .contains("--allow-mutations")
                );
            }
        }

        #[tokio::test]
        async fn an_operator_runs_templates_but_the_lifecycle_tools_are_admin_only() {
            let operator = tpl_ctx(true).with_template_admin(false);
            let names: Vec<&str> = tool_defs(&operator).iter().map(|t| t.name).collect();
            assert!(names.contains(&"run_template"), "{names:?}");
            for t in [
                "register_template",
                "launch_template",
                "rollback_template",
                "deprecate_template",
            ] {
                assert!(
                    !names.contains(&t),
                    "{t} must be hidden from an operator: {names:?}"
                );
                let out = call_tool(&operator, t, &json!({"id":"x","config":"y"})).await;
                assert_eq!(out["isError"], true, "{t}");
                assert!(
                    out["content"][0]["text"]
                        .as_str()
                        .unwrap()
                        .contains("admin-only"),
                    "{t}"
                );
            }
        }

        #[tokio::test]
        async fn register_list_get_and_run_round_trip() {
            let dir = tempfile::tempdir().unwrap();
            let ctx = tpl_ctx(true);

            // `launch: true` registers and goes live in one step.
            let out = call_tool(
                &ctx,
                "register_template",
                &json!({ "config": body(dir.path()), "launch": true }),
            )
            .await;
            assert_eq!(out["isError"], false, "{}", out["content"][0]["text"]);
            assert!(
                out["content"][0]["text"]
                    .as_str()
                    .unwrap()
                    .contains("mcp-tpl")
            );

            let out = call_tool(&ctx, "list_templates", &json!({})).await;
            let text = out["content"][0]["text"].as_str().unwrap();
            assert!(text.contains("\"count\": 1"), "{text}");
            assert!(text.contains("\"launched\""), "status is surfaced: {text}");

            let out = call_tool(&ctx, "get_template", &json!({"id":"mcp-tpl"})).await;
            assert_eq!(out["isError"], false);
            let text = out["content"][0]["text"].as_str().unwrap();
            assert!(text.contains("\"launches\""), "{text}");
            assert!(text.contains("${param.tag}"), "body is verbatim: {text}");

            let out = call_tool(&ctx, "get_template", &json!({"id":"nope"})).await;
            assert_eq!(out["isError"], true);

            // A missing required param is a tool error naming it.
            let out = call_tool(&ctx, "run_template", &json!({"id":"mcp-tpl"})).await;
            assert_eq!(out["isError"], true);
            assert!(out["content"][0]["text"].as_str().unwrap().contains("tag"));

            // dry_run materializes + validates without writing.
            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id":"mcp-tpl","params":{"tag":"dry"},"dry_run":true}),
            )
            .await;
            assert_eq!(out["isError"], false, "{}", out["content"][0]["text"]);
            assert!(
                out["content"][0]["text"]
                    .as_str()
                    .unwrap()
                    .contains("\"dry_run\": true")
            );
            assert!(!dir.path().join("out-dry.jsonl").exists());

            // The real run writes through the ordinary pipeline path.
            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id":"mcp-tpl","params":{"tag":"real"}}),
            )
            .await;
            assert_eq!(out["isError"], false, "{}", out["content"][0]["text"]);
            assert!(
                out["content"][0]["text"]
                    .as_str()
                    .unwrap()
                    .contains("\"records_written\": 2")
            );
            assert_eq!(
                std::fs::read_to_string(dir.path().join("out-real.jsonl"))
                    .unwrap()
                    .lines()
                    .count(),
                2
            );
        }

        #[tokio::test]
        async fn a_draft_template_is_not_runnable_unpinned() {
            let dir = tempfile::tempdir().unwrap();
            let ctx = tpl_ctx(true);
            // No `launch` — the work-in-progress state.
            call_tool(
                &ctx,
                "register_template",
                &json!({ "config": body(dir.path()) }),
            )
            .await;

            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id":"mcp-tpl","params":{"tag":"x"},"dry_run":true}),
            )
            .await;
            assert_eq!(out["isError"], true);
            let text = out["content"][0]["text"].as_str().unwrap();
            assert!(text.contains("no launched version"), "{text}");

            // An explicit build still runs, so a draft is testable.
            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id":"mcp-tpl","params":{"tag":"x"},"version":"newest","dry_run":true}),
            )
            .await;
            assert_eq!(out["isError"], false, "{}", out["content"][0]["text"]);
        }

        #[tokio::test]
        async fn launch_rollback_and_deprecate_tools() {
            let dir = tempfile::tempdir().unwrap();
            let ctx = tpl_ctx(true);
            call_tool(
                &ctx,
                "register_template",
                &json!({ "config": body(dir.path()), "launch": true }),
            )
            .await; // v1 live
            call_tool(
                &ctx,
                "register_template",
                &json!({ "config": body(dir.path()) }),
            )
            .await; // v2 build

            // Launch defaults to `newest`.
            let out = call_tool(&ctx, "launch_template", &json!({"id":"mcp-tpl"})).await;
            assert_eq!(out["isError"], false, "{}", out["content"][0]["text"]);
            let text = out["content"][0]["text"].as_str().unwrap();
            assert!(text.contains("\"version\": 2"), "{text}");
            assert!(text.contains("\"replaced\": 1"), "{text}");

            // Rollback returns to v1.
            let out = call_tool(&ctx, "rollback_template", &json!({"id":"mcp-tpl"})).await;
            assert_eq!(out["isError"], false, "{}", out["content"][0]["text"]);
            assert!(
                out["content"][0]["text"]
                    .as_str()
                    .unwrap()
                    .contains("\"version\": 1")
            );

            // Deprecate, then revive.
            let out = call_tool(
                &ctx,
                "deprecate_template",
                &json!({"id":"mcp-tpl","reason":"superseded"}),
            )
            .await;
            assert!(
                out["content"][0]["text"]
                    .as_str()
                    .unwrap()
                    .contains("deprecated")
            );
            // Launching into a retired template is refused.
            let out = call_tool(&ctx, "launch_template", &json!({"id":"mcp-tpl"})).await;
            assert_eq!(out["isError"], true);
            let out = call_tool(
                &ctx,
                "deprecate_template",
                &json!({"id":"mcp-tpl","undo":true}),
            )
            .await;
            assert!(
                out["content"][0]["text"]
                    .as_str()
                    .unwrap()
                    .contains("launched")
            );
        }

        #[tokio::test]
        async fn register_with_tags_and_run_by_channel() {
            let dir = tempfile::tempdir().unwrap();
            let ctx = tpl_ctx(true);

            // v1 live; v2 tagged `dev` but not launched.
            call_tool(
                &ctx,
                "register_template",
                &json!({ "config": body(dir.path()), "launch": true }),
            )
            .await;
            let out = call_tool(
                &ctx,
                "register_template",
                &json!({ "config": body(dir.path()), "tags": ["dev"] }),
            )
            .await;
            assert_eq!(out["isError"], false, "{}", out["content"][0]["text"]);

            let out = call_tool(&ctx, "get_template", &json!({"id":"mcp-tpl"})).await;
            let text = out["content"][0]["text"].as_str().unwrap();
            assert!(text.contains("\"stable\": 1"), "{text}");
            assert!(text.contains("\"newest\": 2"), "{text}");
            assert!(text.contains("\"dev\": 2"), "{text}");

            // Each selector resolves to its own version.
            for (version, want) in [
                (json!("stable"), 1),
                (json!("dev"), 2),
                (json!("newest"), 2),
                (json!(1), 1),
            ] {
                let out = call_tool(
                    &ctx,
                    "run_template",
                    &json!({"id":"mcp-tpl","params":{"tag":"c"},"version":version,"dry_run":true}),
                )
                .await;
                assert_eq!(out["isError"], false, "{}", out["content"][0]["text"]);
                let text = out["content"][0]["text"].as_str().unwrap();
                assert!(
                    text.contains(&format!("\"template_version\": {want}")),
                    "{version} should resolve to v{want}: {text}"
                );
            }

            // A channel outside the closed set is a tool error, on both paths.
            for args in [
                json!({ "config": body(dir.path()), "tags": ["prd"] }),
                json!({ "config": body(dir.path()), "tags": ["stable"] }),
            ] {
                let out = call_tool(&ctx, "register_template", &args).await;
                assert_eq!(out["isError"], true, "{args}");
            }
            for bad in ["nope", "latest", "canary"] {
                let out = call_tool(
                    &ctx,
                    "run_template",
                    &json!({"id":"mcp-tpl","params":{"tag":"c"},"version":bad}),
                )
                .await;
                assert_eq!(out["isError"], true, "version={bad} must be refused");
            }
        }

        #[tokio::test]
        async fn register_rejects_an_invalid_config() {
            let out = call_tool(
                &tpl_ctx(true),
                "register_template",
                &json!({ "config": "version: 1\nname: x\nbogus: 1\npipeline: {}\n" }),
            )
            .await;
            assert_eq!(out["isError"], true);
        }

        #[tokio::test]
        async fn env_overrides_flow_through_run_template() {
            let dir = tempfile::tempdir().unwrap();
            let ctx = tpl_ctx(true);
            let csv = dir.path().join("in.csv");
            std::fs::write(&csv, "id\n1\n").unwrap();
            let cfg = format!(
                "version: 1\nname: mcp-env\npipeline:\n  source:\n    type: csv\n    config:\n      path: {}\n  sink:\n    type: jsonl\n    config:\n      path: {}/out-${{env:MCP_TPL_SUFFIX}}.jsonl\n",
                csv.display(),
                dir.path().display()
            );
            call_tool(
                &ctx,
                "register_template",
                &json!({ "config": cfg, "launch": true }),
            )
            .await;
            let out = call_tool(
                &ctx,
                "run_template",
                &json!({"id":"mcp-env","env":{"MCP_TPL_SUFFIX":"eu"}}),
            )
            .await;
            assert_eq!(out["isError"], false, "{}", out["content"][0]["text"]);
            assert!(dir.path().join("out-eu.jsonl").exists());
        }
    }
}