MCP: execute modern subscriptions through SDK listen handler
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 40m16s
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
CI / UI Checks (pull_request) Successful in 11s
CI / Rust Checks (pull_request) Successful in 40m16s
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
This commit is contained in:
1 parent
d01bdf019c
commit
0755e6e8e8
13 files changed
+1003
-46
No files matched your search
@@ -176,12 +176,18 @@ impl<'de> Deserialize<'de> for ListenParams {
|
||||
/// The bounded input contract of a `subscriptions/listen` filter.
|
||||
pub(crate) enum FilterOutcome {
|
||||
/// The list-change subset this server honours for the stream.
|
||||
// Compatibility projection retained for the existing filter controls; SDK owns runtime acceptance.
|
||||
#[cfg_attr(not(test), allow(dead_code))]
|
||||
Accepted(Signals),
|
||||
/// One safe detail: no URI, name or value the client sent is echoed back.
|
||||
Invalid(&'static str),
|
||||
}
|
||||
|
||||
impl ListenFilter {
|
||||
pub(crate) fn into_sdk(self) -> SubscriptionFilter {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Validate the bounded inputs and keep only the honoured types.
|
||||
///
|
||||
/// `resourceSubscriptions` is not implemented in the current scope, which is what the
|
||||
@@ -269,6 +275,7 @@ pub(crate) enum Wire {
|
||||
/// `2025-11-25`: a bare server notification without parameters.
|
||||
Legacy,
|
||||
/// `2026-07-28`: every message carries the subscription id of its `subscriptions/listen` request.
|
||||
#[cfg(test)]
|
||||
Modern(Value),
|
||||
}
|
||||
|
||||
@@ -285,6 +292,7 @@ impl Wire {
|
||||
}
|
||||
let subscription_id = match self {
|
||||
Wire::Legacy => None,
|
||||
#[cfg(test)]
|
||||
Wire::Modern(subscription_id) => Some(subscription_id),
|
||||
};
|
||||
frames.push(sdk::signal_frame(method, subscription_id));
|
||||
@@ -294,6 +302,7 @@ impl Wire {
|
||||
}
|
||||
|
||||
/// The first modern frame: the accepted subset, before any list-change notification of the stream.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn acknowledged_frame(subscription_id: &Value, accepted: Signals) -> String {
|
||||
sdk::acknowledged_frame(subscription_id, accepted)
|
||||
}
|
||||
@@ -382,13 +391,17 @@ pub(crate) type Reader = Box<dyn FnMut() -> ReadFuture + Send>;
|
||||
///
|
||||
/// The first read is the baseline taken **before** the acknowledgment is written; it is never reset
|
||||
/// afterwards, so a publication between the baseline and the acknowledgment is still delivered.
|
||||
pub(crate) fn spawn(state: Arc<AppState>, watch: Watch) -> mpsc::Receiver<String> {
|
||||
pub(crate) fn reader(state: Arc<AppState>, watch: &Watch) -> Reader {
|
||||
let scope = (watch.workspace_slug.clone(), watch.agent_slug.clone());
|
||||
let reader: Reader = Box::new(move || {
|
||||
Box::new(move || {
|
||||
let state = Arc::clone(&state);
|
||||
let scope = scope.clone();
|
||||
Box::pin(async move { read(&state, &scope).await })
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn spawn(state: Arc<AppState>, watch: Watch) -> mpsc::Receiver<String> {
|
||||
let reader = reader(state, &watch);
|
||||
let (sender, receiver) = mpsc::channel(SIGNAL_QUEUE_CAPACITY);
|
||||
#[cfg(not(test))]
|
||||
tokio::spawn(run(watch, sender, reader));
|
||||
@@ -408,10 +421,57 @@ pub(crate) fn spawn(state: Arc<AppState>, watch: Watch) -> mpsc::Receiver<String
|
||||
receiver
|
||||
}
|
||||
|
||||
pub(crate) enum Delivery {
|
||||
Frames(mpsc::Sender<String>),
|
||||
Subscription(rmcp::service::SubscriptionSink),
|
||||
}
|
||||
|
||||
impl Delivery {
|
||||
async fn send(&self, wire: &Wire, signals: Signals) -> Result<(), rmcp::ErrorData> {
|
||||
match self {
|
||||
Self::Frames(sender) => {
|
||||
for frame in wire.signal_frames(signals) {
|
||||
let _ = sender.try_send(frame);
|
||||
}
|
||||
}
|
||||
Self::Subscription(sink) => {
|
||||
if signals.resources {
|
||||
sink.notify_resource_list_changed()
|
||||
.await
|
||||
.map_err(send_error)?;
|
||||
}
|
||||
if signals.prompts {
|
||||
sink.notify_prompt_list_changed()
|
||||
.await
|
||||
.map_err(send_error)?;
|
||||
}
|
||||
if signals.tools {
|
||||
sink.notify_tool_list_changed().await.map_err(send_error)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn send_error(_: rmcp::service::SubscriptionSendError) -> rmcp::ErrorData {
|
||||
rmcp::ErrorData::internal_error("subscription output unavailable", None)
|
||||
}
|
||||
|
||||
async fn run(watch: Watch, sender: mpsc::Sender<String>, mut read: Reader) {
|
||||
let mut baseline = match read().await {
|
||||
let initial = read().await;
|
||||
let _ = run_delivery(watch, Delivery::Frames(sender), read, initial).await;
|
||||
}
|
||||
|
||||
pub(crate) async fn run_delivery(
|
||||
watch: Watch,
|
||||
delivery: Delivery,
|
||||
mut read: Reader,
|
||||
initial: Result<CatalogFingerprint, ReadError>,
|
||||
) -> Result<(), rmcp::ErrorData> {
|
||||
let mut baseline = match initial {
|
||||
Ok(fingerprint) => Some(fingerprint),
|
||||
Err(ReadError::Gone) => return,
|
||||
Err(ReadError::Gone) => return Ok(()),
|
||||
Err(ReadError::Retry) => {
|
||||
observe_failure();
|
||||
None
|
||||
@@ -419,36 +479,33 @@ async fn run(watch: Watch, sender: mpsc::Sender<String>, mut read: Reader) {
|
||||
};
|
||||
|
||||
loop {
|
||||
if !still_owned(&watch) || sender.is_closed() {
|
||||
return;
|
||||
if !still_owned(&watch)
|
||||
|| matches!(&delivery, Delivery::Frames(sender) if sender.is_closed())
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
if watch
|
||||
.deadline
|
||||
.is_some_and(|deadline| Instant::now() >= deadline)
|
||||
{
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
tokio::time::sleep(POLL_INTERVAL).await;
|
||||
if !still_owned(&watch) {
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
match read().await {
|
||||
Ok(fingerprint) => {
|
||||
if let Some(previous) = baseline.as_ref() {
|
||||
let changes = fingerprint.changes_since(previous).intersect(watch.signals);
|
||||
for frame in watch.wire.signal_frames(changes) {
|
||||
// A slow reader coalesces: the newest frame is dropped instead of blocking
|
||||
// the producer or growing a queue, and the client recovers through the
|
||||
// authoritative list methods.
|
||||
let _ = sender.try_send(frame);
|
||||
}
|
||||
delivery.send(&watch.wire, changes).await?;
|
||||
}
|
||||
baseline = Some(fingerprint);
|
||||
}
|
||||
// The Agent is no longer published: fail closed by ending the stream instead of serving a
|
||||
// catalog nobody may read any more.
|
||||
Err(ReadError::Gone) => return,
|
||||
Err(ReadError::Gone) => return Ok(()),
|
||||
// The committed catalog is untouched: the failure is observable and this tick is skipped.
|
||||
Err(_) => observe_failure(),
|
||||
}
|
||||
@@ -499,15 +556,39 @@ pub(crate) fn frames_stream<G: Send + 'static>(
|
||||
guard: G,
|
||||
) -> impl stream::Stream<Item = Result<Event, std::convert::Infallible>> {
|
||||
let pending: VecDeque<String> = first.into();
|
||||
#[cfg(test)]
|
||||
let observer = crate::app::dispatch_observer::installed_observer();
|
||||
stream::unfold(
|
||||
(pending, receiver, guard),
|
||||
|(mut pending, mut receiver, guard)| async move {
|
||||
loop {
|
||||
if let Some(frame) = pending.pop_front() {
|
||||
return Some((Ok(Event::default().data(frame)), (pending, receiver, guard)));
|
||||
move |(mut pending, mut receiver, guard)| {
|
||||
#[cfg(test)]
|
||||
let observer = observer.clone();
|
||||
async move {
|
||||
loop {
|
||||
if let Some(frame) = pending.pop_front() {
|
||||
#[cfg(test)]
|
||||
if let Ok(value) = serde_json::from_str::<Value>(&frame)
|
||||
&& value["params"]["_meta"].get(SUBSCRIPTION_ID_KEY).is_some()
|
||||
&& matches!(
|
||||
value["method"].as_str(),
|
||||
Some(
|
||||
RESOURCE_LIST_CHANGED | PROMPT_LIST_CHANGED | TOOL_LIST_CHANGED
|
||||
)
|
||||
)
|
||||
&& let Some(observer) = observer.as_ref()
|
||||
{
|
||||
crate::app::dispatch_observer::observe(Arc::clone(observer), async {
|
||||
crate::app::dispatch_observer::sdk_subscription_notification_consumed();
|
||||
}).await;
|
||||
}
|
||||
return Some((
|
||||
Ok(Event::default().data(frame)),
|
||||
(pending, receiver, guard),
|
||||
));
|
||||
}
|
||||
let frame = receiver.recv().await?;
|
||||
pending.push_back(frame);
|
||||
}
|
||||
let frame = receiver.recv().await?;
|
||||
pending.push_back(frame);
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user