use schemars::JsonSchema;
use super::request::method_marker;
use super::RequestId;
method_marker!(
InitializedMethod, Initialized, "notifications/initialized"
);
method_marker!(
CancelledMethod, Cancelled, "notifications/cancelled"
);
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, JsonSchema)]
#[serde(untagged)]
#[schemars(rename = "mcp.ClientNotificationMethod")]
pub enum ClientNotificationMethod {
Initialized(InitializedMethod),
Cancelled(CancelledMethod),
Other(String),
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, JsonSchema)]
#[serde(untagged)]
#[schemars(rename = "mcp.JsonRpcClientNotification")]
pub enum JsonRpcClientNotification {
Initialized {
jsonrpc: String,
method: InitializedMethod,
},
Cancelled {
jsonrpc: String,
method: CancelledMethod,
params: CancelledNotificationParams,
},
Fallback {
jsonrpc: String,
method: ClientNotificationMethod,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
params: Option<serde_json::Value>,
},
}
impl JsonRpcClientNotification {
pub fn initialized() -> Self {
JsonRpcClientNotification::Initialized {
jsonrpc: "2.0".to_string(),
method: InitializedMethod::Initialized,
}
}
pub fn cancelled(params: CancelledNotificationParams) -> Self {
JsonRpcClientNotification::Cancelled {
jsonrpc: "2.0".to_string(),
method: CancelledMethod::Cancelled,
params,
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
#[schemars(rename = "mcp.CancelledNotificationParams")]
pub struct CancelledNotificationParams {
pub request_id: RequestId,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
pub reason: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn initialized_deserializes_and_ignores_params() {
for frame in [
serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/initialized",
}),
serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/initialized",
"params": {"_meta": {"x": 1}},
}),
] {
let parsed: JsonRpcClientNotification =
serde_json::from_str(&frame.to_string()).unwrap();
assert!(matches!(
parsed,
JsonRpcClientNotification::Initialized { .. },
));
}
}
#[test]
fn cancelled_deserializes_string_and_number_ids() {
let frame = serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/cancelled",
"params": {"requestId": "abc", "reason": "user aborted"},
});
let parsed: JsonRpcClientNotification =
serde_json::from_str(&frame.to_string()).unwrap();
let JsonRpcClientNotification::Cancelled { params, .. } = parsed
else {
panic!("expected Cancelled");
};
assert_eq!(params.request_id, RequestId::String("abc".to_string()));
assert_eq!(params.reason.as_deref(), Some("user aborted"));
let frame = serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/cancelled",
"params": {"requestId": 42},
});
let parsed: JsonRpcClientNotification =
serde_json::from_str(&frame.to_string()).unwrap();
let JsonRpcClientNotification::Cancelled { params, .. } = parsed
else {
panic!("expected Cancelled");
};
assert_eq!(params.request_id, RequestId::Number(42.into()));
assert_eq!(params.reason, None);
}
#[test]
fn cancelled_with_bad_params_is_fallback() {
for frame in [
serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/cancelled",
}),
serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/cancelled",
"params": {"requestId": {"bad": true}},
}),
] {
let parsed: JsonRpcClientNotification =
serde_json::from_str(&frame.to_string()).unwrap();
assert!(matches!(
parsed,
JsonRpcClientNotification::Fallback {
method: ClientNotificationMethod::Cancelled(_),
..
},
));
}
}
#[test]
fn unknown_method_is_fallback_other() {
let frame = serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/roots/list_changed",
});
let parsed: JsonRpcClientNotification =
serde_json::from_str(&frame.to_string()).unwrap();
assert!(matches!(
parsed,
JsonRpcClientNotification::Fallback {
method: ClientNotificationMethod::Other(method),
..
} if method == "notifications/roots/list_changed",
));
}
#[test]
fn round_trip() {
let cancelled = JsonRpcClientNotification::cancelled(
CancelledNotificationParams {
request_id: RequestId::Number(7.into()),
reason: None,
},
);
let value = serde_json::to_value(&cancelled).unwrap();
assert_eq!(
value,
serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/cancelled",
"params": {"requestId": 7},
}),
);
assert!(matches!(
serde_json::from_str::<JsonRpcClientNotification>(
&value.to_string()
)
.unwrap(),
JsonRpcClientNotification::Cancelled { params, .. }
if params.request_id == RequestId::Number(7.into()),
));
let initialized = serde_json::to_value(
JsonRpcClientNotification::initialized(),
)
.unwrap();
assert_eq!(
initialized,
serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/initialized",
}),
);
}
}