use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Deserialize)]
pub struct McpRequest {
pub id: Value,
pub method: String,
pub params: Option<Value>,
}
#[derive(Debug, Serialize)]
pub struct McpResponse {
pub jsonrpc: &'static str,
pub id: Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<McpError>,
}
#[derive(Debug, Serialize)]
pub struct McpError {
pub code: i32,
pub message: String,
}
pub const METHOD_NOT_FOUND: i32 = -32601;
pub const INVALID_PARAMS: i32 = -32602;
pub const INTERNAL_ERROR: i32 = -32603;
pub const PARSE_ERROR: i32 = -32700;
impl McpResponse {
pub fn ok(id: Value, result: Value) -> Self {
Self {
jsonrpc: "2.0",
id,
result: Some(result),
error: None,
}
}
pub fn err(id: Value, code: i32, message: impl Into<String>) -> Self {
Self {
jsonrpc: "2.0",
id,
result: None,
error: Some(McpError {
code,
message: message.into(),
}),
}
}
pub fn parse_error(e: serde_json::Error) -> Self {
Self {
jsonrpc: "2.0",
id: Value::Null,
result: None,
error: Some(McpError {
code: PARSE_ERROR,
message: e.to_string(),
}),
}
}
}