use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize)]
pub struct JsonRpcRequest {
pub jsonrpc: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<u64>,
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<serde_json::Value>,
}
impl JsonRpcRequest {
pub fn new(id: u64, method: &str, params: serde_json::Value) -> Self {
Self {
jsonrpc: "2.0",
id: Some(id),
method: method.to_string(),
params: Some(params),
}
}
pub fn notification(method: &str, params: serde_json::Value) -> Self {
Self {
jsonrpc: "2.0",
id: None,
method: method.to_string(),
params: Some(params),
}
}
}
#[derive(Debug, Deserialize)]
pub struct JsonRpcResponse {
#[allow(dead_code)]
pub jsonrpc: String,
pub id: Option<u64>,
pub result: Option<serde_json::Value>,
pub error: Option<JsonRpcError>,
}
#[derive(Debug, Deserialize)]
pub struct JsonRpcError {
pub code: i64,
pub message: String,
#[allow(dead_code)]
pub data: Option<serde_json::Value>,
}
impl std::fmt::Display for JsonRpcError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "JSON-RPC error {}: {}", self.code, self.message)
}
}
#[derive(Debug, Deserialize)]
pub struct ToolResult {
pub content: Vec<ContentBlock>,
#[serde(rename = "isError", default)]
pub is_error: bool,
}
#[derive(Debug, Deserialize)]
#[serde(tag = "type")]
pub enum ContentBlock {
#[serde(rename = "text")]
Text { text: String },
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct ToolInfo {
pub name: String,
pub description: Option<String>,
#[serde(rename = "inputSchema")]
pub input_schema: Option<serde_json::Value>,
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub struct ToolsList {
pub tools: Vec<ToolInfo>,
}