lumen-sqlite-mcp 0.1.1

An MCP server for storing and manipulating structured data using SQLite
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
//! lumen-sqlite — An MCP server for structured data storage using SQLite.
//!
//! This server exposes tools that let a language model create, query, and manage
//! SQLite databases through the Model Context Protocol.

use std::collections::HashMap;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Mutex;

use csv as csv_crate;
use regex::Regex;
use rmcp::handler::server::router::tool::ToolRouter;
use rmcp::handler::server::wrapper::Parameters;
use rmcp::model::{ServerCapabilities, ServerInfo};
use rmcp::schemars::JsonSchema;
use rmcp::{ServerHandler, ServiceExt, tool, tool_handler, tool_router};
use rusqlite::types::{ToSqlOutput, Value as SqlValue, ValueRef};
use rusqlite::{Connection, MAIN_DB};
use serde::Deserialize;
use tracing::info;

// ---------------------------------------------------------------------------
// State
// ---------------------------------------------------------------------------

/// An open database connection with its resolved file path.
struct OpenDatabase {
    conn: Connection,
    path: PathBuf,
}

/// Server state shared across all tool invocations.
pub struct LumenSqlite {
    /// Tool router for MCP dispatch.
    tool_router: ToolRouter<Self>,
    /// Open database connections keyed by alias.
    connections: Mutex<HashMap<String, OpenDatabase>>,
    /// Base directory for resolving relative database paths.
    db_dir: PathBuf,
    /// Maximum rows returned by execute_sql.
    default_max_rows: usize,
    /// Maximum rows for export_query_csv.
    default_export_max_rows: usize,
    /// When true, all paths must resolve under db_dir.
    sandbox: bool,
}

/// Allowed file extensions for database files.
const ALLOWED_EXTENSIONS: &[&str] = &["db", "sqlite", "sqlite3"];

impl LumenSqlite {
    pub fn new() -> Self {
        let db_dir = std::env::var("LUMEN_SQLITE_DB_DIR")
            .map(PathBuf::from)
            .unwrap_or_else(|_| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
        let db_dir = db_dir.canonicalize().unwrap_or_else(|_| db_dir.clone());

        let default_max_rows = std::env::var("LUMEN_SQLITE_MAX_ROWS")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(500);

        let default_export_max_rows = std::env::var("LUMEN_SQLITE_EXPORT_MAX_ROWS")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(10_000);

        let sandbox = std::env::var("LUMEN_SQLITE_SANDBOX")
            .map(|v| matches!(v.to_lowercase().as_str(), "1" | "true" | "yes"))
            .unwrap_or(false);

        Self {
            tool_router: Self::tool_router(),
            connections: Mutex::new(HashMap::new()),
            db_dir,
            default_max_rows,
            default_export_max_rows,
            sandbox,
        }
    }

    /// Resolve and validate a database file path.
    fn resolve_db_path(&self, path_str: &str) -> Result<PathBuf, String> {
        let p = Path::new(path_str);
        let p = if p.is_absolute() {
            p.to_path_buf()
        } else {
            self.db_dir.join(p)
        };

        // Canonicalize if the file already exists; otherwise canonicalize the parent.
        let resolved = if p.exists() {
            p.canonicalize()
                .map_err(|e| format!("Cannot resolve path: {e}"))?
        } else {
            let parent = p
                .parent()
                .ok_or_else(|| "Invalid path: no parent directory".to_string())?;
            let parent = if parent.exists() {
                parent
                    .canonicalize()
                    .map_err(|e| format!("Cannot resolve parent: {e}"))?
            } else {
                return Err(format!(
                    "Parent directory '{}' does not exist",
                    parent.display()
                ));
            };
            parent.join(p.file_name().unwrap())
        };

        // Check extension
        let ext = resolved.extension().and_then(|e| e.to_str()).unwrap_or("");
        if !ALLOWED_EXTENSIONS.contains(&ext) {
            return Err(format!(
                "File extension '.{ext}' is not allowed. Use one of: {}",
                ALLOWED_EXTENSIONS
                    .iter()
                    .map(|e| format!(".{e}"))
                    .collect::<Vec<_>>()
                    .join(", ")
            ));
        }

        // Sandbox check
        if self.sandbox && !resolved.starts_with(&self.db_dir) {
            return Err(format!(
                "Path '{}' is outside the allowed directory '{}'. \
                 Sandbox mode is enabled (LUMEN_SQLITE_SANDBOX=1).",
                resolved.display(),
                self.db_dir.display()
            ));
        }

        Ok(resolved)
    }

    /// Resolve a file path for CSV/TSV I/O (no extension restriction).
    fn resolve_file_path(&self, path_str: &str) -> Result<PathBuf, String> {
        let p = Path::new(path_str);
        let p = if p.is_absolute() {
            p.to_path_buf()
        } else {
            self.db_dir.join(p)
        };

        // For files that already exist, canonicalize fully.
        // For new output files, canonicalize the parent directory.
        let resolved = if p.exists() {
            p.canonicalize()
                .map_err(|e| format!("Cannot resolve path: {e}"))?
        } else {
            let parent = p
                .parent()
                .ok_or_else(|| "Invalid path: no parent directory".to_string())?;
            let parent = if parent.as_os_str().is_empty() {
                self.db_dir.clone()
            } else if parent.exists() {
                parent
                    .canonicalize()
                    .map_err(|e| format!("Cannot resolve parent: {e}"))?
            } else {
                return Err(format!(
                    "Parent directory '{}' does not exist",
                    parent.display()
                ));
            };
            parent.join(p.file_name().unwrap())
        };

        // Sandbox check
        if self.sandbox && !resolved.starts_with(&self.db_dir) {
            return Err(format!(
                "Path '{}' is outside the allowed directory '{}'. \
                 Sandbox mode is enabled (LUMEN_SQLITE_SANDBOX=1).",
                resolved.display(),
                self.db_dir.display()
            ));
        }

        Ok(resolved)
    }
}

// ---------------------------------------------------------------------------
// SQL value conversion helpers
// ---------------------------------------------------------------------------

/// A JSON value that can be passed as a SQLite bound parameter.
struct JsonParam(serde_json::Value);

impl rusqlite::ToSql for JsonParam {
    fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
        Ok(match &self.0 {
            serde_json::Value::Null => ToSqlOutput::Owned(SqlValue::Null),
            serde_json::Value::Bool(b) => ToSqlOutput::Owned(SqlValue::Integer(*b as i64)),
            serde_json::Value::Number(n) => {
                if let Some(i) = n.as_i64() {
                    ToSqlOutput::Owned(SqlValue::Integer(i))
                } else {
                    ToSqlOutput::Owned(SqlValue::Real(n.as_f64().unwrap_or(0.0)))
                }
            }
            serde_json::Value::String(s) => ToSqlOutput::Owned(SqlValue::Text(s.clone())),
            other => ToSqlOutput::Owned(SqlValue::Text(other.to_string())),
        })
    }
}

fn value_ref_to_string(vr: ValueRef<'_>) -> String {
    match vr {
        ValueRef::Null => String::new(),
        ValueRef::Integer(i) => i.to_string(),
        ValueRef::Real(f) => f.to_string(),
        ValueRef::Text(t) => String::from_utf8_lossy(t).into_owned(),
        ValueRef::Blob(b) => format!("<blob {} bytes>", b.len()),
    }
}

/// Validate that a name is a safe SQL identifier.
fn validate_identifier(name: &str, kind: &str) -> Result<(), String> {
    if name.is_empty() {
        return Err(format!("Invalid {kind}: empty string is not allowed."));
    }
    let mut chars = name.chars();
    let first = chars.next().unwrap();
    if !first.is_ascii_alphabetic() && first != '_' {
        return Err(format!(
            "Invalid {kind}: {name:?}. Must start with a letter or underscore."
        ));
    }
    if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
        return Err(format!(
            "Invalid {kind}: {name:?}. Must match [A-Za-z_][A-Za-z0-9_]*"
        ));
    }
    Ok(())
}

/// Escape a SQL identifier for use inside double-quotes.
fn escape_identifier(name: &str) -> String {
    name.replace('"', "\"\"")
}

/// Return the CSV delimiter byte to use for a file.
/// If `delimiter` is explicitly given, parse it (accepts single ASCII char or `\t`).
/// Otherwise, auto-detect from the file extension: `.tsv` → tab, everything else → comma.
fn parse_delimiter(delimiter: &Option<String>, path: &Path) -> Result<u8, String> {
    if let Some(d) = delimiter {
        // Accept the two-character escape sequence "\t" as a tab
        if d == r"\t" || d == "\t" {
            return Ok(b'\t');
        }
        let bytes = d.as_bytes();
        if bytes.len() == 1 {
            return Ok(bytes[0]);
        }
        return Err(format!(
            "delimiter must be a single character (or \\t for tab), got: {d:?}"
        ));
    }
    // Auto-detect from extension
    if path.extension().and_then(|e| e.to_str()) == Some("tsv") {
        Ok(b'\t')
    } else {
        Ok(b',')
    }
}

/// Guess the best SQLite column type by sampling a slice of string values.
/// Returns "INTEGER", "REAL", or "TEXT".
fn infer_sqlite_type(vals: &[&str]) -> &'static str {
    let non_empty: Vec<&&str> = vals.iter().filter(|v| !v.is_empty()).collect();
    if non_empty.is_empty() {
        return "TEXT";
    }
    if non_empty.iter().all(|v| v.parse::<i64>().is_ok()) {
        return "INTEGER";
    }
    if non_empty.iter().all(|v| v.parse::<f64>().is_ok()) {
        return "REAL";
    }
    "TEXT"
}

/// Map a serde_json::Value to the SQLite column type name it represents.
fn json_value_to_sqlite_type(v: &serde_json::Value) -> &'static str {
    match v {
        serde_json::Value::Bool(_) | serde_json::Value::Number(_) => {
            if let Some(n) = v.as_i64().or_else(|| v.as_u64().map(|u| u as i64)) {
                let _ = n;
                "INTEGER"
            } else {
                "REAL"
            }
        }
        _ => "TEXT",
    }
}

/// Shared implementation for import_json and import_json_file.
fn import_json_objects(
    connections: &Mutex<HashMap<String, OpenDatabase>>,
    alias: &str,
    table: &str,
    if_exists: &str,
    col_names: &[String],
    arr: &[serde_json::Map<String, serde_json::Value>],
) -> String {
    // Infer types from first non-null value per column
    let col_types: Vec<&'static str> = col_names
        .iter()
        .map(|col| {
            for obj in arr.iter().take(200) {
                if let Some(v) = obj.get(col) {
                    if !v.is_null() {
                        return json_value_to_sqlite_type(v);
                    }
                }
            }
            "TEXT"
        })
        .collect();

    let conns = connections.lock().unwrap();
    let db = match conns.get(alias) {
        Some(d) => d,
        None => return LumenSqlite::no_such_alias(alias, &conns),
    };

    let escaped_table = escape_identifier(table);
    let col_defs: String = col_names
        .iter()
        .zip(col_types.iter())
        .map(|(c, t)| format!("\"{}\" {t}", escape_identifier(c)))
        .collect::<Vec<_>>()
        .join(", ");
    let placeholders: String = (0..col_names.len())
        .map(|_| "?")
        .collect::<Vec<_>>()
        .join(", ");
    let insert_sql = format!("INSERT INTO \"{escaped_table}\" VALUES ({placeholders})");

    let result: rusqlite::Result<()> = (|| {
        if if_exists == "replace" {
            db.conn
                .execute_batch(&format!("DROP TABLE IF EXISTS \"{escaped_table}\""))?;
        } else if if_exists == "error" {
            let exists: bool = db.conn.query_row(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?1",
                [table],
                |row| row.get::<_, i64>(0),
            )? > 0;
            if exists {
                return Err(rusqlite::Error::SqliteFailure(
                    rusqlite::ffi::Error::new(rusqlite::ffi::SQLITE_ERROR),
                    Some(format!(
                        "Table '{table}' already exists. Use if_exists='replace' to overwrite or if_exists='append' to add rows."
                    )),
                ));
            }
        }
        db.conn.execute_batch(&format!(
            "CREATE TABLE IF NOT EXISTS \"{escaped_table}\" ({col_defs})"
        ))?;
        db.conn.execute_batch("BEGIN TRANSACTION")?;
        let mut stmt = db.conn.prepare(&insert_sql)?;
        for obj in arr {
            let vals: Vec<serde_json::Value> = col_names
                .iter()
                .map(|c| obj.get(c).cloned().unwrap_or(serde_json::Value::Null))
                .collect();
            let row_refs: Vec<JsonParam> = vals.into_iter().map(JsonParam).collect();
            let row_sql_refs: Vec<&dyn rusqlite::ToSql> =
                row_refs.iter().map(|p| p as &dyn rusqlite::ToSql).collect();
            stmt.execute(row_sql_refs.as_slice())?;
        }
        drop(stmt);
        db.conn.execute_batch("COMMIT")?;
        Ok(())
    })();

    match result {
        Ok(()) => format!(
            "Imported {} rows into '{table}' ({} columns: {}).",
            arr.len(),
            col_names.len(),
            col_names.join(", ")
        ),
        Err(e) => {
            let _ = db.conn.execute_batch("ROLLBACK");
            format!("Import error: {e}")
        }
    }
}

