Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Setup creates Pi's mcp.json when it doesn't exist yet (#268)

BFlat8071522b3d95
diff --git a/crates/setup/src/plan.rs b/crates/setup/src/plan.rs
index e819c26d..d3dd89fd 100644
--- a/crates/setup/src/plan.rs
+++ b/crates/setup/src/plan.rs
@@ -148,7 +148,8 @@ pub fn inspect_client(c: &Client, command: &[String]) -> Action {
return a;
}
};
- let base = text.clone().unwrap_or_default();
+ // A missing file is created; only an existing empty one is invalid where empty is (#238).
+ let base = text.clone().unwrap_or_else(|| if c.empty_is_invalid { "{}".into() } else { String::new() });
let existing = match config::read_entry(c, &base) {
Ok(e) => e,
Err(e) => {
@@ -169,7 +170,7 @@ pub fn inspect_client(c: &Client, command: &[String]) -> Action {
}
match config::set_entry(c, &base, &desired) {
Ok(new) => {
- a.preview = preview(&base, &new);
+ a.preview = preview(text.as_deref().unwrap_or_default(), &new);
a.action = if text.is_some() { ActionKind::Update } else { ActionKind::Create };
a.backup = text.as_ref().map(|_| backup_path(&c.config));
a.old_text = text;
diff --git a/crates/setup/src/tests.rs b/crates/setup/src/tests.rs
index 6f41ae81..7f4dced9 100644
--- a/crates/setup/src/tests.rs
+++ b/crates/setup/src/tests.rs
@@ -144,6 +144,22 @@ fn malformed_config_blocks_plan_and_uninstall() {
assert!(read(h.join(".cursor/mcp.json")).contains("craft-anything-cli"));
}
+/// #238: Pi's folder exists but its mcp.json doesn't yet; setup creates it. An empty file is
+/// still refused.
+#[test]
+fn pi_config_is_created_when_missing() {
+ let fx = fixture();
+ let pi = fx.host.home.join(".pi/agent/mcp.json");
+ std::fs::create_dir_all(pi.parent().unwrap()).unwrap();
+ let p = plan(&fx.host, &req(&fx, &["pi"])).unwrap();
+ assert!(!p.blocked, "{p:?}");
+ assert_eq!(apply(&p).status, "configured");
+ let v: serde_json::Value = serde_json::from_str(&read(&pi)).unwrap();
+ assert_eq!(v["mcpServers"]["craft-anything"]["args"][0], "mcp");
+ std::fs::write(&pi, "").unwrap();
+ assert!(plan(&fx.host, &req(&fx, &["pi"])).unwrap().blocked);
+}
+
#[test]
fn strict_json_client_rejects_comments() {
let fx = fixture();