Skip to content

Commit 3822538

Browse files
authored
add jsonrpc method to RPCMessage trait (#28)
1 parent aca2336 commit 3822538

File tree

6 files changed

+146
-48
lines changed

6 files changed

+146
-48
lines changed

scripts/run_test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ COMMON_FEATURES_STR="${COMMON_FEATURES[*]}"
1212
for FEATURE in "${SCHEMA_VERSION_FEATURES[@]}"; do
1313
echo "🚀 Running tests with: --features \"$COMMON_FEATURES_STR $FEATURE\""
1414
cargo nextest run --no-default-features --features "$COMMON_FEATURES_STR $FEATURE"
15+
16+
# stop on failure
17+
if [ $? -ne 0 ]; then
18+
echo "❌ Tests failed for: --features \"$COMMON_FEATURES_STR $FEATURE\""
19+
exit 1
20+
fi
21+
1522
echo
1623
echo "🚀 Running documentation tests with: --features \"$COMMON_FEATURES_STR $FEATURE\""
1724
cargo test --doc --no-default-features --features "$COMMON_FEATURES_STR $FEATURE"

src/generated_schema/2024_11_05/mcp_schema.rs

Lines changed: 2 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 : 63e1dbb75456b359b9ed8b27d21f4ac68cbb753e
9-
/// Generated at : 2025-02-18 08:30:28
8+
/// Hash : bb1446ff1810a0df57989d78366d626d2c01b9d7
9+
/// Generated at : 2025-02-19 06:30:55
1010
/// ----------------------------------------------------------------------------
1111
///
1212
/// MCP Protocol Version

src/generated_schema/2024_11_05/schema_utils.rs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
6161
pub trait RPCMessage {
6262
fn request_id(&self) -> Option<&RequestId>;
63+
fn jsonrpc(&self) -> &str;
6364
}
6465

6566
pub 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)]
176186
pub 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)]
300313
pub 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)]
389405
pub 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)]
577609
pub 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)]
679714
pub 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)]
767805
pub 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.

src/generated_schema/draft/mcp_schema.rs

Lines changed: 2 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 : 63e1dbb75456b359b9ed8b27d21f4ac68cbb753e
9-
/// Generated at : 2025-02-18 08:30:28
8+
/// Hash : bb1446ff1810a0df57989d78366d626d2c01b9d7
9+
/// Generated at : 2025-02-19 06:30:55
1010
/// ----------------------------------------------------------------------------
1111
///
1212
/// MCP Protocol Version

0 commit comments

Comments
 (0)