MCP: send progress through SDK Peer with private trace tokens
CI / UI Checks (pull_request) Successful in 9s
CI / Rust Checks (pull_request) Successful in 41m28s
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 9s
CI / Rust Checks (pull_request) Successful in 41m28s
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
8f494679b8
commit
48436538f1
12 files changed
+974
-83
No files matched your search
@@ -34,8 +34,7 @@ use axum::{
|
||||
};
|
||||
use crank_core::{CorrelationContext, ExecutionLocale, VerifiedMachineCredential};
|
||||
use futures_util::stream;
|
||||
use rmcp::model::{NumberOrString, ProgressNotificationParam};
|
||||
use serde_json::{Number, Value, json};
|
||||
use serde_json::{Number, Value};
|
||||
use tokio::sync::{mpsc, watch};
|
||||
|
||||
use super::rmcp_dispatch::handle_tools_command;
|
||||
@@ -83,15 +82,23 @@ impl ProgressToken {
|
||||
}
|
||||
}
|
||||
|
||||
fn sdk_token(&self) -> NumberOrString {
|
||||
#[cfg(test)]
|
||||
pub(super) fn sdk_token(&self) -> rmcp::model::NumberOrString {
|
||||
match self {
|
||||
Self::Text(text) => NumberOrString::String(text.clone().into()),
|
||||
Self::Text(text) => rmcp::model::NumberOrString::String(text.clone().into()),
|
||||
Self::Number(number) => number.as_i64().map_or_else(
|
||||
|| NumberOrString::String(number.to_string().into()),
|
||||
NumberOrString::Number,
|
||||
|| rmcp::model::NumberOrString::String(number.to_string().into()),
|
||||
rmcp::model::NumberOrString::Number,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn raw(&self) -> Value {
|
||||
match self {
|
||||
Self::Text(text) => Value::String(text.clone()),
|
||||
Self::Number(number) => Value::Number(number.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The progress token of one request parameters object, already validated and bounded. The adapters
|
||||
@@ -133,28 +140,18 @@ pub(crate) struct ProgressEvent {
|
||||
message: &'static str,
|
||||
}
|
||||
|
||||
/// The pinned `notifications/progress` frame. Both supported revisions declare the same parameters,
|
||||
/// so the projection is shared; the token is echoed verbatim and never validated as a credential.
|
||||
pub(super) fn progress_frame(token: &ProgressToken, event: &ProgressEvent) -> Value {
|
||||
let sdk_params = ProgressNotificationParam::new(
|
||||
rmcp::model::ProgressToken(token.sdk_token()),
|
||||
event.progress as f64,
|
||||
)
|
||||
.with_message(event.message);
|
||||
let mut params = serde_json::to_value(sdk_params)
|
||||
.expect("finite progress and string/number token are serializable SDK params");
|
||||
#[cfg(test)]
|
||||
super::dispatch_observer::sdk_progress_notification_serialized();
|
||||
// SDK uses i64 tokens and f64 progress; restore the original JSON numbers before wire delivery.
|
||||
if let ProgressToken::Number(number) = token {
|
||||
params["progressToken"] = Value::Number(number.clone());
|
||||
#[cfg(test)]
|
||||
impl ProgressEvent {
|
||||
pub(super) fn sdk_params(
|
||||
&self,
|
||||
token: &ProgressToken,
|
||||
) -> rmcp::model::ProgressNotificationParam {
|
||||
rmcp::model::ProgressNotificationParam::new(
|
||||
rmcp::model::ProgressToken(token.sdk_token()),
|
||||
self.progress as f64,
|
||||
)
|
||||
.with_message(self.message)
|
||||
}
|
||||
params["progress"] = Value::from(event.progress);
|
||||
json!({
|
||||
"jsonrpc": "2.0",
|
||||
"method": "notifications/progress",
|
||||
"params": params,
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -421,7 +418,8 @@ pub(crate) async fn run_tool_call(
|
||||
call,
|
||||
events: progress_events,
|
||||
cancel: lifecycle.cancel.clone(),
|
||||
token,
|
||||
peer: Some(super::rmcp_progress_peer::ProgressPeer::start(token.raw())),
|
||||
outcome: None,
|
||||
message,
|
||||
protocol_version: protocol_version.clone(),
|
||||
pending: VecDeque::new(),
|
||||
@@ -474,7 +472,8 @@ struct CallStream {
|
||||
call: Pin<Box<dyn Future<Output = super::rmcp_tools_runtime::Outcome> + Send>>,
|
||||
events: mpsc::Receiver<ProgressEvent>,
|
||||
cancel: CancelSignal,
|
||||
token: ProgressToken,
|
||||
peer: Option<super::rmcp_progress_peer::ProgressPeer>,
|
||||
outcome: Option<super::rmcp_tools_runtime::Outcome>,
|
||||
message: Value,
|
||||
protocol_version: String,
|
||||
pending: VecDeque<Event>,
|
||||
@@ -511,55 +510,101 @@ async fn next_event_in_scope(
|
||||
return None;
|
||||
}
|
||||
|
||||
let step = {
|
||||
let CallStream {
|
||||
call,
|
||||
events,
|
||||
cancel,
|
||||
..
|
||||
} = &mut call;
|
||||
tokio::select! {
|
||||
biased;
|
||||
Some(event) = events.recv() => Step::Progress(event),
|
||||
outcome = call => Step::Outcome(outcome),
|
||||
_ = cancel.cancelled() => Step::Cancelled,
|
||||
}
|
||||
};
|
||||
loop {
|
||||
let step = if let Some(outcome) = call.outcome.take() {
|
||||
Step::Outcome(outcome)
|
||||
} else {
|
||||
let CallStream {
|
||||
call,
|
||||
events,
|
||||
cancel,
|
||||
..
|
||||
} = &mut call;
|
||||
tokio::select! {
|
||||
biased;
|
||||
Some(event) = events.recv() => Step::Progress(event),
|
||||
outcome = call => Step::Outcome(outcome),
|
||||
_ = cancel.cancelled() => Step::Cancelled,
|
||||
}
|
||||
};
|
||||
|
||||
match step {
|
||||
Step::Progress(event) => {
|
||||
let frame = progress_frame(&call.token, &event);
|
||||
Some((Ok(Event::default().data(frame.to_string())), call))
|
||||
}
|
||||
Step::Outcome(outcome) => {
|
||||
call.finished = true;
|
||||
let payload = match sdk_tools_payload(&call.message, &call.protocol_version, outcome) {
|
||||
ToolCommandPayload::Ok(payload) => payload,
|
||||
ToolCommandPayload::Internal(_) => jsonrpc_internal_error(&call.message),
|
||||
};
|
||||
// The same projection point the single response uses, so a streamed answer delivers the
|
||||
// same closed `notifications/message` (Story 3.18) before its terminal frame.
|
||||
crate::transport::note_logging_outcome(&payload);
|
||||
for notification in crate::protocol::take_notifications() {
|
||||
match step {
|
||||
Step::Progress(event) => {
|
||||
let Some(peer) = &mut call.peer else { continue };
|
||||
// One send in flight; keep driving the original domain future/deadline, never a task.
|
||||
let delivery = {
|
||||
let notify = peer.notify(event.progress, event.message);
|
||||
tokio::pin!(notify);
|
||||
loop {
|
||||
tokio::select! {
|
||||
biased;
|
||||
result = &mut notify => break Some(result),
|
||||
outcome = &mut call.call, if call.outcome.is_none() => {
|
||||
let expired = is_deadline(&outcome);
|
||||
call.outcome = Some(outcome);
|
||||
if expired { break None; }
|
||||
}
|
||||
_ = call.cancel.cancelled() => return None,
|
||||
}
|
||||
}
|
||||
};
|
||||
match delivery {
|
||||
Some(Ok(frame)) => {
|
||||
return Some((Ok(Event::default().data(frame.to_string())), call));
|
||||
}
|
||||
// Progress is optional: no retry, handwritten fallback or replay of the Tool.
|
||||
Some(Err(_)) | None => {
|
||||
if let Some(peer) = call.peer.take() {
|
||||
peer.finish().await;
|
||||
}
|
||||
call.events.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Step::Outcome(outcome) => {
|
||||
call.finished = true;
|
||||
if let Some(peer) = call.peer.take() {
|
||||
peer.finish().await;
|
||||
}
|
||||
let payload =
|
||||
match sdk_tools_payload(&call.message, &call.protocol_version, outcome) {
|
||||
ToolCommandPayload::Ok(payload) => payload,
|
||||
ToolCommandPayload::Internal(_) => jsonrpc_internal_error(&call.message),
|
||||
};
|
||||
// The same projection point the single response uses, so a streamed answer delivers the
|
||||
// same closed `notifications/message` (Story 3.18) before its terminal frame.
|
||||
crate::transport::note_logging_outcome(&payload);
|
||||
for notification in crate::protocol::take_notifications() {
|
||||
call.pending
|
||||
.push_back(Event::default().data(notification.to_string()));
|
||||
}
|
||||
call.pending
|
||||
.push_back(Event::default().data(notification.to_string()));
|
||||
.push_back(Event::default().data(payload.to_string()));
|
||||
return Some((Ok(call.pending.pop_front().expect("queued frame")), call));
|
||||
}
|
||||
Step::Cancelled => {
|
||||
call.finished = true;
|
||||
call.pending.clear();
|
||||
return None;
|
||||
}
|
||||
call.pending
|
||||
.push_back(Event::default().data(payload.to_string()));
|
||||
Some((Ok(call.pending.pop_front().expect("queued frame")), call))
|
||||
}
|
||||
Step::Cancelled => {
|
||||
call.finished = true;
|
||||
call.pending.clear();
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_deadline(outcome: &super::rmcp_tools_runtime::Outcome) -> bool {
|
||||
matches!(outcome, super::rmcp_tools_runtime::Outcome::Call(result)
|
||||
if result.structured_content.as_ref().is_some_and(|value|
|
||||
value["error"]["code"] == "upstream_timeout"))
|
||||
}
|
||||
|
||||
fn jsonrpc_internal_error(message: &Value) -> Value {
|
||||
crate::jsonrpc::jsonrpc_error(request_id(message), -32603, "internal server error")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "rmcp_progress_stream_tests.rs"]
|
||||
mod stream_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "lifecycle_tests.rs"]
|
||||
mod tests;
|
||||
Reference in new issue
Block a user