feat: add support for custom client notifications (#556)
MCP servers, particularly ones that offer "experimental" capabilities, may wish to handle custom client notifications that are not part of the standard MCP specification. This change introduces a new `CustomClientNotification` type that allows a server to process such custom notifications. - introduces `CustomClientNotification` to carry arbitrary methods/params while still preserving meta/extensions; wires it into the `ClientNotification` union and `serde` so `params` can be decoded with `params_as` - allows server handlers to receive custom notifications via a new `on_custom_notification` hook - adds integration coverage that sends a custom client notification end-to-end and asserts the server sees the method and payload Test: ```shell cargo test -p rmcp --features client test_custom_client_notification_reaches_server ```
This commit is contained in:
parent
b6dcb282d9
commit
4ef1a163f4
2 changed files with 263 additions and 7 deletions
|
|
@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};
|
|||
use serde_json::Value;
|
||||
|
||||
use super::{
|
||||
ClientNotification, ClientRequest, Extensions, JsonObject, JsonRpcMessage, NumberOrString,
|
||||
ProgressToken, ServerNotification, ServerRequest,
|
||||
ClientNotification, ClientRequest, CustomClientNotification, Extensions, JsonObject,
|
||||
JsonRpcMessage, NumberOrString, ProgressToken, ServerNotification, ServerRequest,
|
||||
};
|
||||
|
||||
pub trait GetMeta {
|
||||
|
|
@ -18,6 +18,26 @@ pub trait GetExtensions {
|
|||
fn extensions_mut(&mut self) -> &mut Extensions;
|
||||
}
|
||||
|
||||
impl GetExtensions for CustomClientNotification {
|
||||
fn extensions(&self) -> &Extensions {
|
||||
&self.extensions
|
||||
}
|
||||
fn extensions_mut(&mut self) -> &mut Extensions {
|
||||
&mut self.extensions
|
||||
}
|
||||
}
|
||||
|
||||
impl GetMeta for CustomClientNotification {
|
||||
fn get_meta_mut(&mut self) -> &mut Meta {
|
||||
self.extensions_mut().get_or_insert_default()
|
||||
}
|
||||
fn get_meta(&self) -> &Meta {
|
||||
self.extensions()
|
||||
.get::<Meta>()
|
||||
.unwrap_or(Meta::static_empty())
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! variant_extension {
|
||||
(
|
||||
$Enum: ident {
|
||||
|
|
@ -84,6 +104,7 @@ variant_extension! {
|
|||
ProgressNotification
|
||||
InitializedNotification
|
||||
RootsListChangedNotification
|
||||
CustomClientNotification
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ use std::borrow::Cow;
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::{
|
||||
Extensions, Meta, Notification, NotificationNoParam, Request, RequestNoParam,
|
||||
RequestOptionalParam,
|
||||
CustomClientNotification, Extensions, Meta, Notification, NotificationNoParam, Request,
|
||||
RequestNoParam, RequestOptionalParam,
|
||||
};
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct WithMeta<'a, P> {
|
||||
|
|
@ -249,6 +249,59 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl Serialize for CustomClientNotification {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
let extensions = &self.extensions;
|
||||
let _meta = extensions.get::<Meta>().map(Cow::Borrowed);
|
||||
let params = self.params.as_ref();
|
||||
|
||||
let params = if _meta.is_some() || params.is_some() {
|
||||
Some(WithMeta {
|
||||
_meta,
|
||||
_rest: &self.params,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
ProxyOptionalParam::serialize(
|
||||
&ProxyOptionalParam {
|
||||
method: &self.method,
|
||||
params,
|
||||
},
|
||||
serializer,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for CustomClientNotification {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let body =
|
||||
ProxyOptionalParam::<'_, _, Option<serde_json::Value>>::deserialize(deserializer)?;
|
||||
let mut params = None;
|
||||
let mut _meta = None;
|
||||
if let Some(body_params) = body.params {
|
||||
params = body_params._rest;
|
||||
_meta = body_params._meta.map(|m| m.into_owned());
|
||||
}
|
||||
let mut extensions = Extensions::new();
|
||||
if let Some(meta) = _meta {
|
||||
extensions.insert(meta);
|
||||
}
|
||||
Ok(CustomClientNotification {
|
||||
extensions,
|
||||
method: body.method,
|
||||
params,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use serde_json::json;
|
||||
|
|
|
|||
Loading…
Reference in a new issue