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
//! Approval for using a server named by an imported client config.
//!
//! A native profile is in the user's own config file. An imported `.mcp.json`
//! entry is not: it can arrive with a repository, choose a program to run, or
//! choose a remote HTTP origin and headers to send. The first time an entry is
//! used, the REPL shows the security-relevant, non-secret identity and asks.
//!
//! Approvals are recorded in `approved-imports.toml` beside the config,
//! keyed by what was approved rather than by a hash of it: the file is meant
//! to be readable, so an operator can audit what past runs agreed to, and
//! deleting it forgets everything. A changed command, working directory,
//! HTTP origin, header set, or set of forwarded variables does not match the
//! record, so it asks again.

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

use nu_ansi_term::{Color, Style};
use serde::{Deserialize, Serialize};

use crate::secure_file;
use crate::style::{paint, sanitize, tag};

/// What a selected import resolved to, and what approval is checked against.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImportPlan {
    /// The config file the entry came from, absolute where it can be.
    pub source: String,
    /// The entry name inside that file.
    pub entry: String,
    /// The program and its arguments, exactly as they will be spawned.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub command: Vec<String>,
    /// The working directory, when the entry sets one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cwd: Option<String>,
    /// The normalized HTTP origin. Paths, queries, and credentials are never
    /// persisted in the approval store.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub origin: Option<String>,
    /// Header names an imported HTTP entry supplies. Values are deliberately
    /// absent for the same reason environment values are absent.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub header_names: Vec<String>,
    /// Names of the variables the entry references for a child or HTTP
    /// connection. Values are deliberately absent: this file records what
    /// was approved, and a decision record is not a place to copy secrets.
    #[serde(default)]
    pub env_keys: Vec<String>,
}

impl ImportPlan {
    pub fn stdio(
        source: &Path,
        entry: &str,
        command: &[String],
        cwd: Option<&Path>,
        env: &BTreeMap<String, String>,
    ) -> Self {
        let mut env_keys: Vec<String> = env.keys().cloned().collect();
        env_keys.sort();
        Self {
            // Canonicalize so the same file reached by different relative
            // paths is one entry; fall back to what was given when the path
            // cannot be resolved.
            source: std::fs::canonicalize(source)
                .unwrap_or_else(|_| source.to_path_buf())
                .display()
                .to_string(),
            entry: entry.to_string(),
            command: command.to_vec(),
            cwd: cwd.map(|p| p.display().to_string()),
            origin: None,
            header_names: Vec::new(),
            env_keys,
        }
    }

    pub fn http(
        source: &Path,
        entry: &str,
        destination: &str,
        header_names: &[String],
        env_keys: &[String],
    ) -> Result<Self, String> {
        let url = url::Url::parse(destination)
            .map_err(|error| format!("invalid imported HTTP URL: {error}"))?;
        if !matches!(url.scheme(), "http" | "https") || url.host().is_none() {
            return Err(
                "invalid imported HTTP URL; expected an http:// or https:// URL".to_string(),
            );
        }
        let mut header_names: Vec<String> = header_names
            .iter()
            .map(|name| name.to_ascii_lowercase())
            .collect();
        header_names.sort();
        header_names.dedup();
        let mut env_keys = env_keys.to_vec();
        env_keys.sort();
        env_keys.dedup();
        Ok(Self {
            source: canonical_source(source),
            entry: entry.to_string(),
            command: Vec::new(),
            cwd: None,
            origin: Some(url.origin().ascii_serialization()),
            header_names,
            env_keys,
        })
    }

    /// The variables being forwarded whose names look like credentials.
    fn credential_env_keys(&self) -> Vec<&String> {
        self.env_keys
            .iter()
            .filter(|key| crate::wire::looks_like_credential(key))
            .collect()
    }

    /// The command as a single shell-ish line, for display only. Nothing
    /// re-parses this; the child is spawned from the argument vector.
    fn command_line(&self) -> String {
        self.command
            .iter()
            .map(|part| {
                if part.contains(char::is_whitespace) {
                    format!("{part:?}")
                } else {
                    part.clone()
                }
            })
            .collect::<Vec<_>>()
            .join(" ")
    }
}

fn canonical_source(source: &Path) -> String {
    std::fs::canonicalize(source)
        .unwrap_or_else(|_| source.to_path_buf())
        .display()
        .to_string()
}

/// The `approved-imports.toml` document.
#[derive(Debug, Default, Deserialize, Serialize)]
struct Approvals {
    #[serde(default, rename = "approved")]
    entries: Vec<ImportPlan>,
}

