agent-loadout 1.0.0

Loadout CLI: manage MCP servers, skills and secret references for AI coding clients
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
//! `loadout exec` (SPEC 11.5): launch-time secret injection for stdio MCP
//! servers.
//!
//! ```text
//! loadout exec [--op-account ACCOUNT] [--ref NAME=REF]... [--op-env ID --map KEY=NAME...] -- <cmd> [args]
//! ```
//!
//! - Self-contained: the references are in argv (references, never
//!   values); the store is never read.
//! - `env:` references come from the inherited environment, `keyring://`
//!   from the OS keyring, `op://` from `op read --no-newline`. With
//!   `--op-env`, the server starts under `op run --environment <id>
//!   --no-masking -- <this binary> exec --stage2 --map KEY=NAME... -- <cmd>`:
//!   stage 2 copies each Environment variable `NAME` to `KEY` and drops
//!   every variable `op run` added that the server did not ask for.
//! - Values live only in memory and in the child's environment.
//!   `OP_SERVICE_ACCOUNT_TOKEN` (from the environment, else
//!   `keyring://loadout/OP_SERVICE_ACCOUNT_TOKEN`) reaches `op` only, never
//!   the server. `--op-account` becomes `OP_ACCOUNT` for `op` (unless the
//!   environment names an account already), for machines whose desktop
//!   app is signed in to several accounts.
//! - Nothing is written to stdout: it is the server's JSON-RPC channel.
//!   Errors go to stderr as `loadout-exec: ...`; exit 78 for configuration
//!   errors, 77 for secrets that cannot be had; otherwise the server's own
//!   exit code.
//! - Unix replaces this process with the server (`execvp`); Windows starts
//!   it inside a kill-on-close Job Object and forwards its exit code.

#[cfg(unix)]
mod unix;
#[cfg(windows)]
mod windows;

use std::collections::BTreeSet;
use std::ffi::{OsStr, OsString};

use agent_loadout_core::secrets::SecretValue;
use agent_loadout_core::secrets::delivery::{ACCOUNT_VAR, SERVICE_ACCOUNT_VAR, op_env};
use agent_loadout_core::secrets::grammar::{SecretRef, is_op_account, is_op_environment_id};
use agent_loadout_core::secrets::providers::onepassword::{OP_NOT_FOUND, OpCli, OpFeature};
use agent_loadout_core::secrets::providers::{env as env_provider, keyring as keyring_provider};
use agent_loadout_core::secrets::render::parse_binding;
use agent_loadout_core::secrets::status::{Resolution, SecretStatus};
use agent_loadout_core::{Env, RealFs, brand};

/// Prefix of every message `exec` writes (stderr only).
pub const PREFIX: &str = brand::EXEC_PREFIX;
/// A secret could not be had: missing, locked, or the provider failed.
pub const EXIT_MISSING: i32 = 77;
/// The invocation or this machine's setup is wrong (bad arguments, a
/// provider not installed or too old, a command that cannot start).
pub const EXIT_CONFIG: i32 = 78;

const STAGE2: &str = "--stage2";
/// Names of the variables stage 1 hands to the server, for stage 2 (names
/// only, separated by line breaks).
const KEEP_VAR: &str = brand::EXEC_ENV_VAR;

fn usage() -> String {
    format!(
        "usage: {} exec [--op-account ACCOUNT] [--ref NAME=REF]... [--op-env ID --map KEY=NAME...] -- <cmd> [args]",
        brand::BIN_NAME
    )
}

/// Why `exec` stops before the server starts.
#[derive(Debug)]
pub struct Failure {
    pub code: i32,
    pub message: String,
}

impl Failure {
    fn config(message: impl Into<String>) -> Self {
        Self {
            code: EXIT_CONFIG,
            message: message.into(),
        }
    }

    fn missing(message: impl Into<String>) -> Self {
        Self {
            code: EXIT_MISSING,
            message: message.into(),
        }
    }

