use crate::prompt::{ToolPrefix, not_found};
use rmcp::model::{
CallToolRequestParams, CallToolResponse, ContentBlock, GetPromptRequestParams,
GetPromptResponse, Implementation, ListPromptsResult, ListToolsResult, PaginatedRequestParams,
Prompt, PromptsCapability, ServerCapabilities, ServerConfig, Tool,
};
use rmcp::service::{RequestContext, RoleServer};
use rmcp::{ErrorData as McpError, ServerHandler};
use std::future::Future;
use std::pin::Pin;
pub const COMBINED: &str = "mcp";
type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
pub trait ToolHandler: Send + Sync + 'static {
fn info(&self) -> ServerConfig;
fn tool(&self, name: &str) -> Option<Tool>;
fn list(
&self,
context: RequestContext<RoleServer>,
) -> BoxFuture<'_, Result<Vec<Tool>, McpError>>;
fn call(
&self,
request: CallToolRequestParams,
context: RequestContext<RoleServer>,
) -> BoxFuture<'_, Result<CallToolResponse, McpError>>;
fn prompts(
&self,
context: RequestContext<RoleServer>,
) -> BoxFuture<'_, Result<Vec<Prompt>, McpError>>;
fn prompt(
&self,
request: GetPromptRequestParams,
context: RequestContext<RoleServer>,
) -> BoxFuture<'_, Result<GetPromptResponse, McpError>>;
}
impl<S: ServerHandler> ToolHandler for S {
fn info(&self) -> ServerConfig {
self.get_info()
}
fn tool(&self, name: &str) -> Option<Tool> {
self.get_tool(name)
}
fn list(
&self,
context: RequestContext<RoleServer>,
) -> BoxFuture<'_, Result<Vec<Tool>, McpError>> {
Box::pin(async move { Ok(self.list_tools(None, context).await?.tools) })
}
fn call(
&self,
request: CallToolRequestParams,
context: RequestContext<RoleServer>,
) -> BoxFuture<'_, Result<CallToolResponse, McpError>> {
Box::pin(self.call_tool(request, context))
}
fn prompts(
&self,
context: RequestContext<RoleServer>,
) -> BoxFuture<'_, Result<Vec<Prompt>, McpError>> {
Box::pin(async move { Ok(self.list_prompts(None, context).await?.prompts) })
}
fn prompt(
&self,
request: GetPromptRequestParams,
context: RequestContext<RoleServer>,
) -> BoxFuture<'_, Result<GetPromptResponse, McpError>> {
Box::pin(self.get_prompt(request, context))
}
}
pub struct DynHandler(Box<dyn ToolHandler>);
impl DynHandler {
pub fn new(handler: impl ToolHandler) -> Self {
Self(Box::new(handler))
}
}
impl ServerHandler for DynHandler {
fn get_info(&self) -> ServerConfig {
self.0.info()
}
fn get_tool(&self, name: &str) -> Option<Tool> {
self.0.tool(name)
}
async fn list_tools(
&self,
_request: Option<PaginatedRequestParams>,
context: RequestContext<RoleServer>,
) -> Result<ListToolsResult, McpError> {
Ok(ListToolsResult::with_all_items(self.0.list(context).await?))
}
async fn call_tool(
&self,
request: CallToolRequestParams,
context: RequestContext<RoleServer>,
) -> Result<CallToolResponse, McpError> {
self.0.call(request, context).await
}
async fn list_prompts(
&self,
_request: Option<PaginatedRequestParams>,
context: RequestContext<RoleServer>,
) -> Result<ListPromptsResult, McpError> {
Ok(ListPromptsResult::with_all_items(
self.0.prompts(context).await?,
))
}
async fn get_prompt(
&self,
request: GetPromptRequestParams,
context: RequestContext<RoleServer>,
) -> Result<GetPromptResponse, McpError> {
self.0.prompt(request, context).await
}
}
pub fn prefixed_name(service: &str, tool: &str) -> String {
format!("{service}_{tool}")
}
fn prefixed(service: &str, mut tool: Tool) -> Tool {
tool.name = prefixed_name(service, &tool.name).into();
tool
}
fn prefix_tool_names(service: &str, tools: &[String], text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut word = String::new();
let flush = |word: &mut String, out: &mut String| {
if tools.iter().any(|t| t == word) {
out.push_str(&prefixed_name(service, word));
} else {
out.push_str(word);
}
word.clear();
};
for c in text.chars() {
if c.is_ascii_alphanumeric() || c == '_' {
word.push(c);
} else {
flush(&mut word, &mut out);
out.push(c);
}
}
flush(&mut word, &mut out);
out
}
fn prefixed_prompt(service: &str, mut prompt: Prompt) -> Prompt {
prompt.name = prefixed_name(service, &prompt.name);
prompt
}
fn with_full_name(error: McpError, own: &str, full: &str) -> McpError {
if error == not_found(own) {
return not_found(full);
}
error
}
fn split<'a>(services: &[&str], name: &'a str) -> Option<(usize, &'a str)> {
let (service, tool) = name.split_once('_')?;
let index = services.iter().position(|s| *s == service)?;
(!tool.is_empty()).then_some((index, tool))
}
fn instructions(services: &[(&str, Option<String>)]) -> String {
let prefixes: Vec<String> = services.iter().map(|(s, _)| format!("{s}_")).collect();
let mut text = format!(
"This server combines several services. Each tool and prompt name starts with its service: {}. \
The notes below, tool descriptions and error messages name tools without that prefix.",
prefixes.join(", ")
);
for (service, own) in services {
if let Some(own) = own.as_deref().filter(|t| !t.trim().is_empty()) {
text.push_str(&format!("\n\n{service}:\n{}", own.trim()));
}
}
text
}
pub struct Combined {
info: ServerConfig,
names: Vec<&'static str>,
handlers: Vec<DynHandler>,
}
impl Combined {
pub fn new(server_info: Implementation, services: Vec<(&'static str, DynHandler)>) -> Self {
let own: Vec<(&str, Option<String>)> = services
.iter()
.map(|(name, h)| (*name, h.get_info().instructions))
.collect();
let mut capabilities = ServerCapabilities::builder().enable_tools().build();
if services
.iter()
.any(|(_, h)| h.get_info().capabilities.prompts.is_some())
{
capabilities.prompts = Some(PromptsCapability::default());
}
let info = ServerConfig::new(capabilities)
.with_server_info(server_info)
.with_instructions(instructions(&own));
let (names, handlers) = services.into_iter().unzip();
Self {
info,
names,
handlers,
}
}
}
impl ServerHandler for Combined {
fn get_info(&self) -> ServerConfig {
self.info.clone()
}
fn get_tool(&self, name: &str) -> Option<Tool> {
let (index, tool) = split(&self.names, name)?;
self.handlers[index]
.get_tool(tool)
.map(|t| prefixed(self.names[index], t))
}
async fn list_tools(
&self,
_request: Option<PaginatedRequestParams>,
context: RequestContext<RoleServer>,
) -> Result<ListToolsResult, McpError> {
let mut tools = Vec::new();
for (service, handler) in self.names.iter().zip(&self.handlers) {
let listed = handler.list_tools(None, context.clone()).await?.tools;
tools.extend(listed.into_iter().map(|t| prefixed(service, t)));
}
Ok(ListToolsResult::with_all_items(tools))
}
async fn call_tool(
&self,
mut request: CallToolRequestParams,
context: RequestContext<RoleServer>,
) -> Result<CallToolResponse, McpError> {
let Some((index, tool)) = split(&self.names, &request.name) else {
return Err(McpError::invalid_params("tool not found", None));
};
request.name = tool.to_string().into();
self.handlers[index].call_tool(request, context).await
}
async fn list_prompts(
&self,
_request: Option<PaginatedRequestParams>,
context: RequestContext<RoleServer>,
) -> Result<ListPromptsResult, McpError> {
let mut prompts = Vec::new();
for (service, handler) in self.names.iter().zip(&self.handlers) {
let listed = handler.list_prompts(None, context.clone()).await?.prompts;
prompts.extend(listed.into_iter().map(|p| prefixed_prompt(service, p)));
}
Ok(ListPromptsResult::with_all_items(prompts))
}
async fn get_prompt(
&self,
mut request: GetPromptRequestParams,
mut context: RequestContext<RoleServer>,
) -> Result<GetPromptResponse, McpError> {
let Some((index, prompt)) = split(&self.names, &request.name) else {
return Err(not_found(&request.name));
};
let prompt = prompt.to_string();
let full = std::mem::replace(&mut request.name, prompt.clone());
let service = self.names[index];
let handler = &self.handlers[index];
context
.extensions
.insert(ToolPrefix(prefixed_name(service, "")));
let response = handler
.get_prompt(request, context.clone())
.await
.map_err(|e| with_full_name(e, &prompt, &full))?;
let GetPromptResponse::Complete(mut result) = response else {
return Ok(response);
};
let tools: Vec<String> = handler
.list_tools(None, context)
.await?
.tools
.into_iter()
.map(|t| t.name.to_string())
.collect();
for message in &mut result.messages {
if let ContentBlock::Text(t) = &mut message.content {
t.text = prefix_tool_names(service, &tools, &t.text);
}
}
Ok(GetPromptResponse::Complete(result))
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
#[test]
fn names_are_prefixed_with_their_service() {
assert_eq!(
prefixed_name("companies", "search_companies"),
"companies_search_companies"
);
let tool = Tool::new("get_company", "d", Arc::new(serde_json::Map::new()));
assert_eq!(prefixed("companies", tool).name, "companies_get_company");
}
#[test]
fn a_prefixed_name_routes_to_its_service_and_original_tool() {
let services = ["companies", "streaming"];
assert_eq!(
split(&services, "streaming_read_stream"),
Some((1, "read_stream"))
);
assert_eq!(
split(&services, "companies_get_company"),
Some((0, "get_company"))
);
assert_eq!(split(&services, "get_company"), None);
assert_eq!(split(&services, "filing_submit"), None);
assert_eq!(split(&services, "companies_"), None);
assert_eq!(split(&services, "companies"), None);
}
#[test]
fn instructions_name_the_prefixes_and_keep_each_services_own() {
let text = instructions(&[
("companies", Some("Find companies.\n".into())),
("streaming", None),
("filing", Some("File changes.".into())),
]);
assert_eq!(
text,
"This server combines several services. Each tool and prompt name starts with its service: \
companies_, streaming_, filing_. The notes below, tool descriptions and error messages name \
tools without that prefix.\n\ncompanies:\nFind companies.\n\nfiling:\nFile changes."
);
}
#[test]
fn tool_names_in_prompt_text_get_their_prefix() {
let tools = vec!["get_company".to_string(), "list_officers".to_string()];
assert_eq!(
prefix_tool_names(
"companies",
&tools,
"Call get_company, then list_officers (not get_company_registers, my_get_company \
or companies_get_company)."
),
"Call companies_get_company, then companies_list_officers (not get_company_registers, \
my_get_company or companies_get_company)."
);
}
#[test]
fn prompt_names_are_prefixed_with_their_service() {
let prompt = Prompt::new("plan_week", Some("d"), None);
assert_eq!(
prefixed_prompt("calendar", prompt).name,
"calendar_plan_week"
);
let services = ["mail", "calendar", "contacts"];
assert_eq!(
split(&services, "contacts_clean_up_duplicates"),
Some((2, "clean_up_duplicates"))
);
}
#[test]
fn an_unknown_prompt_is_reported_with_the_name_the_client_sent() {
let e = with_full_name(not_found("nope"), "nope", "contacts_nope");
assert_eq!(e.code, rmcp::model::ErrorCode::INVALID_PARAMS);
assert_eq!(e.message, "prompt 'contacts_nope' not found");
let other = McpError::invalid_params("limit must be a string", None);
assert_eq!(
with_full_name(other.clone(), "nope", "contacts_nope"),
other
);
}
struct Bare(bool);
impl ServerHandler for Bare {
fn get_info(&self) -> ServerConfig {
let mut capabilities = ServerCapabilities::builder().enable_tools().build();
if self.0 {
capabilities.prompts = Some(PromptsCapability::default());
}
ServerConfig::new(capabilities)
}
}
fn combined_info(with_prompts: &[bool]) -> ServerConfig {
let services = with_prompts
.iter()
.zip(["a", "b"])
.map(|(p, name)| (name, DynHandler::new(Bare(*p))))
.collect();
Combined::new(Implementation::new("t", "1"), services).get_info()
}
#[test]
fn prompts_are_offered_when_any_service_has_them() {
let info = combined_info(&[false, true]);
assert!(info.capabilities.prompts.is_some());
assert!(info.capabilities.tools.is_some());
let info = combined_info(&[false, false]);
assert!(info.capabilities.prompts.is_none());
assert!(info.capabilities.tools.is_some());
}
}