agentic_evolve_mcp/types/
message.rs1use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6pub const JSONRPC_VERSION: &str = "2.0";
8
9#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
11#[serde(untagged)]
12pub enum RequestId {
13 String(String),
15 Number(i64),
17 Null,
19}
20
21impl std::fmt::Display for RequestId {
22 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23 match self {
24 RequestId::String(s) => write!(f, "{s}"),
25 RequestId::Number(n) => write!(f, "{n}"),
26 RequestId::Null => write!(f, "null"),
27 }
28 }
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct JsonRpcRequest {
34 pub jsonrpc: String,
36 pub id: RequestId,
38 pub method: String,
40 #[serde(default, skip_serializing_if = "Option::is_none")]
42 pub params: Option<Value>,
43}
44
45#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct JsonRpcResponse {
48 pub jsonrpc: String,
50 pub id: RequestId,
52 pub result: Value,
54}
55
56#[derive(Debug, Clone, Serialize, Deserialize)]
58pub struct JsonRpcError {
59 pub jsonrpc: String,
61 pub id: RequestId,
63 pub error: JsonRpcErrorObject,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct JsonRpcErrorObject {
70 pub code: i32,
72 pub message: String,
74 #[serde(default, skip_serializing_if = "Option::is_none")]
76 pub data: Option<Value>,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
81pub struct JsonRpcNotification {
82 pub jsonrpc: String,
84 pub method: String,
86 #[serde(default, skip_serializing_if = "Option::is_none")]
88 pub params: Option<Value>,
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize)]
93#[serde(untagged)]
94pub enum JsonRpcMessage {
95 Request(JsonRpcRequest),
97 Response(JsonRpcResponse),
99 Error(JsonRpcError),
101 Notification(JsonRpcNotification),
103}
104
105impl JsonRpcResponse {
106 pub fn new(id: RequestId, result: Value) -> Self {
108 Self {
109 jsonrpc: JSONRPC_VERSION.to_string(),
110 id,
111 result,
112 }
113 }
114}
115
116impl JsonRpcError {
117 pub fn new(id: RequestId, code: i32, message: String) -> Self {
119 Self {
120 jsonrpc: JSONRPC_VERSION.to_string(),
121 id,
122 error: JsonRpcErrorObject {
123 code,
124 message,
125 data: None,
126 },
127 }
128 }
129}
130
131impl JsonRpcNotification {
132 pub fn new(method: String, params: Option<Value>) -> Self {
134 Self {
135 jsonrpc: JSONRPC_VERSION.to_string(),
136 method,
137 params,
138 }
139 }
140}