atman-runtime 1.5.0

atman flow execution runtime: evaluator, tool dispatch, provider dispatch, executor, memory stores
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
//! MCP server config file management — single source of truth.
//!
//! Reads and writes `mcp_servers.json` and `config.toml [[mcp]]` blocks.
//! All operations go through [`McpServerConfig`]:
//!
//! - [`load`] → `Vec<McpServerConfig>` (from JSON + TOML + Claude Desktop)
//! - [`save`] → writes to `mcp_servers.json`
//! - [`toggle_disabled`] = load → toggle → save
//! - [`remove`] = load → filter → save

use std::path::{Path, PathBuf};

use crate::mcp::{McpServerConfig, TransportKind};
use crate::storage;
use crate::tool::Tier;

pub fn json_path() -> Result<PathBuf, String> {
    let dir = storage::config_dir().map_err(|e| e.to_string())?;
    Ok(dir.join("mcp_servers.json"))
}

pub fn json_path_in(config_dir: &Path) -> PathBuf {
    config_dir.join("mcp_servers.json")
}

fn toml_path_in(config_dir: &Path) -> PathBuf {
    config_dir.join("config.toml")
}

/// Load all MCP server configs from all sources: `config.toml` [[mcp]]
/// blocks, `mcp_servers.json`, and Claude Desktop's config (read-only).
/// Later entries override earlier ones by name.
pub fn load(config_dir: Option<&Path>) -> Vec<McpServerConfig> {
    let mut configs = load_in(config_dir.unwrap_or(Path::new("")));

    // Claude Desktop auto-discover (read-only, macOS only)
    if let Ok(home) = std::env::var("HOME") {
        let claude_path = PathBuf::from(home)
            .join("Library/Application Support/Claude/claude_desktop_config.json");
        if claude_path.exists() {
            if let Ok(text) = std::fs::read_to_string(&claude_path) {
                configs.extend(parse_mcp_json(&text));
            }
        }
    }

    dedup_keep_last(configs)
}

/// Load from a specific config directory only (no Claude Desktop discovery).
pub fn load_in(config_dir: &Path) -> Vec<McpServerConfig> {
    let mut configs = Vec::new();

    let toml_path = toml_path_in(config_dir);
    if toml_path.exists() {
        if let Ok(text) = std::fs::read_to_string(&toml_path) {
            configs.extend(parse_mcp_toml(&text));
        }
    }

    let json_path = json_path_in(config_dir);
    if json_path.exists() {
        if let Ok(text) = std::fs::read_to_string(&json_path) {
            configs.extend(parse_mcp_json(&text));
        }
    }

    dedup_keep_last(configs)
}

/// Deduplicate by name, keeping the LAST occurrence (later sources override).
fn dedup_keep_last(configs: Vec<McpServerConfig>) -> Vec<McpServerConfig> {
    let mut seen = std::collections::HashSet::new();
    let mut result: Vec<McpServerConfig> = Vec::with_capacity(configs.len());
    for cfg in configs.into_iter().rev() {
        if seen.insert(cfg.name.clone()) {
            result.push(cfg);
        }
    }
    result.reverse();
    result
}

