Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

fix(mcp): a line that is not UTF-8 gets a parse error instead of ending the session (#337)

chiarorosa3dd2ee95aebd
diff --git a/src/crates/concat-mcp/src/protocol.rs b/src/crates/concat-mcp/src/protocol.rs
index 97c783c5..3b177b80 100644
--- a/src/crates/concat-mcp/src/protocol.rs
+++ b/src/crates/concat-mcp/src/protocol.rs
@@ -152,11 +152,23 @@ fn read(
) {
let mut input = input;
loop {
- let mut line = String::new();
- match std::io::Read::take(&mut input, MAX_LINE).read_line(&mut line) {
+ let mut bytes = Vec::new();
+ match std::io::Read::take(&mut input, MAX_LINE).read_until(b'\n', &mut bytes) {
Ok(0) | Err(_) => return,
Ok(_) => {}
}
+ // JSON is UTF-8. A line that is not is that line's mistake, not the
+ // end of the session: `read_line` would fail the read itself and
+ // stop the server here without a word.
+ let Ok(line) = String::from_utf8(bytes) else {
+ output.send(&failure(
+ Value::Null,
+ code::PARSE,
+ "not JSON: the line is not UTF-8",
+ None,
+ ));
+ continue;
+ };
if line.trim().is_empty() {
continue;
}
@@ -437,3 +449,63 @@ impl Server {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use concat_api::{ApiError, Request};
+ use std::io::Cursor;
+
+ /// A backend that answers every request with an empty object.
+ struct Idle;
+
+ impl Backend for Idle {
+ fn call(&mut self, _request: Request) -> Result<Value, ApiError> {
+ Ok(json!({}))
+ }
+
+ fn describe(&self) -> Value {
+ json!({ "mode": "test" })
+ }
+ }
+
+ /// A writer whose bytes the test can read after `serve` has returned.
+ #[derive(Clone, Default)]
+ struct Shared(Arc<Mutex<Vec<u8>>>);
+
+ impl Write for Shared {
+ fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
+ self.0.lock().expect("lock").extend_from_slice(bytes);
+ Ok(bytes.len())
+ }
+
+ fn flush(&mut self) -> std::io::Result<()> {
+ Ok(())
+ }
+ }
+
+ /// Serves `input` to its end and returns every line written.
+ fn serve_bytes(input: &[u8]) -> Vec<Value> {
+ let written = Shared::default();
+ serve(Cursor::new(input.to_vec()), written.clone(), Box::new(Idle));
+ let bytes = written.0.lock().expect("lock").clone();
+ String::from_utf8(bytes)
+ .expect("the server writes UTF-8")
+ .lines()
+ .map(|line| serde_json::from_str(line).expect("each line is JSON"))
+ .collect()
+ }
+
+ #[test]
+ fn a_line_that_is_not_utf8_is_refused_and_the_session_goes_on() {
+ let mut input = b"\xff\xfe not text\n".to_vec();
+ input.extend_from_slice(br#"{"jsonrpc":"2.0","id":1,"method":"ping"}"#);
+ input.push(b'\n');
+ let replies = serve_bytes(&input);
+ assert_eq!(replies.len(), 2, "{replies:?}");
+ assert_eq!(replies[0]["error"]["code"], code::PARSE);
+ assert_eq!(replies[0]["id"], Value::Null);
+ assert_eq!(replies[1]["id"], 1);
+ assert!(replies[1]["result"].is_object());
+ }
+}