Skip to content

Commit 2b600d3

Browse files
authored
fix(agent): mcp command producing malformed mcp.json (#2345)
1 parent d504231 commit 2b600d3

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

crates/chat-cli/src/cli/agent/mcp_config.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,20 @@ impl McpServerConfig {
3232
}
3333

3434
pub async fn save_to_file(&self, os: &Os, path: impl AsRef<Path>) -> eyre::Result<()> {
35-
let json = serde_json::to_string_pretty(self)?;
35+
let json = self.to_non_transparent_json_pretty()?;
3636
os.fs.write(path.as_ref(), json).await?;
3737
Ok(())
3838
}
39+
40+
/// Because we had annotated [McpServerConfig] with transparent, when writing the config alone
41+
/// to its legacy location (as opposed to writing it along with its agent config), we would
42+
/// need to call this function to stringify it otherwise we would be writing only the inner
43+
/// hashmap.
44+
fn to_non_transparent_json_pretty(&self) -> eyre::Result<String> {
45+
let transparent_json = serde_json::to_value(self)?;
46+
let non_transparent_json = serde_json::json!({
47+
"mcpServers": transparent_json
48+
});
49+
Ok(serde_json::to_string_pretty(&non_transparent_json)?)
50+
}
3951
}

crates/chat-cli/src/cli/mcp.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ impl AddArgs {
159159
}))?;
160160

161161
mcp_servers.mcp_servers.insert(self.name.clone(), tool);
162-
let json = serde_json::to_string_pretty(&mcp_servers)?;
163-
os.fs.write(&global_config_path, json).await?;
162+
mcp_servers.save_to_file(os, &global_config_path).await?;
164163
writeln!(
165164
output,
166165
"✓ Added MCP server '{}' to global config in {}\n",
@@ -220,8 +219,7 @@ impl RemoveArgs {
220219

221220
match config.mcp_servers.remove(&self.name) {
222221
Some(_) => {
223-
let json = serde_json::to_string_pretty(&config)?;
224-
os.fs.write(&global_config_path, json).await?;
222+
config.save_to_file(os, &global_config_path).await?;
225223
writeln!(
226224
output,
227225
"\n✓ Removed MCP server '{}' from global config (path {})\n",

0 commit comments

Comments
 (0)