use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt;
pub const JSON_RPC_VERSION: &str = "2.0";
pub mod error_codes {
pub const PARSE_ERROR: i32 = -32700;
pub const INVALID_REQUEST: i32 = -32600;
pub const METHOD_NOT_FOUND: i32 = -32601;
pub const INVALID_PARAMS: i32 = -32602;
pub const INTERNAL_ERROR: i32 = -32603;
pub const SERVER_ERROR: i32 = -32000;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum JsonRpcMessage {
Request(JsonRpcRequest),
Response(JsonRpcResponse),
Notification(JsonRpcNotification),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcRequest {
pub jsonrpc: String,
pub id: Value,
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<Value>,
}
impl JsonRpcRequest {
pub fn new(id: impl Into<Value>, method: impl Into<String>, params: Option<Value>) -> Self {
Self {
jsonrpc: JSON_RPC_VERSION.to_string(),
id: id.into(),
method: method.into(),
params,
}
}
pub fn list_tools(id: impl Into<Value>) -> Self {
Self::new(id, "tools/list", None)
}
pub fn call_tool(id: impl Into<Value>, name: impl Into<String>, args: Value) -> Self {
let params = serde_json::json!({
"name": name.into(),
"arguments": args
});
Self::new(id, "tools/call", Some(params))
}
pub fn list_resources(id: impl Into<Value>) -> Self {
Self::new(id, "resources/list", None)
}
pub fn get_resource(id: impl Into<Value>, uri: impl Into<String>) -> Self {
let params = serde_json::json!({
"uri": uri.into()
});
Self::new(id, "resources/get", Some(params))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcNotification {
pub jsonrpc: String,
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<Value>,
}
impl JsonRpcNotification {
pub fn new(method: impl Into<String>, params: Option<Value>) -> Self {
Self {
jsonrpc: JSON_RPC_VERSION.to_string(),
method: method.into(),
params,
}
}
pub fn initialized() -> Self {
Self::new("notifications/initialized", None)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcError {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
impl fmt::Display for JsonRpcError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "JSON-RPC error {}: {}", self.code, self.message)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcResponse {
pub jsonrpc: String,
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 {
pub fn success(id: impl Into<Value>, result: Value) -> Self {
Self {
jsonrpc: JSON_RPC_VERSION.to_string(),
id: id.into(),
result: Some(result),
error: None,
}
}
pub fn error(
id: impl Into<Value>,
code: i32,
message: impl Into<String>,
data: Option<Value>,
) -> Self {
Self {
jsonrpc: JSON_RPC_VERSION.to_string(),
id: id.into(),
result: None,
error: Some(JsonRpcError {
code,
message: message.into(),
data,
}),
}
}
pub fn is_success(&self) -> bool {
self.error.is_none() && self.result.is_some()
}
}