@@ -24,53 +24,95 @@ pub const SUPPORTED_PROTOCOL_VERSIONS: &[&str] = &[LATEST_PROTOCOL_VERSION, "202
24
24
pub const JSONRPC_VERSION : & str = "2.0" ;
25
25
26
26
/// A unique identifier for a request
27
+ ///
28
+ /// This enum represents the possible types of request identifiers in the MCP protocol.
29
+ /// It can be either a string or a number, as per JSON-RPC 2.0 specification.
27
30
#[ derive( Debug , Clone , PartialEq , Eq , Hash , Serialize , Deserialize ) ]
28
31
#[ serde( untagged) ]
29
32
pub enum RequestId {
33
+ /// String representation of the request ID
30
34
String ( String ) ,
35
+ /// Numeric representation of the request ID
31
36
Number ( i64 ) ,
32
37
}
33
38
34
39
/// Base JSON-RPC request structure
40
+ ///
41
+ /// This struct represents a JSON-RPC request in the MCP protocol.
42
+ /// It includes the JSON-RPC version, method name, optional parameters, and a unique identifier.
35
43
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
36
44
pub struct Request {
45
+ /// JSON-RPC version (always "2.0")
37
46
pub jsonrpc : String ,
47
+ /// Name of the method to be invoked
38
48
pub method : String ,
49
+ /// Optional parameters for the method
39
50
#[ serde( skip_serializing_if = "Option::is_none" ) ]
40
51
pub params : Option < Value > ,
52
+ /// Unique identifier for the request
41
53
pub id : RequestId ,
42
54
}
43
55
44
56
/// Base JSON-RPC notification structure
57
+ ///
58
+ /// This struct represents a JSON-RPC notification in the MCP protocol.
59
+ /// It is similar to a request but does not include an id field, as it does not expect a response.
45
60
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
46
61
pub struct Notification {
62
+ /// JSON-RPC version (always "2.0")
47
63
pub jsonrpc : String ,
64
+ /// Name of the method to be invoked
48
65
pub method : String ,
66
+ /// Optional parameters for the method
49
67
#[ serde( skip_serializing_if = "Option::is_none" ) ]
50
68
pub params : Option < Value > ,
51
69
}
52
70
53
71
/// Base JSON-RPC response structure
72
+ ///
73
+ /// This struct represents a JSON-RPC response in the MCP protocol.
74
+ /// It includes the JSON-RPC version, the id of the request it's responding to,
75
+ /// and either a result or an error field.
54
76
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
55
77
pub struct Response {
78
+ /// JSON-RPC version (always "2.0")
56
79
pub jsonrpc : String ,
80
+ /// ID of the request this response corresponds to
57
81
pub id : RequestId ,
82
+ /// The result of a successful request (null if there was an error)
58
83
#[ serde( skip_serializing_if = "Option::is_none" ) ]
59
84
pub result : Option < Value > ,
85
+ /// The error object if the request failed (null if the request was successful)
60
86
#[ serde( skip_serializing_if = "Option::is_none" ) ]
61
87
pub error : Option < ResponseError > ,
62
88
}
63
89
64
90
/// JSON-RPC error object
91
+ ///
92
+ /// This struct represents an error that occurred during the processing of a JSON-RPC request.
65
93
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
66
94
pub struct ResponseError {
95
+ /// The error code
67
96
pub code : i32 ,
97
+ /// A short description of the error
68
98
pub message : String ,
99
+ /// Additional information about the error
69
100
#[ serde( skip_serializing_if = "Option::is_none" ) ]
70
101
pub data : Option < Value > ,
71
102
}
72
103
73
104
impl Request {
105
+ /// Creates a new Request instance
106
+ ///
107
+ /// # Arguments
108
+ ///
109
+ /// * `method` - The name of the method to be invoked
110
+ /// * `params` - Optional parameters for the method
111
+ /// * `id` - Unique identifier for the request
112
+ ///
113
+ /// # Returns
114
+ ///
115
+ /// A new Request instance
74
116
pub fn new ( method : impl Into < String > , params : Option < Value > , id : RequestId ) -> Self {
75
117
Self {
76
118
jsonrpc : crate :: JSONRPC_VERSION . to_string ( ) ,
@@ -82,6 +124,16 @@ impl Request {
82
124
}
83
125
84
126
impl Notification {
127
+ /// Creates a new Notification instance
128
+ ///
129
+ /// # Arguments
130
+ ///
131
+ /// * `method` - The name of the method to be invoked
132
+ /// * `params` - Optional parameters for the method
133
+ ///
134
+ /// # Returns
135
+ ///
136
+ /// A new Notification instance
85
137
pub fn new ( method : impl Into < String > , params : Option < Value > ) -> Self {
86
138
Self {
87
139
jsonrpc : crate :: JSONRPC_VERSION . to_string ( ) ,
@@ -92,6 +144,16 @@ impl Notification {
92
144
}
93
145
94
146
impl Response {
147
+ /// Creates a new successful Response instance
148
+ ///
149
+ /// # Arguments
150
+ ///
151
+ /// * `id` - The id of the request this response corresponds to
152
+ /// * `result` - The result of the successful request
153
+ ///
154
+ /// # Returns
155
+ ///
156
+ /// A new Response instance representing a successful result
95
157
pub fn success ( id : RequestId , result : Option < Value > ) -> Self {
96
158
Self {
97
159
jsonrpc : crate :: JSONRPC_VERSION . to_string ( ) ,
@@ -101,6 +163,16 @@ impl Response {
101
163
}
102
164
}
103
165
166
+ /// Creates a new error Response instance
167
+ ///
168
+ /// # Arguments
169
+ ///
170
+ /// * `id` - The id of the request this response corresponds to
171
+ /// * `error` - The error that occurred during request processing
172
+ ///
173
+ /// # Returns
174
+ ///
175
+ /// A new Response instance representing an error
104
176
pub fn error ( id : RequestId , error : ResponseError ) -> Self {
105
177
Self {
106
178
jsonrpc : crate :: JSONRPC_VERSION . to_string ( ) ,
@@ -148,6 +220,9 @@ impl From<Error> for ResponseError {
148
220
}
149
221
150
222
impl fmt:: Display for RequestId {
223
+ /// Provides a string representation of the RequestId
224
+ ///
225
+ /// This implementation allows RequestId to be easily printed or converted to a string.
151
226
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
152
227
match self {
153
228
RequestId :: String ( s) => write ! ( f, "{}" , s) ,
@@ -228,4 +303,4 @@ mod tests {
228
303
assert ! ( SUPPORTED_PROTOCOL_VERSIONS . contains( & LATEST_PROTOCOL_VERSION ) ) ;
229
304
assert_eq ! ( JSONRPC_VERSION , "2.0" ) ;
230
305
}
231
- }
306
+ }
0 commit comments