a4aa 0.4.1

Adapter-For-All-Agents — unified agent client adapter: process supervision, stdio/PTY transports, JSON-RPC and ACP lanes, CLI drivers, and a stdio sidecar for hosts in other languages.
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! MCP server declarations carried by a lane spec, and their translation
//! into protocol-native registration surfaces.
//!
//! Hosts and profiles already describe MCP servers in the ACP `session/new`
//! shape (`{name, command, args, env}`). A lane that can register MCP
//! servers declares *how* that list reaches its wire; a lane that cannot
//! must refuse the list rather than silently drop it. Dropping the servers
//! is what turns a verifier turn into a plausible-looking answer with no way
//! to submit a verdict.

use crate::error::AdapterError;
use crate::model::RpcMcpConfig;
use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet};

/// One stdio MCP server in the ACP declaration shape.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct McpServer {
    /// Dotted-path-safe name; unique within one lane spec.
    pub name: String,
    /// Executable launched by the agent to speak MCP over stdio.
    pub command: String,
    /// Arguments passed to the executable.
    pub args: Vec<String>,
    /// Environment entries the server needs, in declaration order.
    pub env: Vec<(String, String)>,
}

/// The argv additions and child-environment entries one
/// [`RpcMcpConfig`] needs to install a server list.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct McpInstall {
    /// Arguments appended to the agent's argv.
    pub args: Vec<String>,
    /// Environment entries added before the child is spawned. Values are
    /// passed by name to the agent's own config (`env_vars`), so secrets do
    /// not ride the command line.
    pub env: BTreeMap<String, String>,
}

impl McpServer {
    /// Parse one ACP-shaped server declaration. Unsupported or malformed
    /// declarations are refused; an unrecognized field is never ignored.
    pub fn from_value(value: &Value) -> Result<Self, AdapterError> {
        let object = value.as_object().ok_or_else(|| {
            invalid("an MCP server entry must be an object in the ACP `{name, command, args, env}` shape")
        })?;
        for key in object.keys() {
            if !matches!(key.as_str(), "name" | "command" | "args" | "env") {
                return Err(invalid(format!("unsupported MCP server field `{key}`")));
            }
        }
        let name = object
            .get("name")
            .and_then(Value::as_str)
            .filter(|name| valid_name(name))
            .ok_or_else(|| {
                invalid(
                    "MCP server `name` must be non-empty and use only ASCII letters, digits, `_` or `-`",
                )
            })?
            .to_owned();
        let command = object
            .get("command")
            .and_then(Value::as_str)
            .filter(|command| !command.trim().is_empty())
            .ok_or_else(|| invalid(format!("MCP server `{name}` has no `command`")))?
            .to_owned();
        let args = match object.get("args") {
            None => Vec::new(),
            Some(Value::Array(items)) => items
                .iter()
                .map(|item| {
                    item.as_str().map(str::to_owned).ok_or_else(|| {
                        invalid(format!("MCP server `{name}` has a non-string `args` entry"))
                    })
                })
                .collect::<Result<Vec<_>, _>>()?,
            Some(_) => {
                return Err(invalid(format!(
                    "MCP server `{name}` `args` must be an array of strings"
                )))
            }
        };
        let env = parse_env(&name, object.get("env"))?;
        Ok(Self {
            name,
            command,
            args,
            env,
        })
    }

    /// Parse and validate a lane's whole server list. Duplicate names are
    /// refused because the registration key (and therefore the last writer)
    /// would be ambiguous.
    pub fn parse_all(values: &[Value]) -> Result<Vec<Self>, AdapterError> {
        let mut servers = Vec::with_capacity(values.len());
        let mut names = BTreeSet::new();
        for value in values {
            let server = Self::from_value(value)?;
            if !names.insert(server.name.clone()) {
                return Err(invalid(format!(
                    "MCP server name `{}` is declared more than once",
                    server.name
                )));
            }
            servers.push(server);
        }
        Ok(servers)
    }
}

