codex-wrapper 0.3.1

A type-safe Codex CLI wrapper for Rust
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
//! Build MCP server configuration for a single run, without touching the
//! user's persistent config.
//!
//! [`McpAddCommand`](crate::McpAddCommand) and friends mutate
//! `$CODEX_HOME/config.toml`. That is the wrong tool for a host running many
//! isolated invocations: a cancelled run leaves residue, and two overlapping
//! runs race each other.
//!
//! # Why overrides rather than a config file
//!
//! `claude-wrapper`'s equivalent writes a JSON file and passes it to
//! `--mcp-config`. Codex has no such flag. Checked against 0.145.0: the only
//! config-bearing options on `codex exec` are `-c/--config`, `--profile`,
//! which layers `$CODEX_HOME/<name>.config.toml`, and `--ignore-user-config`.
//! Nothing consumes a standalone server-config file.
//!
//! So the per-run mechanism here is `-c` overrides, which suits the purpose
//! better than a file would: nothing is written, nothing is left behind when a
//! run is cancelled, and concurrent runs cannot collide.
//!
//! For the cases that do want a file, [`McpConfigBuilder::to_toml`] and
//! [`McpConfigBuilder::write_profile`] produce a profile that `--profile`
//! layers.
//!
//! # Verified forms
//!
//! Each of these was accepted by `codex exec --strict-config`, which rejects
//! a malformed server outright (a table with no transport fails with
//! `invalid transport`):
//!
//! ```text
//! mcp_servers.<name>.command="npx"
//! mcp_servers.<name>.args=["-y","server"]
//! mcp_servers.<name>.env={API_KEY="x"}
//! mcp_servers.<name>.url="https://example.com/mcp"
//! mcp_servers.<name>.bearer_token_env_var="TOKEN"
//! mcp_servers.<name>.env_http_headers={X-Identity="IDENTITY_TOKEN"}
//! mcp_servers.<name>.required=true
//! ```
//!
//! # Example
//!
//! ```
//! use codex_wrapper::{ExecCommand, McpConfigBuilder};
//!
//! let mcp = McpConfigBuilder::new()
//!     .stdio_server("files", "npx")
//!     .http_server("docs", "https://example.com/mcp");
//!
//! let mut cmd = ExecCommand::new("summarize the docs");
//! for override_ in mcp.config_overrides() {
//!     cmd = cmd.config(override_);
//! }
//! ```

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

use crate::error::{Error, Result};

/// How a server is reached.
#[derive(Debug, Clone, PartialEq, Eq)]
enum Transport {
    Stdio {
        command: String,
        args: Vec<String>,
    },
    Http {
        url: String,
        bearer_token_env_var: Option<String>,
        env_http_headers: BTreeMap<String, String>,
    },
}

/// One MCP server's configuration.
///
/// Mirrors what [`McpAddCommand`](crate::McpAddCommand) can register, so the
/// two describe the same thing whether it is persisted or scoped to a run.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct McpServerConfig {
    transport: Transport,
    env: BTreeMap<String, String>,
    required: bool,
}

impl McpServerConfig {
    /// A server launched as a subprocess.
    #[must_use]
    pub fn stdio(command: impl Into<String>) -> Self {
        Self {
            transport: Transport::Stdio {
                command: command.into(),
                args: Vec::new(),
            },
            env: BTreeMap::new(),
            required: false,
        }
    }

    /// A server reached over HTTP.
    #[must_use]
    pub fn http(url: impl Into<String>) -> Self {
        Self {
            transport: Transport::Http {
                url: url.into(),
                bearer_token_env_var: None,
                env_http_headers: BTreeMap::new(),
            },
            env: BTreeMap::new(),
            required: false,
        }
    }

    /// Append a launch argument. Ignored for an HTTP server.
    #[must_use]
    pub fn arg(mut self, value: impl Into<String>) -> Self {
        if let Transport::Stdio { args, .. } = &mut self.transport {
            args.push(value.into());
        }
        self
    }

