brontes 0.2.0

Transform any clap CLI into an MCP server.
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
//! Integration tests for `mcp zed {enable, disable, list}` plus the
//! Zed-specific JSON shape (`ZedConfig` / `ZedServer`), the JSONC
//! preprocessing hook, and the unrelated-top-level-keys preservation
//! invariant that distinguishes Zed from Claude / Cursor / `VSCode`.
//!
//! Every test that writes to the filesystem isolates inside a
//! `tempfile::TempDir` — no test touches the real `$HOME`. The CLI-driven
//! tests construct the brontes `mcp` subtree, parse a synthetic argv, and
//! dispatch via [`brontes::handle`] so the real production code path is
//! exercised end-to-end.

use std::path::PathBuf;

use clap::Command;
use serde_json::Value;
use tempfile::TempDir;

fn build_cli() -> Command {
    Command::new("my-cli")
        .version("0.1.0")
        .subcommand(brontes::command(None))
}

fn dispatch(argv: &[&str]) -> brontes::Result<()> {
    let cli = build_cli();
    let mut full: Vec<&str> = vec!["my-cli"];
    full.extend_from_slice(argv);
    let matches = cli.clone().get_matches_from(full);
    let Some(("mcp", sub)) = matches.subcommand() else {
        panic!(
            "expected `mcp` subcommand match, got {:?}",
            matches.subcommand_name()
        );
    };
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .expect("runtime");
    rt.block_on(brontes::handle(sub, &cli, None))
}

fn read_json(path: &PathBuf) -> Value {
    let bytes = std::fs::read(path).unwrap_or_else(|e| panic!("read {}: {e}", path.display()));
    serde_json::from_slice(&bytes).unwrap_or_else(|e| panic!("parse {}: {e}", path.display()))
}

// ── enable / disable / list end-to-end ─────────────────────────────────────

#[test]
fn enable_writes_config_with_context_servers_shape() {
    // First-write path: no settings.json exists yet. After enable, the file
    // must materialize with a single `context_servers` block carrying the
    // brontes-managed server entry.
    let dir = TempDir::new().expect("tempdir");
    let cfg_path = dir.path().join("settings.json");

    dispatch(&[
        "mcp",
        "zed",
        "enable",
        "--config-path",
        cfg_path.to_str().expect("utf8 path"),
        "--server-name",
        "test-cli",
    ])
    .expect("enable succeeds");

    let doc = read_json(&cfg_path);
    let server = &doc["context_servers"]["test-cli"];
    assert!(server.is_object(), "test-cli must be present");
    // Local-stdio shape has NO `type` field (unlike VSCode/Cursor) — Zed
    // distinguishes local from remote by presence of `command` vs `url`.
    assert!(server.get("type").is_none(), "zed must not write `type`");
    assert!(server["command"].is_string());
    let args: Vec<&str> = server["args"]
        .as_array()
        .expect("args array")
        .iter()
        .map(|v| v.as_str().expect("string arg"))
        .collect();
    assert_eq!(args, ["mcp", "start"]);
}

#[test]
fn enable_then_disable_removes_only_the_named_server() {
    // Two-entry add-then-disable: the disable must leave the other entry
    // and the `context_servers` map intact.
    let dir = TempDir::new().expect("tempdir");
    let cfg_path = dir.path().join("settings.json");

    dispatch(&[
        "mcp",
        "zed",
        "enable",
        "--config-path",
        cfg_path.to_str().unwrap(),
        "--server-name",
        "alpha",
    ])
    .expect("enable alpha");
    dispatch(&[
        "mcp",
        "zed",
        "enable",
        "--config-path",
        cfg_path.to_str().unwrap(),
        "--server-name",
        "beta",
    ])
    .expect("enable beta");

    dispatch(&[
        "mcp",
        "zed",
        "disable",
        "--config-path",
        cfg_path.to_str().unwrap(),
        "--server-name",
        "alpha",
    ])
    .expect("disable alpha");

    let doc = read_json(&cfg_path);
    assert!(
        doc["context_servers"]["alpha"].is_null(),
        "alpha must be removed; got {doc}"
    );
    assert!(
        doc["context_servers"]["beta"].is_object(),
        "beta must remain; got {doc}"
    );
}

