Skip to content

Commit 0035ccf

Browse files
factorydroidFactory Bot
authored andcommitted
fix(mcp): add --force flag to 'mcp add' command for overwriting existing servers
Fixes bounty issue #1620 The 'mcp add' command now supports a --force flag that allows overwriting an existing MCP server configuration. When --force is specified and the server already exists, the old configuration is removed before adding the new one. Without --force, the error message now suggests using --force as an alternative to manually removing the server first.
1 parent 5579873 commit 0035ccf

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

cortex-cli/src/mcp_cmd.rs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ pub struct AddArgs {
308308
/// Name for the MCP server configuration.
309309
pub name: String,
310310

311+
/// Force overwrite if server already exists.
312+
#[arg(short, long)]
313+
pub force: bool,
314+
311315
#[command(flatten)]
312316
pub transport_args: AddMcpTransportArgs,
313317
}
@@ -492,6 +496,34 @@ fn get_mcp_server(name: &str) -> Result<Option<toml::Value>> {
492496
Ok(servers.get(name).cloned())
493497
}
494498

499+
/// Remove a specific MCP server from config (used by --force).
500+
fn remove_mcp_server_from_config(name: &str) -> Result<()> {
501+
let cortex_home = get_cortex_home_result()?;
502+
let config_path = cortex_home.join("config.toml");
503+
504+
if !config_path.exists() {
505+
return Ok(());
506+
}
507+
508+
let content = std::fs::read_to_string(&config_path)
509+
.with_context(|| format!("failed to read config: {}", config_path.display()))?;
510+
511+
let mut config: toml::Value =
512+
toml::from_str(&content).with_context(|| "failed to parse config")?;
513+
514+
if let Some(mcp_servers) = config.get_mut("mcp_servers").and_then(|v| v.as_table_mut()) {
515+
mcp_servers.remove(name);
516+
}
517+
518+
let new_content =
519+
toml::to_string_pretty(&config).with_context(|| "failed to serialize config")?;
520+
521+
std::fs::write(&config_path, new_content)
522+
.with_context(|| format!("failed to write config: {}", config_path.display()))?;
523+
524+
Ok(())
525+
}
526+
495527
async fn run_list(args: ListArgs) -> Result<()> {
496528
let servers = get_mcp_servers()?;
497529

@@ -662,18 +694,25 @@ async fn run_get(args: GetArgs) -> Result<()> {
662694
async fn run_add(args: AddArgs) -> Result<()> {
663695
let AddArgs {
664696
name,
697+
force,
665698
transport_args,
666699
} = args;
667700

668701
validate_server_name(&name)?;
669702

670703
// Check if server already exists
671704
if get_mcp_server(&name)?.is_some() {
672-
bail!(
673-
"MCP server '{}' already exists. Use 'Cortex mcp remove {}' first.",
674-
name,
675-
name
676-
);
705+
if force {
706+
// Remove existing server before adding the new one
707+
remove_mcp_server_from_config(&name)?;
708+
println!("Replacing existing MCP server '{name}'...");
709+
} else {
710+
bail!(
711+
"MCP server '{}' already exists. Use --force to overwrite or 'Cortex mcp remove {}' first.",
712+
name,
713+
name
714+
);
715+
}
677716
}
678717

679718
let cortex_home = get_cortex_home_result()?;

0 commit comments

Comments
 (0)