rust_mcp_sdk/
error.rs

1#[cfg(feature = "auth")]
2use crate::auth::AuthenticationError;
3use crate::schema::{ParseProtocolVersionError, RpcError};
4use rust_mcp_transport::error::TransportError;
5use thiserror::Error;
6use tokio::task::JoinError;
7
8#[cfg(feature = "hyper-server")]
9use crate::hyper_servers::error::TransportServerError;
10
11pub type SdkResult<T> = core::result::Result<T, McpSdkError>;
12
13#[derive(Debug, Error)]
14pub enum McpSdkError {
15    #[error("Transport error: {0}")]
16    Transport(#[from] TransportError),
17
18    #[error("I/O error: {0}")]
19    Io(#[from] std::io::Error),
20
21    #[error("{0}")]
22    RpcError(#[from] RpcError),
23
24    #[error("{0}")]
25    Join(#[from] JoinError),
26
27    #[cfg(feature = "hyper-server")]
28    #[error("{0}")]
29    HyperServer(#[from] TransportServerError),
30
31    #[cfg(feature = "auth")]
32    #[error("{0}")]
33    AuthenticationError(#[from] AuthenticationError),
34
35    #[error("{0}")]
36    SdkError(#[from] crate::schema::schema_utils::SdkError),
37
38    #[error("Protocol error: {kind}")]
39    Protocol { kind: ProtocolErrorKind },
40
41    #[error("Server error: {description}")]
42    Internal { description: String },
43}
44
45// Sub-enum for protocol-related errors
46#[derive(Debug, Error)]
47pub enum ProtocolErrorKind {
48    #[error("Incompatible protocol version: requested {requested}, current {current}")]
49    IncompatibleVersion { requested: String, current: String },
50    #[error("Failed to parse protocol version: {0}")]
51    ParseError(#[from] ParseProtocolVersionError),
52}
53
54impl McpSdkError {
55    /// Returns the RPC error message if the error is of type `McpSdkError::RpcError`.
56    pub fn rpc_error_message(&self) -> Option<&String> {
57        if let McpSdkError::RpcError(rpc_error) = self {
58            return Some(&rpc_error.message);
59        }
60        None
61    }
62}