oxi-agent 0.39.0

Agent runtime with tool-calling loop for AI coding assistants
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
//! MCP configuration loading.
//!
//! Discovers and loads MCP server configuration from standard locations:
//! - `~/.config/mcp/mcp.json` (shared global)
//! - `<config_dir>/oxi/mcp.json` (oxi-specific global)
//! - `<cwd>/.mcp.json` (shared project)
//! - `<cwd>/.oxi/mcp.json` (oxi-specific project)

use super::types::McpConfig;
use anyhow::{Context, Result};
use std::path::{Path, PathBuf};

/// Resolve all config file paths to try (in priority order).
pub fn config_paths(cwd: &Path) -> Vec<PathBuf> {
    let mut paths = Vec::new();

    // 1. Shared global config
    if let Some(config_dir) = dirs::config_dir() {
        paths.push(config_dir.join("mcp").join("mcp.json"));
    }

    // 2. oxi-specific global config
    if let Some(config_dir) = dirs::config_dir() {
        paths.push(config_dir.join("oxi").join("mcp.json"));
    }

    // 3. Shared project config
    paths.push(cwd.join(".mcp.json"));

    // 4. oxi-specific project config
    paths.push(cwd.join(".oxi").join("mcp.json"));

    paths
}

/// Load MCP configuration, merging all discovered config files.
///
/// Later files override earlier ones for server entries.
/// Returns an empty config if no files exist.
pub fn load_mcp_config() -> McpConfig {
    let cwd = match std::env::current_dir() {
        Ok(c) => c,
        Err(_) => return McpConfig::default(),
    };
    load_mcp_config_from(&cwd)
}

/// Load MCP configuration from a specific working directory.
///
/// oxi's own paths are merged first (later files override earlier).
/// Then, if `settings.discoverExternalConfigs` is enabled, third-party
/// tool configs ([`external_paths`]) are merged with **lower priority**:
/// they only add server entries oxi did not already define and never
/// override oxi's `settings`. v2.4 / G8.
pub fn load_mcp_config_from(cwd: &Path) -> McpConfig {
    let mut merged = McpConfig::default();

    for path in config_paths(cwd) {
        if let Some(config) = read_config_file(&path) {
            for (name, entry) in config.mcp_servers {
                merged.mcp_servers.insert(name, entry);
            }
            if config.settings.is_some() {
                merged.settings = config.settings;
            }
        }
    }

    // Opt-in third-party discovery. oxi's own entries + settings win.
    let discover = merged
        .settings
        .as_ref()
        .and_then(|s| s.discover_external_configs)
        .unwrap_or(false);
    if discover {
        for path in external_paths(cwd) {
            if let Some(config) = read_config_file(&path) {
                for (name, entry) in config.mcp_servers {
                    merged.mcp_servers.entry(name).or_insert(entry);
                }
            }
        }
    }

    merged
}

/// Third-party tool config files to optionally discover (v2.4 / G8).
/// Only files using the same `{"mcpServers": {...}}` schema as oxi are
/// supported here; VS Code's `{"servers": ...}` and opencode's
/// `{"mcp": ...}` schemas need normalization and are out of v2.4 scope.
fn external_paths(cwd: &Path) -> Vec<PathBuf> {
    vec![
        cwd.join(".claude").join("mcp.json"),
        cwd.join(".cursor").join("mcp.json"),
    ]
}

/// Read and parse a single config file. Returns `None` if the file
/// doesn't exist or is invalid.
pub fn read_config_file(path: &Path) -> Option<McpConfig> {
    if !path.exists() {
        return None;
    }

    let content = std::fs::read_to_string(path).ok()?;

    match serde_json::from_str::<McpConfig>(&content) {
        Ok(mut config) => {
            resolve_config(&mut config);
            Some(config)
        }
        Err(e) => {
            tracing::warn!("Failed to parse MCP config {}: {}", path.display(), e);
            None
        }
    }
}

/// The default **write target** for global (user-wide) MCP config.
///
/// This is the oxi-owned global file (`~/.config/oxi/mcp.json` on
/// Unix, the platform equivalent elsewhere). Returns `None` only when
/// the platform has no resolvable config directory.
pub fn default_write_path_global() -> Option<PathBuf> {
    dirs::config_dir().map(|d| d.join("oxi").join("mcp.json"))
}

/// The default **write target** for project-local MCP config:
/// `<cwd>/.oxi/mcp.json`.
pub fn default_write_path_project(cwd: &Path) -> PathBuf {
    cwd.join(".oxi").join("mcp.json")
}