/// Translate a server list into the argv and environment additions declared
/// by `config`.
///
/// The config-override shape installs each server under
/// `<root>.<name>.{command,args,env_vars}`. Environment values travel in the
/// child's environment and only their *names* appear on the command line;
/// two servers asking for the same name with different values are refused
/// instead of one silently winning.
pub fn install(config: &RpcMcpConfig, servers: &[McpServer]) -> Result<McpInstall, AdapterError> {
    if config.flag.trim().is_empty() || config.root.trim().is_empty() {
        return Err(invalid(
            "MCP registration needs a non-empty argv flag and config root",
        ));
    }
    if !config.tools_approval_servers.is_empty() && config.tools_approval.is_none() {
        return Err(invalid(
            "MCP tools approval names servers but the lane declares no approval key",
        ));
    }
    for name in &config.tools_approval_servers {
        if name.is_empty()
            || !name
                .chars()
                .all(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '-')
        {
            return Err(invalid(
                "MCP tools approval server names must be non-empty ASCII letters, digits, `_` or `-`",
            ));
        }
    }
    let mut installed = McpInstall::default();
    for server in servers {
        let base = format!("{}.{}", config.root, server.name);
        push_config(
            &mut installed.args,
            config,
            &format!("{base}.command"),
            toml_string(&server.command),
        );
        if !server.args.is_empty() {
            push_config(
                &mut installed.args,
                config,
                &format!("{base}.args"),
                toml_array(server.args.iter().map(String::as_str)),
            );
        }
        if !server.env.is_empty() {
            if config.env_vars_key.trim().is_empty() {
                return Err(invalid(format!(
                    "MCP server `{}` carries environment values but this lane cannot forward them",
                    server.name
                )));
            }
            for (name, value) in &server.env {
                match installed.env.get(name) {
                    Some(existing) if existing != value => {
                        return Err(invalid(format!(
                        "MCP environment variable `{name}` has conflicting values across servers"
                    )))
                    }
                    Some(_) => {}
                    None => {
                        installed.env.insert(name.clone(), value.clone());
                    }
                }
            }
            push_config(
                &mut installed.args,
                config,
                &format!("{base}.{}", config.env_vars_key),
                toml_array(server.env.iter().map(|(name, _)| name.as_str())),
            );
        }
        if let Some(approval) = &config.tools_approval {
            if approval.key.trim().is_empty() || approval.value.trim().is_empty() {
                return Err(invalid(
                    "MCP tools approval needs a non-empty config key and value",
                ));
            }
            // Approval is per server, never card-wide. A server the host did
            // not name keeps the agent's prompting default; an empty list
            // therefore installs nothing.
            if config
                .tools_approval_servers
                .iter()
                .any(|name| name == &server.name)
            {
                push_config(
                    &mut installed.args,
                    config,
                    &format!("{base}.{}", approval.key),
                    toml_string(&approval.value),
                );
            }
        }
    }
    Ok(installed)
}

fn push_config(args: &mut Vec<String>, config: &RpcMcpConfig, key: &str, value: String) {
    args.push(config.flag.clone());
    args.push(format!("{key}={value}"));
}

fn parse_env(name: &str, value: Option<&Value>) -> Result<Vec<(String, String)>, AdapterError> {
    let mut env = Vec::new();
    match value {
        None => {}
        // ACP shape: [{"name": ..., "value": ...}]
        Some(Value::Array(items)) => {
            for item in items {
                let object = item.as_object().ok_or_else(|| {
                    invalid(format!(
                        "MCP server `{name}` `env` entries must be `{{name, value}}` objects"
                    ))
                })?;
                let key = object.get("name").and_then(Value::as_str).ok_or_else(|| {
                    invalid(format!("MCP server `{name}` has an env entry with no name"))
                })?;
                let value = object.get("value").and_then(Value::as_str).ok_or_else(|| {
                    invalid(format!(
                        "MCP server `{name}` env `{key}` must have a string value"
                    ))
                })?;
                push_env(&mut env, name, key, value)?;
            }
        }
        // Profile shape: {"KEY": "value"}
        Some(Value::Object(map)) => {
            for (key, item) in map {
                let value = item.as_str().ok_or_else(|| {
                    invalid(format!(
                        "MCP server `{name}` env `{key}` must have a string value"
                    ))
                })?;
                push_env(&mut env, name, key, value)?;
            }
        }
        Some(_) => {
            return Err(invalid(format!(
                "MCP server `{name}` `env` must be an array or object"
            )))
        }
    }
    let mut seen = BTreeSet::new();
    for (key, _) in &env {
        if !seen.insert(key.clone()) {
            return Err(invalid(format!(
                "MCP server `{name}` declares env `{key}` more than once"
            )));
        }
    }
    Ok(env)
}

/// Append one environment entry after validating it against `Command`'s own
/// contract. `Command::envs` panics on a name containing `=` or NUL and on a
/// value containing NUL; a malformed host declaration must become the
/// adapter's typed refusal before any spawn, not an abort mid-open.
fn push_env(
    env: &mut Vec<(String, String)>,
    server: &str,
    key: &str,
    value: &str,
) -> Result<(), AdapterError> {
    if key.trim().is_empty() {
        return Err(invalid(format!(
            "MCP server `{server}` has an env entry with no name"
        )));
    }
    if key.contains('=') || key.contains('\0') {
        return Err(invalid(format!(
            "MCP server `{server}` env name {key:?} cannot be passed to a child process"
        )));
    }
    if value.contains('\0') {
        return Err(invalid(format!(
            "MCP server `{server}` env {key:?} has a NUL in its value"
        )));
    }
    env.push((key.to_owned(), value.to_owned()));
    Ok(())
}

