diff --git a/scripts/run_test.sh b/scripts/run_test.sh index b0fefb9..8e3f953 100755 --- a/scripts/run_test.sh +++ b/scripts/run_test.sh @@ -12,6 +12,13 @@ COMMON_FEATURES_STR="${COMMON_FEATURES[*]}" for FEATURE in "${SCHEMA_VERSION_FEATURES[@]}"; do echo "🚀 Running tests with: --features \"$COMMON_FEATURES_STR $FEATURE\"" cargo nextest run --no-default-features --features "$COMMON_FEATURES_STR $FEATURE" + + # stop on failure + if [ $? -ne 0 ]; then + echo "❌ Tests failed for: --features \"$COMMON_FEATURES_STR $FEATURE\"" + exit 1 + fi + echo echo "🚀 Running documentation tests with: --features \"$COMMON_FEATURES_STR $FEATURE\"" cargo test --doc --no-default-features --features "$COMMON_FEATURES_STR $FEATURE" diff --git a/src/generated_schema/2024_11_05/mcp_schema.rs b/src/generated_schema/2024_11_05/mcp_schema.rs index 2f034f4..3aa9051 100644 --- a/src/generated_schema/2024_11_05/mcp_schema.rs +++ b/src/generated_schema/2024_11_05/mcp_schema.rs @@ -5,8 +5,8 @@ /// modify or extend the implementations as needed, but please do so at your own risk. /// /// Generated from : -/// Hash : 63e1dbb75456b359b9ed8b27d21f4ac68cbb753e -/// Generated at : 2025-02-18 08:30:28 +/// Hash : bb1446ff1810a0df57989d78366d626d2c01b9d7 +/// Generated at : 2025-02-19 06:30:55 /// ---------------------------------------------------------------------------- /// /// MCP Protocol Version diff --git a/src/generated_schema/2024_11_05/schema_utils.rs b/src/generated_schema/2024_11_05/schema_utils.rs index 1c09656..e455b27 100644 --- a/src/generated_schema/2024_11_05/schema_utils.rs +++ b/src/generated_schema/2024_11_05/schema_utils.rs @@ -60,6 +60,7 @@ fn detect_message_type(value: &serde_json::Value) -> MessageTypes { /// This trait defines methods to classify and extract information from messages. pub trait RPCMessage { fn request_id(&self) -> Option<&RequestId>; + fn jsonrpc(&self) -> &str; } pub trait MCPMessage: RPCMessage { @@ -132,6 +133,15 @@ impl RPCMessage for ClientMessage { ClientMessage::Error(jsonrpc_error) => Some(&jsonrpc_error.id), } } + + fn jsonrpc(&self) -> &str { + match self { + ClientMessage::Request(client_jsonrpc_request) => client_jsonrpc_request.jsonrpc(), + ClientMessage::Notification(notification) => notification.jsonrpc(), + ClientMessage::Response(client_jsonrpc_response) => client_jsonrpc_response.jsonrpc(), + ClientMessage::Error(jsonrpc_error) => jsonrpc_error.jsonrpc(), + } + } } // Implementing the `MCPMessage` trait for `ClientMessage` @@ -175,7 +185,7 @@ impl MCPMessage for ClientMessage { #[derive(Clone, Debug)] pub struct ClientJsonrpcRequest { pub id: RequestId, - pub jsonrpc: ::std::string::String, + jsonrpc: ::std::string::String, pub method: String, pub request: RequestFromClient, } @@ -190,6 +200,9 @@ impl ClientJsonrpcRequest { request, } } + pub fn jsonrpc(&self) -> &::std::string::String { + &self.jsonrpc + } } /// Formats the ClientJsonrpcRequest as a JSON string. @@ -298,7 +311,7 @@ impl<'de> serde::Deserialize<'de> for RequestFromClient { /// "Similar to JsonrpcNotification , but with the variants restricted to client-side notifications." #[derive(Clone, Debug)] pub struct ClientJsonrpcNotification { - pub jsonrpc: ::std::string::String, + jsonrpc: ::std::string::String, pub method: ::std::string::String, pub notification: NotificationFromClient, } @@ -312,6 +325,9 @@ impl ClientJsonrpcNotification { notification, } } + pub fn jsonrpc(&self) -> &::std::string::String { + &self.jsonrpc + } } /// Formats the ClientJsonrpcNotification as a JSON string. @@ -388,7 +404,7 @@ impl<'de> serde::Deserialize<'de> for NotificationFromClient { #[derive(Clone, Debug)] pub struct ClientJsonrpcResponse { pub id: RequestId, - pub jsonrpc: ::std::string::String, + jsonrpc: ::std::string::String, pub result: ResultFromClient, } @@ -400,6 +416,9 @@ impl ClientJsonrpcResponse { result, } } + pub fn jsonrpc(&self) -> &::std::string::String { + &self.jsonrpc + } } /// Formats the ClientJsonrpcResponse as a JSON string. @@ -514,6 +533,19 @@ impl RPCMessage for ServerMessage { ServerMessage::Error(jsonrpc_error) => Some(&jsonrpc_error.id), } } + + fn jsonrpc(&self) -> &str { + match self { + // If the message is a request, return the associated request ID + ServerMessage::Request(client_jsonrpc_request) => client_jsonrpc_request.jsonrpc(), + // Notifications do not have request IDs + ServerMessage::Notification(notification) => notification.jsonrpc(), + // If the message is a response, return the associated request ID + ServerMessage::Response(client_jsonrpc_response) => client_jsonrpc_response.jsonrpc(), + // If the message is an error, return the associated request ID + ServerMessage::Error(jsonrpc_error) => jsonrpc_error.jsonrpc(), + } + } } // Implementing the `MCPMessage` trait for `ServerMessage` @@ -576,7 +608,7 @@ impl Display for ServerMessage { #[derive(Clone, Debug)] pub struct ServerJsonrpcRequest { pub id: RequestId, - pub jsonrpc: ::std::string::String, + jsonrpc: ::std::string::String, pub method: String, pub request: RequestFromServer, } @@ -591,6 +623,9 @@ impl ServerJsonrpcRequest { request, } } + pub fn jsonrpc(&self) -> &::std::string::String { + &self.jsonrpc + } } /// Formats the ServerJsonrpcRequest as a JSON string. @@ -677,7 +712,7 @@ impl<'de> serde::Deserialize<'de> for RequestFromServer { /// "Similar to JsonrpcNotification , but with the variants restricted to server-side notifications." #[derive(Clone, Debug)] pub struct ServerJsonrpcNotification { - pub jsonrpc: ::std::string::String, + jsonrpc: ::std::string::String, pub method: ::std::string::String, pub notification: NotificationFromServer, } @@ -691,6 +726,9 @@ impl ServerJsonrpcNotification { notification, } } + pub fn jsonrpc(&self) -> &::std::string::String { + &self.jsonrpc + } } /// Formats the ServerJsonrpcNotification as a JSON string. @@ -766,7 +804,7 @@ impl<'de> serde::Deserialize<'de> for NotificationFromServer { #[derive(Clone, Debug)] pub struct ServerJsonrpcResponse { pub id: RequestId, - pub jsonrpc: ::std::string::String, + jsonrpc: ::std::string::String, pub result: ResultFromServer, } @@ -778,6 +816,9 @@ impl ServerJsonrpcResponse { result, } } + pub fn jsonrpc(&self) -> &::std::string::String { + &self.jsonrpc + } } /// Formats the ServerJsonrpcResponse as a JSON string. diff --git a/src/generated_schema/draft/mcp_schema.rs b/src/generated_schema/draft/mcp_schema.rs index 8657970..db309ec 100644 --- a/src/generated_schema/draft/mcp_schema.rs +++ b/src/generated_schema/draft/mcp_schema.rs @@ -5,8 +5,8 @@ /// modify or extend the implementations as needed, but please do so at your own risk. /// /// Generated from : -/// Hash : 63e1dbb75456b359b9ed8b27d21f4ac68cbb753e -/// Generated at : 2025-02-18 08:30:28 +/// Hash : bb1446ff1810a0df57989d78366d626d2c01b9d7 +/// Generated at : 2025-02-19 06:30:55 /// ---------------------------------------------------------------------------- /// /// MCP Protocol Version diff --git a/src/generated_schema/draft/schema_utils.rs b/src/generated_schema/draft/schema_utils.rs index 07789ab..85ae235 100644 --- a/src/generated_schema/draft/schema_utils.rs +++ b/src/generated_schema/draft/schema_utils.rs @@ -56,13 +56,19 @@ fn detect_message_type(value: &serde_json::Value) -> MessageTypes { MessageTypes::Request } -pub trait MCPMessage { +/// Represents a generic MCP (Model Content Protocol) message. +/// This trait defines methods to classify and extract information from messages. +pub trait RPCMessage { + fn request_id(&self) -> Option<&RequestId>; + fn jsonrpc_version(&self) -> &str; +} + +pub trait MCPMessage: RPCMessage { + fn message_type(&self) -> MessageTypes; fn is_response(&self) -> bool; fn is_request(&self) -> bool; fn is_notification(&self) -> bool; fn is_error(&self) -> bool; - fn request_id(&self) -> Option<&RequestId>; - fn message_type(&self) -> MessageTypes; } //*******************************// @@ -113,6 +119,31 @@ pub enum ClientMessage { Error(JsonrpcError), } +impl RPCMessage for ClientMessage { + // Retrieves the request ID associated with the message, if applicable + fn request_id(&self) -> Option<&RequestId> { + match self { + // If the message is a request, return the associated request ID + ClientMessage::Request(client_jsonrpc_request) => Some(&client_jsonrpc_request.id), + // Notifications do not have request IDs + ClientMessage::Notification(_) => None, + // If the message is a response, return the associated request ID + ClientMessage::Response(client_jsonrpc_response) => Some(&client_jsonrpc_response.id), + // If the message is an error, return the associated request ID + ClientMessage::Error(jsonrpc_error) => Some(&jsonrpc_error.id), + } + } + + fn jsonrpc_version(&self) -> &str { + match self { + ClientMessage::Request(client_jsonrpc_request) => client_jsonrpc_request.jsonrpc.as_str(), + ClientMessage::Notification(notification) => notification.jsonrpc.as_str(), + ClientMessage::Response(client_jsonrpc_response) => client_jsonrpc_response.jsonrpc.as_str(), + ClientMessage::Error(jsonrpc_error) => jsonrpc_error.jsonrpc(), + } + } +} + // Implementing the `MCPMessage` trait for `ClientMessage` impl MCPMessage for ClientMessage { // Returns true if the message is a response type @@ -135,20 +166,6 @@ impl MCPMessage for ClientMessage { matches!(self, ClientMessage::Error(_)) } - // Retrieves the request ID associated with the message, if applicable - fn request_id(&self) -> Option<&RequestId> { - match self { - // If the message is a request, return the associated request ID - ClientMessage::Request(client_jsonrpc_request) => Some(&client_jsonrpc_request.id), - // Notifications do not have request IDs - ClientMessage::Notification(_) => None, - // If the message is a response, return the associated request ID - ClientMessage::Response(client_jsonrpc_response) => Some(&client_jsonrpc_response.id), - // If the message is an error, return the associated request ID - ClientMessage::Error(jsonrpc_error) => Some(&jsonrpc_error.id), - } - } - /// Determines the type of the message and returns the corresponding `MessageTypes` variant. fn message_type(&self) -> MessageTypes { match self { @@ -168,7 +185,7 @@ impl MCPMessage for ClientMessage { #[derive(Clone, Debug)] pub struct ClientJsonrpcRequest { pub id: RequestId, - pub jsonrpc: ::std::string::String, + jsonrpc: ::std::string::String, pub method: String, pub request: RequestFromClient, } @@ -183,6 +200,9 @@ impl ClientJsonrpcRequest { request, } } + pub fn jsonrpc(&self) -> &::std::string::String { + &self.jsonrpc + } } /// Formats the ClientJsonrpcRequest as a JSON string. @@ -291,7 +311,7 @@ impl<'de> serde::Deserialize<'de> for RequestFromClient { /// "Similar to JsonrpcNotification , but with the variants restricted to client-side notifications." #[derive(Clone, Debug)] pub struct ClientJsonrpcNotification { - pub jsonrpc: ::std::string::String, + jsonrpc: ::std::string::String, pub method: ::std::string::String, pub notification: NotificationFromClient, } @@ -305,6 +325,9 @@ impl ClientJsonrpcNotification { notification, } } + pub fn jsonrpc(&self) -> &::std::string::String { + &self.jsonrpc + } } /// Formats the ClientJsonrpcNotification as a JSON string. @@ -381,7 +404,7 @@ impl<'de> serde::Deserialize<'de> for NotificationFromClient { #[derive(Clone, Debug)] pub struct ClientJsonrpcResponse { pub id: RequestId, - pub jsonrpc: ::std::string::String, + jsonrpc: ::std::string::String, pub result: ResultFromClient, } @@ -393,6 +416,9 @@ impl ClientJsonrpcResponse { result, } } + pub fn jsonrpc(&self) -> &::std::string::String { + &self.jsonrpc + } } /// Formats the ClientJsonrpcResponse as a JSON string. @@ -493,6 +519,35 @@ pub enum ServerMessage { Error(JsonrpcError), } +impl RPCMessage for ServerMessage { + // Retrieves the request ID associated with the message, if applicable + fn request_id(&self) -> Option<&RequestId> { + match self { + // If the message is a request, return the associated request ID + ServerMessage::Request(client_jsonrpc_request) => Some(&client_jsonrpc_request.id), + // Notifications do not have request IDs + ServerMessage::Notification(_) => None, + // If the message is a response, return the associated request ID + ServerMessage::Response(client_jsonrpc_response) => Some(&client_jsonrpc_response.id), + // If the message is an error, return the associated request ID + ServerMessage::Error(jsonrpc_error) => Some(&jsonrpc_error.id), + } + } + + fn jsonrpc_version(&self) -> &str { + match self { + // If the message is a request, return the associated request ID + ServerMessage::Request(client_jsonrpc_request) => client_jsonrpc_request.jsonrpc.as_str(), + // Notifications do not have request IDs + ServerMessage::Notification(notification) => notification.jsonrpc.as_str(), + // If the message is a response, return the associated request ID + ServerMessage::Response(client_jsonrpc_response) => client_jsonrpc_response.jsonrpc.as_str(), + // If the message is an error, return the associated request ID + ServerMessage::Error(jsonrpc_error) => jsonrpc_error.jsonrpc(), + } + } +} + // Implementing the `MCPMessage` trait for `ServerMessage` impl MCPMessage for ServerMessage { // Returns true if the message is a response type @@ -515,20 +570,6 @@ impl MCPMessage for ServerMessage { matches!(self, ServerMessage::Error(_)) } - // Retrieves the request ID associated with the message, if applicable - fn request_id(&self) -> Option<&RequestId> { - match self { - // If the message is a request, return the associated request ID - ServerMessage::Request(client_jsonrpc_request) => Some(&client_jsonrpc_request.id), - // Notifications do not have request IDs - ServerMessage::Notification(_) => None, - // If the message is a response, return the associated request ID - ServerMessage::Response(client_jsonrpc_response) => Some(&client_jsonrpc_response.id), - // If the message is an error, return the associated request ID - ServerMessage::Error(jsonrpc_error) => Some(&jsonrpc_error.id), - } - } - /// Determines the type of the message and returns the corresponding `MessageTypes` variant. fn message_type(&self) -> MessageTypes { match self { @@ -567,7 +608,7 @@ impl Display for ServerMessage { #[derive(Clone, Debug)] pub struct ServerJsonrpcRequest { pub id: RequestId, - pub jsonrpc: ::std::string::String, + jsonrpc: ::std::string::String, pub method: String, pub request: RequestFromServer, } @@ -582,6 +623,9 @@ impl ServerJsonrpcRequest { request, } } + pub fn jsonrpc(&self) -> &::std::string::String { + &self.jsonrpc + } } /// Formats the ServerJsonrpcRequest as a JSON string. @@ -668,7 +712,7 @@ impl<'de> serde::Deserialize<'de> for RequestFromServer { /// "Similar to JsonrpcNotification , but with the variants restricted to server-side notifications." #[derive(Clone, Debug)] pub struct ServerJsonrpcNotification { - pub jsonrpc: ::std::string::String, + jsonrpc: ::std::string::String, pub method: ::std::string::String, pub notification: NotificationFromServer, } @@ -682,6 +726,9 @@ impl ServerJsonrpcNotification { notification, } } + pub fn jsonrpc(&self) -> &::std::string::String { + &self.jsonrpc + } } /// Formats the ServerJsonrpcNotification as a JSON string. @@ -757,7 +804,7 @@ impl<'de> serde::Deserialize<'de> for NotificationFromServer { #[derive(Clone, Debug)] pub struct ServerJsonrpcResponse { pub id: RequestId, - pub jsonrpc: ::std::string::String, + jsonrpc: ::std::string::String, pub result: ResultFromServer, } @@ -769,6 +816,9 @@ impl ServerJsonrpcResponse { result, } } + pub fn jsonrpc(&self) -> &::std::string::String { + &self.jsonrpc + } } /// Formats the ServerJsonrpcResponse as a JSON string. diff --git a/tests/test_deserialize.rs b/tests/test_deserialize.rs index 520afe4..5008840 100644 --- a/tests/test_deserialize.rs +++ b/tests/test_deserialize.rs @@ -18,7 +18,7 @@ mod test_deserialize { if let ClientMessage::Request(client_message) = message { matches!(&client_message.id, &RequestId::Integer(0)); - assert_eq!(client_message.jsonrpc, JSONRPC_VERSION); + assert_eq!(client_message.jsonrpc(), JSONRPC_VERSION); assert_eq!(client_message.method, "initialize"); if let RequestFromClient::ClientRequest(ClientRequest::InitializeRequest(request)) = client_message.request {