reagent-rs 0.2.1

A Rust library for building AI agents with MCP & custom tools
Documentation
use std::fmt;


/// Errors that can occur while integrating with an MCP server.
///
/// These errors typically arise when discovering available tools,
/// connecting to the server, or converting MCP actions into the
/// agent’s internal tool representation.
#[derive(Debug)]
pub enum McpIntegrationError {
    /// A lower-level error bubbled up from the `rmcp` SDK.
    Sdk(rmcp::Error),
    /// Failure establishing a connection to an MCP server.
    Connection(String),
    /// Failure discovering MCP actions or capabilities.
    Discovery(String),
    /// Failure converting an MCP action into an [`Tool`](crate::Tool).
    ToolConversion(String),
    /// Provided MCP schema was missing or invalid (e.g. not a JSON object).
    InvalidSchema(String),
}

impl fmt::Display for McpIntegrationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            McpIntegrationError::Sdk(e) => write!(f, "MCP SDK error: {e}"),
            McpIntegrationError::Connection(s) => {
                write!(f, "Failed to connect to MCP server: {s}")
            }
            McpIntegrationError::Discovery(s) => {
                write!(f, "Failed to discover MCP actions: {s}")
            }
            McpIntegrationError::ToolConversion(s) => {
                write!(f, "Failed to convert MCP action to agent tool: {s}")
            }
            McpIntegrationError::InvalidSchema(s) => {
                write!(f, "MCP action input schema is missing or not an object: {s}")
            }
        }
    }
}

impl std::error::Error for McpIntegrationError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            McpIntegrationError::Sdk(e) => Some(e),
            McpIntegrationError::Connection(_) => None,
            McpIntegrationError::Discovery(_) => None,
            McpIntegrationError::ToolConversion(_) => None,
            McpIntegrationError::InvalidSchema(_) => None,
        }
    }
}

impl From<rmcp::Error> for McpIntegrationError {
    fn from(err: rmcp::Error) -> Self {
        McpIntegrationError::Sdk(err)
    }
}