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
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
//! Import named servers from the common JSON configuration used by MCP
//! clients such as Claude, Cursor, and VS Code.

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};

use serde::Deserialize;

use crate::config::Connection;

/// An explicit `PATH:ENTRY` selector recognized by the CLI.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Selector {
    pub path: PathBuf,
    pub entry: String,
}

/// A resolved imported server plus its display label.
#[derive(Debug, PartialEq, Eq)]
pub struct ImportedConnection {
    pub selector: Selector,
    pub connection: Connection,
    /// Non-secret provenance needed to approve an imported HTTP connection.
    pub http_trust: Option<ImportedHttpTrust>,
}

/// What an imported HTTP entry can forward, without any resolved values.
#[derive(Debug, PartialEq, Eq)]
pub struct ImportedHttpTrust {
    pub header_names: Vec<String>,
    pub header_env_keys: Vec<String>,
    pub url_env_keys: Vec<String>,
}

#[derive(Debug, PartialEq, Eq)]
struct ResolvedEntry {
    connection: Connection,
    http_trust: Option<ImportedHttpTrust>,
}

impl ImportedConnection {
    pub fn label(&self) -> String {
        format!("{}:{}", self.selector.path.display(), self.selector.entry)
    }
}

/// Recognize an explicit JSON config selector without stealing ordinary
/// executable names containing `:`. A `.json` suffix is explicit even when
/// the file is missing, so the user gets a file error rather than attempting
/// to spawn the whole selector as a command.
pub fn parse_selector(value: &str) -> Option<Result<Selector, String>> {
    let (path, entry) = value.rsplit_once(':')?;
    let path = PathBuf::from(path);
    let looks_like_json = path
        .extension()
        .and_then(|extension| extension.to_str())
        .is_some_and(|extension| extension.eq_ignore_ascii_case("json"));
    if !looks_like_json && !path.exists() {
        return None;
    }
    if path.as_os_str().is_empty() {
        return Some(Err("import selector has an empty file path".to_string()));
    }
    if entry.is_empty() {
        return Some(Err(format!(
            "import selector for {} has an empty entry name",
            path.display()
        )));
    }
    Some(Ok(Selector {
        path,
        entry: entry.to_string(),
    }))
}

/// Read and resolve one selected server. Environment values are supplied by
/// the caller so tests never mutate the process environment.
pub fn load_with(
    selector: Selector,
    lookup: impl Fn(&str) -> Option<String>,
) -> Result<ImportedConnection, String> {
    let source = std::fs::read_to_string(&selector.path)
        .map_err(|error| format!("{}: {error}", selector.path.display()))?;
    let path = std::fs::canonicalize(&selector.path)
        .map_err(|error| format!("{}: {error}", selector.path.display()))?;
    let resolved = parse_document(&source, &path, &selector.entry, &lookup)?;
    tracing::debug!(
        path = %path.display(),
        entry = %selector.entry,
        "resolved a client config entry"
    );
    Ok(ImportedConnection {
        selector: Selector {
            path,
            entry: selector.entry,
        },
        connection: resolved.connection,
        http_trust: resolved.http_trust,
    })
}

/// Where MCP clients keep their server lists.
///
/// Project files first, since a scan is usually run inside a repository and
/// that is the config the user means. Paths that do not exist are dropped by
/// [`scan`], so this can list every location worth trying.
pub fn candidate_paths(cwd: &Path, home: Option<&Path>) -> Vec<PathBuf> {
    let directories = crate::directories::Directories::current()
        .with_home(home.map(std::path::Path::to_path_buf));
    candidate_paths_with(cwd, &directories)
}

pub(crate) fn candidate_paths_with(
    cwd: &Path,
    directories: &crate::directories::Directories,
) -> Vec<PathBuf> {
    let mut paths = vec![
        cwd.join(".mcp.json"),
        cwd.join(".vscode").join("mcp.json"),
        cwd.join(".cursor").join("mcp.json"),
    ];
    if let Some(home) = directories.home() {
        paths.push(home.join(".claude.json"));
        paths.push(home.join(".cursor").join("mcp.json"));
    }
    if let Some(path) = directories.claude_desktop_config() {
        paths.push(path);
    }
    paths
}

