Skip to main content

memory_mcp/
error.rs

1use thiserror::Error;
2
3/// Errors produced by the memory engine.
4#[derive(Debug, Error)]
5#[non_exhaustive]
6pub enum MemoryError {
7    /// An operation on the git-backed store failed.
8    #[error("git error: {0}")]
9    Git(#[from] git2::Error),
10
11    /// The embedding backend failed to produce vectors.
12    #[error("embedding error: {0}")]
13    Embedding(String),
14
15    /// The vector index could not complete the requested operation.
16    #[error("index error: {0}")]
17    Index(String),
18
19    /// A filesystem I/O error occurred.
20    #[error("io error: {0}")]
21    Io(#[from] std::io::Error),
22
23    /// The requested memory does not exist.
24    #[error("memory not found: {name}")]
25    NotFound {
26        /// Name of the missing memory.
27        name: String,
28    },
29
30    /// The caller provided invalid parameters.
31    #[error("invalid input: {reason}")]
32    InvalidInput {
33        /// Why the input was rejected.
34        reason: String,
35    },
36
37    /// Authentication failed (e.g. bad credentials).
38    #[error("auth error: {0}")]
39    Auth(String),
40
41    /// An OAuth flow error occurred.
42    #[error("oauth error: {0}")]
43    OAuth(String),
44
45    /// The credential store could not read or write a token.
46    #[error("token storage error: {0}")]
47    TokenStorage(String),
48
49    /// YAML serialisation or deserialisation failed.
50    #[error("yaml error: {0}")]
51    Yaml(#[from] serde_yaml_ng::Error),
52
53    /// The remote server rejected one or more ref updates during push.
54    #[error("push rejected: {0}")]
55    PushRejected(String),
56
57    /// A background task failed to join.
58    #[error("task join error: {0}")]
59    Join(String),
60
61    /// Catch-all for unexpected internal failures.
62    #[error("internal error: {0}")]
63    Internal(String),
64}
65
66impl From<MemoryError> for rmcp::model::ErrorData {
67    fn from(err: MemoryError) -> Self {
68        let code = match &err {
69            MemoryError::NotFound { .. } | MemoryError::InvalidInput { .. } => {
70                rmcp::model::ErrorCode::INVALID_PARAMS
71            }
72            // PushRejected is a server-side policy decision (e.g. branch
73            // protection), not an internal fault. No standard JSON-RPC code
74            // fits precisely; INTERNAL_ERROR is the least-bad option until
75            // MCP defines application-level error codes.
76            _ => rmcp::model::ErrorCode::INTERNAL_ERROR,
77        };
78        rmcp::model::ErrorData {
79            code,
80            message: std::borrow::Cow::Owned(err.to_string()),
81            data: None,
82        }
83    }
84}