agent-config 0.3.3

Install hooks/integrations into AI coding harnesses (Claude Code, Cursor, Gemini CLI, OpenCode, Codex CLI, Cline, Windsurf, ...) without learning each one's filesystem layout.
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
//! Shared MCP installer for harnesses that key servers by name in a JSON
//! object (Claude, Cursor, Gemini, Cline, Roo, Windsurf, Antigravity).
//!
//! Each harness has its own config-file path, but the on-disk shape is
//! identical:
//!
//! ```json
//! { "mcpServers": { "<name>": { "command": "...", "args": [...], "env": {} } } }
//! ```
//!
//! For HTTP/SSE transports the shape is `{ "url": "...", "headers": {...} }`
//! (with no `command`/`args`).
//!
//! Ownership is tracked in a sidecar `.agent-config-mcp.json` next to the config
//! file so the harness config never carries unknown keys.

use std::path::Path;

use crate::error::AgentConfigError;
use crate::integration::{InstallReport, UninstallReport};
use crate::plan::PlannedChange;
use crate::spec::McpSpec;
use crate::status::ConfigPresence;
use crate::util::{mcp_json_map, ownership};

/// Top-level key under which named server entries live for this helper.
const SERVERS_KEY: &str = "mcpServers";
const SERVERS_PATH: &[&str] = &[SERVERS_KEY];

/// Returns true if `name` exists in the ledger sidecar (single source of truth
/// for "is this currently installed by some consumer").
#[allow(dead_code)]
pub(crate) fn is_installed(ledger_path: &Path, name: &str) -> Result<bool, AgentConfigError> {
    ownership::contains(ledger_path, name)
}

/// Probe whether `name` is present in the standard `mcpServers` object.
pub(crate) fn config_presence(
    config_path: &Path,
    name: &str,
) -> Result<ConfigPresence, AgentConfigError> {
    mcp_json_map::config_presence(
        config_path,
        SERVERS_PATH,
        name,
        mcp_json_map::ConfigFormat::Json,
    )
}

/// Install or update an MCP server in the harness config. Records ownership
/// in the sidecar ledger.
pub(crate) fn install(
    config_path: &Path,
    ledger_path: &Path,
    spec: &McpSpec,
) -> Result<InstallReport, AgentConfigError> {
    mcp_json_map::install(
        config_path,
        ledger_path,
        spec,
        SERVERS_PATH,
        mcp_json_map::mcp_servers_value,
        mcp_json_map::ConfigFormat::Json,
    )
}

/// Plan installing or updating an MCP server in the harness config.
pub(crate) fn plan_install(
    config_path: &Path,
    ledger_path: &Path,
    spec: &McpSpec,
) -> Result<Vec<PlannedChange>, AgentConfigError> {
    mcp_json_map::plan_install(
        config_path,
        ledger_path,
        spec,
        SERVERS_PATH,
        mcp_json_map::mcp_servers_value,
        mcp_json_map::ConfigFormat::Json,
    )
}

/// Uninstall the server identified by `name`. Refuses if the recorded owner
/// differs from `owner_tag`, or if `name` is present in the harness config but
/// missing from the ledger (i.e. user installed by hand).
pub(crate) fn uninstall(
    config_path: &Path,
    ledger_path: &Path,
    name: &str,
    owner_tag: &str,
    kind: &'static str,
) -> Result<UninstallReport, AgentConfigError> {
    mcp_json_map::uninstall(
        config_path,
        ledger_path,
        name,
        owner_tag,
        kind,
        SERVERS_PATH,
        mcp_json_map::ConfigFormat::Json,
    )
}

