Refactor MCP config argument handling

This commit is contained in:
Bryan Helmkamp 2026-08-26 09:01:37 -04:00
parent 24165b10f5
commit e5046d8b1b
No known key found for this signature in database
3 changed files with 13 additions and 21 deletions

View file

@ -206,13 +206,8 @@ pub(crate) struct McpConfigArgs {
pub(crate) struct McpInitArgs {
pub(crate) agent: McpAgent,
/// Name of the mcpServers entry; use distinct names to register multiple
/// Fabro servers
#[arg(long, value_name = "NAME", default_value = fabro_mcp_server::SERVER_NAME, value_parser = clap::builder::NonEmptyStringValueParser::new())]
pub(crate) name: String,
#[command(flatten)]
pub(crate) connection: ServerConnectionArgs,
pub(crate) config: McpConfigArgs,
}
#[derive(Debug, Clone, Copy, ValueEnum)]

View file

@ -2,7 +2,9 @@ use std::fmt::Write as _;
use anyhow::{Context as _, Result};
use crate::args::{McpAgent, McpCommand, McpInitArgs, McpNamespace, ServerConnectionArgs};
use crate::args::{
McpAgent, McpCommand, McpConfigArgs, McpInitArgs, McpNamespace, ServerConnectionArgs,
};
use crate::command_context::CommandContext;
use crate::server_client;
@ -12,8 +14,7 @@ pub(crate) async fn dispatch(ns: McpNamespace, base_ctx: &CommandContext) -> Res
fabro_mcp_server::start(server_settings(base_ctx, &args.connection)?).await
}
McpCommand::Config(args) => {
let json =
fabro_mcp_server::config_json(&config_settings(&args.name, &args.connection))?;
let json = fabro_mcp_server::config_json(&config_settings(&args))?;
let _ = write!(base_ctx.printer().stdout_important(), "{json}");
Ok(())
}
@ -60,19 +61,16 @@ fn server_settings(
fn init_settings(args: &McpInitArgs) -> Result<fabro_mcp_server::McpInitSettings> {
Ok(fabro_mcp_server::McpInitSettings {
agent: McpAgentForServer(args.agent).into(),
config: config_settings(&args.name, &args.connection),
config: config_settings(&args.config),
home_dir: home_dir()?,
})
}
fn config_settings(
name: &str,
connection: &ServerConnectionArgs,
) -> fabro_mcp_server::McpConfigSettings {
fn config_settings(args: &McpConfigArgs) -> fabro_mcp_server::McpConfigSettings {
fabro_mcp_server::McpConfigSettings {
name: name.to_string(),
server: connection.target.server.clone(),
storage_dir: connection.storage_dir.clone_path(),
name: args.name.clone(),
server: args.connection.target.server.clone(),
storage_dir: args.connection.storage_dir.clone_path(),
}
}

View file

@ -18,9 +18,8 @@ pub fn config_json(settings: &McpConfigSettings) -> Result<String> {
}
pub fn init_agent(settings: &McpInitSettings) -> Result<()> {
let entry = server_entry(&settings.config);
for path in agent_config_paths(settings.agent, &settings.home_dir) {
merge_server_entry(&path, &settings.config.name, entry.clone())?;
merge_server_entry(&path, &settings.config)?;
}
Ok(())
}
@ -51,7 +50,7 @@ fn start_args(settings: &McpConfigSettings) -> Vec<String> {
args
}
fn merge_server_entry(path: &Path, name: &str, entry: Value) -> Result<()> {
fn merge_server_entry(path: &Path, settings: &McpConfigSettings) -> Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("failed to create {}", parent.display()))?;
@ -78,7 +77,7 @@ fn merge_server_entry(path: &Path, name: &str, entry: Value) -> Result<()> {
path.display()
)
})?;
servers_object.insert(name.to_string(), entry);
servers_object.insert(settings.name.clone(), server_entry(settings));
let rendered = serde_json::to_string_pretty(&root)
.map(|json| format!("{json}\n"))