Skip to main content

aimdb_mcp/
error.rs

1//! Error types for the MCP server
2
3use thiserror::Error;
4
5/// Result type for MCP operations
6pub type McpResult<T> = Result<T, McpError>;
7
8/// Errors that can occur in the MCP server
9#[derive(Debug, Error)]
10pub enum McpError {
11    /// IO error (stdin/stdout)
12    #[error("IO error: {0}")]
13    Io(#[from] std::io::Error),
14
15    /// JSON serialization/deserialization error
16    #[error("JSON error: {0}")]
17    Json(#[from] serde_json::Error),
18
19    /// AimDB client error
20    #[error("AimDB client error: {0}")]
21    Client(#[from] aimdb_client::ClientError),
22
23    /// Invalid JSON-RPC request
24    #[error("Invalid JSON-RPC request: {0}")]
25    InvalidRequest(String),
26
27    /// Method not found
28    #[error("Method not found: {0}")]
29    MethodNotFound(String),
30
31    /// Invalid parameters
32    #[error("Invalid parameters: {0}")]
33    InvalidParams(String),
34
35    /// Internal error
36    #[error("Internal error: {0}")]
37    Internal(String),
38
39    /// Protocol not initialized
40    #[error("Protocol not initialized - call initialize first")]
41    NotInitialized,
42
43    /// Protocol version mismatch
44    #[error("Unsupported protocol version: {0}")]
45    UnsupportedProtocol(String),
46}
47
48impl McpError {
49    /// Convert error to JSON-RPC error code
50    pub fn error_code(&self) -> i32 {
51        match self {
52            McpError::InvalidRequest(_) => -32600,
53            McpError::MethodNotFound(_) => -32601,
54            McpError::InvalidParams(_) => -32602,
55            McpError::Internal(_) => -32603,
56            McpError::NotInitialized => -32002,
57            McpError::UnsupportedProtocol(_) => -32003,
58            _ => -32000, // Server error
59        }
60    }
61
62    /// Get error message for JSON-RPC response
63    pub fn message(&self) -> String {
64        self.to_string()
65    }
66}