/// Parse standard `mcpServers` JSON format.
pub fn parse_mcp_json(text: &str) -> Vec<McpServerConfig> {
    #[derive(serde::Deserialize)]
    struct JsonConfigFile {
        #[serde(default, rename = "mcpServers")]
        mcp_servers: std::collections::HashMap<String, JsonServerConfig>,
    }

    #[derive(serde::Deserialize)]
    struct JsonServerConfig {
        #[serde(default)]
        r#type: Option<String>,
        #[serde(default)]
        command: Option<String>,
        #[serde(default)]
        args: Vec<String>,
        #[serde(default)]
        env: std::collections::HashMap<String, String>,
        #[serde(default)]
        url: Option<String>,
        #[serde(default, rename = "authToken")]
        auth_token: Option<String>,
        #[serde(default)]
        headers: std::collections::HashMap<String, String>,
        #[serde(default)]
        disabled: bool,
        #[serde(default)]
        tier: Option<String>,
        #[serde(default)]
        timeout_ms: Option<u64>,
    }

    let file: JsonConfigFile = match serde_json::from_str(text) {
        Ok(f) => f,
        Err(_) => return Vec::new(),
    };
    file.mcp_servers
        .into_iter()
        .map(|(name, raw)| {
            let (transport, command, url) = match raw.r#type.as_deref() {
                Some("sse") => (TransportKind::Sse, String::new(), raw.url),
                Some("http") => (TransportKind::Http, String::new(), raw.url),
                _ => (TransportKind::Stdio, raw.command.unwrap_or_default(), None),
            };
            McpServerConfig {
                name,
                transport,
                command,
                args: raw.args,
                env: raw.env.into_iter().collect(),
                url,
                auth_token: raw.auth_token,
                headers: raw.headers.into_iter().collect(),
                tier: parse_tier_str(raw.tier.as_deref()),
                timeout_ms: raw.timeout_ms.unwrap_or(30_000),
                disabled: raw.disabled,
            }
        })
        .collect()
}

/// Parse `config.toml` `[[mcp]]` blocks.
pub fn parse_mcp_toml(text: &str) -> Vec<McpServerConfig> {
    #[derive(Debug, serde::Deserialize)]
    struct RawMcpConfigFile {
        #[serde(default)]
        mcp: Vec<RawMcpConfig>,
    }

    #[derive(Debug, serde::Deserialize)]
    struct RawMcpConfig {
        name: String,
        #[serde(default)]
        transport: Option<String>,
        #[serde(default)]
        command: Option<String>,
        #[serde(default)]
        args: Vec<String>,
        #[serde(default)]
        env: std::collections::HashMap<String, String>,
        #[serde(default)]
        url: Option<String>,
        #[serde(default)]
        auth_token: Option<String>,
        #[serde(default)]
        headers: std::collections::HashMap<String, String>,
        #[serde(default)]
        tier: Option<u8>,
        #[serde(default)]
        timeout_ms: Option<u64>,
        #[serde(default)]
        disabled: bool,
    }

    let file: RawMcpConfigFile = match toml::from_str(text) {
        Ok(f) => f,
        Err(_) => return Vec::new(),
    };
    file.mcp
        .into_iter()
        .map(|raw| {
            let transport = match raw.transport.as_deref() {
                Some("http") => TransportKind::Http,
                Some("sse") => TransportKind::Sse,
                _ => TransportKind::Stdio,
            };
            McpServerConfig {
                name: raw.name,
                transport,
                command: raw.command.unwrap_or_default(),
                args: raw.args,
                env: raw.env.into_iter().collect(),
                url: raw.url,
                auth_token: raw.auth_token,
                headers: raw.headers.into_iter().collect(),
                tier: tier_from_int(raw.tier.unwrap_or(3)),
                timeout_ms: raw.timeout_ms.unwrap_or(30_000),
                disabled: raw.disabled,
            }
        })
        .collect()
}

fn parse_tier_str(s: Option<&str>) -> Tier {
    match s {
        Some("Zero") | Some("zero") | Some("0") => Tier::Zero,
        Some("One") | Some("one") | Some("1") => Tier::One,
        Some("Two") | Some("two") | Some("2") => Tier::Two,
        Some("Three") | Some("three") | Some("3") => Tier::Three,
        Some("Four") | Some("four") | Some("4") => Tier::Four,
        _ => Tier::Three,
    }
}

fn tier_from_int(n: u8) -> Tier {
    match n {
        0 => Tier::Zero,
        1 => Tier::One,
        2 => Tier::Two,
        3 => Tier::Three,
        _ => Tier::Four,
    }
}

fn tier_to_str(t: Tier) -> &'static str {
    match t {
        Tier::Zero => "Zero",
        Tier::One => "One",
        Tier::Two => "Two",
        Tier::Three => "Three",
        Tier::Four => "Four",
    }
}

