use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcRequest {
pub jsonrpc: String,
#[serde(default)]
pub id: Option<Value>,
pub method: String,
#[serde(default)]
pub params: Value,
}
impl JsonRpcRequest {
pub fn new(id: impl Into<Value>, method: impl Into<String>) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id: Some(id.into()),
method: method.into(),
params: Value::Null,
}
}
pub fn with_params(id: impl Into<Value>, method: impl Into<String>, params: Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id: Some(id.into()),
method: method.into(),
params,
}
}
}
#[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 JsonRpcError {
pub fn parse_error(detail: impl Into<String>) -> Self {
Self {
code: -32700,
message: "Parse error".to_string(),
data: Some(Value::String(detail.into())),
}
}
pub fn invalid_request(detail: impl Into<String>) -> Self {
Self {
code: -32600,
message: "Invalid Request".to_string(),
data: Some(Value::String(detail.into())),
}
}
pub fn method_not_found(method: impl Into<String>) -> Self {
Self {
code: -32601,
message: "Method not found".to_string(),
data: Some(Value::String(method.into())),
}
}
pub fn tool_not_found(tool: impl Into<String>) -> Self {
Self {
code: -32803,
message: "Tool not found".to_string(),
data: Some(Value::String(tool.into())),
}
}
pub fn invalid_params(detail: impl Into<String>) -> Self {
Self {
code: -32602,
message: "Invalid params".to_string(),
data: Some(Value::String(detail.into())),
}
}
pub fn internal_error(detail: impl Into<String>) -> Self {
Self {
code: -32603,
message: "Internal error".to_string(),
data: Some(Value::String(detail.into())),
}
}
}
#[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: Value, result: Value) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id,
result: Some(result),
error: None,
}
}
pub fn error(id: Value, error: JsonRpcError) -> Self {
Self {
jsonrpc: "2.0".to_string(),
id,
result: None,
error: Some(error),
}
}
pub fn tool_error(id: Value, message: impl Into<String>) -> Self {
Self::success(
id,
serde_json::json!({
"content": [{"type": "text", "text": message.into()}],
"isError": true
}),
)
}
pub fn into_tool_error_if_needed(self) -> Self {
if let Some(ref err) = self.error {
match err.code {
-32700 | -32600 | -32601 | -32803 => self, _ => {
let msg = if let Some(ref data) = err.data {
format!("{}: {}", err.message, data)
} else {
err.message.clone()
};
Self::tool_error(self.id.clone(), msg)
}
}
} else {
self
}
}
}
#[allow(clippy::result_large_err)]
pub fn parse_request(raw: &str) -> Result<JsonRpcRequest, JsonRpcResponse> {
let value: Value = serde_json::from_str(raw).map_err(|e| {
JsonRpcResponse::error(Value::Null, JsonRpcError::parse_error(e.to_string()))
})?;
let request: JsonRpcRequest = serde_json::from_value(value).map_err(|e| {
JsonRpcResponse::error(Value::Null, JsonRpcError::invalid_request(e.to_string()))
})?;
if request.jsonrpc != "2.0" {
return Err(JsonRpcResponse::error(
request.id.unwrap_or(Value::Null),
JsonRpcError::invalid_request("jsonrpc must be \"2.0\""),
));
}
Ok(request)
}