fn valid_name(name: &str) -> bool {
    !name.is_empty()
        && name
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || ch == '_' || ch == '-')
}

fn invalid(detail: impl Into<String>) -> AdapterError {
    AdapterError::permanent("agent_mcp_server_invalid", "session/open").with_detail(detail.into())
}

/// Encode one TOML basic string. JSON strings and TOML basic strings share
/// the common escapes; control characters use TOML's `\u`/`\U` form.
fn toml_string(text: &str) -> String {
    let mut out = String::with_capacity(text.len() + 2);
    out.push('"');
    for ch in text.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            ch if ch.is_control() => {
                let code = ch as u32;
                if code <= 0xFFFF {
                    out.push_str(&format!("\\u{code:04X}"));
                } else {
                    out.push_str(&format!("\\U{code:08X}"));
                }
            }
            ch => out.push(ch),
        }
    }
    out.push('"');
    out
}

fn toml_array<'a>(items: impl Iterator<Item = &'a str>) -> String {
    let encoded: Vec<String> = items.map(toml_string).collect();
    format!("[{}]", encoded.join(","))
}

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

    fn config() -> RpcMcpConfig {
        RpcMcpConfig {
            flag: "-c".into(),
            root: "mcp_servers".into(),
            env_vars_key: "env_vars".into(),
            tools_approval: None,
            tools_approval_servers: Vec::new(),
        }
    }

    #[test]
    fn tools_approval_is_installed_per_server() {
        let servers = McpServer::parse_all(&[json!({
            "name": "host-tools",
            "command": "/opt/host-tools/bin/server",
            "args": ["mcp"]
        })])
        .unwrap();
        let mut config = config();
        config.tools_approval = Some(crate::model::RpcMcpToolApproval {
            key: "default_tools_approval_mode".into(),
            value: "approve".into(),
        });
        config.tools_approval_servers = vec!["host-tools".into()];
        let installed = install(&config, &servers).unwrap();
        assert_eq!(
            installed.args.last().map(String::as_str),
            Some("mcp_servers.host-tools.default_tools_approval_mode=\"approve\"")
        );
    }

    #[test]
    fn acp_shaped_servers_become_config_overrides_with_env_names() {
        let servers = McpServer::parse_all(&[json!({
            "name": "host-tools",
            "command": "/opt/host-tools/bin/server",
            "args": ["mcp"],
            "env": [{"name": "HOST_TOOLS_TOKEN", "value": "synthetic-token"}]
        })])
        .unwrap();
        let installed = install(&config(), &servers).unwrap();
        assert_eq!(
            installed.args,
            vec![
                "-c".to_owned(),
                "mcp_servers.host-tools.command=\"/opt/host-tools/bin/server\"".to_owned(),
                "-c".to_owned(),
                "mcp_servers.host-tools.args=[\"mcp\"]".to_owned(),
                "-c".to_owned(),
                "mcp_servers.host-tools.env_vars=[\"HOST_TOOLS_TOKEN\"]".to_owned(),
            ]
        );
        assert_eq!(
            installed.env.get("HOST_TOOLS_TOKEN"),
            Some(&"synthetic-token".to_owned())
        );
    }

    #[test]
    fn profile_env_object_shape_is_accepted() {
        let servers = McpServer::parse_all(&[json!({
            "name": "files",
            "command": "/usr/local/bin/files-mcp",
            "env": {"ROOT": "/tmp"}
        })])
        .unwrap();
        assert_eq!(servers[0].env, vec![("ROOT".to_owned(), "/tmp".to_owned())]);
    }

    #[test]
    fn unknown_fields_are_refused_not_dropped() {
        let error = McpServer::from_value(&json!({
            "name": "remote",
            "url": "https://example.test/mcp"
        }))
        .unwrap_err();
        assert_eq!(error.code, "agent_mcp_server_invalid");
        assert!(
            error.to_string().contains("unsupported MCP server field"),
            "{error}"
        );
    }

    #[test]
    fn duplicate_names_and_conflicting_env_are_refused() {
        let duplicate = McpServer::parse_all(&[
            json!({"name": "same", "command": "/bin/one"}),
            json!({"name": "same", "command": "/bin/two"}),
        ])
        .unwrap_err();
        assert!(
            duplicate.to_string().contains("more than once"),
            "{duplicate}"
        );

        let servers = McpServer::parse_all(&[
            json!({"name": "one", "command": "/bin/one", "env": [{"name": "TOKEN", "value": "a"}]}),
            json!({"name": "two", "command": "/bin/two", "env": [{"name": "TOKEN", "value": "b"}]}),
        ])
        .unwrap();
        let conflict = install(&config(), &servers).unwrap_err();
        assert!(
            conflict.to_string().contains("conflicting values"),
            "{conflict}"
        );
    }

    #[test]
    fn escaped_values_never_break_out_of_the_toml_string() {
        let servers = McpServer::parse_all(&[json!({
            "name": "quoted",
            "command": "/bin/agent",
            "args": ["a\"b", "line\nbreak", "slash\\"]
        })])
        .unwrap();
        let installed = install(&config(), &servers).unwrap();
        assert_eq!(
            installed.args[3],
            "mcp_servers.quoted.args=[\"a\\\"b\",\"line\\nbreak\",\"slash\\\\\"]"
        );
    }

    #[test]
    fn env_without_a_forwarding_key_is_refused() {
        let servers = McpServer::parse_all(&[json!({
            "name": "needs-env",
            "command": "/bin/agent",
            "env": [{"name": "TOKEN", "value": "x"}]
        })])
        .unwrap();
        let mut config = config();
        config.env_vars_key.clear();
        let error = install(&config, &servers).unwrap_err();
        assert!(error.to_string().contains("cannot forward"), "{error}");
    }

    #[test]
    fn approval_is_installed_only_for_a_named_server() {
        let servers = McpServer::parse_all(&[
            json!({"name": "trusted", "command": "/bin/trusted"}),
            json!({"name": "other", "command": "/bin/other"}),
        ])
        .unwrap();
        let mut config = config();
        config.tools_approval = Some(crate::model::RpcMcpToolApproval {
            key: "default_tools_approval_mode".into(),
            value: "approve".into(),
        });
        config.tools_approval_servers = vec!["trusted".into()];
        let installed = install(&config, &servers).unwrap();
        let approval: Vec<&String> = installed
            .args
            .iter()
            .filter(|arg| arg.ends_with("default_tools_approval_mode=\"approve\""))
            .collect();
        assert_eq!(approval.len(), 1, "{:?}", installed.args);
        assert!(
            approval[0].starts_with("mcp_servers.trusted."),
            "the named server keeps prompting off, every other server keeps the default: {:?}",
            installed.args
        );
    }

    #[test]
    fn approval_without_named_servers_pre_approves_nothing() {
        let servers = McpServer::parse_all(&[json!({
            "name": "untrusted",
            "command": "/bin/agent"
        })])
        .unwrap();
        let mut config = config();
        config.tools_approval = Some(crate::model::RpcMcpToolApproval {
            key: "default_tools_approval_mode".into(),
            value: "approve".into(),
        });
        let installed = install(&config, &servers).unwrap();
        assert!(
            installed
                .args
                .iter()
                .all(|arg| !arg.contains("tools_approval_mode")),
            "card-wide approval must no longer exist: {:?}",
            installed.args
        );
    }

    #[test]
    fn approval_names_without_a_key_are_refused_not_silently_dropped() {
        let servers = McpServer::parse_all(&[json!({
            "name": "host-tools",
            "command": "/bin/agent"
        })])
        .unwrap();
        let mut config = config();
        config.tools_approval_servers = vec!["host-tools".into()];
        let error = install(&config, &servers).expect_err("a named approval needs its key");
        assert_eq!(error.code, "agent_mcp_server_invalid", "{error}");
        config.tools_approval = Some(crate::model::RpcMcpToolApproval {
            key: "default_tools_approval_mode".into(),
            value: "approve".into(),
        });
        config.tools_approval_servers = vec!["bad name".into()];
        let error = install(&config, &servers).expect_err("approval names are validated");
        assert_eq!(error.code, "agent_mcp_server_invalid", "{error}");
    }

    #[test]
    fn env_entries_that_command_envs_would_panic_on_are_refused() {
        for invalid in [
            json!({"name": "empty-name", "command": "/bin/agent", "env": {"": "x"}}),
            json!({"name": "blank-name", "command": "/bin/agent", "env": {"  ": "x"}}),
            json!({"name": "equals", "command": "/bin/agent", "env": [{"name": "BAD=NAME", "value": "x"}]}),
            json!({"name": "nul-name", "command": "/bin/agent", "env": [{"name": "BAD\u{0}NAME", "value": "x"}]}),
            json!({"name": "nul-value", "command": "/bin/agent", "env": [{"name": "TOKEN", "value": "bad\u{0}value"}]}),
            // The profile object shape goes through the same validation.
            json!({"name": "object-equals", "command": "/bin/agent", "env": {"BAD=NAME": "x"}}),
            json!({"name": "object-nul-name", "command": "/bin/agent", "env": {"BAD\u{0}NAME": "x"}}),
            json!({"name": "object-nul-value", "command": "/bin/agent", "env": {"TOKEN": "bad\u{0}value"}}),
        ] {
            let error = McpServer::from_value(&invalid)
                .expect_err("malformed environment entries must be refused");
            assert_eq!(error.code, "agent_mcp_server_invalid", "{error}");
        }
    }
}