/// Save configs to `mcp_servers.json`. Overwrites the file entirely.
pub fn save(configs: &[McpServerConfig]) -> Result<(), String> {
    let dir = storage::config_dir().map_err(|e| e.to_string())?;
    save_in(&dir, configs)
}

/// Save configs to `mcp_servers.json` in an explicit config directory.
pub fn save_in(config_dir: &Path, configs: &[McpServerConfig]) -> Result<(), String> {
    let path = json_path_in(config_dir);
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
    }
    let root = configs_to_json(configs);
    let json = serde_json::to_string_pretty(&root).map_err(|e| e.to_string())?;
    std::fs::write(&path, json + "\n").map_err(|e| e.to_string())?;
    Ok(())
}

fn configs_to_json(configs: &[McpServerConfig]) -> serde_json::Value {
    let mut servers = serde_json::Map::new();
    for cfg in configs {
        servers.insert(cfg.name.clone(), config_to_json_value(cfg));
    }
    serde_json::json!({ "mcpServers": serde_json::Value::Object(servers) })
}

fn config_to_json_value(cfg: &McpServerConfig) -> serde_json::Value {
    let mut obj = serde_json::Map::new();
    match cfg.transport {
        TransportKind::Sse => {
            obj.insert("type".into(), "sse".into());
        }
        TransportKind::Http => {
            obj.insert("type".into(), "http".into());
        }
        TransportKind::Stdio => {}
    }
    if !cfg.command.is_empty() {
        obj.insert("command".into(), cfg.command.clone().into());
    }
    if !cfg.args.is_empty() {
        obj.insert(
            "args".into(),
            serde_json::Value::Array(cfg.args.iter().map(|a| a.clone().into()).collect()),
        );
    }
    if !cfg.env.is_empty() {
        obj.insert(
            "env".into(),
            serde_json::Value::Object(
                cfg.env
                    .iter()
                    .map(|(k, v)| (k.clone(), v.clone().into()))
                    .collect(),
            ),
        );
    }
    if let Some(url) = &cfg.url {
        obj.insert("url".into(), url.clone().into());
    }
    if let Some(token) = &cfg.auth_token {
        obj.insert("authToken".into(), token.clone().into());
    }
    if !cfg.headers.is_empty() {
        obj.insert(
            "headers".into(),
            serde_json::Value::Object(
                cfg.headers
                    .iter()
                    .map(|(k, v)| (k.clone(), v.clone().into()))
                    .collect(),
            ),
        );
    }
    obj.insert("tier".into(), tier_to_str(cfg.tier).into());
    obj.insert("timeout_ms".into(), cfg.timeout_ms.into());
    if cfg.disabled {
        obj.insert("disabled".into(), true.into());
    }
    serde_json::Value::Object(obj)
}

/// Toggle the `disabled` flag on a server. Returns the new disabled value.
pub fn toggle_disabled(name: &str) -> Result<bool, String> {
    let dir = storage::config_dir().map_err(|e| e.to_string())?;
    toggle_disabled_in(&dir, name)
}

/// Toggle `disabled` in an explicit config directory.
pub fn toggle_disabled_in(config_dir: &Path, name: &str) -> Result<bool, String> {
    let mut configs = load_in(config_dir);
    let cfg = configs
        .iter_mut()
        .find(|c| c.name == name)
        .ok_or_else(|| format!("MCP server \"{name}\" not found"))?;
    cfg.disabled = !cfg.disabled;
    let new_val = cfg.disabled;
    save_in(config_dir, &configs)?;
    Ok(new_val)
}

/// Remove a server from the config.
pub fn remove(name: &str) -> Result<(), String> {
    let dir = storage::config_dir().map_err(|e| e.to_string())?;
    remove_in(&dir, name)
}

