#![allow(missing_docs)]
use std::{
collections::HashMap,
fmt::{self, Display, Formatter},
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use super::{ElicitRequestURLParams, TaskMetadata};
use crate::macros::with_meta;
pub const PREVIOUS_PROTOCOL_VERSION: &str = "2025-06-18";
pub const LATEST_PROTOCOL_VERSION: &str = "2025-11-25";
pub const JSONRPC_VERSION: &str = "2.0";
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum JSONRPCMessage {
Request(JSONRPCRequest),
Notification(JSONRPCNotification),
Response(JSONRPCResponse),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ProgressToken {
String(String),
Number(i64),
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(transparent)]
pub struct Cursor(pub String);
impl From<&str> for Cursor {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl From<String> for Cursor {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&String> for Cursor {
fn from(s: &String) -> Self {
Self(s.clone())
}
}
impl fmt::Display for Cursor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TaskAugmentedRequestParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub task: Option<TaskMetadata>,
#[serde(flatten)]
pub params: RequestParams,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct RequestParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub _meta: Option<RequestMeta>,
#[serde(flatten)]
pub other: HashMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct RequestMeta {
#[serde(rename = "progressToken", skip_serializing_if = "Option::is_none")]
pub progress_token: Option<ProgressToken>,
#[serde(flatten)]
pub other: HashMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Request {
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<RequestParams>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct NotificationParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub _meta: Option<HashMap<String, Value>>,
#[serde(flatten)]
pub other: HashMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Notification {
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<NotificationParams>,
}
#[with_meta]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JSONRpcResult {
#[serde(flatten)]
pub other: HashMap<String, Value>,
}
pub type Result = JSONRpcResult;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum RequestId {
String(String),
Number(i64),
}
impl RequestId {
pub fn to_key(&self) -> String {
match self {
Self::String(s) => s.clone(),
Self::Number(n) => format!("__num__{n}"),
}
}
}
impl Display for RequestId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::String(s) => write!(f, "{s}"),
Self::Number(n) => write!(f, "{n}"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JSONRPCRequest {
pub jsonrpc: String,
pub id: RequestId,
#[serde(flatten)]
pub request: Request,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JSONRPCNotification {
pub jsonrpc: String,
#[serde(flatten)]
pub notification: Notification,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JSONRPCResultResponse {
pub jsonrpc: String,
pub id: RequestId,
pub result: Result,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JSONRPCErrorResponse {
pub jsonrpc: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<RequestId>,
pub error: ErrorObject,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum JSONRPCResponse {
Result(JSONRPCResultResponse),
Error(JSONRPCErrorResponse),
}
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 URL_ELICITATION_REQUIRED: i32 = -32042;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorObject {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct URLElicitationRequiredError {
pub jsonrpc: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<RequestId>,
pub error: URLElicitationRequiredErrorObject,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct URLElicitationRequiredErrorObject {
pub code: i32,
pub message: String,
pub data: URLElicitationRequiredData,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct URLElicitationRequiredData {
pub elicitations: Vec<ElicitRequestURLParams>,
#[serde(flatten)]
pub other: HashMap<String, Value>,
}
pub type EmptyResult = Result;