    /// Set an environment variable for the subprocess.
    #[must_use]
    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.env.insert(key.into(), value.into());
        self
    }

    /// Name the environment variable holding the bearer token.
    ///
    /// The token itself is never part of the configuration: only the name of
    /// the variable to read it from, matching `mcp add --bearer-token-env-var`.
    /// Ignored for a stdio server.
    #[must_use]
    pub fn bearer_token_env_var(mut self, env_var: impl Into<String>) -> Self {
        if let Transport::Http {
            bearer_token_env_var,
            ..
        } = &mut self.transport
        {
            *bearer_token_env_var = Some(env_var.into());
        }
        self
    }

    /// Source an HTTP request header from an environment variable.
    ///
    /// Both arguments are names: `header` is sent on each request and
    /// `env_var` is read by Codex for its value. The secret itself therefore
    /// stays out of argv and persistent configuration. Ignored for a stdio
    /// server.
    #[must_use]
    pub fn env_http_header(
        mut self,
        header: impl Into<String>,
        env_var: impl Into<String>,
    ) -> Self {
        if let Transport::Http {
            env_http_headers, ..
        } = &mut self.transport
        {
            env_http_headers.insert(header.into(), env_var.into());
        }
        self
    }

    /// Require this server to initialize successfully.
    ///
    /// Codex otherwise treats an unavailable MCP server as a warning and may
    /// continue without capabilities the caller expected to be present.
    #[must_use]
    pub fn required(mut self) -> Self {
        self.required = true;
        self
    }

    /// `key = value` pairs for this server, without the `mcp_servers.<name>.`
    /// prefix.
    fn fields(&self) -> Vec<(String, String)> {
        let mut out = Vec::new();
        match &self.transport {
            Transport::Stdio { command, args } => {
                out.push(("command".into(), toml_string(command)));
                if !args.is_empty() {
                    let items: Vec<String> = args.iter().map(|a| toml_string(a)).collect();
                    out.push(("args".into(), format!("[{}]", items.join(","))));
                }
            }
            Transport::Http {
                url,
                bearer_token_env_var,
                env_http_headers,
            } => {
                out.push(("url".into(), toml_string(url)));
                if let Some(var) = bearer_token_env_var {
                    out.push(("bearer_token_env_var".into(), toml_string(var)));
                }
                if !env_http_headers.is_empty() {
                    let pairs: Vec<String> = env_http_headers
                        .iter()
                        .map(|(header, var)| format!("{}={}", toml_key(header), toml_string(var)))
                        .collect();
                    out.push((
                        "env_http_headers".into(),
                        format!("{{{}}}", pairs.join(",")),
                    ));
                }
            }
        }
        if !self.env.is_empty() {
            let pairs: Vec<String> = self
                .env
                .iter()
                .map(|(k, v)| format!("{}={}", toml_key(k), toml_string(v)))
                .collect();
            out.push(("env".into(), format!("{{{}}}", pairs.join(","))));
        }
        if self.required {
            out.push(("required".into(), "true".into()));
        }
        out
    }
}

/// A set of MCP servers for one run.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct McpConfigBuilder {
    servers: BTreeMap<String, McpServerConfig>,
}

impl McpConfigBuilder {
    /// An empty set.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a server.
    #[must_use]
    pub fn server(mut self, name: impl Into<String>, config: McpServerConfig) -> Self {
        self.servers.insert(name.into(), config);
        self
    }

    /// Add a subprocess server. Shorthand for [`McpServerConfig::stdio`].
    #[must_use]
    pub fn stdio_server(self, name: impl Into<String>, command: impl Into<String>) -> Self {
        self.server(name, McpServerConfig::stdio(command))
    }

    /// Add an HTTP server. Shorthand for [`McpServerConfig::http`].
    #[must_use]
    pub fn http_server(self, name: impl Into<String>, url: impl Into<String>) -> Self {
        self.server(name, McpServerConfig::http(url))
    }

