Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions crates/buzz-acp/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//! Config file (TOML) for complex subscription rules.

use std::collections::{HashMap, HashSet};

use serde::{Deserialize, Serialize};
use std::path::PathBuf;

use clap::Parser;
Expand All @@ -28,6 +30,9 @@ pub(crate) const DEFAULT_IDLE_TIMEOUT_SECS: u64 = 900;

#[derive(Debug, Error)]
pub enum ConfigError {
#[error("invalid BUZZ_ACP_MCP_SERVERS: {0}")]
McpServers(String),

#[error("failed to parse nostr keys: {0}")]
KeyParse(#[from] nostr::key::Error),

Expand Down Expand Up @@ -184,6 +189,34 @@ pub struct ModelsArgs {
pub json: bool,
}

/// User-configured stdio MCP server received from desktop via
/// `BUZZ_ACP_MCP_SERVERS`. The desktop resolves `enabled` as local layering
/// metadata before serializing this transport shape.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ConfiguredMcpServer {
pub name: String,
pub command: String,
#[serde(default)]
pub args: Vec<String>,
#[serde(default)]
pub env: Vec<ConfiguredMcpEnvVar>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ConfiguredMcpEnvVar {
pub name: String,
pub value: String,
}

pub fn parse_configured_mcp_servers(
raw: Option<&str>,
) -> Result<Vec<ConfiguredMcpServer>, ConfigError> {
let Some(raw) = raw.filter(|value| !value.trim().is_empty()) else {
return Ok(Vec::new());
};
serde_json::from_str(raw).map_err(|error| ConfigError::McpServers(error.to_string()))
}

#[derive(Debug, Parser)]
#[command(
name = "buzz-acp",
Expand Down Expand Up @@ -443,6 +476,8 @@ pub struct Config {
pub agent_command: String,
pub agent_args: Vec<String>,
pub mcp_command: String,
/// Desktop-resolved MCP servers, appended after the built-in Buzz server.
pub configured_mcp_servers: Vec<ConfiguredMcpServer>,
pub idle_timeout_secs: u64,
pub max_turn_duration_secs: u64,
pub agents: u32,
Expand Down Expand Up @@ -948,12 +983,16 @@ impl Config {

validate_multiple_event_handling(args.multiple_event_handling, args.dedup)?;

let configured_mcp_servers =
parse_configured_mcp_servers(std::env::var("BUZZ_ACP_MCP_SERVERS").ok().as_deref())?;

let config = Config {
keys,
relay_url: args.relay_url,
agent_command,
agent_args,
mcp_command: args.mcp_command,
configured_mcp_servers,
idle_timeout_secs,
max_turn_duration_secs,
agents: args.agents,
Expand Down Expand Up @@ -1325,6 +1364,7 @@ mod tests {
agent_command: "goose".into(),
agent_args: vec!["acp".into()],
mcp_command: "".into(),
configured_mcp_servers: vec![],
idle_timeout_secs: DEFAULT_IDLE_TIMEOUT_SECS,
max_turn_duration_secs: 3600,
agents: 1,
Expand Down Expand Up @@ -1730,6 +1770,28 @@ mod tests {
assert!(f.kinds.is_none(), "wildcard should propagate");
}

#[test]
fn parse_configured_mcp_servers_accepts_json_transport() {
let servers = parse_configured_mcp_servers(Some(
r#"[{"name":"github","command":"npx","args":["github-mcp"],"env":[{"name":"TOKEN","value":"secret"}]}]"#,
))
.unwrap();

assert_eq!(servers.len(), 1);
assert_eq!(servers[0].name, "github");
assert_eq!(servers[0].args, vec!["github-mcp"]);
assert_eq!(servers[0].env[0].name, "TOKEN");
assert_eq!(
serde_json::to_value(&servers).unwrap()[0]["enabled"],
serde_json::Value::Null
);
}

#[test]
fn parse_configured_mcp_servers_rejects_invalid_json() {
assert!(parse_configured_mcp_servers(Some("not-json")).is_err());
}

#[test]
fn test_config_mode_no_matching_rules_empty_result() {
let config = test_config(SubscribeMode::Config);
Expand Down
154 changes: 110 additions & 44 deletions crates/buzz-acp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3361,48 +3361,71 @@ async fn run_models(args: ModelsArgs) -> Result<()> {
}

fn build_mcp_servers(config: &Config) -> Vec<McpServer> {
if config.mcp_command.is_empty() {
return vec![];
}
vec![McpServer {
name: std::path::Path::new(&config.mcp_command)
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("mcp")
.to_string(),
command: config.mcp_command.clone(),
args: vec![],
env: {
let mut env = vec![
EnvVar {
name: "BUZZ_RELAY_URL".into(),
value: config.relay_url.clone(),
},
EnvVar {
name: "BUZZ_PRIVATE_KEY".into(),
// bech32 encoding of a valid secret key is infallible.
// Panic here is correct: injecting a bogus secret would cause
// delayed, hard-to-diagnose agent failures downstream.
value: config
.keys
.secret_key()
.to_bech32()
.expect("secret key bech32 encoding should never fail"),
},
];
// Forward BUZZ_AUTH_TAG (NIP-OA owner attestation credential)
// so the MCP server can attach it to every signed event.
if let Ok(auth_tag) = std::env::var("BUZZ_AUTH_TAG") {
if !auth_tag.is_empty() {
env.push(EnvVar {
name: "BUZZ_AUTH_TAG".into(),
value: auth_tag,
});
let mut servers = Vec::new();
if !config.mcp_command.is_empty() {
servers.push(McpServer {
name: std::path::Path::new(&config.mcp_command)
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("mcp")
.to_string(),
command: config.mcp_command.clone(),
args: vec![],
env: {
let mut env = vec![
EnvVar {
name: "BUZZ_RELAY_URL".into(),
value: config.relay_url.clone(),
},
EnvVar {
name: "BUZZ_PRIVATE_KEY".into(),
// bech32 encoding of a valid secret key is infallible.
// Panic here is correct: injecting a bogus secret would cause
// delayed, hard-to-diagnose agent failures downstream.
value: config
.keys
.secret_key()
.to_bech32()
.expect("secret key bech32 encoding should never fail"),
},
];
// Forward BUZZ_AUTH_TAG (NIP-OA owner attestation credential)
// so the MCP server can attach it to every signed event.
if let Ok(auth_tag) = std::env::var("BUZZ_AUTH_TAG") {
if !auth_tag.is_empty() {
env.push(EnvVar {
name: "BUZZ_AUTH_TAG".into(),
value: auth_tag,
});
}
}
}
env
},
}]
env
},
});
}
servers.extend(
config
.configured_mcp_servers
.iter()
.map(configured_mcp_server),
);
servers
}

fn configured_mcp_server(server: &config::ConfiguredMcpServer) -> McpServer {
McpServer {
name: server.name.clone(),
command: server.command.clone(),
args: server.args.clone(),
env: server
.env
.iter()
.map(|variable| EnvVar {
name: variable.name.clone(),
value: variable.value.clone(),
})
.collect(),
}
}

#[cfg(test)]
Expand Down Expand Up @@ -3834,6 +3857,7 @@ mod build_mcp_servers_tests {
agent_command: "goose".into(),
agent_args: vec!["acp".into()],
mcp_command: "test-mcp-server".into(),
configured_mcp_servers: vec![],
idle_timeout_secs: config::DEFAULT_IDLE_TIMEOUT_SECS,
max_turn_duration_secs: 3600,
agents: 1,
Expand Down Expand Up @@ -3869,6 +3893,28 @@ mod build_mcp_servers_tests {
}
}

#[test]
fn build_mcp_servers_appends_configured_servers_after_builtin() {
let mut config = test_config();
config.configured_mcp_servers = vec![config::ConfiguredMcpServer {
name: "github".into(),
command: "npx".into(),
args: vec!["github-mcp".into()],
env: vec![config::ConfiguredMcpEnvVar {
name: "TOKEN".into(),
value: "secret".into(),
}],
}];

let servers = build_mcp_servers(&config);
assert_eq!(servers.len(), 2);
assert_eq!(servers[0].name, "test-mcp-server");
assert_eq!(servers[1].name, "github");
assert_eq!(servers[1].command, "npx");
assert_eq!(servers[1].args, vec!["github-mcp"]);
assert_eq!(servers[1].env[0].name, "TOKEN");
}

#[test]
fn session_new_mcp_server_has_required_fields() {
let config = test_config();
Expand Down Expand Up @@ -3919,13 +3965,32 @@ mod build_mcp_servers_tests {
}

#[test]
fn empty_mcp_command_returns_no_servers() {
fn empty_mcp_command_omits_builtin_but_includes_user_servers() {
let mut config = test_config();
config.mcp_command = "".into();
config.configured_mcp_servers = vec![config::ConfiguredMcpServer {
name: "github".into(),
command: "npx".into(),
args: vec![],
env: vec![],
}];

let servers = build_mcp_servers(&config);
// The built-in slot is omitted (mcp_command is empty), but user-configured
// servers are always included — they do not depend on the built-in server.
assert_eq!(servers.len(), 1);
assert_eq!(servers[0].name, "github");
}

#[test]
fn empty_mcp_command_with_no_user_servers_returns_empty() {
let mut config = test_config();
config.mcp_command = "".into();
config.configured_mcp_servers = vec![];

assert!(
servers.is_empty(),
"empty mcp_command should produce no MCP servers"
build_mcp_servers(&config).is_empty(),
"no built-in and no user servers should yield an empty list"
);
}

Expand Down Expand Up @@ -3994,6 +4059,7 @@ mod error_outcome_emission_tests {
agent_command: "true".into(),
agent_args: vec![],
mcp_command: "test-mcp-server".into(),
configured_mcp_servers: vec![],
idle_timeout_secs: config::DEFAULT_IDLE_TIMEOUT_SECS,
max_turn_duration_secs: 3600,
agents: 1,
Expand Down
Loading
Loading