// ---------------------------------------------------------------------------
// Tool parameter types
// ---------------------------------------------------------------------------

#[derive(Deserialize, JsonSchema)]
pub struct OpenDatabaseParams {
    /// Path to the .db / .sqlite / .sqlite3 file.
    /// Relative paths are resolved against the server's base directory.
    pub path: String,
    /// Short alias for this database (defaults to file stem).
    pub alias: Option<String>,
}

#[derive(Deserialize, JsonSchema)]
pub struct AliasParams {
    /// The alias of the database.
    pub alias: String,
}

#[derive(Deserialize, JsonSchema)]
pub struct BackupDatabaseParams {
    /// The alias of the source database.
    pub alias: String,
    /// Path for the backup file (.db / .sqlite / .sqlite3).
    pub destination: String,
}

#[derive(Deserialize, JsonSchema)]
pub struct CreateReadmeParams {
    /// The alias of the database.
    pub alias: String,
    /// A one-sentence description of the database's purpose.
    pub purpose: String,
    /// Optional map of table names to descriptions.
    pub tables: Option<HashMap<String, String>>,
}

#[derive(Deserialize, JsonSchema)]
pub struct ExecuteSqlParams {
    /// The alias of the database to query.
    pub alias: String,
    /// The SQL statement to execute.
    pub sql: String,
    /// Optional list of bind parameters (for parameterized queries using ? placeholders).
    pub params: Option<Vec<serde_json::Value>>,
    /// Maximum rows to return per page (default: from env or 500).
    pub max_rows: Option<usize>,
    /// Row offset for pagination (default: 0). If the response says more rows are available,
    /// call again with offset incremented by the number of rows returned.
    pub offset: Option<usize>,
}

#[derive(Deserialize, JsonSchema)]
pub struct ExecuteScriptParams {
    /// The alias of the database.
    pub alias: String,
    /// SQL script containing one or more statements separated by semicolons.
    pub script: String,
}

#[derive(Deserialize, JsonSchema)]
pub struct ExplainQueryParams {
    /// The alias of the database.
    pub alias: String,
    /// The SQL query to explain.
    pub sql: String,
    /// Optional bind parameters.
    pub params: Option<Vec<serde_json::Value>>,
}

#[derive(Deserialize, JsonSchema)]
pub struct BulkInsertParams {
    /// The alias of the database.
    pub alias: String,
    /// The table to insert into.
    pub table: String,
    /// List of column names, e.g. ["name", "age", "city"].
    pub columns: Vec<String>,
    /// List of rows, each row being a list of values matching the columns.
    pub rows: Vec<Vec<serde_json::Value>>,
    /// Conflict resolution: "error" (default), "replace" (INSERT OR REPLACE),
    /// "ignore" (INSERT OR IGNORE — skips rows that violate a constraint).
    pub conflict: Option<String>,
}

#[derive(Deserialize, JsonSchema)]
pub struct ImportCsvParams {
    /// The alias of the database.
    pub alias: String,
    /// CSV-formatted text with a header row followed by data rows.
    pub csv_text: String,
    /// Target table name. Defaults to "imported".
    pub table: Option<String>,
    /// What to do if the table already exists: "append" (default) or "replace".
    pub if_exists: Option<String>,
}

#[derive(Deserialize, JsonSchema)]
pub struct ExportQueryCsvParams {
    /// The alias of the database.
    pub alias: String,
    /// A SELECT statement.
    pub sql: String,
    /// Optional bind parameters.
    pub params: Option<Vec<serde_json::Value>>,
    /// Maximum rows to export per page (default: 10000).
    pub max_rows: Option<usize>,
    /// Row offset for pagination (default: 0).
    pub offset: Option<usize>,
}

#[derive(Deserialize, JsonSchema)]
pub struct TableParams {
    /// The alias of the database.
    pub alias: String,
    /// The name of the table.
    pub table: String,
}

#[derive(Deserialize, JsonSchema)]
pub struct DropTableParams {
    /// The alias of the database.
    pub alias: String,
    /// The name of the table to drop.
    pub table: String,
    /// Set to true to actually drop the table. Default false (preview only).
    pub confirm: Option<bool>,
}

#[derive(Deserialize, JsonSchema)]
pub struct RenameTableParams {
    /// The alias of the database.
    pub alias: String,
    /// Current table name.
    pub old_name: String,
    /// New table name.
    pub new_name: String,
}

#[derive(Deserialize, JsonSchema)]
pub struct InspectCsvFileParams {
    /// Path to the CSV or TSV file to inspect. Absolute or relative to LUMEN_SQLITE_DB_DIR.
    pub path: String,
    /// Column delimiter. "," for CSV (default), "\t" for TSV. Single character.
    pub delimiter: Option<String>,
    /// Number of data rows to sample for type inference (default: 5).
    pub sample_rows: Option<usize>,
}

#[derive(Deserialize, JsonSchema)]
pub struct ImportCsvFileParams {
    /// The alias of the open database to import into.
    pub alias: String,
    /// Path to the CSV or TSV file. Absolute or relative to LUMEN_SQLITE_DB_DIR.
    pub path: String,
    /// Target table name. Required when has_headers is false; defaults to the file stem otherwise.
    pub table: Option<String>,
    /// Whether the first row is a header row with column names (default: true).
    pub has_headers: Option<bool>,
    /// Column names to use when has_headers is false. Must match the number of columns in the file.
    pub columns: Option<Vec<String>>,
    /// Column delimiter. "," for CSV (default), "\t" for TSV. Single character.
    pub delimiter: Option<String>,
    /// What to do if the table already exists: "append" (default) or "replace".
    pub if_exists: Option<String>,
}

#[derive(Deserialize, JsonSchema)]
pub struct ExportQueryCsvFileParams {
    /// The alias of the open database.
    pub alias: String,
    /// A SELECT statement to export.
    pub sql: String,
    /// Optional bind parameters for the query.
    pub params: Option<Vec<serde_json::Value>>,
    /// Path to write the output file. Absolute or relative to LUMEN_SQLITE_DB_DIR.
    pub path: String,
    /// Whether to emit a header row with column names (default: true).
    pub emit_headers: Option<bool>,
    /// Column delimiter. "," for CSV (default), "\t" for TSV. Single character.
    pub delimiter: Option<String>,
    /// Maximum rows to export (default: 10000). Set 0 for unlimited.
    pub max_rows: Option<usize>,
}

#[derive(Deserialize, JsonSchema)]
pub struct ImportJsonParams {
    /// The alias of the open database.
    pub alias: String,
    /// A JSON array of objects to import. All objects should share the same keys.
    pub json_text: String,
    /// Target table name (default: "imported").
    pub table: Option<String>,
    /// What to do if the table already exists: "error" (default), "replace", "append".
    pub if_exists: Option<String>,
}

#[derive(Deserialize, JsonSchema)]
pub struct ImportJsonFileParams {
    /// The alias of the open database.
    pub alias: String,
    /// Path to the JSON file. Absolute or relative to LUMEN_SQLITE_DB_DIR.
    pub path: String,
    /// Target table name (default: file stem).
    pub table: Option<String>,
    /// File format: "array" (default, a JSON array of objects) or "ndjson" (one JSON object per line).
    pub format: Option<String>,
    /// What to do if the table already exists: "error" (default), "replace", "append".
    pub if_exists: Option<String>,
}

#[derive(Deserialize, JsonSchema)]
pub struct ExportQueryJsonFileParams {
    /// The alias of the open database.
    pub alias: String,
    /// A SELECT statement to export.
    pub sql: String,
    /// Optional bind parameters for the query.
    pub params: Option<Vec<serde_json::Value>>,
    /// Path to write the output file. Absolute or relative to LUMEN_SQLITE_DB_DIR.
    pub path: String,
    /// Output format: "array" (default, a JSON array of objects) or "ndjson" (one JSON object per line).
    pub format: Option<String>,
    /// Maximum rows to export (default: 10000). Set 0 for unlimited.
    pub max_rows: Option<usize>,
}

#[derive(Deserialize, JsonSchema)]
pub struct ExportQueryMarkdownParams {
    /// The alias of the open database.
    pub alias: String,
    /// A SELECT statement to render.
    pub sql: String,
    /// Optional bind parameters for the query.
    pub params: Option<Vec<serde_json::Value>>,
    /// Maximum rows to return per page (default: from env or 500).
    pub max_rows: Option<usize>,
    /// Row offset for pagination (default: 0).
    pub offset: Option<usize>,
}

#[derive(Deserialize, JsonSchema)]
pub struct TableStatsParams {
    /// The alias of the database.
    pub alias: String,
    /// The table to profile.
    pub table: String,
    /// Optional subset of columns to include. Defaults to all columns.
    pub columns: Option<Vec<String>>,
}

#[derive(Deserialize, JsonSchema)]
pub struct AttachDatabaseParams {
    /// The alias of the already-open database whose connection will run ATTACH.
    pub alias: String,
    /// Path to the SQLite database file to attach (absolute or relative to db_dir).
    pub path: String,
    /// Schema name to use in SQL (e.g. "other" → SELECT * FROM "other".my_table).
    pub schema_name: String,
}

#[derive(Deserialize, JsonSchema)]
pub struct DetachDatabaseParams {
    /// The alias of the database connection from which to detach.
    pub alias: String,
    /// The schema name to detach (as given to attach_database).
    pub schema_name: String,
}

#[derive(Deserialize, JsonSchema)]
pub struct CreateIndexParams {
    /// The alias of the database.
    pub alias: String,
    /// The table to index.
    pub table: String,
    /// Columns to include in the index, in order.
    pub columns: Vec<String>,
    /// Index name. Auto-generated as idx_table_col1_col2 if not provided.
    pub index_name: Option<String>,
    /// Whether to create a UNIQUE index (default: false).
    pub unique: Option<bool>,
    /// Optional WHERE clause for a partial index (e.g. "status = 'active'").
    pub where_clause: Option<String>,
    /// Use IF NOT EXISTS (default: false).
    pub if_not_exists: Option<bool>,
}

#[derive(Deserialize, JsonSchema)]
pub struct DropIndexParams {
    /// The alias of the database.
    pub alias: String,
    /// The name of the index to drop.
    pub index_name: String,
    /// Set to true to actually drop. Default false shows a preview.
    pub confirm: Option<bool>,
}

#[derive(Deserialize, JsonSchema)]
pub struct AddColumnParams {
    /// The alias of the database.
    pub alias: String,
    /// The table to alter.
    pub table: String,
    /// Name of the new column.
    pub column: String,
    /// SQLite column type (default: TEXT). Use INTEGER, REAL, BLOB, or TEXT.
    pub col_type: Option<String>,
    /// Whether the column is NOT NULL (default: false). Requires default_value.
    pub not_null: Option<bool>,
    /// Default value expression (e.g. "0", "'unknown'", "datetime('now')").
    pub default_value: Option<String>,
}