#[test]
fn enable_preserves_unrelated_top_level_keys() {
    // The defining Zed invariant: settings.json carries theme/font/keymap
    // alongside context_servers. enable must NOT wipe them.
    let dir = TempDir::new().expect("tempdir");
    let cfg_path = dir.path().join("settings.json");
    std::fs::write(
        &cfg_path,
        r#"{
  "theme": "One Dark",
  "font_family": "JetBrains Mono",
  "tab_size": 2,
  "context_servers": {
    "prior": {"command": "/bin/x"}
  }
}"#,
    )
    .expect("seed");

    dispatch(&[
        "mcp",
        "zed",
        "enable",
        "--config-path",
        cfg_path.to_str().unwrap(),
        "--server-name",
        "new-server",
    ])
    .expect("enable new-server");

    let doc = read_json(&cfg_path);
    // brontes-mutated entry present.
    assert!(
        doc["context_servers"]["new-server"].is_object(),
        "new-server must be added"
    );
    // prior entry present.
    assert!(
        doc["context_servers"]["prior"].is_object(),
        "prior must survive enable"
    );
    // unrelated top-level keys present.
    assert_eq!(doc["theme"].as_str(), Some("One Dark"));
    assert_eq!(doc["font_family"].as_str(), Some("JetBrains Mono"));
    assert_eq!(doc["tab_size"].as_i64(), Some(2));
}

#[test]
fn enable_preserves_unrelated_keys_through_jsonc_input() {
    // A real Zed settings.json is JSONC — line comments and trailing
    // commas. The preprocess hook must allow it to load cleanly; the
    // unrelated keys (modulo the comments themselves) must survive a
    // round trip.
    let dir = TempDir::new().expect("tempdir");
    let cfg_path = dir.path().join("settings.json");
    std::fs::write(
        &cfg_path,
        r#"// Zed user settings
{
    // visual choices
    "theme": "Solarized Light",
    "font_family": "Fira Code", // monospace family
    /* MCP block — managed by brontes */
    "context_servers": {
        "prior": {"command": "/bin/x",},
    },
}"#,
    )
    .expect("seed jsonc");

    dispatch(&[
        "mcp",
        "zed",
        "enable",
        "--config-path",
        cfg_path.to_str().unwrap(),
        "--server-name",
        "new-server",
    ])
    .expect("enable through jsonc");

    let doc = read_json(&cfg_path);
    assert!(
        doc["context_servers"]["new-server"].is_object(),
        "new server added through jsonc input"
    );
    assert!(
        doc["context_servers"]["prior"].is_object(),
        "prior entry survived through jsonc parsing"
    );
    assert_eq!(doc["theme"].as_str(), Some("Solarized Light"));
    assert_eq!(doc["font_family"].as_str(), Some("Fira Code"));
}

#[test]
fn enable_backup_before_in_place_mutation() {
    // First write does NOT produce a backup file; the subsequent write
    // (against an existing settings.json) MUST produce
    // `settings.backup.json` next to it before the new bytes are written.
    let dir = TempDir::new().expect("tempdir");
    let cfg_path = dir.path().join("settings.json");
    let backup = dir.path().join("settings.backup.json");

    dispatch(&[
        "mcp",
        "zed",
        "enable",
        "--config-path",
        cfg_path.to_str().unwrap(),
        "--server-name",
        "first",
    ])
    .expect("first enable");
    assert!(
        !backup.exists(),
        "first write must NOT create a .backup.json"
    );

    dispatch(&[
        "mcp",
        "zed",
        "enable",
        "--config-path",
        cfg_path.to_str().unwrap(),
        "--server-name",
        "second",
    ])
    .expect("second enable");
    assert!(
        backup.exists(),
        "second write MUST snapshot to .backup.json before overwriting"
    );
}

#[test]
fn disable_missing_server_is_no_op_not_error() {
    // ophis parity: disabling a server that is not in the map prints a
    // friendly note but exits 0 (no error). The file must NOT change.
    let dir = TempDir::new().expect("tempdir");
    let cfg_path = dir.path().join("settings.json");
    std::fs::write(
        &cfg_path,
        r#"{"theme":"Z","context_servers":{"a":{"command":"/x"}}}"#,
    )
    .expect("seed");
    let before = std::fs::read(&cfg_path).expect("read before");

    dispatch(&[
        "mcp",
        "zed",
        "disable",
        "--config-path",
        cfg_path.to_str().unwrap(),
        "--server-name",
        "does-not-exist",
    ])
    .expect("disable missing exits ok");

    let after = std::fs::read(&cfg_path).expect("read after");
    assert_eq!(before, after, "no-op disable must not rewrite the file");
}

#[test]
fn workspace_flag_resolves_to_dot_zed_under_cwd() {
    // --workspace must route through `$CWD/.zed/settings.json`, NOT the
    // per-OS user mode default. We assert by writing under a tempdir-as-
    // cwd and confirming the file lands there.
    let dir = TempDir::new().expect("tempdir");
    let prev_cwd = std::env::current_dir().expect("cwd");
    std::env::set_current_dir(dir.path()).expect("chdir");

    let result = dispatch(&[
        "mcp",
        "zed",
        "enable",
        "--workspace",
        "--server-name",
        "ws-server",
    ]);

    // Always restore cwd before asserting, even on panic.
    std::env::set_current_dir(prev_cwd).expect("restore cwd");
    result.expect("workspace enable succeeds");

    let workspace_path = dir.path().join(".zed").join("settings.json");
    assert!(
        workspace_path.exists(),
        "workspace settings.json must materialize at {}",
        workspace_path.display()
    );
    let doc = read_json(&workspace_path);
    assert!(
        doc["context_servers"]["ws-server"].is_object(),
        "ws-server present in workspace config"
    );
}

