@@ -60,6 +60,7 @@ fn detect_message_type(value: &serde_json::Value) -> MessageTypes {
6060/// This trait defines methods to classify and extract information from messages.
6161pub trait RPCMessage {
6262 fn request_id ( & self ) -> Option < & RequestId > ;
63+ fn jsonrpc ( & self ) -> & str ;
6364}
6465
6566pub trait MCPMessage : RPCMessage {
@@ -132,6 +133,15 @@ impl RPCMessage for ClientMessage {
132133 ClientMessage :: Error ( jsonrpc_error) => Some ( & jsonrpc_error. id ) ,
133134 }
134135 }
136+
137+ fn jsonrpc ( & self ) -> & str {
138+ match self {
139+ ClientMessage :: Request ( client_jsonrpc_request) => client_jsonrpc_request. jsonrpc ( ) ,
140+ ClientMessage :: Notification ( notification) => notification. jsonrpc ( ) ,
141+ ClientMessage :: Response ( client_jsonrpc_response) => client_jsonrpc_response. jsonrpc ( ) ,
142+ ClientMessage :: Error ( jsonrpc_error) => jsonrpc_error. jsonrpc ( ) ,
143+ }
144+ }
135145}
136146
137147// Implementing the `MCPMessage` trait for `ClientMessage`
@@ -175,7 +185,7 @@ impl MCPMessage for ClientMessage {
175185#[ derive( Clone , Debug ) ]
176186pub struct ClientJsonrpcRequest {
177187 pub id : RequestId ,
178- pub jsonrpc : :: std:: string:: String ,
188+ jsonrpc : :: std:: string:: String ,
179189 pub method : String ,
180190 pub request : RequestFromClient ,
181191}
@@ -190,6 +200,9 @@ impl ClientJsonrpcRequest {
190200 request,
191201 }
192202 }
203+ pub fn jsonrpc ( & self ) -> & :: std:: string:: String {
204+ & self . jsonrpc
205+ }
193206}
194207
195208/// Formats the ClientJsonrpcRequest as a JSON string.
@@ -298,7 +311,7 @@ impl<'de> serde::Deserialize<'de> for RequestFromClient {
298311/// "Similar to JsonrpcNotification , but with the variants restricted to client-side notifications."
299312#[ derive( Clone , Debug ) ]
300313pub struct ClientJsonrpcNotification {
301- pub jsonrpc : :: std:: string:: String ,
314+ jsonrpc : :: std:: string:: String ,
302315 pub method : :: std:: string:: String ,
303316 pub notification : NotificationFromClient ,
304317}
@@ -312,6 +325,9 @@ impl ClientJsonrpcNotification {
312325 notification,
313326 }
314327 }
328+ pub fn jsonrpc ( & self ) -> & :: std:: string:: String {
329+ & self . jsonrpc
330+ }
315331}
316332
317333/// Formats the ClientJsonrpcNotification as a JSON string.
@@ -388,7 +404,7 @@ impl<'de> serde::Deserialize<'de> for NotificationFromClient {
388404#[ derive( Clone , Debug ) ]
389405pub struct ClientJsonrpcResponse {
390406 pub id : RequestId ,
391- pub jsonrpc : :: std:: string:: String ,
407+ jsonrpc : :: std:: string:: String ,
392408 pub result : ResultFromClient ,
393409}
394410
@@ -400,6 +416,9 @@ impl ClientJsonrpcResponse {
400416 result,
401417 }
402418 }
419+ pub fn jsonrpc ( & self ) -> & :: std:: string:: String {
420+ & self . jsonrpc
421+ }
403422}
404423
405424/// Formats the ClientJsonrpcResponse as a JSON string.
@@ -514,6 +533,19 @@ impl RPCMessage for ServerMessage {
514533 ServerMessage :: Error ( jsonrpc_error) => Some ( & jsonrpc_error. id ) ,
515534 }
516535 }
536+
537+ fn jsonrpc ( & self ) -> & str {
538+ match self {
539+ // If the message is a request, return the associated request ID
540+ ServerMessage :: Request ( client_jsonrpc_request) => client_jsonrpc_request. jsonrpc ( ) ,
541+ // Notifications do not have request IDs
542+ ServerMessage :: Notification ( notification) => notification. jsonrpc ( ) ,
543+ // If the message is a response, return the associated request ID
544+ ServerMessage :: Response ( client_jsonrpc_response) => client_jsonrpc_response. jsonrpc ( ) ,
545+ // If the message is an error, return the associated request ID
546+ ServerMessage :: Error ( jsonrpc_error) => jsonrpc_error. jsonrpc ( ) ,
547+ }
548+ }
517549}
518550
519551// Implementing the `MCPMessage` trait for `ServerMessage`
@@ -576,7 +608,7 @@ impl Display for ServerMessage {
576608#[ derive( Clone , Debug ) ]
577609pub struct ServerJsonrpcRequest {
578610 pub id : RequestId ,
579- pub jsonrpc : :: std:: string:: String ,
611+ jsonrpc : :: std:: string:: String ,
580612 pub method : String ,
581613 pub request : RequestFromServer ,
582614}
@@ -591,6 +623,9 @@ impl ServerJsonrpcRequest {
591623 request,
592624 }
593625 }
626+ pub fn jsonrpc ( & self ) -> & :: std:: string:: String {
627+ & self . jsonrpc
628+ }
594629}
595630
596631/// Formats the ServerJsonrpcRequest as a JSON string.
@@ -677,7 +712,7 @@ impl<'de> serde::Deserialize<'de> for RequestFromServer {
677712/// "Similar to JsonrpcNotification , but with the variants restricted to server-side notifications."
678713#[ derive( Clone , Debug ) ]
679714pub struct ServerJsonrpcNotification {
680- pub jsonrpc : :: std:: string:: String ,
715+ jsonrpc : :: std:: string:: String ,
681716 pub method : :: std:: string:: String ,
682717 pub notification : NotificationFromServer ,
683718}
@@ -691,6 +726,9 @@ impl ServerJsonrpcNotification {
691726 notification,
692727 }
693728 }
729+ pub fn jsonrpc ( & self ) -> & :: std:: string:: String {
730+ & self . jsonrpc
731+ }
694732}
695733
696734/// Formats the ServerJsonrpcNotification as a JSON string.
@@ -766,7 +804,7 @@ impl<'de> serde::Deserialize<'de> for NotificationFromServer {
766804#[ derive( Clone , Debug ) ]
767805pub struct ServerJsonrpcResponse {
768806 pub id : RequestId ,
769- pub jsonrpc : :: std:: string:: String ,
807+ jsonrpc : :: std:: string:: String ,
770808 pub result : ResultFromServer ,
771809}
772810
@@ -778,6 +816,9 @@ impl ServerJsonrpcResponse {
778816 result,
779817 }
780818 }
819+ pub fn jsonrpc ( & self ) -> & :: std:: string:: String {
820+ & self . jsonrpc
821+ }
781822}
782823
783824/// Formats the ServerJsonrpcResponse as a JSON string.
0 commit comments