mcp-repl 0.3.0

Interactive MCP client REPL: connects to any MCP server and turns its tools, prompts, and resources into the command set
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//! Command aliases: short names for frequent commands, kept in the same
//! config file as the server profiles.
//!
//! ```toml
//! [aliases]                       # every server sees these
//! t = "tools"
//! d = "describe"
//!
//! [servers.cratesio.aliases]      # only when connected as `cratesio`
//! dl = "get_downloads crate"
//! ```
//!
//! Expansion is a literal substitution of the first word, with whatever
//! followed the alias appended: with `dl = "get_downloads crate"`,
//! `dl =serde` runs `get_downloads crate=serde`. An expansion whose own
//! first word is an alias expands again; a cycle is reported rather than
//! looped.
//!
//! `alias`, `alias <name>=<expansion>`, and `unalias <name>` read and write
//! this file. Writes go through `toml_edit`, so comments, key order, and
//! formatting elsewhere in the file survive.

use std::collections::BTreeMap;
use std::path::PathBuf;

use toml_edit::{DocumentMut, InlineTable, Item, Table, value};

use crate::BUILTINS;

/// Which table of the config file an alias lives in.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Scope {
    /// The file-level `[aliases]` table: in effect against every server.
    Global,
    /// `[servers.<name>.aliases]`: in effect only through that profile.
    Profile(String),
}

impl Scope {
    /// How the scope is named in `alias` output and messages.
    pub fn label(&self) -> String {
        match self {
            Scope::Global => "global".to_string(),
            Scope::Profile(name) => format!("profile {name}"),
        }
    }
}

/// One alias as `alias` lists it.
#[derive(Debug, PartialEq, Eq)]
pub struct Entry {
    pub name: String,
    pub expansion: String,
    pub scope: Scope,
}

/// What a successful `alias`/`unalias` did. `warning` carries a persistence
/// failure: the alias still applies to this session, but the config file was
/// not updated, and saying so is better than pretending it was saved.
#[derive(Debug)]
pub struct Applied {
    pub scope: Scope,
    pub previous: Option<String>,
    pub warning: Option<String>,
}

/// The alias tables in effect for this session: the file's global table plus
/// the connected profile's, if any.
#[derive(Default)]
pub struct Aliases {
    global: BTreeMap<String, String>,
    profile: BTreeMap<String, String>,
    profile_name: Option<String>,
    /// Profile tables visited during this process. Definitions can be
    /// session-only when no config path is available, and persisted changes
    /// should also survive switching away and back without a config reload.
    profile_cache: BTreeMap<String, BTreeMap<String, String>>,
    path: Option<PathBuf>,
}

impl Aliases {
    pub fn new(
        global: BTreeMap<String, String>,
        profile: BTreeMap<String, String>,
        profile_name: Option<String>,
        path: Option<PathBuf>,
    ) -> Self {
        let profile_cache = profile_name
            .as_ref()
            .map(|name| [(name.clone(), profile.clone())].into_iter().collect())
            .unwrap_or_default();
        Self {
            global,
            profile,
            profile_name,
            profile_cache,
            path,
        }
    }

    /// The expansion for `name`, and where it came from. A profile alias
    /// shadows a global one of the same name: the narrower scope is the more
    /// deliberate one.
    pub fn lookup(&self, name: &str) -> Option<(&str, Scope)> {
        if let Some(expansion) = self.profile.get(name) {
            let scope = Scope::Profile(self.profile_name.clone().unwrap_or_default());
            return Some((expansion.as_str(), scope));
        }
        self.global.get(name).map(|e| (e.as_str(), Scope::Global))
    }

    /// Every alias in effect, profile ones first, each group by name. A
    /// global alias shadowed by a profile alias is omitted: listing it would
    /// suggest it applies.
    pub fn entries(&self) -> Vec<Entry> {
        let profile_scope = || Scope::Profile(self.profile_name.clone().unwrap_or_default());
        let mut out: Vec<Entry> = self
            .profile
            .iter()
            .map(|(name, expansion)| Entry {
                name: name.clone(),
                expansion: expansion.clone(),
                scope: profile_scope(),
            })
            .collect();
        out.extend(
            self.global
                .iter()
                .filter(|(name, _)| !self.profile.contains_key(*name))
                .map(|(name, expansion)| Entry {
                    name: name.clone(),
                    expansion: expansion.clone(),
                    scope: Scope::Global,
                }),
        );
        out
    }

