use schemars::JsonSchema;
use super::request::method_marker;
method_marker!(
ToolsListChangedMethod, ToolsListChanged, "notifications/tools/list_changed"
);
method_marker!(
ResourcesListChangedMethod,
ResourcesListChanged,
"notifications/resources/list_changed"
);
method_marker!(
CliRequestMethod, CliRequest, "notifications/objectiveai/cli_request"
);
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, JsonSchema)]
#[serde(untagged)]
#[schemars(rename = "mcp.ServerNotificationMethod")]
pub enum ServerNotificationMethod {
ToolsListChanged(ToolsListChangedMethod),
ResourcesListChanged(ResourcesListChangedMethod),
CliRequest(CliRequestMethod),
Other(String),
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, JsonSchema)]
#[serde(untagged)]
#[schemars(rename = "mcp.JsonRpcServerNotification")]
pub enum JsonRpcServerNotification {
ToolsListChanged {
jsonrpc: String,
method: ToolsListChangedMethod,
},
ResourcesListChanged {
jsonrpc: String,
method: ResourcesListChangedMethod,
},
CliRequest {
jsonrpc: String,
method: CliRequestMethod,
params: CliRequestParams,
},
Fallback {
jsonrpc: String,
method: ServerNotificationMethod,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
params: Option<serde_json::Value>,
},
}
impl JsonRpcServerNotification {
pub fn tools_list_changed() -> Self {
JsonRpcServerNotification::ToolsListChanged {
jsonrpc: "2.0".to_string(),
method: ToolsListChangedMethod::ToolsListChanged,
}
}
pub fn resources_list_changed() -> Self {
JsonRpcServerNotification::ResourcesListChanged {
jsonrpc: "2.0".to_string(),
method: ResourcesListChangedMethod::ResourcesListChanged,
}
}
pub fn cli_request(params: CliRequestParams) -> Self {
JsonRpcServerNotification::CliRequest {
jsonrpc: "2.0".to_string(),
method: CliRequestMethod::CliRequest,
params,
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, JsonSchema)]
#[schemars(rename = "mcp.CliRequestParams")]
pub struct CliRequestParams {
pub id: String,
pub request: crate::cli::command::Request,
}
#[cfg(test)]
mod tests {
use super::*;
fn update_request() -> crate::cli::command::Request {
crate::cli::command::Request::Update(
crate::cli::command::update::Request {
path_type: crate::cli::command::update::Path::Update,
base: crate::cli::command::RequestBase {
jq: None,
python: None,
timeout_seconds: None,
max_tokens: None,
},
},
)
}
#[test]
fn cli_request_deserializes_from_rmcp_custom_wire_shape() {
let frame = serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/objectiveai/cli_request",
"params": {
"id": "42",
"request": serde_json::to_value(update_request()).unwrap(),
},
});
let parsed: JsonRpcServerNotification =
serde_json::from_str(&frame.to_string()).unwrap();
let JsonRpcServerNotification::CliRequest { params, .. } = parsed
else {
panic!("expected CliRequest, got {parsed:?}");
};
assert_eq!(params.id, "42");
assert_eq!(
serde_json::to_value(¶ms.request).unwrap(),
frame["params"]["request"],
);
}
#[test]
fn cli_request_without_params_is_fallback() {
let frame = serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/objectiveai/cli_request",
});
let parsed: JsonRpcServerNotification =
serde_json::from_str(&frame.to_string()).unwrap();
assert!(matches!(
parsed,
JsonRpcServerNotification::Fallback {
method: ServerNotificationMethod::CliRequest(_),
..
},
));
}
#[test]
fn list_changed_frames_ignore_params() {
for (method, want_tools) in [
("notifications/tools/list_changed", true),
("notifications/resources/list_changed", false),
] {
for params in [None, Some(serde_json::json!({"_meta": {"x": 1}}))]
{
let mut frame = serde_json::json!({
"jsonrpc": "2.0",
"method": method,
});
if let Some(p) = params {
frame["params"] = p;
}
let parsed: JsonRpcServerNotification =
serde_json::from_str(&frame.to_string()).unwrap();
match (want_tools, &parsed) {
(
true,
JsonRpcServerNotification::ToolsListChanged { .. },
) => {}
(
false,
JsonRpcServerNotification::ResourcesListChanged {
..
},
) => {}
_ => panic!("wrong variant for {method}: {parsed:?}"),
}
}
}
}
#[test]
fn unknown_method_is_fallback_other() {
let frame = serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/prompts/list_changed",
"params": {"k": "v"},
});
let parsed: JsonRpcServerNotification =
serde_json::from_str(&frame.to_string()).unwrap();
assert!(matches!(
parsed,
JsonRpcServerNotification::Fallback {
method: ServerNotificationMethod::Other(method),
..
} if method == "notifications/prompts/list_changed",
));
}
#[test]
fn round_trip() {
let cli = JsonRpcServerNotification::cli_request(CliRequestParams {
id: "7".to_string(),
request: update_request(),
});
let value = serde_json::to_value(&cli).unwrap();
assert_eq!(value["jsonrpc"], "2.0");
assert_eq!(
value["method"],
"notifications/objectiveai/cli_request"
);
assert_eq!(value["params"]["id"], "7");
let back: JsonRpcServerNotification =
serde_json::from_str(&value.to_string()).unwrap();
assert!(matches!(
back,
JsonRpcServerNotification::CliRequest { params, .. }
if params.id == "7",
));
let tools = serde_json::to_value(
JsonRpcServerNotification::tools_list_changed(),
)
.unwrap();
assert_eq!(
tools,
serde_json::json!({
"jsonrpc": "2.0",
"method": "notifications/tools/list_changed",
}),
);
assert!(matches!(
serde_json::from_str::<JsonRpcServerNotification>(
&tools.to_string()
)
.unwrap(),
JsonRpcServerNotification::ToolsListChanged { .. },
));
}
}