    /// A failed resolution of `key`. A missing 1Password CLI names the
    /// per-machine fix (SPEC 11.11).
    fn resolution(key: &str, resolution: &Resolution) -> Self {
        let detail = resolution.message.as_deref().unwrap_or("");
        match resolution.status {
            SecretStatus::ProviderUnavailable if detail == OP_NOT_FOUND => Self::config(format!(
                "{OP_NOT_FOUND} (op); install it or map the alias behind {key} to keyring/env \
                 for this machine"
            )),
            SecretStatus::ProviderUnavailable | SecretStatus::ProviderTooOld => {
                Self::config(format!("{key}: {detail}"))
            }
            status => Self::missing(format!("{key}: {status}: {detail}")),
        }
    }
}

/// What to run.
#[derive(Debug, Default)]
struct Invocation {
    stage2: bool,
    refs: Vec<(String, SecretRef)>,
    environment: Option<String>,
    /// `--op-account`: the 1Password account `op` uses.
    account: Option<String>,
    maps: Vec<(String, String)>,
    command: OsString,
    args: Vec<OsString>,
}

/// Runs `loadout exec` with the arguments after `exec`. Returns only on
/// failure or (Windows) when the server exits.
pub fn main(args: &[OsString]) -> ! {
    let code = match run(args) {
        Ok(code) => code,
        Err(failure) => {
            eprintln!("{PREFIX} {}", failure.message);
            failure.code
        }
    };
    std::process::exit(code)
}

fn run(args: &[OsString]) -> Result<i32, Failure> {
    let invocation = parse(args)?;
    if invocation.stage2 {
        stage2(invocation)
    } else {
        stage1(invocation)
    }
}

fn parse(args: &[OsString]) -> Result<Invocation, Failure> {
    let mut invocation = Invocation::default();
    let mut rest = args.iter();
    let text = |value: Option<&OsString>, flag: &str| -> Result<String, Failure> {
        let value = value.ok_or_else(|| Failure::config(format!("{flag} needs a value")))?;
        value
            .to_str()
            .map(str::to_owned)
            .ok_or_else(|| Failure::config(format!("{flag} value is not UTF-8")))
    };
    loop {
        let Some(arg) = rest.next() else {
            return Err(Failure::config(
                "expected `-- <command> [args]` after the options",
            ));
        };
        let arg = arg.to_string_lossy();
        let (flag, inline) = match arg.split_once('=') {
            Some((flag, value)) if flag.starts_with("--") => {
                (flag.to_owned(), Some(value.to_owned()))
            }
            _ => (arg.into_owned(), None),
        };
        let value = |rest: &mut std::slice::Iter<'_, OsString>| match &inline {
            Some(value) => Ok(value.clone()),
            None => text(rest.next(), &flag),
        };
        match flag.as_str() {
            "--" => break,
            "-h" | "--help" => {
                return Err(Failure {
                    code: 0,
                    message: usage(),
                });
            }
            STAGE2 => invocation.stage2 = true,
            "--ref" => {
                let binding = value(&mut rest)?;
                let (key, reference) =
                    parse_binding(&binding).map_err(|err| Failure::config(err.message()))?;
                match reference {
                    SecretRef::OnePasswordEnvironment { environment, name } => {
                        set_environment(&mut invocation, environment)?;
                        invocation.maps.push((key, name));
                    }
                    reference => invocation.refs.push((key, reference)),
                }
            }
            "--op-account" => {
                let account = value(&mut rest)?;
                if !is_op_account(&account) {
                    return Err(Failure::config(format!(
                        "invalid 1Password account {account:?}"
                    )));
                }
                invocation.account = Some(account);
            }
            "--op-env" => {
                let id = value(&mut rest)?;
                if !is_op_environment_id(&id) {
                    return Err(Failure::config(format!(
                        "invalid 1Password Environment id {id:?}"
                    )));
                }
                set_environment(&mut invocation, id)?;
            }
            "--map" => {
                let map = value(&mut rest)?;
                let valid = |name: &str| {
                    agent_loadout_core::model::ids::is_valid(
                        agent_loadout_core::model::ids::IdKind::EnvVar,
                        name,
                    )
                };
                match map.split_once('=') {
                    Some((key, name)) if valid(key) && valid(name) => {
                        invocation.maps.push((key.to_owned(), name.to_owned()));
                    }
                    _ => {
                        return Err(Failure::config(format!(
                            "expected --map KEY=NAME (variable names), got {map:?}"
                        )));
                    }
                }
            }
            other => return Err(Failure::config(format!("unknown option {other:?}"))),
        }
    }
    let Some(command) = rest.next() else {
        return Err(Failure::config("expected a command after `--`"));
    };
    invocation.command = command.clone();
    invocation.args = rest.cloned().collect();
    if invocation.stage2 {
        if !invocation.refs.is_empty()
            || invocation.environment.is_some()
            || invocation.account.is_some()
        {
            return Err(Failure::config("--stage2 takes only --map"));
        }
    } else if !invocation.maps.is_empty() && invocation.environment.is_none() {
        return Err(Failure::config("--map needs --op-env <environment-id>"));
    }
    Ok(invocation)
}

