MCP: execute legacy logging through awaited SDK handler
CI / UI Checks (pull_request) Successful in 9s
CI / Rust Checks (pull_request) Successful in 39m38s
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:
bsodfather committed 2026-11-14 03:11:37 +00:00
1 parent 7aab666aaa
commit 22637861c3
11 files changed
+505 -39

No files matched your search

@@ -82,6 +82,8 @@ pub(crate) struct CounterSet {
sdk_logging_dispatch: AtomicUsize,
sdk_logging_level: AtomicUsize,
sdk_logging_notification: AtomicUsize,
sdk_logging_handler_completed: AtomicUsize,
sdk_logging_handler_consumed: AtomicUsize,
sdk_progress_notification: AtomicUsize,
sdk_cancelled_params: AtomicUsize,
sdk_subscriptions_listen: AtomicUsize,
@@ -260,6 +262,14 @@ impl CounterSet {
self.sdk_logging_notification.load(Ordering::SeqCst)
}
pub(crate) fn sdk_logging_handler_completed(&self) -> usize {
self.sdk_logging_handler_completed.load(Ordering::SeqCst)
}
pub(crate) fn sdk_logging_handler_consumed(&self) -> usize {
self.sdk_logging_handler_consumed.load(Ordering::SeqCst)
}
pub(crate) fn sdk_completion_dispatch(&self) -> usize {
self.sdk_completion_dispatch.load(Ordering::SeqCst)
}
@@ -563,9 +573,23 @@ pub(crate) fn sdk_logging_notification_serialized() {
increment(|counters| &counters.sdk_logging_notification);
}
/// Mark after the real logging handler has completed its awaited operation and built its result.
pub(crate) fn sdk_logging_handler_completed() {
increment(|counters| &counters.sdk_logging_handler_completed);
}
/// Mark only after the caller consumes the actual SDK handler Result.
pub(crate) fn sdk_logging_handler_consumed() {
increment(|counters| &counters.sdk_logging_handler_consumed);
}
#[path = "../rmcp_logging_runtime_tests.rs"]
mod rmcp_logging_runtime_tests;
#[cfg(test)]
#[path = "../rmcp_logging_handler_runtime_tests.rs"]
mod rmcp_logging_handler_runtime_tests;
/// Mark only after successfully serializing real SDK ProgressNotificationParam params.
pub(crate) fn sdk_progress_notification_serialized() {
increment(|counters| &counters.sdk_progress_notification);
@@ -31,11 +31,11 @@ use crate::{
auth::VerifiedMachineCredential,
jsonrpc::{LEGACY_PROTOCOL_VERSION, jsonrpc_error, jsonrpc_result, params, request_id},
protocol::{
CompletionCommand, CompletionOutcome, ContinuationSupport, DispatchDecision,
EVENT_LEVEL_APPLIED, INVALID_LEVEL, Kind, LoggingOutcome, MethodHandler, PromptGetCommand,
PromptsCommand, PromptsListCommand, PromptsOutcome, RequestIdentity, ResourceReadCommand,
ResourcesCommand, ResourcesListCommand, ResourcesOutcome, ServiceContext, SetLevelCommand,
ToolCallCommand, ToolsCommand, ToolsListCommand, handle_logging_command,
CompletionCommand, CompletionOutcome, ContinuationSupport, DispatchDecision, INVALID_LEVEL,
Kind, MethodHandler, PromptGetCommand, PromptsCommand, PromptsListCommand, PromptsOutcome,
RequestIdentity, ResourceReadCommand, ResourcesCommand, ResourcesListCommand,
ResourcesOutcome, ServiceContext, SetLevelCommand, ToolCallCommand, ToolsCommand,
ToolsListCommand,
},
tool_error::execution_locale,
transport::{ResponseMode, session_id_from_headers, transport_response},
@@ -727,30 +727,23 @@ pub(super) async fn handle_legacy_request(
);
}
};
match handle_logging_command(&state.logging_levels, &session.id, command) {
LoggingOutcome::Applied(level) => {
crate::protocol::set_active_level(Some(level));
crate::protocol::note(
crate::protocol::LogLevel::Notice,
EVENT_LEVEL_APPLIED,
"applied",
);
transport_response(
StatusCode::OK,
jsonrpc_result(request_id(message), json!({})),
let request = match command.validate() {
Ok(request) => request,
Err(detail) => {
return super::dispatch::sdk_error_response(
message,
response_mode,
None,
Some(&session.protocol_version),
)
&session.protocol_version,
rmcp::ErrorData::invalid_params(detail, None),
);
}
LoggingOutcome::InvalidParams(detail) => transport_response(
StatusCode::OK,
jsonrpc_error(request_id(message), -32602, detail),
response_mode,
None,
Some(&session.protocol_version),
),
}
};
super::rmcp_logging::response(
message,
response_mode,
&session.protocol_version,
super::rmcp_logging::set_level(&state, &session.id, request).await,
)
}
// A notification whose method the registration does not map to this adapter's handlers is
// acknowledged without being dispatched, exactly as before.
@@ -0,0 +1,107 @@
//! Request-owned SDK implementation of the legacy `logging/setLevel` handler.
// The pinned legacy revision still requires this deprecated SDK method.
#![allow(deprecated)]
use super::AppState;
use crate::protocol::{self, LogLevel};
use rmcp::{ErrorData, RoleServer, ServerHandler, model::*, service::RequestContext};
use std::sync::Arc;
#[derive(Clone)]
struct LoggingHandler {
store: Arc<protocol::LogLevelStore>,
session_id: String,
}
impl ServerHandler for LoggingHandler {
async fn set_level(
&self,
request: SetLevelRequestParams,
_context: RequestContext<RoleServer>,
) -> Result<(), ErrorData> {
#[allow(deprecated)]
let level: LogLevel = request.level.into();
self.store.set(&self.session_id, level);
protocol::set_active_level(Some(level));
protocol::note(LogLevel::Notice, protocol::EVENT_LEVEL_APPLIED, "applied");
#[cfg(test)]
super::dispatch_observer::sdk_logging_handler_completed();
Ok(())
}
}
async fn request_context() -> Result<RequestContext<RoleServer>, ErrorData> {
let ping: ClientJsonRpcMessage = serde_json::from_value(serde_json::json!({
"jsonrpc":"2.0", "id":1, "method":"ping"
}))
.map_err(|_| ErrorData::internal_error("logging handler unavailable", None))?;
let (transport, receiver) = rmcp::transport::OneshotTransport::<RoleServer>::new(ping);
let info = InitializeRequestParams::new(
ClientCapabilities::default(),
Implementation::new("crank-logging-context", "1"),
);
#[derive(Clone)]
struct Bootstrap;
impl ServerHandler for Bootstrap {}
let mut running = rmcp::service::serve_directly(Bootstrap, transport, Some(info));
let peer = running.peer().clone();
running
.close()
.await
.map_err(|_| ErrorData::internal_error("logging handler unavailable", None))?;
drop(receiver);
Ok(RequestContext::new(RequestId::Number(1), peer))
}
pub(super) async fn set_level(
state: &Arc<AppState>,
session_id: &str,
request: SetLevelRequestParams,
) -> Result<(), ErrorData> {
let handler = LoggingHandler {
store: Arc::clone(&state.logging_levels),
session_id: session_id.to_owned(),
};
let context = request_context().await?;
invoke(&handler, request, context).await
}
async fn invoke(
handler: &impl ServerHandler,
request: SetLevelRequestParams,
context: RequestContext<RoleServer>,
) -> Result<(), ErrorData> {
ServerHandler::set_level(handler, request, context).await
}
pub(super) fn response(
message: &serde_json::Value,
mode: crate::transport::ResponseMode,
revision: &str,
result: Result<(), ErrorData>,
) -> axum::response::Response {
match result {
Ok(()) => {
#[cfg(test)]
super::dispatch_observer::sdk_logging_handler_consumed();
crate::transport::transport_response(
axum::http::StatusCode::OK,
crate::jsonrpc::jsonrpc_result(
crate::jsonrpc::request_id(message),
serde_json::json!({}),
),
mode,
None,
Some(revision),
)
}
Err(error) => {
#[cfg(test)]
super::dispatch_observer::sdk_logging_handler_consumed();
super::dispatch::sdk_error_response(message, mode, revision, error)
}
}
}
#[cfg(test)]
#[path = "../rmcp_logging_handler_tests.rs"]
mod tests;