    /// Select the aliases scoped to a newly connected profile. Global aliases
    /// and the persistence path remain session-wide.
    pub fn select_profile(&mut self, name: Option<String>, aliases: BTreeMap<String, String>) {
        if let Some(current) = self.profile_name.as_ref() {
            self.profile_cache
                .insert(current.clone(), self.profile.clone());
        }
        self.profile = name
            .as_ref()
            .and_then(|name| self.profile_cache.get(name).cloned())
            .unwrap_or(aliases);
        if let Some(name) = name.as_ref() {
            self.profile_cache
                .insert(name.clone(), self.profile.clone());
        }
        self.profile_name = name;
    }

    /// Expand a line whose first word is an alias. `Ok(None)` means the line
    /// names no alias and should run as typed.
    pub fn expand(&self, line: &str) -> Result<Option<String>, String> {
        let mut current = line.to_string();
        let mut seen: Vec<String> = Vec::new();
        loop {
            let trimmed = current.trim_start();
            let (first, rest) = match trimmed.find(char::is_whitespace) {
                Some(i) => (&trimmed[..i], trimmed[i..].trim_start()),
                None => (trimmed, ""),
            };
            // These are escape hatches for a command-name collision. Keep
            // them reachable even when an older or hand-edited config file
            // contains an alias that now uses one of the reserved names.
            if matches!(first, "tool" | "builtin") {
                break;
            }
            let Some((expansion, _)) = self.lookup(first) else {
                break;
            };
            if seen.iter().any(|s| s == first) {
                seen.push(first.to_string());
                return Err(format!(
                    "alias `{}` expands in a cycle ({}); break it with `unalias {}`",
                    seen[0],
                    seen.join(" -> "),
                    seen[0]
                ));
            }
            seen.push(first.to_string());
            current = if rest.is_empty() {
                expansion.to_string()
            } else {
                format!("{expansion} {rest}")
            };
        }
        Ok((!seen.is_empty()).then_some(current))
    }

    /// Define or redefine an alias, and write it to the config file.
    /// `global` forces the file-level table; without it an alias defined
    /// while connected through a profile belongs to that profile.
    pub fn define(&mut self, name: &str, expansion: &str, global: bool) -> Result<Applied, String> {
        validate(name, expansion)?;
        let scope = self.write_scope(global);
        let table = self.table_mut(&scope);
        let previous = table.insert(name.to_string(), expansion.to_string());
        let warning = self
            .persist(|doc| set_in_document(doc, &scope, name, expansion))
            .err();
        Ok(Applied {
            scope,
            previous,
            warning,
        })
    }

    /// Remove an alias. Without `global`, the profile's own alias goes first;
    /// a global alias is only removed when the profile does not define one,
    /// so `unalias` undoes the definition that is actually in effect.
    pub fn remove(&mut self, name: &str, global: bool) -> Result<Applied, String> {
        let scope = if !global && self.profile.contains_key(name) {
            Scope::Profile(self.profile_name.clone().unwrap_or_default())
        } else if self.global.contains_key(name) {
            Scope::Global
        } else if !global && self.profile_name.is_some() {
            return Err(format!("no alias named `{name}`"));
        } else {
            return Err(format!("no global alias named `{name}`"));
        };
        let previous = self.table_mut(&scope).remove(name);
        let warning = self
            .persist(|doc| remove_from_document(doc, &scope, name))
            .err();
        Ok(Applied {
            scope,
            previous,
            warning,
        })
    }

    /// Where a definition goes when the command did not say.
    fn write_scope(&self, global: bool) -> Scope {
        match (&self.profile_name, global) {
            (Some(name), false) => Scope::Profile(name.clone()),
            _ => Scope::Global,
        }
    }