/// What one config file yielded.
#[derive(Debug)]
pub struct ScannedFile {
    pub path: PathBuf,
    /// The entries, or why the file could not be read. A malformed file is
    /// reported rather than skipped: silence would read as "nothing here".
    pub result: Result<Vec<DiscoveredEntry>, String>,
}

/// Describe every config that exists among `paths`.
///
/// Files that are absent are skipped; files that exist but cannot be parsed
/// are included with their error.
pub fn scan(paths: &[PathBuf]) -> Vec<ScannedFile> {
    tracing::debug!(candidates = paths.len(), "scanning for client configs");
    paths
        .iter()
        .filter(|path| path.is_file())
        .map(|path| ScannedFile {
            path: path.clone(),
            result: list_entries(path),
        })
        .collect()
}

/// One entry found in a client config, described without being resolved.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiscoveredEntry {
    /// The name to put after the colon in a `PATH:ENTRY` selector.
    pub name: String,
    /// `stdio` or `http`, or `?` when the entry declares neither.
    pub transport: String,
    /// The command line or URL, as written in the file.
    pub summary: String,
}

/// List what a config file offers, without resolving it.
///
/// Deliberately not [`load_with`]: resolution expands `${env:NAME}` and fails
/// on a variable that is not set, which would make a scan of somebody else's
/// config report errors for entries that are perfectly fine to use once the
/// variable is exported. Placeholders are shown as written.
///
/// Nothing here executes anything: it reads a file and describes it.
pub fn list_entries(path: &Path) -> Result<Vec<DiscoveredEntry>, String> {
    let source = std::fs::read_to_string(path).map_err(|error| plain_io_error(&error))?;
    let document: Document =
        serde_json::from_str(&source).map_err(|error| format!("not valid JSON: {error}"))?;
    let mut found: Vec<DiscoveredEntry> = document
        .mcp_servers
        .iter()
        .chain(document.servers.iter())
        .map(|(name, entry)| DiscoveredEntry {
            name: name.clone(),
            transport: entry.declared_transport().to_string(),
            summary: entry.summary(),
        })
        .collect();
    found.sort_by(|a, b| a.name.cmp(&b.name));
    found.dedup_by(|a, b| a.name == b.name);
    Ok(found)
}

/// An io error without the path repeated: the caller already prints it.
fn plain_io_error(error: &std::io::Error) -> String {
    match error.kind() {
        std::io::ErrorKind::NotFound => "no such file".to_string(),
        std::io::ErrorKind::PermissionDenied => "permission denied".to_string(),
        _ => error.to_string(),
    }
}

impl Entry {
    /// What the entry says it is, before any validation.
    fn declared_transport(&self) -> &'static str {
        let declared = self.kind.as_deref().or(self.transport.as_deref());
        match declared {
            Some(kind) if kind.eq_ignore_ascii_case("stdio") => "stdio",
            Some(_) => "http",
            None if self.command.is_some() => "stdio",
            None if self.url.is_some() => "http",
            None => "?",
        }
    }

    /// The command line or URL as written, for a scan listing.
    fn summary(&self) -> String {
        if let Some(command) = &self.command {
            let mut line = command.clone();
            for arg in &self.args {
                line.push(' ');
                line.push_str(arg);
            }
            return line;
        }
        self.url
            .clone()
            .unwrap_or_else(|| "(no command or url)".to_string())
    }
}

#[derive(Debug, Default, Deserialize)]
struct Document {
    #[serde(default, rename = "mcpServers")]
    mcp_servers: BTreeMap<String, Entry>,
    #[serde(default)]
    servers: BTreeMap<String, Entry>,
}

