use std::collections::BTreeMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use crate::schema::FieldMeta;
use futures::stream::BoxStream;
use serde_json::Value;
#[cfg(not(target_arch = "wasm32"))]
mod elicitation;
#[cfg(feature = "mcp-files")]
pub mod files;
#[cfg(all(test, feature = "http", not(target_arch = "wasm32")))]
mod parity_tests;
mod portable_server;
mod shared;
mod skills;
mod tasks;
#[cfg(not(target_arch = "wasm32"))]
pub use elicitation::elicitation_for_request;
pub use skills::{
DEFAULT_SKILL_PAGE_SIZE, MAX_SKILL_BYTES, MAX_SKILL_FILES, MAX_SKILL_NAME_LEN, McpSkillBundle,
McpSkillContent, McpSkillFile, McpSkillPublication, SKILL_INVALID, SKILL_TTL_MS,
SKILL_URI_SCHEME, SKILLS_EXTENSION,
};
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
pub(crate) use skills::{decode_base64, parse_frontmatter, sha256_hex};
#[cfg(not(target_arch = "wasm32"))]
pub use tasks::McpTaskDirectoryJournal;
pub use tasks::{
McpTaskJournal, McpTaskRecord, McpTaskSpawner, TASK_POLL_INTERVAL_MS, TASK_TTL_MS,
TASKS_EXTENSION,
};
pub use portable_server::{
DEFAULT_MAX_REQUEST_BODY_BYTES, McpHttpBody, McpHttpConfig, McpHttpRequest, McpHttpResponse,
McpHttpServer,
};
#[derive(Debug, Clone)]
pub struct ToolEntry {
pub name: String,
pub description: Option<String>,
pub input_schema: serde_json::Value,
pub output_schema: Option<serde_json::Value>,
}
#[derive(Debug, Clone)]
pub struct CommandEntry {
pub is_group: bool,
pub description: Option<String>,
pub commands: BTreeMap<String, CommandEntry>,
pub args_fields: Vec<FieldMeta>,
pub options_fields: Vec<FieldMeta>,
pub output_schema: Option<serde_json::Value>,
pub input_schema: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum McpDiscovery {
#[default]
Progressive,
Direct,
}
#[derive(Debug, Clone, Default)]
pub struct McpToolFilter {
pub discovery: McpDiscovery,
pub include: Vec<String>,
pub exclude: Vec<String>,
}
pub struct McpResultContext<'a> {
pub tool: &'a crate::tool::ToolDefinition,
pub outcome: &'a crate::tool::ToolCallOutcome,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct McpResultMapping {
pub is_error: Option<bool>,
pub structured_content: Option<Value>,
pub content: Option<Vec<Value>>,
pub meta: BTreeMap<String, Value>,
}
impl McpResultMapping {
pub fn unchanged() -> Self {
Self::default()
}
pub fn success() -> Self {
Self {
is_error: Some(false),
structured_content: None,
content: None,
meta: BTreeMap::new(),
}
}
pub fn error() -> Self {
Self {
is_error: Some(true),
structured_content: None,
content: None,
meta: BTreeMap::new(),
}
}
}
#[derive(Clone)]
pub struct McpResultMapper(
std::sync::Arc<
dyn for<'a> Fn(McpResultContext<'a>) -> McpResultMapping + Send + Sync + 'static,
>,
);
impl std::fmt::Debug for McpResultMapper {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("McpResultMapper").finish_non_exhaustive()
}
}
impl McpResultMapper {
pub fn new(
mapper: impl for<'a> Fn(McpResultContext<'a>) -> McpResultMapping + Send + Sync + 'static,
) -> Self {
Self(std::sync::Arc::new(mapper))
}
pub(super) fn map(&self, context: McpResultContext<'_>) -> McpResultMapping {
(self.0)(context)
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpResource {
pub uri: String,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<u64>,
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub icons: Vec<Value>,
#[serde(rename = "_meta", skip_serializing_if = "BTreeMap::is_empty", default)]
pub meta: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpResourceTemplate {
pub uri_template: String,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub icons: Vec<Value>,
#[serde(rename = "_meta", skip_serializing_if = "BTreeMap::is_empty", default)]
pub meta: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum McpResourceContents {
Text {
uri: String,
#[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
mime_type: Option<String>,
text: String,
#[serde(rename = "_meta", skip_serializing_if = "BTreeMap::is_empty", default)]
meta: BTreeMap<String, Value>,
},
Blob {
uri: String,
#[serde(rename = "mimeType", skip_serializing_if = "Option::is_none")]
mime_type: Option<String>,
blob: String,
#[serde(rename = "_meta", skip_serializing_if = "BTreeMap::is_empty", default)]
meta: BTreeMap<String, Value>,
},
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpResourceReadResult {
pub contents: Vec<McpResourceContents>,
#[serde(rename = "_meta", skip_serializing_if = "BTreeMap::is_empty", default)]
pub meta: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, Default)]
pub struct McpResourceRequest {
pub request_id: Option<Value>,
pub request: Option<crate::command::RequestContext>,
pub protocol_version: Option<String>,
pub request_meta: Option<Value>,
pub client_capabilities: Option<Value>,
pub input_responses: Option<Value>,
pub request_state: Option<String>,
pub peer: Option<crate::command::McpPeer>,
}
#[derive(Debug, Clone)]
pub struct McpResourceReadRequest {
pub uri: String,
pub context: McpResourceRequest,
}
#[derive(Debug, Clone)]
pub struct McpResourceSubscriptionRequest {
pub uri: String,
pub context: McpResourceRequest,
}
#[derive(Debug, Clone)]
pub struct McpResourceError {
pub code: i32,
pub message: String,
pub data: Option<Value>,
}
pub type McpResourceReadFuture =
Pin<Box<dyn Future<Output = Result<McpResourceReadResult, McpResourceError>> + Send + 'static>>;
pub type McpResourceSubscriptionFuture =
Pin<Box<dyn Future<Output = Result<(), McpResourceError>> + Send + 'static>>;
#[derive(Clone)]
pub struct McpResourceReader(
Arc<dyn Fn(McpResourceReadRequest) -> McpResourceReadFuture + Send + Sync>,
);
impl std::fmt::Debug for McpResourceReader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("McpResourceReader").finish_non_exhaustive()
}
}
impl McpResourceReader {
pub fn new<F, Fut>(reader: F) -> Self
where
F: Fn(McpResourceReadRequest) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<McpResourceReadResult, McpResourceError>> + Send + 'static,
{
Self(Arc::new(move |request| Box::pin(reader(request))))
}
pub(super) async fn read(
&self,
request: McpResourceReadRequest,
) -> Result<McpResourceReadResult, McpResourceError> {
(self.0)(request).await
}
}
#[derive(Clone)]
pub struct McpResourceSubscriptionHandler(
Arc<dyn Fn(McpResourceSubscriptionRequest) -> McpResourceSubscriptionFuture + Send + Sync>,
);
impl std::fmt::Debug for McpResourceSubscriptionHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("McpResourceSubscriptionHandler")
.finish_non_exhaustive()
}
}
impl McpResourceSubscriptionHandler {
pub fn new<F, Fut>(handler: F) -> Self
where
F: Fn(McpResourceSubscriptionRequest) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<(), McpResourceError>> + Send + 'static,
{
Self(Arc::new(move |request| Box::pin(handler(request))))
}
pub(super) async fn handle(
&self,
request: McpResourceSubscriptionRequest,
) -> Result<(), McpResourceError> {
(self.0)(request).await
}
}
#[derive(Debug, Clone)]
pub struct McpSubscriptionListenRequest {
pub notifications: Value,
pub resource_uris: Vec<String>,
pub cursor: Option<String>,
pub context: McpResourceRequest,
}
pub type McpSubscriptionListenStream = BoxStream<'static, Value>;
pub type McpSubscriptionListenFuture = Pin<
Box<
dyn Future<Output = Result<McpSubscriptionListenStream, McpResourceError>> + Send + 'static,
>,
>;
#[derive(Clone)]
pub struct McpSubscriptionListenHandler(
Arc<dyn Fn(McpSubscriptionListenRequest) -> McpSubscriptionListenFuture + Send + Sync>,
);
impl std::fmt::Debug for McpSubscriptionListenHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("McpSubscriptionListenHandler")
.finish_non_exhaustive()
}
}
impl McpSubscriptionListenHandler {
pub fn new<F, Fut>(handler: F) -> Self
where
F: Fn(McpSubscriptionListenRequest) -> Fut + Send + Sync + 'static,
Fut:
Future<Output = Result<McpSubscriptionListenStream, McpResourceError>> + Send + 'static,
{
Self(Arc::new(move |request| Box::pin(handler(request))))
}
pub(super) async fn listen(
&self,
request: McpSubscriptionListenRequest,
) -> Result<McpSubscriptionListenStream, McpResourceError> {
(self.0)(request).await
}
}
#[derive(Debug, Clone, Default)]
pub struct McpResourceRegistry {
pub resources: Vec<McpResource>,
pub templates: Vec<McpResourceTemplate>,
pub read: Option<McpResourceReader>,
pub subscribe: Option<McpResourceSubscriptionHandler>,
pub unsubscribe: Option<McpResourceSubscriptionHandler>,
pub listen: Option<McpSubscriptionListenHandler>,
}
pub const EVENTS_EXTENSION: &str = "io.modelcontextprotocol/events";
#[derive(Debug, Clone, PartialEq)]
pub struct McpCustomMethodError {
pub code: i32,
pub message: String,
pub data: Option<Value>,
}
#[derive(Clone)]
pub struct McpCustomMethodHandler {
inner: Arc<
dyn Fn(
String,
Value,
)
-> Pin<Box<dyn Future<Output = Option<Result<Value, McpCustomMethodError>>> + Send>>
+ Send
+ Sync,
>,
}
impl std::fmt::Debug for McpCustomMethodHandler {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("McpCustomMethodHandler")
}
}
impl McpCustomMethodHandler {
pub fn new<F, Fut>(handler: F) -> Self
where
F: Fn(String, Value) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Option<Result<Value, McpCustomMethodError>>> + Send + 'static,
{
Self {
inner: Arc::new(move |method, params| Box::pin(handler(method, params))),
}
}
pub async fn call(
&self,
method: String,
params: Value,
) -> Option<Result<Value, McpCustomMethodError>> {
(self.inner)(method, params).await
}
}
#[derive(Debug, Clone, Default)]
pub struct McpServeOptions {
pub result_mapper: Option<McpResultMapper>,
pub version: Option<String>,
pub instructions: Option<String>,
pub tools: McpToolFilter,
pub capabilities: BTreeMap<String, Value>,
pub resources: McpResourceRegistry,
pub peer: Option<crate::command::McpPeer>,
pub standards: multicast_mcp_protocol::McpStandardSet,
pub skills: McpSkillPublication,
pub tasks: McpServeTasks,
pub custom_methods: Option<McpCustomMethodHandler>,
}
#[derive(Debug, Clone, Default)]
pub enum McpServeTasks {
#[default]
Default,
Off,
Custom(McpTaskSpawner),
}
#[cfg(not(target_arch = "wasm32"))]
pub fn default_task_directory(server_name: &str) -> Option<std::path::PathBuf> {
dirs::state_dir()
.or_else(dirs::data_local_dir)
.map(|base| base.join("multicast").join(server_name).join("tasks"))
}
pub type McpDetach = std::sync::Arc<dyn Fn(futures::future::BoxFuture<'static, ()>) + Send + Sync>;
pub const DEFAULT_REMOTE_REQUEST_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60);
pub const MAX_REMOTE_TOOL_PAGES: usize = 256;
pub const MAX_REMOTE_TOOLS: usize = 16 * 1024;
#[derive(Clone)]
pub struct McpRemoteOptions {
pub open_telemetry: bool,
pub standards: multicast_mcp_protocol::McpStandardSet,
pub auth_token: Option<String>,
pub headers: Vec<(String, String)>,
pub http_client: Option<crate::outbound::SharedHttpClient>,
pub request_timeout: Option<std::time::Duration>,
pub max_response_bytes: usize,
pub detach: Option<McpDetach>,
pub skill_directory: Option<std::path::PathBuf>,
}
impl Default for McpRemoteOptions {
fn default() -> Self {
Self {
open_telemetry: true,
standards: multicast_mcp_protocol::McpStandardSet::default(),
auth_token: None,
headers: Vec::new(),
http_client: None,
request_timeout: Some(DEFAULT_REMOTE_REQUEST_TIMEOUT),
max_response_bytes: crate::outbound::DEFAULT_MAX_RESPONSE_BYTES,
detach: None,
skill_directory: None,
}
}
}
impl std::fmt::Debug for McpRemoteOptions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("McpRemoteOptions")
.field("standards", &self.standards)
.field("auth_token", &self.auth_token)
.field("headers", &self.headers)
.field(
"http_client",
&self.http_client.as_ref().map(|_| "dyn HttpClient"),
)
.field("request_timeout", &self.request_timeout)
.field("max_response_bytes", &self.max_response_bytes)
.field("detach", &self.detach.as_ref().map(|_| "dyn Fn"))
.field("skill_directory", &self.skill_directory)
.finish()
}
}
impl McpRemoteOptions {
pub fn bearer(token: impl Into<String>) -> Self {
Self {
auth_token: Some(token.into()),
..Self::default()
}
}
}
#[cfg(not(target_arch = "wasm32"))]
fn rmcp_protocol_versions(
standards: &multicast_mcp_protocol::McpStandardSet,
) -> Vec<rmcp::model::ProtocolVersion> {
standards
.versions()
.iter()
.map(|version| match version.as_str() {
"2024-11-05" => rmcp::model::ProtocolVersion::V_2024_11_05,
"2025-03-26" => rmcp::model::ProtocolVersion::V_2025_03_26,
"2025-06-18" => rmcp::model::ProtocolVersion::V_2025_06_18,
"2025-11-25" => rmcp::model::ProtocolVersion::V_2025_11_25,
"2026-07-28" => rmcp::model::ProtocolVersion::V_2026_07_28,
version => unreachable!("McpStandardSet admitted unknown standard {version}"),
})
.collect()
}
pub fn matches_tool_filter(name: &str, filter: &McpToolFilter) -> bool {
fn matches(pattern: &str, value: &str) -> bool {
if pattern == "*" {
return true;
}
let parts = pattern.split('*').collect::<Vec<_>>();
if parts.len() == 1 {
return pattern == value;
}
let mut offset = 0;
for (index, part) in parts.iter().enumerate() {
if part.is_empty() {
continue;
}
let Some(found) = value[offset..].find(part) else {
return false;
};
if index == 0 && !pattern.starts_with('*') && found != 0 {
return false;
}
offset += found + part.len();
}
pattern.ends_with('*') || parts.last().is_some_and(|part| value.ends_with(part))
}
let included =
filter.include.is_empty() || filter.include.iter().any(|pattern| matches(pattern, name));
let excluded = filter.exclude.iter().any(|pattern| matches(pattern, name));
included && !excluded
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
struct RemoteTool {
name: String,
description: Option<String>,
input_schema: Value,
output_schema: Option<Value>,
read_only: bool,
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
impl RemoteTool {
fn from_value(value: &Value) -> Result<Self, crate::errors::Error> {
let name = value
.get("name")
.and_then(Value::as_str)
.ok_or_else(|| remote_error("MCP tool has no name"))?;
let input_schema = value
.get("inputSchema")
.filter(|schema| schema.is_object())
.cloned()
.ok_or_else(|| remote_error(format!("MCP tool `{name}` has no inputSchema object")))?;
Ok(Self {
name: name.to_string(),
description: value
.get("description")
.and_then(Value::as_str)
.map(ToString::to_string),
input_schema,
output_schema: value
.get("outputSchema")
.filter(|schema| schema.is_object())
.cloned()
.map(|schema| {
multicast_mcp_protocol::structured::restore_output_schema(
schema,
value.get("_meta").and_then(Value::as_object),
)
}),
read_only: value
.pointer("/annotations/readOnlyHint")
.and_then(Value::as_bool)
== Some(true),
})
}
}
#[cfg(all(
any(feature = "http", feature = "agent-plugins-mcp"),
not(target_arch = "wasm32")
))]
impl From<rmcp::model::Tool> for RemoteTool {
fn from(tool: rmcp::model::Tool) -> Self {
let wire = serde_json::to_value(&tool).unwrap_or(Value::Null);
Self {
name: tool.name.to_string(),
description: tool.description.map(|description| description.to_string()),
input_schema: Value::Object((*tool.input_schema).clone()),
output_schema: tool.output_schema.map(|schema| {
multicast_mcp_protocol::structured::restore_output_schema(
Value::Object((*schema).clone()),
wire.get("_meta").and_then(Value::as_object),
)
}),
read_only: tool
.annotations
.as_ref()
.and_then(|annotations| annotations.read_only_hint)
== Some(true),
}
}
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
struct RemoteCallResult {
is_error: bool,
structured_content: Option<Value>,
first_text: Option<String>,
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
impl RemoteCallResult {
fn from_value(value: &Value) -> Self {
Self {
is_error: value.get("isError").and_then(Value::as_bool) == Some(true),
structured_content: value.get("structuredContent").cloned().map(|content| {
multicast_mcp_protocol::structured::restore_structured_content(
content,
value.get("_meta").and_then(Value::as_object),
)
}),
first_text: value
.pointer("/content/0")
.filter(|content| content.get("type").and_then(Value::as_str) == Some("text"))
.and_then(|content| content.get("text"))
.and_then(Value::as_str)
.map(ToString::to_string),
}
}
fn data(self) -> Value {
self.structured_content.unwrap_or_else(|| {
self.first_text
.and_then(|text| serde_json::from_str(&text).ok())
.unwrap_or(Value::Null)
})
}
fn failure_message(self) -> String {
self.first_text
.unwrap_or_else(|| "Remote MCP tool failed".to_string())
}
}
#[cfg(all(
any(feature = "http", feature = "agent-plugins-mcp"),
not(target_arch = "wasm32")
))]
impl From<rmcp::model::CallToolResult> for RemoteCallResult {
fn from(result: rmcp::model::CallToolResult) -> Self {
let wire = serde_json::to_value(&result).unwrap_or(Value::Null);
Self {
is_error: result.is_error == Some(true),
first_text: result
.content
.first()
.and_then(|content| content.as_text())
.map(|text| text.text.clone()),
structured_content: result.structured_content.map(|content| {
multicast_mcp_protocol::structured::restore_structured_content(
content,
wire.get("_meta").and_then(Value::as_object),
)
}),
}
}
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
#[async_trait::async_trait]
#[allow(
clippy::double_must_use,
reason = "async_trait annotates generated futures"
)]
trait RemoteToolClient: Send + Sync {
async fn remote_list_tools(&self) -> Result<Vec<RemoteTool>, crate::errors::Error>;
fn remote_deadline(&self) -> Option<(std::time::Duration, crate::outbound::Sleep)> {
None
}
fn remote_parse_tool(&self, value: Value) -> Result<RemoteTool, crate::errors::Error>;
async fn remote_call_tool(
&self,
name: String,
arguments: serde_json::Map<String, Value>,
) -> Result<RemoteCallResult, crate::errors::Error>;
async fn remote_call_outcome(
&self,
name: String,
arguments: serde_json::Map<String, Value>,
resume: Option<(String, Value)>,
) -> Result<RemoteOutcome, crate::errors::Error> {
if resume.is_some() {
return Err(crate::errors::Error::Multicast(
crate::errors::MulticastError {
message: "this remote client cannot resume a task".to_string(),
code: "MCP_PROTOCOL_ERROR".to_string(),
hint: None,
retryable: false,
exit_code: None,
cause: None,
},
));
}
self.remote_call_tool(name, arguments)
.await
.map(RemoteOutcome::Complete)
}
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
enum RemoteOutcome {
Complete(RemoteCallResult),
InputRequired {
task_id: String,
input_requests: Value,
},
}
#[cfg(all(
any(feature = "http", feature = "agent-plugins-mcp"),
not(target_arch = "wasm32")
))]
#[async_trait::async_trait]
impl RemoteToolClient for rmcp::service::RunningService<rmcp::RoleClient, rmcp::model::ClientInfo> {
fn remote_deadline(&self) -> Option<(std::time::Duration, crate::outbound::Sleep)> {
Some((
DEFAULT_REMOTE_REQUEST_TIMEOUT,
Box::pin(tokio::time::sleep(DEFAULT_REMOTE_REQUEST_TIMEOUT)),
))
}
async fn remote_list_tools(&self) -> Result<Vec<RemoteTool>, crate::errors::Error> {
let mut tools = Vec::new();
let mut cursor = None;
for page in 1..=MAX_REMOTE_TOOL_PAGES {
let params = cursor.clone().map(|cursor| {
rmcp::model::PaginatedRequestParams::default().with_cursor(Some(cursor))
});
let result = rmcp::service::Peer::<rmcp::RoleClient>::list_tools(self, params)
.await
.map_err(rmcp_service_error)?;
if result.tools.len() > MAX_REMOTE_TOOLS.saturating_sub(tools.len()) {
return Err(tool_limit_error(format!(
"MCP tool catalog lists more than {MAX_REMOTE_TOOLS} tools"
)));
}
tools.extend(result.tools.into_iter().map(RemoteTool::from));
match result.next_cursor {
Some(next) if cursor.as_deref() == Some(next.as_str()) => {
return Err(remote_error("tools/list returned a non-advancing cursor"));
}
Some(_) if page == MAX_REMOTE_TOOL_PAGES => {
return Err(tool_limit_error(format!(
"tools/list returned more than {MAX_REMOTE_TOOL_PAGES} pages"
)));
}
Some(next) => cursor = Some(next),
None => return Ok(tools),
}
}
unreachable!("the final page returns a result or the page-limit error")
}
fn remote_parse_tool(&self, value: Value) -> Result<RemoteTool, crate::errors::Error> {
serde_json::from_value::<rmcp::model::Tool>(value)
.map(RemoteTool::from)
.map_err(remote_error)
}
async fn remote_call_tool(
&self,
name: String,
arguments: serde_json::Map<String, Value>,
) -> Result<RemoteCallResult, crate::errors::Error> {
rmcp::service::Peer::<rmcp::RoleClient>::call_tool(
self,
rmcp::model::CallToolRequestParams::new(name).with_arguments(arguments),
)
.await
.map(RemoteCallResult::from)
.map_err(rmcp_service_error)
}
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
fn coded_remote_error(
code: &str,
retryable: bool,
hint: Option<String>,
error: impl std::fmt::Display,
) -> crate::errors::Error {
crate::errors::Error::Multicast(crate::errors::MulticastError {
message: error.to_string(),
code: code.to_string(),
hint,
retryable,
exit_code: None,
cause: None,
})
}
#[cfg(all(
any(feature = "http", feature = "agent-plugins-mcp"),
not(target_arch = "wasm32")
))]
fn rmcp_service_error(error: rmcp::ServiceError) -> crate::errors::Error {
use rmcp::ServiceError;
let (code, retryable, hint) = match &error {
ServiceError::McpError(_) => ("MCP_JSONRPC_ERROR", false, None),
ServiceError::TransportSend(_) => ("MCP_TRANSPORT_ERROR", true, None),
ServiceError::TransportClosed => ("MCP_TRANSPORT_ERROR", true, None),
ServiceError::Timeout { .. } => ("MCP_TIMEOUT", true, None),
ServiceError::Cancelled { .. } => ("MCP_TRANSPORT_ERROR", false, None),
_ => ("MCP_PROTOCOL_ERROR", false, None),
};
coded_remote_error(code, retryable, hint, error)
}
#[cfg(all(
any(feature = "http", feature = "agent-plugins-mcp"),
not(target_arch = "wasm32")
))]
fn rmcp_initialize_error(error: rmcp::service::ClientInitializeError) -> crate::errors::Error {
use rmcp::service::ClientInitializeError;
let (code, retryable, hint) = match &error {
ClientInitializeError::JsonRpcError(_) => ("MCP_JSONRPC_ERROR", false, None),
ClientInitializeError::TransportError { .. } => ("MCP_TRANSPORT_ERROR", true, None),
ClientInitializeError::ConnectionClosed(_) => ("MCP_TRANSPORT_ERROR", true, None),
ClientInitializeError::NoCompatibleProtocolVersion { .. } => (
"MCP_NEGOTIATION_FAILED",
false,
Some(
"Enable an MCP standard the server supports in McpRemoteOptions::standards"
.to_string(),
),
),
ClientInitializeError::Cancelled => ("MCP_TRANSPORT_ERROR", false, None),
_ => ("MCP_PROTOCOL_ERROR", false, None),
};
coded_remote_error(code, retryable, hint, error)
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
#[async_trait::async_trait]
impl RemoteToolClient for crate::mcp_client::McpHttpClient {
fn remote_deadline(&self) -> Option<(std::time::Duration, crate::outbound::Sleep)> {
self.deadline()
}
async fn remote_list_tools(&self) -> Result<Vec<RemoteTool>, crate::errors::Error> {
let tools = self.list_tools().await?;
tools.iter().map(RemoteTool::from_value).collect()
}
fn remote_parse_tool(&self, value: Value) -> Result<RemoteTool, crate::errors::Error> {
RemoteTool::from_value(&value)
}
async fn remote_call_tool(
&self,
name: String,
arguments: serde_json::Map<String, Value>,
) -> Result<RemoteCallResult, crate::errors::Error> {
self.call_tool(&name, arguments)
.await
.map(|result| RemoteCallResult::from_value(&result))
.map_err(crate::errors::Error::from)
}
async fn remote_call_outcome(
&self,
name: String,
arguments: serde_json::Map<String, Value>,
resume: Option<(String, Value)>,
) -> Result<RemoteOutcome, crate::errors::Error> {
let outcome = match resume {
Some((task_id, responses)) => self.resume_task(&task_id, responses).await,
None => self.call_tool_outcome(&name, arguments).await,
};
Ok(match outcome? {
crate::mcp_client::McpToolOutcome::Complete(result) => {
RemoteOutcome::Complete(RemoteCallResult::from_value(&result))
}
crate::mcp_client::McpToolOutcome::InputRequired {
task_id,
input_requests,
} => RemoteOutcome::InputRequired {
task_id,
input_requests,
},
})
}
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
struct RemoteToolHandler {
client: std::sync::Arc<dyn RemoteToolClient>,
tool: String,
wrapper: Option<String>,
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
#[async_trait::async_trait]
impl crate::command::CommandHandler for RemoteToolHandler {
async fn run(&self, ctx: crate::command::CommandContext) -> crate::output::CommandResult {
let mut arguments = serde_json::Map::new();
for source in [ctx.args, ctx.options] {
if let Some(values) = source.as_object() {
arguments.extend(values.clone());
}
}
let (name, arguments) = if let Some(wrapper) = &self.wrapper {
(
wrapper.clone(),
serde_json::Map::from_iter([
("name".to_string(), Value::String(self.tool.clone())),
("arguments".to_string(), Value::Object(arguments)),
]),
)
} else {
(self.tool.clone(), arguments)
};
let resume = ctx
.mcp
.as_ref()
.and_then(|mcp| Some((mcp.request_state.clone()?, mcp.input_responses.clone()?)));
match self
.client
.remote_call_outcome(name, arguments, resume)
.await
{
Ok(RemoteOutcome::InputRequired {
task_id,
input_requests,
}) => crate::output::CommandResult::InputRequired {
input_requests: input_requests
.as_object()
.map(|requests| {
requests
.iter()
.map(|(key, value)| (key.clone(), value.clone()))
.collect()
})
.unwrap_or_default(),
request_state: Some(task_id),
meta: BTreeMap::new(),
},
Ok(RemoteOutcome::Complete(result)) if !result.is_error => {
crate::output::CommandResult::Ok {
data: result.data(),
cta: None,
exit_code: None,
}
}
Ok(RemoteOutcome::Complete(result)) => crate::output::CommandResult::Error {
code: "REMOTE_MCP_ERROR".to_string(),
message: result.failure_message(),
retryable: false,
exit_code: Some(1),
cta: None,
},
Err(crate::errors::Error::Multicast(error)) => crate::output::CommandResult::Error {
code: error.code,
message: error.message,
retryable: error.retryable,
exit_code: Some(error.exit_code.unwrap_or(1)),
cta: None,
},
Err(error) => crate::output::CommandResult::Error {
code: "REMOTE_MCP_ERROR".to_string(),
message: error.to_string(),
retryable: false,
exit_code: Some(1),
cta: None,
},
}
}
}
#[cfg(feature = "http")]
pub fn remote_skill_commands_with(
uri: impl Into<String>,
options: &McpRemoteOptions,
) -> std::collections::BTreeMap<String, crate::command::CommandDef> {
let source = std::sync::Arc::new(RemoteSkillSource {
uri: uri.into(),
options: options.clone(),
});
let command = |name: &str,
description: &str,
args_fields: Vec<FieldMeta>,
options_fields: Vec<FieldMeta>,
action: RemoteSkillAction| crate::command::CommandDef {
name: name.to_string(),
description: Some(description.to_string()),
args_fields,
options_fields,
env_fields: Vec::new(),
aliases: std::collections::HashMap::new(),
command_aliases: Vec::new(),
examples: Vec::new(),
hint: None,
format: None,
output_policy: None,
handler: Box::new(RemoteSkillHandler {
source: std::sync::Arc::clone(&source),
action,
}),
middleware: Vec::new(),
output_schema: None,
raw: false,
hidden: false,
};
let field = |name: &'static str, description: &'static str, required: bool| FieldMeta {
name,
cli_name: name.to_string(),
description: Some(description),
field_type: crate::schema::FieldType::String,
required,
default: None,
alias: None,
deprecated: false,
env_name: None,
};
std::collections::BTreeMap::from([
(
"list".to_string(),
command(
"list",
"List the skills the server publishes",
Vec::new(),
Vec::new(),
RemoteSkillAction::List,
),
),
(
"approve".to_string(),
command(
"approve",
"Check every file of a published skill and approve it as listed",
vec![field("skill", "The skill's name", true)],
Vec::new(),
RemoteSkillAction::Approve,
),
),
(
"show".to_string(),
command(
"show",
"Read one file of a published skill, checked against its manifest",
vec![field("skill", "The skill's name", true)],
vec![field(
"file",
"A file path inside the skill (defaults to SKILL.md)",
false,
)],
RemoteSkillAction::Show,
),
),
])
}
#[cfg(feature = "http")]
#[derive(Clone, Copy)]
enum RemoteSkillAction {
List,
Show,
Approve,
}
#[cfg(feature = "http")]
struct RemoteSkillSource {
uri: String,
options: McpRemoteOptions,
}
#[cfg(feature = "http")]
impl RemoteSkillSource {
async fn connect(&self) -> Result<crate::mcp_client::McpHttpClient, crate::errors::Error> {
#[cfg(not(target_arch = "wasm32"))]
if self.options.http_client.is_none() {
let client = crate::outbound::ReqwestHttpClient::without_redirects()
.map_err(|error| remote_error(format!("cannot build an HTTP client: {error}")))?;
let mut options = self.options.clone();
options.http_client = Some(std::sync::Arc::new(client));
return Ok(crate::mcp_client::McpHttpClient::connect(&self.uri, &options).await?);
}
Ok(crate::mcp_client::McpHttpClient::connect(&self.uri, &self.options).await?)
}
fn state_directory(&self) -> Option<std::path::PathBuf> {
if let Some(directory) = &self.options.skill_directory {
return Some(directory.clone());
}
#[cfg(not(target_arch = "wasm32"))]
{
dirs::state_dir()
.or_else(dirs::data_local_dir)
.map(|base| base.join("multicast").join("remote-skills"))
}
#[cfg(target_arch = "wasm32")]
None
}
fn approvals_path(&self) -> Option<std::path::PathBuf> {
use sha2::Digest;
let server = sha2::Sha256::digest(self.uri.as_bytes())
.iter()
.map(|byte| format!("{byte:02x}"))
.collect::<String>();
Some(
self.state_directory()?
.join("approvals")
.join(format!("{server}.json")),
)
}
fn load_approvals(
&self,
) -> Result<
std::collections::BTreeMap<String, crate::mcp_client::ApprovedSkill>,
crate::errors::Error,
> {
let Some(path) = self.approvals_path() else {
return Ok(std::collections::BTreeMap::new());
};
let bytes = match std::fs::read(&path) {
Ok(bytes) => bytes,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
return Ok(std::collections::BTreeMap::new());
}
Err(error) => return Err(skill_state_error(&path, error)),
};
let stored: StoredSkillApprovals =
serde_json::from_slice(&bytes).map_err(|error| skill_state_error(&path, error))?;
if stored.server != self.uri {
return Ok(std::collections::BTreeMap::new());
}
Ok(stored.skills)
}
fn store_approvals(
&self,
skills: std::collections::BTreeMap<String, crate::mcp_client::ApprovedSkill>,
) -> Result<(), crate::errors::Error> {
let Some(path) = self.approvals_path() else {
return Err(coded_remote_error(
"MCP_SKILL_STATE_UNAVAILABLE",
false,
None,
"no directory is set for skill approvals".to_string(),
));
};
let stored = StoredSkillApprovals {
server: self.uri.clone(),
skills,
};
let bytes =
serde_json::to_vec_pretty(&stored).map_err(|error| skill_state_error(&path, error))?;
let temporary = path.with_extension("json.tmp");
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|error| skill_state_error(&path, error))?;
}
std::fs::write(&temporary, bytes).map_err(|error| skill_state_error(&path, error))?;
std::fs::rename(&temporary, &path).map_err(|error| skill_state_error(&path, error))
}
async fn read(
&self,
client: &crate::mcp_client::McpHttpClient,
skill: &crate::mcp_client::RemoteSkill,
uri: &str,
) -> Result<Vec<u8>, crate::errors::Error> {
#[cfg(not(target_arch = "wasm32"))]
if let Some(directory) = self.state_directory()
&& let Ok(cache) =
crate::mcp_client::McpSkillDirectoryCache::open(directory.join("files"))
{
return Ok(client.read_skill_file_cached(skill, uri, &cache).await?);
}
Ok(client.read_skill_file_bytes(skill, uri).await?)
}
async fn listed(
&self,
) -> Result<
(
crate::mcp_client::McpHttpClient,
Vec<crate::mcp_client::RemoteSkill>,
),
crate::errors::Error,
> {
let client = self.connect().await?;
if !client.skills_enabled() {
return Err(remote_error(format!(
"the server does not publish skills ({SKILLS_EXTENSION})"
)));
}
let skills = client.list_skills().await?;
Ok((client, skills))
}
async fn list(&self) -> Result<Value, crate::errors::Error> {
let client = self.connect().await?;
if !client.skills_enabled() {
return Ok(Value::Array(Vec::new()));
}
let skills = client.list_skills().await?;
let approvals = self.load_approvals()?;
Ok(Value::Array(
skills
.into_iter()
.map(|skill| {
let approval = match approvals.get(&skill.name) {
Some(approved) if approved.covers(&skill) => "approved",
Some(_) => "changed",
None => "none",
};
serde_json::json!({
"name": skill.name,
"description": skill.frontmatter.get("description").cloned().unwrap_or(Value::Null),
"uri": skill.uri,
"files": skill.files.iter().map(|file| file.uri.clone()).collect::<Vec<_>>(),
"approval": approval,
})
})
.collect(),
))
}
fn find<'a>(
skills: &'a [crate::mcp_client::RemoteSkill],
name: &str,
) -> Result<&'a crate::mcp_client::RemoteSkill, crate::errors::Error> {
skills
.iter()
.find(|skill| skill.name == name)
.ok_or_else(|| {
coded_remote_error(
"MCP_SKILL_NOT_FOUND",
false,
None,
format!("the server publishes no skill named `{name}`"),
)
})
}
async fn show(&self, name: &str, file: Option<&str>) -> Result<Value, crate::errors::Error> {
let (client, skills) = self.listed().await?;
let skill = Self::find(&skills, name)?;
let uri = match file {
None => skill.uri.clone(),
Some(path) => format!("skill://{name}/{}", path.trim_start_matches('/')),
};
let bytes = self.read(&client, skill, &uri).await?;
String::from_utf8(bytes).map(Value::String).map_err(|_| {
coded_remote_error(
"MCP_PROTOCOL_ERROR",
false,
None,
format!("{uri} is not UTF-8 text"),
)
})
}
async fn approve(&self, name: &str) -> Result<Value, crate::errors::Error> {
let (client, skills) = self.listed().await?;
let skill = Self::find(&skills, name)?;
for file in &skill.files {
self.read(&client, skill, &file.uri).await?;
}
let mut approvals = self.load_approvals()?;
approvals.insert(skill.name.clone(), skill.approve());
self.store_approvals(approvals)?;
Ok(serde_json::json!({
"name": skill.name,
"approval": "approved",
"files": skill.files.iter().map(|file| file.uri.clone()).collect::<Vec<_>>(),
}))
}
}
#[cfg(feature = "http")]
#[derive(serde::Serialize, serde::Deserialize)]
struct StoredSkillApprovals {
server: String,
skills: std::collections::BTreeMap<String, crate::mcp_client::ApprovedSkill>,
}
#[cfg(feature = "http")]
fn skill_state_error(
path: &std::path::Path,
error: impl std::fmt::Display,
) -> crate::errors::Error {
coded_remote_error(
"MCP_SKILL_STATE_UNAVAILABLE",
false,
None,
format!("skill approvals at {}: {error}", path.display()),
)
}
#[cfg(feature = "http")]
struct RemoteSkillHandler {
source: std::sync::Arc<RemoteSkillSource>,
action: RemoteSkillAction,
}
#[cfg(feature = "http")]
#[async_trait::async_trait]
impl crate::command::CommandHandler for RemoteSkillHandler {
async fn run(&self, ctx: crate::command::CommandContext) -> crate::output::CommandResult {
let skill = ctx
.args
.get("skill")
.and_then(Value::as_str)
.unwrap_or_default();
let result = match self.action {
RemoteSkillAction::List => self.source.list().await,
RemoteSkillAction::Show => {
let file = ctx.options.get("file").and_then(Value::as_str);
self.source.show(skill, file).await
}
RemoteSkillAction::Approve => self.source.approve(skill).await,
};
match result {
Ok(data) => crate::output::CommandResult::Ok {
data,
cta: None,
exit_code: None,
},
Err(crate::errors::Error::Multicast(error)) => crate::output::CommandResult::Error {
code: error.code,
message: error.message,
retryable: error.retryable,
exit_code: Some(error.exit_code.unwrap_or(1)),
cta: None,
},
Err(error) => crate::output::CommandResult::Error {
code: "REMOTE_MCP_ERROR".to_string(),
message: error.to_string(),
retryable: false,
exit_code: Some(1),
cta: None,
},
}
}
}
#[cfg(feature = "http")]
pub async fn remote_commands(
uri: impl Into<String>,
) -> Result<std::collections::BTreeMap<String, crate::command::CommandDef>, crate::errors::Error> {
remote_commands_with(uri, &McpRemoteOptions::default()).await
}
#[cfg(feature = "http")]
pub async fn remote_commands_with(
uri: impl Into<String>,
options: &McpRemoteOptions,
) -> Result<std::collections::BTreeMap<String, crate::command::CommandDef>, crate::errors::Error> {
let uri = uri.into();
#[cfg(not(target_arch = "wasm32"))]
if options.http_client.is_none() {
use rmcp::transport::StreamableHttpClientTransport;
use rmcp::transport::streamable_http_client::StreamableHttpClientTransportConfig;
let mut config = StreamableHttpClientTransportConfig::with_uri(uri);
if let Some(token) = &options.auth_token {
config = config.auth_header(token.clone());
}
if !options.headers.is_empty() {
config = config.custom_headers(remote_header_map(&options.headers)?);
}
return remote_commands_from_transport(
StreamableHttpClientTransport::from_config(config),
options,
)
.await;
}
let client = crate::mcp_client::McpHttpClient::connect(&uri, options).await?;
remote_commands_from_client(client).await
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
pub async fn remote_commands_from_client(
client: crate::mcp_client::McpHttpClient,
) -> Result<std::collections::BTreeMap<String, crate::command::CommandDef>, crate::errors::Error> {
project_remote_commands(std::sync::Arc::new(client)).await
}
#[cfg(all(feature = "http", not(target_arch = "wasm32")))]
fn remote_header_map(
headers: &[(String, String)],
) -> Result<std::collections::HashMap<http::HeaderName, http::HeaderValue>, crate::errors::Error> {
let mut map = std::collections::HashMap::with_capacity(headers.len());
for (name, value) in headers {
let header = http::HeaderName::try_from(name.as_str()).map_err(|error| {
crate::errors::Error::Multicast(crate::errors::MulticastError {
message: format!("`{name}` is not a valid header name"),
code: "INVALID_HEADER".to_string(),
hint: None,
retryable: false,
exit_code: None,
cause: Some(Box::new(error)),
})
})?;
let parsed = http::HeaderValue::from_str(value).map_err(|error| {
crate::errors::Error::Multicast(crate::errors::MulticastError {
message: format!("the value for header `{name}` is not valid"),
code: "INVALID_HEADER".to_string(),
hint: None,
retryable: false,
exit_code: None,
cause: Some(Box::new(error)),
})
})?;
map.insert(header, parsed);
}
Ok(map)
}
#[cfg(all(
any(feature = "http", feature = "agent-plugins-mcp"),
not(target_arch = "wasm32")
))]
pub(crate) async fn remote_commands_from_transport<T, E, A>(
transport: T,
options: &McpRemoteOptions,
) -> Result<std::collections::BTreeMap<String, crate::command::CommandDef>, crate::errors::Error>
where
T: rmcp::transport::IntoTransport<rmcp::RoleClient, E, A>,
E: std::error::Error + Send + Sync + 'static,
{
use rmcp::model::ClientInfo;
use rmcp::{ClientLifecycleMode, ClientServiceExt};
let versions = rmcp_protocol_versions(&options.standards);
let preferred_versions: Vec<_> = versions
.iter()
.filter(|version| version.as_str() == "2026-07-28")
.cloned()
.collect();
let legacy_version = versions
.into_iter()
.find(|version| version.as_str() != "2026-07-28");
let lifecycle = if preferred_versions.is_empty() {
ClientLifecycleMode::Initialize
} else if legacy_version.is_none() {
ClientLifecycleMode::Discover { preferred_versions }
} else {
ClientLifecycleMode::Auto {
preferred_versions,
legacy_version: legacy_version.clone(),
}
};
let client = ClientInfo::default()
.with_protocol_version(legacy_version.unwrap_or(rmcp::model::ProtocolVersion::V_2026_07_28))
.serve_with_lifecycle(transport, lifecycle)
.await
.map_err(rmcp_initialize_error)?;
project_remote_commands(std::sync::Arc::new(client)).await
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
async fn project_remote_commands(
client: std::sync::Arc<dyn RemoteToolClient>,
) -> Result<std::collections::BTreeMap<String, crate::command::CommandDef>, crate::errors::Error> {
let work = std::pin::pin!(project_remote_commands_unbounded(std::sync::Arc::clone(
&client
)));
if let Some((after, timer)) = client.remote_deadline() {
match futures::future::select(work, timer).await {
futures::future::Either::Left((result, _)) => result,
futures::future::Either::Right(((), _)) => {
Err(crate::mcp_client::McpClientError::Timeout { after }.into())
}
}
} else {
work.await
}
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
async fn project_remote_commands_unbounded(
client: std::sync::Arc<dyn RemoteToolClient>,
) -> Result<std::collections::BTreeMap<String, crate::command::CommandDef>, crate::errors::Error> {
let listed = client.remote_list_tools().await?;
let progressive = {
let names = listed
.iter()
.map(|tool| tool.name.as_str())
.collect::<std::collections::HashSet<_>>();
listed.len() == 4
&& [
"search_tools",
"get_tool_details",
"call_read_tool",
"call_write_tool",
]
.iter()
.all(|name| names.contains(name))
};
let tools = if progressive {
discover_remote_tools(client.as_ref()).await?
} else {
listed
};
let mut commands = std::collections::BTreeMap::new();
for tool in tools {
let required = tool
.input_schema
.get("required")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(Value::as_str)
.map(ToString::to_string)
.collect::<std::collections::HashSet<_>>();
let fields = tool
.input_schema
.get("properties")
.and_then(Value::as_object)
.into_iter()
.flat_map(|properties| properties.iter())
.map(|(name, schema)| remote_field(name, schema, required.contains(name)))
.collect();
let name = tool.name;
commands.insert(
name.clone(),
crate::command::CommandDef {
name: name.clone(),
description: tool.description,
args_fields: Vec::new(),
options_fields: fields,
env_fields: Vec::new(),
aliases: std::collections::HashMap::new(),
command_aliases: Vec::new(),
examples: Vec::new(),
hint: None,
format: None,
output_policy: None,
handler: Box::new(RemoteToolHandler {
client: std::sync::Arc::clone(&client),
wrapper: progressive.then(|| {
if tool.read_only {
"call_read_tool".to_string()
} else {
"call_write_tool".to_string()
}
}),
tool: name,
}),
middleware: Vec::new(),
output_schema: tool.output_schema,
raw: false,
hidden: false,
},
);
}
Ok(commands)
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
async fn discover_remote_tools(
client: &dyn RemoteToolClient,
) -> Result<Vec<RemoteTool>, crate::errors::Error> {
let mut tools = Vec::new();
let mut offset = 0_u64;
let mut pages = 0_usize;
loop {
pages += 1;
let search = client
.remote_call_tool(
"search_tools".to_string(),
serde_json::Map::from_iter([
("query".to_string(), Value::String(String::new())),
("limit".to_string(), Value::from(20)),
("offset".to_string(), Value::from(offset)),
]),
)
.await?;
let value = remote_result_value(search)?;
for name in value["tools"]
.as_array()
.into_iter()
.flatten()
.filter_map(|tool| tool["name"].as_str())
{
if tools.len() >= MAX_REMOTE_TOOLS {
return Err(tool_limit_error(format!(
"MCP tool catalog lists more than {MAX_REMOTE_TOOLS} tools"
)));
}
let details = client
.remote_call_tool(
"get_tool_details".to_string(),
serde_json::Map::from_iter([(
"name".to_string(),
Value::String(name.to_string()),
)]),
)
.await?;
tools.push(client.remote_parse_tool(remote_result_value(details)?)?);
}
let Some(next) = value.get("nextOffset").and_then(Value::as_u64) else {
break;
};
if next <= offset {
return Err(remote_error(
"MCP tool catalog returned a non-advancing offset",
));
}
if pages >= MAX_REMOTE_TOOL_PAGES {
return Err(tool_limit_error(format!(
"MCP tool catalog has more than {MAX_REMOTE_TOOL_PAGES} pages"
)));
}
offset = next;
}
Ok(tools)
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
fn remote_result_value(result: RemoteCallResult) -> Result<Value, crate::errors::Error> {
if result.is_error {
return Err(coded_remote_error(
"REMOTE_MCP_ERROR",
false,
None,
result.failure_message(),
));
}
Ok(result.data())
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
fn remote_error(error: impl std::fmt::Display) -> crate::errors::Error {
coded_remote_error("MCP_PROTOCOL_ERROR", false, None, error)
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
fn tool_limit_error(message: String) -> crate::errors::Error {
coded_remote_error("MCP_TOOL_LIMIT_EXCEEDED", false, None, message)
}
#[cfg(any(feature = "http", feature = "agent-plugins-mcp"))]
fn remote_field(name: &str, schema: &Value, required: bool) -> FieldMeta {
let field_type = match schema.get("type").and_then(Value::as_str) {
Some("boolean") => crate::schema::FieldType::Boolean,
Some("integer" | "number") => crate::schema::FieldType::Number,
Some("array") => crate::schema::FieldType::Array(Box::new(crate::schema::FieldType::Value)),
Some("object") => crate::schema::FieldType::Value,
_ => crate::schema::FieldType::String,
};
let name: &'static str = Box::leak(name.to_string().into_boxed_str());
let description = schema
.get("description")
.and_then(Value::as_str)
.map(|description| &*Box::leak(description.to_string().into_boxed_str()));
FieldMeta {
name,
cli_name: crate::schema::to_kebab(name),
description,
field_type,
required,
default: schema.get("default").cloned(),
alias: None,
deprecated: false,
env_name: None,
}
}
pub fn collect_tools(
commands: &BTreeMap<String, CommandEntry>,
prefix: &[String],
) -> Vec<ToolEntry> {
let mut result: Vec<ToolEntry> = Vec::new();
for (name, entry) in commands {
let mut path = prefix.to_vec();
path.push(name.clone());
if entry.is_group {
result.extend(collect_tools(&entry.commands, &path));
} else {
let tool_name = path.join("_");
let input_schema = entry
.input_schema
.clone()
.unwrap_or_else(|| build_tool_schema(&entry.args_fields, &entry.options_fields));
result.push(ToolEntry {
name: tool_name,
description: entry.description.clone(),
input_schema,
output_schema: entry.output_schema.clone(),
});
}
}
result.sort_by(|a, b| a.name.cmp(&b.name));
result
}
pub(crate) fn build_tool_schema(
args_fields: &[FieldMeta],
options_fields: &[FieldMeta],
) -> serde_json::Value {
let mut properties = serde_json::Map::new();
let mut required: Vec<String> = Vec::new();
for field in args_fields.iter().chain(options_fields.iter()) {
let mut prop = serde_json::Map::new();
prop.insert(
"type".to_string(),
serde_json::Value::String(field_type_to_json_type(&field.field_type)),
);
if let crate::schema::FieldType::Enum(values) = &field.field_type {
prop.insert(
"enum".to_string(),
serde_json::Value::Array(
values
.iter()
.map(|value| serde_json::Value::String(value.clone()))
.collect(),
),
);
}
if let Some(desc) = field.description {
prop.insert(
"description".to_string(),
serde_json::Value::String(desc.to_string()),
);
}
if let Some(default) = &field.default {
prop.insert("default".to_string(), default.clone());
}
properties.insert(field.name.to_string(), serde_json::Value::Object(prop));
if field.required {
required.push(field.name.to_string());
}
}
let mut schema = serde_json::Map::new();
schema.insert(
"type".to_string(),
serde_json::Value::String("object".to_string()),
);
schema.insert(
"properties".to_string(),
serde_json::Value::Object(properties),
);
if !required.is_empty() {
schema.insert(
"required".to_string(),
serde_json::Value::Array(
required
.into_iter()
.map(serde_json::Value::String)
.collect(),
),
);
}
serde_json::Value::Object(schema)
}
fn field_type_to_json_type(ft: &crate::schema::FieldType) -> String {
use crate::schema::FieldType;
match ft {
FieldType::String => "string".to_string(),
FieldType::Number => "number".to_string(),
FieldType::Boolean => "boolean".to_string(),
FieldType::Array(_) => "array".to_string(),
FieldType::Enum(_) => "string".to_string(),
FieldType::Count => "number".to_string(),
FieldType::Value => "string".to_string(),
}
}
#[cfg(not(target_arch = "wasm32"))]
mod server {
use std::borrow::Cow;
use std::sync::Arc;
use serde_json::{Map, Value};
use rmcp::ErrorData as McpError;
use rmcp::handler::server::ServerHandler;
use rmcp::model::{
CacheScope, CallToolRequestParams, CallToolResponse, CallToolResult, CustomNotification,
CustomRequest, CustomResult, GetPromptRequestParams, GetPromptResponse, GetPromptResult,
Implementation, InitializeRequestParams, InitializeResult, InputRequiredResult,
ListPromptsResult, ListResourceTemplatesResult, ListResourcesResult, ListToolsResult,
PaginatedRequestParams, ProgressNotificationParam, ProtocolVersion,
ReadResourceRequestParams, ReadResourceResponse, ReadResourceResult, ServerCapabilities,
ServerInfo, ServerNotification, ServerRequest, SubscribeRequestParams, Tool,
UnsubscribeRequestParams,
};
use rmcp::service::{RequestContext, RoleServer};
#[cfg(test)]
use crate::command::McpResultContent;
#[cfg(test)]
use crate::tool::ToolCallOutcome;
use crate::tool::{EnvironmentSource, ToolCallControl, ToolEvent, ToolEventSink};
use super::McpServeOptions;
pub(super) use super::shared::ServerSource;
use super::shared::{self, CallContext, ResourceContext, ToolServer};
fn non_empty_json_object(value: Value) -> Option<Value> {
if value.as_object().is_some_and(serde_json::Map::is_empty) {
None
} else {
Some(value)
}
}
fn context_protocol_version(context: &RequestContext<RoleServer>) -> Option<String> {
context
.protocol_version()
.or_else(|| context.meta.protocol_version())
.map(|version| version.as_str().to_string())
}
fn context_meta_value(context: &RequestContext<RoleServer>) -> Option<Value> {
serde_json::to_value(&context.meta)
.ok()
.and_then(non_empty_json_object)
}
fn context_client_capabilities(context: &RequestContext<RoleServer>) -> Option<Value> {
context
.meta
.client_capabilities()
.and_then(|capabilities| serde_json::to_value(capabilities).ok())
}
fn resource_context<I: serde::Serialize>(
protocol_version: Option<String>,
request_meta: Option<Value>,
client_capabilities: Option<Value>,
input_responses: &Option<I>,
request_state: Option<String>,
peer: Option<crate::command::McpPeer>,
) -> ResourceContext {
ResourceContext {
request_id: None,
request: None,
protocol_version,
request_meta,
client_capabilities,
input_responses: input_responses
.as_ref()
.and_then(|responses| serde_json::to_value(responses).ok()),
request_state,
peer,
}
}
fn params_with_meta(params: Option<Value>, meta: Option<Value>) -> Option<Value> {
let Some(meta) = meta else {
return params;
};
match params {
Some(Value::Object(mut object)) => {
object.insert("_meta".to_string(), meta);
Some(Value::Object(object))
}
Some(value) => {
let mut object = Map::new();
object.insert("value".to_string(), value);
object.insert("_meta".to_string(), meta);
Some(Value::Object(object))
}
None => {
let mut object = Map::new();
object.insert("_meta".to_string(), meta);
Some(Value::Object(object))
}
}
}
fn native_peer(peer: rmcp::service::Peer<RoleServer>) -> crate::command::McpPeer {
let request_peer = peer.clone();
let notify_peer = peer;
crate::command::McpPeer::with_notify(
move |request: crate::command::McpPeerRequest| {
let peer = request_peer.clone();
async move {
let params = params_with_meta(request.params, request.meta);
let result = peer
.send_request(ServerRequest::CustomRequest(CustomRequest::new(
request.method,
params,
)))
.await
.map_err(|error| crate::command::McpPeerError {
code: "MCP_PEER_REQUEST_FAILED".to_string(),
message: error.to_string(),
data: None,
})?;
serde_json::to_value(result).map_err(|error| crate::command::McpPeerError {
code: "MCP_PEER_RESPONSE_SERIALIZATION_FAILED".to_string(),
message: error.to_string(),
data: None,
})
}
},
move |notification: crate::command::McpPeerNotification| {
let peer = notify_peer.clone();
async move {
let params = params_with_meta(notification.params, notification.meta);
peer.send_notification(ServerNotification::CustomNotification(
CustomNotification::new(notification.method, params),
))
.await
.map_err(|error| crate::command::McpPeerError {
code: "MCP_PEER_NOTIFICATION_FAILED".to_string(),
message: error.to_string(),
data: None,
})
}
},
)
}
fn native_capabilities(tools: &ToolServer, tasks: bool) -> ServerCapabilities {
let mut capabilities = tools.capabilities();
if tasks && let Some(object) = capabilities.as_object_mut() {
let extensions = object
.entry("extensions")
.or_insert_with(|| serde_json::json!({}));
if let Some(extensions) = extensions.as_object_mut() {
extensions.insert(
super::tasks::TASKS_EXTENSION.to_string(),
serde_json::json!({}),
);
}
}
serde_json::from_value(capabilities).unwrap_or_else(|_| {
ServerCapabilities::builder()
.enable_tools()
.enable_prompts()
.enable_resources()
.build()
})
}
fn task_error(error: Value) -> McpError {
let code = error
.get("code")
.and_then(Value::as_i64)
.and_then(|code| i32::try_from(code).ok())
.unwrap_or(-32603);
let message = error
.get("message")
.and_then(Value::as_str)
.unwrap_or("Task error")
.to_string();
McpError::new(
rmcp::model::ErrorCode(code),
message,
error.get("data").cloned(),
)
}
fn resource_error(error: super::McpResourceError) -> McpError {
McpError::new(
rmcp::model::ErrorCode(error.code),
error.message,
error.data,
)
}
async fn resource_call<T>(
future: impl std::future::Future<Output = Result<T, super::McpResourceError>>,
cancellation: tokio_util::sync::CancellationToken,
) -> Result<T, McpError> {
tokio::select! {
biased;
_ = cancellation.cancelled() => Err(McpError::new(
rmcp::model::ErrorCode(-32800), "Resource request cancelled", None,
)),
result = future => result.map_err(resource_error),
}
}
fn call_tool_result(value: Value) -> CallToolResult {
serde_json::from_value(value).expect("shared CallToolResult wire value is valid")
}
fn call_tool_response(value: Value) -> CallToolResponse {
if value.get("resultType") == Some(&Value::String("input_required".to_string())) {
let result: InputRequiredResult = serde_json::from_value(value)
.expect("shared InputRequiredResult wire value is valid");
result.into()
} else {
call_tool_result(value).into()
}
}
#[cfg(test)]
pub(super) fn tool_result_success(
name: &str,
data: Value,
cta: Option<crate::output::CtaBlock>,
structured: bool,
presentation: &[McpResultContent],
) -> CallToolResult {
call_tool_result(shared::tool_result_success(
name,
data,
cta,
structured,
presentation,
))
}
#[cfg(test)]
mod custom_method_tests;
#[cfg(test)]
mod elicitation_tests;
#[cfg(test)]
mod error_tests;
#[cfg(test)]
fn tool_call_result(
name: &str,
outcome: ToolCallOutcome,
structured: bool,
presentation: &[McpResultContent],
) -> CallToolResult {
call_tool_result(shared::tool_call_result(
name,
outcome,
structured,
presentation,
))
}
struct McpEventSink {
peer: rmcp::service::Peer<RoleServer>,
progress_token: Option<rmcp::model::ProgressToken>,
count: tokio::sync::Mutex<u64>,
}
#[async_trait::async_trait]
impl ToolEventSink for McpEventSink {
async fn emit(&self, event: ToolEvent) {
let Some(progress_token) = self.progress_token.clone() else {
return;
};
let message = shared::progress_message(event);
let mut count = self.count.lock().await;
*count += 1;
let _ = self
.peer
.notify_progress(
ProgressNotificationParam::new(progress_token, *count as f64)
.with_message(message),
)
.await;
}
}
#[derive(Clone)]
pub(crate) struct MulticastMcpServer {
tools: ToolServer,
tool_list: Arc<Vec<Tool>>,
tasks: Option<Arc<super::tasks::TaskStore>>,
custom_methods: Option<super::McpCustomMethodHandler>,
}
impl MulticastMcpServer {
pub(super) fn new(
source: &ServerSource<'_>,
options: &McpServeOptions,
) -> Result<Self, crate::errors::Error> {
let tools = ToolServer::new(source, options)?;
let tool_list = tools
.tool_list
.iter()
.map(|tool| {
serde_json::from_value(tool.clone()).expect("shared Tool wire value is valid")
})
.collect();
Ok(MulticastMcpServer {
tools,
tool_list: Arc::new(tool_list),
tasks: None,
custom_methods: options.custom_methods.clone(),
})
}
pub(super) fn with_tasks(
mut self,
tasks: &super::McpServeTasks,
) -> Result<Self, crate::errors::Error> {
if !self.tools.any_task_tool() {
return Ok(self);
}
let spawner = match tasks {
super::McpServeTasks::Off => return Ok(self),
super::McpServeTasks::Custom(spawner) => spawner.clone(),
super::McpServeTasks::Default => {
let spawner = super::McpTaskSpawner::tokio();
match super::default_task_directory(&self.tools.server_name).and_then(
|directory| {
super::McpTaskDirectoryJournal::open_exclusive(directory)
.ok()
.flatten()
},
) {
Some(journal) => spawner.with_journal(journal),
None => spawner,
}
}
};
let store = super::tasks::TaskStore::new(spawner).map_err(|message| {
crate::errors::Error::Other(Box::new(std::io::Error::other(format!(
"could not restore MCP tasks: {message}"
))))
})?;
self.tasks = Some(Arc::new(store));
Ok(self)
}
fn task_store(&self) -> Result<&Arc<super::tasks::TaskStore>, McpError> {
self.tasks.as_ref().ok_or_else(|| {
McpError::new(
rmcp::model::ErrorCode(-32601),
"Method not found: tasks",
None,
)
})
}
fn initialize_protocol_versions(&self) -> Vec<ProtocolVersion> {
super::rmcp_protocol_versions(&self.tools.standards)
.into_iter()
.filter(|version| version.as_str() != "2026-07-28")
.collect()
}
}
impl ServerHandler for MulticastMcpServer {
fn get_info(&self) -> ServerInfo {
let protocol = self
.initialize_protocol_versions()
.into_iter()
.next()
.unwrap_or(ProtocolVersion::V_2026_07_28);
let mut capabilities = native_capabilities(&self.tools, self.tasks.is_some());
if self.custom_methods.is_some() {
capabilities
.extensions
.get_or_insert_with(Default::default)
.insert(super::EVENTS_EXTENSION.to_string(), serde_json::Map::new());
}
let info = ServerInfo::new(capabilities)
.with_protocol_version(protocol)
.with_server_info(Implementation::new(
self.tools.server_name.clone(),
self.tools.server_version.clone(),
));
if let Some(instructions) = &self.tools.instructions {
info.with_instructions(instructions.clone())
} else {
info
}
}
fn supported_protocol_versions(&self) -> Cow<'static, [ProtocolVersion]> {
Cow::Owned(super::rmcp_protocol_versions(&self.tools.standards))
}
fn initialize(
&self,
request: InitializeRequestParams,
context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<InitializeResult, McpError>> + Send + '_
{
context.peer.set_peer_info(request.clone());
let initialize_supported = self.initialize_protocol_versions();
let selected = initialize_supported
.iter()
.find(|version| version.as_str() == request.protocol_version.as_str())
.cloned();
let selected = if selected.is_none()
&& ProtocolVersion::KNOWN_VERSIONS.contains(&request.protocol_version)
{
None
} else {
selected.or_else(|| initialize_supported.first().cloned())
};
std::future::ready(match selected {
Some(selected) => Ok(self.get_info().with_protocol_version(selected)),
None => Err(McpError::unsupported_protocol_version(
request.protocol_version,
&initialize_supported,
)),
})
}
fn list_tools(
&self,
_request: Option<PaginatedRequestParams>,
context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<ListToolsResult, McpError>> + Send + '_
{
let tools = (*self.tool_list).clone();
let mut result = ListToolsResult {
tools,
next_cursor: None,
meta: None,
..Default::default()
};
if context
.protocol_version()
.is_some_and(|version| version.as_str() == "2026-07-28")
{
result = result.with_ttl_ms(0).with_cache_scope(CacheScope::Private);
}
std::future::ready(Ok(result))
}
fn list_prompts(
&self,
_request: Option<PaginatedRequestParams>,
context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<ListPromptsResult, McpError>> + Send + '_
{
let mut result: ListPromptsResult =
serde_json::from_value(self.tools.list_prompts()).unwrap_or_default();
if context
.protocol_version()
.is_some_and(|version| version.as_str() == "2026-07-28")
{
result = result.with_ttl_ms(0).with_cache_scope(CacheScope::Private);
}
std::future::ready(Ok(result))
}
fn get_prompt(
&self,
request: GetPromptRequestParams,
_context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<GetPromptResponse, McpError>> + Send + '_
{
let result = self
.tools
.get_prompt(&request.name, request.arguments.as_ref())
.map_err(|message| McpError::invalid_params(message, None))
.and_then(|value| {
serde_json::from_value::<GetPromptResult>(value)
.map_err(|error| McpError::internal_error(error.to_string(), None))
})
.map(GetPromptResponse::from);
std::future::ready(result)
}
fn list_resources(
&self,
_request: Option<PaginatedRequestParams>,
context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<ListResourcesResult, McpError>> + Send + '_
{
let mut result: ListResourcesResult =
serde_json::from_value(self.tools.list_resources()).unwrap_or_default();
if context
.protocol_version()
.is_some_and(|version| version.as_str() == "2026-07-28")
{
result = result.with_ttl_ms(0).with_cache_scope(CacheScope::Private);
}
std::future::ready(Ok(result))
}
fn list_resource_templates(
&self,
_request: Option<PaginatedRequestParams>,
context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<ListResourceTemplatesResult, McpError>> + Send + '_
{
let mut result: ListResourceTemplatesResult =
serde_json::from_value(self.tools.list_resource_templates()).unwrap_or_default();
if context
.protocol_version()
.is_some_and(|version| version.as_str() == "2026-07-28")
{
result = result.with_ttl_ms(0).with_cache_scope(CacheScope::Private);
}
std::future::ready(Ok(result))
}
fn read_resource(
&self,
request: ReadResourceRequestParams,
context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<ReadResourceResponse, McpError>> + Send + '_
{
let tools = self.tools.clone();
let cancellation = context.ct.clone();
let protocol_version = context_protocol_version(&context);
let modern = protocol_version.as_deref() == Some("2026-07-28");
let request_meta = context_meta_value(&context).or_else(|| {
request
.meta
.as_ref()
.and_then(|meta| serde_json::to_value(meta).ok())
.and_then(non_empty_json_object)
});
let client_capabilities = context_client_capabilities(&context).or_else(|| {
request_meta
.as_ref()
.and_then(|meta| meta.get("io.modelcontextprotocol/clientCapabilities"))
.cloned()
});
let peer = native_peer(context.peer.clone());
async move {
let ctx = resource_context(
protocol_version,
request_meta,
client_capabilities,
&request.input_responses,
request.request_state.clone(),
Some(peer),
);
let mut value =
resource_call(tools.read_resource(request.uri, ctx), cancellation).await?;
if !modern && let Some(object) = value.as_object_mut() {
object.remove("ttlMs");
object.remove("cacheScope");
}
let result: ReadResourceResult = serde_json::from_value(value)
.map_err(|error| McpError::internal_error(error.to_string(), None))?;
Ok(ReadResourceResponse::from(result))
}
}
fn on_custom_request(
&self,
request: CustomRequest,
context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<CustomResult, McpError>>
+ rmcp::service::MaybeSendFuture
+ '_ {
let custom_methods = self.custom_methods.clone();
let tools = self.tools.clone();
let modern = context
.protocol_version()
.is_some_and(|version| version.as_str() == "2026-07-28");
async move {
let params = request.params.unwrap_or(Value::Null);
if let Some(handler) = &custom_methods
&& let Some(outcome) =
handler.call(request.method.clone(), params.clone()).await
{
return match outcome {
Ok(value) => Ok(CustomResult::new(value)),
Err(error) => Err(McpError::new(
rmcp::model::ErrorCode(error.code),
error.message,
error.data,
)),
};
}
let text = |key: &str| params.get(key).and_then(Value::as_str);
if !modern || !tools.skills_published() {
return Err(McpError::new(
rmcp::model::ErrorCode::METHOD_NOT_FOUND,
request.method.clone(),
None,
));
}
match request.method.as_str() {
"skills/list" => tools.list_skills(text("cursor")),
"skills/get" => tools.get_skill(text("uri").unwrap_or_default()),
"resources/directory/read" => {
tools.read_directory(text("uri").unwrap_or_default(), text("cursor"))
}
_ => {
return Err(McpError::new(
rmcp::model::ErrorCode::METHOD_NOT_FOUND,
request.method.clone(),
None,
));
}
}
.map(CustomResult::new)
.map_err(resource_error)
}
}
#[allow(deprecated)]
fn subscribe(
&self,
request: SubscribeRequestParams,
context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<(), McpError>> + Send + '_ {
let tools = self.tools.clone();
let cancellation = context.ct.clone();
let protocol_version = context
.protocol_version()
.map(|version| version.as_str().to_string());
let peer = native_peer(context.peer.clone());
async move {
resource_call(
tools.set_resource_subscription(
request.uri,
true,
ResourceContext {
protocol_version,
peer: Some(peer),
..ResourceContext::default()
},
),
cancellation,
)
.await?;
Ok(())
}
}
#[allow(deprecated)]
fn unsubscribe(
&self,
request: UnsubscribeRequestParams,
context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<(), McpError>> + Send + '_ {
let tools = self.tools.clone();
let cancellation = context.ct.clone();
let protocol_version = context
.protocol_version()
.map(|version| version.as_str().to_string());
let peer = native_peer(context.peer.clone());
async move {
resource_call(
tools.set_resource_subscription(
request.uri,
false,
ResourceContext {
protocol_version,
peer: Some(peer),
..ResourceContext::default()
},
),
cancellation,
)
.await?;
Ok(())
}
}
fn get_task(
&self,
request: rmcp::model::GetTaskParams,
_context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<rmcp::model::GetTaskResult, McpError>> + Send + '_
{
let result = self.task_store().and_then(|tasks| {
let value = tasks.get(&request.task_id, None).map_err(task_error)?;
serde_json::from_value(value)
.map_err(|error| McpError::internal_error(error.to_string(), None))
});
std::future::ready(result)
}
fn update_task(
&self,
request: rmcp::model::UpdateTaskParams,
_context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<(), McpError>> + Send + '_ {
let result = self.task_store().and_then(|tasks| {
let responses = serde_json::to_value(&request.input_responses)
.map_err(|error| McpError::invalid_params(error.to_string(), None))?;
tasks
.update(&self.tools, &request.task_id, None, responses)
.map(|_| ())
.map_err(task_error)
});
std::future::ready(result)
}
fn cancel_task(
&self,
request: rmcp::model::CancelTaskParams,
_context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<(), McpError>> + Send + '_ {
let result = self.task_store().and_then(|tasks| {
tasks
.cancel(&request.task_id, None)
.map(|_| ())
.map_err(task_error)
});
std::future::ready(result)
}
fn call_tool(
&self,
request: CallToolRequestParams,
context: RequestContext<RoleServer>,
) -> impl std::future::Future<Output = Result<CallToolResponse, McpError>> + Send + '_
{
let tools = self.tools.clone();
let progress_token = context.meta.get_progress_token();
#[cfg(feature = "http")]
let transport_request =
context
.extensions
.get::<axum::http::request::Parts>()
.map(|parts| crate::command::RequestContext {
elicitation: None,
headers: parts
.headers
.iter()
.filter_map(|(name, value)| {
value
.to_str()
.ok()
.map(|value| (name.as_str().to_string(), value.to_string()))
})
.collect(),
method: parts.method.to_string(),
path: parts.uri.path().to_string(),
});
#[cfg(not(feature = "http"))]
let transport_request: Option<crate::command::RequestContext> = None;
let channel = super::elicitation_for_request(&context);
let transport_request = if transport_request.is_some()
|| context
.client_capabilities()
.is_some_and(|caps| caps.elicitation.is_some())
{
#[cfg(feature = "http")]
let request = transport_request.unwrap_or_default();
#[cfg(not(feature = "http"))]
let request = crate::command::RequestContext::default();
Some(crate::command::RequestContext {
elicitation: Some(channel),
..request
})
} else {
None
};
let request_meta = context_meta_value(&context).or_else(|| {
request
.meta
.as_ref()
.and_then(|meta| serde_json::to_value(meta).ok())
.and_then(non_empty_json_object)
});
let client_capabilities = context_client_capabilities(&context).or_else(|| {
request_meta
.as_ref()
.and_then(|meta| meta.get("io.modelcontextprotocol/clientCapabilities"))
.cloned()
});
let protocol_version =
context_protocol_version(&context).unwrap_or_else(|| "2025-03-26".to_string());
if let Some(tasks) = &self.tasks
&& protocol_version == "2026-07-28"
&& super::tasks::opted_in(client_capabilities.as_ref())
&& tools.task_allowed(&request.name, request.arguments.as_ref())
&& let Some(created) = tasks.create(
&tools,
None,
super::tasks::TaskCall {
name: request.name.to_string(),
arguments: request.arguments.clone(),
protocol_version: protocol_version.clone(),
request: transport_request.clone(),
request_meta: request_meta.clone(),
client_capabilities: client_capabilities.clone(),
environment: EnvironmentSource::DeclaredHost,
},
)
{
let created = serde_json::from_value::<rmcp::model::CreateTaskResult>(created)
.map(CallToolResponse::Task)
.map_err(|error| McpError::internal_error(error.to_string(), None));
return futures::future::Either::Left(std::future::ready(created));
}
let peer = context.peer;
let mcp_peer = native_peer(peer.clone());
let cancellation = context.ct;
futures::future::Either::Right(async move {
let result = tools
.call_tool(
request.name.to_string(),
request.arguments,
CallContext {
protocol_version,
request: transport_request,
request_meta,
client_capabilities,
input_responses: request
.input_responses
.clone()
.map(serde_json::to_value)
.transpose()
.ok()
.flatten(),
request_state: request.request_state.clone(),
peer: Some(mcp_peer),
control: ToolCallControl {
cancellation,
events: Some(Arc::new(McpEventSink {
peer,
progress_token,
count: tokio::sync::Mutex::new(0),
})),
},
environment: EnvironmentSource::DeclaredHost,
},
)
.await
.map_err(|message| McpError::invalid_params(message, None))?;
Ok(call_tool_response(result))
})
}
}
pub(super) async fn serve(
source: ServerSource<'_>,
options: &McpServeOptions,
) -> Result<(), crate::errors::Error> {
use rmcp::ServiceExt;
use rmcp::transport::io::stdio;
let server = crate::telemetry::McpService::new(
MulticastMcpServer::new(&source, options)?.with_tasks(&options.tasks)?,
source.open_telemetry,
);
let transport = stdio();
let running = server.serve(transport).await.map_err(|e| {
crate::errors::Error::Other(Box::new(std::io::Error::other(format!(
"MCP server failed to start: {e}"
))))
})?;
let _quit_reason = running.waiting().await.map_err(|e| {
crate::errors::Error::Other(Box::new(std::io::Error::other(format!(
"MCP server task failed: {e}"
))))
})?;
Ok(())
}
#[cfg(feature = "http")]
pub(crate) fn http_service(
source: ServerSource<'_>,
options: &McpServeOptions,
) -> Result<
rmcp::transport::StreamableHttpService<
crate::telemetry::McpService<MulticastMcpServer>,
rmcp::transport::streamable_http_server::session::local::LocalSessionManager,
>,
crate::errors::Error,
> {
use rmcp::transport::{StreamableHttpServerConfig, StreamableHttpService};
let server = crate::telemetry::McpService::new(
MulticastMcpServer::new(&source, options)?,
source.open_telemetry,
);
let mut config = StreamableHttpServerConfig::default();
config.legacy_session_mode = false;
Ok(StreamableHttpService::new(
move || Ok(server.clone()),
Default::default(),
config,
))
}
}
#[cfg(all(feature = "http", not(target_arch = "wasm32")))]
pub(crate) fn http_service(
cli: &crate::cli::Cli,
) -> Result<
rmcp::transport::StreamableHttpService<
impl rmcp::ServerHandler,
rmcp::transport::streamable_http_server::session::local::LocalSessionManager,
>,
crate::errors::Error,
> {
server::http_service(server::ServerSource::from_cli(cli), &cli.mcp_options)
}
pub async fn serve_cli(cli: &crate::cli::Cli) -> Result<(), crate::errors::Error> {
#[cfg(not(target_arch = "wasm32"))]
{
server::serve(server::ServerSource::from_cli(cli), &cli.mcp_options).await
}
#[cfg(target_arch = "wasm32")]
{
let _ = cli;
Err(stdio_unavailable())
}
}
pub async fn serve(
name: &str,
version: &str,
commands: &std::collections::BTreeMap<String, crate::cli::CommandEntry>,
root_middleware: &[crate::middleware::MiddlewareFn],
env_fields: &[FieldMeta],
options: &McpServeOptions,
) -> Result<(), crate::errors::Error> {
#[cfg(not(target_arch = "wasm32"))]
{
server::serve(
server::ServerSource::from_parts(name, version, commands, root_middleware, env_fields),
options,
)
.await
}
#[cfg(target_arch = "wasm32")]
{
let _ = (
name,
version,
commands,
root_middleware,
env_fields,
options,
);
Err(stdio_unavailable())
}
}
#[cfg(target_arch = "wasm32")]
fn stdio_unavailable() -> crate::errors::Error {
crate::errors::Error::Multicast(crate::errors::MulticastError {
message: "MCP over stdio is not available on wasm32".to_string(),
code: "MCP_STDIO_UNAVAILABLE".to_string(),
hint: Some("Serve tools through the host's HTTP adapter over ToolCatalog".to_string()),
retryable: false,
exit_code: None,
cause: None,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::schema::{FieldType, to_kebab};
#[test]
fn test_tool_result_success_presents_declared_image_content() {
use crate::command::McpResultContent;
let data = serde_json::json!({
"preview": {
"data": "aW1hZ2U=",
"mimeType": "image/png"
},
"artifactUrl": "https://example.test/artifacts/ui.png"
});
let presentation = vec![McpResultContent::Image {
data_pointer: "/preview/data".to_string(),
mime_type_pointer: "/preview/mimeType".to_string(),
}];
let result = server::tool_result_success("visualize", data, None, true, &presentation);
let encoded = serde_json::to_value(result.content).expect("content serializes");
assert_eq!(encoded[0]["type"], "text");
assert_eq!(encoded[1]["type"], "image");
assert_eq!(encoded[1]["data"], "aW1hZ2U=");
assert_eq!(encoded[1]["mimeType"], "image/png");
}
fn make_field(name: &'static str, ft: FieldType, required: bool) -> FieldMeta {
FieldMeta {
name,
cli_name: to_kebab(name),
description: Some("A field"),
field_type: ft,
required,
default: None,
alias: None,
deprecated: false,
env_name: None,
}
}
fn make_leaf(desc: &str) -> CommandEntry {
CommandEntry {
is_group: false,
description: Some(desc.to_string()),
commands: BTreeMap::new(),
args_fields: vec![],
options_fields: vec![],
output_schema: None,
input_schema: None,
}
}
fn make_leaf_with_input_schema(desc: &str, input_schema: serde_json::Value) -> CommandEntry {
CommandEntry {
input_schema: Some(input_schema),
..make_leaf(desc)
}
}
fn make_group(desc: &str, commands: BTreeMap<String, CommandEntry>) -> CommandEntry {
CommandEntry {
is_group: true,
description: Some(desc.to_string()),
commands,
args_fields: vec![],
options_fields: vec![],
output_schema: None,
input_schema: None,
}
}
#[test]
fn test_collect_tools_flat() {
let mut commands = BTreeMap::new();
commands.insert("deploy".to_string(), make_leaf("Deploy app"));
commands.insert("status".to_string(), make_leaf("Show status"));
let tools = collect_tools(&commands, &[]);
assert_eq!(tools.len(), 2);
assert_eq!(tools[0].name, "deploy");
assert_eq!(tools[1].name, "status");
}
#[test]
fn test_collect_tools_nested() {
let mut sub = BTreeMap::new();
sub.insert("app".to_string(), make_leaf("Deploy app"));
sub.insert("config".to_string(), make_leaf("Deploy config"));
let mut commands = BTreeMap::new();
commands.insert("deploy".to_string(), make_group("Deploy group", sub));
commands.insert("status".to_string(), make_leaf("Show status"));
let tools = collect_tools(&commands, &[]);
assert_eq!(tools.len(), 3);
assert_eq!(tools[0].name, "deploy_app");
assert_eq!(tools[1].name, "deploy_config");
assert_eq!(tools[2].name, "status");
}
#[test]
fn test_collect_tools_honors_input_schema_override() {
let published = serde_json::json!({
"type": "object",
"properties": {
"steps": {
"type": "array",
"items": {"type": "object"},
},
},
});
let mut commands = BTreeMap::new();
commands.insert(
"run".to_string(),
make_leaf_with_input_schema("Run steps", published.clone()),
);
commands.insert("status".to_string(), make_leaf("Show status"));
let tools = collect_tools(&commands, &[]);
assert_eq!(tools[0].name, "run");
assert_eq!(tools[0].input_schema, published);
assert_ne!(tools[1].input_schema, published);
}
#[test]
fn test_collect_tools_sorted() {
let mut commands = BTreeMap::new();
commands.insert("zebra".to_string(), make_leaf("Z"));
commands.insert("alpha".to_string(), make_leaf("A"));
let tools = collect_tools(&commands, &[]);
assert_eq!(tools[0].name, "alpha");
assert_eq!(tools[1].name, "zebra");
}
#[test]
fn test_build_tool_schema_basic() {
let args = vec![make_field("target", FieldType::String, true)];
let opts = vec![make_field("verbose", FieldType::Boolean, false)];
let schema = build_tool_schema(&args, &opts);
let obj = schema.as_object().unwrap();
assert_eq!(obj["type"], "object");
let props = obj["properties"].as_object().unwrap();
assert!(props.contains_key("target"));
assert!(props.contains_key("verbose"));
let required = obj["required"].as_array().unwrap();
assert_eq!(required.len(), 1);
assert_eq!(required[0], "target");
}
#[test]
fn test_build_tool_schema_no_required() {
let schema = build_tool_schema(&[], &[make_field("verbose", FieldType::Boolean, false)]);
let obj = schema.as_object().unwrap();
assert!(!obj.contains_key("required"));
}
#[test]
fn test_field_type_to_json_type() {
assert_eq!(field_type_to_json_type(&FieldType::String), "string");
assert_eq!(field_type_to_json_type(&FieldType::Number), "number");
assert_eq!(field_type_to_json_type(&FieldType::Boolean), "boolean");
assert_eq!(
field_type_to_json_type(&FieldType::Array(Box::new(FieldType::String))),
"array"
);
assert_eq!(
field_type_to_json_type(&FieldType::Enum(vec!["a".to_string()])),
"string"
);
assert_eq!(field_type_to_json_type(&FieldType::Count), "number");
}
#[cfg(feature = "http")]
#[tokio::test]
async fn test_remote_commands_discovers_progressive_catalog() {
struct Ping;
#[async_trait::async_trait]
impl crate::command::CommandHandler for Ping {
async fn run(
&self,
_ctx: crate::command::CommandContext,
) -> crate::output::CommandResult {
crate::output::CommandResult::Ok {
data: serde_json::json!({ "pong": true }),
cta: None,
exit_code: None,
}
}
}
let cli = crate::cli::Cli::create("remote-test").command(
"ping",
crate::command::CommandDef {
name: "ping".to_string(),
description: Some("Ping the server".to_string()),
args_fields: Vec::new(),
options_fields: Vec::new(),
env_fields: Vec::new(),
aliases: std::collections::HashMap::new(),
command_aliases: Vec::new(),
examples: Vec::new(),
hint: None,
format: None,
output_policy: None,
handler: Box::new(Ping),
middleware: Vec::new(),
output_schema: Some(serde_json::json!({
"type": "object",
"properties": { "pong": { "type": "boolean" } },
})),
raw: false,
hidden: false,
},
);
let app = axum::Router::new().nest_service("/mcp", http_service(&cli).unwrap());
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
let commands = remote_commands(format!("http://{addr}/mcp")).await.unwrap();
server.abort();
assert_eq!(commands.keys().cloned().collect::<Vec<_>>(), vec!["ping"]);
assert_eq!(
commands["ping"].description.as_deref(),
Some("Ping the server")
);
assert!(commands["ping"].output_schema.is_some());
}
#[cfg(feature = "http")]
#[tokio::test]
async fn test_mcp_calls_use_tool_catalog_config_defaults() {
use rmcp::ServiceExt;
use rmcp::transport::StreamableHttpClientTransport;
struct EchoOptions;
#[async_trait::async_trait]
impl crate::command::CommandHandler for EchoOptions {
async fn run(
&self,
ctx: crate::command::CommandContext,
) -> crate::output::CommandResult {
crate::output::CommandResult::Ok {
data: serde_json::json!({ "options": ctx.options }),
cta: None,
exit_code: None,
}
}
}
let config_path = std::env::temp_dir().join(format!(
"multicast-mcp-catalog-defaults-{}.json",
std::process::id()
));
std::fs::write(
&config_path,
serde_json::json!({
"commands": {
"echo": {
"options": { "profile": "configured" }
}
}
})
.to_string(),
)
.unwrap();
fn cli(discovery: McpDiscovery, config_path: &std::path::Path) -> crate::cli::Cli {
let mut command = crate::command::CommandDef::build("echo", EchoOptions).done();
command.options_fields = vec![make_field("profile", FieldType::String, false)];
command.output_schema = Some(serde_json::json!({
"type": "object",
"properties": {
"options": {
"type": "object",
"properties": { "profile": { "type": "string" } }
}
}
}));
crate::cli::Cli::create("catalog-test")
.config(crate::cli::ConfigOptions {
flag: "config".to_string(),
files: vec![config_path.to_string_lossy().into_owned()],
})
.mcp(McpServeOptions {
tools: McpToolFilter {
discovery,
..Default::default()
},
..Default::default()
})
.command("echo", command)
}
let direct_cli = cli(McpDiscovery::Direct, &config_path);
let direct_app =
axum::Router::new().nest_service("/mcp", http_service(&direct_cli).unwrap());
let direct_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let direct_addr = direct_listener.local_addr().unwrap();
let direct_server =
tokio::spawn(async move { axum::serve(direct_listener, direct_app).await.unwrap() });
let direct_client = ()
.serve(StreamableHttpClientTransport::from_uri(format!(
"http://{direct_addr}/mcp"
)))
.await
.unwrap();
let direct = direct_client
.call_tool(
rmcp::model::CallToolRequestParams::new("echo")
.with_arguments(serde_json::Map::new()),
)
.await
.unwrap();
direct_server.abort();
let progressive_cli = cli(McpDiscovery::Progressive, &config_path);
let progressive_app =
axum::Router::new().nest_service("/mcp", http_service(&progressive_cli).unwrap());
let progressive_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let progressive_addr = progressive_listener.local_addr().unwrap();
let progressive_server = tokio::spawn(async move {
axum::serve(progressive_listener, progressive_app)
.await
.unwrap()
});
let progressive_client = ()
.serve(StreamableHttpClientTransport::from_uri(format!(
"http://{progressive_addr}/mcp"
)))
.await
.unwrap();
let progressive = progressive_client
.call_tool(
rmcp::model::CallToolRequestParams::new("call_write_tool").with_arguments(
serde_json::Map::from_iter([
("name".to_string(), serde_json::json!("echo")),
("arguments".to_string(), serde_json::json!({})),
]),
),
)
.await
.unwrap();
progressive_server.abort();
let _ = std::fs::remove_file(config_path);
assert_eq!(
direct.structured_content.unwrap()["options"]["profile"],
"configured"
);
assert_eq!(
progressive.structured_content.unwrap()["options"]["profile"],
"configured"
);
}
#[cfg(not(target_arch = "wasm32"))]
#[tokio::test]
async fn native_duplex_transport_carries_metadata_resources_and_peer_requests() {
use crate::command::{CommandContext, CommandDef, McpCommandOptions, McpPeerRequest};
use crate::output::CommandResult;
use rmcp::model::{
CallToolRequestParams, ClientInfo, CustomRequest, CustomResult, ErrorData as McpError,
Implementation, ProtocolVersion, ReadResourceRequestParams,
};
use rmcp::service::{RequestContext, RoleClient};
use rmcp::{ClientHandler, ClientServiceExt, ServiceExt};
struct ContextTool;
#[async_trait::async_trait]
impl crate::command::CommandHandler for ContextTool {
async fn run(&self, ctx: CommandContext) -> CommandResult {
let mcp = ctx.mcp.expect("MCP context");
let peer = mcp.peer.clone().expect("connected peer");
let peer_response = peer
.request(McpPeerRequest {
method: "client/echo".to_string(),
params: Some(serde_json::json!({ "from": "server" })),
meta: None,
})
.await
.expect("peer response");
CommandResult::Ok {
data: serde_json::json!({
"protocolVersion": mcp.protocol_version,
"requestMeta": mcp.request_meta,
"clientCapabilities": mcp.client_capabilities,
"peer": peer_response,
}),
cta: None,
exit_code: None,
}
}
}
struct EchoClient;
impl ClientHandler for EchoClient {
fn get_info(&self) -> ClientInfo {
let mut info = ClientInfo::default();
info.protocol_version = ProtocolVersion::V_2026_07_28;
info.client_info = Implementation::new("native-duplex", "1.0.0");
info
}
fn on_custom_request(
&self,
request: CustomRequest,
_context: RequestContext<RoleClient>,
) -> impl Future<Output = Result<CustomResult, McpError>> + rmcp::service::MaybeSendFuture + '_
{
std::future::ready(Ok(CustomResult::new(serde_json::json!({
"method": request.method,
"params": request.params,
}))))
}
}
let mut command = CommandDef::build("context", ContextTool)
.description("Return MCP context")
.mcp(McpCommandOptions {
name: Some("native.context".to_string()),
title: Some("Native Context".to_string()),
meta: BTreeMap::from([("example/tool".to_string(), serde_json::json!(true))]),
result_meta: BTreeMap::from([(
"example/result".to_string(),
serde_json::json!(true),
)]),
direct: true,
..McpCommandOptions::default()
})
.done();
command.output_schema = Some(serde_json::json!({ "type": "object" }));
let mut resources = McpResourceRegistry::default();
resources.resources.push(McpResource {
uri: "memory://native".to_string(),
name: "native-resource".to_string(),
title: Some("Native Resource".to_string()),
description: Some("Resource served over native transport".to_string()),
mime_type: Some("text/plain".to_string()),
size: Some(6),
icons: Vec::new(),
meta: BTreeMap::from([("example/resource".to_string(), serde_json::json!(true))]),
});
struct ResourceDrop(std::sync::Arc<std::sync::atomic::AtomicBool>);
impl Drop for ResourceDrop {
fn drop(&mut self) {
self.0.store(true, std::sync::atomic::Ordering::SeqCst);
}
}
let resource_started = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let resource_dropped = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let started = resource_started.clone();
let dropped = resource_dropped.clone();
resources.read = Some(McpResourceReader::new(move |request| {
let started = started.clone();
let dropped = dropped.clone();
async move {
if request.uri == "memory://pending" {
let _guard = ResourceDrop(dropped);
started.store(true, std::sync::atomic::Ordering::SeqCst);
std::future::pending::<()>().await;
}
Ok(McpResourceReadResult {
contents: vec![McpResourceContents::Text {
uri: request.uri,
mime_type: Some("text/plain".to_string()),
text: "native".to_string(),
meta: BTreeMap::from([(
"example/content".to_string(),
serde_json::json!(true),
)]),
}],
meta: BTreeMap::from([("example/read".to_string(), serde_json::json!(true))]),
})
}
}));
let cli = crate::cli::Cli::create("native-duplex")
.mcp(McpServeOptions {
tools: McpToolFilter {
discovery: McpDiscovery::Direct,
..Default::default()
},
resources,
capabilities: BTreeMap::from([
(
"extensions".to_string(),
serde_json::json!({ "example/extensions": { "enabled": true } }),
),
(
"experimental".to_string(),
serde_json::json!({ "example/feature": { "mode": "on" } }),
),
]),
..Default::default()
})
.command("context", command);
let (server_io, client_io) = tokio::io::duplex(32 * 1024);
let (server_read, server_write) = tokio::io::split(server_io);
let (client_read, client_write) = tokio::io::split(client_io);
let server = server::MulticastMcpServer::new(
&server::ServerSource::from_cli(&cli),
&cli.mcp_options,
)
.unwrap();
let server_task = tokio::spawn(async move {
let running = server
.serve((server_read, server_write))
.await
.expect("server starts");
let _ = running.waiting().await;
});
let client = EchoClient
.serve_with_lifecycle(
(client_read, client_write),
rmcp::ClientLifecycleMode::Discover {
preferred_versions: vec![ProtocolVersion::V_2026_07_28],
},
)
.await
.expect("client discovers server");
let info = client.peer_info().expect("server info");
let capabilities =
serde_json::to_value(&info.capabilities).expect("capabilities serialize");
assert_eq!(
capabilities["extensions"]["example/extensions"]["enabled"],
true
);
assert_eq!(
capabilities["experimental"]["example/feature"]["mode"],
"on"
);
let tools = client.list_tools(None).await.expect("tools list");
let tool = serde_json::to_value(&tools.tools[0]).expect("tool serializes");
assert_eq!(tool["name"], "native.context");
assert_eq!(tool["title"], "Native Context");
assert_eq!(tool["_meta"]["example/tool"], true);
let resources = client.list_resources(None).await.expect("resources list");
let resource = serde_json::to_value(&resources.resources[0]).expect("resource serializes");
assert_eq!(resource["uri"], "memory://native");
assert_eq!(resource["_meta"]["example/resource"], true);
let read = client
.read_resource(ReadResourceRequestParams::new("memory://native"))
.await
.expect("resource read");
let read = serde_json::to_value(read).expect("read result serializes");
assert_eq!(read["contents"][0]["text"], "native");
assert_eq!(read["contents"][0]["_meta"]["example/content"], true);
assert_eq!(read["_meta"]["example/read"], true);
let result = client
.call_tool(
CallToolRequestParams::new("native.context").with_arguments(serde_json::Map::new()),
)
.await
.expect("tool call");
let result = serde_json::to_value(result).expect("tool result serializes");
assert_eq!(result["structuredContent"]["protocolVersion"], "2026-07-28");
assert!(result["structuredContent"]["requestMeta"].is_object());
assert!(result["structuredContent"]["clientCapabilities"].is_object());
assert_eq!(result["structuredContent"]["peer"]["method"], "client/echo");
assert_eq!(
result["structuredContent"]["peer"]["params"]["from"],
"server"
);
assert_eq!(result["_meta"]["example/result"], true);
let pending_request = client
.send_cancellable_request(
rmcp::model::ClientRequest::ReadResourceRequest(
rmcp::model::ReadResourceRequest::new(ReadResourceRequestParams::new(
"memory://pending",
)),
),
rmcp::service::PeerRequestOptions::no_options(),
)
.await
.expect("pending read is sent");
tokio::time::timeout(std::time::Duration::from_secs(2), async {
while !resource_started.load(std::sync::atomic::Ordering::SeqCst) {
tokio::task::yield_now().await;
}
})
.await
.expect("resource callback started");
pending_request
.cancel(Some("literal cancellation control".to_string()))
.await
.expect("cancel notification sent");
tokio::time::timeout(std::time::Duration::from_secs(2), async {
while !resource_dropped.load(std::sync::atomic::Ordering::SeqCst) {
tokio::task::yield_now().await;
}
})
.await
.expect("cancelled resource callback is dropped");
drop(client);
server_task.abort();
}
#[cfg(not(target_arch = "wasm32"))]
fn native_task_cli(held: &Arc<std::sync::atomic::AtomicU8>) -> crate::cli::Cli {
use crate::command::{CommandContext, CommandDef, McpCommandOptions, McpTaskSupport};
use crate::output::CommandResult;
struct Echo;
#[async_trait::async_trait]
impl crate::command::CommandHandler for Echo {
async fn run(&self, ctx: CommandContext) -> CommandResult {
CommandResult::Ok {
data: serde_json::json!({ "echo": ctx.options }),
cta: None,
exit_code: None,
}
}
}
struct Hold(Arc<std::sync::atomic::AtomicU8>);
struct Stopped(Arc<std::sync::atomic::AtomicU8>);
impl Drop for Stopped {
fn drop(&mut self) {
self.0.store(2, std::sync::atomic::Ordering::SeqCst);
}
}
#[async_trait::async_trait]
impl crate::command::CommandHandler for Hold {
async fn run(&self, _ctx: CommandContext) -> CommandResult {
let _stopped = Stopped(Arc::clone(&self.0));
self.0.store(1, std::sync::atomic::Ordering::SeqCst);
futures::future::pending::<()>().await;
unreachable!("a held command only ends by cancellation")
}
}
let task = |name: &'static str, support: McpTaskSupport| McpCommandOptions {
name: Some(name.to_string()),
task: support,
direct: true,
..McpCommandOptions::default()
};
let held = Arc::clone(held);
let mut echo = CommandDef::build("echo", Echo)
.description("Echo as a task")
.mcp(task("echo", McpTaskSupport::Allowed))
.done();
echo.options_fields = vec![crate::schema::FieldMeta {
name: "message",
cli_name: "message".to_string(),
description: None,
field_type: crate::schema::FieldType::String,
required: false,
default: None,
alias: None,
deprecated: false,
env_name: None,
}];
crate::cli::Cli::create("native-tasks")
.command("echo", echo)
.command(
"inline",
CommandDef::build("inline", Echo)
.description("Echo inline")
.mcp(task("inline", McpTaskSupport::Never))
.done(),
)
.command(
"hold",
CommandDef::build("hold", Hold(held))
.description("Hold until cancelled")
.mcp(task("hold", McpTaskSupport::Allowed))
.done(),
)
}
#[cfg(not(target_arch = "wasm32"))]
async fn native_task_client(
cli: &crate::cli::Cli,
tasks: &McpServeTasks,
opt_in: bool,
) -> (
rmcp::service::RunningService<rmcp::RoleClient, rmcp::model::ClientInfo>,
tokio::task::JoinHandle<()>,
) {
use rmcp::model::{ClientInfo, Implementation, ProtocolVersion};
use rmcp::{ClientServiceExt, ServiceExt};
let (server_io, client_io) = tokio::io::duplex(64 * 1024);
let server =
server::MulticastMcpServer::new(&server::ServerSource::from_cli(cli), &cli.mcp_options)
.unwrap()
.with_tasks(tasks)
.unwrap();
let server_task = tokio::spawn(async move {
let running = server
.serve(tokio::io::split(server_io))
.await
.expect("server starts");
let _ = running.waiting().await;
});
let mut info = ClientInfo::default();
info.protocol_version = ProtocolVersion::V_2026_07_28;
info.client_info = Implementation::new("native-tasks", "1.0.0");
if opt_in {
info.capabilities = serde_json::from_value(serde_json::json!({
"extensions": { "io.modelcontextprotocol/tasks": {} }
}))
.unwrap();
}
let client = info
.serve_with_lifecycle(
tokio::io::split(client_io),
rmcp::ClientLifecycleMode::Discover {
preferred_versions: vec![ProtocolVersion::V_2026_07_28],
},
)
.await
.expect("client discovers server");
(client, server_task)
}
#[cfg(not(target_arch = "wasm32"))]
async fn native_settled_task(
client: &rmcp::service::RunningService<rmcp::RoleClient, rmcp::model::ClientInfo>,
id: &str,
) -> serde_json::Value {
for _ in 0..500 {
let task = client
.get_task(rmcp::model::GetTaskParams::new(id))
.await
.expect("tasks/get");
let task = serde_json::to_value(task).unwrap();
if task["status"] != "working" {
return task;
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
panic!("task {id} did not settle");
}
#[cfg(not(target_arch = "wasm32"))]
fn native_task_id(response: rmcp::model::CallToolResponse) -> String {
match response {
rmcp::model::CallToolResponse::Task(created) => {
let created = serde_json::to_value(created).unwrap();
created["taskId"]
.as_str()
.unwrap_or_else(|| panic!("task id in {created}"))
.to_string()
}
other => panic!("expected a task, got {other:?}"),
}
}
#[cfg(not(target_arch = "wasm32"))]
fn native_task_directory() -> std::path::PathBuf {
static SEQUENCE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
let directory = std::env::temp_dir().join(format!(
"multicast-native-tasks-{}-{}",
std::process::id(),
SEQUENCE.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
));
let _ = std::fs::remove_dir_all(&directory);
directory
}
#[cfg(not(target_arch = "wasm32"))]
#[tokio::test(flavor = "multi_thread")]
async fn native_stdio_server_runs_opted_in_calls_as_tasks() {
use rmcp::model::{CallToolRequestParams, CallToolResponse, CancelTaskParams};
let held = Arc::new(std::sync::atomic::AtomicU8::new(0));
let cli = native_task_cli(&held);
let tasks = McpServeTasks::Custom(McpTaskSpawner::tokio());
let arguments =
serde_json::Map::from_iter([("message".to_string(), serde_json::json!("hello"))]);
let (client, server_task) = native_task_client(&cli, &tasks, true).await;
let capabilities =
serde_json::to_value(&client.peer_info().expect("server info").capabilities).unwrap();
assert!(
capabilities["extensions"]["io.modelcontextprotocol/tasks"].is_object(),
"{capabilities}"
);
let id = native_task_id(
client
.call_tool_once(
CallToolRequestParams::new("echo").with_arguments(arguments.clone()),
)
.await
.expect("task call"),
);
let settled = native_settled_task(&client, &id).await;
assert_eq!(settled["status"], "completed", "{settled}");
let text: serde_json::Value = serde_json::from_str(
settled["result"]["content"][0]["text"]
.as_str()
.expect("text content"),
)
.unwrap();
assert_eq!(text["echo"]["message"], "hello", "{settled}");
let inline = client
.call_tool_once(CallToolRequestParams::new("inline").with_arguments(arguments.clone()))
.await
.expect("inline call");
assert!(
matches!(inline, CallToolResponse::Complete(_)),
"{inline:?}"
);
let held_id = native_task_id(
client
.call_tool_once(
CallToolRequestParams::new("hold").with_arguments(serde_json::Map::new()),
)
.await
.expect("held task"),
);
for _ in 0..500 {
if held.load(std::sync::atomic::Ordering::SeqCst) == 1 {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
client
.cancel_task(CancelTaskParams::new(held_id.clone()))
.await
.expect("tasks/cancel");
let cancelled = native_settled_task(&client, &held_id).await;
assert_eq!(cancelled["status"], "cancelled", "{cancelled}");
for _ in 0..500 {
if held.load(std::sync::atomic::Ordering::SeqCst) == 2 {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
assert_eq!(held.load(std::sync::atomic::Ordering::SeqCst), 2);
let unknown = client
.get_task(rmcp::model::GetTaskParams::new("missing"))
.await
.expect_err("unknown task");
assert!(unknown.to_string().contains("-32602"), "{unknown}");
drop(client);
server_task.abort();
let (plain, server_task) = native_task_client(&cli, &tasks, false).await;
let capabilities =
serde_json::to_value(&plain.peer_info().expect("server info").capabilities).unwrap();
assert!(
capabilities["extensions"]["io.modelcontextprotocol/tasks"].is_object(),
"{capabilities}"
);
let inline = plain
.call_tool_once(CallToolRequestParams::new("echo").with_arguments(arguments))
.await
.expect("call without opt-in");
assert!(
matches!(inline, CallToolResponse::Complete(_)),
"{inline:?}"
);
drop(plain);
server_task.abort();
let (off, server_task) = native_task_client(&cli, &McpServeTasks::Off, true).await;
let capabilities =
serde_json::to_value(&off.peer_info().expect("server info").capabilities).unwrap();
assert!(
capabilities["extensions"]
.get("io.modelcontextprotocol/tasks")
.is_none(),
"{capabilities}"
);
drop(off);
server_task.abort();
}
#[cfg(not(target_arch = "wasm32"))]
#[tokio::test(flavor = "multi_thread")]
async fn native_stdio_tasks_fail_after_restart_instead_of_running_again() {
use rmcp::model::CallToolRequestParams;
let directory = native_task_directory();
let journaled = || {
McpServeTasks::Custom(
McpTaskSpawner::tokio()
.with_journal(McpTaskDirectoryJournal::open(&directory).unwrap()),
)
};
let held = Arc::new(std::sync::atomic::AtomicU8::new(0));
let cli = native_task_cli(&held);
let (client, server_task) = native_task_client(&cli, &journaled(), true).await;
let done = native_task_id(
client
.call_tool_once(CallToolRequestParams::new("echo").with_arguments(
serde_json::Map::from_iter([(
"message".to_string(),
serde_json::json!("kept"),
)]),
))
.await
.expect("task call"),
);
let completed = native_settled_task(&client, &done).await;
assert_eq!(completed["status"], "completed", "{completed}");
let running = native_task_id(
client
.call_tool_once(
CallToolRequestParams::new("hold").with_arguments(serde_json::Map::new()),
)
.await
.expect("held task"),
);
for _ in 0..500 {
if held.load(std::sync::atomic::Ordering::SeqCst) == 1 {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
assert_eq!(held.load(std::sync::atomic::Ordering::SeqCst), 1);
drop(client);
server_task.abort();
held.store(0, std::sync::atomic::Ordering::SeqCst);
let (client, server_task) = native_task_client(&cli, &journaled(), true).await;
let kept = native_settled_task(&client, &done).await;
assert_eq!(kept["status"], "completed", "{kept}");
assert_eq!(kept["result"], completed["result"], "{kept}");
let lost = native_settled_task(&client, &running).await;
assert_eq!(lost["status"], "failed", "{lost}");
assert_eq!(lost["error"]["data"]["code"], "TASK_INTERRUPTED", "{lost}");
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
assert_eq!(
held.load(std::sync::atomic::Ordering::SeqCst),
0,
"an interrupted task must not run again"
);
drop(client);
server_task.abort();
let _ = std::fs::remove_dir_all(&directory);
}
#[cfg(feature = "http")]
#[test]
fn remote_header_map_converts_valid_pairs() {
let headers = vec![
("x-temper-machine".to_string(), "mac-1".to_string()),
("x-trace".to_string(), "abc123".to_string()),
];
let map = super::remote_header_map(&headers).expect("valid headers convert");
assert_eq!(map.len(), 2);
assert_eq!(
map.get(&http::HeaderName::from_static("x-temper-machine"))
.map(|value| value.to_str().unwrap()),
Some("mac-1"),
);
}
#[cfg(feature = "http")]
#[test]
fn remote_header_map_names_the_offending_header() {
let error = super::remote_header_map(&[("bad name".to_string(), "v".to_string())])
.expect_err("a space is not valid in a header name");
assert!(error.to_string().contains("bad name"), "got: {error}");
let error = super::remote_header_map(&[("x-ok".to_string(), "bad\nvalue".to_string())])
.expect_err("a newline is not valid in a header value");
assert!(error.to_string().contains("x-ok"), "got: {error}");
}
#[cfg(feature = "http")]
#[test]
fn bearer_options_carry_only_a_token() {
let options = super::McpRemoteOptions::bearer("secret-token");
assert_eq!(options.auth_token.as_deref(), Some("secret-token"));
assert!(options.headers.is_empty());
}
#[cfg(all(
any(feature = "http", feature = "agent-plugins-mcp"),
not(target_arch = "wasm32")
))]
#[test]
fn remote_adapters_restore_only_the_exact_output_projection_marker() {
let payload_schema = serde_json::json!({ "type": "array", "items": { "type": "integer" } });
let wrapped_schema = serde_json::json!({
"type": "object", "properties": { "data": payload_schema.clone() },
"required": ["data"], "additionalProperties": false
});
let wrapped_content = serde_json::json!({ "data": [1, 2] });
let cases = [
(None, false),
(
Some(serde_json::json!({
"version": 1, "shape": "value-wrapper", "field": "data",
"schemaRefBase": "#/properties/data"
})),
true,
),
(
Some(serde_json::json!({
"version": 2, "shape": "value-wrapper", "field": "data",
"schemaRefBase": "#/properties/data"
})),
false,
),
(
Some(serde_json::json!({
"version": 1, "shape": "value-wrapper", "field": "other",
"schemaRefBase": "#/properties/data"
})),
false,
),
(
Some(serde_json::json!({
"version": 1, "shape": "value-wrapper", "field": "data",
"schemaRefBase": "#/properties/data", "extra": true
})),
false,
),
];
for (marker, restore) in cases {
let mut tool = serde_json::json!({
"name": "echo", "inputSchema": { "type": "object" },
"outputSchema": wrapped_schema.clone()
});
let mut result = serde_json::json!({
"content": [], "structuredContent": wrapped_content.clone(), "isError": false
});
if let Some(marker) = marker {
let metadata = serde_json::json!({ "io.incurs.outputProjection": marker });
tool["_meta"] = metadata.clone();
result["_meta"] = metadata;
}
let expected_schema = if restore {
&payload_schema
} else {
&wrapped_schema
};
let expected_content = if restore {
serde_json::json!([1, 2])
} else {
wrapped_content.clone()
};
let portable_tool = super::RemoteTool::from_value(&tool).unwrap();
assert_eq!(portable_tool.output_schema.as_ref(), Some(expected_schema));
let native_tool: super::RemoteTool = serde_json::from_value::<rmcp::model::Tool>(tool)
.unwrap()
.into();
assert_eq!(native_tool.output_schema.as_ref(), Some(expected_schema));
assert_eq!(
super::RemoteCallResult::from_value(&result).data(),
expected_content
);
let native_result: super::RemoteCallResult =
serde_json::from_value::<rmcp::model::CallToolResult>(result)
.unwrap()
.into();
assert_eq!(native_result.data(), expected_content);
}
}
#[cfg(all(feature = "http", not(target_arch = "wasm32")))]
#[test]
fn rmcp_failures_keep_a_code_and_only_transport_failures_are_retryable() {
let coded = |error: crate::errors::Error| match error {
crate::errors::Error::Multicast(error) => (error.code, error.retryable, error.message),
other => panic!("expected a coded error, got {other}"),
};
let (code, retryable, message) =
coded(super::rmcp_service_error(rmcp::ServiceError::McpError(
rmcp::model::ErrorData::new(rmcp::model::ErrorCode(-32602), "bad", None),
)));
assert_eq!(code, "MCP_JSONRPC_ERROR");
assert!(!retryable);
assert_eq!(message, "Mcp error: -32602: bad");
let (code, retryable, _) = coded(super::rmcp_service_error(
rmcp::ServiceError::TransportClosed,
));
assert_eq!(code, "MCP_TRANSPORT_ERROR");
assert!(retryable);
let (code, retryable, _) = coded(super::rmcp_service_error(
rmcp::ServiceError::UnexpectedResponse,
));
assert_eq!(code, "MCP_PROTOCOL_ERROR");
assert!(!retryable);
}
}