foundry_mcp/mcp/
error.rs

1//! # Foundry MCP Error Types
2//!
3//! This module defines comprehensive error types for the Foundry MCP server,
4//! providing proper error categorization and conversion from various error sources.
5
6use rust_mcp_sdk::schema::schema_utils::CallToolError;
7use serde_json;
8
9/// Comprehensive error type for Foundry MCP server operations
10#[derive(Debug, thiserror::Error)]
11pub enum FoundryMcpError {
12    /// Parameter validation errors (invalid input parameters)
13    #[error("Parameter validation failed: {message}")]
14    InvalidParams { message: String },
15
16    /// CLI command execution errors
17    #[error("CLI command execution failed: {source}")]
18    CliCommand {
19        #[from]
20        source: anyhow::Error,
21    },
22
23    /// JSON serialization/deserialization errors
24    #[error("JSON serialization failed: {source}")]
25    Serialization {
26        #[from]
27        source: serde_json::Error,
28    },
29
30    /// File system operation errors
31    #[error("File system operation failed: {source}")]
32    Filesystem {
33        #[from]
34        source: std::io::Error,
35    },
36
37    /// Transport layer errors
38    #[error("Transport error: {message}")]
39    Transport { message: String },
40
41    /// Internal server errors
42    #[error("Internal server error: {message}")]
43    Internal { message: String },
44}
45
46/// Custom error types that implement std::error::Error for MCP protocol
47#[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
69/// Convert FoundryMcpError to CallToolError for MCP protocol compliance
70impl 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
93/// Utility function to create parameter validation errors
94impl 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}