1use rust_mcp_sdk::schema::schema_utils::CallToolError;
7use serde_json;
8
9#[derive(Debug, thiserror::Error)]
11pub enum FoundryMcpError {
12 #[error("Parameter validation failed: {message}")]
14 InvalidParams { message: String },
15
16 #[error("CLI command execution failed: {source}")]
18 CliCommand {
19 #[from]
20 source: anyhow::Error,
21 },
22
23 #[error("JSON serialization failed: {source}")]
25 Serialization {
26 #[from]
27 source: serde_json::Error,
28 },
29
30 #[error("File system operation failed: {source}")]
32 Filesystem {
33 #[from]
34 source: std::io::Error,
35 },
36
37 #[error("Transport error: {message}")]
39 Transport { message: String },
40
41 #[error("Internal server error: {message}")]
43 Internal { message: String },
44}
45
46#[derive(Debug)]
48pub struct InvalidParamsError(pub String);
49
50impl std::fmt::Display for InvalidParamsError {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 write!(f, "Parameter validation failed: {}", self.0)
53 }
54}
55
56impl std::error::Error for InvalidParamsError {}
57
58#[derive(Debug)]
59pub struct InternalMcpError(pub String);
60
61impl std::fmt::Display for InternalMcpError {
62 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63 write!(f, "Internal server error: {}", self.0)
64 }
65}
66
67impl std::error::Error for InternalMcpError {}
68
69impl From<FoundryMcpError> for CallToolError {
71 fn from(err: FoundryMcpError) -> Self {
72 match err {
73 FoundryMcpError::InvalidParams { message } => {
74 CallToolError::new(InvalidParamsError(message))
75 }
76 FoundryMcpError::CliCommand { source } => {
77 CallToolError::new(InternalMcpError(format!("CLI command failed: {}", source)))
78 }
79 FoundryMcpError::Serialization { source } => CallToolError::new(InternalMcpError(
80 format!("JSON serialization failed: {}", source),
81 )),
82 FoundryMcpError::Filesystem { source } => {
83 CallToolError::new(InternalMcpError(format!("File system error: {}", source)))
84 }
85 FoundryMcpError::Transport { message } => {
86 CallToolError::new(InternalMcpError(format!("Transport error: {}", message)))
87 }
88 FoundryMcpError::Internal { message } => CallToolError::new(InternalMcpError(message)),
89 }
90 }
91}
92
93impl FoundryMcpError {
95 pub fn invalid_params<S: Into<String>>(message: S) -> Self {
96 FoundryMcpError::InvalidParams {
97 message: message.into(),
98 }
99 }
100
101 pub fn transport_error<S: Into<String>>(message: S) -> Self {
102 FoundryMcpError::Transport {
103 message: message.into(),
104 }
105 }
106
107 pub fn internal_error<S: Into<String>>(message: S) -> Self {
108 FoundryMcpError::Internal {
109 message: message.into(),
110 }
111 }
112}