    /// `key=value` strings for
    /// [`ExecCommand::config`](crate::ExecCommand::config), or for the client
    /// builder's `config` when the whole client should carry them.
    ///
    /// Ordered, so the same set produces the same arguments.
    #[must_use]
    pub fn config_overrides(&self) -> Vec<String> {
        self.servers
            .iter()
            .flat_map(|(name, config)| {
                config.fields().into_iter().map(move |(key, value)| {
                    format!("mcp_servers.{}.{key}={value}", toml_key(name))
                })
            })
            .collect()
    }

    /// The same configuration as a TOML document.
    ///
    /// Suitable for a `$CODEX_HOME/<name>.config.toml` profile, which
    /// `--profile` layers over the base config.
    #[must_use]
    pub fn to_toml(&self) -> String {
        let mut out = String::new();
        for (name, config) in &self.servers {
            out.push_str(&format!("[mcp_servers.{}]\n", toml_key(name)));
            for (key, value) in config.fields() {
                out.push_str(&format!("{key} = {value}\n"));
            }
            out.push('\n');
        }
        out
    }

    /// Write [`to_toml`](Self::to_toml) to `$CODEX_HOME/<profile>.config.toml`
    /// and return the path.
    ///
    /// Reachable afterwards as `--profile <profile>`. This writes into the
    /// user's codex home, so it is persistent: prefer
    /// [`config_overrides`](Self::config_overrides) for a single run.
    pub fn write_profile(&self, codex_home: impl AsRef<Path>, profile: &str) -> Result<PathBuf> {
        let path = codex_home.as_ref().join(format!("{profile}.config.toml"));
        std::fs::write(&path, self.to_toml()).map_err(|e| Error::Io {
            message: format!("failed to write {}: {e}", path.display()),
            source: e,
            working_dir: Some(codex_home.as_ref().to_path_buf()),
        })?;
        Ok(path)
    }

    /// Whether any server has been added.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.servers.is_empty()
    }
}

/// A TOML basic string, quoted and escaped.
fn toml_string(value: &str) -> String {
    let mut out = String::with_capacity(value.len() + 2);
    out.push('"');
    for ch in value.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04X}", c as u32)),
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

