CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 29m12s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 1s
CI / Deploy (pull_request) Skipped
179 lines
6.7 KiB
Rust
179 lines
6.7 KiB
Rust
//! SDK serialization for the stream-local list-change notifications.
|
|
|
|
use rmcp::model::{
|
|
JsonRpcNotification, JsonRpcVersion2_0, NotificationMetaObject, PromptListChangedNotification,
|
|
ResourceListChangedNotification, SubscriptionFilter, SubscriptionsAcknowledgedNotification,
|
|
SubscriptionsAcknowledgedNotificationParams, ToolListChangedNotification,
|
|
};
|
|
use serde_json::{Map, Value};
|
|
|
|
use super::{PROMPT_LIST_CHANGED, RESOURCE_LIST_CHANGED, Signals, TOOL_LIST_CHANGED};
|
|
|
|
fn subscription_meta(subscription_id: &Value) -> NotificationMetaObject {
|
|
let mut meta = Map::new();
|
|
// Keep the JSON identity: SDK RequestId narrows numbers to i64.
|
|
meta.insert(
|
|
super::SUBSCRIPTION_ID_KEY.to_owned(),
|
|
subscription_id.clone(),
|
|
);
|
|
NotificationMetaObject::from(meta)
|
|
}
|
|
|
|
pub(super) fn signal_frame(method: &str, subscription_id: Option<&Value>) -> String {
|
|
let meta = subscription_id.map(subscription_meta);
|
|
let frame = match method {
|
|
RESOURCE_LIST_CHANGED => {
|
|
let mut notification = ResourceListChangedNotification::default();
|
|
if let Some(meta) = meta {
|
|
notification.extensions.insert(meta);
|
|
}
|
|
serde_json::to_string(&JsonRpcNotification {
|
|
jsonrpc: JsonRpcVersion2_0,
|
|
notification,
|
|
})
|
|
}
|
|
PROMPT_LIST_CHANGED => {
|
|
let mut notification = PromptListChangedNotification::default();
|
|
if let Some(meta) = meta {
|
|
notification.extensions.insert(meta);
|
|
}
|
|
serde_json::to_string(&JsonRpcNotification {
|
|
jsonrpc: JsonRpcVersion2_0,
|
|
notification,
|
|
})
|
|
}
|
|
TOOL_LIST_CHANGED => {
|
|
let mut notification = ToolListChangedNotification::default();
|
|
if let Some(meta) = meta {
|
|
notification.extensions.insert(meta);
|
|
}
|
|
serde_json::to_string(&JsonRpcNotification {
|
|
jsonrpc: JsonRpcVersion2_0,
|
|
notification,
|
|
})
|
|
}
|
|
_ => unreachable!("list-change methods are a closed set"),
|
|
}
|
|
.expect("SDK list-change notification with JSON metadata is serializable");
|
|
#[cfg(test)]
|
|
crate::app::dispatch_observer::sdk_signal_notification_serialized();
|
|
frame
|
|
}
|
|
|
|
pub(super) fn acknowledged_frame(subscription_id: &Value, accepted: Signals) -> String {
|
|
let mut notifications = SubscriptionFilter::new();
|
|
notifications.resources_list_changed = accepted.resources.then_some(true);
|
|
notifications.prompts_list_changed = accepted.prompts.then_some(true);
|
|
notifications.tools_list_changed = accepted.tools.then_some(true);
|
|
let params = SubscriptionsAcknowledgedNotificationParams::new(notifications)
|
|
.with_meta(subscription_meta(subscription_id));
|
|
let notification = SubscriptionsAcknowledgedNotification::new(params);
|
|
let frame = serde_json::to_string(&JsonRpcNotification {
|
|
jsonrpc: JsonRpcVersion2_0,
|
|
notification,
|
|
})
|
|
.expect("SDK subscription acknowledgment with JSON metadata is serializable");
|
|
#[cfg(test)]
|
|
crate::app::dispatch_observer::sdk_subscriptions_acknowledged_serialized();
|
|
frame
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use serde_json::json;
|
|
|
|
use super::*;
|
|
|
|
const METHODS: [&str; 3] = [
|
|
RESOURCE_LIST_CHANGED,
|
|
PROMPT_LIST_CHANGED,
|
|
TOOL_LIST_CHANGED,
|
|
];
|
|
|
|
#[test]
|
|
fn legacy_signals_omit_params() {
|
|
for method in METHODS {
|
|
let frame: Value = serde_json::from_str(&signal_frame(method, None)).unwrap();
|
|
assert_eq!(frame, json!({"jsonrpc": "2.0", "method": method}));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn modern_signals_and_acknowledgments_preserve_json_identity() {
|
|
for encoded in [
|
|
r#""subscription-\"quoted""#,
|
|
"-7",
|
|
"18446744073709551615",
|
|
"1.0",
|
|
"0.12345678901234567890123456789",
|
|
"1.234567890123456789e+25",
|
|
] {
|
|
let id: Value = serde_json::from_str(encoded).unwrap();
|
|
let mut frames = METHODS
|
|
.map(|method| (signal_frame(method, Some(&id)), method))
|
|
.to_vec();
|
|
frames.push((
|
|
acknowledged_frame(&id, Signals::default()),
|
|
super::super::SUBSCRIPTIONS_ACKNOWLEDGED,
|
|
));
|
|
for (encoded_frame, method) in frames {
|
|
let frame: Value = serde_json::from_str(&encoded_frame).unwrap();
|
|
assert_eq!(frame["jsonrpc"], "2.0");
|
|
assert_eq!(frame["method"], method);
|
|
assert_eq!(
|
|
frame["params"]["_meta"][super::super::SUBSCRIPTION_ID_KEY],
|
|
id
|
|
);
|
|
assert_eq!(
|
|
frame["params"]["_meta"][super::super::SUBSCRIPTION_ID_KEY].to_string(),
|
|
id.to_string(),
|
|
);
|
|
assert_eq!(frame["params"]["_meta"].as_object().unwrap().len(), 1);
|
|
assert!(frame.get("id").is_none());
|
|
if method != super::super::SUBSCRIPTIONS_ACKNOWLEDGED {
|
|
assert_eq!(frame["params"].as_object().unwrap().len(), 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn acknowledgment_reports_only_accepted_true_flags_and_never_uris() {
|
|
for resources in [false, true] {
|
|
for prompts in [false, true] {
|
|
for tools in [false, true] {
|
|
let accepted = Signals {
|
|
resources,
|
|
prompts,
|
|
tools,
|
|
};
|
|
let frame: Value =
|
|
serde_json::from_str(&acknowledged_frame(&json!("listen"), accepted))
|
|
.unwrap();
|
|
let mut expected = Map::new();
|
|
for (enabled, name) in [
|
|
(resources, "resourcesListChanged"),
|
|
(prompts, "promptsListChanged"),
|
|
(tools, "toolsListChanged"),
|
|
] {
|
|
if enabled {
|
|
expected.insert(name.to_owned(), Value::Bool(true));
|
|
}
|
|
}
|
|
assert_eq!(
|
|
frame,
|
|
json!({
|
|
"jsonrpc": "2.0",
|
|
"method": super::super::SUBSCRIPTIONS_ACKNOWLEDGED,
|
|
"params": {
|
|
"_meta": {super::super::SUBSCRIPTION_ID_KEY: "listen"},
|
|
"notifications": expected,
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|