#[test]
fn list_prints_servers_one_per_line() {
    // `mcp zed list` must surface configured server names, one per line.
    // The test asserts on file state plus the trait surface (sorted-key
    // iteration) rather than capturing stdout (which is the same code
    // path Cursor / VSCode use).
    let dir = TempDir::new().expect("tempdir");
    let cfg_path = dir.path().join("settings.json");
    std::fs::write(
        &cfg_path,
        r#"{
            "theme": "Solarized",
            "context_servers": {
                "zebra": {"command": "/z"},
                "alpha": {"command": "/a"}
            }
        }"#,
    )
    .expect("seed");

    dispatch(&[
        "mcp",
        "zed",
        "list",
        "--config-path",
        cfg_path.to_str().unwrap(),
    ])
    .expect("list ok");

    // File state unchanged — list is read-only.
    let doc = read_json(&cfg_path);
    assert_eq!(doc["theme"].as_str(), Some("Solarized"));
    assert!(doc["context_servers"]["zebra"].is_object());
    assert!(doc["context_servers"]["alpha"].is_object());
}

#[test]
fn env_flag_merges_into_server_env() {
    // -e KEY=VAL is the same parsing layer Claude/Cursor/VSCode use, but
    // this test pins that Zed routes through it too — and that the
    // resulting `env` lands under context_servers.<name>.env on disk.
    let dir = TempDir::new().expect("tempdir");
    let cfg_path = dir.path().join("settings.json");

    dispatch(&[
        "mcp",
        "zed",
        "enable",
        "--config-path",
        cfg_path.to_str().unwrap(),
        "--server-name",
        "envtest",
        "-e",
        "FOO=bar",
        "-e",
        "BAZ=qux",
    ])
    .expect("enable with env");

    let doc = read_json(&cfg_path);
    let env = &doc["context_servers"]["envtest"]["env"];
    assert_eq!(env["FOO"].as_str(), Some("bar"));
    assert_eq!(env["BAZ"].as_str(), Some("qux"));
}

#[test]
fn enable_no_env_produces_no_env_key() {
    // Per the env-merge contract: when no `default_env` and no `--env`, the
    // resulting server entry MUST NOT carry an `env` JSON key.
    let dir = TempDir::new().expect("tempdir");
    let cfg_path = dir.path().join("settings.json");

    dispatch(&[
        "mcp",
        "zed",
        "enable",
        "--config-path",
        cfg_path.to_str().unwrap(),
        "--server-name",
        "no-env",
    ])
    .expect("enable no-env");

    let doc = read_json(&cfg_path);
    let server = &doc["context_servers"]["no-env"];
    assert!(
        server.get("env").is_none(),
        "empty env must collapse to no JSON key; got {server}"
    );
}

#[test]
fn enable_with_log_level_appends_flag_to_args() {
    // --log-level threads through into the spawned-server argv (Zed will
    // re-invoke the bin as `<bin> mcp start --log-level <lvl>`).
    let dir = TempDir::new().expect("tempdir");
    let cfg_path = dir.path().join("settings.json");

    dispatch(&[
        "mcp",
        "zed",
        "enable",
        "--config-path",
        cfg_path.to_str().unwrap(),
        "--server-name",
        "lvl",
        "--log-level",
        "debug",
    ])
    .expect("enable log-level");

    let doc = read_json(&cfg_path);
    let args: Vec<&str> = doc["context_servers"]["lvl"]["args"]
        .as_array()
        .expect("args")
        .iter()
        .map(|v| v.as_str().unwrap())
        .collect();
    assert_eq!(args, ["mcp", "start", "--log-level", "debug"]);
}

#[test]
fn invalid_env_pair_surfaces_as_config_error() {
    // The merge_env shared layer is also Zed's enable path; a missing `=`
    // must surface as `Error::Config`, NOT panic, NOT silently drop.
    let dir = TempDir::new().expect("tempdir");
    let cfg_path = dir.path().join("settings.json");

    let result = dispatch(&[
        "mcp",
        "zed",
        "enable",
        "--config-path",
        cfg_path.to_str().unwrap(),
        "--server-name",
        "bad",
        "-e",
        "MISSING_SEPARATOR",
    ]);
    let err = result.expect_err("must reject malformed --env");
    let msg = err.to_string();
    assert!(msg.contains("missing '='"), "got {msg}");
    // No file written when the env validation fails.
    assert!(!cfg_path.exists(), "no settings.json on validation failure");
}