pub struct McpError {
pub code: McpErrorCode,
pub message: String,
pub data: Option<Value>,
}Expand description
An MCP error response.
This maps directly to the JSON-RPC error object and can be serialized for transport.
Fields§
§code: McpErrorCodeThe error code.
message: StringA short description of the error.
data: Option<Value>Additional error data (optional).
Implementations§
Source§impl McpError
impl McpError
Sourcepub fn new(code: McpErrorCode, message: impl Into<String>) -> McpError
pub fn new(code: McpErrorCode, message: impl Into<String>) -> McpError
Creates a new MCP error.
Sourcepub fn with_data(
code: McpErrorCode,
message: impl Into<String>,
data: Value,
) -> McpError
pub fn with_data( code: McpErrorCode, message: impl Into<String>, data: Value, ) -> McpError
Creates a new MCP error with additional data.
Sourcepub fn parse_error(message: impl Into<String>) -> McpError
pub fn parse_error(message: impl Into<String>) -> McpError
Creates a parse error.
Sourcepub fn invalid_request(message: impl Into<String>) -> McpError
pub fn invalid_request(message: impl Into<String>) -> McpError
Creates an invalid request error.
Sourcepub fn method_not_found(method: &str) -> McpError
pub fn method_not_found(method: &str) -> McpError
Creates a method not found error.
Sourcepub fn invalid_params(message: impl Into<String>) -> McpError
pub fn invalid_params(message: impl Into<String>) -> McpError
Creates an invalid params error.
Sourcepub fn internal_error(message: impl Into<String>) -> McpError
pub fn internal_error(message: impl Into<String>) -> McpError
Creates an internal error.
Sourcepub fn tool_error(message: impl Into<String>) -> McpError
pub fn tool_error(message: impl Into<String>) -> McpError
Creates a tool execution error.
Sourcepub fn resource_not_found(uri: &str) -> McpError
pub fn resource_not_found(uri: &str) -> McpError
Creates a resource not found error.
Sourcepub fn request_cancelled() -> McpError
pub fn request_cancelled() -> McpError
Creates a request cancelled error.
Sourcepub fn masked(&self, mask_enabled: bool) -> McpError
pub fn masked(&self, mask_enabled: bool) -> McpError
Returns a masked version of this error for client responses.
When masking is enabled, internal error details are hidden to prevent leaking sensitive information (file paths, stack traces, internal state).
§What gets masked
InternalError,ToolExecutionError,Customcodes: message replaced with “Internal server error” and data removed
§What’s preserved
- Error code (for programmatic handling)
- Client errors (
ParseError,InvalidRequest,MethodNotFound,InvalidParams,ResourceNotFound,ResourceForbidden,PromptNotFound,RequestCancelled): preserved as-is since they don’t contain internal details
§Example
use fastmcp_core::{McpError, McpErrorCode};
let internal = McpError::internal_error("Connection failed at /etc/secrets/db.conf");
let masked = internal.masked(true);
assert_eq!(masked.message, "Internal server error");
assert!(masked.data.is_none());
// Client errors are preserved
let client = McpError::invalid_params("Missing field 'name'");
let masked_client = client.masked(true);
assert!(masked_client.message.contains("name"));Sourcepub fn is_internal(&self) -> bool
pub fn is_internal(&self) -> bool
Returns whether this error contains internal details that should be masked.