// ---------------------------------------------------------------------------
// MCP Tools
// ---------------------------------------------------------------------------

#[tool_router]
impl LumenSqlite {
    #[tool(
        description = "Open or create a SQLite database file and assign it an alias (defaults to the file stem). Creates the file if it does not exist."
    )]
    async fn open_database(&self, params: Parameters<OpenDatabaseParams>) -> String {
        let params = params.0;
        let db_path = match self.resolve_db_path(&params.path) {
            Ok(p) => p,
            Err(e) => return e,
        };

        let alias = params.alias.unwrap_or_else(|| {
            db_path
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("db")
                .to_string()
        });

        let mut conns = self.connections.lock().unwrap();
        if conns.contains_key(&alias) {
            return format!(
                "Database '{alias}' is already open. \
                 Close it first or choose a different alias."
            );
        }

        let creating = !db_path.exists();
        let conn = match Connection::open(&db_path) {
            Ok(c) => c,
            Err(e) => return format!("Failed to open database: {e}"),
        };

        // Enable WAL mode and foreign keys
        let _ = conn.execute_batch("PRAGMA journal_mode=WAL; PRAGMA foreign_keys=ON;");

        // Register REGEXP scalar function: text REGEXP pattern
        // Returns 1 if pattern matches anywhere in text, 0 otherwise.
        // NULL input yields NULL. Invalid patterns return an error.
        conn.create_scalar_function(
            "regexp",
            2,
            rusqlite::functions::FunctionFlags::SQLITE_UTF8
                | rusqlite::functions::FunctionFlags::SQLITE_DETERMINISTIC,
            |ctx| {
                let pattern: Option<String> = ctx.get(0)?;
                let text: Option<String> = ctx.get(1)?;
                match (pattern, text) {
                    (Some(pat), Some(txt)) => {
                        let re = Regex::new(&pat).map_err(|e| {
                            rusqlite::Error::UserFunctionError(Box::new(std::io::Error::new(
                                std::io::ErrorKind::InvalidInput,
                                e.to_string(),
                            )))
                        })?;
                        Ok(re.is_match(&txt) as i64)
                    }
                    _ => Ok(0i64),
                }
            },
        )
        .map_err(|e| format!("Failed to register regexp UDF: {e}"))
        .unwrap_or(());

        let verb = if creating {
            "Created and opened"
        } else {
            "Opened"
        };
        info!("{verb} database '{alias}' at {}", db_path.display());
        conns.insert(
            alias.clone(),
            OpenDatabase {
                conn,
                path: db_path.clone(),
            },
        );
        format!("{verb} database '{alias}' at {}", db_path.display())
    }

    #[tool(description = "Close an open database connection.")]
    async fn close_database(&self, params: Parameters<AliasParams>) -> String {
        let alias = params.0.alias;
        let mut conns = self.connections.lock().unwrap();
        if conns.remove(&alias).is_some() {
            info!("Closed database '{alias}'");
            format!("Closed database '{alias}'.")
        } else {
            Self::no_such_alias(&alias, &conns)
        }
    }

    #[tool(description = "List all currently open database aliases.")]
    async fn list_databases(&self) -> String {
        let conns = self.connections.lock().unwrap();
        if conns.is_empty() {
            "(none)".to_string()
        } else {
            let mut aliases: Vec<&str> = conns.keys().map(|s| s.as_str()).collect();
            aliases.sort();
            aliases.join("\n")
        }
    }

    #[tool(
        description = "List .db files in the data directory, including ones not currently open. Useful for discovering databases from prior sessions."
    )]
    async fn list_database_files(&self) -> String {
        let mut files: Vec<PathBuf> = Vec::new();
        for ext in ALLOWED_EXTENSIONS {
            let pattern = self.db_dir.join(format!("*.{ext}"));
            if let Ok(entries) = glob::glob(pattern.to_string_lossy().as_ref()) {
                for entry in entries.flatten() {
                    files.push(entry);
                }
            }
        }
        files.sort();

        if files.is_empty() {
            return format!("No database files found in {}", self.db_dir.display());
        }

        let mut lines = vec![
            format!("Database files in {}:", self.db_dir.display()),
            String::new(),
            format!("{:<40} {:>10} {}", "File", "Size", "Modified"),
            "-".repeat(70),
        ];

        for f in &files {
            if let Ok(meta) = f.metadata() {
                let size = meta.len();
                let size_str = if size < 1024 * 1024 {
                    format!("{:.1} KB", size as f64 / 1024.0)
                } else {
                    format!("{:.1} MB", size as f64 / (1024.0 * 1024.0))
                };
                let mtime = meta
                    .modified()
                    .ok()
                    .and_then(|t| {
                        let secs = t.duration_since(std::time::UNIX_EPOCH).ok()?.as_secs();
                        // Format as YYYY-MM-DD HH:MM using chrono
                        use chrono::{DateTime, TimeZone, Utc};
                        let dt: DateTime<Utc> = Utc.timestamp_opt(secs as i64, 0).single()?;
                        Some(dt.format("%Y-%m-%d %H:%M").to_string())
                    })
                    .unwrap_or_else(|| "?".to_string());
                let name = f.file_name().and_then(|n| n.to_str()).unwrap_or("?");
                lines.push(format!("{:<40} {:>10} {}", name, size_str, mtime));
            }
        }
        lines.join("\n")
    }

    #[tool(
        description = "Get a comprehensive summary of an open database: _readme contents, all tables with row counts and column schemas. Use this instead of calling list_tables + describe_table + SELECT COUNT(*) repeatedly."
    )]
    async fn database_info(&self, params: Parameters<AliasParams>) -> String {
        let alias = params.0.alias;
        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let mut lines = vec![format!("Database: {alias}"), "=".repeat(60)];

        // _readme contents
        let has_readme: bool = db
            .conn
            .query_row(
                "SELECT name FROM sqlite_master WHERE type='table' AND name='_readme'",
                [],
                |_| Ok(true),
            )
            .unwrap_or(false);

        if has_readme {
            lines.push(String::new());
            lines.push("## _readme".to_string());
            if let Ok(mut stmt) = db
                .conn
                .prepare("SELECT key, value FROM _readme ORDER BY key")
            {
                if let Ok(rows) = stmt.query_map([], |row| {
                    Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
                }) {
                    for row in rows.flatten() {
                        lines.push(format!("  {}: {}", row.0, row.1));
                    }
                }
            }
        }

        // All tables
        let tables: Vec<String> = {
            let mut stmt = match db
                .conn
                .prepare("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
            {
                Ok(s) => s,
                Err(e) => return format!("Error reading tables: {e}"),
            };
            stmt.query_map([], |row| row.get::<_, String>(0))
                .map(|mapped| mapped.flatten().collect())
                .unwrap_or_default()
        };

        if tables.is_empty() {
            lines.push(String::new());
            lines.push("(no tables)".to_string());
            return lines.join("\n");
        }

        lines.push(String::new());
        lines.push("## Tables".to_string());

        for table in &tables {
            if table == "sqlite_sequence" {
                continue;
            }
            let safe = escape_identifier(table);

            let count: i64 = db
                .conn
                .query_row(&format!("SELECT COUNT(*) FROM \"{safe}\""), [], |r| {
                    r.get(0)
                })
                .unwrap_or(-1);
            let count_str = if count < 0 {
                "?".to_string()
            } else {
                count.to_string()
            };

            lines.push(String::new());
            lines.push(format!("### {table} ({count_str} rows)"));

            if let Ok(mut stmt) = db.conn.prepare(&format!("PRAGMA table_info(\"{safe}\")")) {
                if let Ok(cols) = stmt.query_map([], |row| {
                    Ok((
                        row.get::<_, String>(1)?,         // name
                        row.get::<_, String>(2)?,         // type
                        row.get::<_, i32>(3)?,            // notnull
                        row.get::<_, Option<String>>(4)?, // default
                        row.get::<_, i32>(5)?,            // pk
                    ))
                }) {
                    for col in cols.flatten() {
                        let (name, col_type, notnull, default_val, pk) = col;
                        let mut parts = vec![format!("  {name}")];
                        if !col_type.is_empty() {
                            parts.push(col_type);
                        }
                        if pk != 0 {
                            parts.push("PK".to_string());
                        }
                        if notnull != 0 {
                            parts.push("NOT NULL".to_string());
                        }
                        if let Some(d) = default_val {
                            parts.push(format!("DEFAULT {d}"));
                        }
                        lines.push(parts.join(" "));
                    }
                }
            }

            // Indexes on this table
            let indexes: Vec<(String, i32)> = {
                let mut stmt = db
                    .conn
                    .prepare(&format!("PRAGMA index_list(\"{safe}\")"))
                    .ok();
                stmt.as_mut()
                    .and_then(|s| {
                        s.query_map([], |row| {
                            Ok((
                                row.get::<_, String>(1)?, // name
                                row.get::<_, i32>(2)?,    // unique
                            ))
                        })
                        .ok()
                        .map(|m| m.flatten().collect())
                    })
                    .unwrap_or_default()
            };
            for (idx_name, unique) in &indexes {
                let safe_idx = escape_identifier(idx_name);
                let cols: Vec<String> = {
                    let mut stmt = db
                        .conn
                        .prepare(&format!("PRAGMA index_info(\"{safe_idx}\")"))
                        .ok();
                    stmt.as_mut()
                        .and_then(|s| {
                            s.query_map([], |row| row.get::<_, String>(2))
                                .ok()
                                .map(|m| m.flatten().collect())
                        })
                        .unwrap_or_default()
                };
                let unique_str = if *unique != 0 { "UNIQUE " } else { "" };
                lines.push(format!(
                    "  {unique_str}INDEX {idx_name} ({})",
                    cols.join(", ")
                ));
            }
        }

        lines.push(String::new());
        lines.join("\n")
    }

    #[tool(
        description = "Safely copy an open database to a new file using SQLite's backup API. Safe to call while the database is in use."
    )]
    async fn backup_database(&self, params: Parameters<BackupDatabaseParams>) -> String {
        let BackupDatabaseParams { alias, destination } = params.0;
        let dest_path = match self.resolve_db_path(&destination) {
            Ok(p) => p,
            Err(e) => return e,
        };

        if dest_path.exists() {
            return format!(
                "Destination '{}' already exists. Choose a different path or remove it first.",
                dest_path.display()
            );
        }

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        match db.conn.backup(MAIN_DB, &dest_path, None) {
            Ok(()) => {
                let size_kb = dest_path
                    .metadata()
                    .map(|m| m.len() as f64 / 1024.0)
                    .unwrap_or(0.0);
                info!("Backed up '{alias}' to {}", dest_path.display());
                format!(
                    "Backed up '{alias}' to {} ({size_kb:.1} KB).",
                    dest_path.display()
                )
            }
            Err(e) => {
                let _ = std::fs::remove_file(&dest_path);
                format!("Backup error: {e}")
            }
        }
    }

    #[tool(
        description = "Rebuild the database file to reclaim free space (VACUUM). After many DELETE operations, the database file doesn't shrink until VACUUM is run."
    )]
    async fn compact_database(&self, params: Parameters<AliasParams>) -> String {
        let alias = params.0.alias;
        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let size_before = db.path.metadata().map(|m| m.len()).unwrap_or(0);
        match db.conn.execute_batch("VACUUM") {
            Err(e) => format!("Compact error: {e}"),
            Ok(()) => {
                let size_after = db.path.metadata().map(|m| m.len()).unwrap_or(0);
                let before_kb = size_before as f64 / 1024.0;
                let after_kb = size_after as f64 / 1024.0;
                if size_before > size_after {
                    let saved_kb = (size_before - size_after) as f64 / 1024.0;
                    format!(
                        "Compacted '{alias}': {before_kb:.1} KB → {after_kb:.1} KB (saved {saved_kb:.1} KB)."
                    )
                } else {
                    format!("Compacted '{alias}': no space to reclaim ({after_kb:.1} KB).")
                }
            }
        }
    }

    #[tool(
        description = "Create or replace a _readme table that self-documents the database. Call immediately after creating a new database. Do NOT call for databases from external sources."
    )]
    async fn create_readme(&self, params: Parameters<CreateReadmeParams>) -> String {
        let CreateReadmeParams {
            alias,
            purpose,
            tables,
        } = params.0;
        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let result: rusqlite::Result<usize> = (|| {
            db.conn.execute_batch(
                "DROP TABLE IF EXISTS _readme;
                 CREATE TABLE _readme (key TEXT PRIMARY KEY, value TEXT NOT NULL);",
            )?;
            db.conn.execute(
                "INSERT INTO _readme VALUES ('purpose', ?1)",
                rusqlite::params![purpose],
            )?;
            db.conn.execute(
                "INSERT INTO _readme VALUES ('created', datetime('now'))",
                [],
            )?;
            let mut n = 2usize;
            if let Some(ref tbl_map) = tables {
                for (tbl_name, tbl_desc) in tbl_map {
                    db.conn.execute(
                        "INSERT INTO _readme VALUES (?1, ?2)",
                        rusqlite::params![format!("table:{tbl_name}"), tbl_desc],
                    )?;
                    n += 1;
                }
            }
            Ok(n)
        })();

        match result {
            Ok(n) => {
                info!("Created _readme in '{alias}'");
                format!("Created _readme table with {n} entries in '{alias}'.")
            }
            Err(e) => format!("Error creating _readme: {e}"),
        }
    }

    #[tool(
        description = "Execute a single SQL statement. SELECT returns rows; INSERT/UPDATE/DELETE/DDL returns affected row count. Use ? placeholders with params to prevent injection. Paginate with offset+max_rows — increment offset by rows returned when response says more are available. TRAP: cannot span transactions — BEGIN in one call then COMMIT in another has no effect; use execute_script for manual transactions or bulk_insert for row batches."
    )]
    async fn execute_sql(&self, params: Parameters<ExecuteSqlParams>) -> String {
        let ExecuteSqlParams {
            alias,
            sql,
            params: bind_params,
            max_rows,
            offset,
        } = params.0;
        let max_rows = max_rows.unwrap_or(self.default_max_rows);
        let offset = offset.unwrap_or(0);
        let bind_params = bind_params.unwrap_or_default();
        let json_params: Vec<JsonParam> = bind_params.into_iter().map(JsonParam).collect();

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let mut stmt = match db.conn.prepare(&sql) {
            Ok(s) => s,
            Err(e) => return format!("SQL error: {e}"),
        };

        let col_count = stmt.column_count();

        if col_count > 0 {
            // SELECT-like: returns rows
            let col_names: Vec<String> =
                stmt.column_names().iter().map(|&s| s.to_string()).collect();

            let rows_result: rusqlite::Result<Vec<Vec<String>>> = stmt
                .query_map(rusqlite::params_from_iter(json_params.iter()), |row| {
                    let vals: Vec<String> = (0..col_count)
                        .map(|i| row.get_ref(i).map(value_ref_to_string).unwrap_or_default())
                        .collect();
                    Ok(vals)
                })
                .and_then(|mapped| {
                    let mut collected = Vec::new();
                    let mut skipped = 0usize;
                    for r in mapped {
                        if skipped < offset {
                            let _ = r?;
                            skipped += 1;
                            continue;
                        }
                        collected.push(r?);
                        if collected.len() >= max_rows + 1 {
                            break;
                        }
                    }
                    Ok(collected)
                });

            match rows_result {
                Err(e) => format!("SQL error: {e}"),
                Ok(mut rows) => {
                    let has_more = rows.len() > max_rows;
                    if has_more {
                        rows.pop();
                    }
                    if rows.is_empty() {
                        return if offset > 0 {
                            format!("(No rows at offset {offset} -- past end of results.)")
                        } else {
                            "(no rows)".to_string()
                        };
                    }
                    let start = offset + 1;
                    let end = offset + rows.len();
                    let status = if has_more {
                        format!(
                            "(Rows {start}-{end}, more available. Call again with offset={end}.)"
                        )
                    } else {
                        format!("(Rows {start}-{end}.)")
                    };
                    let mut out = status;
                    out.push('\n');
                    out.push_str(&col_names.join("\t"));
                    for row in &rows {
                        out.push('\n');
                        out.push_str(&row.join("\t"));
                    }
                    out
                }
            }
        } else {
            // DML / DDL: returns affected row count
            match stmt.execute(rusqlite::params_from_iter(json_params.iter())) {
                Ok(n) => format!("{n} row(s) affected."),
                Err(e) => format!("SQL error: {e}"),
            }
        }
    }

    #[tool(
        description = "Execute multiple semicolon-separated SQL statements in one call. Use for schema setup (CREATE TABLE, CREATE INDEX) or when you need manual transaction control (BEGIN; …; COMMIT). No parameter binding — never interpolate untrusted values into the script; use execute_sql with params for that."
    )]
    async fn execute_script(&self, params: Parameters<ExecuteScriptParams>) -> String {
        let ExecuteScriptParams { alias, script } = params.0;
        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };
        match db.conn.execute_batch(&script) {
            Ok(()) => "Script executed successfully.".to_string(),
            Err(e) => format!("SQL error: {e}"),
        }
    }

    #[tool(
        description = "Show how SQLite will execute a query (EXPLAIN QUERY PLAN). Use to understand whether indices are used, whether full table scans occur, and the join order."
    )]
    async fn explain_query(&self, params: Parameters<ExplainQueryParams>) -> String {
        let ExplainQueryParams {
            alias,
            sql,
            params: bind_params,
        } = params.0;
        let bind_params = bind_params.unwrap_or_default();
        let json_params: Vec<JsonParam> = bind_params.into_iter().map(JsonParam).collect();

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let explain_sql = format!("EXPLAIN QUERY PLAN {sql}");
        let mut stmt = match db.conn.prepare(&explain_sql) {
            Ok(s) => s,
            Err(e) => return format!("SQL error: {e}"),
        };

        let rows_result: rusqlite::Result<Vec<(i32, String)>> = stmt
            .query_map(rusqlite::params_from_iter(json_params.iter()), |row| {
                // EXPLAIN QUERY PLAN: (id, parent, notused, detail)
                let id: i32 = row.get(0)?;
                let detail: String = row.get(3)?;
                Ok((id, detail))
            })
            .and_then(|mapped| mapped.collect());

        match rows_result {
            Err(e) => format!("SQL error: {e}"),
            Ok(rows) if rows.is_empty() => "No query plan available.".to_string(),
            Ok(rows) => {
                let mut lines = vec!["Query plan:".to_string(), String::new()];
                for (id, detail) in &rows {
                    let indent = "  ".repeat(*id as usize);
                    lines.push(format!("{indent}{detail}"));
                }
                lines.join("\n")
            }
        }
    }

    #[tool(
        description = "Insert many rows into a pre-existing table in one transaction. 50-100x faster than repeated execute_sql INSERTs. Table must already exist — create it with execute_sql first if needed. conflict: 'error' (default, rolls back batch on violation), 'replace' (upsert), 'ignore' (skip violating rows silently)."
    )]
    async fn bulk_insert(&self, params: Parameters<BulkInsertParams>) -> String {
        let BulkInsertParams {
            alias,
            table,
            columns,
            rows,
            conflict,
        } = params.0;
        let conflict_kw = match conflict.as_deref().unwrap_or("error") {
            "replace" => "OR REPLACE ",
            "ignore" => "OR IGNORE ",
            _ => "",
        };

        if rows.is_empty() {
            return "No rows to insert.".to_string();
        }
        if let Err(e) = validate_identifier(&table, "table name") {
            return e;
        }
        for col in &columns {
            if let Err(e) = validate_identifier(col, "column name") {
                return e;
            }
        }

        let col_clause: String = columns
            .iter()
            .map(|c| format!("\"{}\"", escape_identifier(c)))
            .collect::<Vec<_>>()
            .join(", ");
        let placeholders: String = (0..columns.len())
            .map(|_| "?")
            .collect::<Vec<_>>()
            .join(", ");
        let sql = format!(
            "INSERT {conflict_kw}INTO \"{}\" ({col_clause}) VALUES ({placeholders})",
            escape_identifier(&table)
        );

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let result: rusqlite::Result<()> = (|| {
            db.conn.execute_batch("BEGIN TRANSACTION")?;
            let mut stmt = db.conn.prepare(&sql)?;
            for row in &rows {
                let json_params: Vec<JsonParam> = row.iter().cloned().map(JsonParam).collect();
                stmt.execute(rusqlite::params_from_iter(json_params.iter()))?;
            }
            drop(stmt);
            db.conn.execute_batch("COMMIT")?;
            Ok(())
        })();

        match result {
            Ok(()) => {
                info!("Bulk-inserted {} rows into '{alias}.{table}'", rows.len());
                format!("Inserted {} rows into '{table}'.", rows.len())
            }
            Err(e) => {
                let _ = db.conn.execute_batch("ROLLBACK");
                format!("Bulk insert error (rolled back): {e}")
            }
        }
    }

    #[tool(
        description = "Import inline CSV text into a table. Table auto-created from the header row with inferred types if absent. if_exists: 'error' (default), 'replace' (drop+recreate), 'append'. For large datasets, prefer import_csv_file to avoid oversized arguments."
    )]
    async fn import_csv(&self, params: Parameters<ImportCsvParams>) -> String {
        let ImportCsvParams {
            alias,
            csv_text,
            table,
            if_exists,
        } = params.0;
        let table = table.unwrap_or_else(|| "imported".to_string());
        let if_exists = if_exists.unwrap_or_else(|| "error".to_string());

        if let Err(e) = validate_identifier(&table, "table name") {
            return e;
        }

        let mut rdr = csv_crate::Reader::from_reader(io::Cursor::new(csv_text.as_bytes()));
        let headers: Vec<String> = match rdr.headers() {
            Ok(h) => h.iter().map(|s| s.trim().to_string()).collect(),
            Err(e) => return format!("CSV parse error: {e}"),
        };

        if headers.is_empty() {
            return "CSV header row is empty.".to_string();
        }
        for h in &headers {
            if let Err(e) = validate_identifier(h, "column name (from CSV header)") {
                return e;
            }
        }

        let rows: Vec<Vec<String>> = rdr
            .records()
            .filter_map(|r| r.ok())
            .filter(|r| !r.is_empty())
            .map(|r| r.iter().map(|s| s.to_string()).collect())
            .collect();

        if rows.is_empty() {
            return "CSV has a header row but no data rows.".to_string();
        }

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let escaped_table = escape_identifier(&table);
        let col_types: Vec<&'static str> = (0..headers.len())
            .map(|ci| {
                let samples: Vec<&str> = rows
                    .iter()
                    .take(200)
                    .filter_map(|r| r.get(ci).map(|s| s.as_str()))
                    .collect();
                infer_sqlite_type(&samples)
            })
            .collect();
        let col_defs: String = headers
            .iter()
            .zip(col_types.iter())
            .map(|(h, t)| format!("\"{}\" {t}", escape_identifier(h)))
            .collect::<Vec<_>>()
            .join(", ");
        let placeholders: String = (0..headers.len())
            .map(|_| "?")
            .collect::<Vec<_>>()
            .join(", ");
        let insert_sql = format!("INSERT INTO \"{escaped_table}\" VALUES ({placeholders})");

        let result: rusqlite::Result<()> = (|| {
            if if_exists == "replace" {
                db.conn
                    .execute_batch(&format!("DROP TABLE IF EXISTS \"{escaped_table}\""))?;
            } else if if_exists == "error" {
                let exists: bool = db.conn.query_row(
                    "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?1",
                    [&table],
                    |row| row.get::<_, i64>(0),
                )? > 0;
                if exists {
                    return Err(rusqlite::Error::SqliteFailure(
                        rusqlite::ffi::Error::new(rusqlite::ffi::SQLITE_ERROR),
                        Some(format!(
                            "Table '{table}' already exists. Use if_exists='replace' to overwrite or if_exists='append' to add rows."
                        )),
                    ));
                }
            }
            db.conn.execute_batch(&format!(
                "CREATE TABLE IF NOT EXISTS \"{escaped_table}\" ({col_defs})"
            ))?;
            db.conn.execute_batch("BEGIN TRANSACTION")?;
            let mut stmt = db.conn.prepare(&insert_sql)?;
            for row in &rows {
                let row_refs: Vec<&dyn rusqlite::ToSql> =
                    row.iter().map(|s| s as &dyn rusqlite::ToSql).collect();
                stmt.execute(row_refs.as_slice())?;
            }
            drop(stmt);
            db.conn.execute_batch("COMMIT")?;
            Ok(())
        })();

        match result {
            Ok(()) => {
                info!("Imported {} rows into '{alias}.{table}'", rows.len());
                format!(
                    "Imported {} rows into '{table}' ({} columns: {}).",
                    rows.len(),
                    headers.len(),
                    headers.join(", ")
                )
            }
            Err(e) => {
                let _ = db.conn.execute_batch("ROLLBACK");
                format!("Import error: {e}")
            }
        }
    }

    #[tool(
        description = "Run a SELECT query and return results as CSV text. Paginate with offset+max_rows — increment offset by rows returned when the response indicates more are available."
    )]
    async fn export_query_csv(&self, params: Parameters<ExportQueryCsvParams>) -> String {
        let ExportQueryCsvParams {
            alias,
            sql,
            params: bind_params,
            max_rows,
            offset,
        } = params.0;
        let max_rows = max_rows.unwrap_or(self.default_export_max_rows);
        let offset = offset.unwrap_or(0);
        let bind_params = bind_params.unwrap_or_default();
        let json_params: Vec<JsonParam> = bind_params.into_iter().map(JsonParam).collect();

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let mut stmt = match db.conn.prepare(&sql) {
            Ok(s) => s,
            Err(e) => return format!("SQL error: {e}"),
        };

        if stmt.column_count() == 0 {
            return "Statement did not return results.".to_string();
        }

        let col_names: Vec<String> = stmt.column_names().iter().map(|&s| s.to_string()).collect();
        let col_count = stmt.column_count();

        let mut output = Vec::new();
        let mut wtr = csv_crate::Writer::from_writer(&mut output);
        if let Err(e) = wtr.write_record(&col_names) {
            return format!("CSV error: {e}");
        }

        let rows_result: rusqlite::Result<(usize, bool)> = stmt
            .query_map(rusqlite::params_from_iter(json_params.iter()), |row| {
                let vals: Vec<String> = (0..col_count)
                    .map(|i| row.get_ref(i).map(value_ref_to_string).unwrap_or_default())
                    .collect();
                Ok(vals)
            })
            .and_then(|mapped| {
                let mut count = 0usize;
                let mut skipped = 0usize;
                let mut truncated = false;
                for row in mapped {
                    let r = row?;
                    if skipped < offset {
                        skipped += 1;
                        continue;
                    }
                    if max_rows > 0 && count >= max_rows {
                        truncated = true;
                        break;
                    }
                    wtr.write_record(&r)
                        .map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))?;
                    count += 1;
                }
                Ok((count, truncated))
            });

        if let Err(e) = wtr.flush() {
            return format!("CSV write error: {e}");
        }
        drop(wtr);

        match rows_result {
            Err(e) => format!("SQL error: {e}"),
            Ok((count, truncated)) => {
                let mut csv_text = String::from_utf8_lossy(&output).into_owned();
                if truncated {
                    let start = offset + 1;
                    let end = offset + count;
                    csv_text.push_str(&format!(
                        "\n# Rows {start}-{end}, more available. Call again with offset={end}.\n"
                    ));
                } else if offset > 0 {
                    let start = offset + 1;
                    let end = offset + count;
                    csv_text.push_str(&format!("\n# Rows {start}-{end}, end of results.\n"));
                }
                csv_text
            }
        }
    }

    #[tool(
        description = "Inspect a CSV or TSV file on disk: detect the delimiter, identify column names from the header row, sample data rows, and infer likely SQLite column types. Use this before import_csv_file to understand an unfamiliar file's structure."
    )]
    async fn inspect_csv_file(&self, params: Parameters<InspectCsvFileParams>) -> String {
        let InspectCsvFileParams {
            path,
            delimiter,
            sample_rows,
        } = params.0;
        let sample_rows = sample_rows.unwrap_or(5);

        let file_path = match self.resolve_file_path(&path) {
            Ok(p) => p,
            Err(e) => return e,
        };

        let raw = match std::fs::read(&file_path) {
            Ok(b) => b,
            Err(e) => return format!("Cannot read file '{}': {e}", file_path.display()),
        };

        let delim_byte = match parse_delimiter(&delimiter, &file_path) {
            Ok(b) => b,
            Err(e) => return e,
        };

        let mut rdr = csv_crate::ReaderBuilder::new()
            .delimiter(delim_byte)
            .has_headers(true)
            .flexible(true)
            .from_reader(std::io::Cursor::new(&raw));

        let headers: Vec<String> = match rdr.headers() {
            Ok(h) => h.iter().map(|s| s.trim().to_string()).collect(),
            Err(e) => return format!("CSV parse error: {e}"),
        };

        let mut samples: Vec<Vec<String>> = Vec::new();
        for result in rdr.records().take(sample_rows) {
            match result {
                Ok(r) => samples.push(r.iter().map(|s| s.to_string()).collect()),
                Err(_) => break,
            }
        }

        let delim_name = if delim_byte == b'\t' {
            "TSV (tab-separated)"
        } else {
            "CSV (comma-separated)"
        };
        let mut out = format!(
            "File: {}\nFormat: {}\nColumns: {}\nSample rows shown: {}\n\nColumn schema (inferred):\n",
            file_path.display(),
            delim_name,
            headers.len(),
            samples.len(),
        );
        for (i, col) in headers.iter().enumerate() {
            let col_vals: Vec<&str> = samples
                .iter()
                .filter_map(|r| r.get(i).map(|s| s.as_str()))
                .collect();
            let inferred = infer_sqlite_type(&col_vals);
            out.push_str(&format!("  {col}  →  {inferred}\n"));
        }
        if !samples.is_empty() {
            out.push_str(&format!("\nSample data ({} row(s)):\n", samples.len()));
            out.push_str(&headers.join("\t"));
            out.push('\n');
            for row in &samples {
                out.push_str(&row.join("\t"));
                out.push('\n');
            }
        }
        out
    }

    #[tool(
        description = "Import a CSV or TSV file from disk into a table. Delimiter auto-detected (.tsv→tab, others→comma). Table auto-created with inferred types if absent. IMPORTANT: if_exists defaults to 'error' — pass 'replace' to overwrite or 'append' to extend. Supply columns when has_headers=false."
    )]
    async fn import_csv_file(&self, params: Parameters<ImportCsvFileParams>) -> String {
        let ImportCsvFileParams {
            alias,
            path,
            table,
            has_headers,
            columns,
            delimiter,
            if_exists,
        } = params.0;
        let has_headers = has_headers.unwrap_or(true);
        let if_exists = if_exists.unwrap_or_else(|| "error".to_string());

        let file_path = match self.resolve_file_path(&path) {
            Ok(p) => p,
            Err(e) => return e,
        };

        let delim_byte = match parse_delimiter(&delimiter, &file_path) {
            Ok(b) => b,
            Err(e) => return e,
        };

        let raw = match std::fs::read(&file_path) {
            Ok(b) => b,
            Err(e) => return format!("Cannot read file '{}': {e}", file_path.display()),
        };

        let mut rdr = csv_crate::ReaderBuilder::new()
            .delimiter(delim_byte)
            .has_headers(has_headers)
            .flexible(true)
            .from_reader(std::io::Cursor::new(&raw));

        let col_names: Vec<String> = if has_headers {
            match rdr.headers() {
                Ok(h) => h.iter().map(|s| s.trim().to_string()).collect(),
                Err(e) => return format!("CSV parse error: {e}"),
            }
        } else {
            match columns {
                Some(c) => c,
                None => return "has_headers is false: you must supply 'columns' with the column names to use.".to_string(),
            }
        };

        if col_names.is_empty() {
            return "No columns found.".to_string();
        }
        for c in &col_names {
            if let Err(e) = validate_identifier(c, "column name") {
                return e;
            }
        }

        let table_name = match table {
            Some(t) => t,
            None => file_path
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("imported")
                .to_string(),
        };
        if let Err(e) = validate_identifier(&table_name, "table name") {
            return e;
        }

        let rows: Vec<Vec<String>> = rdr
            .records()
            .filter_map(|r| r.ok())
            .filter(|r| !r.is_empty())
            .map(|r| r.iter().map(|s| s.to_string()).collect())
            .collect();

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let escaped_table = escape_identifier(&table_name);
        // Infer per-column types from sampled row data
        let col_types: Vec<&'static str> = (0..col_names.len())
            .map(|ci| {
                let samples: Vec<&str> = rows
                    .iter()
                    .take(200)
                    .filter_map(|r| r.get(ci).map(|s| s.as_str()))
                    .collect();
                infer_sqlite_type(&samples)
            })
            .collect();
        let col_defs: String = col_names
            .iter()
            .zip(col_types.iter())
            .map(|(c, t)| format!("\"{}\" {t}", escape_identifier(c)))
            .collect::<Vec<_>>()
            .join(", ");
        let placeholders: String = (0..col_names.len())
            .map(|_| "?")
            .collect::<Vec<_>>()
            .join(", ");
        let insert_sql = format!("INSERT INTO \"{escaped_table}\" VALUES ({placeholders})");

        let result: rusqlite::Result<()> = (|| {
            if if_exists == "replace" {
                db.conn
                    .execute_batch(&format!("DROP TABLE IF EXISTS \"{escaped_table}\""))?;
            } else if if_exists == "error" {
                let exists: bool = db.conn.query_row(
                    "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?1",
                    [&table_name],
                    |row| row.get::<_, i64>(0),
                )? > 0;
                if exists {
                    return Err(rusqlite::Error::SqliteFailure(
                        rusqlite::ffi::Error::new(rusqlite::ffi::SQLITE_ERROR),
                        Some(format!(
                            "Table '{table_name}' already exists. Use if_exists='replace' to overwrite or if_exists='append' to add rows."
                        )),
                    ));
                }
            }
            db.conn.execute_batch(&format!(
                "CREATE TABLE IF NOT EXISTS \"{escaped_table}\" ({col_defs})"
            ))?;
            db.conn.execute_batch("BEGIN TRANSACTION")?;
            let mut stmt = db.conn.prepare(&insert_sql)?;
            for row in &rows {
                let row_refs: Vec<&dyn rusqlite::ToSql> =
                    row.iter().map(|s| s as &dyn rusqlite::ToSql).collect();
                stmt.execute(row_refs.as_slice())?;
            }
            drop(stmt);
            db.conn.execute_batch("COMMIT")?;
            Ok(())
        })();

        match result {
            Ok(()) => {
                info!(
                    "Imported {} rows from '{}' into '{alias}.{table_name}'",
                    rows.len(),
                    file_path.display()
                );
                format!(
                    "Imported {} rows into '{table_name}' from '{}' ({} columns: {}).",
                    rows.len(),
                    file_path.display(),
                    col_names.len(),
                    col_names.join(", ")
                )
            }
            Err(e) => {
                let _ = db.conn.execute_batch("ROLLBACK");
                format!("Import error: {e}")
            }
        }
    }

    #[tool(
        description = "Run a SELECT query and write results to a CSV or TSV file on disk. Use for large exports that would be unwieldy as inline text. Supports optional header row and custom delimiter. Returns a row count summary."
    )]
    async fn export_query_csv_file(&self, params: Parameters<ExportQueryCsvFileParams>) -> String {
        let ExportQueryCsvFileParams {
            alias,
            sql,
            params: bind_params,
            path,
            emit_headers,
            delimiter,
            max_rows,
        } = params.0;
        let emit_headers = emit_headers.unwrap_or(true);
        let max_rows = max_rows.unwrap_or(self.default_export_max_rows);
        let bind_params = bind_params.unwrap_or_default();
        let json_params: Vec<JsonParam> = bind_params.into_iter().map(JsonParam).collect();

        let file_path = match self.resolve_file_path(&path) {
            Ok(p) => p,
            Err(e) => return e,
        };

        let delim_byte = match parse_delimiter(&delimiter, &file_path) {
            Ok(b) => b,
            Err(e) => return e,
        };

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let mut stmt = match db.conn.prepare(&sql) {
            Ok(s) => s,
            Err(e) => return format!("SQL error: {e}"),
        };

        if stmt.column_count() == 0 {
            return "Statement did not return results.".to_string();
        }

        let col_names: Vec<String> = stmt.column_names().iter().map(|&s| s.to_string()).collect();
        let col_count = stmt.column_count();

        let file = match std::fs::File::create(&file_path) {
            Ok(f) => f,
            Err(e) => return format!("Cannot create file '{}': {e}", file_path.display()),
        };

        let mut wtr = csv_crate::WriterBuilder::new()
            .delimiter(delim_byte)
            .from_writer(file);

        if emit_headers {
            if let Err(e) = wtr.write_record(&col_names) {
                return format!("Write error: {e}");
            }
        }

        let rows_result: rusqlite::Result<(usize, bool)> = stmt
            .query_map(rusqlite::params_from_iter(json_params.iter()), |row| {
                let vals: Vec<String> = (0..col_count)
                    .map(|i| row.get_ref(i).map(value_ref_to_string).unwrap_or_default())
                    .collect();
                Ok(vals)
            })
            .and_then(|mapped| {
                let mut count = 0usize;
                let mut truncated = false;
                for row in mapped {
                    let r = row?;
                    if max_rows > 0 && count >= max_rows {
                        truncated = true;
                        break;
                    }
                    wtr.write_record(&r)
                        .map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))?;
                    count += 1;
                }
                Ok((count, truncated))
            });

        if let Err(e) = wtr.flush() {
            return format!("Write error: {e}");
        }
        drop(wtr);

        match rows_result {
            Err(e) => format!("SQL error: {e}"),
            Ok((count, truncated)) => {
                info!("Exported {count} rows to '{}'", file_path.display());
                let mut msg = format!(
                    "Exported {count} rows to '{}' ({} columns).",
                    file_path.display(),
                    col_names.len()
                );
                if truncated {
                    msg.push_str(&format!(" Truncated at {max_rows} rows."));
                }
                msg
            }
        }
    }

    #[tool(
        description = "Import an inline JSON array of objects into a table. Table auto-created with types inferred from JSON values if absent. if_exists: 'error' (default), 'replace', 'append'. For large JSON, use import_json_file instead."
    )]
    async fn import_json(&self, params: Parameters<ImportJsonParams>) -> String {
        let ImportJsonParams {
            alias,
            json_text,
            table,
            if_exists,
        } = params.0;
        let table = table.unwrap_or_else(|| "imported".to_string());
        let if_exists = if_exists.unwrap_or_else(|| "error".to_string());

        if let Err(e) = validate_identifier(&table, "table name") {
            return e;
        }

        let arr: Vec<serde_json::Map<String, serde_json::Value>> =
            match serde_json::from_str::<serde_json::Value>(&json_text) {
                Ok(serde_json::Value::Array(arr)) => arr
                    .into_iter()
                    .filter_map(|v| {
                        if let serde_json::Value::Object(m) = v {
                            Some(m)
                        } else {
                            None
                        }
                    })
                    .collect(),
                Ok(_) => return "JSON must be an array of objects.".to_string(),
                Err(e) => return format!("JSON parse error: {e}"),
            };

        if arr.is_empty() {
            return "JSON array is empty.".to_string();
        }

        // Collect column names from the first object
        let col_names: Vec<String> = arr[0].keys().cloned().collect();
        for c in &col_names {
            if let Err(e) = validate_identifier(c, "column name") {
                return e;
            }
        }

        import_json_objects(
            &self.connections,
            &alias,
            &table,
            &if_exists,
            &col_names,
            &arr,
        )
    }

    #[tool(
        description = "Import a JSON file into a table. format: 'array' (default, JSON array of objects) or 'ndjson' (one object per line, good for large files). if_exists: 'error' (default), 'replace', 'append'. Table auto-created with inferred types if absent."
    )]
    async fn import_json_file(&self, params: Parameters<ImportJsonFileParams>) -> String {
        let ImportJsonFileParams {
            alias,
            path,
            table,
            format,
            if_exists,
        } = params.0;
        let if_exists = if_exists.unwrap_or_else(|| "error".to_string());
        let format = format.unwrap_or_else(|| "array".to_string());

        let file_path = match self.resolve_file_path(&path) {
            Ok(p) => p,
            Err(e) => return e,
        };

        let table = match table {
            Some(t) => t,
            None => file_path
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("imported")
                .to_string(),
        };
        if let Err(e) = validate_identifier(&table, "table name") {
            return e;
        }

        let raw = match std::fs::read_to_string(&file_path) {
            Ok(s) => s,
            Err(e) => return format!("Cannot read file '{}': {e}", file_path.display()),
        };

        let arr: Vec<serde_json::Map<String, serde_json::Value>> = if format == "ndjson" {
            let mut objs = Vec::new();
            for (i, line) in raw.lines().enumerate() {
                let line = line.trim();
                if line.is_empty() {
                    continue;
                }
                match serde_json::from_str::<serde_json::Value>(line) {
                    Ok(serde_json::Value::Object(m)) => objs.push(m),
                    Ok(_) => return format!("Line {}: expected a JSON object.", i + 1),
                    Err(e) => return format!("Line {}: JSON parse error: {e}", i + 1),
                }
            }
            objs
        } else {
            match serde_json::from_str::<serde_json::Value>(&raw) {
                Ok(serde_json::Value::Array(arr)) => arr
                    .into_iter()
                    .filter_map(|v| {
                        if let serde_json::Value::Object(m) = v {
                            Some(m)
                        } else {
                            None
                        }
                    })
                    .collect(),
                Ok(_) => {
                    return "JSON file must contain an array of objects (or use format='ndjson')."
                        .to_string();
                }
                Err(e) => return format!("JSON parse error: {e}"),
            }
        };

        if arr.is_empty() {
            return "JSON source is empty.".to_string();
        }

        let col_names: Vec<String> = arr[0].keys().cloned().collect();
        for c in &col_names {
            if let Err(e) = validate_identifier(c, "column name") {
                return e;
            }
        }

        let result = import_json_objects(
            &self.connections,
            &alias,
            &table,
            &if_exists,
            &col_names,
            &arr,
        );
        info!("Imported from JSON file '{}'", file_path.display());
        result
    }

    #[tool(
        description = "Run a SELECT query and write results to a JSON file: either a JSON array of objects (format='array', default) or NDJSON with one object per line (format='ndjson', streaming-friendly for large files)."
    )]
    async fn export_query_json_file(
        &self,
        params: Parameters<ExportQueryJsonFileParams>,
    ) -> String {
        let ExportQueryJsonFileParams {
            alias,
            sql,
            params: bind_params,
            path,
            format,
            max_rows,
        } = params.0;
        let format = format.unwrap_or_else(|| "array".to_string());
        let max_rows = max_rows.unwrap_or(self.default_export_max_rows);
        let bind_params = bind_params.unwrap_or_default();
        let json_params: Vec<JsonParam> = bind_params.into_iter().map(JsonParam).collect();

        let file_path = match self.resolve_file_path(&path) {
            Ok(p) => p,
            Err(e) => return e,
        };

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let mut stmt = match db.conn.prepare(&sql) {
            Ok(s) => s,
            Err(e) => return format!("SQL error: {e}"),
        };
        if stmt.column_count() == 0 {
            return "Statement did not return results.".to_string();
        }

        let col_names: Vec<String> = stmt.column_names().iter().map(|&s| s.to_string()).collect();
        let col_count = stmt.column_count();

        let param_refs: Vec<&dyn rusqlite::ToSql> = json_params
            .iter()
            .map(|p| p as &dyn rusqlite::ToSql)
            .collect();
        let rows_iter = match stmt.query(param_refs.as_slice()) {
            Ok(r) => r,
            Err(e) => return format!("Query error: {e}"),
        };

        // Collect rows as JSON objects
        let mut rows: Vec<serde_json::Map<String, serde_json::Value>> = Vec::new();
        let mut truncated = false;
        let mut rows_iter = rows_iter;
        loop {
            match rows_iter.next() {
                Err(e) => return format!("Row error: {e}"),
                Ok(None) => break,
                Ok(Some(row)) => {
                    if max_rows > 0 && rows.len() >= max_rows {
                        truncated = true;
                        break;
                    }
                    let mut obj = serde_json::Map::new();
                    for i in 0..col_count {
                        let val = match row.get_ref(i) {
                            Ok(ValueRef::Null) => serde_json::Value::Null,
                            Ok(ValueRef::Integer(n)) => serde_json::Value::Number(n.into()),
                            Ok(ValueRef::Real(f)) => serde_json::Number::from_f64(f)
                                .map(serde_json::Value::Number)
                                .unwrap_or(serde_json::Value::Null),
                            Ok(ValueRef::Text(t)) => {
                                serde_json::Value::String(String::from_utf8_lossy(t).into_owned())
                            }
                            Ok(ValueRef::Blob(b)) => {
                                serde_json::Value::String(format!("<blob {} bytes>", b.len()))
                            }
                            Err(_) => serde_json::Value::Null,
                        };
                        obj.insert(col_names[i].clone(), val);
                    }
                    rows.push(obj);
                }
            }
        }
        drop(rows_iter);
        drop(stmt);

        // Write to file
        let write_result: std::io::Result<()> = (|| {
            use std::io::Write;
            let mut f = std::fs::File::create(&file_path)?;
            if format == "ndjson" {
                for obj in &rows {
                    let line = serde_json::to_string(obj).unwrap();
                    f.write_all(line.as_bytes())?;
                    f.write_all(b"\n")?;
                }
            } else {
                let json = serde_json::to_string_pretty(&rows).unwrap();
                f.write_all(json.as_bytes())?;
                f.write_all(b"\n")?;
            }
            Ok(())
        })();

        match write_result {
            Err(e) => format!("Write error: {e}"),
            Ok(()) => {
                info!(
                    "Exported {} rows to '{}' as JSON {format}",
                    rows.len(),
                    file_path.display()
                );
                let mut msg = format!(
                    "Exported {} rows to '{}' ({format}, {} columns).",
                    rows.len(),
                    file_path.display(),
                    col_names.len()
                );
                if truncated {
                    msg.push_str(&format!(" Truncated at {max_rows} rows."));
                }
                msg
            }
        }
    }

    #[tool(
        description = "Run a SELECT query and return results as a Markdown table for display in chat. Pipe characters in cells are auto-escaped. Paginate with offset+max_rows the same way as execute_sql."
    )]
    async fn export_query_markdown(&self, params: Parameters<ExportQueryMarkdownParams>) -> String {
        let ExportQueryMarkdownParams {
            alias,
            sql,
            params: bind_params,
            max_rows,
            offset,
        } = params.0;
        let max_rows = max_rows.unwrap_or(self.default_max_rows);
        let offset = offset.unwrap_or(0);
        let bind_params = bind_params.unwrap_or_default();
        let json_params: Vec<JsonParam> = bind_params.into_iter().map(JsonParam).collect();

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let mut stmt = match db.conn.prepare(&sql) {
            Ok(s) => s,
            Err(e) => return format!("SQL error: {e}"),
        };
        if stmt.column_count() == 0 {
            return "Statement did not return results.".to_string();
        }

        let col_names: Vec<String> = stmt.column_names().iter().map(|&s| s.to_string()).collect();
        let col_count = stmt.column_count();

        let param_refs: Vec<&dyn rusqlite::ToSql> = json_params
            .iter()
            .map(|p| p as &dyn rusqlite::ToSql)
            .collect();
        let rows_iter = match stmt.query(param_refs.as_slice()) {
            Ok(r) => r,
            Err(e) => return format!("Query error: {e}"),
        };

        let mut data: Vec<Vec<String>> = Vec::new();
        let mut truncated = false;
        let mut skipped = 0usize;
        let mut rows_iter = rows_iter;
        loop {
            match rows_iter.next() {
                Err(e) => return format!("Row error: {e}"),
                Ok(None) => break,
                Ok(Some(row)) => {
                    if skipped < offset {
                        skipped += 1;
                        continue;
                    }
                    if max_rows > 0 && data.len() >= max_rows {
                        truncated = true;
                        break;
                    }
                    let cells: Vec<String> = (0..col_count)
                        .map(|i| value_ref_to_string(row.get_ref(i).unwrap_or(ValueRef::Null)))
                        .map(|s| s.replace('|', "\\|").replace('\n', " "))
                        .collect();
                    data.push(cells);
                }
            }
        }
        drop(rows_iter);
        drop(stmt);

        if data.is_empty() {
            return if offset > 0 {
                format!("(No rows at offset {offset} -- past end of results.)")
            } else {
                "(no rows)".to_string()
            };
        }

        // Calculate column widths
        let mut widths: Vec<usize> = col_names.iter().map(|h| h.len()).collect();
        for row in &data {
            for (i, cell) in row.iter().enumerate() {
                if i < widths.len() {
                    widths[i] = widths[i].max(cell.len());
                }
            }
        }

        // Render
        let mut out = String::new();
        // Header
        out.push('|');
        for (i, h) in col_names.iter().enumerate() {
            out.push_str(&format!(" {:width$} |", h, width = widths[i]));
        }
        out.push('\n');
        // Separator
        out.push('|');
        for w in &widths {
            out.push_str(&format!(" {} |", "-".repeat(*w)));
        }
        out.push('\n');
        // Rows
        for row in &data {
            out.push('|');
            for (i, cell) in row.iter().enumerate() {
                let w = widths.get(i).copied().unwrap_or(0);
                out.push_str(&format!(" {:width$} |", cell, width = w));
            }
            out.push('\n');
        }

        if truncated {
            let start = offset + 1;
            let end = offset + data.len();
            out.push_str(&format!(
                "\n_(Rows {start}-{end}, more available. Call again with `offset={end}`.)_\n"
            ));
        } else if offset > 0 {
            let start = offset + 1;
            let end = offset + data.len();
            out.push_str(&format!("\n_(Rows {start}-{end}, end of results.)_\n"));
        }
        out
    }

    #[tool(
        description = "Profile a table: row count, NULL count, distinct count, min/max, and avg per column. A one-call replacement for many exploratory SELECT queries on an unfamiliar dataset."
    )]
    async fn table_stats(&self, params: Parameters<TableStatsParams>) -> String {
        let TableStatsParams {
            alias,
            table,
            columns,
        } = params.0;
        if let Err(e) = validate_identifier(&table, "table name") {
            return e;
        }
        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let safe_table = escape_identifier(&table);

        let all_cols: Vec<(String, String)> = {
            let mut stmt = db
                .conn
                .prepare(&format!("PRAGMA table_info(\"{safe_table}\")"))
                .ok();
            stmt.as_mut()
                .and_then(|s| {
                    s.query_map([], |row| {
                        Ok((row.get::<_, String>(1)?, row.get::<_, String>(2)?))
                    })
                    .ok()
                    .map(|m| m.flatten().collect())
                })
                .unwrap_or_default()
        };

        if all_cols.is_empty() {
            return format!("Table '{table}' not found.");
        }

        let col_info: Vec<(String, String)> = if let Some(requested) = columns {
            all_cols
                .into_iter()
                .filter(|(n, _)| requested.contains(n))
                .collect()
        } else {
            all_cols
        };

        let total_rows: i64 = match db.conn.query_row(
            &format!("SELECT COUNT(*) FROM \"{safe_table}\""),
            [],
            |row| row.get(0),
        ) {
            Ok(n) => n,
            Err(e) => return format!("Error counting rows: {e}"),
        };

        let mut lines = vec![
            format!("Table: {table}"),
            format!("Total rows: {total_rows}"),
        ];

        for (col_name, col_type) in &col_info {
            let safe_col = escape_identifier(col_name);
            let stats: Option<(i64, i64, Option<String>, Option<String>, Option<f64>)> = db
                .conn
                .query_row(
                    &format!(
                        "SELECT COUNT(\"{safe_col}\"), COUNT(DISTINCT \"{safe_col}\"), \
                         CAST(MIN(\"{safe_col}\") AS TEXT), CAST(MAX(\"{safe_col}\") AS TEXT), \
                         AVG(CAST(\"{safe_col}\" AS REAL)) FROM \"{safe_table}\""
                    ),
                    [],
                    |row| {
                        Ok((
                            row.get::<_, i64>(0)?,
                            row.get::<_, i64>(1)?,
                            row.get::<_, Option<String>>(2)?,
                            row.get::<_, Option<String>>(3)?,
                            row.get::<_, Option<f64>>(4)?,
                        ))
                    },
                )
                .ok();

            lines.push(String::new());
            if let Some((non_null, distinct, min_val, max_val, avg_val)) = stats {
                let null_count = total_rows - non_null;
                let type_str = if col_type.is_empty() {
                    "?".to_string()
                } else {
                    col_type.clone()
                };
                let null_str = if null_count == 0 {
                    "0 nulls".to_string()
                } else {
                    format!(
                        "{null_count} nulls ({:.1}%)",
                        null_count as f64 / total_rows as f64 * 100.0
                    )
                };
                let min_str = min_val.as_deref().unwrap_or("NULL");
                let max_str = max_val.as_deref().unwrap_or("NULL");
                let min_d = if min_str.len() > 30 {
                    format!("{}...", &min_str[..30])
                } else {
                    min_str.to_string()
                };
                let max_d = if max_str.len() > 30 {
                    format!("{}...", &max_str[..30])
                } else {
                    max_str.to_string()
                };
                lines.push(format!("  {col_name}  [{type_str}]  non_null: {non_null}  {null_str}  distinct: {distinct}"));
                lines.push(format!("    min: {min_d}  max: {max_d}"));
                if let Some(avg) = avg_val {
                    lines.push(format!("    avg: {avg:.4}"));
                }
            } else {
                lines.push(format!("  {col_name}  (error reading stats)"));
            }
        }

        lines.join("\n")
    }

    #[tool(
        description = "Attach a second SQLite file to an open connection. Its tables become accessible as schema_name.table_name for cross-database JOINs and copies. No new alias is created — all queries run on the main alias. Call detach_database when done to release the file lock."
    )]
    async fn attach_database(&self, params: Parameters<AttachDatabaseParams>) -> String {
        let AttachDatabaseParams {
            alias,
            path,
            schema_name,
        } = params.0;
        if let Err(e) = validate_identifier(&schema_name, "schema name") {
            return e;
        }
        let file_path = match self.resolve_db_path(&path) {
            Ok(p) => p,
            Err(e) => return e,
        };
        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };
        let path_str = file_path.to_string_lossy().replace('\'', "''");
        let safe_schema = escape_identifier(&schema_name);
        let sql = format!("ATTACH DATABASE '{path_str}' AS \"{safe_schema}\"");
        match db.conn.execute_batch(&sql) {
            Ok(()) => {
                info!(
                    "Attached '{}' as schema '{schema_name}' on '{alias}'",
                    file_path.display()
                );
                format!(
                    "Attached '{}' as schema '{schema_name}'. \
                     Tables are accessible as \"{schema_name}\".table_name in queries.",
                    file_path.display()
                )
            }
            Err(e) => format!("ATTACH error: {e}"),
        }
    }

    #[tool(
        description = "Remove a previously attached schema from a connection and release its file lock. Does not close the main connection or delete any files."
    )]
    async fn detach_database(&self, params: Parameters<DetachDatabaseParams>) -> String {
        let DetachDatabaseParams { alias, schema_name } = params.0;
        if let Err(e) = validate_identifier(&schema_name, "schema name") {
            return e;
        }
        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };
        let safe_schema = escape_identifier(&schema_name);
        match db
            .conn
            .execute_batch(&format!("DETACH DATABASE \"{safe_schema}\""))
        {
            Ok(()) => {
                info!("Detached schema '{schema_name}' from '{alias}'");
                format!("Detached schema '{schema_name}'.")
            }
            Err(e) => format!("DETACH error: {e}"),
        }
    }

    #[tool(
        description = "Create an index to speed up WHERE, JOIN, and ORDER BY on the indexed columns. Name auto-generated as idx_table_col1_col2 if omitted. unique=true enforces uniqueness. where_clause creates a partial (filtered) index. Use explain_query to verify index usage."
    )]
    async fn create_index(&self, params: Parameters<CreateIndexParams>) -> String {
        let CreateIndexParams {
            alias,
            table,
            columns,
            index_name,
            unique,
            where_clause,
            if_not_exists,
        } = params.0;
        let unique = unique.unwrap_or(false);
        let if_not_exists = if_not_exists.unwrap_or(false);

        if let Err(e) = validate_identifier(&table, "table name") {
            return e;
        }
        if columns.is_empty() {
            return "At least one column is required.".to_string();
        }
        for col in &columns {
            if let Err(e) = validate_identifier(col, "column name") {
                return e;
            }
        }

        let idx_name = match index_name {
            Some(n) => n,
            None => format!("idx_{}_{}", table, columns.join("_")),
        };
        if let Err(e) = validate_identifier(&idx_name, "index name") {
            return e;
        }

        let unique_kw = if unique { "UNIQUE " } else { "" };
        let ine_kw = if if_not_exists { "IF NOT EXISTS " } else { "" };
        let col_list = columns
            .iter()
            .map(|c| format!("\"{}\"", escape_identifier(c)))
            .collect::<Vec<_>>()
            .join(", ");
        let where_part = where_clause
            .as_deref()
            .map(|w| format!(" WHERE {w}"))
            .unwrap_or_default();
        let sql = format!(
            "CREATE {unique_kw}INDEX {ine_kw}\"{}\" ON \"{}\" ({col_list}){where_part}",
            escape_identifier(&idx_name),
            escape_identifier(&table),
        );

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };
        match db.conn.execute_batch(&sql) {
            Ok(()) => {
                info!("Created index '{idx_name}' on '{alias}.{table}'");
                let kind = if unique { "UNIQUE INDEX" } else { "INDEX" };
                format!(
                    "Created {kind} '{idx_name}' on '{table}' ({}).{where_part}",
                    columns.join(", ")
                )
            }
            Err(e) => format!("CREATE INDEX error: {e}"),
        }
    }

    #[tool(
        description = "Drop an index. Never deletes data — only removes the index structure. Call without confirm to preview (table name, DDL). Call with confirm=true to execute."
    )]
    async fn drop_index(&self, params: Parameters<DropIndexParams>) -> String {
        let DropIndexParams {
            alias,
            index_name,
            confirm,
        } = params.0;
        let confirm = confirm.unwrap_or(false);

        if let Err(e) = validate_identifier(&index_name, "index name") {
            return e;
        }

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let info: Option<(String, Option<String>)> = db
            .conn
            .query_row(
                "SELECT tbl_name, sql FROM sqlite_master WHERE type='index' AND name=?1",
                rusqlite::params![index_name],
                |row| Ok((row.get::<_, String>(0)?, row.get::<_, Option<String>>(1)?)),
            )
            .ok();

        let (tbl_name, idx_ddl) = match info {
            Some(i) => i,
            None => return format!("Index '{index_name}' not found."),
        };

        if !confirm {
            let ddl_line = idx_ddl
                .as_deref()
                .unwrap_or("(auto-generated — no stored DDL)");
            return format!(
                "Preview: DROP INDEX '{index_name}' on table '{tbl_name}'\nDDL: {ddl_line}\nCall again with confirm=true to drop."
            );
        }

        let safe_idx = escape_identifier(&index_name);
        match db.conn.execute_batch(&format!("DROP INDEX \"{safe_idx}\"")) {
            Ok(()) => {
                info!("Dropped index '{index_name}' from '{alias}.{tbl_name}'");
                format!("Dropped index '{index_name}' (was on table '{tbl_name}').")
            }
            Err(e) => format!("DROP INDEX error: {e}"),
        }
    }

    #[tool(
        description = "Add a column to an existing table (ALTER TABLE ... ADD COLUMN). Defaults to TEXT type. NOT NULL requires a default_value (SQLite restriction)."
    )]
    async fn add_column(&self, params: Parameters<AddColumnParams>) -> String {
        let AddColumnParams {
            alias,
            table,
            column,
            col_type,
            not_null,
            default_value,
        } = params.0;
        let col_type = col_type.unwrap_or_else(|| "TEXT".to_string());
        let not_null = not_null.unwrap_or(false);

        if let Err(e) = validate_identifier(&table, "table name") {
            return e;
        }
        if let Err(e) = validate_identifier(&column, "column name") {
            return e;
        }

        if not_null && default_value.is_none() {
            return "NOT NULL requires a default_value (SQLite requires a non-null default \
                    for columns added via ALTER TABLE ADD COLUMN)."
                .to_string();
        }

        let safe_table = escape_identifier(&table);
        let safe_col = escape_identifier(&column);
        let mut sql = format!("ALTER TABLE \"{safe_table}\" ADD COLUMN \"{safe_col}\" {col_type}");
        if not_null {
            sql.push_str(" NOT NULL");
        }
        if let Some(ref def) = default_value {
            sql.push_str(&format!(" DEFAULT {def}"));
        }

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };
        match db.conn.execute_batch(&sql) {
            Ok(()) => {
                info!("Added column '{column}' ({col_type}) to '{alias}.{table}'");
                let mut desc = format!("Added column '{column}' ({col_type})");
                if not_null {
                    desc.push_str(" NOT NULL");
                }
                if let Some(ref def) = default_value {
                    desc.push_str(&format!(" DEFAULT {def}"));
                }
                desc.push_str(&format!(" to table '{table}'."));
                desc
            }
            Err(e) => format!("ADD COLUMN error: {e}"),
        }
    }

    #[tool(
        description = "List all user tables. Prefer database_info for a full overview including row counts, column schemas, and indexes."
    )]
    async fn list_tables(&self, params: Parameters<AliasParams>) -> String {
        let alias = params.0.alias;
        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let result: rusqlite::Result<Vec<String>> = db
            .conn
            .prepare("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
            .and_then(|mut stmt| {
                stmt.query_map([], |row| row.get(0))
                    .and_then(|mapped| mapped.collect())
            });

        match result {
            Err(e) => format!("SQL error: {e}"),
            Ok(tables) if tables.is_empty() => "No tables found.".to_string(),
            Ok(tables) => tables.join("\n"),
        }
    }

    #[tool(
        description = "Show a table's column definitions (names, types, constraints, DDL) and all its indexes. Prefer database_info for a multi-table overview."
    )]
    async fn describe_table(&self, params: Parameters<TableParams>) -> String {
        let TableParams { alias, table } = params.0;
        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let safe = escape_identifier(&table);
        let cols_result: rusqlite::Result<Vec<(i32, String, String, i32, Option<String>, i32)>> =
            db.conn
                .prepare(&format!("PRAGMA table_info(\"{safe}\")"))
                .and_then(|mut stmt| {
                    stmt.query_map([], |row| {
                        Ok((
                            row.get::<_, i32>(0)?,
                            row.get::<_, String>(1)?,
                            row.get::<_, String>(2)?,
                            row.get::<_, i32>(3)?,
                            row.get::<_, Option<String>>(4)?,
                            row.get::<_, i32>(5)?,
                        ))
                    })
                    .and_then(|mapped| mapped.collect())
                });

        let cols = match cols_result {
            Err(e) => return format!("SQL error: {e}"),
            Ok(c) if c.is_empty() => return format!("Table '{table}' not found."),
            Ok(c) => c,
        };

        let mut lines = vec![
            format!("Table: {table}"),
            String::new(),
            format!(
                "{:<4} {:<30} {:<15} {:<10} {:<15} {}",
                "#", "Name", "Type", "Nullable", "Default", "PK"
            ),
            "-".repeat(80),
        ];

        for (cid, name, col_type, notnull, default_val, pk) in &cols {
            let nullable = if *notnull != 0 { "NO" } else { "YES" };
            let default_str = default_val.as_deref().unwrap_or("");
            let pk_str = if *pk != 0 { "YES" } else { "" };
            lines.push(format!(
                "{:<4} {:<30} {:<15} {:<10} {:<15} {}",
                cid, name, col_type, nullable, default_str, pk_str
            ));
        }

        // DDL
        let ddl: Option<String> = db
            .conn
            .query_row(
                "SELECT sql FROM sqlite_master WHERE type='table' AND name=?1",
                rusqlite::params![table],
                |row| row.get(0),
            )
            .ok()
            .flatten();

        if let Some(ddl) = ddl {
            lines.push(String::new());
            lines.push("DDL:".to_string());
            lines.push(ddl);
        }

        // Indexes
        let safe = escape_identifier(&table);
        let indexes: Vec<(String, i32, String)> = {
            let mut stmt = db
                .conn
                .prepare(&format!("PRAGMA index_list(\"{safe}\")"))
                .ok();
            stmt.as_mut()
                .and_then(|s| {
                    s.query_map([], |row| {
                        Ok((
                            row.get::<_, String>(1)?, // name
                            row.get::<_, i32>(2)?,    // unique
                            row.get::<_, String>(3)?, // origin: c=CREATE INDEX, u=UNIQUE constraint, pk=PRIMARY KEY
                        ))
                    })
                    .ok()
                    .map(|m| m.flatten().collect())
                })
                .unwrap_or_default()
        };

        if !indexes.is_empty() {
            lines.push(String::new());
            lines.push("Indexes:".to_string());
            for (idx_name, unique, origin) in &indexes {
                let safe_idx = escape_identifier(idx_name);
                let cols: Vec<String> = {
                    let mut stmt = db
                        .conn
                        .prepare(&format!("PRAGMA index_info(\"{safe_idx}\")"))
                        .ok();
                    stmt.as_mut()
                        .and_then(|s| {
                            s.query_map([], |row| row.get::<_, String>(2))
                                .ok()
                                .map(|m| m.flatten().collect())
                        })
                        .unwrap_or_default()
                };
                let kind = match origin.as_str() {
                    "pk" => "PRIMARY KEY",
                    "u" => "UNIQUE constraint",
                    _ => {
                        if *unique != 0 {
                            "UNIQUE INDEX"
                        } else {
                            "INDEX"
                        }
                    }
                };
                // Get DDL for explicit CREATE INDEX (origin == "c")
                let idx_ddl: Option<String> = if origin == "c" {
                    db.conn
                        .query_row(
                            "SELECT sql FROM sqlite_master WHERE type='index' AND name=?1",
                            rusqlite::params![idx_name],
                            |row| row.get(0),
                        )
                        .ok()
                        .flatten()
                } else {
                    None
                };
                lines.push(format!(
                    "  {idx_name}  [{kind}]  columns: ({})",
                    cols.join(", ")
                ));
                if let Some(ddl) = idx_ddl {
                    lines.push(format!("    {ddl}"));
                }
            }
        }

        lines.join("\n")
    }

    #[tool(
        description = "Drop a table permanently. Call without confirm to preview (row count, columns). Call with confirm=true to execute. Irreversible — verify you no longer need the data before confirming."
    )]
    async fn drop_table(&self, params: Parameters<DropTableParams>) -> String {
        let DropTableParams {
            alias,
            table,
            confirm,
        } = params.0;
        let confirm = confirm.unwrap_or(false);

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        // Check it exists
        let exists: bool = db
            .conn
            .query_row(
                "SELECT name FROM sqlite_master WHERE type='table' AND name=?1",
                rusqlite::params![table],
                |_| Ok(true),
            )
            .unwrap_or(false);
        if !exists {
            return format!("Table '{table}' does not exist.");
        }

        let safe = escape_identifier(&table);

        let count: i64 = db
            .conn
            .query_row(&format!("SELECT COUNT(*) FROM \"{safe}\""), [], |r| {
                r.get(0)
            })
            .unwrap_or(-1);

        let col_names: Vec<String> = db
            .conn
            .prepare(&format!("PRAGMA table_info(\"{safe}\")"))
            .and_then(|mut stmt| {
                stmt.query_map([], |row| row.get::<_, String>(1))
                    .and_then(|m| m.collect())
            })
            .unwrap_or_default();

        if !confirm {
            return format!(
                "Would drop table '{table}' with {count} rows and {} column(s) ({}). \
                 Call again with confirm=true to proceed.",
                col_names.len(),
                col_names.join(", ")
            );
        }

        match db.conn.execute_batch(&format!("DROP TABLE \"{safe}\"")) {
            Ok(()) => {
                info!("Dropped table '{table}' from '{alias}'");
                format!("Dropped table '{table}' ({count} rows deleted).")
            }
            Err(e) => format!("Error dropping table: {e}"),
        }
    }

    #[tool(description = "Rename a table in place. All data, indexes, and triggers are preserved.")]
    async fn rename_table(&self, params: Parameters<RenameTableParams>) -> String {
        let RenameTableParams {
            alias,
            old_name,
            new_name,
        } = params.0;

        if let Err(e) = validate_identifier(&new_name, "new table name") {
            return e;
        }

        let conns = self.connections.lock().unwrap();
        let db = match conns.get(&alias) {
            Some(d) => d,
            None => return Self::no_such_alias(&alias, &conns),
        };

        let exists: bool = db
            .conn
            .query_row(
                "SELECT name FROM sqlite_master WHERE type='table' AND name=?1",
                rusqlite::params![old_name],
                |_| Ok(true),
            )
            .unwrap_or(false);
        if !exists {
            return format!("Table '{old_name}' does not exist.");
        }

        let old_safe = escape_identifier(&old_name);
        let new_safe = escape_identifier(&new_name);
        match db.conn.execute_batch(&format!(
            "ALTER TABLE \"{old_safe}\" RENAME TO \"{new_safe}\""
        )) {
            Ok(()) => {
                info!("Renamed table '{old_name}' to '{new_name}' in '{alias}'");
                format!("Renamed table '{old_name}' to '{new_name}'.")
            }
            Err(e) => format!("Error renaming table: {e}"),
        }
    }
}