/// Where approvals live: beside the config file that named the profiles.
pub fn approvals_path(config_path: Option<&Path>) -> Option<PathBuf> {
    let directory = config_path?.parent()?;
    Some(directory.join("approved-imports.toml"))
}

fn load(path: &Path) -> Approvals {
    let Ok(text) = std::fs::read_to_string(path) else {
        return Approvals::default();
    };
    // A corrupt or hand-mangled trust store is treated as empty: asking
    // again is the safe direction.
    toml::from_str(&text).unwrap_or_default()
}

fn remember(path: &Path, plan: &ImportPlan) -> Result<(), String> {
    let mut approvals = load(path);
    if !approvals.entries.iter().any(|known| known == plan) {
        approvals.entries.push(plan.clone());
    }
    let text = toml::to_string_pretty(&approvals)
        .map_err(|e| format!("could not encode {}: {e}", path.display()))?;
    let document = format!(
        "# Imported client-config entries approved for use, written by\n\
         # mcp-repl. Delete a block to be asked about it again, or delete the\n\
         # file to forget every approval.\n\n{text}"
    );
    secure_file::write_atomic(path, &document)
        .map_err(|e| format!("could not write {}: {e}", path.display()))
}

/// Whether this exact plan was approved before.
fn is_approved(path: Option<&Path>, plan: &ImportPlan) -> bool {
    path.is_some_and(|path| load(path).entries.iter().any(|known| known == plan))
}

/// The outcome of asking about a plan.
pub enum Decision {
    /// Use the imported entry.
    Approved,
    /// Do not use it, with the reason to report.
    Refused(String),
}

/// Decide whether to use an imported entry.
///
/// `trusted` is `--trust-import`, which approves without asking and without
/// recording. `interactive` is false under `--exec`/`--json` and when stdin
/// is not a terminal, where there is nobody to ask: rather than blocking on
/// a read that will never return, the run stops with the flag to pass.
pub fn authorize(
    plan: &ImportPlan,
    config_path: Option<&Path>,
    trusted: bool,
    interactive: bool,
) -> Decision {
    if trusted {
        tracing::debug!(source = %plan.source, entry = %plan.entry, "import approved by --trust-import");
        return Decision::Approved;
    }
    let store = approvals_path(config_path);
    if is_approved(store.as_deref(), plan) {
        tracing::debug!(
            source = %plan.source,
            entry = %plan.entry,
            store = ?store.as_deref().map(|p| p.display().to_string()),
            "import approved by a recorded approval"
        );
        return Decision::Approved;
    }
    tracing::debug!(
        source = %plan.source,
        entry = %plan.entry,
        interactive,
        "import has no recorded approval"
    );
    if !interactive {
        return Decision::Refused(format!(
            "refusing to use the server imported from {}:{} without approval.\n\
             This entry has not been approved before, and a non-interactive session has \
             nobody to ask. Run it once interactively to review and approve it, or pass \
             --trust-import to skip the check.",
            plan.source, plan.entry
        ));
    }
    describe(plan);
    match confirm() {
        false => Decision::Refused(format!(
            "not using {}:{}",
            plan.source,
            sanitize(&plan.entry)
        )),
        true => {
            match store {
                Some(path) => {
                    if let Err(e) = remember(&path, plan) {
                        // The import was approved for this run either way;
                        // only the memory of it failed.
                        eprintln!("warning: {e}");
                    }
                }
                None => eprintln!(
                    "note: no config location, so this approval is not saved and will be \
                     asked again next time"
                ),
            }
            Decision::Approved
        }
    }
}

