Skip to content

Commit 2dbd271

Browse files
authored
feat: enhance schema_utils and mcp_schema (#12)
* feat: implement default trait for Result * implement Display trait for ClientMessage and ServerMessage variants
1 parent c4b66bb commit 2dbd271

File tree

5 files changed

+179
-5
lines changed

5 files changed

+179
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ fn handle_message(message_payload: &str) -> std::result::Result<(), AppError> {
288288
// Check it's a Request type of message
289289
if let JsonrpcMessage::Request(client_message) = message {
290290

291-
// Check method to detect it it a "initialize" request
291+
// Check method to detect is a "initialize" request
292292
if client_message.method == "initialize" {
293293

294294
// Now that we can handle the message, we simply print out the details.

src/generated_schema/2024_11_05/mcp_schema.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/// modify or extend the implementations as needed, but please do so at your own risk.
66
///
77
/// Generated from : <https://github.com/modelcontextprotocol/specification.git>
8-
/// Hash : 3d4877e69cbc9921e1b511a90cdf17d42483036b
9-
/// Generated at : 2025-02-09 20:40:09
8+
/// Hash : 55c983fd85fafa458d31f729e433c60e95932178
9+
/// Generated at : 2025-02-11 20:13:30
1010
/// ----------------------------------------------------------------------------
1111
///
1212
/// MCP Protocol Version
@@ -4542,6 +4542,14 @@ pub struct Result {
45424542
#[serde(flatten, default, skip_serializing_if = "::std::option::Option::is_none")]
45434543
pub extra: ::std::option::Option<::serde_json::Map<::std::string::String, ::serde_json::Value>>,
45444544
}
4545+
impl ::std::default::Default for Result {
4546+
fn default() -> Self {
4547+
Self {
4548+
meta: Default::default(),
4549+
extra: Default::default(),
4550+
}
4551+
}
4552+
}
45454553
///The sender or recipient of messages and data in a conversation.
45464554
///
45474555
/// <details><summary>JSON schema</summary>

src/generated_schema/2024_11_05/schema_utils.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ impl ClientJsonrpcRequest {
7777
}
7878
}
7979

80+
/// Formats the ClientJsonrpcRequest as a JSON string.
81+
impl Display for ClientJsonrpcRequest {
82+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83+
write!(
84+
f,
85+
"{}",
86+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
87+
)
88+
}
89+
}
90+
8091
//*************************//
8192
//** Request From Client **//
8293
//*************************//
@@ -150,6 +161,17 @@ impl ClientJsonrpcNotification {
150161
}
151162
}
152163

164+
/// Formats the ClientJsonrpcNotification as a JSON string.
165+
impl Display for ClientJsonrpcNotification {
166+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
167+
write!(
168+
f,
169+
"{}",
170+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
171+
)
172+
}
173+
}
174+
153175
//*******************************//
154176
//** NotificationFromClient **//
155177
//*******************************//
@@ -210,6 +232,17 @@ impl ClientJsonrpcResponse {
210232
}
211233
}
212234

235+
/// Formats the ClientJsonrpcResponse as a JSON string.
236+
impl Display for ClientJsonrpcResponse {
237+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
238+
write!(
239+
f,
240+
"{}",
241+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
242+
)
243+
}
244+
}
245+
213246
//*******************************//
214247
//** ResultFromClient **//
215248
//*******************************//
@@ -331,6 +364,16 @@ impl ServerJsonrpcRequest {
331364
}
332365
}
333366

367+
/// Formats the ServerJsonrpcRequest as a JSON string.
368+
impl Display for ServerJsonrpcRequest {
369+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
370+
write!(
371+
f,
372+
"{}",
373+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
374+
)
375+
}
376+
}
334377
//*************************//
335378
//** Request From Server **//
336379
//*************************//
@@ -404,6 +447,16 @@ impl ServerJsonrpcNotification {
404447
}
405448
}
406449

450+
/// Formats the ServerJsonrpcNotification as a JSON string.
451+
impl Display for ServerJsonrpcNotification {
452+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
453+
write!(
454+
f,
455+
"{}",
456+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
457+
)
458+
}
459+
}
407460
//*******************************//
408461
//** NotificationFromServer **//
409462
//*******************************//
@@ -464,6 +517,17 @@ impl ServerJsonrpcResponse {
464517
}
465518
}
466519

520+
/// Formats the ServerJsonrpcResponse as a JSON string.
521+
impl Display for ServerJsonrpcResponse {
522+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
523+
write!(
524+
f,
525+
"{}",
526+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
527+
)
528+
}
529+
}
530+
467531
//*******************************//
468532
//** ResultFromServer **//
469533
//*******************************//
@@ -511,6 +575,21 @@ impl<'de> serde::Deserialize<'de> for ResultFromServer {
511575
}
512576
}
513577

