@@ -4,13 +4,31 @@ use serde_json::{json, Value};
4
4
use std:: hash:: { Hash , Hasher } ;
5
5
use std:: { fmt:: Display , str:: FromStr } ;
6
6
7
- #[ derive( Debug ) ]
7
+ #[ derive( Debug , PartialEq ) ]
8
8
pub enum MessageTypes {
9
9
Request ,
10
10
Response ,
11
11
Notification ,
12
12
Error ,
13
13
}
14
+ /// Implements the `Display` trait for the `MessageTypes` enum,
15
+ /// allowing it to be converted into a human-readable string.
16
+ impl Display for MessageTypes {
17
+ /// Formats the `MessageTypes` enum variant as a string.
18
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
19
+ write ! (
20
+ f,
21
+ "{}" ,
22
+ // Match the current enum variant and return a corresponding string
23
+ match self {
24
+ MessageTypes :: Request => "Request" ,
25
+ MessageTypes :: Response => "Response" ,
26
+ MessageTypes :: Notification => "Notification" ,
27
+ MessageTypes :: Error => "Error" ,
28
+ }
29
+ )
30
+ }
31
+ }
14
32
15
33
/// A utility function used internally to detect the message type from the payload.
16
34
/// This function is used when deserializing a `ClientMessage` into strongly-typed structs that represent the specific message received.
@@ -38,12 +56,18 @@ fn detect_message_type(value: &serde_json::Value) -> MessageTypes {
38
56
MessageTypes :: Request
39
57
}
40
58
41
- pub trait MCPMessage {
59
+ /// Represents a generic MCP (Model Content Protocol) message.
60
+ /// This trait defines methods to classify and extract information from messages.
61
+ pub trait RPCMessage {
62
+ fn request_id ( & self ) -> Option < & RequestId > ;
63
+ }
64
+
65
+ pub trait MCPMessage : RPCMessage {
66
+ fn message_type ( & self ) -> MessageTypes ;
42
67
fn is_response ( & self ) -> bool ;
43
68
fn is_request ( & self ) -> bool ;
44
69
fn is_notification ( & self ) -> bool ;
45
70
fn is_error ( & self ) -> bool ;
46
- fn request_id ( & self ) -> Option < & RequestId > ;
47
71
}
48
72
49
73
//*******************************//
@@ -94,6 +118,22 @@ pub enum ClientMessage {
94
118
Error ( JsonrpcError ) ,
95
119
}
96
120
121
+ impl RPCMessage for ClientMessage {
122
+ // Retrieves the request ID associated with the message, if applicable
123
+ fn request_id ( & self ) -> Option < & RequestId > {
124
+ match self {
125
+ // If the message is a request, return the associated request ID
126
+ ClientMessage :: Request ( client_jsonrpc_request) => Some ( & client_jsonrpc_request. id ) ,
127
+ // Notifications do not have request IDs
128
+ ClientMessage :: Notification ( _) => None ,
129
+ // If the message is a response, return the associated request ID
130
+ ClientMessage :: Response ( client_jsonrpc_response) => Some ( & client_jsonrpc_response. id ) ,
131
+ // If the message is an error, return the associated request ID
132
+ ClientMessage :: Error ( jsonrpc_error) => Some ( & jsonrpc_error. id ) ,
133
+ }
134
+ }
135
+ }
136
+
97
137
// Implementing the `MCPMessage` trait for `ClientMessage`
98
138
impl MCPMessage for ClientMessage {
99
139
// Returns true if the message is a response type
@@ -116,17 +156,13 @@ impl MCPMessage for ClientMessage {
116
156
matches ! ( self , ClientMessage :: Error ( _) )
117
157
}
118
158
119
- // Retrieves the request ID associated with the message, if applicable
120
- fn request_id ( & self ) -> Option < & RequestId > {
159
+ /// Determines the type of the message and returns the corresponding `MessageTypes` variant.
160
+ fn message_type ( & self ) -> MessageTypes {
121
161
match self {
122
- // If the message is a request, return the associated request ID
123
- ClientMessage :: Request ( client_jsonrpc_request) => Some ( & client_jsonrpc_request. id ) ,
124
- // Notifications do not have request IDs
125
- ClientMessage :: Notification ( _) => None ,
126
- // If the message is a response, return the associated request ID
127
- ClientMessage :: Response ( client_jsonrpc_response) => Some ( & client_jsonrpc_response. id ) ,
128
- // If the message is an error, return the associated request ID
129
- ClientMessage :: Error ( jsonrpc_error) => Some ( & jsonrpc_error. id ) ,
162
+ ClientMessage :: Request ( _) => MessageTypes :: Request ,
163
+ ClientMessage :: Notification ( _) => MessageTypes :: Notification ,
164
+ ClientMessage :: Response ( _) => MessageTypes :: Response ,
165
+ ClientMessage :: Error ( _) => MessageTypes :: Error ,
130
166
}
131
167
}
132
168
}
@@ -464,6 +500,22 @@ pub enum ServerMessage {
464
500
Error ( JsonrpcError ) ,
465
501
}
466
502
503
+ impl RPCMessage for ServerMessage {
504
+ // Retrieves the request ID associated with the message, if applicable
505
+ fn request_id ( & self ) -> Option < & RequestId > {
506
+ match self {
507
+ // If the message is a request, return the associated request ID
508
+ ServerMessage :: Request ( client_jsonrpc_request) => Some ( & client_jsonrpc_request. id ) ,
509
+ // Notifications do not have request IDs
510
+ ServerMessage :: Notification ( _) => None ,
511
+ // If the message is a response, return the associated request ID
512
+ ServerMessage :: Response ( client_jsonrpc_response) => Some ( & client_jsonrpc_response. id ) ,
513
+ // If the message is an error, return the associated request ID
514
+ ServerMessage :: Error ( jsonrpc_error) => Some ( & jsonrpc_error. id ) ,
515
+ }
516
+ }
517
+ }
518
+
467
519
// Implementing the `MCPMessage` trait for `ServerMessage`
468
520
impl MCPMessage for ServerMessage {
469
521
// Returns true if the message is a response type
@@ -486,17 +538,13 @@ impl MCPMessage for ServerMessage {
486
538
matches ! ( self , ServerMessage :: Error ( _) )
487
539
}
488
540
489
- // Retrieves the request ID associated with the message, if applicable
490
- fn request_id ( & self ) -> Option < & RequestId > {
541
+ /// Determines the type of the message and returns the corresponding `MessageTypes` variant.
542
+ fn message_type ( & self ) -> MessageTypes {
491
543
match self {
492
- // If the message is a request, return the associated request ID
493
- ServerMessage :: Request ( client_jsonrpc_request) => Some ( & client_jsonrpc_request. id ) ,
494
- // Notifications do not have request IDs
495
- ServerMessage :: Notification ( _) => None ,
496
- // If the message is a response, return the associated request ID
497
- ServerMessage :: Response ( client_jsonrpc_response) => Some ( & client_jsonrpc_response. id ) ,
498
- // If the message is an error, return the associated request ID
499
- ServerMessage :: Error ( jsonrpc_error) => Some ( & jsonrpc_error. id ) ,
544
+ ServerMessage :: Request ( _) => MessageTypes :: Request ,
545
+ ServerMessage :: Notification ( _) => MessageTypes :: Notification ,
546
+ ServerMessage :: Response ( _) => MessageTypes :: Response ,
547
+ ServerMessage :: Error ( _) => MessageTypes :: Error ,
500
548
}
501
549
}
502
550
}
0 commit comments