Skip to content

feat: implement new utility functions #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/generated_schema/2024_11_05/mcp_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
///
/// Generated from : <https://github.com/modelcontextprotocol/specification.git>
/// Hash : 63e1dbb75456b359b9ed8b27d21f4ac68cbb753e
/// Generated at : 2025-02-17 17:12:03
/// Generated at : 2025-02-17 17:23:32
/// ----------------------------------------------------------------------------
///
/// MCP Protocol Version
Expand Down
116 changes: 116 additions & 0 deletions src/generated_schema/2024_11_05/schema_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::generated_schema::*;
use serde::ser::SerializeStruct;
use serde_json::{json, Value};
use std::hash::{Hash, Hasher};
use std::{fmt::Display, str::FromStr};

#[derive(Debug)]
Expand Down Expand Up @@ -37,6 +38,47 @@ fn detect_message_type(value: &serde_json::Value) -> MessageTypes {
MessageTypes::Request
}

pub trait MCPMessage {
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>;
}

//*******************************//
//** RequestId Implementations **//
//*******************************//

// Implement PartialEq and Eq for RequestId
impl PartialEq for RequestId {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(RequestId::String(a), RequestId::String(b)) => a == b,
(RequestId::Integer(a), RequestId::Integer(b)) => a == b,
_ => false, // Different variants are never equal
}
}
}

impl Eq for RequestId {}

// Implement Hash for RequestId, so we can store it in HashMaps, HashSets, etc.
impl Hash for RequestId {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
RequestId::String(s) => {
0u8.hash(state); // Prefix with 0 for String variant
s.hash(state);
}
RequestId::Integer(i) => {
1u8.hash(state); // Prefix with 1 for Integer variant
i.hash(state);
}
}
}
}

//*******************//
//** ClientMessage **//
//*******************//
Expand All @@ -52,6 +94,43 @@ pub enum ClientMessage {
Error(JsonrpcError),
}

// Implementing the `MCPMessage` trait for `ClientMessage`
impl MCPMessage for ClientMessage {
// Returns true if the message is a response type
fn is_response(&self) -> bool {
matches!(self, ClientMessage::Response(_))
}

// Returns true if the message is a request type
fn is_request(&self) -> bool {
matches!(self, ClientMessage::Request(_))
}

// Returns true if the message is a notification type (i.e., does not expect a response)
fn is_notification(&self) -> bool {
matches!(self, ClientMessage::Notification(_))
}

// Returns true if the message represents an error
fn is_error(&self) -> bool {
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),
}
}
}

//**************************//
//** ClientJsonrpcRequest **//
//**************************//
Expand Down Expand Up @@ -385,6 +464,43 @@ pub enum ServerMessage {
Error(JsonrpcError),
}

// Implementing the `MCPMessage` trait for `ServerMessage`
impl MCPMessage for ServerMessage {
// Returns true if the message is a response type
fn is_response(&self) -> bool {
matches!(self, ServerMessage::Response(_))
}

// Returns true if the message is a request type
fn is_request(&self) -> bool {
matches!(self, ServerMessage::Request(_))
}

// Returns true if the message is a notification type (i.e., does not expect a response)
fn is_notification(&self) -> bool {
matches!(self, ServerMessage::Notification(_))
}

// Returns true if the message represents an error
fn is_error(&self) -> bool {
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),
}
}
}

impl FromStr for ServerMessage {
type Err = JsonrpcErrorError;

Expand Down
2 changes: 1 addition & 1 deletion src/generated_schema/draft/mcp_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
///
/// Generated from : <https://github.com/modelcontextprotocol/specification.git>
/// Hash : 63e1dbb75456b359b9ed8b27d21f4ac68cbb753e
/// Generated at : 2025-02-17 17:12:04
/// Generated at : 2025-02-17 17:23:32
/// ----------------------------------------------------------------------------
///
/// MCP Protocol Version
Expand Down
116 changes: 116 additions & 0 deletions src/generated_schema/draft/schema_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::generated_schema::*;
use serde::ser::SerializeStruct;
use serde_json::{json, Value};
use std::hash::{Hash, Hasher};
use std::{fmt::Display, str::FromStr};

#[derive(Debug)]
Expand Down Expand Up @@ -37,6 +38,47 @@ fn detect_message_type(value: &serde_json::Value) -> MessageTypes {
MessageTypes::Request
}

pub trait MCPMessage {
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>;
}

//*******************************//
//** RequestId Implementations **//
//*******************************//

// Implement PartialEq and Eq for RequestId
impl PartialEq for RequestId {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(RequestId::String(a), RequestId::String(b)) => a == b,
(RequestId::Integer(a), RequestId::Integer(b)) => a == b,
_ => false, // Different variants are never equal
}
}
}

impl Eq for RequestId {}

// Implement Hash for RequestId, so we can store it in HashMaps, HashSets, etc.
impl Hash for RequestId {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
RequestId::String(s) => {
0u8.hash(state); // Prefix with 0 for String variant
s.hash(state);
}
RequestId::Integer(i) => {
1u8.hash(state); // Prefix with 1 for Integer variant
i.hash(state);
}
}
}
}

//*******************//
//** ClientMessage **//
//*******************//
Expand All @@ -52,6 +94,43 @@ pub enum ClientMessage {
Error(JsonrpcError),
}

// Implementing the `MCPMessage` trait for `ClientMessage`
impl MCPMessage for ClientMessage {
// Returns true if the message is a response type
fn is_response(&self) -> bool {
matches!(self, ClientMessage::Response(_))
}

// Returns true if the message is a request type
fn is_request(&self) -> bool {
matches!(self, ClientMessage::Request(_))
}

// Returns true if the message is a notification type (i.e., does not expect a response)
fn is_notification(&self) -> bool {
matches!(self, ClientMessage::Notification(_))
}

// Returns true if the message represents an error
fn is_error(&self) -> bool {
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),
}
}
}

//**************************//
//** ClientJsonrpcRequest **//
//**************************//
Expand Down Expand Up @@ -385,6 +464,43 @@ pub enum ServerMessage {
Error(JsonrpcError),
}

// Implementing the `MCPMessage` trait for `ServerMessage`
impl MCPMessage for ServerMessage {
// Returns true if the message is a response type
fn is_response(&self) -> bool {
matches!(self, ServerMessage::Response(_))
}

// Returns true if the message is a request type
fn is_request(&self) -> bool {
matches!(self, ServerMessage::Request(_))
}

// Returns true if the message is a notification type (i.e., does not expect a response)
fn is_notification(&self) -> bool {
matches!(self, ServerMessage::Notification(_))
}

// Returns true if the message represents an error
fn is_error(&self) -> bool {
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),
}
}
}

impl FromStr for ServerMessage {
type Err = JsonrpcErrorError;

Expand Down
Loading