578+
//***************************//
579+
//** impl for JsonrpcError **//
580+
//***************************//
581+
582+
/// Formats the ServerJsonrpcResponse as a JSON string.
583+
impl Display for JsonrpcError {
584+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
585+
write!(
586+
f,
587+
"{}",
588+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
589+
)
590+
}
591+
}
592+
514593
/// BEGIN AUTO GENERATED
515594
impl ::serde::Serialize for ClientJsonrpcRequest {
516595
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>

src/generated_schema/draft/mcp_schema.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/// modify or extend the implementations as needed, but please do so at your own risk.
66
///
77
/// Generated from : <https://github.com/modelcontextprotocol/specification.git>
8-
/// Hash : 3d4877e69cbc9921e1b511a90cdf17d42483036b
9-
/// Generated at : 2025-02-09 20:40:10
8+
/// Hash : 55c983fd85fafa458d31f729e433c60e95932178
9+
/// Generated at : 2025-02-11 20:13:30
1010
/// ----------------------------------------------------------------------------
1111
///
1212
/// MCP Protocol Version
@@ -4673,6 +4673,14 @@ pub struct Result {
46734673
#[serde(flatten, default, skip_serializing_if = "::std::option::Option::is_none")]
46744674
pub extra: ::std::option::Option<::serde_json::Map<::std::string::String, ::serde_json::Value>>,
46754675
}
4676+
impl ::std::default::Default for Result {
4677+
fn default() -> Self {
4678+
Self {
4679+
meta: Default::default(),
4680+
extra: Default::default(),
4681+
}
4682+
}
4683+
}
46764684
///The sender or recipient of messages and data in a conversation.
46774685
///
46784686
/// <details><summary>JSON schema</summary>

src/generated_schema/draft/schema_utils.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ impl ClientJsonrpcRequest {
7777
}
7878
}
7979

80+
/// Formats the ClientJsonrpcRequest as a JSON string.
81+
impl Display for ClientJsonrpcRequest {
82+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83+
write!(
84+
f,
85+
"{}",
86+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
87+
)
88+
}
89+
}
90+
8091
//*************************//
8192
//** Request From Client **//
8293
//*************************//
@@ -150,6 +161,17 @@ impl ClientJsonrpcNotification {
150161
}
151162
}
152163

164+
/// Formats the ClientJsonrpcNotification as a JSON string.
165+
impl Display for ClientJsonrpcNotification {
166+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
167+
write!(
168+
f,
169+
"{}",
170+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
171+
)
172+
}
173+
}
174+
153175
//*******************************//
154176
//** NotificationFromClient **//
155177
//*******************************//
@@ -210,6 +232,17 @@ impl ClientJsonrpcResponse {
210232
}
211233
}
212234

235+
/// Formats the ClientJsonrpcResponse as a JSON string.
236+
impl Display for ClientJsonrpcResponse {
237+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
238+
write!(
239+
f,
240+
"{}",
241+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
242+
)
243+
}
244+
}
245+
213246
//*******************************//
214247
//** ResultFromClient **//
215248
//*******************************//
@@ -331,6 +364,16 @@ impl ServerJsonrpcRequest {
331364
}
332365
}
333366

367+
/// Formats the ServerJsonrpcRequest as a JSON string.
368+
impl Display for ServerJsonrpcRequest {
369+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
370+
write!(
371+
f,
372+
"{}",
373+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
374+
)
375+
}
376+
}
334377
//*************************//
335378
//** Request From Server **//
336379
//*************************//
@@ -404,6 +447,16 @@ impl ServerJsonrpcNotification {
404447
}
405448
}
406449

450+
/// Formats the ServerJsonrpcNotification as a JSON string.
451+
impl Display for ServerJsonrpcNotification {
452+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
453+
write!(
454+
f,
455+
"{}",
456+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
457+
)
458+
}
459+
}
407460
//*******************************//
408461
//** NotificationFromServer **//
409462
//*******************************//
@@ -464,6 +517,17 @@ impl ServerJsonrpcResponse {
464517
}
465518
}
466519

520+
/// Formats the ServerJsonrpcResponse as a JSON string.
521+
impl Display for ServerJsonrpcResponse {
522+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
523+
write!(
524+
f,
525+
"{}",
526+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
527+
)
528+
}
529+
}
530+
467531
//*******************************//
468532
//** ResultFromServer **//
469533
//*******************************//
@@ -511,6 +575,21 @@ impl<'de> serde::Deserialize<'de> for ResultFromServer {
511575
}
512576
}
513577

578+
//***************************//
579+
//** impl for JsonrpcError **//
580+
//***************************//
581+
582+
/// Formats the ServerJsonrpcResponse as a JSON string.
583+
impl Display for JsonrpcError {
584+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
585+
write!(
586+
f,
587+
"{}",
588+
serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
589+
)
590+
}
591+
}
592+
514593
/// BEGIN AUTO GENERATED
515594
impl ::serde::Serialize for ClientJsonrpcRequest {
516595
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>

0 commit comments

Comments
 (0)