fn set_environment(invocation: &mut Invocation, id: String) -> Result<(), Failure> {
    match &invocation.environment {
        Some(other) if *other != id => Err(Failure::config(
            "one 1Password Environment per server (--op-env and op-env:// references differ)",
        )),
        _ => {
            invocation.environment = Some(id);
            Ok(())
        }
    }
}

/// Resolves the references and starts the server, or `op run` for an
/// Environment.
fn stage1(invocation: Invocation) -> Result<i32, Failure> {
    let env = Env::from_process().map_err(|err| Failure::config(err.message()))?;
    let keyring = crate::keyring::keyring(&env);
    let needs_op = invocation.environment.is_some()
        || invocation
            .refs
            .iter()
            .any(|(_, reference)| matches!(reference, SecretRef::OnePassword { .. }));
    let (op, op_child_env) = if needs_op {
        (
            OpCli::detect(&env, &RealFs),
            op_env(&env, keyring.as_ref(), invocation.account.as_deref()),
        )
    } else {
        (OpCli::NotFound, env.clone())
    };

    let mut values: Vec<(String, SecretValue)> = Vec::new();
    for (key, reference) in &invocation.refs {
        let value = match reference {
            SecretRef::Env { name } => env_provider::resolve(&env, name),
            SecretRef::Keyring { service, account } => {
                keyring_provider::resolve(keyring.as_ref(), service, account)
            }
            SecretRef::OnePassword { .. } => op.read(&op_child_env, &reference.to_string()),
            SecretRef::OnePasswordEnvironment { .. } => {
                unreachable!("parse moves Environment references to --map")
            }
        }
        .map_err(|resolution| Failure::resolution(key, &resolution))?;
        values.push((key.clone(), value));
    }

    // The server's environment: inherited, without the service account
    // token, plus the resolved values. Variables named like a `--map` key
    // or Environment name are dropped too, so whatever stage 2 finds under
    // those names came from `op run`, never from the client's environment.
    let mut child: Vec<(OsString, OsString)> = std::env::vars_os()
        .filter(|(name, _)| !same_name(name, SERVICE_ACCOUNT_VAR))
        .filter(|(name, _)| !values.iter().any(|(key, _)| same_name(name, key)))
        .filter(|(name, _)| {
            !invocation
                .maps
                .iter()
                .any(|(key, var)| same_name(name, key) || same_name(name, var))
        })
        .collect();
    child.extend(
        values
            .iter()
            .map(|(key, value)| (OsString::from(key), OsString::from(value.expose()))),
    );

    let Some(environment) = &invocation.environment else {
        return spawn(&invocation.command, &invocation.args, &child);
    };
    let op_path = op
        .require(OpFeature::Environments)
        .map_err(|resolution| {
            let key = invocation
                .maps
                .first()
                .map_or("the 1Password Environment", |(key, _)| key.as_str());
            Failure::resolution(key, &resolution)
        })?
        .to_path_buf();
    // `op` gets the account `op_env` chose. When that is not the value the
    // client passed (an empty `OP_ACCOUNT` counts as unset), the server
    // does not get it back through stage 2.
    let account = op_child_env.var(ACCOUNT_VAR);
    let replaced = |name: &OsStr, value: &OsStr| {
        same_name(name, ACCOUNT_VAR) && account.is_some_and(|a| value != OsStr::new(a))
    };
    let names: Vec<String> = child
        .iter()
        .filter(|(name, value)| !replaced(name, value))
        .map(|(name, _)| name.to_string_lossy().into_owned())
        .collect();
    let mut op_vars = child.clone();
    op_vars.push((KEEP_VAR.into(), names.join("\n").into()));
    if let Some(token) = op_child_env.var(SERVICE_ACCOUNT_VAR) {
        op_vars.push((SERVICE_ACCOUNT_VAR.into(), token.to_owned()));
    }
    if let Some(account) = account {
        op_vars.retain(|(name, _)| !same_name(name, ACCOUNT_VAR));
        op_vars.push((ACCOUNT_VAR.into(), account.to_owned()));
    }
    let this = std::env::current_exe()
        .map_err(|err| Failure::config(format!("cannot find this binary: {err}")))?;
    let mut args: Vec<OsString> = [
        "run",
        "--environment",
        environment.as_str(),
        "--no-masking",
        "--",
    ]
    .iter()
    .map(OsString::from)
    .collect();
    args.push(this.into_os_string());
    args.push("exec".into());
    args.push(STAGE2.into());
    for (key, name) in &invocation.maps {
        args.push("--map".into());
        args.push(format!("{key}={name}").into());
    }
    args.push("--".into());
    args.push(invocation.command.clone());
    args.extend(invocation.args.iter().cloned());
    spawn(op_path.as_os_str(), &args, &op_vars)
}

