diff --git a/crates/jsonrpc/README.md b/crates/jsonrpc/README.md index 8711c25ff4..79f425cbc6 100644 --- a/crates/jsonrpc/README.md +++ b/crates/jsonrpc/README.md @@ -26,6 +26,8 @@ object or array parameters are valid for each method. The caller may inspect the bounded parsed value before `process` to support an existing legacy protocol. Parse failures and limit failures are returned explicitly so each transport can choose its error and connection policy; `parse_error_response` builds the JSON-RPC parse error. +`ParseError::error_response` builds a response for any of these failures: `-32700` for invalid +JSON and `-32600` for a message or batch over its limit, each with a null ID. The envelope behavior is extracted from StencilCraft's JSON-lines MCP/control server. App-specific method routing, legacy control requests, and MCP lifecycle policy stay with the app. diff --git a/crates/jsonrpc/src/lib.rs b/crates/jsonrpc/src/lib.rs index 97105b4531..a50d58e983 100644 --- a/crates/jsonrpc/src/lib.rs +++ b/crates/jsonrpc/src/lib.rs @@ -51,6 +51,28 @@ impl std::fmt::Display for ParseError { } impl std::error::Error for ParseError {} +impl ParseError { + /// The JSON-RPC error for this failure: `-32700` for input that is not JSON, and `-32600` for + /// a message or batch over its limit, which the server did not read as a request. + pub fn rpc_error(self) -> RpcError { + match self { + Self::InvalidJson => RpcError::new(-32700, "Parse error"), + Self::MessageTooLarge | Self::BatchTooLarge => RpcError::new(-32600, self.to_string()), + } + } + + /// Build the error response for this failure. The ID is null because no request ID was read. + pub fn error_response(self) -> Value { + error_response(Value::Null, self.rpc_error()) + } +} + +impl From for RpcError { + fn from(error: ParseError) -> Self { + error.rpc_error() + } +} + /// Error reading a newline-delimited message. After `TooLong`, close or otherwise discard the /// remaining input line before reading another message. #[derive(Debug)] diff --git a/crates/jsonrpc/tests/server.rs b/crates/jsonrpc/tests/server.rs index a9186f4ae8..03d54387b1 100644 --- a/crates/jsonrpc/tests/server.rs +++ b/crates/jsonrpc/tests/server.rs @@ -113,6 +113,23 @@ fn parse_and_batch_limits_apply_before_dispatch() { assert_eq!(process(&one, limits, |_| Ok(json!({})), |_| {}).unwrap().unwrap().as_array().unwrap().len(), 1); } +#[test] +fn parse_failures_map_to_error_responses() { + assert_eq!(ParseError::InvalidJson.error_response(), parse_error_response()); + for error in [ParseError::MessageTooLarge, ParseError::BatchTooLarge] { + let reply = error.error_response(); + assert_eq!(reply["jsonrpc"], "2.0"); + assert_eq!(reply["id"], Value::Null); + assert_eq!(reply["error"]["code"], -32600); + assert_eq!(reply["error"]["message"], error.to_string()); + assert_eq!(RpcError::from(error), error.rpc_error()); + } + let limits = Limits { max_batch_items: 0, ..Limits::default() }; + let input = json!([{"jsonrpc":"2.0", "id":1, "method":"ping"}]); + let error = process(&input, limits, |_| Ok(json!({})), |_| {}).unwrap_err(); + assert_eq!(error.error_response()["error"]["code"], -32600); +} + #[test] fn invalid_utf8_and_excessive_nesting_are_rejected() { assert_eq!(parse_json(&[0xff], Limits::default()), Err(ParseError::InvalidJson));