impl LumenSqlite {
    fn no_such_alias(alias: &str, conns: &HashMap<String, OpenDatabase>) -> String {
        let available = if conns.is_empty() {
            "(none)".to_string()
        } else {
            let mut aliases: Vec<&str> = conns.keys().map(|s| s.as_str()).collect();
            aliases.sort();
            aliases.join(", ")
        };
        format!("No open database with alias '{alias}'. Open databases: {available}")
    }
}

// ---------------------------------------------------------------------------
// MCP handler wiring
// ---------------------------------------------------------------------------

#[tool_handler]
impl ServerHandler for LumenSqlite {
    fn get_info(&self) -> ServerInfo {
        ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
            .with_instructions(SERVER_INSTRUCTIONS)
            .with_server_info(rmcp::model::Implementation::new(
                "lumen-sqlite",
                env!("CARGO_PKG_VERSION"),
            ))
    }
}

// ---------------------------------------------------------------------------
// Server instructions
// ---------------------------------------------------------------------------

const SERVER_INSTRUCTIONS: &str = include_str!("../instructions.md");

// ---------------------------------------------------------------------------
// Entry point
// ---------------------------------------------------------------------------

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Log to stderr (stdout is the MCP transport)
    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| "info".into()),
        )
        .with_ansi(false)
        .with_writer(std::io::stderr)
        .init();

    info!(
        "lumen-sqlite MCP server starting (db_dir={})",
        std::env::var("LUMEN_SQLITE_DB_DIR").unwrap_or_else(|_| ".".into())
    );

    let server = LumenSqlite::new();
    let transport = rmcp::transport::io::stdio();
    let ct = server.serve(transport).await?;
    ct.waiting().await?;
    Ok(())
}