Skip to content

Commit 9614cdb

Browse files
committed
fix: preserve peer public API
1 parent dd7799a commit 9614cdb

3 files changed

Lines changed: 38 additions & 28 deletions

File tree

crates/rmcp/src/service.rs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ pub trait ServiceRole: std::fmt::Debug + Send + Sync + 'static + Copy + Clone {
124124
type PeerNot: TryInto<CancelledNotification, Error = Self::PeerNot>
125125
+ From<CancelledNotification>
126126
+ TransferObject
127-
+ CancelledNotificationReference
128127
+ GetMeta<Metadata = NotificationMetaObject>
129128
+ GetExtensions;
130129
type InitializeError;
@@ -133,6 +132,10 @@ pub trait ServiceRole: std::fmt::Debug + Send + Sync + 'static + Copy + Clone {
133132
type PeerInfo: TransferObject;
134133
#[doc(hidden)]
135134
fn configure_direct_peer(_peer: &Peer<Self>, _info: &Self::Info) {}
135+
#[doc(hidden)]
136+
fn peer_cancelled_params(_notification: &Self::PeerNot) -> Option<&CancelledNotificationParam> {
137+
None
138+
}
136139
}
137140

138141
pub type TxJsonRpcMessage<R> =
@@ -319,31 +322,6 @@ pub trait ProgressNotificationToken {
319322
fn progress_token(&self) -> Option<&ProgressToken>;
320323
}
321324

322-
#[doc(hidden)]
323-
pub trait CancelledNotificationReference {
324-
fn cancelled_params(&self) -> Option<&CancelledNotificationParam>;
325-
}
326-
327-
#[cfg(feature = "server")]
328-
impl CancelledNotificationReference for ClientNotification {
329-
fn cancelled_params(&self) -> Option<&CancelledNotificationParam> {
330-
match self {
331-
ClientNotification::CancelledNotification(notification) => Some(&notification.params),
332-
_ => None,
333-
}
334-
}
335-
}
336-
337-
#[cfg(feature = "client")]
338-
impl CancelledNotificationReference for ServerNotification {
339-
fn cancelled_params(&self) -> Option<&CancelledNotificationParam> {
340-
match self {
341-
ServerNotification::CancelledNotification(notification) => Some(&notification.params),
342-
_ => None,
343-
}
344-
}
345-
}
346-
347325
#[cfg(feature = "server")]
348326
impl ProgressNotificationToken for ClientNotification {
349327
fn progress_token(&self) -> Option<&ProgressToken> {
@@ -565,7 +543,6 @@ pub(crate) struct ClientRequestMetadata {
565543
/// For general purpose, call [`Peer::send_request`] or [`Peer::send_notification`] to send message to remote peer.
566544
///
567545
/// To create a cancellable request, call [`Peer::send_request_with_option`].
568-
#[derive(Clone)]
569546
pub struct Peer<R: ServiceRole> {
570547
tx: mpsc::Sender<PeerSinkMessage<R>>,
571548
request_id_provider: Arc<dyn RequestIdProvider>,
@@ -577,6 +554,25 @@ pub struct Peer<R: ServiceRole> {
577554
subscription_channels: Arc<std::sync::RwLock<SubscriptionChannelMap<R::PeerNot>>>,
578555
}
579556

557+
impl<R> Clone for Peer<R>
558+
where
559+
R: Clone + ServiceRole,
560+
R::PeerInfo: Clone,
561+
{
562+
fn clone(&self) -> Self {
563+
Self {
564+
tx: self.tx.clone(),
565+
request_id_provider: self.request_id_provider.clone(),
566+
progress_token_provider: self.progress_token_provider.clone(),
567+
progress_timeout_watchers: self.progress_timeout_watchers.clone(),
568+
info: self.info.clone(),
569+
client_request_metadata: self.client_request_metadata.clone(),
570+
request_metadata_required: self.request_metadata_required.clone(),
571+
subscription_channels: self.subscription_channels.clone(),
572+
}
573+
}
574+
}
575+
580576
impl<R: ServiceRole> std::fmt::Debug for Peer<R> {
581577
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
582578
f.debug_struct("PeerSink")
@@ -1389,7 +1385,7 @@ where
13891385
})) => {
13901386
tracing::info!(?notification, "received notification");
13911387
let cancellation_request_id =
1392-
if let Some(cancelled) = notification.cancelled_params() {
1388+
if let Some(cancelled) = R::peer_cancelled_params(&notification) {
13931389
let request_id = cancelled.request_id.clone();
13941390
if let Some(request_id) = request_id.as_ref() {
13951391
if R::IS_CLIENT {

crates/rmcp/src/service/client.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ impl ServiceRole for RoleClient {
199199
client_capabilities: info.capabilities.clone(),
200200
});
201201
}
202+
203+
fn peer_cancelled_params(notification: &Self::PeerNot) -> Option<&CancelledNotificationParam> {
204+
match notification {
205+
ServerNotification::CancelledNotification(notification) => Some(&notification.params),
206+
_ => None,
207+
}
208+
}
202209
}
203210

204211
pub type ServerSink = Peer<RoleClient>;

crates/rmcp/src/service/server.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ impl ServiceRole for RoleServer {
4242

4343
type InitializeError = ServerInitializeError;
4444
const IS_CLIENT: bool = false;
45+
46+
fn peer_cancelled_params(notification: &Self::PeerNot) -> Option<&CancelledNotificationParam> {
47+
match notification {
48+
ClientNotification::CancelledNotification(notification) => Some(&notification.params),
49+
_ => None,
50+
}
51+
}
4552
}
4653

4754
/// It represents the error that may occur when serving the server.

0 commit comments

Comments
 (0)