/// Plan uninstalling the server identified by `name`.
pub(crate) fn plan_uninstall(
    config_path: &Path,
    ledger_path: &Path,
    name: &str,
    owner_tag: &str,
    kind: &'static str,
) -> Result<Vec<PlannedChange>, AgentConfigError> {
    mcp_json_map::plan_uninstall(
        config_path,
        ledger_path,
        name,
        owner_tag,
        kind,
        SERVERS_PATH,
        mcp_json_map::ConfigFormat::Json,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::spec::McpTransport;
    use serde_json::{json, Value};
    use std::collections::BTreeMap;
    use std::sync::{Arc, Barrier};
    use std::thread;
    use tempfile::tempdir;

    fn run_two<A, B, FA, FB>(a: FA, b: FB) -> (A, B)
    where
        A: Send + 'static,
        B: Send + 'static,
        FA: FnOnce() -> A + Send + 'static,
        FB: FnOnce() -> B + Send + 'static,
    {
        let barrier = Arc::new(Barrier::new(3));
        let a_barrier = Arc::clone(&barrier);
        let b_barrier = Arc::clone(&barrier);
        let a_thread = thread::spawn(move || {
            a_barrier.wait();
            a()
        });
        let b_thread = thread::spawn(move || {
            b_barrier.wait();
            b()
        });
        barrier.wait();
        (
            a_thread.join().expect("first MCP writer panicked"),
            b_thread.join().expect("second MCP writer panicked"),
        )
    }

    fn paths(dir: &Path) -> (std::path::PathBuf, std::path::PathBuf) {
        (dir.join("mcp.json"), dir.join(".agent-config-mcp.json"))
    }

    fn stdio_spec(name: &str, owner: &str) -> McpSpec {
        McpSpec::builder(name)
            .owner(owner)
            .stdio("npx", ["-y", "@example/server"])
            .env("FOO", "bar")
            .build()
    }

    fn http_spec(name: &str, owner: &str) -> McpSpec {
        let mut headers = BTreeMap::new();
        headers.insert("Authorization".into(), "Bearer xyz".into());
        McpSpec {
            name: name.into(),
            owner_tag: owner.into(),
            transport: McpTransport::Http {
                url: "https://example.com/mcp".into(),
                headers,
            },
            friendly_name: None,
            secret_policy: crate::spec::SecretPolicy::RefuseInlineSecretsInLocalScope,
            adopt_unowned: false,
        }
    }

    #[test]
    fn install_creates_config_and_ledger() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        let report = install(&cfg, &led, &stdio_spec("github", "myapp")).unwrap();
        assert!(report.created.contains(&cfg));
        assert!(led.exists(), "ledger created");
        let v: Value = serde_json::from_slice(&std::fs::read(&cfg).unwrap()).unwrap();
        assert_eq!(v["mcpServers"]["github"]["command"], json!("npx"));
        assert_eq!(v["mcpServers"]["github"]["env"]["FOO"], json!("bar"));
    }

    #[test]
    fn install_idempotent_with_same_owner() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        let spec = stdio_spec("github", "myapp");
        install(&cfg, &led, &spec).unwrap();
        let r2 = install(&cfg, &led, &spec).unwrap();
        assert!(r2.already_installed);
    }

    #[test]
    fn install_refuses_other_owner() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        install(&cfg, &led, &stdio_spec("github", "appA")).unwrap();
        let err = install(&cfg, &led, &stdio_spec("github", "appB")).unwrap_err();
        assert!(matches!(err, AgentConfigError::NotOwnedByCaller { .. }));
        assert_eq!(
            ownership::owner_of(&led, "github").unwrap().as_deref(),
            Some("appA")
        );
    }

    #[test]
    fn install_refuses_user_installed_same_name() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        std::fs::write(
            &cfg,
            r#"{ "mcpServers": { "github": { "command": "user-cmd" } } }"#,
        )
        .unwrap();
        let err = install(&cfg, &led, &stdio_spec("github", "myapp")).unwrap_err();
        assert!(matches!(
            err,
            AgentConfigError::NotOwnedByCaller { actual: None, .. }
        ));
        let v: Value = serde_json::from_slice(&std::fs::read(&cfg).unwrap()).unwrap();
        assert_eq!(v["mcpServers"]["github"]["command"], json!("user-cmd"));
    }

    #[test]
    fn install_http_writes_url_and_headers() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        install(&cfg, &led, &http_spec("remote", "myapp")).unwrap();
        let v: Value = serde_json::from_slice(&std::fs::read(&cfg).unwrap()).unwrap();
        assert_eq!(
            v["mcpServers"]["remote"]["url"],
            json!("https://example.com/mcp")
        );
        assert_eq!(v["mcpServers"]["remote"]["type"], json!("http"));
        assert_eq!(
            v["mcpServers"]["remote"]["headers"]["Authorization"],
            json!("Bearer xyz")
        );
    }

    #[test]
    fn install_coexists_with_existing_user_servers() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        std::fs::write(
            &cfg,
            r#"{ "mcpServers": { "user-thing": { "command": "user-cmd" } } }"#,
        )
        .unwrap();
        install(&cfg, &led, &stdio_spec("github", "myapp")).unwrap();
        let v: Value = serde_json::from_slice(&std::fs::read(&cfg).unwrap()).unwrap();
        assert_eq!(v["mcpServers"]["user-thing"]["command"], json!("user-cmd"));
        assert_eq!(v["mcpServers"]["github"]["command"], json!("npx"));
        // backup made for the modified pre-existing file
        assert!(dir.path().join("mcp.json.bak").exists());
    }

    #[test]
    fn uninstall_removes_owned_entry_only() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        install(&cfg, &led, &stdio_spec("github", "myapp")).unwrap();
        let r = uninstall(&cfg, &led, "github", "myapp", "mcp server").unwrap();
        assert!(r.removed.contains(&cfg) || r.restored.contains(&cfg));
        assert!(!led.exists(), "empty ledger removed");
        assert!(ownership::owner_of(&led, "github").unwrap().is_none());
    }

    #[test]
    fn uninstall_refuses_other_owner() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        install(&cfg, &led, &stdio_spec("github", "appA")).unwrap();
        let err = uninstall(&cfg, &led, "github", "appB", "mcp server").unwrap_err();
        assert!(matches!(
            err,
            AgentConfigError::NotOwnedByCaller {
                actual: Some(_),
                ..
            }
        ));
        // Config and ledger untouched.
        let v: Value = serde_json::from_slice(&std::fs::read(&cfg).unwrap()).unwrap();
        assert!(v["mcpServers"]["github"].is_object());
        assert!(led.exists());
    }

    #[test]
    fn uninstall_refuses_user_installed_entry() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        std::fs::write(
            &cfg,
            r#"{ "mcpServers": { "user-thing": { "command": "user-cmd" } } }"#,
        )
        .unwrap();
        let err = uninstall(&cfg, &led, "user-thing", "myapp", "mcp server").unwrap_err();
        assert!(matches!(
            err,
            AgentConfigError::NotOwnedByCaller { actual: None, .. }
        ));
    }

    #[test]
    fn uninstall_unknown_entry_is_noop() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        let r = uninstall(&cfg, &led, "ghost", "myapp", "mcp server").unwrap();
        assert!(r.not_installed);
    }

    #[test]
    fn uninstall_keeps_other_servers_intact() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        install(&cfg, &led, &stdio_spec("alpha", "myapp")).unwrap();
        install(&cfg, &led, &stdio_spec("beta", "myapp")).unwrap();
        uninstall(&cfg, &led, "alpha", "myapp", "mcp server").unwrap();
        let v: Value = serde_json::from_slice(&std::fs::read(&cfg).unwrap()).unwrap();
        assert!(v["mcpServers"]["beta"].is_object());
        assert!(v["mcpServers"]["alpha"].is_null());
    }

    #[test]
    fn uninstall_final_entry_does_not_restore_stale_backup() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        std::fs::write(
            &cfg,
            r#"{ "mcpServers": { "user-thing": { "command": "user-cmd" } } }"#,
        )
        .unwrap();

        install(&cfg, &led, &stdio_spec("github", "myapp")).unwrap();

        let mut current: Value = serde_json::from_slice(&std::fs::read(&cfg).unwrap()).unwrap();
        current["mcpServers"]
            .as_object_mut()
            .unwrap()
            .remove("user-thing");
        std::fs::write(&cfg, crate::util::json_patch::to_pretty(&current)).unwrap();

        let report = uninstall(&cfg, &led, "github", "myapp", "mcp server").unwrap();
        assert!(report.removed.contains(&cfg));
        assert!(!cfg.exists(), "stale backup should not be restored");
        assert!(
            dir.path().join("mcp.json.bak").exists(),
            "stale backup is left for manual recovery"
        );
    }

    #[test]
    fn is_installed_uses_ledger() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        assert!(!is_installed(&led, "github").unwrap());
        install(&cfg, &led, &stdio_spec("github", "myapp")).unwrap();
        assert!(is_installed(&led, "github").unwrap());
    }

    #[test]
    fn concurrent_install_different_names_keeps_both_config_and_ledger_entries() {
        let dir = tempdir().unwrap();
        let (cfg, led) = paths(dir.path());
        let cfg_a = cfg.clone();
        let led_a = led.clone();
        let cfg_b = cfg.clone();
        let led_b = led.clone();
        let spec_a = stdio_spec("alpha", "appA");
        let spec_b = stdio_spec("beta", "appB");

        let (ra, rb) = run_two(
            move || install(&cfg_a, &led_a, &spec_a),
            move || install(&cfg_b, &led_b, &spec_b),
        );

        ra.unwrap();
        rb.unwrap();
        let v: Value = serde_json::from_slice(&std::fs::read(&cfg).unwrap()).unwrap();
        assert_eq!(v["mcpServers"]["alpha"]["command"], json!("npx"));
        assert_eq!(v["mcpServers"]["beta"]["command"], json!("npx"));
        assert_eq!(
            ownership::owner_of(&led, "alpha").unwrap().as_deref(),
            Some("appA")
        );
        assert_eq!(
            ownership::owner_of(&led, "beta").unwrap().as_deref(),
            Some("appB")
        );
    }
}