MCP: dispatch incoming requests and notifications through SDK envelopes
CI / UI Checks (pull_request) Successful in 14s
CI / Rust Checks (pull_request) Successful in 35m55s
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-12 21:53:24 +00:00
1 parent 156a849087
commit 0de1e94df4
8 files changed
+662 -33

No files matched your search

+18 -9
View File
@@ -12,10 +12,7 @@ use super::rmcp_dispatch::{
};
use crate::{
auth::VerifiedMachineCredential,
jsonrpc::{
is_notification, jsonrpc_error, jsonrpc_result, method_name, negotiated_protocol_version,
params, request_id,
},
jsonrpc::{jsonrpc_error, jsonrpc_result, negotiated_protocol_version, params, request_id},
protocol::{
CompletionCommand, CompletionOutcome, ContinuationSupport, DispatchDecision, MethodHandler,
PromptGetCommand, PromptsCommand, PromptsListCommand, PromptsOutcome, RequestIdentity,
@@ -81,7 +78,20 @@ pub(super) async fn handle_modern_request(
// matrix row 18). The envelope, identity and mixed-carrier checks — and authentication — have
// already run at this point, so this early return cannot admit a malformed identity or a session
// conflict, and the notification is never dispatched.
if is_notification(message) {
let envelope = match crate::protocol::decode_dispatch_envelope(message) {
Ok(Some(envelope)) => envelope,
_ => {
return internal_jsonrpc_error(
message,
"admitted dispatch envelope could not be decoded",
);
}
};
let method = envelope.method();
let notification = envelope.is_notification();
#[cfg(test)]
super::dispatch_observer::sdk_dispatch_envelope_consumed();
if notification {
return crate::protocol::modern_notification_response();
}
@@ -89,15 +99,14 @@ pub(super) async fn handle_modern_request(
Ok(declared_method) => declared_method,
Err(response) => return *response,
};
if declared_method != method_name(message) {
if declared_method != method {
return crate::protocol::modern_header_mismatch_response(message);
}
// R5/F5: the same registration-backed dispatch as the legacy adapter. The handler is decided by
// the owning registration (not by a literal method list inside this adapter), and a method whose
// capability is not eligible in this revision is never served here.
let dispatch =
crate::protocol::dispatch_decision(method_name(message).unwrap_or_default(), version);
let dispatch = crate::protocol::dispatch_decision(method.unwrap_or_default(), version);
match dispatch {
DispatchDecision::Allowed {
handler: MethodHandler::Discover,
@@ -121,7 +130,7 @@ pub(super) async fn handle_modern_request(
request_id(message),
crate::protocol::project_modern_result(
version,
crate::protocol::ping_result(message, version),
crate::protocol::ping_result(&json!({"method": "ping"}), version),
),
),
response_mode,
@@ -75,6 +75,8 @@ pub(crate) struct CounterSet {
sdk_jsonrpc_error: AtomicUsize,
sdk_ping_request: AtomicUsize,
sdk_ping_result: AtomicUsize,
sdk_dispatch_decoded: AtomicUsize,
sdk_dispatch_consumed: AtomicUsize,
sdk_initialized_decoded: AtomicUsize,
sdk_initialized_consumed: AtomicUsize,
sdk_initialize_input: AtomicUsize,
@@ -83,6 +85,12 @@ pub(crate) struct CounterSet {
}
impl CounterSet {
pub(crate) fn sdk_dispatch_decoded(&self) -> usize {
self.sdk_dispatch_decoded.load(Ordering::SeqCst)
}
pub(crate) fn sdk_dispatch_consumed(&self) -> usize {
self.sdk_dispatch_consumed.load(Ordering::SeqCst)
}
pub(crate) fn sdk_initialized_decoded(&self) -> usize {
self.sdk_initialized_decoded.load(Ordering::SeqCst)
}
@@ -456,6 +464,19 @@ pub(crate) fn sdk_initialized_notification_consumed(
#[path = "../rmcp_initialized_runtime_tests.rs"]
mod rmcp_initialized_runtime_tests;
/// Mark after successful SDK envelope decode, independently of routing consumption.
pub(crate) fn sdk_dispatch_envelope_decoded() {
increment(|counters| &counters.sdk_dispatch_decoded);
}
/// Mark only when decoded method/kind selects the existing registry-backed route.
pub(crate) fn sdk_dispatch_envelope_consumed() {
increment(|counters| &counters.sdk_dispatch_consumed);
}
#[path = "../rmcp_dispatch_runtime_tests.rs"]
mod rmcp_dispatch_runtime_tests;
fn increment(counter: impl Fn(&CounterSet) -> &AtomicUsize) {
let _ = OBSERVER.try_with(|counters| {
counter(counters).fetch_add(1, Ordering::SeqCst);
@@ -30,10 +30,7 @@ use super::rmcp_dispatch::{
use super::{AgentRoutePath, AppState};
use crate::{
auth::VerifiedMachineCredential,
jsonrpc::{
LEGACY_PROTOCOL_VERSION, is_notification, is_request, jsonrpc_error, jsonrpc_result,
method_name, params, request_id,
},
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,
@@ -65,10 +62,20 @@ pub(super) async fn handle_legacy_request(
// `protocol::registry` and is read through `dispatch_decision`, so a newly registered method
// really reaches its handler and removing a descriptor removes the dispatch.
let registration_revision = protocol_version.unwrap_or(LEGACY_PROTOCOL_VERSION);
let dispatch = crate::protocol::dispatch_decision(
method_name(message).unwrap_or_default(),
registration_revision,
);
let envelope = match crate::protocol::decode_dispatch_envelope(message) {
Ok(Some(envelope)) => envelope,
_ => {
return internal_jsonrpc_error(
message,
"admitted dispatch envelope could not be decoded",
);
}
};
let method = envelope.method();
let dispatch =
crate::protocol::dispatch_decision(method.unwrap_or_default(), registration_revision);
#[cfg(test)]
super::dispatch_observer::sdk_dispatch_envelope_consumed();
match dispatch {
// G2: the *registered kind* of the matched method, not the envelope shape, decides which
// legacy lifecycle action runs. `initialize` is a request and `notifications/initialized` is a
@@ -78,11 +85,11 @@ pub(super) async fn handle_legacy_request(
DispatchDecision::Allowed {
handler: MethodHandler::Lifecycle,
kind: Kind::Method,
} if is_request(message) => handle_initialize(state, path, message, response_mode).await,
} if envelope.is_request() => handle_initialize(state, path, message, response_mode).await,
DispatchDecision::Allowed {
handler: MethodHandler::Lifecycle,
kind: Kind::Notification,
} if is_notification(message) => {
} if envelope.is_notification() => {
handle_initialized_notification(
state,
path,
@@ -94,7 +101,7 @@ pub(super) async fn handle_legacy_request(
DispatchDecision::Allowed {
handler: MethodHandler::Ping,
..
} if is_request(message) => {
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
@@ -104,7 +111,10 @@ pub(super) async fn handle_legacy_request(
StatusCode::OK,
jsonrpc_result(
request_id(message),
crate::protocol::ping_result(message, &session.protocol_version),
crate::protocol::ping_result(
&json!({"method": "ping"}),
&session.protocol_version,
),
),
response_mode,
None,
@@ -114,7 +124,7 @@ pub(super) async fn handle_legacy_request(
DispatchDecision::Allowed {
handler: MethodHandler::ToolList,
..
} if is_request(message) => {
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
@@ -183,7 +193,7 @@ pub(super) async fn handle_legacy_request(
DispatchDecision::Allowed {
handler: MethodHandler::ToolCall,
..
} if is_request(message) => {
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
@@ -241,13 +251,13 @@ pub(super) async fn handle_legacy_request(
DispatchDecision::Allowed {
handler: MethodHandler::Cancelled,
kind: Kind::Notification,
} if is_notification(message) => {
} if envelope.is_notification() => {
handle_cancelled_notification(&state, path, headers, message, credential).await
}
DispatchDecision::Allowed {
handler: MethodHandler::ResourceList,
..
} if is_request(message) => {
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
@@ -316,7 +326,7 @@ pub(super) async fn handle_legacy_request(
DispatchDecision::Allowed {
handler: MethodHandler::ResourceRead,
..
} if is_request(message) => {
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
@@ -397,7 +407,7 @@ pub(super) async fn handle_legacy_request(
DispatchDecision::Allowed {
handler: MethodHandler::ResourceTemplateList,
..
} if is_request(message) => {
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
@@ -469,7 +479,7 @@ pub(super) async fn handle_legacy_request(
DispatchDecision::Allowed {
handler: MethodHandler::PromptList,
..
} if is_request(message) => {
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
@@ -534,7 +544,7 @@ pub(super) async fn handle_legacy_request(
DispatchDecision::Allowed {
handler: MethodHandler::PromptGet,
..
} if is_request(message) => {
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
@@ -632,7 +642,7 @@ pub(super) async fn handle_legacy_request(
DispatchDecision::Allowed {
handler: MethodHandler::Completion,
..
} if is_request(message) => {
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
@@ -711,7 +721,7 @@ pub(super) async fn handle_legacy_request(
DispatchDecision::Allowed {
handler: MethodHandler::LoggingSetLevel,
..
} if is_request(message) => {
} if envelope.is_request() => {
let session = match require_initialized_session(&state, path, headers, message).await {
Ok(session) => session,
Err(response) => return response,
@@ -757,10 +767,10 @@ pub(super) async fn handle_legacy_request(
}
// A notification whose method the registration does not map to this adapter's handlers is
// acknowledged without being dispatched, exactly as before.
_ if is_notification(message) => StatusCode::ACCEPTED.into_response(),
_ if envelope.is_notification() => StatusCode::ACCEPTED.into_response(),
// Every remaining decision (unregistered, wrong era, or a handler this adapter does not
// serve) is a method-level rejection: no handler, no service entry, no outbound call.
_ => match method_name(message) {
_ => match method {
Some(method) => transport_response(
StatusCode::OK,
jsonrpc_error(