/// Show what will run. Everything here comes from the imported file, so it
/// is sanitized before display like any other untrusted text.
fn describe(plan: &ImportPlan) {
    if let Some(origin) = &plan.origin {
        println!(
            "{} {}:{} wants to connect to an HTTP server:",
            tag(Style::new().fg(Color::Yellow), "import"),
            sanitize(&plan.source),
            sanitize(&plan.entry)
        );
        println!(
            "  origin:  {}",
            paint(Style::new().bold(), &sanitize(origin))
        );
        if !plan.header_names.is_empty() {
            let names: Vec<String> = plan
                .header_names
                .iter()
                .map(|name| sanitize(name).into_owned())
                .collect();
            println!("  headers: {}", names.join(", "));
        }
        if !plan.env_keys.is_empty() {
            let names: Vec<String> = plan
                .env_keys
                .iter()
                .map(|key| sanitize(key).into_owned())
                .collect();
            println!("  env:     {}", names.join(", "));
        }
        for name in &plan.header_names {
            if crate::wire::looks_like_credential(name) {
                println!(
                    "  {} {} can carry a credential to this origin",
                    paint(Style::new().fg(Color::Yellow).bold(), "warning:"),
                    sanitize(name)
                );
            }
        }
        for key in plan.credential_env_keys() {
            println!(
                "  {} {} looks like a credential, and its value can be sent to this origin",
                paint(Style::new().fg(Color::Yellow).bold(), "warning:"),
                sanitize(key)
            );
        }
        println!(
            "{}",
            paint(
                Style::new().dimmed(),
                "The imported file chooses the remote server and headers. Approve it only if you trust that file."
            )
        );
        return;
    }
    println!(
        "{} {}:{} wants to start a server process on this machine:",
        tag(Style::new().fg(Color::Yellow), "import"),
        sanitize(&plan.source),
        sanitize(&plan.entry)
    );
    println!(
        "  command: {}",
        paint(Style::new().bold(), &sanitize(&plan.command_line()))
    );
    if let Some(cwd) = &plan.cwd {
        println!("  cwd:     {}", sanitize(cwd));
    }
    if !plan.env_keys.is_empty() {
        let names: Vec<String> = plan
            .env_keys
            .iter()
            .map(|key| sanitize(key).into_owned())
            .collect();
        println!("  env:     {}", names.join(", "));
    }
    for key in plan.credential_env_keys() {
        println!(
            "  {} {} looks like a credential, and its value goes to this program",
            paint(Style::new().fg(Color::Yellow).bold(), "warning:"),
            sanitize(key)
        );
    }
    println!(
        "{}",
        paint(
            Style::new().dimmed(),
            "The imported file chooses the program and which of your environment variables \
             it receives. Approve it only if you trust that file."
        )
    );
}

