1use crate::generated_schema::*;
2use serde::ser::SerializeStruct;
3use serde_json::{json, Value};
4use std::hash::{Hash, Hasher};
5use std::result;
6use std::{fmt::Display, str::FromStr};
7
8#[derive(Debug, PartialEq)]
9pub enum MessageTypes {
10 Request,
11 Response,
12 Notification,
13 Error,
14}
15impl Display for MessageTypes {
18 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 write!(
21 f,
22 "{}",
23 match self {
25 MessageTypes::Request => "Request",
26 MessageTypes::Response => "Response",
27 MessageTypes::Notification => "Notification",
28 MessageTypes::Error => "Error",
29 }
30 )
31 }
32}
33
34#[allow(dead_code)]
37fn detect_message_type(value: &serde_json::Value) -> MessageTypes {
38 let id_field = value.get("id");
39
40 if id_field.is_some() && value.get("error").is_some() {
41 return MessageTypes::Error;
42 }
43
44 let method_field = value.get("method");
45 let result_field = value.get("result");
46
47 if id_field.is_some() {
48 if result_field.is_some() && method_field.is_none() {
49 return MessageTypes::Response;
50 } else if method_field.is_some() {
51 return MessageTypes::Request;
52 }
53 } else if method_field.is_some() {
54 return MessageTypes::Notification;
55 }
56
57 MessageTypes::Request
58}
59
60pub trait RPCMessage: MCPMessage {
63 fn request_id(&self) -> Option<&RequestId>;
64 fn jsonrpc(&self) -> &str;
65}
66
67pub trait MCPMessage {
68 fn is_response(&self) -> bool;
69 fn is_request(&self) -> bool;
70 fn is_notification(&self) -> bool;
71 fn is_error(&self) -> bool;
72 fn message_type(&self) -> MessageTypes;
73}
74
75pub trait FromMessage<T>
81where
82 Self: Sized,
83{
84 fn from_message(message: T, request_id: Option<RequestId>) -> std::result::Result<Self, JsonrpcErrorError>;
85}
86
87pub trait ToMessage<T>
88where
89 T: FromMessage<Self>,
90 Self: Sized,
91{
92 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<T, JsonrpcErrorError>;
93}
94
95impl PartialEq for RequestId {
101 fn eq(&self, other: &Self) -> bool {
102 match (self, other) {
103 (RequestId::String(a), RequestId::String(b)) => a == b,
104 (RequestId::Integer(a), RequestId::Integer(b)) => a == b,
105 _ => false, }
107 }
108}
109
110impl Eq for RequestId {}
111
112impl Hash for RequestId {
114 fn hash<H: Hasher>(&self, state: &mut H) {
115 match self {
116 RequestId::String(s) => {
117 0u8.hash(state); s.hash(state);
119 }
120 RequestId::Integer(i) => {
121 1u8.hash(state); i.hash(state);
123 }
124 }
125 }
126}
127
128#[derive(::serde::Deserialize, ::serde::Serialize, Clone, Debug)]
135#[serde(untagged)]
136pub enum ClientMessage {
137 Request(ClientJsonrpcRequest),
138 Notification(ClientJsonrpcNotification),
139 Response(ClientJsonrpcResponse),
140 Error(JsonrpcError),
141}
142
143impl ClientMessage {
144 pub fn as_response(self) -> std::result::Result<ClientJsonrpcResponse, JsonrpcErrorError> {
154 if let Self::Response(response) = self {
155 Ok(response)
156 } else {
157 Err(JsonrpcErrorError::internal_error().with_message(format!(
158 "Invalid message type, expected: \"{}\" received\"{}\"",
159 MessageTypes::Response,
160 self.message_type()
161 )))
162 }
163 }
164
165 pub fn as_request(self) -> std::result::Result<ClientJsonrpcRequest, JsonrpcErrorError> {
175 if let Self::Request(request) = self {
176 Ok(request)
177 } else {
178 Err(JsonrpcErrorError::internal_error().with_message(format!(
179 "Invalid message type, expected: \"{}\" received\"{}\"",
180 MessageTypes::Request,
181 self.message_type()
182 )))
183 }
184 }
185
186 pub fn as_notification(self) -> std::result::Result<ClientJsonrpcNotification, JsonrpcErrorError> {
196 if let Self::Notification(notification) = self {
197 Ok(notification)
198 } else {
199 Err(JsonrpcErrorError::internal_error().with_message(format!(
200 "Invalid message type, expected: \"{}\" received\"{}\"",
201 MessageTypes::Notification,
202 self.message_type()
203 )))
204 }
205 }
206
207 pub fn as_error(self) -> std::result::Result<JsonrpcError, JsonrpcErrorError> {
217 if let Self::Error(error) = self {
218 Ok(error)
219 } else {
220 Err(JsonrpcErrorError::internal_error().with_message(format!(
221 "Invalid message type, expected: \"{}\" received\"{}\"",
222 MessageTypes::Error,
223 self.message_type()
224 )))
225 }
226 }
227}
228
229impl RPCMessage for ClientMessage {
230 fn request_id(&self) -> Option<&RequestId> {
232 match self {
233 ClientMessage::Request(client_jsonrpc_request) => Some(&client_jsonrpc_request.id),
235 ClientMessage::Notification(_) => None,
237 ClientMessage::Response(client_jsonrpc_response) => Some(&client_jsonrpc_response.id),
239 ClientMessage::Error(jsonrpc_error) => Some(&jsonrpc_error.id),
241 }
242 }
243
244 fn jsonrpc(&self) -> &str {
245 match self {
246 ClientMessage::Request(client_jsonrpc_request) => client_jsonrpc_request.jsonrpc(),
247 ClientMessage::Notification(notification) => notification.jsonrpc(),
248 ClientMessage::Response(client_jsonrpc_response) => client_jsonrpc_response.jsonrpc(),
249 ClientMessage::Error(jsonrpc_error) => jsonrpc_error.jsonrpc(),
250 }
251 }
252}
253
254impl MCPMessage for ClientMessage {
256 fn is_response(&self) -> bool {
258 matches!(self, ClientMessage::Response(_))
259 }
260
261 fn is_request(&self) -> bool {
263 matches!(self, ClientMessage::Request(_))
264 }
265
266 fn is_notification(&self) -> bool {
268 matches!(self, ClientMessage::Notification(_))
269 }
270
271 fn is_error(&self) -> bool {
273 matches!(self, ClientMessage::Error(_))
274 }
275
276 fn message_type(&self) -> MessageTypes {
278 match self {
279 ClientMessage::Request(_) => MessageTypes::Request,
280 ClientMessage::Notification(_) => MessageTypes::Notification,
281 ClientMessage::Response(_) => MessageTypes::Response,
282 ClientMessage::Error(_) => MessageTypes::Error,
283 }
284 }
285}
286
287#[derive(Clone, Debug)]
293pub struct ClientJsonrpcRequest {
294 pub id: RequestId,
295 jsonrpc: ::std::string::String,
296 pub method: String,
297 pub request: RequestFromClient,
298}
299
300impl ClientJsonrpcRequest {
301 pub fn new(id: RequestId, request: RequestFromClient) -> Self {
302 let method = request.method().to_string();
303 Self {
304 id,
305 jsonrpc: JSONRPC_VERSION.to_string(),
306 method,
307 request,
308 }
309 }
310 pub fn jsonrpc(&self) -> &::std::string::String {
311 &self.jsonrpc
312 }
313}
314
315impl Display for ClientJsonrpcRequest {
317 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
318 write!(
319 f,
320 "{}",
321 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
322 )
323 }
324}
325
326impl FromStr for ClientJsonrpcRequest {
327 type Err = JsonrpcErrorError;
328
329 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
351 serde_json::from_str(s)
352 .map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
353 }
354}
355
356#[derive(::serde::Serialize, Clone, Debug)]
363#[serde(untagged)]
364pub enum RequestFromClient {
365 ClientRequest(ClientRequest),
366 CustomRequest(serde_json::Value),
367}
368
369impl TryFrom<RequestFromClient> for ClientRequest {
370 type Error = JsonrpcErrorError;
371 fn try_from(value: RequestFromClient) -> result::Result<Self, Self::Error> {
372 if let RequestFromClient::ClientRequest(client_request) = value {
373 Ok(client_request)
374 } else {
375 Err(JsonrpcErrorError::internal_error().with_message("Not a ClientRequest".to_string()))
376 }
377 }
378}
379
380impl RequestFromClient {
381 #[deprecated(since = "0.1.4", note = "Use `method()` instead.")]
382 pub fn get_method(&self) -> &str {
383 match self {
384 RequestFromClient::ClientRequest(request) => request.method(),
385 RequestFromClient::CustomRequest(request) => request["method"].as_str().unwrap(),
386 }
387 }
388
389 fn method(&self) -> &str {
390 match self {
391 RequestFromClient::ClientRequest(request) => request.method(),
392 RequestFromClient::CustomRequest(request) => request["method"].as_str().unwrap(),
393 }
394 }
395}
396
397impl From<ClientRequest> for RequestFromClient {
398 fn from(value: ClientRequest) -> Self {
399 Self::ClientRequest(value)
400 }
401}
402
403impl From<serde_json::Value> for RequestFromClient {
404 fn from(value: serde_json::Value) -> Self {
405 Self::CustomRequest(value)
406 }
407}
408
409impl<'de> serde::Deserialize<'de> for RequestFromClient {
410 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
411 where
412 D: serde::Deserializer<'de>,
413 {
414 let raw_value = Value::deserialize(deserializer)?;
415
416 let client_result = ClientRequest::deserialize(&raw_value);
417
418 match client_result {
419 Ok(client_request) => Ok(Self::ClientRequest(client_request)),
420 Err(_) => Ok(Self::CustomRequest(raw_value)),
421 }
422 }
423}
424
425#[derive(Clone, Debug)]
431pub struct ClientJsonrpcNotification {
432 jsonrpc: ::std::string::String,
433 pub method: ::std::string::String,
434 pub notification: NotificationFromClient,
435}
436
437impl ClientJsonrpcNotification {
438 pub fn new(notification: NotificationFromClient) -> Self {
439 let method = notification.method().to_string();
440 Self {
441 jsonrpc: JSONRPC_VERSION.to_string(),
442 method,
443 notification,
444 }
445 }
446 pub fn jsonrpc(&self) -> &::std::string::String {
447 &self.jsonrpc
448 }
449}
450
451impl Display for ClientJsonrpcNotification {
453 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
454 write!(
455 f,
456 "{}",
457 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
458 )
459 }
460}
461
462impl FromStr for ClientJsonrpcNotification {
463 type Err = JsonrpcErrorError;
464
465 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
466 serde_json::from_str(s)
467 .map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
468 }
469}
470
471#[derive(::serde::Serialize, Clone, Debug)]
478#[serde(untagged)]
479pub enum NotificationFromClient {
480 ClientNotification(ClientNotification),
481 CustomNotification(serde_json::Value),
482}
483
484impl TryFrom<NotificationFromClient> for ClientNotification {
485 type Error = JsonrpcErrorError;
486 fn try_from(value: NotificationFromClient) -> result::Result<Self, Self::Error> {
487 if let NotificationFromClient::ClientNotification(client_notification) = value {
488 Ok(client_notification)
489 } else {
490 Err(JsonrpcErrorError::internal_error().with_message("Not a ClientNotification".to_string()))
491 }
492 }
493}
494
495impl NotificationFromClient {
496 pub fn is_initialized_notification(&self) -> bool {
498 matches!(
499 self,
500 NotificationFromClient::ClientNotification(ClientNotification::InitializedNotification(_))
501 )
502 }
503
504 #[deprecated(since = "0.1.4", note = "Use `method()` instead.")]
505 pub fn get_method(&self) -> &str {
506 match self {
507 NotificationFromClient::ClientNotification(notification) => notification.method(),
508 NotificationFromClient::CustomNotification(notification) => notification["method"].as_str().unwrap(),
509 }
510 }
511
512 fn method(&self) -> &str {
513 match self {
514 NotificationFromClient::ClientNotification(notification) => notification.method(),
515 NotificationFromClient::CustomNotification(notification) => notification["method"].as_str().unwrap(),
516 }
517 }
518}
519
520impl<'de> serde::Deserialize<'de> for NotificationFromClient {
521 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
522 where
523 D: serde::Deserializer<'de>,
524 {
525 let raw_value = Value::deserialize(deserializer)?;
526
527 let result = ClientNotification::deserialize(&raw_value);
528
529 match result {
530 Ok(client_notification) => Ok(Self::ClientNotification(client_notification)),
531 Err(_) => Ok(Self::CustomNotification(raw_value)),
532 }
533 }
534}
535
536#[derive(Clone, Debug)]
542pub struct ClientJsonrpcResponse {
543 pub id: RequestId,
544 jsonrpc: ::std::string::String,
545 pub result: ResultFromClient,
546}
547
548impl ClientJsonrpcResponse {
549 pub fn new(id: RequestId, result: ResultFromClient) -> Self {
550 Self {
551 id,
552 jsonrpc: JSONRPC_VERSION.to_string(),
553 result,
554 }
555 }
556 pub fn jsonrpc(&self) -> &::std::string::String {
557 &self.jsonrpc
558 }
559}
560
561impl Display for ClientJsonrpcResponse {
563 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
564 write!(
565 f,
566 "{}",
567 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
568 )
569 }
570}
571
572impl FromStr for ClientJsonrpcResponse {
573 type Err = JsonrpcErrorError;
574
575 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
576 serde_json::from_str(s)
577 .map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
578 }
579}
580#[derive(::serde::Serialize, Clone, Debug)]
587#[serde(untagged)]
588pub enum ResultFromClient {
589 ClientResult(ClientResult),
590 CustomResult(serde_json::Value),
591}
592
593impl TryFrom<ResultFromClient> for ClientResult {
594 type Error = JsonrpcErrorError;
595 fn try_from(value: ResultFromClient) -> result::Result<Self, Self::Error> {
596 if let ResultFromClient::ClientResult(client_result) = value {
597 Ok(client_result)
598 } else {
599 Err(JsonrpcErrorError::internal_error().with_message("Not a ClientResult".to_string()))
600 }
601 }
602}
603
604impl From<ClientResult> for ResultFromClient {
605 fn from(value: ClientResult) -> Self {
606 Self::ClientResult(value)
607 }
608}
609
610impl From<serde_json::Value> for ResultFromClient {
611 fn from(value: serde_json::Value) -> Self {
612 Self::CustomResult(value)
613 }
614}
615
616impl<'de> serde::Deserialize<'de> for ResultFromClient {
617 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
618 where
619 D: serde::Deserializer<'de>,
620 {
621 let raw_value = Value::deserialize(deserializer)?;
622
623 let result = ClientResult::deserialize(&raw_value);
624
625 match result {
626 Ok(client_result) => Ok(Self::ClientResult(client_result)),
627 Err(_) => Ok(Self::CustomResult(raw_value)),
628 }
629 }
630}
631
632impl FromStr for ClientMessage {
637 type Err = JsonrpcErrorError;
638
639 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
640 serde_json::from_str(s)
641 .map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
642 }
643}
644
645impl Display for ClientMessage {
646 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
647 write!(
648 f,
649 "{}",
650 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
651 )
652 }
653}
654
655#[derive(::serde::Deserialize, ::serde::Serialize, Clone, Debug)]
662#[serde(untagged)]
663pub enum ServerMessage {
664 Request(ServerJsonrpcRequest),
665 Notification(ServerJsonrpcNotification),
666 Response(ServerJsonrpcResponse),
667 Error(JsonrpcError),
668}
669
670impl ServerMessage {
671 pub fn as_response(self) -> std::result::Result<ServerJsonrpcResponse, JsonrpcErrorError> {
681 if let Self::Response(response) = self {
682 Ok(response)
683 } else {
684 Err(JsonrpcErrorError::internal_error().with_message(format!(
685 "Invalid message type, expected: \"{}\" received\"{}\"",
686 MessageTypes::Response,
687 self.message_type()
688 )))
689 }
690 }
691
692 pub fn as_request(self) -> std::result::Result<ServerJsonrpcRequest, JsonrpcErrorError> {
702 if let Self::Request(request) = self {
703 Ok(request)
704 } else {
705 Err(JsonrpcErrorError::internal_error().with_message(format!(
706 "Invalid message type, expected: \"{}\" received\"{}\"",
707 MessageTypes::Request,
708 self.message_type()
709 )))
710 }
711 }
712
713 pub fn as_notification(self) -> std::result::Result<ServerJsonrpcNotification, JsonrpcErrorError> {
723 if let Self::Notification(notification) = self {
724 Ok(notification)
725 } else {
726 Err(JsonrpcErrorError::internal_error().with_message(format!(
727 "Invalid message type, expected: \"{}\" received\"{}\"",
728 MessageTypes::Notification,
729 self.message_type()
730 )))
731 }
732 }
733
734 pub fn as_error(self) -> std::result::Result<JsonrpcError, JsonrpcErrorError> {
744 if let Self::Error(error) = self {
745 Ok(error)
746 } else {
747 Err(JsonrpcErrorError::internal_error().with_message(format!(
748 "Invalid message type, expected: \"{}\" received\"{}\"",
749 MessageTypes::Error,
750 self.message_type()
751 )))
752 }
753 }
754}
755
756impl RPCMessage for ServerMessage {
757 fn request_id(&self) -> Option<&RequestId> {
759 match self {
760 ServerMessage::Request(server_jsonrpc_request) => Some(&server_jsonrpc_request.id),
762 ServerMessage::Notification(_) => None,
764 ServerMessage::Response(server_jsonrpc_response) => Some(&server_jsonrpc_response.id),
766 ServerMessage::Error(jsonrpc_error) => Some(&jsonrpc_error.id),
768 }
769 }
770
771 fn jsonrpc(&self) -> &str {
772 match self {
773 ServerMessage::Request(server_jsonrpc_request) => server_jsonrpc_request.jsonrpc(),
775 ServerMessage::Notification(notification) => notification.jsonrpc(),
777 ServerMessage::Response(server_jsonrpc_response) => server_jsonrpc_response.jsonrpc(),
779 ServerMessage::Error(jsonrpc_error) => jsonrpc_error.jsonrpc(),
781 }
782 }
783}
784
785impl MCPMessage for ServerMessage {
787 fn is_response(&self) -> bool {
789 matches!(self, ServerMessage::Response(_))
790 }
791
792 fn is_request(&self) -> bool {
794 matches!(self, ServerMessage::Request(_))
795 }
796
797 fn is_notification(&self) -> bool {
799 matches!(self, ServerMessage::Notification(_))
800 }
801
802 fn is_error(&self) -> bool {
804 matches!(self, ServerMessage::Error(_))
805 }
806
807 fn message_type(&self) -> MessageTypes {
809 match self {
810 ServerMessage::Request(_) => MessageTypes::Request,
811 ServerMessage::Notification(_) => MessageTypes::Notification,
812 ServerMessage::Response(_) => MessageTypes::Response,
813 ServerMessage::Error(_) => MessageTypes::Error,
814 }
815 }
816}
817
818impl FromStr for ServerMessage {
819 type Err = JsonrpcErrorError;
820
821 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
822 serde_json::from_str(s)
823 .map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
824 }
825}
826
827impl Display for ServerMessage {
828 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
829 write!(
830 f,
831 "{}",
832 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
833 )
834 }
835}
836
837#[derive(Clone, Debug)]
843pub struct ServerJsonrpcRequest {
844 pub id: RequestId,
845 jsonrpc: ::std::string::String,
846 pub method: String,
847 pub request: RequestFromServer,
848}
849
850impl ServerJsonrpcRequest {
851 pub fn new(id: RequestId, request: RequestFromServer) -> Self {
852 let method = request.method().to_string();
853 Self {
854 id,
855 jsonrpc: JSONRPC_VERSION.to_string(),
856 method,
857 request,
858 }
859 }
860 pub fn jsonrpc(&self) -> &::std::string::String {
861 &self.jsonrpc
862 }
863}
864
865impl Display for ServerJsonrpcRequest {
867 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
868 write!(
869 f,
870 "{}",
871 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
872 )
873 }
874}
875
876impl FromStr for ServerJsonrpcRequest {
877 type Err = JsonrpcErrorError;
878
879 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
880 serde_json::from_str(s)
881 .map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
882 }
883}
884#[derive(::serde::Serialize, Clone, Debug)]
891#[serde(untagged)]
892pub enum RequestFromServer {
893 ServerRequest(ServerRequest),
894 CustomRequest(serde_json::Value),
895}
896
897impl TryFrom<RequestFromServer> for ServerRequest {
898 type Error = JsonrpcErrorError;
899 fn try_from(value: RequestFromServer) -> result::Result<Self, Self::Error> {
900 if let RequestFromServer::ServerRequest(server_request) = value {
901 Ok(server_request)
902 } else {
903 Err(JsonrpcErrorError::internal_error().with_message("Not a ServerRequest".to_string()))
904 }
905 }
906}
907
908impl RequestFromServer {
909 #[deprecated(since = "0.1.4", note = "Use `method()` instead.")]
910 pub fn get_method(&self) -> &str {
911 match self {
912 RequestFromServer::ServerRequest(request) => request.method(),
913 RequestFromServer::CustomRequest(request) => request["method"].as_str().unwrap(),
914 }
915 }
916
917 fn method(&self) -> &str {
918 match self {
919 RequestFromServer::ServerRequest(request) => request.method(),
920 RequestFromServer::CustomRequest(request) => request["method"].as_str().unwrap(),
921 }
922 }
923}
924
925impl From<ServerRequest> for RequestFromServer {
926 fn from(value: ServerRequest) -> Self {
927 Self::ServerRequest(value)
928 }
929}
930
931impl From<serde_json::Value> for RequestFromServer {
932 fn from(value: serde_json::Value) -> Self {
933 Self::CustomRequest(value)
934 }
935}
936
937impl<'de> serde::Deserialize<'de> for RequestFromServer {
938 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
939 where
940 D: serde::Deserializer<'de>,
941 {
942 let raw_value = Value::deserialize(deserializer)?;
943
944 let server_result = ServerRequest::deserialize(&raw_value);
945
946 match server_result {
947 Ok(server_request) => Ok(Self::ServerRequest(server_request)),
948 Err(_) => Ok(Self::CustomRequest(raw_value)),
949 }
950 }
951}
952
953#[derive(Clone, Debug)]
959pub struct ServerJsonrpcNotification {
960 jsonrpc: ::std::string::String,
961 pub method: ::std::string::String,
962 pub notification: NotificationFromServer,
963}
964
965impl ServerJsonrpcNotification {
966 pub fn new(notification: NotificationFromServer) -> Self {
967 let method = notification.method().to_string();
968 Self {
969 jsonrpc: JSONRPC_VERSION.to_string(),
970 method,
971 notification,
972 }
973 }
974 pub fn jsonrpc(&self) -> &::std::string::String {
975 &self.jsonrpc
976 }
977}
978
979impl Display for ServerJsonrpcNotification {
981 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
982 write!(
983 f,
984 "{}",
985 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
986 )
987 }
988}
989
990impl FromStr for ServerJsonrpcNotification {
991 type Err = JsonrpcErrorError;
992
993 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
994 serde_json::from_str(s)
995 .map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
996 }
997}
998#[derive(::serde::Serialize, Clone, Debug)]
1005#[serde(untagged)]
1006pub enum NotificationFromServer {
1007 ServerNotification(ServerNotification),
1008 CustomNotification(serde_json::Value),
1009}
1010
1011impl TryFrom<NotificationFromServer> for ServerNotification {
1012 type Error = JsonrpcErrorError;
1013 fn try_from(value: NotificationFromServer) -> result::Result<Self, Self::Error> {
1014 if let NotificationFromServer::ServerNotification(server_notification) = value {
1015 Ok(server_notification)
1016 } else {
1017 Err(JsonrpcErrorError::internal_error().with_message("Not a ServerNotification".to_string()))
1018 }
1019 }
1020}
1021
1022impl NotificationFromServer {
1023 #[deprecated(since = "0.1.4", note = "Use `method()` instead.")]
1024 pub fn get_method(&self) -> &str {
1025 match self {
1026 NotificationFromServer::ServerNotification(notification) => notification.method(),
1027 NotificationFromServer::CustomNotification(notification) => notification["method"].as_str().unwrap(),
1028 }
1029 }
1030
1031 fn method(&self) -> &str {
1032 match self {
1033 NotificationFromServer::ServerNotification(notification) => notification.method(),
1034 NotificationFromServer::CustomNotification(notification) => notification["method"].as_str().unwrap(),
1035 }
1036 }
1037}
1038
1039impl<'de> serde::Deserialize<'de> for NotificationFromServer {
1040 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
1041 where
1042 D: serde::Deserializer<'de>,
1043 {
1044 let raw_value = Value::deserialize(deserializer)?;
1045
1046 let result = ServerNotification::deserialize(&raw_value);
1047
1048 match result {
1049 Ok(client_notification) => Ok(Self::ServerNotification(client_notification)),
1050 Err(_) => Ok(Self::CustomNotification(raw_value)),
1051 }
1052 }
1053}
1054
1055#[derive(Clone, Debug)]
1061pub struct ServerJsonrpcResponse {
1062 pub id: RequestId,
1063 jsonrpc: ::std::string::String,
1064 pub result: ResultFromServer,
1065}
1066
1067impl ServerJsonrpcResponse {
1068 pub fn new(id: RequestId, result: ResultFromServer) -> Self {
1069 Self {
1070 id,
1071 jsonrpc: JSONRPC_VERSION.to_string(),
1072 result,
1073 }
1074 }
1075 pub fn jsonrpc(&self) -> &::std::string::String {
1076 &self.jsonrpc
1077 }
1078}
1079
1080impl Display for ServerJsonrpcResponse {
1082 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1083 write!(
1084 f,
1085 "{}",
1086 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
1087 )
1088 }
1089}
1090
1091impl FromStr for ServerJsonrpcResponse {
1092 type Err = JsonrpcErrorError;
1093
1094 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
1095 serde_json::from_str(s)
1096 .map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
1097 }
1098}
1099#[derive(::serde::Serialize, Clone, Debug)]
1106#[serde(untagged)]
1107pub enum ResultFromServer {
1108 ServerResult(ServerResult),
1109 CustomResult(serde_json::Value),
1110}
1111
1112impl TryFrom<ResultFromServer> for ServerResult {
1113 type Error = JsonrpcErrorError;
1114 fn try_from(value: ResultFromServer) -> result::Result<Self, Self::Error> {
1115 if let ResultFromServer::ServerResult(server_result) = value {
1116 Ok(server_result)
1117 } else {
1118 Err(JsonrpcErrorError::internal_error().with_message("Not a ServerResult".to_string()))
1119 }
1120 }
1121}
1122
1123impl From<ServerResult> for ResultFromServer {
1124 fn from(value: ServerResult) -> Self {
1125 Self::ServerResult(value)
1126 }
1127}
1128
1129impl From<serde_json::Value> for ResultFromServer {
1130 fn from(value: serde_json::Value) -> Self {
1131 Self::CustomResult(value)
1132 }
1133}
1134
1135impl<'de> serde::Deserialize<'de> for ResultFromServer {
1136 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
1137 where
1138 D: serde::Deserializer<'de>,
1139 {
1140 let raw_value = Value::deserialize(deserializer)?;
1141
1142 let result = ServerResult::deserialize(&raw_value);
1143
1144 match result {
1145 Ok(server_result) => {
1146 if matches!(server_result, ServerResult::Result(_)) {
1147 Ok(Self::CustomResult(raw_value))
1148 } else {
1149 Ok(Self::ServerResult(server_result))
1150 }
1151 }
1152 Err(_) => Ok(Self::CustomResult(raw_value)),
1153 }
1154 }
1155}
1156
1157impl Display for JsonrpcError {
1163 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1164 write!(
1165 f,
1166 "{}",
1167 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
1168 )
1169 }
1170}
1171
1172impl FromStr for JsonrpcError {
1173 type Err = JsonrpcErrorError;
1174
1175 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
1176 serde_json::from_str(s)
1177 .map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
1178 }
1179}
1180
1181#[derive(::serde::Serialize, Clone, Debug)]
1190#[serde(untagged)]
1191pub enum MessageFromServer {
1192 RequestFromServer(RequestFromServer),
1193 ResultFromServer(ResultFromServer),
1194 NotificationFromServer(NotificationFromServer),
1195 Error(JsonrpcErrorError),
1196}
1197
1198impl From<RequestFromServer> for MessageFromServer {
1199 fn from(value: RequestFromServer) -> Self {
1200 Self::RequestFromServer(value)
1201 }
1202}
1203
1204impl From<ResultFromServer> for MessageFromServer {
1205 fn from(value: ResultFromServer) -> Self {
1206 Self::ResultFromServer(value)
1207 }
1208}
1209
1210impl From<NotificationFromServer> for MessageFromServer {
1211 fn from(value: NotificationFromServer) -> Self {
1212 Self::NotificationFromServer(value)
1213 }
1214}
1215
1216impl From<JsonrpcErrorError> for MessageFromServer {
1217 fn from(value: JsonrpcErrorError) -> Self {
1218 Self::Error(value)
1219 }
1220}
1221
1222impl MCPMessage for MessageFromServer {
1223 fn is_response(&self) -> bool {
1224 matches!(self, MessageFromServer::ResultFromServer(_))
1225 }
1226
1227 fn is_request(&self) -> bool {
1228 matches!(self, MessageFromServer::RequestFromServer(_))
1229 }
1230
1231 fn is_notification(&self) -> bool {
1232 matches!(self, MessageFromServer::NotificationFromServer(_))
1233 }
1234
1235 fn is_error(&self) -> bool {
1236 matches!(self, MessageFromServer::Error(_))
1237 }
1238
1239 fn message_type(&self) -> MessageTypes {
1240 match self {
1241 MessageFromServer::RequestFromServer(_) => MessageTypes::Request,
1242 MessageFromServer::ResultFromServer(_) => MessageTypes::Response,
1243 MessageFromServer::NotificationFromServer(_) => MessageTypes::Notification,
1244 MessageFromServer::Error(_) => MessageTypes::Error,
1245 }
1246 }
1247}
1248
1249impl FromMessage<MessageFromServer> for ServerMessage {
1250 fn from_message(
1251 message: MessageFromServer,
1252 request_id: Option<RequestId>,
1253 ) -> std::result::Result<Self, JsonrpcErrorError> {
1254 match message {
1255 MessageFromServer::RequestFromServer(request_from_server) => {
1256 let request_id = request_id
1257 .ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
1258 Ok(ServerMessage::Request(ServerJsonrpcRequest::new(
1259 request_id,
1260 request_from_server,
1261 )))
1262 }
1263 MessageFromServer::ResultFromServer(result_from_server) => {
1264 let request_id = request_id
1265 .ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
1266 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
1267 request_id,
1268 result_from_server,
1269 )))
1270 }
1271 MessageFromServer::NotificationFromServer(notification_from_server) => {
1272 if request_id.is_some() {
1273 return Err(JsonrpcErrorError::internal_error()
1274 .with_message("request_id expected to be None for Notifications!".to_string()));
1275 }
1276 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(
1277 notification_from_server,
1278 )))
1279 }
1280 MessageFromServer::Error(jsonrpc_error_error) => {
1281 let request_id = request_id
1282 .ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
1283 Ok(ServerMessage::Error(JsonrpcError::new(jsonrpc_error_error, request_id)))
1284 }
1285 }
1286 }
1287}
1288
1289#[derive(::serde::Serialize, Clone, Debug)]
1298#[serde(untagged)]
1299pub enum MessageFromClient {
1300 RequestFromClient(RequestFromClient),
1301 ResultFromClient(ResultFromClient),
1302 NotificationFromClient(NotificationFromClient),
1303 Error(JsonrpcErrorError),
1304}
1305
1306impl From<RequestFromClient> for MessageFromClient {
1307 fn from(value: RequestFromClient) -> Self {
1308 Self::RequestFromClient(value)
1309 }
1310}
1311
1312impl From<ResultFromClient> for MessageFromClient {
1313 fn from(value: ResultFromClient) -> Self {
1314 Self::ResultFromClient(value)
1315 }
1316}
1317
1318impl From<NotificationFromClient> for MessageFromClient {
1319 fn from(value: NotificationFromClient) -> Self {
1320 Self::NotificationFromClient(value)
1321 }
1322}
1323
1324impl From<JsonrpcErrorError> for MessageFromClient {
1325 fn from(value: JsonrpcErrorError) -> Self {
1326 Self::Error(value)
1327 }
1328}
1329
1330impl FromMessage<MessageFromClient> for ClientMessage {
1331 fn from_message(
1332 message: MessageFromClient,
1333 request_id: Option<RequestId>,
1334 ) -> std::result::Result<Self, JsonrpcErrorError> {
1335 match message {
1336 MessageFromClient::RequestFromClient(request_from_client) => {
1337 let request_id = request_id
1338 .ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
1339 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(
1340 request_id,
1341 request_from_client,
1342 )))
1343 }
1344 MessageFromClient::ResultFromClient(result_from_client) => {
1345 let request_id = request_id
1346 .ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
1347 Ok(ClientMessage::Response(ClientJsonrpcResponse::new(
1348 request_id,
1349 result_from_client,
1350 )))
1351 }
1352 MessageFromClient::NotificationFromClient(notification_from_client) => {
1353 if request_id.is_some() {
1354 return Err(JsonrpcErrorError::internal_error()
1355 .with_message("request_id expected to be None for Notifications!".to_string()));
1356 }
1357
1358 Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(
1359 notification_from_client,
1360 )))
1361 }
1362 MessageFromClient::Error(jsonrpc_error_error) => {
1363 let request_id = request_id
1364 .ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
1365 Ok(ClientMessage::Error(JsonrpcError::new(jsonrpc_error_error, request_id)))
1366 }
1367 }
1368 }
1369}
1370
1371impl ::serde::Serialize for ClientJsonrpcRequest {
1373 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
1374 where
1375 S: ::serde::Serializer,
1376 {
1377 let mut state = serializer.serialize_struct("JsonrpcRequest", 4)?;
1378 state.serialize_field("id", &self.id)?;
1379 state.serialize_field("jsonrpc", &self.jsonrpc)?;
1380 state.serialize_field("method", &self.method)?;
1381 use ClientRequest::*;
1382 match &self.request {
1383 RequestFromClient::ClientRequest(message) => match message {
1384 InitializeRequest(msg) => state.serialize_field("params", &msg.params)?,
1385 PingRequest(msg) => {
1386 if let Some(params) = &msg.params {
1387 state.serialize_field("params", params)?
1388 }
1389 }
1390 ListResourcesRequest(msg) => state.serialize_field("params", &msg.params)?,
1391 ListResourceTemplatesRequest(msg) => {
1392 if let Some(params) = &msg.params {
1393 state.serialize_field("params", params)?
1394 }
1395 }
1396 ReadResourceRequest(msg) => state.serialize_field("params", &msg.params)?,
1397 SubscribeRequest(msg) => state.serialize_field("params", &msg.params)?,
1398 UnsubscribeRequest(msg) => state.serialize_field("params", &msg.params)?,
1399 ListPromptsRequest(msg) => {
1400 if let Some(params) = &msg.params {
1401 state.serialize_field("params", params)?
1402 }
1403 }
1404 GetPromptRequest(msg) => state.serialize_field("params", &msg.params)?,
1405 ListToolsRequest(msg) => {
1406 if let Some(params) = &msg.params {
1407 state.serialize_field("params", params)?
1408 }
1409 }
1410 CallToolRequest(msg) => state.serialize_field("params", &msg.params)?,
1411 SetLevelRequest(msg) => state.serialize_field("params", &msg.params)?,
1412 CompleteRequest(msg) => state.serialize_field("params", &msg.params)?,
1413 },
1414 RequestFromClient::CustomRequest(value) => state.serialize_field("params", value)?,
1415 }
1416 state.end()
1417 }
1418}
1419impl<'de> ::serde::Deserialize<'de> for ClientJsonrpcRequest {
1420 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1421 where
1422 D: ::serde::Deserializer<'de>,
1423 {
1424 use serde::de::{self, MapAccess, Visitor};
1425 use std::fmt;
1426 struct ClientJsonrpcRequestVisitor;
1427 impl<'de> Visitor<'de> for ClientJsonrpcRequestVisitor {
1428 type Value = ClientJsonrpcRequest;
1429 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1430 formatter.write_str("a valid JSON-RPC request object")
1431 }
1432 fn visit_map<M>(self, mut map: M) -> std::result::Result<ClientJsonrpcRequest, M::Error>
1433 where
1434 M: MapAccess<'de>,
1435 {
1436 let mut id: Option<RequestId> = None;
1437 let mut jsonrpc: Option<String> = None;
1438 let mut method: Option<String> = None;
1439 let mut params: Option<Value> = None;
1440 while let Some(key) = map.next_key::<String>()? {
1441 match key.as_str() {
1442 "id" => id = Some(map.next_value()?),
1443 "jsonrpc" => jsonrpc = Some(map.next_value()?),
1444 "method" => method = Some(map.next_value()?),
1445 "params" => params = Some(map.next_value()?),
1446 _ => {
1447 return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"]));
1448 }
1449 }
1450 }
1451 let id = id.ok_or_else(|| de::Error::missing_field("id"))?;
1452 let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?;
1453 let method = method.ok_or_else(|| de::Error::missing_field("method"))?;
1454 let params = params.unwrap_or_default();
1455 let req_object = json!({ "method" : method, "params" : params });
1456 let request = serde_json::from_value::<RequestFromClient>(req_object).map_err(de::Error::custom)?;
1457 Ok(ClientJsonrpcRequest {
1458 id,
1459 jsonrpc,
1460 method,
1461 request,
1462 })
1463 }
1464 }
1465 deserializer.deserialize_struct(
1466 "JsonrpcRequest",
1467 &["id", "jsonrpc", "method", "params"],
1468 ClientJsonrpcRequestVisitor,
1469 )
1470 }
1471}
1472impl ::serde::Serialize for ServerJsonrpcRequest {
1473 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
1474 where
1475 S: ::serde::Serializer,
1476 {
1477 let mut state = serializer.serialize_struct("JsonrpcRequest", 4)?;
1478 state.serialize_field("id", &self.id)?;
1479 state.serialize_field("jsonrpc", &self.jsonrpc)?;
1480 state.serialize_field("method", &self.method)?;
1481 use ServerRequest::*;
1482 match &self.request {
1483 RequestFromServer::ServerRequest(message) => match message {
1484 PingRequest(msg) => {
1485 if let Some(params) = &msg.params {
1486 state.serialize_field("params", params)?
1487 }
1488 }
1489 CreateMessageRequest(msg) => state.serialize_field("params", &msg.params)?,
1490 ListRootsRequest(msg) => {
1491 if let Some(params) = &msg.params {
1492 state.serialize_field("params", params)?
1493 }
1494 }
1495 },
1496 RequestFromServer::CustomRequest(value) => state.serialize_field("params", value)?,
1497 }
1498 state.end()
1499 }
1500}
1501impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcRequest {
1502 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1503 where
1504 D: ::serde::Deserializer<'de>,
1505 {
1506 use serde::de::{self, MapAccess, Visitor};
1507 use std::fmt;
1508 struct ServerJsonrpcRequestVisitor;
1509 impl<'de> Visitor<'de> for ServerJsonrpcRequestVisitor {
1510 type Value = ServerJsonrpcRequest;
1511 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1512 formatter.write_str("a valid JSON-RPC request object")
1513 }
1514 fn visit_map<M>(self, mut map: M) -> std::result::Result<ServerJsonrpcRequest, M::Error>
1515 where
1516 M: MapAccess<'de>,
1517 {
1518 let mut id: Option<RequestId> = None;
1519 let mut jsonrpc: Option<String> = None;
1520 let mut method: Option<String> = None;
1521 let mut params: Option<Value> = None;
1522 while let Some(key) = map.next_key::<String>()? {
1523 match key.as_str() {
1524 "id" => id = Some(map.next_value()?),
1525 "jsonrpc" => jsonrpc = Some(map.next_value()?),
1526 "method" => method = Some(map.next_value()?),
1527 "params" => params = Some(map.next_value()?),
1528 _ => {
1529 return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"]));
1530 }
1531 }
1532 }
1533 let id = id.ok_or_else(|| de::Error::missing_field("id"))?;
1534 let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?;
1535 let method = method.ok_or_else(|| de::Error::missing_field("method"))?;
1536 let params = params.unwrap_or_default();
1537 let req_object = json!({ "method" : method, "params" : params });
1538 let request = serde_json::from_value::<RequestFromServer>(req_object).map_err(de::Error::custom)?;
1539 Ok(ServerJsonrpcRequest {
1540 id,
1541 jsonrpc,
1542 method,
1543 request,
1544 })
1545 }
1546 }
1547 deserializer.deserialize_struct(
1548 "JsonrpcRequest",
1549 &["id", "jsonrpc", "method", "params"],
1550 ServerJsonrpcRequestVisitor,
1551 )
1552 }
1553}
1554impl ::serde::Serialize for ClientJsonrpcNotification {
1555 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
1556 where
1557 S: ::serde::Serializer,
1558 {
1559 let mut state = serializer.serialize_struct("JsonrpcNotification", 3)?;
1560 state.serialize_field("jsonrpc", &self.jsonrpc)?;
1561 state.serialize_field("method", &self.method)?;
1562 use ClientNotification::*;
1563 match &self.notification {
1564 NotificationFromClient::ClientNotification(message) => match message {
1565 CancelledNotification(msg) => state.serialize_field("params", &msg.params)?,
1566 InitializedNotification(msg) => {
1567 if let Some(params) = &msg.params {
1568 state.serialize_field("params", params)?
1569 }
1570 }
1571 ProgressNotification(msg) => state.serialize_field("params", &msg.params)?,
1572 RootsListChangedNotification(msg) => {
1573 if let Some(params) = &msg.params {
1574 state.serialize_field("params", params)?
1575 }
1576 }
1577 },
1578 NotificationFromClient::CustomNotification(value) => state.serialize_field("params", value)?,
1579 }
1580 state.end()
1581 }
1582}
1583impl<'de> ::serde::Deserialize<'de> for ClientJsonrpcNotification {
1584 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1585 where
1586 D: ::serde::Deserializer<'de>,
1587 {
1588 use serde::de::{self, MapAccess, Visitor};
1589 use std::fmt;
1590 struct ClientJsonrpcNotificationVisitor;
1591 impl<'de> Visitor<'de> for ClientJsonrpcNotificationVisitor {
1592 type Value = ClientJsonrpcNotification;
1593 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1594 formatter.write_str("a valid JSON-RPC notification object")
1595 }
1596 fn visit_map<M>(self, mut map: M) -> std::result::Result<ClientJsonrpcNotification, M::Error>
1597 where
1598 M: MapAccess<'de>,
1599 {
1600 let mut jsonrpc: Option<String> = None;
1601 let mut method: Option<String> = None;
1602 let mut params: Option<Value> = None;
1603 while let Some(key) = map.next_key::<String>()? {
1604 match key.as_str() {
1605 "jsonrpc" => jsonrpc = Some(map.next_value()?),
1606 "method" => method = Some(map.next_value()?),
1607 "params" => params = Some(map.next_value()?),
1608 _ => {
1609 return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"]));
1610 }
1611 }
1612 }
1613 let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?;
1614 let method = method.ok_or_else(|| de::Error::missing_field("method"))?;
1615 let params = params.unwrap_or_default();
1616 let req_object = json!({ "method" : method, "params" : params });
1617 let notification =
1618 serde_json::from_value::<NotificationFromClient>(req_object).map_err(de::Error::custom)?;
1619 Ok(ClientJsonrpcNotification {
1620 jsonrpc,
1621 method,
1622 notification,
1623 })
1624 }
1625 }
1626 deserializer.deserialize_struct(
1627 "JsonrpcRequest",
1628 &["jsonrpc", "method", "params"],
1629 ClientJsonrpcNotificationVisitor,
1630 )
1631 }
1632}
1633impl ::serde::Serialize for ServerJsonrpcNotification {
1634 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
1635 where
1636 S: ::serde::Serializer,
1637 {
1638 let mut state = serializer.serialize_struct("JsonrpcNotification", 3)?;
1639 state.serialize_field("jsonrpc", &self.jsonrpc)?;
1640 state.serialize_field("method", &self.method)?;
1641 use ServerNotification::*;
1642 match &self.notification {
1643 NotificationFromServer::ServerNotification(message) => match message {
1644 CancelledNotification(msg) => state.serialize_field("params", &msg.params)?,
1645 ProgressNotification(msg) => state.serialize_field("params", &msg.params)?,
1646 ResourceListChangedNotification(msg) => {
1647 if let Some(params) = &msg.params {
1648 state.serialize_field("params", params)?
1649 }
1650 }
1651 ResourceUpdatedNotification(msg) => state.serialize_field("params", &msg.params)?,
1652 PromptListChangedNotification(msg) => {
1653 if let Some(params) = &msg.params {
1654 state.serialize_field("params", params)?
1655 }
1656 }
1657 ToolListChangedNotification(msg) => {
1658 if let Some(params) = &msg.params {
1659 state.serialize_field("params", params)?
1660 }
1661 }
1662 LoggingMessageNotification(msg) => state.serialize_field("params", &msg.params)?,
1663 },
1664 NotificationFromServer::CustomNotification(value) => state.serialize_field("params", value)?,
1665 }
1666 state.end()
1667 }
1668}
1669impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcNotification {
1670 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1671 where
1672 D: ::serde::Deserializer<'de>,
1673 {
1674 use serde::de::{self, MapAccess, Visitor};
1675 use std::fmt;
1676 struct ServerJsonrpcNotificationVisitor;
1677 impl<'de> Visitor<'de> for ServerJsonrpcNotificationVisitor {
1678 type Value = ServerJsonrpcNotification;
1679 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1680 formatter.write_str("a valid JSON-RPC notification object")
1681 }
1682 fn visit_map<M>(self, mut map: M) -> std::result::Result<ServerJsonrpcNotification, M::Error>
1683 where
1684 M: MapAccess<'de>,
1685 {
1686 let mut jsonrpc: Option<String> = None;
1687 let mut method: Option<String> = None;
1688 let mut params: Option<Value> = None;
1689 while let Some(key) = map.next_key::<String>()? {
1690 match key.as_str() {
1691 "jsonrpc" => jsonrpc = Some(map.next_value()?),
1692 "method" => method = Some(map.next_value()?),
1693 "params" => params = Some(map.next_value()?),
1694 _ => {
1695 return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"]));
1696 }
1697 }
1698 }
1699 let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?;
1700 let method = method.ok_or_else(|| de::Error::missing_field("method"))?;
1701 let params = params.unwrap_or_default();
1702 let req_object = json!({ "method" : method, "params" : params });
1703 let notification =
1704 serde_json::from_value::<NotificationFromServer>(req_object).map_err(de::Error::custom)?;
1705 Ok(ServerJsonrpcNotification {
1706 jsonrpc,
1707 method,
1708 notification,
1709 })
1710 }
1711 }
1712 deserializer.deserialize_struct(
1713 "JsonrpcRequest",
1714 &["jsonrpc", "method", "params"],
1715 ServerJsonrpcNotificationVisitor,
1716 )
1717 }
1718}
1719impl ::serde::Serialize for ServerJsonrpcResponse {
1720 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
1721 where
1722 S: ::serde::Serializer,
1723 {
1724 let mut state = serializer.serialize_struct("JsonrpcResponse", 3)?;
1725 state.serialize_field("id", &self.id)?;
1726 state.serialize_field("jsonrpc", &self.jsonrpc)?;
1727 state.serialize_field("result", &self.result)?;
1728 state.end()
1729 }
1730}
1731impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcResponse {
1732 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1733 where
1734 D: ::serde::Deserializer<'de>,
1735 {
1736 use serde::de::{self, MapAccess, Visitor};
1737 use std::fmt;
1738 struct ServerJsonrpcResultVisitor;
1739 impl<'de> Visitor<'de> for ServerJsonrpcResultVisitor {
1740 type Value = ServerJsonrpcResponse;
1741 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1742 formatter.write_str("a valid JSON-RPC response object")
1743 }
1744 fn visit_map<M>(self, mut map: M) -> std::result::Result<ServerJsonrpcResponse, M::Error>
1745 where
1746 M: MapAccess<'de>,
1747 {
1748 let mut id: Option<RequestId> = None;
1749 let mut jsonrpc: Option<String> = None;
1750 let mut result: Option<Value> = None;
1751 while let Some(key) = map.next_key::<String>()? {
1752 match key.as_str() {
1753 "id" => id = Some(map.next_value()?),
1754 "jsonrpc" => jsonrpc = Some(map.next_value()?),
1755 "result" => result = Some(map.next_value()?),
1756 _ => {
1757 return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "result"]));
1758 }
1759 }
1760 }
1761 let id = id.ok_or_else(|| de::Error::missing_field("id"))?;
1762 let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?;
1763 let result = result.ok_or_else(|| de::Error::missing_field("result"))?;
1764 let result = serde_json::from_value::<ResultFromServer>(result).map_err(de::Error::custom)?;
1765 Ok(ServerJsonrpcResponse { id, jsonrpc, result })
1766 }
1767 }
1768 deserializer.deserialize_struct("JsonrpcResponse", &["id", "jsonrpc", "result"], ServerJsonrpcResultVisitor)
1769 }
1770}
1771impl ::serde::Serialize for ClientJsonrpcResponse {
1772 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
1773 where
1774 S: ::serde::Serializer,
1775 {
1776 let mut state = serializer.serialize_struct("JsonrpcResponse", 3)?;
1777 state.serialize_field("id", &self.id)?;
1778 state.serialize_field("jsonrpc", &self.jsonrpc)?;
1779 state.serialize_field("result", &self.result)?;
1780 state.end()
1781 }
1782}
1783impl<'de> ::serde::Deserialize<'de> for ClientJsonrpcResponse {
1784 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1785 where
1786 D: ::serde::Deserializer<'de>,
1787 {
1788 use serde::de::{self, MapAccess, Visitor};
1789 use std::fmt;
1790 struct ClientJsonrpcResultVisitor;
1791 impl<'de> Visitor<'de> for ClientJsonrpcResultVisitor {
1792 type Value = ClientJsonrpcResponse;
1793 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1794 formatter.write_str("a valid JSON-RPC response object")
1795 }
1796 fn visit_map<M>(self, mut map: M) -> std::result::Result<ClientJsonrpcResponse, M::Error>
1797 where
1798 M: MapAccess<'de>,
1799 {
1800 let mut id: Option<RequestId> = None;
1801 let mut jsonrpc: Option<String> = None;
1802 let mut result: Option<Value> = None;
1803 while let Some(key) = map.next_key::<String>()? {
1804 match key.as_str() {
1805 "id" => id = Some(map.next_value()?),
1806 "jsonrpc" => jsonrpc = Some(map.next_value()?),
1807 "result" => result = Some(map.next_value()?),
1808 _ => {
1809 return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "result"]));
1810 }
1811 }
1812 }
1813 let id = id.ok_or_else(|| de::Error::missing_field("id"))?;
1814 let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?;
1815 let result = result.ok_or_else(|| de::Error::missing_field("result"))?;
1816 let result = serde_json::from_value::<ResultFromClient>(result).map_err(de::Error::custom)?;
1817 Ok(ClientJsonrpcResponse { id, jsonrpc, result })
1818 }
1819 }
1820 deserializer.deserialize_struct("JsonrpcResponse", &["id", "jsonrpc", "result"], ClientJsonrpcResultVisitor)
1821 }
1822}
1823impl From<InitializeRequest> for RequestFromClient {
1824 fn from(value: InitializeRequest) -> Self {
1825 Self::ClientRequest(value.into())
1826 }
1827}
1828impl From<PingRequest> for RequestFromClient {
1829 fn from(value: PingRequest) -> Self {
1830 Self::ClientRequest(value.into())
1831 }
1832}
1833impl From<ListResourcesRequest> for RequestFromClient {
1834 fn from(value: ListResourcesRequest) -> Self {
1835 Self::ClientRequest(value.into())
1836 }
1837}
1838impl From<ListResourceTemplatesRequest> for RequestFromClient {
1839 fn from(value: ListResourceTemplatesRequest) -> Self {
1840 Self::ClientRequest(value.into())
1841 }
1842}
1843impl From<ReadResourceRequest> for RequestFromClient {
1844 fn from(value: ReadResourceRequest) -> Self {
1845 Self::ClientRequest(value.into())
1846 }
1847}
1848impl From<SubscribeRequest> for RequestFromClient {
1849 fn from(value: SubscribeRequest) -> Self {
1850 Self::ClientRequest(value.into())
1851 }
1852}
1853impl From<UnsubscribeRequest> for RequestFromClient {
1854 fn from(value: UnsubscribeRequest) -> Self {
1855 Self::ClientRequest(value.into())
1856 }
1857}
1858impl From<ListPromptsRequest> for RequestFromClient {
1859 fn from(value: ListPromptsRequest) -> Self {
1860 Self::ClientRequest(value.into())
1861 }
1862}
1863impl From<GetPromptRequest> for RequestFromClient {
1864 fn from(value: GetPromptRequest) -> Self {
1865 Self::ClientRequest(value.into())
1866 }
1867}
1868impl From<ListToolsRequest> for RequestFromClient {
1869 fn from(value: ListToolsRequest) -> Self {
1870 Self::ClientRequest(value.into())
1871 }
1872}
1873impl From<CallToolRequest> for RequestFromClient {
1874 fn from(value: CallToolRequest) -> Self {
1875 Self::ClientRequest(value.into())
1876 }
1877}
1878impl From<SetLevelRequest> for RequestFromClient {
1879 fn from(value: SetLevelRequest) -> Self {
1880 Self::ClientRequest(value.into())
1881 }
1882}
1883impl From<CompleteRequest> for RequestFromClient {
1884 fn from(value: CompleteRequest) -> Self {
1885 Self::ClientRequest(value.into())
1886 }
1887}
1888impl From<InitializeRequest> for MessageFromClient {
1889 fn from(value: InitializeRequest) -> Self {
1890 MessageFromClient::RequestFromClient(value.into())
1891 }
1892}
1893impl From<PingRequest> for MessageFromClient {
1894 fn from(value: PingRequest) -> Self {
1895 MessageFromClient::RequestFromClient(value.into())
1896 }
1897}
1898impl From<ListResourcesRequest> for MessageFromClient {
1899 fn from(value: ListResourcesRequest) -> Self {
1900 MessageFromClient::RequestFromClient(value.into())
1901 }
1902}
1903impl From<ListResourceTemplatesRequest> for MessageFromClient {
1904 fn from(value: ListResourceTemplatesRequest) -> Self {
1905 MessageFromClient::RequestFromClient(value.into())
1906 }
1907}
1908impl From<ReadResourceRequest> for MessageFromClient {
1909 fn from(value: ReadResourceRequest) -> Self {
1910 MessageFromClient::RequestFromClient(value.into())
1911 }
1912}
1913impl From<SubscribeRequest> for MessageFromClient {
1914 fn from(value: SubscribeRequest) -> Self {
1915 MessageFromClient::RequestFromClient(value.into())
1916 }
1917}
1918impl From<UnsubscribeRequest> for MessageFromClient {
1919 fn from(value: UnsubscribeRequest) -> Self {
1920 MessageFromClient::RequestFromClient(value.into())
1921 }
1922}
1923impl From<ListPromptsRequest> for MessageFromClient {
1924 fn from(value: ListPromptsRequest) -> Self {
1925 MessageFromClient::RequestFromClient(value.into())
1926 }
1927}
1928impl From<GetPromptRequest> for MessageFromClient {
1929 fn from(value: GetPromptRequest) -> Self {
1930 MessageFromClient::RequestFromClient(value.into())
1931 }
1932}
1933impl From<ListToolsRequest> for MessageFromClient {
1934 fn from(value: ListToolsRequest) -> Self {
1935 MessageFromClient::RequestFromClient(value.into())
1936 }
1937}
1938impl From<CallToolRequest> for MessageFromClient {
1939 fn from(value: CallToolRequest) -> Self {
1940 MessageFromClient::RequestFromClient(value.into())
1941 }
1942}
1943impl From<SetLevelRequest> for MessageFromClient {
1944 fn from(value: SetLevelRequest) -> Self {
1945 MessageFromClient::RequestFromClient(value.into())
1946 }
1947}
1948impl From<CompleteRequest> for MessageFromClient {
1949 fn from(value: CompleteRequest) -> Self {
1950 MessageFromClient::RequestFromClient(value.into())
1951 }
1952}
1953impl From<CancelledNotification> for NotificationFromClient {
1954 fn from(value: CancelledNotification) -> Self {
1955 Self::ClientNotification(value.into())
1956 }
1957}
1958impl From<InitializedNotification> for NotificationFromClient {
1959 fn from(value: InitializedNotification) -> Self {
1960 Self::ClientNotification(value.into())
1961 }
1962}
1963impl From<ProgressNotification> for NotificationFromClient {
1964 fn from(value: ProgressNotification) -> Self {
1965 Self::ClientNotification(value.into())
1966 }
1967}
1968impl From<RootsListChangedNotification> for NotificationFromClient {
1969 fn from(value: RootsListChangedNotification) -> Self {
1970 Self::ClientNotification(value.into())
1971 }
1972}
1973impl From<CancelledNotification> for ClientJsonrpcNotification {
1974 fn from(value: CancelledNotification) -> Self {
1975 Self::new(value.into())
1976 }
1977}
1978impl From<InitializedNotification> for ClientJsonrpcNotification {
1979 fn from(value: InitializedNotification) -> Self {
1980 Self::new(value.into())
1981 }
1982}
1983impl From<ProgressNotification> for ClientJsonrpcNotification {
1984 fn from(value: ProgressNotification) -> Self {
1985 Self::new(value.into())
1986 }
1987}
1988impl From<RootsListChangedNotification> for ClientJsonrpcNotification {
1989 fn from(value: RootsListChangedNotification) -> Self {
1990 Self::new(value.into())
1991 }
1992}
1993impl From<CancelledNotification> for MessageFromClient {
1994 fn from(value: CancelledNotification) -> Self {
1995 MessageFromClient::NotificationFromClient(value.into())
1996 }
1997}
1998impl From<InitializedNotification> for MessageFromClient {
1999 fn from(value: InitializedNotification) -> Self {
2000 MessageFromClient::NotificationFromClient(value.into())
2001 }
2002}
2003impl From<ProgressNotification> for MessageFromClient {
2004 fn from(value: ProgressNotification) -> Self {
2005 MessageFromClient::NotificationFromClient(value.into())
2006 }
2007}
2008impl From<RootsListChangedNotification> for MessageFromClient {
2009 fn from(value: RootsListChangedNotification) -> Self {
2010 MessageFromClient::NotificationFromClient(value.into())
2011 }
2012}
2013impl From<Result> for ResultFromClient {
2014 fn from(value: Result) -> Self {
2015 Self::ClientResult(value.into())
2016 }
2017}
2018impl From<CreateMessageResult> for ResultFromClient {
2019 fn from(value: CreateMessageResult) -> Self {
2020 Self::ClientResult(value.into())
2021 }
2022}
2023impl From<ListRootsResult> for ResultFromClient {
2024 fn from(value: ListRootsResult) -> Self {
2025 Self::ClientResult(value.into())
2026 }
2027}
2028impl From<Result> for MessageFromClient {
2029 fn from(value: Result) -> Self {
2030 MessageFromClient::ResultFromClient(value.into())
2031 }
2032}
2033impl From<CreateMessageResult> for MessageFromClient {
2034 fn from(value: CreateMessageResult) -> Self {
2035 MessageFromClient::ResultFromClient(value.into())
2036 }
2037}
2038impl From<ListRootsResult> for MessageFromClient {
2039 fn from(value: ListRootsResult) -> Self {
2040 MessageFromClient::ResultFromClient(value.into())
2041 }
2042}
2043#[allow(non_camel_case_types)]
2045pub enum SdkErrorCodes {
2046 CONNECTION_CLOSED = -32000,
2047 REQUEST_TIMEOUT = -32001,
2048}
2049impl core::fmt::Display for SdkErrorCodes {
2050 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2051 match self {
2052 SdkErrorCodes::CONNECTION_CLOSED => write!(f, "Connection closed"),
2053 SdkErrorCodes::REQUEST_TIMEOUT => write!(f, "Request timeout"),
2054 }
2055 }
2056}
2057impl From<SdkErrorCodes> for i64 {
2058 fn from(code: SdkErrorCodes) -> Self {
2059 code as i64
2060 }
2061}
2062#[derive(Debug)]
2063pub struct SdkError {
2064 pub code: i64,
2066 pub data: ::std::option::Option<::serde_json::Value>,
2068 pub message: ::std::string::String,
2070}
2071impl core::fmt::Display for SdkError {
2072 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2073 write!(f, "MCP error {}: {}", self.code, self.message)
2074 }
2075}
2076impl std::error::Error for SdkError {
2077 fn description(&self) -> &str {
2078 &self.message
2079 }
2080}
2081impl SdkError {
2082 pub fn new(
2083 error_code: SdkErrorCodes,
2084 message: ::std::string::String,
2085 data: ::std::option::Option<::serde_json::Value>,
2086 ) -> Self {
2087 Self {
2088 code: error_code.into(),
2089 data,
2090 message,
2091 }
2092 }
2093 pub fn connection_closed() -> Self {
2094 Self {
2095 code: SdkErrorCodes::CONNECTION_CLOSED.into(),
2096 data: None,
2097 message: SdkErrorCodes::CONNECTION_CLOSED.to_string(),
2098 }
2099 }
2100 pub fn request_timeout(timeout: u128) -> Self {
2101 Self {
2102 code: SdkErrorCodes::REQUEST_TIMEOUT.into(),
2103 data: Some(json!({ "timeout" : timeout })),
2104 message: SdkErrorCodes::REQUEST_TIMEOUT.to_string(),
2105 }
2106 }
2107}
2108#[allow(non_camel_case_types)]
2110pub enum RpcErrorCodes {
2111 PARSE_ERROR = -32700isize,
2112 INVALID_REQUEST = -32600isize,
2113 METHOD_NOT_FOUND = -32601isize,
2114 INVALID_PARAMS = -32602isize,
2115 INTERNAL_ERROR = -32603isize,
2116}
2117impl From<RpcErrorCodes> for i64 {
2118 fn from(code: RpcErrorCodes) -> Self {
2119 code as i64
2120 }
2121}
2122impl JsonrpcErrorError {
2123 pub fn new(
2140 error_code: RpcErrorCodes,
2141 message: ::std::string::String,
2142 data: ::std::option::Option<::serde_json::Value>,
2143 ) -> Self {
2144 Self {
2145 code: error_code.into(),
2146 data,
2147 message,
2148 }
2149 }
2150 pub fn method_not_found() -> Self {
2161 Self {
2162 code: RpcErrorCodes::METHOD_NOT_FOUND.into(),
2163 data: None,
2164 message: "Method not found".to_string(),
2165 }
2166 }
2167 pub fn invalid_params() -> Self {
2177 Self {
2178 code: RpcErrorCodes::INVALID_PARAMS.into(),
2179 data: None,
2180 message: "Invalid params".to_string(),
2181 }
2182 }
2183 pub fn invalid_request() -> Self {
2193 Self {
2194 code: RpcErrorCodes::INVALID_REQUEST.into(),
2195 data: None,
2196 message: "Invalid request".to_string(),
2197 }
2198 }
2199 pub fn internal_error() -> Self {
2209 Self {
2210 code: RpcErrorCodes::INTERNAL_ERROR.into(),
2211 data: None,
2212 message: "Internal error".to_string(),
2213 }
2214 }
2215 pub fn parse_error() -> Self {
2225 Self {
2226 code: RpcErrorCodes::PARSE_ERROR.into(),
2227 data: None,
2228 message: "Parse error".to_string(),
2229 }
2230 }
2231 pub fn with_message(mut self, message: String) -> Self {
2241 self.message = message;
2242 self
2243 }
2244 pub fn with_data(mut self, data: ::std::option::Option<::serde_json::Value>) -> Self {
2255 self.data = data;
2256 self
2257 }
2258}
2259impl std::error::Error for JsonrpcErrorError {
2260 fn description(&self) -> &str {
2261 &self.message
2262 }
2263}
2264impl Display for JsonrpcErrorError {
2265 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2266 write!(
2267 f,
2268 "{}",
2269 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
2270 )
2271 }
2272}
2273impl FromStr for JsonrpcErrorError {
2274 type Err = JsonrpcErrorError;
2275 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
2276 serde_json::from_str(s)
2277 .map_err(|error| JsonrpcErrorError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
2278 }
2279}
2280impl JsonrpcError {
2282 pub fn create(
2283 id: RequestId,
2284 error_code: RpcErrorCodes,
2285 error_message: ::std::string::String,
2286 error_data: ::std::option::Option<::serde_json::Value>,
2287 ) -> Self {
2288 Self::new(JsonrpcErrorError::new(error_code, error_message, error_data), id)
2289 }
2290}
2291impl From<CancelledNotification> for NotificationFromServer {
2292 fn from(value: CancelledNotification) -> Self {
2293 Self::ServerNotification(value.into())
2294 }
2295}
2296impl From<ProgressNotification> for NotificationFromServer {
2297 fn from(value: ProgressNotification) -> Self {
2298 Self::ServerNotification(value.into())
2299 }
2300}
2301impl From<ResourceListChangedNotification> for NotificationFromServer {
2302 fn from(value: ResourceListChangedNotification) -> Self {
2303 Self::ServerNotification(value.into())
2304 }
2305}
2306impl From<ResourceUpdatedNotification> for NotificationFromServer {
2307 fn from(value: ResourceUpdatedNotification) -> Self {
2308 Self::ServerNotification(value.into())
2309 }
2310}
2311impl From<PromptListChangedNotification> for NotificationFromServer {
2312 fn from(value: PromptListChangedNotification) -> Self {
2313 Self::ServerNotification(value.into())
2314 }
2315}
2316impl From<ToolListChangedNotification> for NotificationFromServer {
2317 fn from(value: ToolListChangedNotification) -> Self {
2318 Self::ServerNotification(value.into())
2319 }
2320}
2321impl From<LoggingMessageNotification> for NotificationFromServer {
2322 fn from(value: LoggingMessageNotification) -> Self {
2323 Self::ServerNotification(value.into())
2324 }
2325}
2326impl From<CancelledNotification> for ServerJsonrpcNotification {
2327 fn from(value: CancelledNotification) -> Self {
2328 Self::new(value.into())
2329 }
2330}
2331impl From<ProgressNotification> for ServerJsonrpcNotification {
2332 fn from(value: ProgressNotification) -> Self {
2333 Self::new(value.into())
2334 }
2335}
2336impl From<ResourceListChangedNotification> for ServerJsonrpcNotification {
2337 fn from(value: ResourceListChangedNotification) -> Self {
2338 Self::new(value.into())
2339 }
2340}
2341impl From<ResourceUpdatedNotification> for ServerJsonrpcNotification {
2342 fn from(value: ResourceUpdatedNotification) -> Self {
2343 Self::new(value.into())
2344 }
2345}
2346impl From<PromptListChangedNotification> for ServerJsonrpcNotification {
2347 fn from(value: PromptListChangedNotification) -> Self {
2348 Self::new(value.into())
2349 }
2350}
2351impl From<ToolListChangedNotification> for ServerJsonrpcNotification {
2352 fn from(value: ToolListChangedNotification) -> Self {
2353 Self::new(value.into())
2354 }
2355}
2356impl From<LoggingMessageNotification> for ServerJsonrpcNotification {
2357 fn from(value: LoggingMessageNotification) -> Self {
2358 Self::new(value.into())
2359 }
2360}
2361impl From<CancelledNotification> for MessageFromServer {
2362 fn from(value: CancelledNotification) -> Self {
2363 MessageFromServer::NotificationFromServer(value.into())
2364 }
2365}
2366impl From<ProgressNotification> for MessageFromServer {
2367 fn from(value: ProgressNotification) -> Self {
2368 MessageFromServer::NotificationFromServer(value.into())
2369 }
2370}
2371impl From<ResourceListChangedNotification> for MessageFromServer {
2372 fn from(value: ResourceListChangedNotification) -> Self {
2373 MessageFromServer::NotificationFromServer(value.into())
2374 }
2375}
2376impl From<ResourceUpdatedNotification> for MessageFromServer {
2377 fn from(value: ResourceUpdatedNotification) -> Self {
2378 MessageFromServer::NotificationFromServer(value.into())
2379 }
2380}
2381impl From<PromptListChangedNotification> for MessageFromServer {
2382 fn from(value: PromptListChangedNotification) -> Self {
2383 MessageFromServer::NotificationFromServer(value.into())
2384 }
2385}
2386impl From<ToolListChangedNotification> for MessageFromServer {
2387 fn from(value: ToolListChangedNotification) -> Self {
2388 MessageFromServer::NotificationFromServer(value.into())
2389 }
2390}
2391impl From<LoggingMessageNotification> for MessageFromServer {
2392 fn from(value: LoggingMessageNotification) -> Self {
2393 MessageFromServer::NotificationFromServer(value.into())
2394 }
2395}
2396impl From<PingRequest> for RequestFromServer {
2397 fn from(value: PingRequest) -> Self {
2398 Self::ServerRequest(value.into())
2399 }
2400}
2401impl From<CreateMessageRequest> for RequestFromServer {
2402 fn from(value: CreateMessageRequest) -> Self {
2403 Self::ServerRequest(value.into())
2404 }
2405}
2406impl From<ListRootsRequest> for RequestFromServer {
2407 fn from(value: ListRootsRequest) -> Self {
2408 Self::ServerRequest(value.into())
2409 }
2410}
2411impl From<PingRequest> for MessageFromServer {
2412 fn from(value: PingRequest) -> Self {
2413 MessageFromServer::RequestFromServer(value.into())
2414 }
2415}
2416impl From<CreateMessageRequest> for MessageFromServer {
2417 fn from(value: CreateMessageRequest) -> Self {
2418 MessageFromServer::RequestFromServer(value.into())
2419 }
2420}
2421impl From<ListRootsRequest> for MessageFromServer {
2422 fn from(value: ListRootsRequest) -> Self {
2423 MessageFromServer::RequestFromServer(value.into())
2424 }
2425}
2426impl From<Result> for ResultFromServer {
2427 fn from(value: Result) -> Self {
2428 Self::ServerResult(value.into())
2429 }
2430}
2431impl From<InitializeResult> for ResultFromServer {
2432 fn from(value: InitializeResult) -> Self {
2433 Self::ServerResult(value.into())
2434 }
2435}
2436impl From<ListResourcesResult> for ResultFromServer {
2437 fn from(value: ListResourcesResult) -> Self {
2438 Self::ServerResult(value.into())
2439 }
2440}
2441impl From<ListResourceTemplatesResult> for ResultFromServer {
2442 fn from(value: ListResourceTemplatesResult) -> Self {
2443 Self::ServerResult(value.into())
2444 }
2445}
2446impl From<ReadResourceResult> for ResultFromServer {
2447 fn from(value: ReadResourceResult) -> Self {
2448 Self::ServerResult(value.into())
2449 }
2450}
2451impl From<ListPromptsResult> for ResultFromServer {
2452 fn from(value: ListPromptsResult) -> Self {
2453 Self::ServerResult(value.into())
2454 }
2455}
2456impl From<GetPromptResult> for ResultFromServer {
2457 fn from(value: GetPromptResult) -> Self {
2458 Self::ServerResult(value.into())
2459 }
2460}
2461impl From<ListToolsResult> for ResultFromServer {
2462 fn from(value: ListToolsResult) -> Self {
2463 Self::ServerResult(value.into())
2464 }
2465}
2466impl From<CallToolResult> for ResultFromServer {
2467 fn from(value: CallToolResult) -> Self {
2468 Self::ServerResult(value.into())
2469 }
2470}
2471impl From<CompleteResult> for ResultFromServer {
2472 fn from(value: CompleteResult) -> Self {
2473 Self::ServerResult(value.into())
2474 }
2475}
2476impl From<Result> for MessageFromServer {
2477 fn from(value: Result) -> Self {
2478 MessageFromServer::ResultFromServer(value.into())
2479 }
2480}
2481impl From<InitializeResult> for MessageFromServer {
2482 fn from(value: InitializeResult) -> Self {
2483 MessageFromServer::ResultFromServer(value.into())
2484 }
2485}
2486impl From<ListResourcesResult> for MessageFromServer {
2487 fn from(value: ListResourcesResult) -> Self {
2488 MessageFromServer::ResultFromServer(value.into())
2489 }
2490}
2491impl From<ListResourceTemplatesResult> for MessageFromServer {
2492 fn from(value: ListResourceTemplatesResult) -> Self {
2493 MessageFromServer::ResultFromServer(value.into())
2494 }
2495}
2496impl From<ReadResourceResult> for MessageFromServer {
2497 fn from(value: ReadResourceResult) -> Self {
2498 MessageFromServer::ResultFromServer(value.into())
2499 }
2500}
2501impl From<ListPromptsResult> for MessageFromServer {
2502 fn from(value: ListPromptsResult) -> Self {
2503 MessageFromServer::ResultFromServer(value.into())
2504 }
2505}
2506impl From<GetPromptResult> for MessageFromServer {
2507 fn from(value: GetPromptResult) -> Self {
2508 MessageFromServer::ResultFromServer(value.into())
2509 }
2510}
2511impl From<ListToolsResult> for MessageFromServer {
2512 fn from(value: ListToolsResult) -> Self {
2513 MessageFromServer::ResultFromServer(value.into())
2514 }
2515}
2516impl From<CallToolResult> for MessageFromServer {
2517 fn from(value: CallToolResult) -> Self {
2518 MessageFromServer::ResultFromServer(value.into())
2519 }
2520}
2521impl From<CompleteResult> for MessageFromServer {
2522 fn from(value: CompleteResult) -> Self {
2523 MessageFromServer::ResultFromServer(value.into())
2524 }
2525}
2526impl FromMessage<InitializeRequest> for ClientMessage {
2527 fn from_message(
2528 message: InitializeRequest,
2529 request_id: Option<RequestId>,
2530 ) -> std::result::Result<Self, JsonrpcErrorError> {
2531 let request_id =
2532 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2533 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2534 }
2535}
2536impl ToMessage<ClientMessage> for InitializeRequest {
2537 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2538 ClientMessage::from_message(self, request_id)
2539 }
2540}
2541impl FromMessage<PingRequest> for ClientMessage {
2542 fn from_message(message: PingRequest, request_id: Option<RequestId>) -> std::result::Result<Self, JsonrpcErrorError> {
2543 let request_id =
2544 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2545 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2546 }
2547}
2548impl ToMessage<ClientMessage> for PingRequest {
2549 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2550 ClientMessage::from_message(self, request_id)
2551 }
2552}
2553impl FromMessage<ListResourcesRequest> for ClientMessage {
2554 fn from_message(
2555 message: ListResourcesRequest,
2556 request_id: Option<RequestId>,
2557 ) -> std::result::Result<Self, JsonrpcErrorError> {
2558 let request_id =
2559 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2560 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2561 }
2562}
2563impl ToMessage<ClientMessage> for ListResourcesRequest {
2564 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2565 ClientMessage::from_message(self, request_id)
2566 }
2567}
2568impl FromMessage<ListResourceTemplatesRequest> for ClientMessage {
2569 fn from_message(
2570 message: ListResourceTemplatesRequest,
2571 request_id: Option<RequestId>,
2572 ) -> std::result::Result<Self, JsonrpcErrorError> {
2573 let request_id =
2574 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2575 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2576 }
2577}
2578impl ToMessage<ClientMessage> for ListResourceTemplatesRequest {
2579 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2580 ClientMessage::from_message(self, request_id)
2581 }
2582}
2583impl FromMessage<ReadResourceRequest> for ClientMessage {
2584 fn from_message(
2585 message: ReadResourceRequest,
2586 request_id: Option<RequestId>,
2587 ) -> std::result::Result<Self, JsonrpcErrorError> {
2588 let request_id =
2589 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2590 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2591 }
2592}
2593impl ToMessage<ClientMessage> for ReadResourceRequest {
2594 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2595 ClientMessage::from_message(self, request_id)
2596 }
2597}
2598impl FromMessage<SubscribeRequest> for ClientMessage {
2599 fn from_message(
2600 message: SubscribeRequest,
2601 request_id: Option<RequestId>,
2602 ) -> std::result::Result<Self, JsonrpcErrorError> {
2603 let request_id =
2604 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2605 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2606 }
2607}
2608impl ToMessage<ClientMessage> for SubscribeRequest {
2609 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2610 ClientMessage::from_message(self, request_id)
2611 }
2612}
2613impl FromMessage<UnsubscribeRequest> for ClientMessage {
2614 fn from_message(
2615 message: UnsubscribeRequest,
2616 request_id: Option<RequestId>,
2617 ) -> std::result::Result<Self, JsonrpcErrorError> {
2618 let request_id =
2619 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2620 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2621 }
2622}
2623impl ToMessage<ClientMessage> for UnsubscribeRequest {
2624 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2625 ClientMessage::from_message(self, request_id)
2626 }
2627}
2628impl FromMessage<ListPromptsRequest> for ClientMessage {
2629 fn from_message(
2630 message: ListPromptsRequest,
2631 request_id: Option<RequestId>,
2632 ) -> std::result::Result<Self, JsonrpcErrorError> {
2633 let request_id =
2634 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2635 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2636 }
2637}
2638impl ToMessage<ClientMessage> for ListPromptsRequest {
2639 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2640 ClientMessage::from_message(self, request_id)
2641 }
2642}
2643impl FromMessage<GetPromptRequest> for ClientMessage {
2644 fn from_message(
2645 message: GetPromptRequest,
2646 request_id: Option<RequestId>,
2647 ) -> std::result::Result<Self, JsonrpcErrorError> {
2648 let request_id =
2649 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2650 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2651 }
2652}
2653impl ToMessage<ClientMessage> for GetPromptRequest {
2654 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2655 ClientMessage::from_message(self, request_id)
2656 }
2657}
2658impl FromMessage<ListToolsRequest> for ClientMessage {
2659 fn from_message(
2660 message: ListToolsRequest,
2661 request_id: Option<RequestId>,
2662 ) -> std::result::Result<Self, JsonrpcErrorError> {
2663 let request_id =
2664 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2665 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2666 }
2667}
2668impl ToMessage<ClientMessage> for ListToolsRequest {
2669 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2670 ClientMessage::from_message(self, request_id)
2671 }
2672}
2673impl FromMessage<CallToolRequest> for ClientMessage {
2674 fn from_message(
2675 message: CallToolRequest,
2676 request_id: Option<RequestId>,
2677 ) -> std::result::Result<Self, JsonrpcErrorError> {
2678 let request_id =
2679 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2680 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2681 }
2682}
2683impl ToMessage<ClientMessage> for CallToolRequest {
2684 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2685 ClientMessage::from_message(self, request_id)
2686 }
2687}
2688impl FromMessage<SetLevelRequest> for ClientMessage {
2689 fn from_message(
2690 message: SetLevelRequest,
2691 request_id: Option<RequestId>,
2692 ) -> std::result::Result<Self, JsonrpcErrorError> {
2693 let request_id =
2694 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2695 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2696 }
2697}
2698impl ToMessage<ClientMessage> for SetLevelRequest {
2699 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2700 ClientMessage::from_message(self, request_id)
2701 }
2702}
2703impl FromMessage<CompleteRequest> for ClientMessage {
2704 fn from_message(
2705 message: CompleteRequest,
2706 request_id: Option<RequestId>,
2707 ) -> std::result::Result<Self, JsonrpcErrorError> {
2708 let request_id =
2709 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2710 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2711 }
2712}
2713impl ToMessage<ClientMessage> for CompleteRequest {
2714 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2715 ClientMessage::from_message(self, request_id)
2716 }
2717}
2718impl FromMessage<Result> for ClientMessage {
2719 fn from_message(message: Result, request_id: Option<RequestId>) -> std::result::Result<Self, JsonrpcErrorError> {
2720 let request_id =
2721 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2722 Ok(ClientMessage::Response(ClientJsonrpcResponse::new(
2723 request_id,
2724 message.into(),
2725 )))
2726 }
2727}
2728impl ToMessage<ClientMessage> for Result {
2729 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2730 ClientMessage::from_message(self, request_id)
2731 }
2732}
2733impl FromMessage<CreateMessageResult> for ClientMessage {
2734 fn from_message(
2735 message: CreateMessageResult,
2736 request_id: Option<RequestId>,
2737 ) -> std::result::Result<Self, JsonrpcErrorError> {
2738 let request_id =
2739 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2740 Ok(ClientMessage::Response(ClientJsonrpcResponse::new(
2741 request_id,
2742 message.into(),
2743 )))
2744 }
2745}
2746impl ToMessage<ClientMessage> for CreateMessageResult {
2747 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2748 ClientMessage::from_message(self, request_id)
2749 }
2750}
2751impl FromMessage<ListRootsResult> for ClientMessage {
2752 fn from_message(
2753 message: ListRootsResult,
2754 request_id: Option<RequestId>,
2755 ) -> std::result::Result<Self, JsonrpcErrorError> {
2756 let request_id =
2757 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2758 Ok(ClientMessage::Response(ClientJsonrpcResponse::new(
2759 request_id,
2760 message.into(),
2761 )))
2762 }
2763}
2764impl ToMessage<ClientMessage> for ListRootsResult {
2765 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2766 ClientMessage::from_message(self, request_id)
2767 }
2768}
2769impl FromMessage<CancelledNotification> for ClientMessage {
2770 fn from_message(
2771 message: CancelledNotification,
2772 request_id: Option<RequestId>,
2773 ) -> std::result::Result<Self, JsonrpcErrorError> {
2774 if request_id.is_some() {
2775 return Err(JsonrpcErrorError::internal_error()
2776 .with_message("request_id expected to be None for Notifications!".to_string()));
2777 }
2778 Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into())))
2779 }
2780}
2781impl ToMessage<ClientMessage> for CancelledNotification {
2782 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2783 ClientMessage::from_message(self, request_id)
2784 }
2785}
2786impl FromMessage<InitializedNotification> for ClientMessage {
2787 fn from_message(
2788 message: InitializedNotification,
2789 request_id: Option<RequestId>,
2790 ) -> std::result::Result<Self, JsonrpcErrorError> {
2791 if request_id.is_some() {
2792 return Err(JsonrpcErrorError::internal_error()
2793 .with_message("request_id expected to be None for Notifications!".to_string()));
2794 }
2795 Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into())))
2796 }
2797}
2798impl ToMessage<ClientMessage> for InitializedNotification {
2799 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2800 ClientMessage::from_message(self, request_id)
2801 }
2802}
2803impl FromMessage<ProgressNotification> for ClientMessage {
2804 fn from_message(
2805 message: ProgressNotification,
2806 request_id: Option<RequestId>,
2807 ) -> std::result::Result<Self, JsonrpcErrorError> {
2808 if request_id.is_some() {
2809 return Err(JsonrpcErrorError::internal_error()
2810 .with_message("request_id expected to be None for Notifications!".to_string()));
2811 }
2812 Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into())))
2813 }
2814}
2815impl ToMessage<ClientMessage> for ProgressNotification {
2816 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2817 ClientMessage::from_message(self, request_id)
2818 }
2819}
2820impl FromMessage<RootsListChangedNotification> for ClientMessage {
2821 fn from_message(
2822 message: RootsListChangedNotification,
2823 request_id: Option<RequestId>,
2824 ) -> std::result::Result<Self, JsonrpcErrorError> {
2825 if request_id.is_some() {
2826 return Err(JsonrpcErrorError::internal_error()
2827 .with_message("request_id expected to be None for Notifications!".to_string()));
2828 }
2829 Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into())))
2830 }
2831}
2832impl ToMessage<ClientMessage> for RootsListChangedNotification {
2833 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, JsonrpcErrorError> {
2834 ClientMessage::from_message(self, request_id)
2835 }
2836}
2837impl FromMessage<PingRequest> for ServerMessage {
2838 fn from_message(message: PingRequest, request_id: Option<RequestId>) -> std::result::Result<Self, JsonrpcErrorError> {
2839 let request_id =
2840 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2841 Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into())))
2842 }
2843}
2844impl ToMessage<ServerMessage> for PingRequest {
2845 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
2846 ServerMessage::from_message(self, request_id)
2847 }
2848}
2849impl FromMessage<CreateMessageRequest> for ServerMessage {
2850 fn from_message(
2851 message: CreateMessageRequest,
2852 request_id: Option<RequestId>,
2853 ) -> std::result::Result<Self, JsonrpcErrorError> {
2854 let request_id =
2855 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2856 Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into())))
2857 }
2858}
2859impl ToMessage<ServerMessage> for CreateMessageRequest {
2860 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
2861 ServerMessage::from_message(self, request_id)
2862 }
2863}
2864impl FromMessage<ListRootsRequest> for ServerMessage {
2865 fn from_message(
2866 message: ListRootsRequest,
2867 request_id: Option<RequestId>,
2868 ) -> std::result::Result<Self, JsonrpcErrorError> {
2869 let request_id =
2870 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2871 Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into())))
2872 }
2873}
2874impl ToMessage<ServerMessage> for ListRootsRequest {
2875 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
2876 ServerMessage::from_message(self, request_id)
2877 }
2878}
2879impl FromMessage<Result> for ServerMessage {
2880 fn from_message(message: Result, request_id: Option<RequestId>) -> std::result::Result<Self, JsonrpcErrorError> {
2881 let request_id =
2882 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2883 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
2884 request_id,
2885 message.into(),
2886 )))
2887 }
2888}
2889impl ToMessage<ServerMessage> for Result {
2890 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
2891 ServerMessage::from_message(self, request_id)
2892 }
2893}
2894impl FromMessage<InitializeResult> for ServerMessage {
2895 fn from_message(
2896 message: InitializeResult,
2897 request_id: Option<RequestId>,
2898 ) -> std::result::Result<Self, JsonrpcErrorError> {
2899 let request_id =
2900 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2901 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
2902 request_id,
2903 message.into(),
2904 )))
2905 }
2906}
2907impl ToMessage<ServerMessage> for InitializeResult {
2908 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
2909 ServerMessage::from_message(self, request_id)
2910 }
2911}
2912impl FromMessage<ListResourcesResult> for ServerMessage {
2913 fn from_message(
2914 message: ListResourcesResult,
2915 request_id: Option<RequestId>,
2916 ) -> std::result::Result<Self, JsonrpcErrorError> {
2917 let request_id =
2918 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2919 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
2920 request_id,
2921 message.into(),
2922 )))
2923 }
2924}
2925impl ToMessage<ServerMessage> for ListResourcesResult {
2926 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
2927 ServerMessage::from_message(self, request_id)
2928 }
2929}
2930impl FromMessage<ListResourceTemplatesResult> for ServerMessage {
2931 fn from_message(
2932 message: ListResourceTemplatesResult,
2933 request_id: Option<RequestId>,
2934 ) -> std::result::Result<Self, JsonrpcErrorError> {
2935 let request_id =
2936 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2937 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
2938 request_id,
2939 message.into(),
2940 )))
2941 }
2942}
2943impl ToMessage<ServerMessage> for ListResourceTemplatesResult {
2944 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
2945 ServerMessage::from_message(self, request_id)
2946 }
2947}
2948impl FromMessage<ReadResourceResult> for ServerMessage {
2949 fn from_message(
2950 message: ReadResourceResult,
2951 request_id: Option<RequestId>,
2952 ) -> std::result::Result<Self, JsonrpcErrorError> {
2953 let request_id =
2954 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2955 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
2956 request_id,
2957 message.into(),
2958 )))
2959 }
2960}
2961impl ToMessage<ServerMessage> for ReadResourceResult {
2962 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
2963 ServerMessage::from_message(self, request_id)
2964 }
2965}
2966impl FromMessage<ListPromptsResult> for ServerMessage {
2967 fn from_message(
2968 message: ListPromptsResult,
2969 request_id: Option<RequestId>,
2970 ) -> std::result::Result<Self, JsonrpcErrorError> {
2971 let request_id =
2972 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2973 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
2974 request_id,
2975 message.into(),
2976 )))
2977 }
2978}
2979impl ToMessage<ServerMessage> for ListPromptsResult {
2980 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
2981 ServerMessage::from_message(self, request_id)
2982 }
2983}
2984impl FromMessage<GetPromptResult> for ServerMessage {
2985 fn from_message(
2986 message: GetPromptResult,
2987 request_id: Option<RequestId>,
2988 ) -> std::result::Result<Self, JsonrpcErrorError> {
2989 let request_id =
2990 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
2991 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
2992 request_id,
2993 message.into(),
2994 )))
2995 }
2996}
2997impl ToMessage<ServerMessage> for GetPromptResult {
2998 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
2999 ServerMessage::from_message(self, request_id)
3000 }
3001}
3002impl FromMessage<ListToolsResult> for ServerMessage {
3003 fn from_message(
3004 message: ListToolsResult,
3005 request_id: Option<RequestId>,
3006 ) -> std::result::Result<Self, JsonrpcErrorError> {
3007 let request_id =
3008 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
3009 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
3010 request_id,
3011 message.into(),
3012 )))
3013 }
3014}
3015impl ToMessage<ServerMessage> for ListToolsResult {
3016 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
3017 ServerMessage::from_message(self, request_id)
3018 }
3019}
3020impl FromMessage<CallToolResult> for ServerMessage {
3021 fn from_message(message: CallToolResult, request_id: Option<RequestId>) -> std::result::Result<Self, JsonrpcErrorError> {
3022 let request_id =
3023 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
3024 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
3025 request_id,
3026 message.into(),
3027 )))
3028 }
3029}
3030impl ToMessage<ServerMessage> for CallToolResult {
3031 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
3032 ServerMessage::from_message(self, request_id)
3033 }
3034}
3035impl FromMessage<CompleteResult> for ServerMessage {
3036 fn from_message(message: CompleteResult, request_id: Option<RequestId>) -> std::result::Result<Self, JsonrpcErrorError> {
3037 let request_id =
3038 request_id.ok_or_else(|| JsonrpcErrorError::internal_error().with_message("request_id is None!".to_string()))?;
3039 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
3040 request_id,
3041 message.into(),
3042 )))
3043 }
3044}
3045impl ToMessage<ServerMessage> for CompleteResult {
3046 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
3047 ServerMessage::from_message(self, request_id)
3048 }
3049}
3050impl FromMessage<CancelledNotification> for ServerMessage {
3051 fn from_message(
3052 message: CancelledNotification,
3053 request_id: Option<RequestId>,
3054 ) -> std::result::Result<Self, JsonrpcErrorError> {
3055 if request_id.is_some() {
3056 return Err(JsonrpcErrorError::internal_error()
3057 .with_message("request_id expected to be None for Notifications!".to_string()));
3058 }
3059 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3060 }
3061}
3062impl ToMessage<ServerMessage> for CancelledNotification {
3063 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
3064 ServerMessage::from_message(self, request_id)
3065 }
3066}
3067impl FromMessage<ProgressNotification> for ServerMessage {
3068 fn from_message(
3069 message: ProgressNotification,
3070 request_id: Option<RequestId>,
3071 ) -> std::result::Result<Self, JsonrpcErrorError> {
3072 if request_id.is_some() {
3073 return Err(JsonrpcErrorError::internal_error()
3074 .with_message("request_id expected to be None for Notifications!".to_string()));
3075 }
3076 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3077 }
3078}
3079impl ToMessage<ServerMessage> for ProgressNotification {
3080 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
3081 ServerMessage::from_message(self, request_id)
3082 }
3083}
3084impl FromMessage<ResourceListChangedNotification> for ServerMessage {
3085 fn from_message(
3086 message: ResourceListChangedNotification,
3087 request_id: Option<RequestId>,
3088 ) -> std::result::Result<Self, JsonrpcErrorError> {
3089 if request_id.is_some() {
3090 return Err(JsonrpcErrorError::internal_error()
3091 .with_message("request_id expected to be None for Notifications!".to_string()));
3092 }
3093 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3094 }
3095}
3096impl ToMessage<ServerMessage> for ResourceListChangedNotification {
3097 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
3098 ServerMessage::from_message(self, request_id)
3099 }
3100}
3101impl FromMessage<ResourceUpdatedNotification> for ServerMessage {
3102 fn from_message(
3103 message: ResourceUpdatedNotification,
3104 request_id: Option<RequestId>,
3105 ) -> std::result::Result<Self, JsonrpcErrorError> {
3106 if request_id.is_some() {
3107 return Err(JsonrpcErrorError::internal_error()
3108 .with_message("request_id expected to be None for Notifications!".to_string()));
3109 }
3110 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3111 }
3112}
3113impl ToMessage<ServerMessage> for ResourceUpdatedNotification {
3114 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
3115 ServerMessage::from_message(self, request_id)
3116 }
3117}
3118impl FromMessage<PromptListChangedNotification> for ServerMessage {
3119 fn from_message(
3120 message: PromptListChangedNotification,
3121 request_id: Option<RequestId>,
3122 ) -> std::result::Result<Self, JsonrpcErrorError> {
3123 if request_id.is_some() {
3124 return Err(JsonrpcErrorError::internal_error()
3125 .with_message("request_id expected to be None for Notifications!".to_string()));
3126 }
3127 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3128 }
3129}
3130impl ToMessage<ServerMessage> for PromptListChangedNotification {
3131 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
3132 ServerMessage::from_message(self, request_id)
3133 }
3134}
3135impl FromMessage<ToolListChangedNotification> for ServerMessage {
3136 fn from_message(
3137 message: ToolListChangedNotification,
3138 request_id: Option<RequestId>,
3139 ) -> std::result::Result<Self, JsonrpcErrorError> {
3140 if request_id.is_some() {
3141 return Err(JsonrpcErrorError::internal_error()
3142 .with_message("request_id expected to be None for Notifications!".to_string()));
3143 }
3144 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3145 }
3146}
3147impl ToMessage<ServerMessage> for ToolListChangedNotification {
3148 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
3149 ServerMessage::from_message(self, request_id)
3150 }
3151}
3152impl FromMessage<LoggingMessageNotification> for ServerMessage {
3153 fn from_message(
3154 message: LoggingMessageNotification,
3155 request_id: Option<RequestId>,
3156 ) -> std::result::Result<Self, JsonrpcErrorError> {
3157 if request_id.is_some() {
3158 return Err(JsonrpcErrorError::internal_error()
3159 .with_message("request_id expected to be None for Notifications!".to_string()));
3160 }
3161 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3162 }
3163}
3164impl ToMessage<ServerMessage> for LoggingMessageNotification {
3165 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, JsonrpcErrorError> {
3166 ServerMessage::from_message(self, request_id)
3167 }
3168}
3169impl TryFrom<RequestFromClient> for InitializeRequest {
3170 type Error = JsonrpcErrorError;
3171 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3172 let matched_type: ClientRequest = value.try_into()?;
3173 if let ClientRequest::InitializeRequest(result) = matched_type {
3174 Ok(result)
3175 } else {
3176 Err(JsonrpcErrorError::internal_error().with_message("Not a InitializeRequest".to_string()))
3177 }
3178 }
3179}
3180impl TryFrom<RequestFromClient> for PingRequest {
3181 type Error = JsonrpcErrorError;
3182 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3183 let matched_type: ClientRequest = value.try_into()?;
3184 if let ClientRequest::PingRequest(result) = matched_type {
3185 Ok(result)
3186 } else {
3187 Err(JsonrpcErrorError::internal_error().with_message("Not a PingRequest".to_string()))
3188 }
3189 }
3190}
3191impl TryFrom<RequestFromClient> for ListResourcesRequest {
3192 type Error = JsonrpcErrorError;
3193 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3194 let matched_type: ClientRequest = value.try_into()?;
3195 if let ClientRequest::ListResourcesRequest(result) = matched_type {
3196 Ok(result)
3197 } else {
3198 Err(JsonrpcErrorError::internal_error().with_message("Not a ListResourcesRequest".to_string()))
3199 }
3200 }
3201}
3202impl TryFrom<RequestFromClient> for ListResourceTemplatesRequest {
3203 type Error = JsonrpcErrorError;
3204 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3205 let matched_type: ClientRequest = value.try_into()?;
3206 if let ClientRequest::ListResourceTemplatesRequest(result) = matched_type {
3207 Ok(result)
3208 } else {
3209 Err(JsonrpcErrorError::internal_error().with_message("Not a ListResourceTemplatesRequest".to_string()))
3210 }
3211 }
3212}
3213impl TryFrom<RequestFromClient> for ReadResourceRequest {
3214 type Error = JsonrpcErrorError;
3215 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3216 let matched_type: ClientRequest = value.try_into()?;
3217 if let ClientRequest::ReadResourceRequest(result) = matched_type {
3218 Ok(result)
3219 } else {
3220 Err(JsonrpcErrorError::internal_error().with_message("Not a ReadResourceRequest".to_string()))
3221 }
3222 }
3223}
3224impl TryFrom<RequestFromClient> for SubscribeRequest {
3225 type Error = JsonrpcErrorError;
3226 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3227 let matched_type: ClientRequest = value.try_into()?;
3228 if let ClientRequest::SubscribeRequest(result) = matched_type {
3229 Ok(result)
3230 } else {
3231 Err(JsonrpcErrorError::internal_error().with_message("Not a SubscribeRequest".to_string()))
3232 }
3233 }
3234}
3235impl TryFrom<RequestFromClient> for UnsubscribeRequest {
3236 type Error = JsonrpcErrorError;
3237 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3238 let matched_type: ClientRequest = value.try_into()?;
3239 if let ClientRequest::UnsubscribeRequest(result) = matched_type {
3240 Ok(result)
3241 } else {
3242 Err(JsonrpcErrorError::internal_error().with_message("Not a UnsubscribeRequest".to_string()))
3243 }
3244 }
3245}
3246impl TryFrom<RequestFromClient> for ListPromptsRequest {
3247 type Error = JsonrpcErrorError;
3248 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3249 let matched_type: ClientRequest = value.try_into()?;
3250 if let ClientRequest::ListPromptsRequest(result) = matched_type {
3251 Ok(result)
3252 } else {
3253 Err(JsonrpcErrorError::internal_error().with_message("Not a ListPromptsRequest".to_string()))
3254 }
3255 }
3256}
3257impl TryFrom<RequestFromClient> for GetPromptRequest {
3258 type Error = JsonrpcErrorError;
3259 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3260 let matched_type: ClientRequest = value.try_into()?;
3261 if let ClientRequest::GetPromptRequest(result) = matched_type {
3262 Ok(result)
3263 } else {
3264 Err(JsonrpcErrorError::internal_error().with_message("Not a GetPromptRequest".to_string()))
3265 }
3266 }
3267}
3268impl TryFrom<RequestFromClient> for ListToolsRequest {
3269 type Error = JsonrpcErrorError;
3270 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3271 let matched_type: ClientRequest = value.try_into()?;
3272 if let ClientRequest::ListToolsRequest(result) = matched_type {
3273 Ok(result)
3274 } else {
3275 Err(JsonrpcErrorError::internal_error().with_message("Not a ListToolsRequest".to_string()))
3276 }
3277 }
3278}
3279impl TryFrom<RequestFromClient> for CallToolRequest {
3280 type Error = JsonrpcErrorError;
3281 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3282 let matched_type: ClientRequest = value.try_into()?;
3283 if let ClientRequest::CallToolRequest(result) = matched_type {
3284 Ok(result)
3285 } else {
3286 Err(JsonrpcErrorError::internal_error().with_message("Not a CallToolRequest".to_string()))
3287 }
3288 }
3289}
3290impl TryFrom<RequestFromClient> for SetLevelRequest {
3291 type Error = JsonrpcErrorError;
3292 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3293 let matched_type: ClientRequest = value.try_into()?;
3294 if let ClientRequest::SetLevelRequest(result) = matched_type {
3295 Ok(result)
3296 } else {
3297 Err(JsonrpcErrorError::internal_error().with_message("Not a SetLevelRequest".to_string()))
3298 }
3299 }
3300}
3301impl TryFrom<RequestFromClient> for CompleteRequest {
3302 type Error = JsonrpcErrorError;
3303 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3304 let matched_type: ClientRequest = value.try_into()?;
3305 if let ClientRequest::CompleteRequest(result) = matched_type {
3306 Ok(result)
3307 } else {
3308 Err(JsonrpcErrorError::internal_error().with_message("Not a CompleteRequest".to_string()))
3309 }
3310 }
3311}
3312impl TryFrom<ResultFromClient> for Result {
3313 type Error = JsonrpcErrorError;
3314 fn try_from(value: ResultFromClient) -> std::result::Result<Self, Self::Error> {
3315 let matched_type: ClientResult = value.try_into()?;
3316 if let ClientResult::Result(result) = matched_type {
3317 Ok(result)
3318 } else {
3319 Err(JsonrpcErrorError::internal_error().with_message("Not a Result".to_string()))
3320 }
3321 }
3322}
3323impl TryFrom<ResultFromClient> for CreateMessageResult {
3324 type Error = JsonrpcErrorError;
3325 fn try_from(value: ResultFromClient) -> std::result::Result<Self, Self::Error> {
3326 let matched_type: ClientResult = value.try_into()?;
3327 if let ClientResult::CreateMessageResult(result) = matched_type {
3328 Ok(result)
3329 } else {
3330 Err(JsonrpcErrorError::internal_error().with_message("Not a CreateMessageResult".to_string()))
3331 }
3332 }
3333}
3334impl TryFrom<ResultFromClient> for ListRootsResult {
3335 type Error = JsonrpcErrorError;
3336 fn try_from(value: ResultFromClient) -> std::result::Result<Self, Self::Error> {
3337 let matched_type: ClientResult = value.try_into()?;
3338 if let ClientResult::ListRootsResult(result) = matched_type {
3339 Ok(result)
3340 } else {
3341 Err(JsonrpcErrorError::internal_error().with_message("Not a ListRootsResult".to_string()))
3342 }
3343 }
3344}
3345impl TryFrom<NotificationFromClient> for CancelledNotification {
3346 type Error = JsonrpcErrorError;
3347 fn try_from(value: NotificationFromClient) -> std::result::Result<Self, Self::Error> {
3348 let matched_type: ClientNotification = value.try_into()?;
3349 if let ClientNotification::CancelledNotification(result) = matched_type {
3350 Ok(result)
3351 } else {
3352 Err(JsonrpcErrorError::internal_error().with_message("Not a CancelledNotification".to_string()))
3353 }
3354 }
3355}
3356impl TryFrom<NotificationFromClient> for InitializedNotification {
3357 type Error = JsonrpcErrorError;
3358 fn try_from(value: NotificationFromClient) -> std::result::Result<Self, Self::Error> {
3359 let matched_type: ClientNotification = value.try_into()?;
3360 if let ClientNotification::InitializedNotification(result) = matched_type {
3361 Ok(result)
3362 } else {
3363 Err(JsonrpcErrorError::internal_error().with_message("Not a InitializedNotification".to_string()))
3364 }
3365 }
3366}
3367impl TryFrom<NotificationFromClient> for ProgressNotification {
3368 type Error = JsonrpcErrorError;
3369 fn try_from(value: NotificationFromClient) -> std::result::Result<Self, Self::Error> {
3370 let matched_type: ClientNotification = value.try_into()?;
3371 if let ClientNotification::ProgressNotification(result) = matched_type {
3372 Ok(result)
3373 } else {
3374 Err(JsonrpcErrorError::internal_error().with_message("Not a ProgressNotification".to_string()))
3375 }
3376 }
3377}
3378impl TryFrom<NotificationFromClient> for RootsListChangedNotification {
3379 type Error = JsonrpcErrorError;
3380 fn try_from(value: NotificationFromClient) -> std::result::Result<Self, Self::Error> {
3381 let matched_type: ClientNotification = value.try_into()?;
3382 if let ClientNotification::RootsListChangedNotification(result) = matched_type {
3383 Ok(result)
3384 } else {
3385 Err(JsonrpcErrorError::internal_error().with_message("Not a RootsListChangedNotification".to_string()))
3386 }
3387 }
3388}
3389impl TryFrom<RequestFromServer> for PingRequest {
3390 type Error = JsonrpcErrorError;
3391 fn try_from(value: RequestFromServer) -> std::result::Result<Self, Self::Error> {
3392 let matched_type: ServerRequest = value.try_into()?;
3393 if let ServerRequest::PingRequest(result) = matched_type {
3394 Ok(result)
3395 } else {
3396 Err(JsonrpcErrorError::internal_error().with_message("Not a PingRequest".to_string()))
3397 }
3398 }
3399}
3400impl TryFrom<RequestFromServer> for CreateMessageRequest {
3401 type Error = JsonrpcErrorError;
3402 fn try_from(value: RequestFromServer) -> std::result::Result<Self, Self::Error> {
3403 let matched_type: ServerRequest = value.try_into()?;
3404 if let ServerRequest::CreateMessageRequest(result) = matched_type {
3405 Ok(result)
3406 } else {
3407 Err(JsonrpcErrorError::internal_error().with_message("Not a CreateMessageRequest".to_string()))
3408 }
3409 }
3410}
3411impl TryFrom<RequestFromServer> for ListRootsRequest {
3412 type Error = JsonrpcErrorError;
3413 fn try_from(value: RequestFromServer) -> std::result::Result<Self, Self::Error> {
3414 let matched_type: ServerRequest = value.try_into()?;
3415 if let ServerRequest::ListRootsRequest(result) = matched_type {
3416 Ok(result)
3417 } else {
3418 Err(JsonrpcErrorError::internal_error().with_message("Not a ListRootsRequest".to_string()))
3419 }
3420 }
3421}
3422impl TryFrom<ResultFromServer> for Result {
3423 type Error = JsonrpcErrorError;
3424 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3425 let matched_type: ServerResult = value.try_into()?;
3426 if let ServerResult::Result(result) = matched_type {
3427 Ok(result)
3428 } else {
3429 Err(JsonrpcErrorError::internal_error().with_message("Not a Result".to_string()))
3430 }
3431 }
3432}
3433impl TryFrom<ResultFromServer> for InitializeResult {
3434 type Error = JsonrpcErrorError;
3435 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3436 let matched_type: ServerResult = value.try_into()?;
3437 if let ServerResult::InitializeResult(result) = matched_type {
3438 Ok(result)
3439 } else {
3440 Err(JsonrpcErrorError::internal_error().with_message("Not a InitializeResult".to_string()))
3441 }
3442 }
3443}
3444impl TryFrom<ResultFromServer> for ListResourcesResult {
3445 type Error = JsonrpcErrorError;
3446 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3447 let matched_type: ServerResult = value.try_into()?;
3448 if let ServerResult::ListResourcesResult(result) = matched_type {
3449 Ok(result)
3450 } else {
3451 Err(JsonrpcErrorError::internal_error().with_message("Not a ListResourcesResult".to_string()))
3452 }
3453 }
3454}
3455impl TryFrom<ResultFromServer> for ListResourceTemplatesResult {
3456 type Error = JsonrpcErrorError;
3457 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3458 let matched_type: ServerResult = value.try_into()?;
3459 if let ServerResult::ListResourceTemplatesResult(result) = matched_type {
3460 Ok(result)
3461 } else {
3462 Err(JsonrpcErrorError::internal_error().with_message("Not a ListResourceTemplatesResult".to_string()))
3463 }
3464 }
3465}
3466impl TryFrom<ResultFromServer> for ReadResourceResult {
3467 type Error = JsonrpcErrorError;
3468 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3469 let matched_type: ServerResult = value.try_into()?;
3470 if let ServerResult::ReadResourceResult(result) = matched_type {
3471 Ok(result)
3472 } else {
3473 Err(JsonrpcErrorError::internal_error().with_message("Not a ReadResourceResult".to_string()))
3474 }
3475 }
3476}
3477impl TryFrom<ResultFromServer> for ListPromptsResult {
3478 type Error = JsonrpcErrorError;
3479 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3480 let matched_type: ServerResult = value.try_into()?;
3481 if let ServerResult::ListPromptsResult(result) = matched_type {
3482 Ok(result)
3483 } else {
3484 Err(JsonrpcErrorError::internal_error().with_message("Not a ListPromptsResult".to_string()))
3485 }
3486 }
3487}
3488impl TryFrom<ResultFromServer> for GetPromptResult {
3489 type Error = JsonrpcErrorError;
3490 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3491 let matched_type: ServerResult = value.try_into()?;
3492 if let ServerResult::GetPromptResult(result) = matched_type {
3493 Ok(result)
3494 } else {
3495 Err(JsonrpcErrorError::internal_error().with_message("Not a GetPromptResult".to_string()))
3496 }
3497 }
3498}
3499impl TryFrom<ResultFromServer> for ListToolsResult {
3500 type Error = JsonrpcErrorError;
3501 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3502 let matched_type: ServerResult = value.try_into()?;
3503 if let ServerResult::ListToolsResult(result) = matched_type {
3504 Ok(result)
3505 } else {
3506 Err(JsonrpcErrorError::internal_error().with_message("Not a ListToolsResult".to_string()))
3507 }
3508 }
3509}
3510impl TryFrom<ResultFromServer> for CallToolResult {
3511 type Error = JsonrpcErrorError;
3512 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3513 let matched_type: ServerResult = value.try_into()?;
3514 if let ServerResult::CallToolResult(result) = matched_type {
3515 Ok(result)
3516 } else {
3517 Err(JsonrpcErrorError::internal_error().with_message("Not a CallToolResult".to_string()))
3518 }
3519 }
3520}
3521impl TryFrom<ResultFromServer> for CompleteResult {
3522 type Error = JsonrpcErrorError;
3523 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3524 let matched_type: ServerResult = value.try_into()?;
3525 if let ServerResult::CompleteResult(result) = matched_type {
3526 Ok(result)
3527 } else {
3528 Err(JsonrpcErrorError::internal_error().with_message("Not a CompleteResult".to_string()))
3529 }
3530 }
3531}
3532impl TryFrom<NotificationFromServer> for CancelledNotification {
3533 type Error = JsonrpcErrorError;
3534 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3535 let matched_type: ServerNotification = value.try_into()?;
3536 if let ServerNotification::CancelledNotification(result) = matched_type {
3537 Ok(result)
3538 } else {
3539 Err(JsonrpcErrorError::internal_error().with_message("Not a CancelledNotification".to_string()))
3540 }
3541 }
3542}
3543impl TryFrom<NotificationFromServer> for ProgressNotification {
3544 type Error = JsonrpcErrorError;
3545 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3546 let matched_type: ServerNotification = value.try_into()?;
3547 if let ServerNotification::ProgressNotification(result) = matched_type {
3548 Ok(result)
3549 } else {
3550 Err(JsonrpcErrorError::internal_error().with_message("Not a ProgressNotification".to_string()))
3551 }
3552 }
3553}
3554impl TryFrom<NotificationFromServer> for ResourceListChangedNotification {
3555 type Error = JsonrpcErrorError;
3556 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3557 let matched_type: ServerNotification = value.try_into()?;
3558 if let ServerNotification::ResourceListChangedNotification(result) = matched_type {
3559 Ok(result)
3560 } else {
3561 Err(JsonrpcErrorError::internal_error().with_message("Not a ResourceListChangedNotification".to_string()))
3562 }
3563 }
3564}
3565impl TryFrom<NotificationFromServer> for ResourceUpdatedNotification {
3566 type Error = JsonrpcErrorError;
3567 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3568 let matched_type: ServerNotification = value.try_into()?;
3569 if let ServerNotification::ResourceUpdatedNotification(result) = matched_type {
3570 Ok(result)
3571 } else {
3572 Err(JsonrpcErrorError::internal_error().with_message("Not a ResourceUpdatedNotification".to_string()))
3573 }
3574 }
3575}
3576impl TryFrom<NotificationFromServer> for PromptListChangedNotification {
3577 type Error = JsonrpcErrorError;
3578 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3579 let matched_type: ServerNotification = value.try_into()?;
3580 if let ServerNotification::PromptListChangedNotification(result) = matched_type {
3581 Ok(result)
3582 } else {
3583 Err(JsonrpcErrorError::internal_error().with_message("Not a PromptListChangedNotification".to_string()))
3584 }
3585 }
3586}
3587impl TryFrom<NotificationFromServer> for ToolListChangedNotification {
3588 type Error = JsonrpcErrorError;
3589 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3590 let matched_type: ServerNotification = value.try_into()?;
3591 if let ServerNotification::ToolListChangedNotification(result) = matched_type {
3592 Ok(result)
3593 } else {
3594 Err(JsonrpcErrorError::internal_error().with_message("Not a ToolListChangedNotification".to_string()))
3595 }
3596 }
3597}
3598impl TryFrom<NotificationFromServer> for LoggingMessageNotification {
3599 type Error = JsonrpcErrorError;
3600 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3601 let matched_type: ServerNotification = value.try_into()?;
3602 if let ServerNotification::LoggingMessageNotification(result) = matched_type {
3603 Ok(result)
3604 } else {
3605 Err(JsonrpcErrorError::internal_error().with_message("Not a LoggingMessageNotification".to_string()))
3606 }
3607 }
3608}
3609#[cfg(test)]
3611mod tests {
3612 use super::*;
3613 use serde_json::json;
3614
3615 #[test]
3616 fn test_detect_message_type() {
3617 let message = ClientJsonrpcRequest::new(RequestId::Integer(0), PingRequest::new(None).into());
3619 let result = detect_message_type(&json!(message));
3620 assert!(matches!(result, MessageTypes::Request));
3621
3622 let result = detect_message_type(&json!({
3625 "id":0,
3626 "method":"add_numbers",
3627 "params":{},
3628 "jsonrpc":"2.0"
3629 }));
3630 assert!(matches!(result, MessageTypes::Request));
3631
3632 let message = ClientJsonrpcNotification::new(RootsListChangedNotification::new(None).into());
3634 let result = detect_message_type(&json!(message));
3635 assert!(matches!(result, MessageTypes::Notification));
3636
3637 let result = detect_message_type(&json!({
3639 "method":"notifications/email_sent",
3640 "jsonrpc":"2.0"
3641 }));
3642 assert!(matches!(result, MessageTypes::Notification));
3643
3644 let message = ClientJsonrpcResponse::new(
3646 RequestId::Integer(0),
3647 ListRootsResult {
3648 meta: None,
3649 roots: vec![],
3650 }
3651 .into(),
3652 );
3653 let result = detect_message_type(&json!(message));
3654 assert!(matches!(result, MessageTypes::Response));
3655
3656 let result = detect_message_type(&json!({
3659 "id":1,
3660 "jsonrpc":"2.0",
3661 "result":"{}",
3662 }));
3663 assert!(matches!(result, MessageTypes::Response));
3664
3665 let message = JsonrpcError::create(
3667 RequestId::Integer(0),
3668 RpcErrorCodes::INVALID_PARAMS,
3669 "Invalid params!".to_string(),
3670 None,
3671 );
3672 let result = detect_message_type(&json!(message));
3673 assert!(matches!(result, MessageTypes::Error));
3674
3675 let result = detect_message_type(&json!({}));
3677 assert!(matches!(result, MessageTypes::Request));
3678 }
3679}