#[derive(Debug, Default, Deserialize)]
struct Entry {
    #[serde(rename = "type")]
    kind: Option<String>,
    transport: Option<String>,
    command: Option<String>,
    #[serde(default)]
    args: Vec<String>,
    #[serde(default)]
    env: BTreeMap<String, String>,
    cwd: Option<String>,
    url: Option<String>,
    #[serde(default)]
    headers: BTreeMap<String, String>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum ImportedTransport {
    Http,
    Stdio,
}

fn parse_document(
    source: &str,
    path: &Path,
    selected: &str,
    lookup: &impl Fn(&str) -> Option<String>,
) -> Result<ResolvedEntry, String> {
    let document: Document = serde_json::from_str(source)
        .map_err(|error| format!("{}: invalid MCP JSON config: {error}", path.display()))?;
    let mut entries = document.mcp_servers;
    for (name, entry) in document.servers {
        if entries.insert(name.clone(), entry).is_some() {
            return Err(format!(
                "{} defines server {name:?} in both `mcpServers` and `servers`",
                path.display()
            ));
        }
    }
    let entry = entries.get(selected).ok_or_else(|| {
        let available = entries.keys().cloned().collect::<Vec<_>>();
        if available.is_empty() {
            format!(
                "{} has no entries under `mcpServers` or `servers`",
                path.display()
            )
        } else {
            format!(
                "{} has no server named {selected:?}; available servers: {}",
                path.display(),
                available.join(", ")
            )
        }
    })?;
    resolve_entry(entry, path, selected, lookup)
}

fn resolve_entry(
    entry: &Entry,
    path: &Path,
    name: &str,
    lookup: &impl Fn(&str) -> Option<String>,
) -> Result<ResolvedEntry, String> {
    let workspace = workspace_folder(path);
    let declared = match (&entry.kind, &entry.transport) {
        (Some(kind), Some(transport)) => {
            let kind = parse_transport(kind)?;
            let transport = parse_transport(transport)?;
            if kind != transport {
                return Err(format!(
                    "server {name:?} has conflicting `type` and `transport` values"
                ));
            }
            Some(kind)
        }
        (Some(kind), None) | (None, Some(kind)) => Some(parse_transport(kind)?),
        (None, None) => None,
    };
    let has_command = entry.command.is_some();
    let has_url = entry.url.is_some();
    let transport = match (declared, has_command, has_url) {
        (Some(transport), _, _) => transport,
        (None, true, false) => ImportedTransport::Stdio,
        (None, false, true) => ImportedTransport::Http,
        (None, true, true) => {
            return Err(format!(
                "server {name:?} sets both `command` and `url`; add `type` to choose a transport"
            ));
        }
        (None, false, false) => {
            return Err(format!(
                "server {name:?} has neither `command` nor `url`, so its transport cannot be inferred"
            ));
        }
    };

    match transport {
        ImportedTransport::Stdio => {
            if entry.url.is_some() || !entry.headers.is_empty() {
                return Err(format!(
                    "stdio server {name:?} also sets HTTP-only `url` or `headers`"
                ));
            }
            let command = entry
                .command
                .as_deref()
                .ok_or_else(|| format!("stdio server {name:?} has no `command`"))?;
            let mut command_and_args = Vec::with_capacity(entry.args.len() + 1);
            command_and_args.push(expand(command, &workspace, lookup)?);
            for argument in &entry.args {
                command_and_args.push(expand(argument, &workspace, lookup)?);
            }
            if command_and_args[0].is_empty() {
                return Err(format!("stdio server {name:?} has an empty `command`"));
            }
            let env = entry
                .env
                .iter()
                .map(|(key, value)| {
                    expand(value, &workspace, lookup).map(|value| (key.clone(), value))
                })
                .collect::<Result<BTreeMap<_, _>, _>>()?;
            let cwd = entry
                .cwd
                .as_deref()
                .map(|cwd| expand(cwd, &workspace, lookup))
                .transpose()?
                .map(PathBuf::from)
                .map(|cwd| {
                    if cwd.is_absolute() {
                        cwd
                    } else {
                        workspace.join(cwd)
                    }
                });
            Ok(ResolvedEntry {
                connection: Connection::Stdio {
                    command: command_and_args,
                    env,
                    cwd,
                },
                http_trust: None,
            })
        }
        ImportedTransport::Http => {
            if entry.command.is_some()
                || !entry.args.is_empty()
                || !entry.env.is_empty()
                || entry.cwd.is_some()
            {
                return Err(format!(
                    "HTTP server {name:?} also sets stdio-only `command`, `args`, `env`, or `cwd`"
                ));
            }
            let url = entry
                .url
                .as_deref()
                .ok_or_else(|| format!("HTTP server {name:?} has no `url`"))?;
            let mut url_env_keys = BTreeSet::new();
            let url = expand_recording(url, &workspace, lookup, &mut url_env_keys)?;
            let mut header_env_keys = BTreeSet::new();
            let mut headers = Vec::with_capacity(entry.headers.len());
            for (key, value) in &entry.headers {
                headers.push((
                    key.clone(),
                    expand_recording(value, &workspace, lookup, &mut header_env_keys)?,
                ));
            }
            let mut header_names: Vec<String> = entry
                .headers
                .keys()
                .map(|name| name.to_ascii_lowercase())
                .collect();
            header_names.sort();
            header_names.dedup();
            Ok(ResolvedEntry {
                connection: Connection::Http {
                    url,
                    bearer: None,
                    headers,
                    oauth: None,
                },
                http_trust: Some(ImportedHttpTrust {
                    header_names,
                    header_env_keys: header_env_keys.into_iter().collect(),
                    url_env_keys: url_env_keys.into_iter().collect(),
                }),
            })
        }
    }
}

fn parse_transport(value: &str) -> Result<ImportedTransport, String> {
    match value.to_ascii_lowercase().replace(['-', '_'], "").as_str() {
        "stdio" => Ok(ImportedTransport::Stdio),
        "http" | "streamablehttp" => Ok(ImportedTransport::Http),
        "sse" => Err(
            "transport `sse` is not supported; mcp-repl requires Streamable HTTP (`http`)"
                .to_string(),
        ),
        _ => Err(format!(
            "unsupported imported transport {value:?}; expected `stdio` or `http`"
        )),
    }
}

fn workspace_folder(config_path: &Path) -> PathBuf {
    let parent = config_path.parent().unwrap_or_else(|| Path::new("."));
    if parent.file_name().is_some_and(|name| name == ".vscode") {
        parent.parent().unwrap_or(parent).to_path_buf()
    } else {
        parent.to_path_buf()
    }
}

fn expand(
    input: &str,
    workspace: &Path,
    lookup: &impl Fn(&str) -> Option<String>,
) -> Result<String, String> {
    expand_recording(input, workspace, lookup, &mut BTreeSet::new())
}

fn expand_recording(
    input: &str,
    workspace: &Path,
    lookup: &impl Fn(&str) -> Option<String>,
    env_keys: &mut BTreeSet<String>,
) -> Result<String, String> {
    let mut rendered = String::new();
    let mut rest = input;
    while let Some(start) = rest.find("${") {
        rendered.push_str(&rest[..start]);
        let after_open = &rest[start + 2..];
        let Some(end) = after_open.find('}') else {
            return Err("unterminated `${...}` substitution in imported config".to_string());
        };
        let variable = &after_open[..end];
        let replacement = match variable {
            "workspaceFolder" => workspace.to_string_lossy().into_owned(),
            "workspaceFolderBasename" => workspace
                .file_name()
                .map(|name| name.to_string_lossy().into_owned())
                .unwrap_or_default(),
            "userHome" => {
                if let Some(value) = lookup("HOME") {
                    env_keys.insert("HOME".to_string());
                    value
                } else if let Some(value) = lookup("USERPROFILE") {
                    env_keys.insert("USERPROFILE".to_string());
                    value
                } else {
                    return Err(
                        "`${userHome}` requires the HOME or USERPROFILE environment variable"
                            .to_string(),
                    );
                }
            }
            variable if variable.starts_with("input:") => {
                return Err(format!(
                    "`${{{variable}}}` requires interactive client input, which mcp-repl cannot import; use an environment variable instead"
                ));
            }
            variable => {
                let variable = variable.strip_prefix("env:").unwrap_or(variable);
                if variable.is_empty() {
                    return Err(
                        "imported config contains an empty environment substitution".to_string()
                    );
                }
                env_keys.insert(variable.to_string());
                lookup(variable).ok_or_else(|| {
                    format!("imported config requires environment variable {variable:?}, but it is unset")
                })?
            }
        };
        rendered.push_str(&replacement);
        rest = &after_open[end + 1..];
    }
    rendered.push_str(rest);
    Ok(rendered)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::directories::{Directories, Platform};
    use crate::property::{
        GENERATED_CASES, Generator, INTERPOLATION_REGRESSIONS, SELECTOR_REGRESSIONS,
    };
    use std::ffi::OsString;

    #[test]
    fn property_selectors_and_interpolation_are_total_and_do_not_leak_on_error() {
        for selector in SELECTOR_REGRESSIONS {
            if let Some(Err(error)) = parse_selector(selector) {
                assert!(!error.is_empty());
            }
        }
        for input in INTERPOLATION_REGRESSIONS {
            let result = expand(input, Path::new("/workspace/project"), &|name| {
                Some(format!("value-for-{name}"))
            });
            if let Err(error) = result {
                assert!(!error.is_empty());
            }
        }

        let mut generator = Generator::new(0x03);
        for case in 0..GENERATED_CASES {
            let arbitrary = generator.text(128);
            if let Some(result) = parse_selector(&arbitrary) {
                match result {
                    Ok(selector) => {
                        assert!(!selector.path.as_os_str().is_empty());
                        assert!(!selector.entry.is_empty());
                    }
                    Err(error) => assert!(!error.is_empty()),
                }
            }

            let entry = format!("server_{case}");
            let selector = parse_selector(&format!("fuzz-{case}.JSON:{entry}"))
                .expect("a JSON suffix is an explicit selector")
                .expect("a non-empty selector is valid");
            assert_eq!(selector.entry, entry);

            let _ = expand(&arbitrary, Path::new("/workspace/project"), &|name| {
                Some(format!("value-for-{name}"))
            });

            let seeded_secret = format!("never-echo-{case:04x}");
            let error = expand(
                &format!("{seeded_secret}-${{env:MISSING}}"),
                Path::new("/workspace/project"),
                &|_| None,
            )
            .expect_err("the generated missing variable must be rejected");
            assert!(
                !error.contains(&seeded_secret),
                "secret leaked in {error:?}"
            );
        }
    }

    fn write(dir: &std::path::Path, name: &str, body: &str) -> PathBuf {
        let path = dir.join(name);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(&path, body).unwrap();
        path
    }

    #[test]
    fn listing_describes_both_roots_and_both_transports() {
        let dir = tempfile::tempdir().unwrap();
        let path = write(
            dir.path(),
            ".mcp.json",
            r#"{
                "mcpServers": {
                    "local": {"command": "node", "args": ["server.js", "--stdio"]}
                },
                "servers": {
                    "remote": {"type": "http", "url": "https://example.com/mcp"}
                }
            }"#,
        );
        let entries = list_entries(&path).unwrap();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].name, "local");
        assert_eq!(entries[0].transport, "stdio");
        assert_eq!(entries[0].summary, "node server.js --stdio");
        assert_eq!(entries[1].name, "remote");
        assert_eq!(entries[1].transport, "http");
        assert_eq!(entries[1].summary, "https://example.com/mcp");
    }

    #[test]
    fn listing_does_not_resolve_placeholders() {
        // Resolution fails on a variable that is not set. A scan of someone
        // else's config must still describe the entry, since exporting the
        // variable is all that is needed to use it.
        let dir = tempfile::tempdir().unwrap();
        let path = write(
            dir.path(),
            ".mcp.json",
            r#"{"mcpServers": {"x": {"url": "${env:NEVER_SET_ANYWHERE}"}}}"#,
        );
        let entries = list_entries(&path).unwrap();
        assert_eq!(entries[0].summary, "${env:NEVER_SET_ANYWHERE}");
        // The resolving path is the one that objects.
        assert!(
            load_with(
                Selector {
                    path,
                    entry: "x".to_string()
                },
                |_| None
            )
            .is_err()
        );
    }

    #[test]
    fn a_malformed_file_reports_rather_than_disappearing() {
        let dir = tempfile::tempdir().unwrap();
        let path = write(dir.path(), ".mcp.json", "not json {{{");
        let error = list_entries(&path).unwrap_err();
        assert!(error.contains("not valid JSON"), "{error}");

        // And a scan includes it, so a broken config is visible instead of
        // looking like an empty one.
        let scanned = scan(std::slice::from_ref(&path));
        assert_eq!(scanned.len(), 1);
        assert!(scanned[0].result.is_err());
    }

    #[test]
    fn scanning_skips_paths_that_do_not_exist() {
        let dir = tempfile::tempdir().unwrap();
        let real = write(
            dir.path(),
            ".mcp.json",
            r#"{"mcpServers":{"a":{"url":"http://x"}}}"#,
        );
        let scanned = scan(&[dir.path().join("absent.json"), real]);
        assert_eq!(scanned.len(), 1, "a missing file is not an error to report");
    }

    #[test]
    fn candidates_cover_the_project_files_and_the_home_ones() {
        let cwd = std::path::Path::new("/work/api");
        let home = std::path::Path::new("/home/dev");
        let directories = Directories::from_lookup(Platform::Unix, |name| {
            (name == "HOME").then(|| home.as_os_str().to_owned())
        });
        let paths = candidate_paths_with(cwd, &directories);
        for expected in [
            cwd.join(".mcp.json"),
            cwd.join(".vscode").join("mcp.json"),
            cwd.join(".cursor").join("mcp.json"),
            home.join(".claude.json"),
        ] {
            assert!(paths.contains(&expected), "missing {}", expected.display());
        }
        // Project files come first: a scan run in a repo means that repo.
        assert!(paths[0].starts_with(cwd));
        // Without a home directory it still offers the project files.
        let no_home = Directories::from_lookup(Platform::Unix, |_| None);
        assert_eq!(candidate_paths_with(cwd, &no_home).len(), 3);
    }

    #[test]
    fn windows_directories_find_appdata_candidates_without_a_home() {
        let cwd = std::path::Path::new("workspace");
        let directories = Directories::from_lookup(Platform::Windows, |name| {
            (name == "APPDATA").then(|| OsString::from(r"C:\Users\Ada\AppData\Roaming"))
        });
        let paths = candidate_paths_with(cwd, &directories);
        assert!(
            paths.contains(
                &PathBuf::from(r"C:\Users\Ada\AppData\Roaming")
                    .join("Claude")
                    .join("claude_desktop_config.json")
            ),
            "{paths:?}"
        );
        assert_eq!(paths.len(), 4, "only project and APPDATA paths are known");
    }

    fn env(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<String> + use<> {
        let values: BTreeMap<String, String> = pairs
            .iter()
            .map(|(key, value)| (key.to_string(), value.to_string()))
            .collect();
        move |key| values.get(key).cloned()
    }

    #[test]
    fn parses_claude_stdio_shape_with_workspace_and_environment() {
        let source = r#"{
          "mcpServers": {
            "local": {
              "command": "${workspaceFolder}/bin/server",
              "args": ["--repo", "${workspaceFolderBasename}"],
              "env": {"API_TOKEN": "${env:HOST_TOKEN}"},
              "cwd": "work"
            }
          }
        }"#;
        let resolved = parse_document(
            source,
            Path::new("/repo/.mcp.json"),
            "local",
            &env(&[("HOST_TOKEN", "secret")]),
        )
        .unwrap();
        assert_eq!(
            resolved.connection,
            Connection::Stdio {
                command: vec![
                    "/repo/bin/server".to_string(),
                    "--repo".to_string(),
                    "repo".to_string(),
                ],
                env: BTreeMap::from([("API_TOKEN".to_string(), "secret".to_string())]),
                cwd: Some(PathBuf::from("/repo/work")),
            }
        );
    }

    #[test]
    fn parses_vscode_http_shape_and_uses_workspace_parent() {
        let source = r#"{
          "servers": {
            "remote": {
              "type": "streamable-http",
              "url": "${env:MCP_URL}",
              "headers": {"Authorization": "Bearer ${TOKEN}"}
            }
          }
        }"#;
        let resolved = parse_document(
            source,
            Path::new("/repo/.vscode/mcp.json"),
            "remote",
            &env(&[("MCP_URL", "https://example/mcp"), ("TOKEN", "secret")]),
        )
        .unwrap();
        assert_eq!(
            resolved.connection,
            Connection::Http {
                url: "https://example/mcp".to_string(),
                bearer: None,
                headers: vec![("Authorization".to_string(), "Bearer secret".to_string())],
                oauth: None,
            }
        );
        assert_eq!(
            resolved.http_trust,
            Some(ImportedHttpTrust {
                header_names: vec!["authorization".to_string()],
                header_env_keys: vec!["TOKEN".to_string()],
                url_env_keys: vec!["MCP_URL".to_string()],
            })
        );
    }

    #[test]
    fn missing_entry_lists_sorted_names() {
        let error = parse_document(
            r#"{"mcpServers":{"z":{"command":"z"},"a":{"command":"a"}}}"#,
            Path::new("/repo/.mcp.json"),
            "missing",
            &env(&[]),
        )
        .unwrap_err();
        assert!(error.contains("a, z"), "{error}");
    }

    #[test]
    fn rejects_ambiguous_and_unsupported_transports() {
        let ambiguous = parse_document(
            r#"{"mcpServers":{"x":{"command":"x","url":"https://example"}}}"#,
            Path::new("/repo/.mcp.json"),
            "x",
            &env(&[]),
        )
        .unwrap_err();
        assert!(ambiguous.contains("both"), "{ambiguous}");

        let sse = parse_document(
            r#"{"servers":{"x":{"type":"sse","url":"https://example"}}}"#,
            Path::new("/repo/mcp.json"),
            "x",
            &env(&[]),
        )
        .unwrap_err();
        assert!(sse.contains("Streamable HTTP"), "{sse}");
    }

    #[test]
    fn missing_substitutions_name_the_variable_without_leaking_values() {
        let error = parse_document(
            r#"{
              "mcpServers": {
                "x": {
                  "command": "server",
                  "args": ["${env:MISSING}"],
                  "env": {"LITERAL_SECRET": "do-not-print-me"}
                }
              }
            }"#,
            Path::new("/repo/.mcp.json"),
            "x",
            &env(&[]),
        )
        .unwrap_err();
        assert!(error.contains("MISSING"), "{error}");
        assert!(!error.contains("do-not-print-me"), "{error}");
    }

    #[test]
    fn interactive_input_substitutions_are_actionable_errors() {
        let error = expand("${input:token}", Path::new("/repo"), &env(&[])).unwrap_err();
        assert!(error.contains("interactive"), "{error}");
        assert!(error.contains("environment variable"), "{error}");
    }

    #[test]
    fn selector_recognition_does_not_steal_ordinary_commands() {
        assert!(parse_selector("registry:serve").is_none());
        assert_eq!(
            parse_selector("path/to/.mcp.json:server").unwrap().unwrap(),
            Selector {
                path: PathBuf::from("path/to/.mcp.json"),
                entry: "server".to_string(),
            }
        );
    }
}