@@ -4,13 +4,31 @@ use serde_json::{json, Value};
44use std:: hash:: { Hash , Hasher } ;
55use std:: { fmt:: Display , str:: FromStr } ;
66
7- #[ derive( Debug ) ]
7+ #[ derive( Debug , PartialEq ) ]
88pub enum MessageTypes {
99 Request ,
1010 Response ,
1111 Notification ,
1212 Error ,
1313}
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+ }
1432
1533/// A utility function used internally to detect the message type from the payload.
1634/// 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 {
3856 MessageTypes :: Request
3957}
4058
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 ;
4267 fn is_response ( & self ) -> bool ;
4368 fn is_request ( & self ) -> bool ;
4469 fn is_notification ( & self ) -> bool ;
4570 fn is_error ( & self ) -> bool ;
46- fn request_id ( & self ) -> Option < & RequestId > ;
4771}
4872
4973//*******************************//
@@ -94,6 +118,22 @@ pub enum ClientMessage {
94118 Error ( JsonrpcError ) ,
95119}
96120
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+
97137// Implementing the `MCPMessage` trait for `ClientMessage`
98138impl MCPMessage for ClientMessage {
99139 // Returns true if the message is a response type
@@ -116,17 +156,13 @@ impl MCPMessage for ClientMessage {
116156 matches ! ( self , ClientMessage :: Error ( _) )
117157 }
118158
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 {
121161 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 ,
130166 }
131167 }
132168}
@@ -464,6 +500,22 @@ pub enum ServerMessage {
464500 Error ( JsonrpcError ) ,
465501}
466502
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+
467519// Implementing the `MCPMessage` trait for `ServerMessage`
468520impl MCPMessage for ServerMessage {
469521 // Returns true if the message is a response type
@@ -486,17 +538,13 @@ impl MCPMessage for ServerMessage {
486538 matches ! ( self , ServerMessage :: Error ( _) )
487539 }
488540
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 {
491543 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 ,
500548 }
501549 }
502550}
0 commit comments