use std::collections::HashMap;
use std::fmt;
use futures_util::future::{self, BoxFuture, FutureExt};
use rmcp::model::{CallToolRequestParams, CallToolResult};
use rmcp::{Peer, RoleClient, ServiceError};
use serde_json::Value;
use crate::host::{self, Call, Host, Tool};
use crate::schema;
#[derive(Clone, Debug)]
pub struct Server {
peer: Peer<RoleClient>,
tools: Vec<Tool>,
mcp_ids: HashMap<String, String>,
}
impl Server {
pub async fn new(peer: Peer<RoleClient>) -> Result<Self, Error> {
let mut tools = Vec::new();
let mut mcp_ids = HashMap::new();
for mcp_tool in peer.list_all_tools().await.map_err(Error::ListTools)? {
let mcp_id = mcp_tool.name.into_owned();
let tool_id = to_tool_id(&mcp_id);
if !schema::is_tool_id(&tool_id) {
return Err(Error::UncallableId { tool_id });
}
if let Some(other) = mcp_ids.insert(tool_id.clone(), mcp_id.clone()) {
let mcp_ids = [other, mcp_id];
return Err(Error::SharedId { tool_id, mcp_ids });
}
let description = mcp_tool.description.unwrap_or_default();
let input_schema = Value::Object((*mcp_tool.input_schema).clone());
tools.push(Tool::new(tool_id, description, input_schema));
}
Ok(Self {
peer,
tools,
mcp_ids,
})
}
}
impl Host for Server {
fn tools(&self) -> Vec<Tool> {
self.tools.clone()
}
fn call(&self, call: &Call) -> BoxFuture<'static, Result<Value, host::Error>> {
let Some(mcp_id) = self.mcp_ids.get(&call.tool_id).cloned() else {
let message = format!("no MCP tool has the id {}", call.tool_id);
return future::ready(Err(host::Error::Failed(message))).boxed();
};
let peer = self.peer.clone();
let params = CallToolRequestParams::new(mcp_id).with_arguments(call.args.clone());
async move {
let result = peer.call_tool(params).await;
to_answer(result.map_err(|error| host::Error::Failed(error.to_string()))?)
}
.boxed()
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
ListTools(ServiceError),
SharedId {
tool_id: String,
mcp_ids: [String; 2],
},
UncallableId {
tool_id: String,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ListTools(_) => f.write_str("listing the MCP server's tools failed"),
Self::SharedId {
tool_id,
mcp_ids: [first, second],
} => write!(
f,
"MCP tools `{first}` and `{second}` both map to the id `{tool_id}`"
),
Self::UncallableId { tool_id } => {
write!(
f,
"MCP tool `{tool_id}` is not a Python name a script can call"
)
}
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::ListTools(error) => Some(error),
Self::SharedId { .. } | Self::UncallableId { .. } => None,
}
}
}
fn to_tool_id(mcp_id: &str) -> String {
let mut tool_id: String = mcp_id
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '_' })
.collect();
if tool_id.starts_with(|c: char| c.is_ascii_digit()) {
tool_id.replace_range(..1, "_");
}
tool_id
}
fn to_answer(result: CallToolResult) -> Result<Value, host::Error> {
let text: Vec<&str> = result
.content
.iter()
.filter_map(|content| content.as_text())
.map(|content| content.text.as_str())
.collect();
let text = text.join("\n");
if result.is_error == Some(true) {
return Err(host::Error::Failed(text));
}
if let Some(value) = result.structured_content {
return Ok(value);
}
Ok(serde_json::from_str(&text).unwrap_or(Value::String(text)))
}