Files
crank/crates/crank-community-mcp/src/app/rmcp_dispatch.rs
T
bsodfather 4f83d8d24c
CI / UI Checks (pull_request) Successful in 12s
CI / Rust Checks (pull_request) Successful in 36m53s
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
refactor(mcp): route completion requests and results through rmcp SDK
2026-11-11 10:11:35 +00:00

182 lines
6.6 KiB
Rust

//! Runtime SDK dispatch; admission, lifecycle and response projection stay with the caller.
use std::sync::Arc;
use rmcp::model::{CallToolRequestParams, PaginatedRequestParams, ReadResourceRequestParams};
use serde_json::Value;
use super::{AppState, lifecycle::ToolCallLifecycle};
use crate::protocol::{
ServiceContext, ToolCallCommand, ToolCallOutcome, ToolsCommand, ToolsListCommand, ToolsOutcome,
};
// Crank extensions are not SDK fields and must survive the typed request boundary unchanged.
enum SdkToolsCommand {
List {
params: PaginatedRequestParams,
limit: Option<usize>,
},
Call {
params: CallToolRequestParams,
confirmation_token: Option<String>,
},
}
pub(super) async fn handle_tools_command(
state: &Arc<AppState>,
context: ServiceContext<'_>,
command: ToolsCommand,
lifecycle: Option<ToolCallLifecycle>,
) -> ToolsOutcome {
let request = match command {
ToolsCommand::List(list) => SdkToolsCommand::List {
params: PaginatedRequestParams::default().with_cursor(list.cursor),
limit: list.limit,
},
ToolsCommand::Call(call) => {
let Value::Object(arguments) = call.arguments else {
return ToolsOutcome::Call(ToolCallOutcome::InvalidParams(
"tool arguments must be an object".to_owned(),
));
};
SdkToolsCommand::Call {
params: CallToolRequestParams::new(call.tool_name).with_arguments(arguments),
confirmation_token: call.confirmation_token,
}
}
};
#[cfg(test)]
super::dispatch_observer::sdk_tools_dispatch_enter();
let command = match request {
SdkToolsCommand::List { params, limit } => ToolsCommand::List(ToolsListCommand {
cursor: params.cursor,
limit,
}),
SdkToolsCommand::Call {
params,
confirmation_token,
} => ToolsCommand::Call(ToolCallCommand {
tool_name: params.name.into_owned(),
arguments: Value::Object(params.arguments.unwrap_or_default()),
confirmation_token,
}),
};
crate::protocol::handle_tools_command(state, context, command, lifecycle).await
}
pub(super) async fn handle_prompts_command(
state: &Arc<AppState>,
context: ServiceContext<'_>,
revision: &str,
command: crate::protocol::PromptsCommand,
) -> crate::protocol::PromptsOutcome {
use crate::protocol::{PromptGetCommand, PromptsCommand, PromptsListCommand};
let command = match command {
PromptsCommand::List(list) => {
let params = PaginatedRequestParams::default().with_cursor(list.cursor);
PromptsCommand::List(PromptsListCommand {
cursor: params.cursor,
limit: list.limit,
})
}
PromptsCommand::Get(get) => {
let mut params = rmcp::model::GetPromptRequestParams::new(get.name);
params.arguments = get.arguments;
PromptsCommand::Get(PromptGetCommand {
name: params.name,
arguments: params.arguments,
})
}
};
#[cfg(test)]
super::dispatch_observer::sdk_prompts_dispatch_enter();
crate::protocol::handle_prompts_command(state, context, revision, command).await
}
pub(super) async fn handle_resources_command(
state: &Arc<AppState>,
context: ServiceContext<'_>,
revision: &str,
command: crate::protocol::ResourcesCommand,
) -> crate::protocol::ResourcesOutcome {
use crate::protocol::{ResourceReadCommand, ResourcesCommand};
let command = match command {
ResourcesCommand::List(list) => ResourcesCommand::List(sdk_resource_page(list)),
ResourcesCommand::Read(read) => {
let params = ReadResourceRequestParams::new(read.uri);
ResourcesCommand::Read(ResourceReadCommand { uri: params.uri })
}
};
#[cfg(test)]
super::dispatch_observer::sdk_resources_dispatch_enter();
crate::protocol::handle_resources_command(state, context, revision, command).await
}
pub(super) async fn list_resource_templates(
state: &Arc<AppState>,
workspace_slug: &str,
agent_slug: &str,
revision: &str,
command: crate::protocol::ResourcesListCommand,
) -> Result<(Vec<Value>, Option<String>), crate::protocol::ResourceTemplatesListError> {
let command = sdk_resource_page(command);
#[cfg(test)]
super::dispatch_observer::sdk_resources_dispatch_enter();
crate::protocol::list_resource_templates(state, workspace_slug, agent_slug, revision, command)
.await
}
pub(super) async fn handle_completion_command(
state: &Arc<AppState>,
context: ServiceContext<'_>,
revision: &str,
command: crate::protocol::CompletionCommand,
) -> crate::protocol::CompletionOutcome {
use crate::protocol::{
CompletionArgument, CompletionCommand, CompletionOutcome, CompletionReference,
};
use rmcp::model::{ArgumentInfo, CompleteRequestParams, Reference};
let reference = match command.reference {
CompletionReference::Prompt { name } => Reference::for_prompt(name),
CompletionReference::Resource { uri } => Reference::for_resource(uri),
};
let params = CompleteRequestParams::new(
reference,
ArgumentInfo::new(command.argument.name, command.argument.value),
);
// SDK context only supports strings; retain arbitrary JSON unchanged for domain validation.
let context_arguments = command.context;
#[cfg(test)]
super::dispatch_observer::sdk_completion_dispatch_enter();
let reference = match params.r#ref {
Reference::Prompt(reference) => CompletionReference::Prompt {
name: reference.name,
},
Reference::Resource(reference) => CompletionReference::Resource { uri: reference.uri },
_ => return CompletionOutcome::InvalidParams("invalid completion/complete parameters"),
};
let command = CompletionCommand {
reference,
argument: CompletionArgument {
name: params.argument.name,
value: params.argument.value,
},
context: context_arguments,
};
crate::protocol::handle_completion_command(state, context, revision, command).await
}
fn sdk_resource_page(
list: crate::protocol::ResourcesListCommand,
) -> crate::protocol::ResourcesListCommand {
let params = PaginatedRequestParams::default().with_cursor(list.cursor);
// Crank's page limit is not an SDK field; retain its existing validation and clamping.
crate::protocol::ResourcesListCommand {
cursor: params.cursor,
limit: list.limit,
}
}