/// Load a single config file, returning an empty config if it does
/// not exist (rather than `None`) — convenient for the TUI editor
/// which wants to edit "the file" whether or not it exists yet.
pub fn load_or_default(path: &Path) -> McpConfig {
    read_config_file(path).unwrap_or_default()
}

/// Resolve [`ServerEntry`] string fields against the process environment
/// and (for `!cmd` values) the host shell. Called by [`read_config_file`]
/// after parsing so that values stored in `mcp.json` are ready to use at
/// connect time without further substitution.
///
/// Rules (matching the OMP behaviour):
/// - Values starting with `!` run through a shell (`sh -c` on Unix,
///   `cmd /C` on Windows) with a 10s timeout. The trimmed stdout replaces
///   the value; failure or empty stdout yields `None`.
/// - Other values have `${VAR}` and `${VAR:-default}` placeholders
///   expanded against the process environment; unresolved placeholders
///   remain literal (so a misconfigured value surfaces rather than
///   silently disappearing).
pub fn resolve_config(cfg: &mut McpConfig) {
    for (name, entry) in cfg.mcp_servers.iter_mut() {
        if let Some(s) = entry.command.as_ref() {
            entry.command = match resolve_value(s) {
                Some(v) => Some(v),
                None => {
                    tracing::warn!("MCP server '{}': failed to resolve command", name);
                    None
                }
            };
        }
        if let Some(args) = entry.args.as_mut() {
            args.retain_mut(|a| match resolve_value(a) {
                Some(r) => {
                    *a = r;
                    true
                }
                None => false,
            });
        }
        if let Some(s) = entry.cwd.as_ref() {
            entry.cwd = match resolve_value(s) {
                Some(v) => Some(v),
                None => {
                    tracing::warn!("MCP server '{}': failed to resolve cwd", name);
                    None
                }
            };
        }
        if let Some(s) = entry.url.as_ref() {
            entry.url = match resolve_value(s) {
                Some(v) => Some(v),
                None => {
                    tracing::warn!("MCP server '{}': failed to resolve url", name);
                    None
                }
            };
        }
        if let Some(env) = entry.env.as_mut() {
            env.retain(|_, v| match resolve_value(v) {
                Some(r) => {
                    *v = r;
                    true
                }
                None => false,
            });
        }
        if let Some(headers) = entry.headers.as_mut() {
            headers.retain(|_, v| match resolve_value(v) {
                Some(r) => {
                    *v = r;
                    true
                }
                None => false,
            });
        }
    }
}

fn resolve_value(value: &str) -> Option<String> {
    if let Some(cmd) = value.strip_prefix('!') {
        run_shell_capture(cmd, std::time::Duration::from_secs(10))
    } else {
        Some(expand_env_placeholders(value))
    }
}

fn run_shell_capture(cmd: &str, timeout: std::time::Duration) -> Option<String> {
    let (tx, rx) = std::sync::mpsc::channel();
    let thread_cmd = cmd.to_string();
    std::thread::spawn(move || {
        let result = shell_command_output(&thread_cmd);
        let _ = tx.send(result);
    });
    match rx.recv_timeout(timeout) {
        Ok(Ok(out)) => {
            let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
            if s.is_empty() { None } else { Some(s) }
        }
        _ => None,
    }
}

#[cfg(unix)]
fn shell_command_output(cmd: &str) -> std::io::Result<std::process::Output> {
    std::process::Command::new("sh").arg("-c").arg(cmd).output()
}

#[cfg(not(unix))]
fn shell_command_output(cmd: &str) -> std::io::Result<std::process::Output> {
    std::process::Command::new("cmd")
        .arg("/C")
        .arg(cmd)
        .output()
}

fn expand_env_placeholders(input: &str) -> String {
    let mut out = String::with_capacity(input.len());
    let bytes = input.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'$' && i + 1 < bytes.len() && bytes[i + 1] == b'{' {
            if let Some(end_rel) = input[i + 2..].find('}') {
                let inner = &input[i + 2..i + 2 + end_rel];
                let (name, default) = match inner.split_once(":-") {
                    Some((n, d)) => (n, Some(d)),
                    None => (inner, None),
                };
                let value = std::env::var(name)
                    .ok()
                    .or_else(|| default.map(|d| d.to_string()));
                if let Some(v) = value {
                    out.push_str(&v);
                } else {
                    out.push_str(&input[i..i + 2 + end_rel + 1]);
                }
                i += 2 + end_rel + 1;
                continue;
            }
        }
        let ch = input[i..].chars().next().expect("valid UTF-8 boundary");
        out.push(ch);
        i += ch.len_utf8();
    }
    out
}