/// Remove a server from an explicit config directory.
pub fn remove_in(config_dir: &Path, name: &str) -> Result<(), String> {
    let mut configs = load_in(config_dir);
    let before = configs.len();
    configs.retain(|c| c.name != name);
    if configs.len() == before {
        return Err(format!("MCP server \"{name}\" not found"));
    }
    save_in(config_dir, &configs)
}

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

    #[test]
    fn load_empty_when_no_files() {
        let dir = tempfile::tempdir().unwrap();
        let configs = load_in(dir.path());
        assert!(configs.is_empty());
    }

    #[test]
    fn save_then_load_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        let original = vec![
            McpServerConfig::stdio("srv-a", "echo", vec!["hello".into()], Tier::Two, 30_000),
            McpServerConfig::http(
                "srv-b",
                "https://api.example.com",
                Some("secret".into()),
                Tier::Three,
                30_000,
            ),
        ];
        save_in(dir.path(), &original).unwrap();
        let loaded = load_in(dir.path());
        assert_eq!(loaded.len(), 2);
        let a = loaded.iter().find(|c| c.name == "srv-a").unwrap();
        assert_eq!(a.command, "echo");
        assert_eq!(a.transport, TransportKind::Stdio);
        let b = loaded.iter().find(|c| c.name == "srv-b").unwrap();
        assert_eq!(b.transport, TransportKind::Http);
        assert_eq!(b.url.as_deref(), Some("https://api.example.com"));
    }

    #[test]
    fn toggle_disabled_load_modify_save() {
        let dir = tempfile::tempdir().unwrap();
        save_in(
            dir.path(),
            &[McpServerConfig::stdio(
                "srv",
                "echo",
                vec![],
                Tier::Two,
                30_000,
            )],
        )
        .unwrap();

        assert!(toggle_disabled_in(dir.path(), "srv").unwrap());
        let loaded = load_in(dir.path());
        assert!(loaded[0].disabled);

        assert!(!toggle_disabled_in(dir.path(), "srv").unwrap());
        let loaded = load_in(dir.path());
        assert!(!loaded[0].disabled);
    }

    #[test]
    fn toggle_disabled_on_toml_config() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            toml_path_in(dir.path()),
            "[[mcp]]\nname = \"exa\"\ncommand = \"exa-mcp-server\"\ntimeout_ms = 30000\n",
        )
        .unwrap();

        assert!(toggle_disabled_in(dir.path(), "exa").unwrap());
        let loaded = load_in(dir.path());
        assert_eq!(loaded.len(), 1);
        assert!(loaded[0].disabled);
        assert!(json_path_in(dir.path()).exists());
    }

    #[test]
    fn remove_server() {
        let dir = tempfile::tempdir().unwrap();
        save_in(
            dir.path(),
            &[
                McpServerConfig::stdio("a", "echo", vec![], Tier::Two, 30_000),
                McpServerConfig::stdio("b", "ls", vec![], Tier::Two, 30_000),
            ],
        )
        .unwrap();

        remove_in(dir.path(), "a").unwrap();
        let loaded = load_in(dir.path());
        assert_eq!(loaded.len(), 1);
        assert_eq!(loaded[0].name, "b");
    }

    #[test]
    fn remove_missing_errors() {
        let dir = tempfile::tempdir().unwrap();
        save_in(
            dir.path(),
            &[McpServerConfig::stdio(
                "a",
                "echo",
                vec![],
                Tier::Two,
                30_000,
            )],
        )
        .unwrap();
        assert!(remove_in(dir.path(), "nonexistent").is_err());
    }

    #[test]
    fn toggle_missing_errors() {
        let dir = tempfile::tempdir().unwrap();
        save_in(
            dir.path(),
            &[McpServerConfig::stdio(
                "a",
                "echo",
                vec![],
                Tier::Two,
                30_000,
            )],
        )
        .unwrap();
        assert!(toggle_disabled_in(dir.path(), "nonexistent").is_err());
    }

    #[test]
    fn dedup_keeps_last() {
        let configs = vec![
            McpServerConfig::stdio("a", "from-toml", vec![], Tier::Two, 30_000),
            McpServerConfig::stdio("a", "from-json", vec![], Tier::Two, 30_000),
        ];
        let deduped = dedup_keep_last(configs);
        assert_eq!(deduped.len(), 1);
        assert_eq!(deduped[0].command, "from-json");
    }
}