/// Under `op run`: maps Environment variables to the server's keys, keeps
/// only what stage 1 passed on, and starts the server.
fn stage2(invocation: Invocation) -> Result<i32, Failure> {
    let keep = std::env::var_os(KEEP_VAR).ok_or_else(|| {
        Failure::config("--stage2 runs only under `loadout exec --op-env` (through `op run`)")
    })?;
    let keep: BTreeSet<String> = keep.to_string_lossy().split('\n').map(name_key).collect();
    let current: Vec<(OsString, OsString)> = std::env::vars_os().collect();
    let mut mapped = Vec::new();
    for (key, name) in &invocation.maps {
        let value = current
            .iter()
            .find(|(var, value)| same_name(var, name) && !value.is_empty())
            .map(|(_, value)| value.clone())
            .ok_or_else(|| {
                Failure::missing(format!(
                    "{key}: the 1Password Environment has no variable {name}"
                ))
            })?;
        mapped.push((OsString::from(key), value));
    }
    let mut child: Vec<(OsString, OsString)> = current
        .into_iter()
        .filter(|(name, _)| keep.contains(&name_key(&name.to_string_lossy())))
        .filter(|(name, _)| !same_name(name, SERVICE_ACCOUNT_VAR) && !same_name(name, KEEP_VAR))
        .filter(|(name, _)| {
            !mapped
                .iter()
                .any(|(key, _)| same_name(name, &key.to_string_lossy()))
        })
        .collect();
    child.extend(mapped);
    spawn(&invocation.command, &invocation.args, &child)
}

/// Environment variable names compare case-insensitively on Windows.
fn name_key(name: &str) -> String {
    if cfg!(windows) {
        name.to_ascii_uppercase()
    } else {
        name.to_owned()
    }
}

fn same_name(name: &OsStr, other: &str) -> bool {
    name_key(&name.to_string_lossy()) == name_key(other)
}

#[cfg(unix)]
fn spawn(program: &OsStr, args: &[OsString], env: &[(OsString, OsString)]) -> Result<i32, Failure> {
    unix::exec(program, args, env)
}

#[cfg(windows)]
fn spawn(program: &OsStr, args: &[OsString], env: &[(OsString, OsString)]) -> Result<i32, Failure> {
    windows::run(program, args, env)
}