fn confirm() -> bool {
    print!("  approve it? [y/N]> ");
    let _ = std::io::stdout().flush();
    let mut buf = String::new();
    let read = {
        let mut lock = std::io::stdin().lock();
        std::io::BufRead::read_line(&mut lock, &mut buf)
    };
    match read {
        Ok(0) | Err(_) => false,
        Ok(_) => matches!(buf.trim(), "y" | "Y" | "yes" | "Yes"),
    }
}

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

    fn plan(command: &[&str]) -> ImportPlan {
        let mut env = BTreeMap::new();
        env.insert("API_TOKEN".to_string(), "secret-value".to_string());
        env.insert("REGION".to_string(), "us-east-1".to_string());
        ImportPlan::stdio(
            Path::new("/repo/.mcp.json"),
            "local",
            &command.iter().map(|s| s.to_string()).collect::<Vec<_>>(),
            Some(Path::new("/repo")),
            &env,
        )
    }

    #[test]
    fn a_recorded_plan_is_not_asked_about_again() {
        let dir = tempfile::tempdir().unwrap();
        let store = dir.path().join("approved-imports.toml");
        let plan = plan(&["server", "--stdio"]);
        assert!(!is_approved(Some(&store), &plan));
        remember(&store, &plan).unwrap();
        assert!(is_approved(Some(&store), &plan));
    }

    #[test]
    fn a_changed_command_is_a_different_plan() {
        let dir = tempfile::tempdir().unwrap();
        let store = dir.path().join("approved-imports.toml");
        remember(&store, &plan(&["server", "--stdio"])).unwrap();
        // The entry now runs something else: the old approval must not
        // cover it.
        assert!(!is_approved(Some(&store), &plan(&["curl", "evil.sh"])));
    }

    #[test]
    fn a_changed_env_set_is_a_different_plan() {
        let dir = tempfile::tempdir().unwrap();
        let store = dir.path().join("approved-imports.toml");
        let approved = plan(&["server"]);
        remember(&store, &approved).unwrap();
        let mut widened = approved.clone();
        widened.env_keys.push("AWS_SECRET_ACCESS_KEY".to_string());
        assert!(!is_approved(Some(&store), &widened));
    }

    #[test]
    fn the_store_never_holds_env_values() {
        let dir = tempfile::tempdir().unwrap();
        let store = dir.path().join("approved-imports.toml");
        remember(&store, &plan(&["server"])).unwrap();
        let written = std::fs::read_to_string(&store).unwrap();
        assert!(written.contains("API_TOKEN"), "keys are the record");
        assert!(
            !written.contains("secret-value"),
            "a trust record must not become a place secrets live"
        );
    }

    #[test]
    fn credential_shaped_variables_are_singled_out() {
        let mut env = BTreeMap::new();
        env.insert("GITHUB_TOKEN".to_string(), "x".to_string());
        env.insert("AWS_SECRET_ACCESS_KEY".to_string(), "x".to_string());
        env.insert("REGION".to_string(), "x".to_string());
        let plan = ImportPlan::stdio(Path::new("/repo/.mcp.json"), "local", &[], None, &env);
        let flagged: Vec<&str> = plan
            .credential_env_keys()
            .into_iter()
            .map(String::as_str)
            .collect();
        assert_eq!(flagged, vec!["AWS_SECRET_ACCESS_KEY", "GITHUB_TOKEN"]);
    }

    #[test]
    fn a_corrupt_store_asks_again_rather_than_trusting() {
        let dir = tempfile::tempdir().unwrap();
        let store = dir.path().join("approved-imports.toml");
        std::fs::write(&store, "this is not toml {{{").unwrap();
        assert!(!is_approved(Some(&store), &plan(&["server"])));
    }

    #[test]
    fn without_a_config_location_nothing_is_pre_approved() {
        assert!(!is_approved(None, &plan(&["server"])));
    }

    #[test]
    fn trust_import_skips_the_check_entirely() {
        assert!(matches!(
            authorize(&plan(&["server"]), None, true, false),
            Decision::Approved
        ));
    }

    #[test]
    fn a_non_interactive_session_refuses_with_guidance() {
        match authorize(&plan(&["server"]), None, false, false) {
            Decision::Refused(message) => {
                assert!(message.contains("--trust-import"));
                assert!(message.contains("local"));
            }
            Decision::Approved => panic!("an unapproved entry must not run unattended"),
        }
    }

    fn http_plan(destination: &str, headers: &[&str], env_keys: &[&str]) -> ImportPlan {
        ImportPlan::http(
            Path::new("/repo/.mcp.json"),
            "remote",
            destination,
            &headers
                .iter()
                .map(|value| value.to_string())
                .collect::<Vec<_>>(),
            &env_keys
                .iter()
                .map(|value| value.to_string())
                .collect::<Vec<_>>(),
        )
        .unwrap()
    }

    #[test]
    fn an_http_plan_records_only_the_origin_and_forwarded_names() {
        let plan = http_plan(
            "https://user:password@example.com:443/private?token=literal-secret",
            &["X-Api-Key", "Authorization"],
            &["TOKEN", "MCP_URL", "TOKEN"],
        );
        assert_eq!(plan.origin.as_deref(), Some("https://example.com"));
        assert_eq!(plan.header_names, ["authorization", "x-api-key"]);
        assert_eq!(plan.env_keys, ["MCP_URL", "TOKEN"]);

        let dir = tempfile::tempdir().unwrap();
        let store = dir.path().join("approved-imports.toml");
        remember(&store, &plan).unwrap();
        let written = std::fs::read_to_string(store).unwrap();
        assert!(written.contains("https://example.com"));
        assert!(written.contains("authorization"));
        assert!(written.contains("TOKEN"));
        for secret in ["user", "password", "private", "literal-secret"] {
            assert!(!written.contains(secret), "approval leaked {secret:?}");
        }
    }

    #[test]
    fn an_http_origin_or_forwarded_set_change_requires_approval() {
        let dir = tempfile::tempdir().unwrap();
        let store = dir.path().join("approved-imports.toml");
        let approved = http_plan("https://api.example/mcp", &["Authorization"], &["TOKEN"]);
        remember(&store, &approved).unwrap();
        assert!(is_approved(
            Some(&store),
            &http_plan(
                "https://api.example/another-path",
                &["authorization"],
                &["TOKEN"]
            )
        ));
        assert!(!is_approved(
            Some(&store),
            &http_plan("https://other.example/mcp", &["Authorization"], &["TOKEN"])
        ));
        assert!(!is_approved(
            Some(&store),
            &http_plan(
                "https://api.example/mcp",
                &["Authorization", "X-Api-Key"],
                &["TOKEN"]
            )
        ));
        assert!(!is_approved(
            Some(&store),
            &http_plan(
                "https://api.example/mcp",
                &["Authorization"],
                &["TOKEN", "SECOND_TOKEN"]
            )
        ));
    }

    #[test]
    fn existing_stdio_approval_records_remain_readable() {
        let old = r#"
            [[approved]]
            source = "/repo/.mcp.json"
            entry = "local"
            command = ["server", "--stdio"]
            cwd = "/repo"
            env_keys = ["API_TOKEN", "REGION"]
        "#;
        let approvals: Approvals = toml::from_str(old).unwrap();
        assert_eq!(approvals.entries, vec![plan(&["server", "--stdio"])]);
    }
}