Skip to main content

rustapi_mcp/
error.rs

1//! MCP-specific error types.
2
3use thiserror::Error;
4
5/// Result alias used throughout the `rustapi-mcp` crate.
6pub type Result<T> = std::result::Result<T, McpError>;
7
8/// Top-level error type for MCP operations.
9#[derive(Debug, Error)]
10pub enum McpError {
11    /// The MCP client is not authorized (missing or bad token).
12    #[error("unauthorized: {0}")]
13    Unauthorized(String),
14
15    /// The requested capability (e.g. tools) is not enabled in config.
16    #[error("capability not enabled: {0}")]
17    CapabilityNotEnabled(String),
18
19    /// A tool with the given name was not found / not exposed.
20    #[error("tool not found: {0}")]
21    ToolNotFound(String),
22
23    /// Invalid request from the MCP client (bad parameters, schema mismatch, etc.).
24    #[error("invalid request: {0}")]
25    InvalidRequest(String),
26
27    /// An error occurred while executing the underlying RustAPI handler.
28    /// The inner value is the stringified error (respecting redaction rules).
29    #[error("tool execution failed: {0}")]
30    ToolExecution(String),
31
32    /// Transport-level or protocol-level error.
33    #[error("transport error: {0}")]
34    Transport(String),
35
36    /// Internal / unexpected error.
37    #[error("internal mcp error: {0}")]
38    Internal(String),
39}
40
41impl McpError {
42    /// Create an unauthorized error.
43    pub fn unauthorized(msg: impl Into<String>) -> Self {
44        McpError::Unauthorized(msg.into())
45    }
46
47    /// Create an invalid request error.
48    pub fn invalid_request(msg: impl Into<String>) -> Self {
49        McpError::InvalidRequest(msg.into())
50    }
51}