use serde::{Deserialize, Serialize};
use serde_json::Value;
pub const MCP_PROTOCOL_VERSION: &str = "2024-11-05";
#[derive(Deserialize, Debug)]
pub(crate) struct Request {
#[serde(default)]
pub jsonrpc: String,
pub id: Option<Value>,
pub method: String,
#[serde(default)]
pub params: Value,
}
#[derive(Serialize, Debug)]
pub(crate) struct Response {
pub jsonrpc: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<RpcError>,
}
#[derive(Serialize, Debug)]
pub(crate) struct RpcError {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
impl Response {
pub(crate) fn ok(id: Option<Value>, result: Value) -> Self {
Self {
jsonrpc: "2.0",
id,
result: Some(result),
error: None,
}
}
pub(crate) fn err(id: Option<Value>, code: i32, message: impl Into<String>) -> Self {
Self {
jsonrpc: "2.0",
id,
result: None,
error: Some(RpcError {
code,
message: message.into(),
data: None,
}),
}
}
}
pub(crate) mod error_code {
pub(crate) const PARSE_ERROR: i32 = -32700;
pub(crate) const INVALID_REQUEST: i32 = -32600;
pub(crate) const METHOD_NOT_FOUND: i32 = -32601;
#[allow(
dead_code,
reason = "standard JSON-RPC code; referenced by future error paths"
)]
pub(crate) const INVALID_PARAMS: i32 = -32602;
#[allow(
dead_code,
reason = "standard JSON-RPC code; referenced by future error paths"
)]
pub(crate) const INTERNAL_ERROR: i32 = -32603;
}
#[derive(Serialize, Debug)]
pub struct ToolDef {
pub name: &'static str,
pub description: &'static str,
#[serde(rename = "inputSchema")]
pub input_schema: Value,
}