use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone)]
pub struct McpError {
pub code: i32,
pub message: String,
pub operation: String,
pub details: Option<String>,
}
impl McpError {
pub fn new(code: i32, message: impl Into<String>, operation: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
operation: operation.into(),
details: None,
}
}
pub fn with_details(mut self, details: impl Into<String>) -> Self {
self.details = Some(details.into());
self
}
pub fn invalid_params(message: impl Into<String>, operation: impl Into<String>) -> Self {
Self::new(-32602, message, operation)
}
pub fn internal_error(message: impl Into<String>, operation: impl Into<String>) -> Self {
Self::new(-32603, message, operation)
}
pub fn method_not_found(message: impl Into<String>, operation: impl Into<String>) -> Self {
Self::new(-32601, message, operation)
}
}
impl From<anyhow::Error> for McpError {
fn from(error: anyhow::Error) -> Self {
McpError::internal_error(error.to_string(), "unknown_operation")
}
}
impl std::fmt::Display for McpError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({})", self.message, self.operation)
}
}
impl std::error::Error for McpError {}
#[derive(Debug, Serialize, Deserialize)]
pub struct McpTool {
pub name: String,
pub description: String,
#[serde(rename = "inputSchema")]
pub input_schema: Value,
}