MCP: execute Tools through request-scoped SDK handlers
CI / UI Checks (pull_request) Successful in 8s
CI / Rust Checks (pull_request) Successful in 35m42s
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-13 16:57:28 +00:00
1 parent 5611e81579
commit 3b30aad28e
12 files changed
+798 -151

No files matched your search

+53 -12
View File
@@ -1,5 +1,7 @@
use axum::{http::StatusCode, response::Response};
use rmcp::model::{CacheScope, CallToolResult, ContentBlock, ListToolsResult, ResultType, Tool};
#[cfg(test)]
use rmcp::model::{CacheScope, ListToolsResult, Tool};
use rmcp::model::{CallToolResult, ContentBlock, ResultType};
use serde_json::{Value, json};
use crate::{
@@ -9,8 +11,11 @@ use crate::{
};
use crate::jsonrpc::jsonrpc_error;
#[cfg(test)]
use crate::protocol::{ToolCallOutcome, ToolsOutcome};
#[cfg(test)]
use crate::tool_error::{control_tool_error_contract, execution_locale};
#[cfg(test)]
use crank_core::CorrelationContext;
pub(crate) fn take_confirmation_token(arguments: &mut Value) -> Option<String> {
@@ -25,6 +30,7 @@ pub(crate) fn take_confirmation_token(arguments: &mut Value) -> Option<String> {
/// The era-neutral success payload of one `tools/call` (Story 3.4: the streamed response needs the
/// payload itself, because the terminal response is the last event of the request's stream).
#[cfg(test)]
pub(super) fn success_tool_payload(
message: &Value,
protocol_version: &str,
@@ -49,6 +55,7 @@ pub(crate) fn tool_error_response(
)
}
#[cfg(test)]
pub(super) fn tool_error_payload(
message: &Value,
protocol_version: &str,
@@ -79,6 +86,7 @@ fn call_result_payload(message: &Value, version: &str, mut result: CallToolResul
jsonrpc_result(request_id(message), result)
}
#[cfg(test)]
pub(super) fn sdk_tools_list_result(
tools: &[Value],
next_cursor: Option<String>,
@@ -95,6 +103,7 @@ pub(super) fn sdk_tools_list_result(
}
/// Serialize the SDK model, preserving catalog extensions that pinned Tool serde cannot represent.
#[cfg(test)]
pub(super) fn tools_list_result(
tools: Vec<Value>,
next_cursor: Option<String>,
@@ -256,6 +265,7 @@ pub(super) enum ToolCommandPayload {
/// service never sees `ResponseMode`, the protocol revision, the request envelope or an
/// `axum::Response`. `ApprovalPending` is a success for the client (it still has to answer), while
/// an unavailable elicitation and a failing call keep their era-correct tool-error shape.
#[cfg(test)]
pub(super) fn tool_command_payload(
message: &Value,
protocol_version: &str,
@@ -300,22 +310,53 @@ pub(super) fn tool_command_payload(
}
}
/// The single-response form of [`tool_command_payload`].
pub(super) fn tool_command_response(
pub(super) fn sdk_tools_payload(
message: &Value,
response_mode: ResponseMode,
protocol_version: &str,
correlation: &CorrelationContext,
outcome: ToolsOutcome,
version: &str,
outcome: super::rmcp_tools_runtime::Outcome,
) -> ToolCommandPayload {
use super::rmcp_tools_runtime::Outcome;
let payload = match outcome {
Outcome::List { result, extras } => {
match super::rmcp_tools_projection::serialize_list(result, extras, version) {
Ok(result) => jsonrpc_result(request_id(message), result),
Err(_) => return ToolCommandPayload::Internal("invalid tool catalog"),
}
}
Outcome::Call(result) => call_result_payload(message, version, result),
Outcome::Error(error) => sdk_tools_error_payload(message, error),
};
ToolCommandPayload::Ok(payload)
}
pub(super) fn sdk_tools_error_payload(message: &Value, error: rmcp::ErrorData) -> Value {
let mut payload = jsonrpc_error(
request_id(message),
i64::from(error.code.0),
error.message.to_string(),
);
if let Some(data) = error.data {
payload["error"]["data"] = data;
}
payload
}
pub(super) fn sdk_tools_response(
message: &Value,
mode: ResponseMode,
version: &str,
outcome: super::rmcp_tools_runtime::Outcome,
) -> Response {
match tool_command_payload(message, protocol_version, correlation, outcome) {
ToolCommandPayload::Ok(payload) => transport_response(
StatusCode::OK,
let internal = matches!(&outcome, super::rmcp_tools_runtime::Outcome::Error(error) if error.code == rmcp::model::ErrorCode::INTERNAL_ERROR);
match sdk_tools_payload(message, version, outcome) {
ToolCommandPayload::Ok(payload) if internal => transport_response(
StatusCode::INTERNAL_SERVER_ERROR,
payload,
response_mode,
ResponseMode::Json,
None,
None,
Some(protocol_version),
),
ToolCommandPayload::Ok(payload) => {
transport_response(StatusCode::OK, payload, mode, None, Some(version))
}
ToolCommandPayload::Internal(detail) => internal_jsonrpc_error(message, detail),
}
}