1use crate::messaging::{JsonRpcError, JsonRpcResponse};
2use std::error::Error as StdError;
3use std::fmt;
4use thiserror::Error;
5use url::ParseError;
6
7#[derive(Debug, Error)]
8pub enum McpError {
9 #[error("I/O error: {0}")]
11 Io(#[from] std::io::Error),
12 #[error("Transport not open")]
13 TransportNotOpen,
14 #[error("Failed to spawn process")]
15 ProcessSpawnError,
16 #[error("Stdin not available")]
17 StdinNotAvailable,
18 #[error("Stdout not available")]
19 StdoutNotAvailable,
20 #[error("Stderr not available")]
21 StderrNotAvailable,
22
23 #[error("JSON-RPC error: {0}")]
25 JsonRpc(#[from] JsonRpcError),
26 #[error("Serialization error: {0}")]
27 SerializationError(#[from] serde_json::Error),
28 #[error("URL parse error: {0}")]
29 UrlParse(#[from] ParseError),
30 #[error("Missing result in response")]
31 MissingResult,
32
33 #[error("Client not initialized")]
35 ClientNotInitialized,
36 #[error("Unsupported capability: {0}")]
37 UnsupportedCapability(&'static str),
38 #[error("Request timeout for method '{method}': {source}")]
39 RequestTimeout {
40 method: String,
41 #[source]
42 source: std::sync::mpsc::RecvTimeoutError,
43 },
44 #[error("Command '{command}' failed: {error}")]
45 CommandFailed {
46 command: String,
47 error: JsonRpcError,
48 },
49 #[error("Failed to send response for request {id}: {source}")]
50 SendError {
51 id: u64,
52 #[source]
53 source: std::sync::mpsc::SendError<JsonRpcResponse>,
54 },
55 #[error("Terminating the client thread failed.")]
56 ThreadJoinFailed,
57
58 #[error("{0}")]
60 Other(String),
61}
62
63impl fmt::Display for JsonRpcError {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 write!(f, "JSON-RPC error {}: {}", self.code, self.message)
66 }
67}
68
69impl StdError for JsonRpcError {}