use serde::{Deserialize, Serialize};
use serde_json::Value;
pub const PROTOCOL_VERSION: &str = "2025-06-18";
pub const JSONRPC_VERSION: &str = "2.0";
#[derive(Debug, Clone, Deserialize)]
pub struct JsonRpcRequest {
#[allow(dead_code)] #[serde(default)]
pub jsonrpc: String,
#[serde(default)]
pub id: Option<Value>,
pub method: String,
#[serde(default)]
pub params: Option<Value>,
}
impl JsonRpcRequest {
#[must_use]
pub fn is_notification(&self) -> bool {
self.id.is_none()
}
}
#[derive(Debug, Clone, Serialize)]
pub struct JsonRpcResponse {
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<JsonRpcError>,
}
impl JsonRpcResponse {
#[must_use]
pub fn success(id: Value, result: Value) -> Self {
Self {
jsonrpc: JSONRPC_VERSION,
id,
result: Some(result),
error: None,
}
}
#[must_use]
pub fn failure(id: Value, error: JsonRpcError) -> Self {
Self {
jsonrpc: JSONRPC_VERSION,
id,
result: None,
error: Some(error),
}
}
}
#[derive(Debug, Clone, Serialize)]
pub struct JsonRpcError {
pub code: i64,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
impl JsonRpcError {
#[must_use]
pub fn new(code: i64, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
data: None,
}
}
#[must_use]
pub fn parse_error() -> Self {
Self::new(codes::PARSE_ERROR, "parse error: invalid JSON")
}
#[must_use]
pub fn invalid_request(detail: impl Into<String>) -> Self {
Self::new(codes::INVALID_REQUEST, detail)
}
#[must_use]
pub fn method_not_found(method: &str) -> Self {
Self::new(
codes::METHOD_NOT_FOUND,
format!("method not found: {method}"),
)
}
#[must_use]
pub fn invalid_params(detail: impl Into<String>) -> Self {
Self::new(codes::INVALID_PARAMS, detail)
}
}
pub mod codes {
pub const PARSE_ERROR: i64 = -32700;
pub const INVALID_REQUEST: i64 = -32600;
pub const METHOD_NOT_FOUND: i64 = -32601;
pub const INVALID_PARAMS: i64 = -32602;
pub const INTERNAL_ERROR: i64 = -32603;
pub const TOOL_NOT_FOUND: i64 = -32002;
pub const TOOL_FORBIDDEN: i64 = -32003;
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InitializeResult {
pub protocol_version: &'static str,
pub capabilities: ServerCapabilities,
pub server_info: Implementation,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ServerCapabilities {
#[serde(skip_serializing_if = "Option::is_none")]
pub tools: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub prompts: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub resources: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub logging: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub completions: Option<Value>,
}
#[derive(Debug, Clone, Serialize)]
pub struct Implementation {
pub name: &'static str,
pub version: &'static str,
}