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: Uuidrich-errors only.Unique error ID for tracing (only with rich-errors feature)
kind: ErrorKindError classification
message: StringHuman-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>rich-errors only.Timestamp when error occurred (only with rich-errors feature)
Implementations§
Source§impl McpError
impl McpError
Sourcepub fn new(kind: ErrorKind, message: impl Into<String>) -> McpError
pub fn new(kind: ErrorKind, message: impl Into<String>) -> McpError
Create a new error with kind and message
Sourcepub const fn id(&self) -> Uuid
Available on crate feature rich-errors only.
pub const fn id(&self) -> Uuid
rich-errors only.Get the error ID (only available with rich-errors feature)
Sourcepub const fn timestamp(&self) -> DateTime<Utc>
Available on crate feature rich-errors only.
pub const fn timestamp(&self) -> DateTime<Utc>
rich-errors only.Get the error timestamp (only available with rich-errors feature)
Sourcepub fn invalid_params(message: impl Into<String>) -> McpError
pub fn invalid_params(message: impl Into<String>) -> McpError
Create a validation/invalid params error
Sourcepub fn safe_internal(message: impl Into<String>) -> McpError
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"));Sourcepub fn safe_tool_execution_failed(
tool_name: impl Into<String>,
reason: impl Into<String>,
) -> McpError
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.
Sourcepub fn sanitized(self) -> McpError
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.
Sourcepub fn parse_error(message: impl Into<String>) -> McpError
pub fn parse_error(message: impl Into<String>) -> McpError
Create a parse error
Sourcepub fn invalid_request(message: impl Into<String>) -> McpError
pub fn invalid_request(message: impl Into<String>) -> McpError
Create an invalid request error
Sourcepub fn method_not_found(method: impl Into<String>) -> McpError
pub fn method_not_found(method: impl Into<String>) -> McpError
Create a method not found error
Sourcepub fn tool_not_found(tool_name: impl Into<String>) -> McpError
pub fn tool_not_found(tool_name: impl Into<String>) -> McpError
Create a tool not found error
Sourcepub fn tool_execution_failed(
tool_name: impl Into<String>,
reason: impl Into<String>,
) -> McpError
pub fn tool_execution_failed( tool_name: impl Into<String>, reason: impl Into<String>, ) -> McpError
Create a tool execution failed error
Sourcepub fn prompt_not_found(prompt_name: impl Into<String>) -> McpError
pub fn prompt_not_found(prompt_name: impl Into<String>) -> McpError
Create a prompt not found error
Sourcepub fn resource_not_found(uri: impl Into<String>) -> McpError
pub fn resource_not_found(uri: impl Into<String>) -> McpError
Create a resource not found error
Sourcepub fn resource_access_denied(
uri: impl Into<String>,
reason: impl Into<String>,
) -> McpError
pub fn resource_access_denied( uri: impl Into<String>, reason: impl Into<String>, ) -> McpError
Create a resource access denied error
Sourcepub fn capability_not_supported(capability: impl Into<String>) -> McpError
pub fn capability_not_supported(capability: impl Into<String>) -> McpError
Create a capability not supported error
Sourcepub fn protocol_version_mismatch(
client_version: impl Into<String>,
server_version: impl Into<String>,
) -> McpError
pub fn protocol_version_mismatch( client_version: impl Into<String>, server_version: impl Into<String>, ) -> McpError
Create a protocol version mismatch error
Sourcepub fn authentication(message: impl Into<String>) -> McpError
pub fn authentication(message: impl Into<String>) -> McpError
Create an authentication error
Sourcepub fn permission_denied(message: impl Into<String>) -> McpError
pub fn permission_denied(message: impl Into<String>) -> McpError
Create a permission denied error
Sourcepub fn rate_limited(message: impl Into<String>) -> McpError
pub fn rate_limited(message: impl Into<String>) -> McpError
Create a rate limited error
Sourcepub fn user_rejected(message: impl Into<String>) -> McpError
pub fn user_rejected(message: impl Into<String>) -> McpError
Create a user rejected error
Sourcepub fn serialization(message: impl Into<String>) -> McpError
pub fn serialization(message: impl Into<String>) -> McpError
Create a serialization error
Create an unavailable error
Sourcepub fn configuration(message: impl Into<String>) -> McpError
pub fn configuration(message: impl Into<String>) -> McpError
Create a configuration error
Sourcepub fn external_service(message: impl Into<String>) -> McpError
pub fn external_service(message: impl Into<String>) -> McpError
Create an external service error
Sourcepub fn server_overloaded() -> McpError
pub fn server_overloaded() -> McpError
Create a server overloaded error
Sourcepub fn from_rpc_code(code: i32, message: impl Into<String>) -> McpError
pub fn from_rpc_code(code: i32, message: impl Into<String>) -> McpError
Create an error from a JSON-RPC error code
Sourcepub fn with_operation(self, operation: impl Into<String>) -> McpError
pub fn with_operation(self, operation: impl Into<String>) -> McpError
Set the operation context
Sourcepub fn with_component(self, component: impl Into<String>) -> McpError
pub fn with_component(self, component: impl Into<String>) -> McpError
Set the component context
Sourcepub fn with_request_id(self, request_id: impl Into<String>) -> McpError
pub fn with_request_id(self, request_id: impl Into<String>) -> McpError
Set the request ID context
Sourcepub fn with_source_location(self, location: impl Into<String>) -> McpError
pub fn with_source_location(self, location: impl Into<String>) -> McpError
Set the source location (typically file:line)
Sourcepub const fn is_retryable(&self) -> bool
pub const fn is_retryable(&self) -> bool
Check if this error is retryable
Sourcepub const fn is_temporary(&self) -> bool
pub const fn is_temporary(&self) -> bool
Check if this error is temporary
Sourcepub const fn jsonrpc_code(&self) -> i32
pub const fn jsonrpc_code(&self) -> i32
Get the JSON-RPC error code for this error
Sourcepub const fn jsonrpc_error_code(&self) -> i32
pub const fn jsonrpc_error_code(&self) -> i32
Get the JSON-RPC error code (canonical name)
Sourcepub const fn http_status(&self) -> u16
pub const fn http_status(&self) -> u16
Get the HTTP status code equivalent
Trait Implementations§
Source§impl<'de> Deserialize<'de> for McpError
impl<'de> Deserialize<'de> for McpError
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<McpError, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<McpError, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Error for McpError
Available on crate feature std only.
impl Error for McpError
std only.1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()