use std::sync::PoisonError;
use rho_protocol::{
ErrorCode, RpcError, SessionId, SessionMcpCatalog, SessionMcpCatalogParams,
SessionMcpCompleteParams, SessionMcpCompleteResponse, SessionMcpInventory, SessionMcpParams,
SessionMcpResourceContent, SessionMcpResourceParams, SessionMcpResourceResponse,
MAX_LINE_BYTES,
};
use super::Sessions;
use crate::{
app::session_mcp::SessionMcp,
tools::mcp::{McpCatalogError, McpResourceContent},
};
const RESPONSE_ENVELOPE_BYTES: usize = 1024;
impl Sessions {
pub(in crate::app::daemon_host) async fn mcp(
&self,
params: SessionMcpParams,
) -> Result<SessionMcpInventory, RpcError> {
Ok(self.loaded_mcp(¶ms.session).await?.snapshot())
}
pub(in crate::app::daemon_host) async fn mcp_catalog(
&self,
params: SessionMcpCatalogParams,
) -> Result<SessionMcpCatalog, RpcError> {
Ok(self.loaded_mcp(¶ms.session).await?.offered())
}
pub(in crate::app::daemon_host) async fn mcp_resource(
&self,
params: SessionMcpResourceParams,
) -> Result<SessionMcpResourceResponse, RpcError> {
let mcp = self.loaded_mcp(¶ms.session).await?;
let contents = mcp
.servers()
.read_resource(¶ms.server, ¶ms.uri)
.await
.map_err(|error| {
let code = match error {
McpCatalogError::UnknownServer(_) => ErrorCode::InvalidParams,
McpCatalogError::Server(_) => ErrorCode::InternalError,
};
RpcError::new(code, error.to_string())
})?;
let max_text_bytes = params
.max_text_bytes
.map(|max| usize::try_from(max).unwrap_or(usize::MAX));
let response = SessionMcpResourceResponse {
contents: contents
.into_iter()
.map(|content| match (content, max_text_bytes) {
(
McpResourceContent::Text {
uri,
mime_type,
text,
},
Some(max),
) => McpResourceContent::Text {
uri,
mime_type,
text: cut_text(text, max),
},
(content, _) => content,
})
.map(content_to_wire)
.collect(),
};
let limit = MAX_LINE_BYTES - RESPONSE_ENVELOPE_BYTES;
let size = serde_json::to_vec(&response)
.map_err(|error| RpcError::new(ErrorCode::InternalError, error.to_string()))?
.len();
if size > limit {
return Err(RpcError::new(
ErrorCode::InternalError,
format!(
"resource is {size} bytes as a response, over the daemon protocol's \
{limit}-byte response limit"
),
));
}
Ok(response)
}
pub(in crate::app::daemon_host) async fn mcp_complete(
&self,
params: SessionMcpCompleteParams,
) -> Result<SessionMcpCompleteResponse, RpcError> {
let mcp = self.loaded_mcp(¶ms.session).await?;
Ok(SessionMcpCompleteResponse {
values: mcp
.servers()
.complete_prompt_argument(
¶ms.server,
¶ms.prompt,
¶ms.argument,
¶ms.value,
)
.await,
})
}
async fn loaded_mcp(&self, session: &SessionId) -> Result<SessionMcp, RpcError> {
self.refuse_when_stopping()?;
let not_loaded = || {
RpcError::new(
ErrorCode::SessionNotLoaded,
"the daemon has not loaded the session; attach to it first",
)
};
let session = self.loaded_session(session).await?.ok_or_else(not_loaded)?;
let turn = session.enter().ok_or_else(not_loaded)?;
let mcp = session
.mcp
.lock()
.unwrap_or_else(PoisonError::into_inner)
.clone();
drop(turn);
Ok(mcp)
}
}
const UTF8_CHAR_BYTES: usize = 4;
fn cut_text(mut text: String, max: usize) -> String {
let keep = max.saturating_add(UTF8_CHAR_BYTES);
if text.len() > keep {
text.truncate(rho_sdk::floor_char_boundary(&text, keep));
}
text
}
fn content_to_wire(content: McpResourceContent) -> SessionMcpResourceContent {
match content {
McpResourceContent::Text {
uri,
mime_type,
text,
} => SessionMcpResourceContent::Text {
uri,
mime_type,
text,
},
McpResourceContent::Blob {
uri,
mime_type,
blob,
} => SessionMcpResourceContent::Blob {
uri,
mime_type,
blob,
},
McpResourceContent::Unsupported => SessionMcpResourceContent::Unknown,
}
}
#[cfg(test)]
#[path = "mcp_tests.rs"]
mod tests;