Skip to content

Commit a9f0a40

Browse files
author
=
committed
remove the Message in model and support batch messages
1 parent 59bfb6e commit a9f0a40

4 files changed

Lines changed: 172 additions & 137 deletions

File tree

crates/rmcp/src/model.rs

Lines changed: 81 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ impl Default for ProtocolVersion {
9595
}
9696
}
9797
impl ProtocolVersion {
98-
pub const LATEST: Self = Self(Cow::Borrowed("2024-11-05"));
99-
pub const V_2024_11_05: Self = Self::LATEST;
98+
pub const V_2025_03_26: Self = Self(Cow::Borrowed("2025-03-26"));
99+
pub const V_2024_11_05: Self = Self(Cow::Borrowed("2024-11-05"));
100+
pub const LATEST: Self = Self::V_2025_03_26;
100101
}
101102

102103
impl Serialize for ProtocolVersion {
@@ -117,11 +118,13 @@ impl<'de> Deserialize<'de> for ProtocolVersion {
117118
#[allow(clippy::single_match)]
118119
match s.as_str() {
119120
"2024-11-05" => return Ok(ProtocolVersion::V_2024_11_05),
121+
"2024-03-26" => return Ok(ProtocolVersion::V_2025_03_26),
120122
_ => {}
121123
}
122124
Ok(ProtocolVersion(Cow::Owned(s)))
123125
}
124126
}
127+
125128
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
126129
pub enum NumberOrString {
127130
Number(u32),
@@ -296,95 +299,111 @@ impl ErrorData {
296299

297300
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
298301
#[serde(untagged)]
299-
pub enum JsonRpcMessage<Req = Request, Resp = DefaultResponse, Noti = Notification> {
302+
pub enum JsonRpcBatchRequestItem<Req, Not> {
300303
Request(JsonRpcRequest<Req>),
304+
Notification(JsonRpcNotification<Not>),
305+
}
306+
307+
impl<Req, Not> JsonRpcBatchRequestItem<Req, Not> {
308+
pub fn into_non_batch_message<Resp>(self) -> JsonRpcMessage<Req, Resp, Not> {
309+
match self {
310+
JsonRpcBatchRequestItem::Request(r) => JsonRpcMessage::Request(r),
311+
JsonRpcBatchRequestItem::Notification(n) => JsonRpcMessage::Notification(n),
312+
}
313+
}
314+
}
315+
316+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
317+
#[serde(untagged)]
318+
pub enum JsonRpcBatchResponseItem<Resp> {
301319
Response(JsonRpcResponse<Resp>),
302-
Notification(JsonRpcNotification<Noti>),
303320
Error(JsonRpcError),
304321
}
305322

306-
impl<Req, Resp, Noti> JsonRpcMessage<Req, Resp, Noti> {
307-
pub fn into_message(self) -> Message<Req, Resp, Noti> {
323+
impl<Resp> JsonRpcBatchResponseItem<Resp> {
324+
pub fn into_non_batch_message<Req, Not>(self) -> JsonRpcMessage<Req, Resp, Not> {
308325
match self {
309-
JsonRpcMessage::Request(JsonRpcRequest { id, request, .. }) => {
310-
Message::Request(request, id)
311-
}
312-
JsonRpcMessage::Response(JsonRpcResponse { id, result, .. }) => {
313-
Message::Response(result, id)
314-
}
315-
JsonRpcMessage::Notification(JsonRpcNotification { notification, .. }) => {
316-
Message::Notification(notification)
317-
}
318-
JsonRpcMessage::Error(JsonRpcError { id, error, .. }) => Message::Error(error, id),
326+
JsonRpcBatchResponseItem::Response(r) => JsonRpcMessage::Response(r),
327+
JsonRpcBatchResponseItem::Error(e) => JsonRpcMessage::Error(e),
319328
}
320329
}
321330
}
322331

323-
#[derive(Debug, Clone, PartialEq)]
324-
pub enum Message<Req = Request, Resp = DefaultResponse, Noti = Notification> {
325-
Request(Req, RequestId),
326-
Response(Resp, RequestId),
327-
Error(ErrorData, RequestId),
328-
Notification(Noti),
332+
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
333+
#[serde(untagged)]
334+
pub enum JsonRpcMessage<Req = Request, Resp = DefaultResponse, Noti = Notification> {
335+
Request(JsonRpcRequest<Req>),
336+
Response(JsonRpcResponse<Resp>),
337+
Notification(JsonRpcNotification<Noti>),
338+
BatchRequest(Vec<JsonRpcBatchRequestItem<Req, Noti>>),
339+
BatchResponse(Vec<JsonRpcBatchResponseItem<Resp>>),
340+
Error(JsonRpcError),
329341
}
330342

331-
impl<Req, Resp, Noti> Message<Req, Resp, Noti> {
332-
pub fn into_notification(self) -> Option<Noti> {
343+
impl<Req, Resp, Not> JsonRpcMessage<Req, Resp, Not> {
344+
#[inline]
345+
pub const fn request(request: Req, id: RequestId) -> Self {
346+
JsonRpcMessage::Request(JsonRpcRequest {
347+
jsonrpc: JsonRpcVersion2_0,
348+
id,
349+
request,
350+
})
351+
}
352+
#[inline]
353+
pub const fn response(response: Resp, id: RequestId) -> Self {
354+
JsonRpcMessage::Response(JsonRpcResponse {
355+
jsonrpc: JsonRpcVersion2_0,
356+
id,
357+
result: response,
358+
})
359+
}
360+
#[inline]
361+
pub const fn error(error: ErrorData, id: RequestId) -> Self {
362+
JsonRpcMessage::Error(JsonRpcError {
363+
jsonrpc: JsonRpcVersion2_0,
364+
id,
365+
error,
366+
})
367+
}
368+
#[inline]
369+
pub const fn notification(notification: Not) -> Self {
370+
JsonRpcMessage::Notification(JsonRpcNotification {
371+
jsonrpc: JsonRpcVersion2_0,
372+
notification,
373+
})
374+
}
375+
pub fn into_request(self) -> Option<(Req, RequestId)> {
333376
match self {
334-
Message::Notification(notification) => Some(notification),
377+
JsonRpcMessage::Request(r) => Some((r.request, r.id)),
335378
_ => None,
336379
}
337380
}
338381
pub fn into_response(self) -> Option<(Resp, RequestId)> {
339382
match self {
340-
Message::Response(result, id) => Some((result, id)),
383+
JsonRpcMessage::Response(r) => Some((r.result, r.id)),
341384
_ => None,
342385
}
343386
}
344-
pub fn into_request(self) -> Option<(Req, RequestId)> {
387+
pub fn into_notification(self) -> Option<Not> {
345388
match self {
346-
Message::Request(request, id) => Some((request, id)),
389+
JsonRpcMessage::Notification(n) => Some(n.notification),
347390
_ => None,
348391
}
349392
}
350393
pub fn into_error(self) -> Option<(ErrorData, RequestId)> {
351394
match self {
352-
Message::Error(error, id) => Some((error, id)),
395+
JsonRpcMessage::Error(e) => Some((e.error, e.id)),
353396
_ => None,
354397
}
355398
}
356399
pub fn into_result(self) -> Option<(Result<Resp, ErrorData>, RequestId)> {
357400
match self {
358-
Message::Response(result, id) => Some((Ok(result), id)),
359-
Message::Error(error, id) => Some((Err(error), id)),
401+
JsonRpcMessage::Response(r) => Some((Ok(r.result), r.id)),
402+
JsonRpcMessage::Error(e) => Some((Err(e.error), e.id)),
403+
360404
_ => None,
361405
}
362406
}
363-
pub fn into_json_rpc_message(self) -> JsonRpcMessage<Req, Resp, Noti> {
364-
match self {
365-
Message::Request(request, id) => JsonRpcMessage::Request(JsonRpcRequest {
366-
jsonrpc: JsonRpcVersion2_0,
367-
id,
368-
request,
369-
}),
370-
Message::Response(result, id) => JsonRpcMessage::Response(JsonRpcResponse {
371-
jsonrpc: JsonRpcVersion2_0,
372-
id,
373-
result,
374-
}),
375-
Message::Error(error, id) => JsonRpcMessage::Error(JsonRpcError {
376-
jsonrpc: JsonRpcVersion2_0,
377-
id,
378-
error,
379-
}),
380-
Message::Notification(notification) => {
381-
JsonRpcMessage::Notification(JsonRpcNotification {
382-
jsonrpc: JsonRpcVersion2_0,
383-
notification,
384-
})
385-
}
386-
}
387-
}
388407
}
389408

390409
/// # Empty result
@@ -885,7 +904,6 @@ impl ClientResult {
885904
}
886905

887906
pub type ClientJsonRpcMessage = JsonRpcMessage<ClientRequest, ClientResult, ClientNotification>;
888-
pub type ClientMessage = Message<ClientRequest, ClientResult, ClientNotification>;
889907

890908
ts_union!(
891909
export type ServerRequest =
@@ -927,7 +945,6 @@ impl ServerResult {
927945
}
928946

929947
pub type ServerJsonRpcMessage = JsonRpcMessage<ServerRequest, ServerResult, ServerNotification>;
930-
pub type ServerMessage = Message<ServerRequest, ServerResult, ServerNotification>;
931948

932949
impl TryInto<CancelledNotification> for ServerNotification {
933950
type Error = ServerNotification;
@@ -975,12 +992,14 @@ mod tests {
975992
});
976993
let message: ClientJsonRpcMessage =
977994
serde_json::from_value(raw.clone()).expect("invalid notification");
978-
let message = message.into_message();
979995
match &message {
980-
ClientMessage::Notification(ClientNotification::InitializedNotification(_n)) => {}
996+
ClientJsonRpcMessage::Notification(JsonRpcNotification {
997+
notification: ClientNotification::InitializedNotification(_n),
998+
..
999+
}) => {}
9811000
_ => panic!("Expected Notification"),
9821001
}
983-
let json = serde_json::to_value(message.into_json_rpc_message()).expect("valid json");
1002+
let json = serde_json::to_value(message).expect("valid json");
9841003
assert_eq!(json, raw);
9851004
}
9861005

@@ -1057,10 +1076,7 @@ mod tests {
10571076
});
10581077
let request: ClientJsonRpcMessage =
10591078
serde_json::from_value(request.clone()).expect("invalid request");
1060-
let (request, id) = request
1061-
.into_message()
1062-
.into_request()
1063-
.expect("expect request");
1079+
let (request, id) = request.into_request().expect("should be a request");
10641080
assert_eq!(id, RequestId::Number(1));
10651081
match request {
10661082
ClientRequest::InitializeRequest(Request {
@@ -1083,7 +1099,6 @@ mod tests {
10831099
serde_json::from_value(raw_response_json.clone()).expect("invalid response");
10841100
let (response, id) = server_response
10851101
.clone()
1086-
.into_message()
10871102
.into_response()
10881103
.expect("expect response");
10891104
assert_eq!(id, RequestId::Number(1));

0 commit comments

Comments
 (0)