automation: share JSON-RPC envelope framing via craft-jsonrpc (#3967)

* automation: share JSON-RPC envelope framing via craft-jsonrpc

* chore: update Cargo.lock for craft-jsonrpc
This commit is contained in:
Dylan M. Taylor
2026-11-19 18:34:19 -05:00
committed by GitHub
parent 7be2b0dc27
commit cb45fb0e34
4 changed files with 22 additions and 27 deletions
Generated
+9
View File
@@ -1368,6 +1368,14 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-jsonrpc"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=bc186ca719958fcce3b6ee24794277eabed14609#bc186ca719958fcce3b6ee24794277eabed14609"
dependencies = [
"serde_json",
]
[[package]]
name = "craft-lottie"
version = "0.1.0"
@@ -1990,6 +1998,7 @@ dependencies = [
name = "effectcraft-automation"
version = "0.24.1"
dependencies = [
"craft-jsonrpc",
"craft-mime",
"effectcraft-engine",
"effectcraft-host",
+1
View File
@@ -15,6 +15,7 @@ craft-timeline = { git = "https://github.com/storytold/craft-libs", rev = "0d6dd
craft-mime = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "808730f49b9637b7909fe2ae56f9d63fdb100cae", version = "=0.1.0", default-features = false, features = ["read"] }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-jsonrpc = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "bc186ca719958fcce3b6ee24794277eabed14609" }
craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "5a7a63a21dd03dd1a2bc6c308a3a6ce818caae87" }
+1
View File
@@ -10,6 +10,7 @@ rust-version.workspace = true
workspace = true
[dependencies]
craft-jsonrpc = { workspace = true }
craft-mime = { workspace = true }
effectcraft-engine = { workspace = true }
serde_json = { workspace = true }
+11 -27
View File
@@ -17,7 +17,6 @@ const INSTRUCTIONS: &str = "EffectCraft is an After Effects-class motion graphic
// JSON-RPC error codes.
const PARSE_ERROR: i64 = -32700;
const INVALID_REQUEST: i64 = -32600;
const METHOD_NOT_FOUND: i64 = -32601;
const INVALID_PARAMS: i64 = -32602;
const INTERNAL_ERROR: i64 = -32603;
@@ -61,31 +60,16 @@ impl McpServer {
/// Handle one decoded message (request, notification or batch); `None` when no reply is due.
pub fn handle(&mut self, msg: &Value) -> Option<Value> {
if let Value::Array(batch) = msg {
if batch.is_empty() {
return Some(error(Value::Null, INVALID_REQUEST, "empty batch"));
}
let out: Vec<Value> = batch.iter().filter_map(|m| self.handle(m)).collect();
return (!out.is_empty()).then_some(Value::Array(out));
}
let Some(method) = msg.get("method").and_then(Value::as_str) else {
// A response from the client (we send no requests) or garbage.
return msg
.get("id")
.filter(|_| msg.get("result").is_none() && msg.get("error").is_none())
.map(|id| error(id.clone(), INVALID_REQUEST, "missing `method`"));
};
let Some(id) = msg.get("id").cloned() else {
return None; // notification (notifications/initialized, notifications/cancelled, ...)
};
let params = msg.get("params").cloned().unwrap_or(json!({}));
Some(match self.request(method, &params) {
Ok(mut result) => {
modern_result_fields(method, &params, &mut result);
json!({"jsonrpc": "2.0", "id": id, "result": result})
}
Err((code, m)) => error(id, code, &m),
})
craft_jsonrpc::process_legacy(
msg,
|req| {
let params = req.params.cloned().unwrap_or_else(|| json!({}));
let mut result = self.request(req.method, &params).map_err(|(code, message)| craft_jsonrpc::RpcError::new(code, message))?;
modern_result_fields(req.method, &params, &mut result);
Ok(result)
},
|_| {},
)
}
/// Handle one line of input; `None` when no reply is due.
@@ -304,5 +288,5 @@ pub(crate) fn modern_result_fields(method: &str, params: &Value, result: &mut Va
}
fn error(id: Value, code: i64, message: &str) -> Value {
json!({"jsonrpc": "2.0", "id": id, "error": {"code": code, "message": message}})
craft_jsonrpc::error_response(id, craft_jsonrpc::RpcError::new(code, message))
}