Merge pull request #2603 from dotCooCoo/feat/jsonrpc-single-handler

craft-jsonrpc: one handler can take both requests and notifications
This commit is contained in:
BFlat authored and GitHub committed 2026-11-26 10:04:27 +07:00
commit e2a0739dd4
3 files changed
+124 -18

No files matched your search

+4
View File
@@ -18,6 +18,10 @@ which discourages fractional values. Set `integer_ids_only` and clear `allow_nul
protocols such as MCP that require integer or string IDs. A legacy server can retain its previous
null-ID behavior independently. An omitted ID is a notification: it calls the separate notification
handler and gets no reply.
`process_with` and `process_legacy_with` take one value that implements `Handler` in place of the
two closures. Its `request` and `notification` methods both take `&mut self`, so a server can keep
one session that requests and notifications both update, in message order, with each
notification's `params`.
Malformed batch entries receive an invalid-request error; an empty batch gets one error response;
a batch containing only notifications has no response. Invalid parameter shapes receive an
invalid-params error for requests and no reply for notifications. The handler decides whether
+60 -17
View File
@@ -2,7 +2,8 @@
//!
//! Parse a bounded line with [`parse_json`], then pass the value to [`process`]. A host may
//! inspect the parsed value first for a legacy protocol. `process` validates envelopes, dispatches
//! requests and notifications separately, and constructs single or batch replies. The host owns
//! requests and notifications separately, and constructs single or batch replies. [`process_with`]
//! does the same with one [`Handler`] for both requests and notifications. The host owns
//! the transport, method semantics and response-size policy.
#![forbid(unsafe_code)]
@@ -183,21 +184,54 @@ fn invalid_request() -> Value {
error_response(Value::Null, RpcError::new(-32600, "Invalid Request"))
}
fn one<F, N>(value: &Value, limits: Limits, on_request: &mut F, on_notification: &mut N) -> Option<Value>
/// Receives the requests and notifications of one message for [`process_with`] and
/// [`process_legacy_with`]. Both methods take `&mut self`, so one handler can update the same
/// state for requests and notifications, in message order, without a `RefCell`.
pub trait Handler {
/// Handle a request with an ID. The result or error becomes its response.
fn request(&mut self, request: Request<'_>) -> Result<Value, RpcError>;
/// Handle a notification, including its `params`. A notification never receives a response.
fn notification(&mut self, request: Request<'_>);
}
impl<H: Handler + ?Sized> Handler for &mut H {
fn request(&mut self, request: Request<'_>) -> Result<Value, RpcError> {
(**self).request(request)
}
fn notification(&mut self, request: Request<'_>) {
(**self).notification(request);
}
}
struct Callbacks<F, N> {
on_request: F,
on_notification: N,
}
impl<F, N> Handler for Callbacks<F, N>
where
F: FnMut(Request<'_>) -> Result<Value, RpcError>,
N: FnMut(Request<'_>),
{
fn request(&mut self, request: Request<'_>) -> Result<Value, RpcError> {
(self.on_request)(request)
}
fn notification(&mut self, request: Request<'_>) {
(self.on_notification)(request);
}
}
fn one<H: Handler>(value: &Value, limits: Limits, handler: &mut H) -> Option<Value> {
let Some(request) = request(value, limits) else { return Some(invalid_request()) };
let id = request.id;
if request.params.is_some_and(|v| !v.is_object() && !v.is_array()) {
return id.map(|id| error_response(id.clone(), RpcError::new(-32602, "Invalid params")));
}
let Some(id) = id else {
on_notification(request);
handler.notification(request);
return None;
};
Some(match on_request(request) {
Some(match handler.request(request) {
Ok(result) => json!({"jsonrpc": "2.0", "id": id, "result": result}),
Err(error) => error_response(id.clone(), error),
})
@@ -206,34 +240,43 @@ where
/// Process a parsed JSON-RPC message. Notifications never receive a response. Invalid batch
/// entries produce individual errors; an empty batch produces one invalid-request response.
/// A batch over the limit returns [`ParseError::BatchTooLarge`] without invoking either callback.
pub fn process<F, N>(value: &Value, limits: Limits, mut on_request: F, mut on_notification: N) -> Result<Option<Value>, ParseError>
pub fn process<F, N>(value: &Value, limits: Limits, on_request: F, on_notification: N) -> Result<Option<Value>, ParseError>
where
F: FnMut(Request<'_>) -> Result<Value, RpcError>,
N: FnMut(Request<'_>),
{
let Some(batch) = value.as_array() else { return Ok(one(value, limits, &mut on_request, &mut on_notification)) };
process_with(value, limits, Callbacks { on_request, on_notification })
}
/// Process a parsed JSON-RPC message as [`process`] does, dispatching requests and
/// notifications to one [`Handler`]. A batch over the limit returns
/// [`ParseError::BatchTooLarge`] without calling the handler.
pub fn process_with<H: Handler>(value: &Value, limits: Limits, mut handler: H) -> Result<Option<Value>, ParseError> {
let Some(batch) = value.as_array() else { return Ok(one(value, limits, &mut handler)) };
if batch.len() > limits.max_batch_items {
return Err(ParseError::BatchTooLarge);
}
if batch.is_empty() {
return Ok(Some(invalid_request()));
}
let responses: Vec<_> = batch.iter().filter_map(|v| one(v, limits, &mut on_request, &mut on_notification)).collect();
let responses: Vec<_> = batch.iter().filter_map(|v| one(v, limits, &mut handler)).collect();
Ok((!responses.is_empty()).then_some(Value::Array(responses)))
}
/// Process a permissive legacy JSON-RPC envelope. This mode accepts arbitrary IDs and
/// parameter shapes, ignores response envelopes, and retains legacy diagnostic messages.
/// The caller supplies dispatch and notification callbacks, and should bound transport input.
pub fn process_legacy<F, N>(value: &Value, mut on_request: F, mut on_notification: N) -> Option<Value>
pub fn process_legacy<F, N>(value: &Value, on_request: F, on_notification: N) -> Option<Value>
where
F: FnMut(Request<'_>) -> Result<Value, RpcError>,
N: FnMut(Request<'_>),
{
fn one<F, N>(value: &Value, on_request: &mut F, on_notification: &mut N) -> Option<Value>
where
F: FnMut(Request<'_>) -> Result<Value, RpcError>,
N: FnMut(Request<'_>),
{
process_legacy_with(value, Callbacks { on_request, on_notification })
}
/// Process a permissive legacy JSON-RPC envelope as [`process_legacy`] does, dispatching
/// requests and notifications to one [`Handler`].
pub fn process_legacy_with<H: Handler>(value: &Value, mut handler: H) -> Option<Value> {
fn one<H: Handler>(value: &Value, handler: &mut H) -> Option<Value> {
let Some(object) = value.as_object() else { return Some(error_response(Value::Null, RpcError::new(-32600, "message must be an object"))) };
let id = object.get("id");
let Some(method) = object.get("method").and_then(Value::as_str) else {
@@ -244,10 +287,10 @@ where
};
let request = Request { id, method, params: object.get("params") };
let Some(id) = id else {
on_notification(request);
handler.notification(request);
return None;
};
Some(match on_request(request) {
Some(match handler.request(request) {
Ok(result) => json!({"jsonrpc": "2.0", "id": id, "result": result}),
Err(error) => error_response(id.clone(), error),
})
@@ -256,10 +299,10 @@ where
if batch.is_empty() {
return Some(error_response(Value::Null, RpcError::new(-32600, "empty batch")));
}
let replies: Vec<_> = batch.iter().filter_map(|v| one(v, &mut on_request, &mut on_notification)).collect();
let replies: Vec<_> = batch.iter().filter_map(|v| one(v, &mut handler)).collect();
(!replies.is_empty()).then_some(Value::Array(replies))
} else {
one(value, &mut on_request, &mut on_notification)
one(value, &mut handler)
}
}
+60 -1
View File
@@ -1,6 +1,65 @@
use craft_jsonrpc::{Limits, ParseError, RpcError, parse_error_response, parse_json, process};
use craft_jsonrpc::{Handler, Limits, ParseError, Request, RpcError, parse_error_response, parse_json, process, process_legacy_with, process_with};
use serde_json::{Value, json};
#[derive(Default)]
struct Session {
log: Vec<String>,
level: Value,
}
impl Handler for Session {
fn request(&mut self, request: Request<'_>) -> Result<Value, RpcError> {
self.log.push(format!("request {}", request.method));
match request.method {
"level" => Ok(self.level.clone()),
_ => Err(RpcError::new(-32601, "Method not found")),
}
}
fn notification(&mut self, request: Request<'_>) {
self.log.push(format!("notification {}", request.method));
if let Some(level) = request.params.and_then(|params| params.get("level")) {
self.level = level.clone();
}
}
}
#[test]
fn one_handler_shares_state_between_requests_and_notifications_in_order() {
let input = json!([
{"jsonrpc":"2.0", "id":1, "method":"level"},
{"jsonrpc":"2.0", "method":"set", "params":{"level":"debug"}},
{"jsonrpc":"2.0", "id":2, "method":"level"},
{"jsonrpc":"2.0", "id":3, "method":"missing"}
]);
let mut session = Session::default();
let result = process_with(&input, Limits::default(), &mut session).unwrap().unwrap();
assert_eq!(
result,
json!([
{"jsonrpc":"2.0", "id":1, "result":null},
{"jsonrpc":"2.0", "id":2, "result":"debug"},
{"jsonrpc":"2.0", "id":3, "error":{"code":-32601,"message":"Method not found"}}
])
);
assert_eq!(session.log, ["request level", "notification set", "request level", "request missing"]);
let notification = json!({"jsonrpc":"2.0", "method":"set", "params":{"level":"trace"}});
assert!(process_with(&notification, Limits::default(), &mut session).unwrap().is_none());
assert_eq!(session.level, "trace");
let limits = Limits { max_batch_items: 1, ..Limits::default() };
let mut untouched = Session::default();
assert_eq!(process_with(&input, limits, &mut untouched), Err(ParseError::BatchTooLarge));
assert!(untouched.log.is_empty());
}
#[test]
fn one_handler_serves_legacy_envelopes() {
let input = json!([{"method":"set", "params":{"level":"info"}}, {"id":"x", "method":"level"}, {"result":1}]);
let mut session = Session::default();
let result = process_legacy_with(&input, &mut session).unwrap();
assert_eq!(result, json!([{"jsonrpc":"2.0", "id":"x", "result":"info"}]));
assert_eq!(session.log, ["notification set", "request level"]);
}
#[test]
fn batches_preserve_ids_order_and_suppress_notification_replies() {
let input = json!([