Skip to main content

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