    fn table_mut(&mut self, scope: &Scope) -> &mut BTreeMap<String, String> {
        match scope {
            Scope::Global => &mut self.global,
            Scope::Profile(_) => &mut self.profile,
        }
    }

    /// Apply `edit` to the config file, read-modify-write. A REPL with no
    /// platform config path and no `--config` keeps its aliases in memory for
    /// the session and says so.
    fn persist(
        &self,
        edit: impl FnOnce(&mut DocumentMut) -> Result<(), String>,
    ) -> Result<(), String> {
        let Some(path) = &self.path else {
            return Err(
                "no platform config directory (pass --config), so the alias applies to \
                 this session only"
                    .to_string(),
            );
        };
        let source = match std::fs::read_to_string(path) {
            Ok(s) => s,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(),
            Err(e) => return Err(format!("{}: {e}", path.display())),
        };
        let mut doc = source
            .parse::<DocumentMut>()
            .map_err(|e| format!("{}: {e}", path.display()))?;
        edit(&mut doc)?;
        write_atomic(path, &doc.to_string()).map_err(|e| format!("{}: {e}", path.display()))
    }
}

/// An alias name has to be a single word that dispatch can recognize, and it
/// must not bury a built-in: expansion happens before dispatch, so an alias
/// named `help` would make `help` unreachable.
fn validate(name: &str, expansion: &str) -> Result<(), String> {
    if name.is_empty() {
        return Err("usage: alias <name>=<expansion>".to_string());
    }
    if name.chars().any(char::is_whitespace) {
        return Err(format!("alias name `{name}` cannot contain whitespace"));
    }
    if name.contains('=') {
        return Err(format!("alias name `{name}` cannot contain `=`"));
    }
    if let Some((builtin, _)) = BUILTINS.iter().find(|(b, _)| *b == name) {
        return Err(format!(
            "`{builtin}` is a built-in command, so an alias by that name would hide it"
        ));
    }
    if expansion.trim().is_empty() {
        return Err(format!(
            "alias `{name}` needs something to expand to (usage: alias {name}=<expansion>)"
        ));
    }
    Ok(())
}

/// Write to `path` through a temporary file in the same directory, so a
/// failure part-way leaves the existing config intact rather than truncated.
use crate::secure_file::write_atomic;

// ---------------------------------------------------------------------------
// Config file edits
// ---------------------------------------------------------------------------

/// The aliases table of a scope, as it is written in the file. Both spellings
/// are accepted: a standalone `[aliases]` table and an inline
/// `aliases = { t = "tools" }`.
enum AliasTable<'t> {
    Table(&'t mut Table),
    Inline(&'t mut InlineTable),
}

impl AliasTable<'_> {
    fn set(&mut self, name: &str, expansion: &str) {
        match self {
            AliasTable::Table(t) => t[name] = value(expansion),
            AliasTable::Inline(t) => {
                t.insert(name, expansion.into());
            }
        }
    }

    fn remove(&mut self, name: &str) -> bool {
        match self {
            AliasTable::Table(t) => t.remove(name).is_some(),
            AliasTable::Inline(t) => t.remove(name).is_some(),
        }
    }
}

/// Set `name` in the scope's aliases table, creating the table (and, for a
/// profile scope, nothing else: the profile table must already exist).
pub fn set_in_document(
    doc: &mut DocumentMut,
    scope: &Scope,
    name: &str,
    expansion: &str,
) -> Result<(), String> {
    let mut table = aliases_table(doc, scope, true)?
        .ok_or_else(|| format!("no `{}` table in the config file", scope.label()))?;
    table.set(name, expansion);
    Ok(())
}

/// Remove `name` from the scope's aliases table. An emptied table is left in
/// place: a comment above `[aliases]` is that table's decoration, so deleting
/// the table would delete the comment with it.
pub fn remove_from_document(
    doc: &mut DocumentMut,
    scope: &Scope,
    name: &str,
) -> Result<(), String> {
    let Some(mut table) = aliases_table(doc, scope, false)? else {
        return Ok(());
    };
    table.remove(name);
    Ok(())
}

