Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

MCP: exported files replace the target atomically (#436)

aiongg1a8df25fddb6
diff --git a/crates/mcp/src/headless.rs b/crates/mcp/src/headless.rs
index aea3e8763..eea81ddd3 100644
--- a/crates/mcp/src/headless.rs
+++ b/crates/mcp/src/headless.rs
@@ -201,7 +201,7 @@ impl Headless {
let png = img.to_png();
match s(p, "path") {
Some(path) => {
- std::fs::write(path, &png).map_err(|e| format!("write {path}: {e}"))?;
+ designcraft_engine::atomic::write_atomic(std::path::Path::new(path), &png).map_err(|e| format!("write {path}: {e}"))?;
Ok(json!({"path": path, "width": img.width, "height": img.height}))
}
None => Ok(json!({"width": img.width, "height": img.height, "pngBase64": designcraft_engine::cmd::base64_encode(&png)})),
@@ -221,7 +221,7 @@ impl Headless {
.ok_or_else(|| format!("no page {page}"))?;
let lower = path.to_ascii_lowercase();
let bytes = if lower.ends_with(".jpg") || lower.ends_with(".jpeg") { img.to_jpeg(90) } else { img.to_png() };
- std::fs::write(path, &bytes).map_err(|e| format!("write {path}: {e}"))?;
+ designcraft_engine::atomic::write_atomic(std::path::Path::new(path), &bytes).map_err(|e| format!("write {path}: {e}"))?;
Ok(json!({"path": path, "width": img.width, "height": img.height}))
}
}
diff --git a/crates/mcp/src/tools.rs b/crates/mcp/src/tools.rs
index 802f615dc..c51bac8ed 100644
--- a/crates/mcp/src/tools.rs
+++ b/crates/mcp/src/tools.rs
@@ -479,7 +479,7 @@ fn render_page(b: &mut dyn Backend, a: &Args) -> Result<ToolResult, String> {
let path = a.get("path").and_then(Value::as_str);
if let Some(path) = path {
let png = designcraft_engine::cmd::base64_decode(&b64);
- std::fs::write(path, png).map_err(|e| format!("write {path}: {e}"))?;
+ designcraft_engine::atomic::write_atomic(std::path::Path::new(path), &png).map_err(|e| format!("write {path}: {e}"))?;
}
Ok(ToolResult::image(b64, &json!({"width": r.get("width"), "height": r.get("height"), "path": path})))
}