/// Atomically write a full [`McpConfig`] to `path` as pretty-printed
/// JSON. Creates parent directories as needed. Uses the temp-file +
/// rename pattern so a crash mid-write cannot corrupt the config.
pub fn save_mcp_config(path: &Path, config: &McpConfig) -> Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).with_context(|| {
            format!("Failed to create MCP config directory {}", parent.display())
        })?;
    }
    let json = serde_json::to_string_pretty(config).context("Failed to serialize MCP config")?;
    let tmp = path.with_extension("json.tmp");
    std::fs::write(&tmp, &json)
        .with_context(|| format!("Failed to write MCP config tmp {}", tmp.display()))?;
    std::fs::rename(&tmp, path).with_context(|| {
        format!(
            "Failed to rename MCP config {} → {}",
            tmp.display(),
            path.display()
        )
    })?;
    Ok(())
}

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

    #[test]
    fn test_empty_config_when_no_files() {
        let config = load_mcp_config_from(Path::new("/nonexistent"));
        assert!(config.mcp_servers.is_empty());
    }
}

#[cfg(test)]
mod compat_tests {
    use super::*;
    use crate::mcp::types::McpConfig;

    #[test]
    fn reads_standard_camel_case_mcp_config() {
        // The canonical MCP ecosystem format (Cursor / Claude / pi-mcp-adapter).
        let json = r#"{
            "mcpServers": {
                "filesystem": {
                    "command": "npx",
                    "args": ["-y", "@modelcontextprotocol/server-filesystem"],
                    "idleTimeout": 15,
                    "directTools": true
                }
            },
            "settings": {
                "toolPrefix": "server",
                "idleTimeout": 10
            }
        }"#;
        let cfg: McpConfig = serde_json::from_str(json).unwrap();
        let fs = cfg.mcp_servers.get("filesystem").expect("server present");
        assert_eq!(fs.command.as_deref(), Some("npx"));
        assert_eq!(fs.idle_timeout, Some(15));
        assert!(fs.direct_tools.is_some());
        assert!(cfg.settings.as_ref().unwrap().tool_prefix.is_some());
        assert_eq!(cfg.settings.as_ref().unwrap().idle_timeout, Some(10));
    }

    #[test]
    fn reads_legacy_snake_case_mcp_config() {
        let json = r#"{
            "mcpServers": {
                "legacy": {
                    "command": "node",
                    "idle_timeout": 5,
                    "exclude_tools": ["secret_tool"]
                }
            }
        }"#;
        let cfg: McpConfig = serde_json::from_str(json).unwrap();
        let legacy = cfg.mcp_servers.get("legacy").unwrap();
        assert_eq!(legacy.idle_timeout, Some(5));
        assert_eq!(
            legacy.exclude_tools.as_deref(),
            Some(&vec!["secret_tool".to_string()][..])
        );
    }

    #[test]
    fn round_trip_uses_camel_case_aliases() {
        let mut cfg = McpConfig::default();
        cfg.mcp_servers.insert(
            "s".to_string(),
            crate::mcp::types::ServerEntry {
                command: Some("npx".to_string()),
                idle_timeout: Some(7),
                ..Default::default()
            },
        );
        let s = serde_json::to_string(&cfg).unwrap();
        assert!(s.contains("mcpServers"), "serialized key must be camelCase");
        assert!(
            s.contains("idleTimeout"),
            "serialized field must be camelCase"
        );
        // And the round trip parses back.
        let back: McpConfig = serde_json::from_str(&s).unwrap();
        assert_eq!(back.mcp_servers["s"].idle_timeout, Some(7));
    }

    #[test]
    fn save_and_load_round_trip() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("nested").join("mcp.json");
        let mut cfg = McpConfig::default();
        cfg.mcp_servers.insert(
            "remote".to_string(),
            crate::mcp::types::ServerEntry {
                url: Some("https://example.com/mcp".to_string()),
                idle_timeout: Some(3),
                ..Default::default()
            },
        );
        save_mcp_config(&path, &cfg).unwrap();
        assert!(path.exists(), "temp file was renamed into place");
        let loaded = load_or_default(&path);
        assert_eq!(loaded.mcp_servers.len(), 1);
        assert_eq!(
            loaded.mcp_servers["remote"].url.as_deref(),
            Some("https://example.com/mcp")
        );
        assert_eq!(loaded.mcp_servers["remote"].idle_timeout, Some(3));
    }
}