Skip to main content

McpError

Struct McpError 

Source
pub struct McpError {
    pub id: Uuid,
    pub kind: ErrorKind,
    pub message: String,
    pub source_location: Option<String>,
    pub context: Option<Box<ErrorContext>>,
    pub timestamp: DateTime<Utc>,
}
Expand description

Unified MCP error type

This is the single error type used across all TurboMCP crates in v3. It is no_std compatible and maps to JSON-RPC error codes per MCP spec.

With rich-errors feature enabled, includes UUID tracking and timestamps.

The context field is boxed to keep error size small for efficient Result<T, McpError> usage.

Fields§

§id: Uuid
Available on crate feature rich-errors only.

Unique error ID for tracing (only with rich-errors feature)

§kind: ErrorKind

Error classification

§message: String

Human-readable error message

§source_location: Option<String>

Source location (file:line for debugging) Note: Never serialized to clients to prevent information leakage

§context: Option<Box<ErrorContext>>

Additional context (boxed to keep McpError small)

§timestamp: DateTime<Utc>
Available on crate feature rich-errors only.

Timestamp when error occurred (only with rich-errors feature)

Implementations§

Source§

impl McpError

Source

pub fn new(kind: ErrorKind, message: impl Into<String>) -> McpError

Create a new error with kind and message

Source

pub const fn id(&self) -> Uuid

Available on crate feature rich-errors only.

Get the error ID (only available with rich-errors feature)

Source

pub const fn timestamp(&self) -> DateTime<Utc>

Available on crate feature rich-errors only.

Get the error timestamp (only available with rich-errors feature)

Source

pub fn invalid_params(message: impl Into<String>) -> McpError

Create a validation/invalid params error

Source

pub fn internal(message: impl Into<String>) -> McpError

Create an internal error

Source

pub fn safe_internal(message: impl Into<String>) -> McpError

Create a safe internal error with sanitized message.

Use this for errors that may contain sensitive information (file paths, IP addresses, connection strings, etc.). The message is automatically sanitized to prevent information leakage per OWASP guidelines.

§Example
use turbomcp_core::error::McpError;

let err = McpError::safe_internal("Failed: postgres://admin:secret@192.168.1.1/db");
assert!(!err.message.contains("secret"));
assert!(!err.message.contains("192.168.1.1"));
Source

pub fn safe_tool_execution_failed( tool_name: impl Into<String>, reason: impl Into<String>, ) -> McpError

Create a safe tool execution error with sanitized message.

Like safe_internal, but specifically for tool execution failures.

Source

pub fn sanitized(self) -> McpError

Sanitize this error’s message in-place.

Call this before returning errors to clients in production to ensure no sensitive information is leaked.

Source

pub fn parse_error(message: impl Into<String>) -> McpError

Create a parse error

Source

pub fn invalid_request(message: impl Into<String>) -> McpError

Create an invalid request error

Source

pub fn method_not_found(method: impl Into<String>) -> McpError

Create a method not found error

Source

pub fn tool_not_found(tool_name: impl Into<String>) -> McpError

Create a tool not found error

Source

pub fn tool_execution_failed( tool_name: impl Into<String>, reason: impl Into<String>, ) -> McpError

Create a tool execution failed error

Source

pub fn prompt_not_found(prompt_name: impl Into<String>) -> McpError

Create a prompt not found error

Source

pub fn resource_not_found(uri: impl Into<String>) -> McpError

Create a resource not found error

Source

pub fn resource_access_denied( uri: impl Into<String>, reason: impl Into<String>, ) -> McpError

Create a resource access denied error

Source

pub fn capability_not_supported(capability: impl Into<String>) -> McpError

Create a capability not supported error

Source

pub fn protocol_version_mismatch( client_version: impl Into<String>, server_version: impl Into<String>, ) -> McpError

Create a protocol version mismatch error

Source

pub fn timeout(message: impl Into<String>) -> McpError

Create a timeout error

Source

pub fn transport(message: impl Into<String>) -> McpError

Create a transport error

Source

pub fn authentication(message: impl Into<String>) -> McpError

Create an authentication error

Source

pub fn permission_denied(message: impl Into<String>) -> McpError

Create a permission denied error

Source

pub fn rate_limited(message: impl Into<String>) -> McpError

Create a rate limited error

Source

pub fn cancelled(message: impl Into<String>) -> McpError

Create a cancelled error

Source

pub fn user_rejected(message: impl Into<String>) -> McpError

Create a user rejected error

Source

pub fn serialization(message: impl Into<String>) -> McpError

Create a serialization error

Source

pub fn security(message: impl Into<String>) -> McpError

Create a security error

Source

pub fn unavailable(message: impl Into<String>) -> McpError

Create an unavailable error

Source

pub fn configuration(message: impl Into<String>) -> McpError

Create a configuration error

Source

pub fn external_service(message: impl Into<String>) -> McpError

Create an external service error

Source

pub fn server_overloaded() -> McpError

Create a server overloaded error

Source

pub fn from_rpc_code(code: i32, message: impl Into<String>) -> McpError

Create an error from a JSON-RPC error code

Source

pub fn with_operation(self, operation: impl Into<String>) -> McpError

Set the operation context

Source

pub fn with_component(self, component: impl Into<String>) -> McpError

Set the component context

Source

pub fn with_request_id(self, request_id: impl Into<String>) -> McpError

Set the request ID context

Source

pub fn with_source_location(self, location: impl Into<String>) -> McpError

Set the source location (typically file:line)

Source

pub const fn is_retryable(&self) -> bool

Check if this error is retryable

Source

pub const fn is_temporary(&self) -> bool

Check if this error is temporary

Source

pub const fn jsonrpc_code(&self) -> i32

Get the JSON-RPC error code for this error

Source

pub const fn jsonrpc_error_code(&self) -> i32

Get the JSON-RPC error code (canonical name)

Source

pub const fn http_status(&self) -> u16

Get the HTTP status code equivalent

Trait Implementations§

Source§

impl Clone for McpError

Source§

fn clone(&self) -> McpError

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for McpError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for McpError

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<McpError, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for McpError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Error for McpError

Available on crate feature std only.
1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<Box<McpError>> for McpError

Source§

fn from(boxed: Box<McpError>) -> McpError

Converts to this type from the input type.
Source§

impl From<Error> for McpError

Source§

fn from(err: Error) -> McpError

Converts to this type from the input type.
Source§

impl From<Error> for McpError

Available on crate feature std only.
Source§

fn from(err: Error) -> McpError

Converts to this type from the input type.
Source§

impl From<McpError> for ToolError

Source§

fn from(e: McpError) -> ToolError

Converts to this type from the input type.
Source§

impl From<RegistryError> for McpError

Source§

fn from(err: RegistryError) -> Self

Converts to this type from the input type.
Source§

impl From<SharedError> for McpError

Source§

fn from(err: SharedError) -> Self

Converts to this type from the input type.
Source§

impl Serialize for McpError

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<E> IntoToolError for E
where E: Display,

Source§

fn tool_err(self, context: impl Display) -> ToolError

Convert to a ToolError with additional context
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T> MaybeSync for T
where T: Sync + ?Sized,