Skip to main content

mcp_utils/client/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum McpError {
5    /// Tool not found in the registry
6    #[error("Tool not found: {0}")]
7    ToolNotFound(String),
8    /// Invalid tool name format (should be `server__tool`)
9    #[error("Invalid tool name format: {0}")]
10    InvalidToolNameFormat(String),
11    /// MCP server not found
12    #[error("Server not found: {0}")]
13    ServerNotFound(String),
14    /// Failed to execute tool on MCP server
15    #[error("Failed to execute tool {tool_name} on server {server_name}: {error}")]
16    ToolExecutionFailed { tool_name: String, server_name: String, error: String },
17    /// Tool execution returned an error
18    #[error("Tool execution failed: {0}")]
19    ToolExecutionError(String),
20    /// Tool discovery failed
21    #[error("Tool discovery failed: {0}")]
22    ToolDiscoveryFailed(String),
23    /// Prompt not found in the registry
24    #[error("Prompt not found: {0}")]
25    PromptNotFound(String),
26    /// Prompt listing failed
27    #[error("Prompt listing failed: {0}")]
28    PromptListFailed(String),
29    /// Prompt retrieval failed
30    #[error("Prompt retrieval failed: {0}")]
31    PromptGetFailed(String),
32    /// Failed to spawn a stdio server process
33    #[error("Failed to spawn '{command}': {reason}")]
34    SpawnFailed { command: String, reason: String },
35    /// Server connection failed
36    #[error("Connection failed: {0}")]
37    ConnectionFailed(String),
38    /// Server startup failed
39    #[error("Server startup failed: {0}")]
40    ServerStartupFailed(String),
41    /// Transport error
42    #[error("Transport error: {0}")]
43    TransportError(String),
44    /// JSON serialization/deserialization error
45    #[error("JSON error: {0}")]
46    JsonError(String),
47    /// Generic error for other cases
48    #[error("{0}")]
49    Other(String),
50}
51
52impl From<serde_json::Error> for McpError {
53    fn from(error: serde_json::Error) -> Self {
54        McpError::JsonError(error.to_string())
55    }
56}
57
58impl From<std::io::Error> for McpError {
59    fn from(error: std::io::Error) -> Self {
60        McpError::TransportError(error.to_string())
61    }
62}
63
64impl From<rmcp::service::ClientInitializeError> for McpError {
65    fn from(error: rmcp::service::ClientInitializeError) -> Self {
66        McpError::ConnectionFailed(error.to_string())
67    }
68}
69
70pub type Result<T> = std::result::Result<T, McpError>;