fn aliases_table<'d>(
    doc: &'d mut DocumentMut,
    scope: &Scope,
    create: bool,
) -> Result<Option<AliasTable<'d>>, String> {
    let parent = match scope {
        Scope::Global => doc.as_table_mut(),
        Scope::Profile(profile) => {
            let Some(servers) = child_table(doc.as_table_mut(), "servers")? else {
                return Ok(None);
            };
            match child_table(servers, profile)? {
                Some(t) => t,
                None => return Ok(None),
            }
        }
    };
    if !parent.contains_key("aliases") {
        if !create {
            return Ok(None);
        }
        parent.insert("aliases", Item::Table(Table::new()));
    }
    match parent.get_mut("aliases") {
        Some(Item::Table(t)) => Ok(Some(AliasTable::Table(t))),
        Some(Item::Value(v)) if v.is_inline_table() => Ok(Some(AliasTable::Inline(
            v.as_inline_table_mut().expect("checked"),
        ))),
        Some(_) => Err("`aliases` in the config file is not a table".to_string()),
        None => Ok(None),
    }
}

/// A child table by key. Only the standalone table spelling is writable here:
/// an inline `servers = { ... }` would have to be rewritten wholesale, which
/// is the user's call, not the REPL's.
fn child_table<'t>(parent: &'t mut Table, key: &str) -> Result<Option<&'t mut Table>, String> {
    match parent.get_mut(key) {
        Some(Item::Table(t)) => Ok(Some(t)),
        Some(_) => Err(format!(
            "`{key}` in the config file is not a standalone table, so the alias cannot be written \
             into it; add it to the file-level [aliases] table with `alias --global` instead"
        )),
        None => Ok(None),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::property::{ALIAS_REGRESSIONS, GENERATED_CASES, Generator};

    fn map(pairs: &[(&str, &str)]) -> BTreeMap<String, String> {
        pairs
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect()
    }

    fn aliases(global: &[(&str, &str)], profile: &[(&str, &str)]) -> Aliases {
        let name = (!profile.is_empty()).then(|| "cratesio".to_string());
        Aliases::new(map(global), map(profile), name, None)
    }

    #[test]
    fn property_alias_expansion_always_terminates_at_a_command_or_cycle() {
        for (pairs, line) in ALIAS_REGRESSIONS {
            let aliases = aliases(pairs, &[]);
            if let Err(error) = aliases.expand(line) {
                assert!(error.contains("cycle"), "unexpected alias failure: {error}");
            }
        }

        let mut generator = Generator::new(0x05);
        for _ in 0..GENERATED_CASES {
            let count = 1 + generator.index(32);
            let mut table = BTreeMap::new();
            for index in 0..count {
                let target = generator.index(count + 2);
                let first = if target < count {
                    format!("a{target}")
                } else if target == count {
                    "tool echo".to_string()
                } else {
                    "echo".to_string()
                };
                let tail = generator.text(24).replace(char::is_whitespace, "_");
                let expansion = if tail.is_empty() {
                    first
                } else {
                    format!("{first} value={tail}")
                };
                table.insert(format!("a{index}"), expansion);
            }

            let aliases = Aliases::new(table.clone(), BTreeMap::new(), None, None);
            let line = format!("a{} arg={}", generator.index(count), generator.text(24));
            match aliases.expand(&line) {
                Ok(Some(expanded)) => {
                    let first = expanded.split_whitespace().next().unwrap_or_default();
                    assert!(
                        matches!(first, "tool" | "builtin") || !table.contains_key(first),
                        "expansion stopped on alias {first:?}: {expanded:?}"
                    );
                }
                Ok(None) => panic!("generated input always starts with an alias: {line:?}"),
                Err(error) => assert!(error.contains("cycle"), "unexpected failure: {error}"),
            }
        }
    }

    #[test]
    fn expansion_substitutes_the_first_word_and_keeps_the_rest() {
        let a = aliases(&[("dl", "get_downloads crate")], &[]);
        assert_eq!(
            a.expand("dl =serde").unwrap().as_deref(),
            Some("get_downloads crate =serde")
        );
        assert_eq!(
            a.expand("dl").unwrap().as_deref(),
            Some("get_downloads crate")
        );
    }

    #[test]
    fn a_line_that_names_no_alias_is_left_alone() {
        let a = aliases(&[("t", "tools")], &[]);
        assert_eq!(a.expand("tools").unwrap(), None);
        assert_eq!(a.expand("").unwrap(), None);
        // Only the first word is a candidate; an alias elsewhere is just text.
        assert_eq!(a.expand("describe t").unwrap(), None);
    }

    #[test]
    fn an_expansion_naming_another_alias_expands_again() {
        let a = aliases(&[("t", "tools"), ("lst", "t")], &[]);
        assert_eq!(a.expand("lst").unwrap().as_deref(), Some("tools"));
    }

    #[test]
    fn a_cycle_is_reported_rather_than_looped() {
        let a = aliases(&[("a", "b"), ("b", "a")], &[]);
        let err = a.expand("a x").unwrap_err();
        assert!(err.contains("cycle"), "{err}");
        assert!(err.contains("a -> b -> a"), "{err}");
        assert!(err.contains("unalias a"), "{err}");
    }

    #[test]
    fn a_trailing_ampersand_survives_expansion() {
        // The background marker is read after expansion, so an alias can
        // carry it (or a line can add it to an alias that does not).
        let a = aliases(&[("sa", "slow_add a=1 b=2 &")], &[]);
        assert_eq!(
            a.expand("sa").unwrap().as_deref(),
            Some("slow_add a=1 b=2 &")
        );
        let a = aliases(&[("sa", "slow_add a=1 b=2")], &[]);
        assert_eq!(
            a.expand("sa &").unwrap().as_deref(),
            Some("slow_add a=1 b=2 &")
        );
    }

    #[test]
    fn a_profile_alias_shadows_the_global_one() {
        let a = aliases(&[("t", "tools")], &[("t", "templates")]);
        assert_eq!(a.expand("t").unwrap().as_deref(), Some("templates"));
        assert_eq!(
            a.lookup("t").map(|(e, s)| (e.to_string(), s)),
            Some(("templates".to_string(), Scope::Profile("cratesio".into())))
        );
        // ... and the shadowed global is not listed as if it applied.
        let entries = a.entries();
        assert_eq!(entries.len(), 1, "{entries:?}");
        assert_eq!(entries[0].expansion, "templates");
    }

    #[test]
    fn switching_profiles_keeps_globals_and_replaces_profile_aliases() {
        let mut aliases = aliases(&[("g", "help")], &[("p", "tools")]);
        aliases.select_profile(Some("other".to_string()), map(&[("q", "prompts")]));
        assert!(aliases.lookup("p").is_none());
        assert_eq!(aliases.lookup("g").map(|(value, _)| value), Some("help"));
        assert_eq!(
            aliases.lookup("q"),
            Some(("prompts", Scope::Profile("other".to_string())))
        );

        aliases.define("live", "resources", false).unwrap();
        aliases.select_profile(None, BTreeMap::new());
        assert!(aliases.lookup("live").is_none());
        aliases.select_profile(Some("other".to_string()), BTreeMap::new());
        assert_eq!(
            aliases.lookup("live").map(|(value, _)| value),
            Some("resources")
        );
    }

    #[test]
    fn entries_list_profile_aliases_before_global_ones() {
        let a = aliases(&[("z", "tools"), ("a", "prompts")], &[("p", "templates")]);
        let names: Vec<String> = a.entries().into_iter().map(|e| e.name).collect();
        assert_eq!(names, vec!["p", "a", "z"]);
    }

    #[test]
    fn a_name_that_would_hide_a_builtin_is_refused() {
        let mut a = aliases(&[], &[]);
        for reserved in ["help", "tool", "builtin"] {
            let err = a.define(reserved, "tools", false).unwrap_err();
            assert!(err.contains("built-in"), "{err}");
        }
        assert!(a.entries().is_empty());
    }

    #[test]
    fn an_alias_can_expand_to_an_explicit_namespace() {
        let a = aliases(&[("w", "tool wait"), ("tool", "tools")], &[]);
        assert_eq!(
            a.expand("w id=42").unwrap().as_deref(),
            Some("tool wait id=42")
        );
        assert_eq!(a.expand("tool wait id=42").unwrap(), None);
    }

    #[test]
    fn a_malformed_name_or_empty_expansion_is_refused() {
        let mut a = aliases(&[], &[]);
        assert!(
            a.define("two words", "tools", false)
                .unwrap_err()
                .contains("whitespace")
        );
        assert!(a.define("a=b", "tools", false).unwrap_err().contains('='));
        assert!(
            a.define("t", "  ", false)
                .unwrap_err()
                .contains("expand to")
        );
    }

    #[test]
    fn define_reports_the_scope_and_what_it_replaced() {
        let mut a = aliases(&[], &[("t", "tools")]);
        let applied = a.define("t", "templates", false).unwrap();
        assert_eq!(applied.scope, Scope::Profile("cratesio".into()));
        assert_eq!(applied.previous.as_deref(), Some("tools"));
        // No config path in these tests, so persistence warns rather than
        // silently claiming a save.
        assert!(applied.warning.is_some());
        assert_eq!(a.expand("t").unwrap().as_deref(), Some("templates"));
    }

    #[test]
    fn define_without_a_profile_lands_in_the_global_table() {
        let mut a = aliases(&[], &[]);
        assert_eq!(a.define("t", "tools", false).unwrap().scope, Scope::Global);
        // --global forces it there even when a profile is connected.
        let mut a = aliases(&[], &[("x", "tools")]);
        assert_eq!(a.define("t", "tools", true).unwrap().scope, Scope::Global);
    }

    #[test]
    fn unalias_removes_the_definition_in_effect() {
        let mut a = aliases(&[("t", "tools")], &[("t", "templates")]);
        assert_eq!(
            a.remove("t", false).unwrap().scope,
            Scope::Profile("cratesio".into())
        );
        // The global one is still there, and now in effect.
        assert_eq!(a.expand("t").unwrap().as_deref(), Some("tools"));
        assert_eq!(a.remove("t", false).unwrap().scope, Scope::Global);
        assert_eq!(a.expand("t").unwrap(), None);
    }

    #[test]
    fn unalias_global_does_not_reach_a_profile_alias() {
        let mut a = aliases(&[], &[("t", "templates")]);
        let err = a.remove("t", true).unwrap_err();
        assert!(err.contains("no global alias"), "{err}");
        assert_eq!(
            a.remove("nope", false).unwrap_err(),
            "no alias named `nope`"
        );
    }

    // -- config file edits --------------------------------------------------

    fn edited(source: &str, edit: impl FnOnce(&mut DocumentMut)) -> String {
        let mut doc = source.parse::<DocumentMut>().unwrap();
        edit(&mut doc);
        doc.to_string()
    }

    const WITH_PROFILE: &str = r#"# my servers
[servers.cratesio]
url = "https://cratesio-mcp.fly.dev/"  # the public one
"#;

    #[test]
    fn writing_an_alias_preserves_the_rest_of_the_file() {
        let out = edited(WITH_PROFILE, |doc| {
            set_in_document(doc, &Scope::Global, "t", "tools").unwrap()
        });
        assert!(out.contains("# my servers"), "{out}");
        assert!(out.contains("# the public one"), "{out}");
        assert!(out.contains("[aliases]"), "{out}");
        assert!(out.contains(r#"t = "tools""#), "{out}");
        // The result still parses as the config it claims to be.
        out.parse::<DocumentMut>().unwrap();
    }

    #[test]
    fn a_profile_alias_is_written_under_the_profile() {
        let out = edited(WITH_PROFILE, |doc| {
            set_in_document(
                doc,
                &Scope::Profile("cratesio".into()),
                "dl",
                "get_downloads",
            )
            .unwrap()
        });
        assert!(out.contains("[servers.cratesio.aliases]"), "{out}");
        assert!(out.contains(r#"dl = "get_downloads""#), "{out}");
    }

    #[test]
    fn a_profile_that_is_not_in_the_file_is_an_error_not_a_new_profile() {
        let mut doc = WITH_PROFILE.parse::<DocumentMut>().unwrap();
        let err =
            set_in_document(&mut doc, &Scope::Profile("absent".into()), "t", "tools").unwrap_err();
        assert!(err.contains("absent"), "{err}");
    }

    #[test]
    fn redefining_replaces_the_value_in_place() {
        let out = edited("[aliases]\nt = \"tools\"\nd = \"describe\"\n", |doc| {
            set_in_document(doc, &Scope::Global, "t", "templates").unwrap()
        });
        assert!(out.contains(r#"t = "templates""#), "{out}");
        assert!(out.contains(r#"d = "describe""#), "{out}");
        assert!(!out.contains(r#""tools""#), "{out}");
    }

    #[test]
    fn an_inline_aliases_table_is_edited_in_place() {
        let out = edited(
            "[servers.x]\nurl = \"https://example/mcp\"\naliases = { t = \"tools\" }\n",
            |doc| {
                set_in_document(doc, &Scope::Profile("x".into()), "d", "describe").unwrap();
            },
        );
        // Still inline, now with both aliases in it.
        assert!(out.contains("aliases = {"), "{out}");
        assert!(out.contains(r#"t = "tools""#), "{out}");
        assert!(out.contains(r#"d = "describe""#), "{out}");
    }

    #[test]
    fn removing_the_last_alias_empties_the_table_but_keeps_its_comment() {
        // The comment above `[aliases]` belongs to that table, so removing
        // the table would take the comment with it. The stub stays instead.
        let out = edited("# keep me\n[aliases]\nt = \"tools\"\n", |doc| {
            remove_from_document(doc, &Scope::Global, "t").unwrap()
        });
        assert!(!out.contains(r#"t = "tools""#), "{out}");
        assert!(out.contains("# keep me"), "{out}");
        assert!(
            crate::config::Config::parse(&out)
                .unwrap()
                .aliases
                .is_empty(),
            "the emptied table should read back as no aliases: {out}"
        );

        // A table with other aliases left in it keeps them.
        let out = edited("[aliases]\nt = \"tools\"\nd = \"describe\"\n", |doc| {
            remove_from_document(doc, &Scope::Global, "t").unwrap()
        });
        assert!(out.contains("[aliases]"), "{out}");
        assert!(out.contains(r#"d = "describe""#), "{out}");
    }

    #[test]
    fn removing_from_a_file_without_the_table_is_not_an_error() {
        let out = edited(WITH_PROFILE, |doc| {
            remove_from_document(doc, &Scope::Global, "t").unwrap();
            remove_from_document(doc, &Scope::Profile("cratesio".into()), "t").unwrap();
            remove_from_document(doc, &Scope::Profile("absent".into()), "t").unwrap();
        });
        assert_eq!(out, WITH_PROFILE);
    }

    #[test]
    fn a_full_round_trip_through_a_real_file() {
        let dir = std::env::temp_dir().join(format!("mcp-repl-alias-{}", std::process::id()));
        let path = dir.join("config.toml");
        let _ = std::fs::remove_dir_all(&dir);
        let mut a = Aliases::new(BTreeMap::new(), BTreeMap::new(), None, Some(path.clone()));

        // The config file does not exist yet: defining creates it.
        let applied = a.define("t", "tools", false).unwrap();
        assert!(applied.warning.is_none(), "{:?}", applied.warning);
        let written = std::fs::read_to_string(&path).unwrap();
        assert!(written.contains("[aliases]"), "{written}");
        assert!(written.contains(r#"t = "tools""#), "{written}");

        // And a later session reads it back as the same alias.
        let config = crate::config::Config::parse(&written).unwrap();
        assert_eq!(config.aliases.get("t").map(String::as_str), Some("tools"));

        a.remove("t", false).unwrap();
        let written = std::fs::read_to_string(&path).unwrap();
        assert!(
            crate::config::Config::parse(&written)
                .unwrap()
                .aliases
                .is_empty(),
            "{written}"
        );
        let _ = std::fs::remove_dir_all(&dir);
    }
}