/// A TOML key, bare when it can be and quoted when it cannot.
///
/// Server names come from the caller, so a name with a dot would otherwise
/// silently become a nested table rather than a server called `a.b`.
fn toml_key(key: &str) -> String {
    let bare = !key.is_empty()
        && key
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-');
    if bare {
        key.to_string()
    } else {
        toml_string(key)
    }
}

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

    /// Exactly the forms accepted by `codex exec --strict-config` on 0.145.0.
    #[test]
    fn overrides_match_the_verified_forms() {
        let mcp = McpConfigBuilder::new()
            .server(
                "files",
                McpServerConfig::stdio("npx").arg("-y").arg("server"),
            )
            .server(
                "docs",
                McpServerConfig::http("https://example.com/mcp")
                    .bearer_token_env_var("TOKEN")
                    .env_http_header("X-Identity", "IDENTITY_TOKEN")
                    .required(),
            );

        assert_eq!(
            mcp.config_overrides(),
            vec![
                r#"mcp_servers.docs.url="https://example.com/mcp""#,
                r#"mcp_servers.docs.bearer_token_env_var="TOKEN""#,
                r#"mcp_servers.docs.env_http_headers={X-Identity="IDENTITY_TOKEN"}"#,
                "mcp_servers.docs.required=true",
                r#"mcp_servers.files.command="npx""#,
                r#"mcp_servers.files.args=["-y","server"]"#,
            ]
        );
    }

    #[test]
    fn env_becomes_an_inline_table() {
        let mcp = McpConfigBuilder::new().server(
            "files",
            McpServerConfig::stdio("run").env("B", "2").env("A", "1"),
        );

        assert_eq!(
            mcp.config_overrides(),
            vec![
                r#"mcp_servers.files.command="run""#,
                r#"mcp_servers.files.env={A="1",B="2"}"#,
            ],
            "entries are ordered, so the same set produces the same arguments"
        );
    }

    /// A value carrying a quote or a backslash must not break out of the TOML
    /// string and turn into a different override than intended.
    #[test]
    fn values_are_escaped() {
        let mcp = McpConfigBuilder::new().server(
            "s",
            McpServerConfig::stdio(r#"say "hi""#)
                .arg("back\\slash")
                .arg("two\nlines"),
        );

        let overrides = mcp.config_overrides();
        assert_eq!(overrides[0], r#"mcp_servers.s.command="say \"hi\"""#);
        assert_eq!(
            overrides[1],
            r#"mcp_servers.s.args=["back\\slash","two\nlines"]"#
        );
    }

    /// A dotted name would otherwise become a nested table rather than a
    /// server whose name contains a dot.
    #[test]
    fn a_name_needing_quotes_gets_them() {
        let mcp = McpConfigBuilder::new().stdio_server("my.server", "run");
        assert_eq!(
            mcp.config_overrides(),
            vec![r#"mcp_servers."my.server".command="run""#]
        );
    }

    #[test]
    fn args_and_bearer_token_apply_only_where_they_belong() {
        // An HTTP server ignores launch args; a stdio server ignores the token.
        let http =
            McpConfigBuilder::new().server("h", McpServerConfig::http("https://x").arg("-y"));
        assert_eq!(
            http.config_overrides(),
            vec![r#"mcp_servers.h.url="https://x""#]
        );

        let stdio = McpConfigBuilder::new()
            .server("s", McpServerConfig::stdio("run").bearer_token_env_var("T"));
        assert_eq!(
            stdio.config_overrides(),
            vec![r#"mcp_servers.s.command="run""#]
        );

        let stdio = McpConfigBuilder::new().server(
            "s",
            McpServerConfig::stdio("run").env_http_header("X-Identity", "TOKEN"),
        );
        assert_eq!(
            stdio.config_overrides(),
            vec![r#"mcp_servers.s.command="run""#]
        );
    }

    #[test]
    fn env_backed_http_headers_are_ordered_and_escape_header_names() {
        let mcp = McpConfigBuilder::new().server(
            "api",
            McpServerConfig::http("https://example.com/mcp")
                .env_http_header("x.second", "SECOND_TOKEN")
                .env_http_header("x-first", "FIRST_TOKEN"),
        );

        assert_eq!(
            mcp.config_overrides(),
            vec![
                r#"mcp_servers.api.url="https://example.com/mcp""#,
                r#"mcp_servers.api.env_http_headers={x-first="FIRST_TOKEN","x.second"="SECOND_TOKEN"}"#,
            ]
        );
    }

    #[test]
    fn to_toml_produces_a_profile_document() {
        let mcp = McpConfigBuilder::new().server("files", McpServerConfig::stdio("npx").arg("-y"));

        assert_eq!(
            mcp.to_toml(),
            "[mcp_servers.files]\ncommand = \"npx\"\nargs = [\"-y\"]\n\n"
        );
    }

    #[test]
    fn write_profile_lands_where_profile_would_look() {
        let home = std::env::temp_dir().join(format!("codex-wrapper-mcp-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&home);
        std::fs::create_dir_all(&home).unwrap();

        let path = McpConfigBuilder::new()
            .stdio_server("files", "npx")
            .write_profile(&home, "isolated")
            .unwrap();

        assert_eq!(path, home.join("isolated.config.toml"));
        let written = std::fs::read_to_string(&path).unwrap();
        assert!(written.contains("[mcp_servers.files]"), "{written}");

        let _ = std::fs::remove_dir_all(&home);
    }

    #[test]
    fn an_empty_builder_produces_nothing() {
        let mcp = McpConfigBuilder::new();
        assert!(mcp.is_empty());
        assert!(mcp.config_overrides().is_empty());
        assert_eq!(mcp.to_toml(), "");
    }
}