use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value, Map};
use jsonrpc_core::{Params, Error as JsonRpcError};
use std::path::PathBuf;
#[derive(Debug, Serialize, Deserialize)]
pub struct McpResponse {
pub status: McpStatus,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<McpError>,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<McpMetadata>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum McpStatus {
Success,
Error,
Pending,
Warning,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct McpError {
pub code: String,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub details: Option<Value>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct McpMetadata {
#[serde(skip_serializing_if = "Option::is_none")]
pub execution_time_ms: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub project_path: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub database_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub connection: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub context: Option<Map<String, Value>>,
}
#[allow(dead_code)]
impl McpResponse {
pub fn success(data: Value) -> Self {
Self {
status: McpStatus::Success,
error: None,
data: Some(data),
metadata: None,
}
}
pub fn success_with_metadata(data: Value, metadata: McpMetadata) -> Self {
Self {
status: McpStatus::Success,
error: None,
data: Some(data),
metadata: Some(metadata),
}
}
pub fn error(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
status: McpStatus::Error,
error: Some(McpError {
code: code.into(),
message: message.into(),
details: None,
}),
data: None,
metadata: None,
}
}
pub fn error_with_details(
code: impl Into<String>,
message: impl Into<String>,
details: Value
) -> Self {
Self {
status: McpStatus::Error,
error: Some(McpError {
code: code.into(),
message: message.into(),
details: Some(details),
}),
data: None,
metadata: None,
}
}
pub fn pending(message: impl Into<String>) -> Self {
Self {
status: McpStatus::Pending,
error: None,
data: Some(json!({"message": message.into()})),
metadata: None,
}
}
pub fn warning(message: impl Into<String>, data: Option<Value>) -> Self {
Self {
status: McpStatus::Warning,
error: Some(McpError {
code: "WARNING".to_string(),
message: message.into(),
details: None,
}),
data,
metadata: None,
}
}
pub fn to_json_value(self) -> Value {
serde_json::to_value(self).unwrap_or_else(|_| {
json!({
"status": "error",
"error": {
"code": "SERIALIZATION_ERROR",
"message": "Failed to serialize response"
}
})
})
}
}
#[allow(dead_code)]
pub struct McpParams;
#[allow(dead_code)]
impl McpParams {
pub fn parse(params: Params) -> Result<Map<String, Value>, JsonRpcError> {
params.parse::<Map<String, Value>>()
.map_err(|_| JsonRpcError::invalid_params("Invalid parameters format"))
}
pub fn get_required_string(
params: &Map<String, Value>,
key: &str
) -> Result<String, JsonRpcError> {
params
.get(key)
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.ok_or_else(|| JsonRpcError::invalid_params(&format!("Missing required parameter: {}", key)))
}
pub fn get_optional_string(
params: &Map<String, Value>,
key: &str
) -> Option<String> {
params
.get(key)
.and_then(|v| v.as_str())
.map(|s| s.to_string())
}
pub fn get_optional_bool(
params: &Map<String, Value>,
key: &str
) -> Option<bool> {
params
.get(key)
.and_then(|v| v.as_bool())
}
pub fn get_optional_string_array(
params: &Map<String, Value>,
key: &str
) -> Option<Vec<String>> {
params
.get(key)
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str())
.map(|s| s.to_string())
.collect()
})
}
pub fn get_project_path(
params: &Map<String, Value>
) -> Result<PathBuf, JsonRpcError> {
let path_str = Self::get_optional_string(params, "project_path")
.unwrap_or_else(|| std::env::current_dir()
.unwrap_or_default()
.to_string_lossy()
.to_string());
let path = PathBuf::from(path_str);
if !path.exists() {
return Err(JsonRpcError::invalid_params("Project path does not exist"));
}
Ok(path)
}
}
#[allow(dead_code)]
pub struct McpContext {
pub project_path: PathBuf,
pub start_time: std::time::Instant,
}
#[allow(dead_code)]
impl McpContext {
pub fn new(project_path: PathBuf) -> Self {
Self {
project_path,
start_time: std::time::Instant::now(),
}
}
pub fn create_metadata(&self) -> McpMetadata {
McpMetadata {
execution_time_ms: Some(self.start_time.elapsed().as_millis() as u64),
project_path: Some(self.project_path.to_string_lossy().to_string()),
database_type: None,
connection: None,
context: None,
}
}
pub fn create_db_metadata(
&self,
database_type: Option<String>,
connection: Option<String>
) -> McpMetadata {
McpMetadata {
execution_time_ms: Some(self.start_time.elapsed().as_millis() as u64),
project_path: Some(self.project_path.to_string_lossy().to_string()),
database_type,
connection,
context: None,
}
}
}
#[allow(dead_code)]
#[async_trait::async_trait]
pub trait McpHandler {
fn tool_name(&self) -> &'static str;
async fn execute(&self, params: Params) -> Result<Value, JsonRpcError>;
fn description(&self) -> &'static str;
fn parameter_schema(&self) -> Value {
json!({
"type": "object",
"properties": {},
"required": []
})
}
}
#[macro_export]
macro_rules! impl_mcp_handler {
(
$handler:ident,
$tool_name:literal,
$description:literal,
$execute_fn:ident
) => {
pub struct $handler;
#[async_trait::async_trait]
impl McpHandler for $handler {
fn tool_name(&self) -> &'static str {
$tool_name
}
async fn execute(&self, params: Params) -> Result<Value, JsonRpcError> {
$execute_fn(params).await
}
fn description(&self) -> &'static str {
$description
}
}
};
}
#[allow(dead_code)]
pub fn error_to_mcp_response(error: anyhow::Error) -> Value {
let error_msg = error.to_string();
let error_code = if error_msg.contains("connection") {
"CONNECTION_ERROR"
} else if error_msg.contains("permission") || error_msg.contains("access") {
"ACCESS_ERROR"
} else if error_msg.contains("not found") || error_msg.contains("missing") {
"NOT_FOUND"
} else if error_msg.contains("invalid") {
"INVALID_INPUT"
} else if error_msg.contains("timeout") {
"TIMEOUT_ERROR"
} else {
"INTERNAL_ERROR"
};
McpResponse::error(error_code, error_msg).to_json_value()
}
#[allow(dead_code)]
pub fn result_to_json_rpc<T>(
result: Result<T>,
context: Option<McpContext>
) -> Result<Value, JsonRpcError>
where
T: Serialize,
{
match result {
Ok(data) => {
let json_data = serde_json::to_value(data)
.map_err(|_| JsonRpcError::internal_error())?;
let response = if let Some(ctx) = context {
McpResponse::success_with_metadata(json_data, ctx.create_metadata())
} else {
McpResponse::success(json_data)
};
Ok(response.to_json_value())
}
Err(error) => {
Ok(error_to_mcp_response(error))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mcp_response_success() {
let data = json!({"test": "value"});
let response = McpResponse::success(data.clone());
assert!(matches!(response.status, McpStatus::Success));
assert_eq!(response.data, Some(data));
assert!(response.error.is_none());
}
#[test]
fn test_mcp_response_error() {
let response = McpResponse::error("TEST_ERROR", "Test error message");
assert!(matches!(response.status, McpStatus::Error));
assert!(response.data.is_none());
assert!(response.error.is_some());
let error = response.error.unwrap();
assert_eq!(error.code, "TEST_ERROR");
assert_eq!(error.message, "Test error message");
}
#[test]
fn test_mcp_params_extraction() {
let mut params_map = Map::new();
params_map.insert("test_string".to_string(), json!("test_value"));
params_map.insert("test_bool".to_string(), json!(true));
params_map.insert("test_array".to_string(), json!(["item1", "item2"]));
assert_eq!(
McpParams::get_optional_string(¶ms_map, "test_string"),
Some("test_value".to_string())
);
assert_eq!(
McpParams::get_optional_bool(¶ms_map, "test_bool"),
Some(true)
);
assert_eq!(
McpParams::get_optional_string_array(¶ms_map, "test_array"),
Some(vec!["item1".to_string(), "item2".to_string()])
);
}
}