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, RpcError>;
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, RpcError>;
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, RpcError> {
154 if let Self::Response(response) = self {
155 Ok(response)
156 } else {
157 Err(RpcError::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, RpcError> {
175 if let Self::Request(request) = self {
176 Ok(request)
177 } else {
178 Err(RpcError::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, RpcError> {
196 if let Self::Notification(notification) = self {
197 Ok(notification)
198 } else {
199 Err(RpcError::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, RpcError> {
217 if let Self::Error(error) = self {
218 Ok(error)
219 } else {
220 Err(RpcError::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 = RpcError;
328
329 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
351 serde_json::from_str(s)
352 .map_err(|error| RpcError::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 = RpcError;
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(RpcError::internal_error().with_message("Not a ClientRequest".to_string()))
376 }
377 }
378}
379
380impl RequestFromClient {
381 pub fn method(&self) -> &str {
382 match self {
383 RequestFromClient::ClientRequest(request) => request.method(),
384 RequestFromClient::CustomRequest(request) => request["method"].as_str().unwrap(),
385 }
386 }
387}
388
389impl From<ClientRequest> for RequestFromClient {
390 fn from(value: ClientRequest) -> Self {
391 Self::ClientRequest(value)
392 }
393}
394
395impl From<serde_json::Value> for RequestFromClient {
396 fn from(value: serde_json::Value) -> Self {
397 Self::CustomRequest(value)
398 }
399}
400
401impl<'de> serde::Deserialize<'de> for RequestFromClient {
402 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
403 where
404 D: serde::Deserializer<'de>,
405 {
406 let raw_value = Value::deserialize(deserializer)?;
407
408 let client_result = ClientRequest::deserialize(&raw_value);
409
410 match client_result {
411 Ok(client_request) => Ok(Self::ClientRequest(client_request)),
412 Err(_) => Ok(Self::CustomRequest(raw_value)),
413 }
414 }
415}
416
417#[derive(Clone, Debug)]
423pub struct ClientJsonrpcNotification {
424 jsonrpc: ::std::string::String,
425 pub method: ::std::string::String,
426 pub notification: NotificationFromClient,
427}
428
429impl ClientJsonrpcNotification {
430 pub fn new(notification: NotificationFromClient) -> Self {
431 let method = notification.method().to_string();
432 Self {
433 jsonrpc: JSONRPC_VERSION.to_string(),
434 method,
435 notification,
436 }
437 }
438 pub fn jsonrpc(&self) -> &::std::string::String {
439 &self.jsonrpc
440 }
441}
442
443impl Display for ClientJsonrpcNotification {
445 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
446 write!(
447 f,
448 "{}",
449 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
450 )
451 }
452}
453
454impl FromStr for ClientJsonrpcNotification {
455 type Err = RpcError;
456
457 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
458 serde_json::from_str(s)
459 .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
460 }
461}
462
463#[derive(::serde::Serialize, Clone, Debug)]
470#[serde(untagged)]
471pub enum NotificationFromClient {
472 ClientNotification(ClientNotification),
473 CustomNotification(serde_json::Value),
474}
475
476impl TryFrom<NotificationFromClient> for ClientNotification {
477 type Error = RpcError;
478 fn try_from(value: NotificationFromClient) -> result::Result<Self, Self::Error> {
479 if let NotificationFromClient::ClientNotification(client_notification) = value {
480 Ok(client_notification)
481 } else {
482 Err(RpcError::internal_error().with_message("Not a ClientNotification".to_string()))
483 }
484 }
485}
486
487impl NotificationFromClient {
488 pub fn is_initialized_notification(&self) -> bool {
490 matches!(
491 self,
492 NotificationFromClient::ClientNotification(ClientNotification::InitializedNotification(_))
493 )
494 }
495
496 fn method(&self) -> &str {
497 match self {
498 NotificationFromClient::ClientNotification(notification) => notification.method(),
499 NotificationFromClient::CustomNotification(notification) => notification["method"].as_str().unwrap(),
500 }
501 }
502}
503
504impl<'de> serde::Deserialize<'de> for NotificationFromClient {
505 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
506 where
507 D: serde::Deserializer<'de>,
508 {
509 let raw_value = Value::deserialize(deserializer)?;
510
511 let result = ClientNotification::deserialize(&raw_value);
512
513 match result {
514 Ok(client_notification) => Ok(Self::ClientNotification(client_notification)),
515 Err(_) => Ok(Self::CustomNotification(raw_value)),
516 }
517 }
518}
519
520#[derive(Clone, Debug)]
526pub struct ClientJsonrpcResponse {
527 pub id: RequestId,
528 jsonrpc: ::std::string::String,
529 pub result: ResultFromClient,
530}
531
532impl ClientJsonrpcResponse {
533 pub fn new(id: RequestId, result: ResultFromClient) -> Self {
534 Self {
535 id,
536 jsonrpc: JSONRPC_VERSION.to_string(),
537 result,
538 }
539 }
540 pub fn jsonrpc(&self) -> &::std::string::String {
541 &self.jsonrpc
542 }
543}
544
545impl Display for ClientJsonrpcResponse {
547 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
548 write!(
549 f,
550 "{}",
551 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
552 )
553 }
554}
555
556impl FromStr for ClientJsonrpcResponse {
557 type Err = RpcError;
558
559 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
560 serde_json::from_str(s)
561 .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
562 }
563}
564#[derive(::serde::Serialize, Clone, Debug)]
571#[serde(untagged)]
572pub enum ResultFromClient {
573 ClientResult(ClientResult),
574 CustomResult(serde_json::Value),
576}
577
578impl TryFrom<ResultFromClient> for ClientResult {
579 type Error = RpcError;
580 fn try_from(value: ResultFromClient) -> result::Result<Self, Self::Error> {
581 if let ResultFromClient::ClientResult(client_result) = value {
582 Ok(client_result)
583 } else {
584 Err(RpcError::internal_error().with_message("Not a ClientResult".to_string()))
585 }
586 }
587}
588
589impl From<ClientResult> for ResultFromClient {
590 fn from(value: ClientResult) -> Self {
591 Self::ClientResult(value)
592 }
593}
594
595impl From<serde_json::Value> for ResultFromClient {
596 fn from(value: serde_json::Value) -> Self {
597 Self::CustomResult(value)
598 }
599}
600
601impl<'de> serde::Deserialize<'de> for ResultFromClient {
602 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
603 where
604 D: serde::Deserializer<'de>,
605 {
606 let raw_value = Value::deserialize(deserializer)?;
607
608 let result = ClientResult::deserialize(&raw_value);
609
610 match result {
611 Ok(client_result) => Ok(Self::ClientResult(client_result)),
612 Err(_) => Ok(Self::CustomResult(raw_value)),
613 }
614 }
615}
616
617impl FromStr for ClientMessage {
622 type Err = RpcError;
623
624 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
625 serde_json::from_str(s)
626 .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
627 }
628}
629
630impl Display for ClientMessage {
631 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
632 write!(
633 f,
634 "{}",
635 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
636 )
637 }
638}
639
640#[derive(::serde::Deserialize, ::serde::Serialize, Clone, Debug)]
647#[serde(untagged)]
648pub enum ServerMessage {
649 Request(ServerJsonrpcRequest),
650 Notification(ServerJsonrpcNotification),
651 Response(ServerJsonrpcResponse),
652 Error(JsonrpcError),
653}
654
655impl ServerMessage {
656 pub fn as_response(self) -> std::result::Result<ServerJsonrpcResponse, RpcError> {
666 if let Self::Response(response) = self {
667 Ok(response)
668 } else {
669 Err(RpcError::internal_error().with_message(format!(
670 "Invalid message type, expected: \"{}\" received\"{}\"",
671 MessageTypes::Response,
672 self.message_type()
673 )))
674 }
675 }
676
677 pub fn as_request(self) -> std::result::Result<ServerJsonrpcRequest, RpcError> {
687 if let Self::Request(request) = self {
688 Ok(request)
689 } else {
690 Err(RpcError::internal_error().with_message(format!(
691 "Invalid message type, expected: \"{}\" received\"{}\"",
692 MessageTypes::Request,
693 self.message_type()
694 )))
695 }
696 }
697
698 pub fn as_notification(self) -> std::result::Result<ServerJsonrpcNotification, RpcError> {
708 if let Self::Notification(notification) = self {
709 Ok(notification)
710 } else {
711 Err(RpcError::internal_error().with_message(format!(
712 "Invalid message type, expected: \"{}\" received\"{}\"",
713 MessageTypes::Notification,
714 self.message_type()
715 )))
716 }
717 }
718
719 pub fn as_error(self) -> std::result::Result<JsonrpcError, RpcError> {
729 if let Self::Error(error) = self {
730 Ok(error)
731 } else {
732 Err(RpcError::internal_error().with_message(format!(
733 "Invalid message type, expected: \"{}\" received\"{}\"",
734 MessageTypes::Error,
735 self.message_type()
736 )))
737 }
738 }
739}
740
741impl RpcMessage for ServerMessage {
742 fn request_id(&self) -> Option<&RequestId> {
744 match self {
745 ServerMessage::Request(server_jsonrpc_request) => Some(&server_jsonrpc_request.id),
747 ServerMessage::Notification(_) => None,
749 ServerMessage::Response(server_jsonrpc_response) => Some(&server_jsonrpc_response.id),
751 ServerMessage::Error(jsonrpc_error) => Some(&jsonrpc_error.id),
753 }
754 }
755
756 fn jsonrpc(&self) -> &str {
757 match self {
758 ServerMessage::Request(server_jsonrpc_request) => server_jsonrpc_request.jsonrpc(),
760 ServerMessage::Notification(notification) => notification.jsonrpc(),
762 ServerMessage::Response(server_jsonrpc_response) => server_jsonrpc_response.jsonrpc(),
764 ServerMessage::Error(jsonrpc_error) => jsonrpc_error.jsonrpc(),
766 }
767 }
768}
769
770impl McpMessage for ServerMessage {
772 fn is_response(&self) -> bool {
774 matches!(self, ServerMessage::Response(_))
775 }
776
777 fn is_request(&self) -> bool {
779 matches!(self, ServerMessage::Request(_))
780 }
781
782 fn is_notification(&self) -> bool {
784 matches!(self, ServerMessage::Notification(_))
785 }
786
787 fn is_error(&self) -> bool {
789 matches!(self, ServerMessage::Error(_))
790 }
791
792 fn message_type(&self) -> MessageTypes {
794 match self {
795 ServerMessage::Request(_) => MessageTypes::Request,
796 ServerMessage::Notification(_) => MessageTypes::Notification,
797 ServerMessage::Response(_) => MessageTypes::Response,
798 ServerMessage::Error(_) => MessageTypes::Error,
799 }
800 }
801}
802
803impl FromStr for ServerMessage {
804 type Err = RpcError;
805
806 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
807 serde_json::from_str(s)
808 .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
809 }
810}
811
812impl Display for ServerMessage {
813 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
814 write!(
815 f,
816 "{}",
817 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
818 )
819 }
820}
821
822#[derive(Clone, Debug)]
828pub struct ServerJsonrpcRequest {
829 pub id: RequestId,
830 jsonrpc: ::std::string::String,
831 pub method: String,
832 pub request: RequestFromServer,
833}
834
835impl ServerJsonrpcRequest {
836 pub fn new(id: RequestId, request: RequestFromServer) -> Self {
837 let method = request.method().to_string();
838 Self {
839 id,
840 jsonrpc: JSONRPC_VERSION.to_string(),
841 method,
842 request,
843 }
844 }
845 pub fn jsonrpc(&self) -> &::std::string::String {
846 &self.jsonrpc
847 }
848}
849
850impl Display for ServerJsonrpcRequest {
852 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
853 write!(
854 f,
855 "{}",
856 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
857 )
858 }
859}
860
861impl FromStr for ServerJsonrpcRequest {
862 type Err = RpcError;
863
864 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
865 serde_json::from_str(s)
866 .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
867 }
868}
869#[derive(::serde::Serialize, Clone, Debug)]
876#[serde(untagged)]
877pub enum RequestFromServer {
878 ServerRequest(ServerRequest),
879 CustomRequest(serde_json::Value),
880}
881
882impl TryFrom<RequestFromServer> for ServerRequest {
883 type Error = RpcError;
884 fn try_from(value: RequestFromServer) -> result::Result<Self, Self::Error> {
885 if let RequestFromServer::ServerRequest(server_request) = value {
886 Ok(server_request)
887 } else {
888 Err(RpcError::internal_error().with_message("Not a ServerRequest".to_string()))
889 }
890 }
891}
892
893impl RequestFromServer {
894 pub fn method(&self) -> &str {
895 match self {
896 RequestFromServer::ServerRequest(request) => request.method(),
897 RequestFromServer::CustomRequest(request) => request["method"].as_str().unwrap(),
898 }
899 }
900}
901
902impl From<ServerRequest> for RequestFromServer {
903 fn from(value: ServerRequest) -> Self {
904 Self::ServerRequest(value)
905 }
906}
907
908impl From<serde_json::Value> for RequestFromServer {
909 fn from(value: serde_json::Value) -> Self {
910 Self::CustomRequest(value)
911 }
912}
913
914impl<'de> serde::Deserialize<'de> for RequestFromServer {
915 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
916 where
917 D: serde::Deserializer<'de>,
918 {
919 let raw_value = Value::deserialize(deserializer)?;
920
921 let server_result = ServerRequest::deserialize(&raw_value);
922
923 match server_result {
924 Ok(server_request) => Ok(Self::ServerRequest(server_request)),
925 Err(_) => Ok(Self::CustomRequest(raw_value)),
926 }
927 }
928}
929
930#[derive(Clone, Debug)]
936pub struct ServerJsonrpcNotification {
937 jsonrpc: ::std::string::String,
938 pub method: ::std::string::String,
939 pub notification: NotificationFromServer,
940}
941
942impl ServerJsonrpcNotification {
943 pub fn new(notification: NotificationFromServer) -> Self {
944 let method = notification.method().to_string();
945 Self {
946 jsonrpc: JSONRPC_VERSION.to_string(),
947 method,
948 notification,
949 }
950 }
951 pub fn jsonrpc(&self) -> &::std::string::String {
952 &self.jsonrpc
953 }
954}
955
956impl Display for ServerJsonrpcNotification {
958 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
959 write!(
960 f,
961 "{}",
962 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
963 )
964 }
965}
966
967impl FromStr for ServerJsonrpcNotification {
968 type Err = RpcError;
969
970 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
971 serde_json::from_str(s)
972 .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
973 }
974}
975#[derive(::serde::Serialize, Clone, Debug)]
982#[serde(untagged)]
983pub enum NotificationFromServer {
984 ServerNotification(ServerNotification),
985 CustomNotification(serde_json::Value),
986}
987
988impl TryFrom<NotificationFromServer> for ServerNotification {
989 type Error = RpcError;
990 fn try_from(value: NotificationFromServer) -> result::Result<Self, Self::Error> {
991 if let NotificationFromServer::ServerNotification(server_notification) = value {
992 Ok(server_notification)
993 } else {
994 Err(RpcError::internal_error().with_message("Not a ServerNotification".to_string()))
995 }
996 }
997}
998
999impl NotificationFromServer {
1000 pub fn method(&self) -> &str {
1001 match self {
1002 NotificationFromServer::ServerNotification(notification) => notification.method(),
1003 NotificationFromServer::CustomNotification(notification) => notification["method"].as_str().unwrap(),
1004 }
1005 }
1006}
1007
1008impl<'de> serde::Deserialize<'de> for NotificationFromServer {
1009 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
1010 where
1011 D: serde::Deserializer<'de>,
1012 {
1013 let raw_value = Value::deserialize(deserializer)?;
1014
1015 let result = ServerNotification::deserialize(&raw_value);
1016
1017 match result {
1018 Ok(client_notification) => Ok(Self::ServerNotification(client_notification)),
1019 Err(_) => Ok(Self::CustomNotification(raw_value)),
1020 }
1021 }
1022}
1023
1024#[derive(Clone, Debug)]
1030pub struct ServerJsonrpcResponse {
1031 pub id: RequestId,
1032 jsonrpc: ::std::string::String,
1033 pub result: ResultFromServer,
1034}
1035
1036impl ServerJsonrpcResponse {
1037 pub fn new(id: RequestId, result: ResultFromServer) -> Self {
1038 Self {
1039 id,
1040 jsonrpc: JSONRPC_VERSION.to_string(),
1041 result,
1042 }
1043 }
1044 pub fn jsonrpc(&self) -> &::std::string::String {
1045 &self.jsonrpc
1046 }
1047}
1048
1049impl Display for ServerJsonrpcResponse {
1051 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1052 write!(
1053 f,
1054 "{}",
1055 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
1056 )
1057 }
1058}
1059
1060impl FromStr for ServerJsonrpcResponse {
1061 type Err = RpcError;
1062
1063 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
1064 serde_json::from_str(s)
1065 .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
1066 }
1067}
1068#[allow(clippy::large_enum_variant)]
1075#[derive(::serde::Serialize, Clone, Debug)]
1076#[serde(untagged)]
1077pub enum ResultFromServer {
1078 ServerResult(ServerResult),
1079 CustomResult(serde_json::Value),
1081}
1082
1083impl TryFrom<ResultFromServer> for ServerResult {
1084 type Error = RpcError;
1085 fn try_from(value: ResultFromServer) -> result::Result<Self, Self::Error> {
1086 if let ResultFromServer::ServerResult(server_result) = value {
1087 Ok(server_result)
1088 } else {
1089 Err(RpcError::internal_error().with_message("Not a ServerResult".to_string()))
1090 }
1091 }
1092}
1093
1094impl From<ServerResult> for ResultFromServer {
1095 fn from(value: ServerResult) -> Self {
1096 Self::ServerResult(value)
1097 }
1098}
1099
1100impl From<serde_json::Value> for ResultFromServer {
1101 fn from(value: serde_json::Value) -> Self {
1102 Self::CustomResult(value)
1103 }
1104}
1105
1106impl<'de> serde::Deserialize<'de> for ResultFromServer {
1107 fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
1108 where
1109 D: serde::Deserializer<'de>,
1110 {
1111 let raw_value = Value::deserialize(deserializer)?;
1112
1113 let result = ServerResult::deserialize(&raw_value);
1114
1115 match result {
1116 Ok(server_result) => Ok(Self::ServerResult(server_result)),
1117 Err(_) => Ok(Self::CustomResult(raw_value)),
1118 }
1119 }
1120}
1121
1122impl Display for JsonrpcError {
1128 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1129 write!(
1130 f,
1131 "{}",
1132 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
1133 )
1134 }
1135}
1136
1137impl FromStr for JsonrpcError {
1138 type Err = RpcError;
1139
1140 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
1141 serde_json::from_str(s)
1142 .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
1143 }
1144}
1145
1146#[derive(::serde::Serialize, Clone, Debug)]
1155#[serde(untagged)]
1156pub enum MessageFromServer {
1157 RequestFromServer(RequestFromServer),
1158 ResultFromServer(ResultFromServer),
1159 NotificationFromServer(NotificationFromServer),
1160 Error(RpcError),
1161}
1162
1163impl From<RequestFromServer> for MessageFromServer {
1164 fn from(value: RequestFromServer) -> Self {
1165 Self::RequestFromServer(value)
1166 }
1167}
1168
1169impl From<ResultFromServer> for MessageFromServer {
1170 fn from(value: ResultFromServer) -> Self {
1171 Self::ResultFromServer(value)
1172 }
1173}
1174
1175impl From<NotificationFromServer> for MessageFromServer {
1176 fn from(value: NotificationFromServer) -> Self {
1177 Self::NotificationFromServer(value)
1178 }
1179}
1180
1181impl From<RpcError> for MessageFromServer {
1182 fn from(value: RpcError) -> Self {
1183 Self::Error(value)
1184 }
1185}
1186
1187impl McpMessage for MessageFromServer {
1188 fn is_response(&self) -> bool {
1189 matches!(self, MessageFromServer::ResultFromServer(_))
1190 }
1191
1192 fn is_request(&self) -> bool {
1193 matches!(self, MessageFromServer::RequestFromServer(_))
1194 }
1195
1196 fn is_notification(&self) -> bool {
1197 matches!(self, MessageFromServer::NotificationFromServer(_))
1198 }
1199
1200 fn is_error(&self) -> bool {
1201 matches!(self, MessageFromServer::Error(_))
1202 }
1203
1204 fn message_type(&self) -> MessageTypes {
1205 match self {
1206 MessageFromServer::RequestFromServer(_) => MessageTypes::Request,
1207 MessageFromServer::ResultFromServer(_) => MessageTypes::Response,
1208 MessageFromServer::NotificationFromServer(_) => MessageTypes::Notification,
1209 MessageFromServer::Error(_) => MessageTypes::Error,
1210 }
1211 }
1212}
1213
1214impl FromMessage<MessageFromServer> for ServerMessage {
1215 fn from_message(message: MessageFromServer, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
1216 match message {
1217 MessageFromServer::RequestFromServer(request_from_server) => {
1218 let request_id =
1219 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
1220 Ok(ServerMessage::Request(ServerJsonrpcRequest::new(
1221 request_id,
1222 request_from_server,
1223 )))
1224 }
1225 MessageFromServer::ResultFromServer(result_from_server) => {
1226 let request_id =
1227 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
1228 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
1229 request_id,
1230 result_from_server,
1231 )))
1232 }
1233 MessageFromServer::NotificationFromServer(notification_from_server) => {
1234 if request_id.is_some() {
1235 return Err(RpcError::internal_error()
1236 .with_message("request_id expected to be None for Notifications!".to_string()));
1237 }
1238 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(
1239 notification_from_server,
1240 )))
1241 }
1242 MessageFromServer::Error(jsonrpc_error_error) => {
1243 let request_id =
1244 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
1245 Ok(ServerMessage::Error(JsonrpcError::new(jsonrpc_error_error, request_id)))
1246 }
1247 }
1248 }
1249}
1250
1251#[derive(::serde::Serialize, Clone, Debug)]
1260#[serde(untagged)]
1261pub enum MessageFromClient {
1262 RequestFromClient(RequestFromClient),
1263 ResultFromClient(ResultFromClient),
1264 NotificationFromClient(NotificationFromClient),
1265 Error(RpcError),
1266}
1267
1268impl From<RequestFromClient> for MessageFromClient {
1269 fn from(value: RequestFromClient) -> Self {
1270 Self::RequestFromClient(value)
1271 }
1272}
1273
1274impl From<ResultFromClient> for MessageFromClient {
1275 fn from(value: ResultFromClient) -> Self {
1276 Self::ResultFromClient(value)
1277 }
1278}
1279
1280impl From<NotificationFromClient> for MessageFromClient {
1281 fn from(value: NotificationFromClient) -> Self {
1282 Self::NotificationFromClient(value)
1283 }
1284}
1285
1286impl From<RpcError> for MessageFromClient {
1287 fn from(value: RpcError) -> Self {
1288 Self::Error(value)
1289 }
1290}
1291
1292impl McpMessage for MessageFromClient {
1293 fn is_response(&self) -> bool {
1294 matches!(self, MessageFromClient::ResultFromClient(_))
1295 }
1296
1297 fn is_request(&self) -> bool {
1298 matches!(self, MessageFromClient::RequestFromClient(_))
1299 }
1300
1301 fn is_notification(&self) -> bool {
1302 matches!(self, MessageFromClient::NotificationFromClient(_))
1303 }
1304
1305 fn is_error(&self) -> bool {
1306 matches!(self, MessageFromClient::Error(_))
1307 }
1308
1309 fn message_type(&self) -> MessageTypes {
1310 match self {
1311 MessageFromClient::RequestFromClient(_) => MessageTypes::Request,
1312 MessageFromClient::ResultFromClient(_) => MessageTypes::Response,
1313 MessageFromClient::NotificationFromClient(_) => MessageTypes::Notification,
1314 MessageFromClient::Error(_) => MessageTypes::Error,
1315 }
1316 }
1317}
1318
1319impl FromMessage<MessageFromClient> for ClientMessage {
1320 fn from_message(message: MessageFromClient, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
1321 match message {
1322 MessageFromClient::RequestFromClient(request_from_client) => {
1323 let request_id =
1324 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
1325 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(
1326 request_id,
1327 request_from_client,
1328 )))
1329 }
1330 MessageFromClient::ResultFromClient(result_from_client) => {
1331 let request_id =
1332 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
1333 Ok(ClientMessage::Response(ClientJsonrpcResponse::new(
1334 request_id,
1335 result_from_client,
1336 )))
1337 }
1338 MessageFromClient::NotificationFromClient(notification_from_client) => {
1339 if request_id.is_some() {
1340 return Err(RpcError::internal_error()
1341 .with_message("request_id expected to be None for Notifications!".to_string()));
1342 }
1343
1344 Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(
1345 notification_from_client,
1346 )))
1347 }
1348 MessageFromClient::Error(jsonrpc_error_error) => {
1349 let request_id =
1350 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
1351 Ok(ClientMessage::Error(JsonrpcError::new(jsonrpc_error_error, request_id)))
1352 }
1353 }
1354 }
1355}
1356
1357#[derive(Debug)]
1364pub struct UnknownTool(pub String);
1365
1366impl core::fmt::Display for UnknownTool {
1368 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1369 write!(f, "Unknown tool: {}", self.0)
1371 }
1372}
1373
1374impl std::error::Error for UnknownTool {}
1376
1377#[derive(Debug)]
1383pub struct CallToolError(pub Box<dyn std::error::Error>);
1384
1385impl CallToolError {
1387 pub fn new<E: std::error::Error + 'static>(err: E) -> Self {
1389 CallToolError(Box::new(err))
1391 }
1392
1393 pub fn unknown_tool(tool_name: String) -> Self {
1395 CallToolError(Box::new(UnknownTool(tool_name)))
1397 }
1398}
1399
1400impl From<CallToolError> for RpcError {
1406 fn from(value: CallToolError) -> Self {
1407 Self::internal_error().with_message(value.to_string())
1408 }
1409}
1410
1411impl core::fmt::Display for CallToolError {
1413 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1414 write!(f, "{}", self.0)
1415 }
1416}
1417
1418impl std::error::Error for CallToolError {
1420 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
1421 self.0.source()
1422 }
1423}
1424
1425impl From<CallToolError> for CallToolResult {
1427 fn from(value: CallToolError) -> Self {
1428 CallToolResult::with_error(value)
1430 }
1431}
1432
1433impl CallToolRequest {
1434 pub fn tool_name(&self) -> &str {
1442 &self.params.name
1443 }
1444}
1445
1446#[deprecated(since = "0.4.0", note = "This trait was renamed to RpcMessage. Use RpcMessage instead.")]
1447pub type RPCMessage = ();
1448#[deprecated(since = "0.4.0", note = "This trait was renamed to McpMessage. Use McpMessage instead.")]
1449pub type MCPMessage = ();
1450
1451impl ::serde::Serialize for ClientJsonrpcRequest {
1453 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
1454 where
1455 S: ::serde::Serializer,
1456 {
1457 let mut state = serializer.serialize_struct("JsonrpcRequest", 4)?;
1458 state.serialize_field("id", &self.id)?;
1459 state.serialize_field("jsonrpc", &self.jsonrpc)?;
1460 state.serialize_field("method", &self.method)?;
1461 use ClientRequest::*;
1462 match &self.request {
1463 RequestFromClient::ClientRequest(message) => match message {
1464 InitializeRequest(msg) => state.serialize_field("params", &msg.params)?,
1465 PingRequest(msg) => {
1466 if let Some(params) = &msg.params {
1467 state.serialize_field("params", params)?
1468 }
1469 }
1470 ListResourcesRequest(msg) => state.serialize_field("params", &msg.params)?,
1471 ListResourceTemplatesRequest(msg) => {
1472 if let Some(params) = &msg.params {
1473 state.serialize_field("params", params)?
1474 }
1475 }
1476 ReadResourceRequest(msg) => state.serialize_field("params", &msg.params)?,
1477 SubscribeRequest(msg) => state.serialize_field("params", &msg.params)?,
1478 UnsubscribeRequest(msg) => state.serialize_field("params", &msg.params)?,
1479 ListPromptsRequest(msg) => {
1480 if let Some(params) = &msg.params {
1481 state.serialize_field("params", params)?
1482 }
1483 }
1484 GetPromptRequest(msg) => state.serialize_field("params", &msg.params)?,
1485 ListToolsRequest(msg) => {
1486 if let Some(params) = &msg.params {
1487 state.serialize_field("params", params)?
1488 }
1489 }
1490 CallToolRequest(msg) => state.serialize_field("params", &msg.params)?,
1491 SetLevelRequest(msg) => state.serialize_field("params", &msg.params)?,
1492 CompleteRequest(msg) => state.serialize_field("params", &msg.params)?,
1493 },
1494 RequestFromClient::CustomRequest(value) => state.serialize_field("params", value)?,
1495 }
1496 state.end()
1497 }
1498}
1499impl<'de> ::serde::Deserialize<'de> for ClientJsonrpcRequest {
1500 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1501 where
1502 D: ::serde::Deserializer<'de>,
1503 {
1504 use serde::de::{self, MapAccess, Visitor};
1505 use std::fmt;
1506 struct ClientJsonrpcRequestVisitor;
1507 impl<'de> Visitor<'de> for ClientJsonrpcRequestVisitor {
1508 type Value = ClientJsonrpcRequest;
1509 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1510 formatter.write_str("a valid JSON-RPC request object")
1511 }
1512 fn visit_map<M>(self, mut map: M) -> std::result::Result<ClientJsonrpcRequest, M::Error>
1513 where
1514 M: MapAccess<'de>,
1515 {
1516 let mut id: Option<RequestId> = None;
1517 let mut jsonrpc: Option<String> = None;
1518 let mut method: Option<String> = None;
1519 let mut params: Option<Value> = None;
1520 while let Some(key) = map.next_key::<String>()? {
1521 match key.as_str() {
1522 "id" => id = Some(map.next_value()?),
1523 "jsonrpc" => jsonrpc = Some(map.next_value()?),
1524 "method" => method = Some(map.next_value()?),
1525 "params" => params = Some(map.next_value()?),
1526 _ => {
1527 return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"]));
1528 }
1529 }
1530 }
1531 let id = id.ok_or_else(|| de::Error::missing_field("id"))?;
1532 let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?;
1533 let method = method.ok_or_else(|| de::Error::missing_field("method"))?;
1534 let params = params.unwrap_or_default();
1535 let req_object = json!({ "method" : method, "params" : params });
1536 let request = serde_json::from_value::<RequestFromClient>(req_object).map_err(de::Error::custom)?;
1537 Ok(ClientJsonrpcRequest {
1538 id,
1539 jsonrpc,
1540 method,
1541 request,
1542 })
1543 }
1544 }
1545 deserializer.deserialize_struct(
1546 "JsonrpcRequest",
1547 &["id", "jsonrpc", "method", "params"],
1548 ClientJsonrpcRequestVisitor,
1549 )
1550 }
1551}
1552impl ::serde::Serialize for ServerJsonrpcRequest {
1553 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
1554 where
1555 S: ::serde::Serializer,
1556 {
1557 let mut state = serializer.serialize_struct("JsonrpcRequest", 4)?;
1558 state.serialize_field("id", &self.id)?;
1559 state.serialize_field("jsonrpc", &self.jsonrpc)?;
1560 state.serialize_field("method", &self.method)?;
1561 use ServerRequest::*;
1562 match &self.request {
1563 RequestFromServer::ServerRequest(message) => match message {
1564 PingRequest(msg) => {
1565 if let Some(params) = &msg.params {
1566 state.serialize_field("params", params)?
1567 }
1568 }
1569 CreateMessageRequest(msg) => state.serialize_field("params", &msg.params)?,
1570 ListRootsRequest(msg) => {
1571 if let Some(params) = &msg.params {
1572 state.serialize_field("params", params)?
1573 }
1574 }
1575 },
1576 RequestFromServer::CustomRequest(value) => state.serialize_field("params", value)?,
1577 }
1578 state.end()
1579 }
1580}
1581impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcRequest {
1582 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1583 where
1584 D: ::serde::Deserializer<'de>,
1585 {
1586 use serde::de::{self, MapAccess, Visitor};
1587 use std::fmt;
1588 struct ServerJsonrpcRequestVisitor;
1589 impl<'de> Visitor<'de> for ServerJsonrpcRequestVisitor {
1590 type Value = ServerJsonrpcRequest;
1591 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1592 formatter.write_str("a valid JSON-RPC request object")
1593 }
1594 fn visit_map<M>(self, mut map: M) -> std::result::Result<ServerJsonrpcRequest, M::Error>
1595 where
1596 M: MapAccess<'de>,
1597 {
1598 let mut id: Option<RequestId> = None;
1599 let mut jsonrpc: Option<String> = None;
1600 let mut method: Option<String> = None;
1601 let mut params: Option<Value> = None;
1602 while let Some(key) = map.next_key::<String>()? {
1603 match key.as_str() {
1604 "id" => id = Some(map.next_value()?),
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 id = id.ok_or_else(|| de::Error::missing_field("id"))?;
1614 let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?;
1615 let method = method.ok_or_else(|| de::Error::missing_field("method"))?;
1616 let params = params.unwrap_or_default();
1617 let req_object = json!({ "method" : method, "params" : params });
1618 let request = serde_json::from_value::<RequestFromServer>(req_object).map_err(de::Error::custom)?;
1619 Ok(ServerJsonrpcRequest {
1620 id,
1621 jsonrpc,
1622 method,
1623 request,
1624 })
1625 }
1626 }
1627 deserializer.deserialize_struct(
1628 "JsonrpcRequest",
1629 &["id", "jsonrpc", "method", "params"],
1630 ServerJsonrpcRequestVisitor,
1631 )
1632 }
1633}
1634impl ::serde::Serialize for ClientJsonrpcNotification {
1635 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
1636 where
1637 S: ::serde::Serializer,
1638 {
1639 let mut state = serializer.serialize_struct("JsonrpcNotification", 3)?;
1640 state.serialize_field("jsonrpc", &self.jsonrpc)?;
1641 state.serialize_field("method", &self.method)?;
1642 use ClientNotification::*;
1643 match &self.notification {
1644 NotificationFromClient::ClientNotification(message) => match message {
1645 CancelledNotification(msg) => state.serialize_field("params", &msg.params)?,
1646 InitializedNotification(msg) => {
1647 if let Some(params) = &msg.params {
1648 state.serialize_field("params", params)?
1649 }
1650 }
1651 ProgressNotification(msg) => state.serialize_field("params", &msg.params)?,
1652 RootsListChangedNotification(msg) => {
1653 if let Some(params) = &msg.params {
1654 state.serialize_field("params", params)?
1655 }
1656 }
1657 },
1658 NotificationFromClient::CustomNotification(value) => state.serialize_field("params", value)?,
1659 }
1660 state.end()
1661 }
1662}
1663impl<'de> ::serde::Deserialize<'de> for ClientJsonrpcNotification {
1664 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1665 where
1666 D: ::serde::Deserializer<'de>,
1667 {
1668 use serde::de::{self, MapAccess, Visitor};
1669 use std::fmt;
1670 struct ClientJsonrpcNotificationVisitor;
1671 impl<'de> Visitor<'de> for ClientJsonrpcNotificationVisitor {
1672 type Value = ClientJsonrpcNotification;
1673 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1674 formatter.write_str("a valid JSON-RPC notification object")
1675 }
1676 fn visit_map<M>(self, mut map: M) -> std::result::Result<ClientJsonrpcNotification, M::Error>
1677 where
1678 M: MapAccess<'de>,
1679 {
1680 let mut jsonrpc: Option<String> = None;
1681 let mut method: Option<String> = None;
1682 let mut params: Option<Value> = None;
1683 while let Some(key) = map.next_key::<String>()? {
1684 match key.as_str() {
1685 "jsonrpc" => jsonrpc = Some(map.next_value()?),
1686 "method" => method = Some(map.next_value()?),
1687 "params" => params = Some(map.next_value()?),
1688 _ => {
1689 return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"]));
1690 }
1691 }
1692 }
1693 let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?;
1694 let method = method.ok_or_else(|| de::Error::missing_field("method"))?;
1695 let params = params.unwrap_or_default();
1696 let req_object = json!({ "method" : method, "params" : params });
1697 let notification =
1698 serde_json::from_value::<NotificationFromClient>(req_object).map_err(de::Error::custom)?;
1699 Ok(ClientJsonrpcNotification {
1700 jsonrpc,
1701 method,
1702 notification,
1703 })
1704 }
1705 }
1706 deserializer.deserialize_struct(
1707 "JsonrpcRequest",
1708 &["jsonrpc", "method", "params"],
1709 ClientJsonrpcNotificationVisitor,
1710 )
1711 }
1712}
1713impl ::serde::Serialize for ServerJsonrpcNotification {
1714 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
1715 where
1716 S: ::serde::Serializer,
1717 {
1718 let mut state = serializer.serialize_struct("JsonrpcNotification", 3)?;
1719 state.serialize_field("jsonrpc", &self.jsonrpc)?;
1720 state.serialize_field("method", &self.method)?;
1721 use ServerNotification::*;
1722 match &self.notification {
1723 NotificationFromServer::ServerNotification(message) => match message {
1724 CancelledNotification(msg) => state.serialize_field("params", &msg.params)?,
1725 ProgressNotification(msg) => state.serialize_field("params", &msg.params)?,
1726 ResourceListChangedNotification(msg) => {
1727 if let Some(params) = &msg.params {
1728 state.serialize_field("params", params)?
1729 }
1730 }
1731 ResourceUpdatedNotification(msg) => state.serialize_field("params", &msg.params)?,
1732 PromptListChangedNotification(msg) => {
1733 if let Some(params) = &msg.params {
1734 state.serialize_field("params", params)?
1735 }
1736 }
1737 ToolListChangedNotification(msg) => {
1738 if let Some(params) = &msg.params {
1739 state.serialize_field("params", params)?
1740 }
1741 }
1742 LoggingMessageNotification(msg) => state.serialize_field("params", &msg.params)?,
1743 },
1744 NotificationFromServer::CustomNotification(value) => state.serialize_field("params", value)?,
1745 }
1746 state.end()
1747 }
1748}
1749impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcNotification {
1750 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1751 where
1752 D: ::serde::Deserializer<'de>,
1753 {
1754 use serde::de::{self, MapAccess, Visitor};
1755 use std::fmt;
1756 struct ServerJsonrpcNotificationVisitor;
1757 impl<'de> Visitor<'de> for ServerJsonrpcNotificationVisitor {
1758 type Value = ServerJsonrpcNotification;
1759 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1760 formatter.write_str("a valid JSON-RPC notification object")
1761 }
1762 fn visit_map<M>(self, mut map: M) -> std::result::Result<ServerJsonrpcNotification, M::Error>
1763 where
1764 M: MapAccess<'de>,
1765 {
1766 let mut jsonrpc: Option<String> = None;
1767 let mut method: Option<String> = None;
1768 let mut params: Option<Value> = None;
1769 while let Some(key) = map.next_key::<String>()? {
1770 match key.as_str() {
1771 "jsonrpc" => jsonrpc = Some(map.next_value()?),
1772 "method" => method = Some(map.next_value()?),
1773 "params" => params = Some(map.next_value()?),
1774 _ => {
1775 return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "method", "params"]));
1776 }
1777 }
1778 }
1779 let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?;
1780 let method = method.ok_or_else(|| de::Error::missing_field("method"))?;
1781 let params = params.unwrap_or_default();
1782 let req_object = json!({ "method" : method, "params" : params });
1783 let notification =
1784 serde_json::from_value::<NotificationFromServer>(req_object).map_err(de::Error::custom)?;
1785 Ok(ServerJsonrpcNotification {
1786 jsonrpc,
1787 method,
1788 notification,
1789 })
1790 }
1791 }
1792 deserializer.deserialize_struct(
1793 "JsonrpcRequest",
1794 &["jsonrpc", "method", "params"],
1795 ServerJsonrpcNotificationVisitor,
1796 )
1797 }
1798}
1799impl ::serde::Serialize for ServerJsonrpcResponse {
1800 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
1801 where
1802 S: ::serde::Serializer,
1803 {
1804 let mut state = serializer.serialize_struct("JsonrpcResponse", 3)?;
1805 state.serialize_field("id", &self.id)?;
1806 state.serialize_field("jsonrpc", &self.jsonrpc)?;
1807 state.serialize_field("result", &self.result)?;
1808 state.end()
1809 }
1810}
1811impl<'de> ::serde::Deserialize<'de> for ServerJsonrpcResponse {
1812 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1813 where
1814 D: ::serde::Deserializer<'de>,
1815 {
1816 use serde::de::{self, MapAccess, Visitor};
1817 use std::fmt;
1818 struct ServerJsonrpcResultVisitor;
1819 impl<'de> Visitor<'de> for ServerJsonrpcResultVisitor {
1820 type Value = ServerJsonrpcResponse;
1821 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1822 formatter.write_str("a valid JSON-RPC response object")
1823 }
1824 fn visit_map<M>(self, mut map: M) -> std::result::Result<ServerJsonrpcResponse, M::Error>
1825 where
1826 M: MapAccess<'de>,
1827 {
1828 let mut id: Option<RequestId> = None;
1829 let mut jsonrpc: Option<String> = None;
1830 let mut result: Option<Value> = None;
1831 while let Some(key) = map.next_key::<String>()? {
1832 match key.as_str() {
1833 "id" => id = Some(map.next_value()?),
1834 "jsonrpc" => jsonrpc = Some(map.next_value()?),
1835 "result" => result = Some(map.next_value()?),
1836 _ => {
1837 return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "result"]));
1838 }
1839 }
1840 }
1841 let id = id.ok_or_else(|| de::Error::missing_field("id"))?;
1842 let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?;
1843 let result = result.ok_or_else(|| de::Error::missing_field("result"))?;
1844 let result = serde_json::from_value::<ResultFromServer>(result).map_err(de::Error::custom)?;
1845 Ok(ServerJsonrpcResponse { id, jsonrpc, result })
1846 }
1847 }
1848 deserializer.deserialize_struct("JsonrpcResponse", &["id", "jsonrpc", "result"], ServerJsonrpcResultVisitor)
1849 }
1850}
1851impl ::serde::Serialize for ClientJsonrpcResponse {
1852 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
1853 where
1854 S: ::serde::Serializer,
1855 {
1856 let mut state = serializer.serialize_struct("JsonrpcResponse", 3)?;
1857 state.serialize_field("id", &self.id)?;
1858 state.serialize_field("jsonrpc", &self.jsonrpc)?;
1859 state.serialize_field("result", &self.result)?;
1860 state.end()
1861 }
1862}
1863impl<'de> ::serde::Deserialize<'de> for ClientJsonrpcResponse {
1864 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1865 where
1866 D: ::serde::Deserializer<'de>,
1867 {
1868 use serde::de::{self, MapAccess, Visitor};
1869 use std::fmt;
1870 struct ClientJsonrpcResultVisitor;
1871 impl<'de> Visitor<'de> for ClientJsonrpcResultVisitor {
1872 type Value = ClientJsonrpcResponse;
1873 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1874 formatter.write_str("a valid JSON-RPC response object")
1875 }
1876 fn visit_map<M>(self, mut map: M) -> std::result::Result<ClientJsonrpcResponse, M::Error>
1877 where
1878 M: MapAccess<'de>,
1879 {
1880 let mut id: Option<RequestId> = None;
1881 let mut jsonrpc: Option<String> = None;
1882 let mut result: Option<Value> = None;
1883 while let Some(key) = map.next_key::<String>()? {
1884 match key.as_str() {
1885 "id" => id = Some(map.next_value()?),
1886 "jsonrpc" => jsonrpc = Some(map.next_value()?),
1887 "result" => result = Some(map.next_value()?),
1888 _ => {
1889 return Err(de::Error::unknown_field(&key, &["id", "jsonrpc", "result"]));
1890 }
1891 }
1892 }
1893 let id = id.ok_or_else(|| de::Error::missing_field("id"))?;
1894 let jsonrpc = jsonrpc.ok_or_else(|| de::Error::missing_field("jsonrpc"))?;
1895 let result = result.ok_or_else(|| de::Error::missing_field("result"))?;
1896 let result = serde_json::from_value::<ResultFromClient>(result).map_err(de::Error::custom)?;
1897 Ok(ClientJsonrpcResponse { id, jsonrpc, result })
1898 }
1899 }
1900 deserializer.deserialize_struct("JsonrpcResponse", &["id", "jsonrpc", "result"], ClientJsonrpcResultVisitor)
1901 }
1902}
1903impl From<InitializeRequest> for RequestFromClient {
1904 fn from(value: InitializeRequest) -> Self {
1905 Self::ClientRequest(value.into())
1906 }
1907}
1908impl From<PingRequest> for RequestFromClient {
1909 fn from(value: PingRequest) -> Self {
1910 Self::ClientRequest(value.into())
1911 }
1912}
1913impl From<ListResourcesRequest> for RequestFromClient {
1914 fn from(value: ListResourcesRequest) -> Self {
1915 Self::ClientRequest(value.into())
1916 }
1917}
1918impl From<ListResourceTemplatesRequest> for RequestFromClient {
1919 fn from(value: ListResourceTemplatesRequest) -> Self {
1920 Self::ClientRequest(value.into())
1921 }
1922}
1923impl From<ReadResourceRequest> for RequestFromClient {
1924 fn from(value: ReadResourceRequest) -> Self {
1925 Self::ClientRequest(value.into())
1926 }
1927}
1928impl From<SubscribeRequest> for RequestFromClient {
1929 fn from(value: SubscribeRequest) -> Self {
1930 Self::ClientRequest(value.into())
1931 }
1932}
1933impl From<UnsubscribeRequest> for RequestFromClient {
1934 fn from(value: UnsubscribeRequest) -> Self {
1935 Self::ClientRequest(value.into())
1936 }
1937}
1938impl From<ListPromptsRequest> for RequestFromClient {
1939 fn from(value: ListPromptsRequest) -> Self {
1940 Self::ClientRequest(value.into())
1941 }
1942}
1943impl From<GetPromptRequest> for RequestFromClient {
1944 fn from(value: GetPromptRequest) -> Self {
1945 Self::ClientRequest(value.into())
1946 }
1947}
1948impl From<ListToolsRequest> for RequestFromClient {
1949 fn from(value: ListToolsRequest) -> Self {
1950 Self::ClientRequest(value.into())
1951 }
1952}
1953impl From<CallToolRequest> for RequestFromClient {
1954 fn from(value: CallToolRequest) -> Self {
1955 Self::ClientRequest(value.into())
1956 }
1957}
1958impl From<SetLevelRequest> for RequestFromClient {
1959 fn from(value: SetLevelRequest) -> Self {
1960 Self::ClientRequest(value.into())
1961 }
1962}
1963impl From<CompleteRequest> for RequestFromClient {
1964 fn from(value: CompleteRequest) -> Self {
1965 Self::ClientRequest(value.into())
1966 }
1967}
1968impl From<InitializeRequest> for MessageFromClient {
1969 fn from(value: InitializeRequest) -> Self {
1970 MessageFromClient::RequestFromClient(value.into())
1971 }
1972}
1973impl From<PingRequest> for MessageFromClient {
1974 fn from(value: PingRequest) -> Self {
1975 MessageFromClient::RequestFromClient(value.into())
1976 }
1977}
1978impl From<ListResourcesRequest> for MessageFromClient {
1979 fn from(value: ListResourcesRequest) -> Self {
1980 MessageFromClient::RequestFromClient(value.into())
1981 }
1982}
1983impl From<ListResourceTemplatesRequest> for MessageFromClient {
1984 fn from(value: ListResourceTemplatesRequest) -> Self {
1985 MessageFromClient::RequestFromClient(value.into())
1986 }
1987}
1988impl From<ReadResourceRequest> for MessageFromClient {
1989 fn from(value: ReadResourceRequest) -> Self {
1990 MessageFromClient::RequestFromClient(value.into())
1991 }
1992}
1993impl From<SubscribeRequest> for MessageFromClient {
1994 fn from(value: SubscribeRequest) -> Self {
1995 MessageFromClient::RequestFromClient(value.into())
1996 }
1997}
1998impl From<UnsubscribeRequest> for MessageFromClient {
1999 fn from(value: UnsubscribeRequest) -> Self {
2000 MessageFromClient::RequestFromClient(value.into())
2001 }
2002}
2003impl From<ListPromptsRequest> for MessageFromClient {
2004 fn from(value: ListPromptsRequest) -> Self {
2005 MessageFromClient::RequestFromClient(value.into())
2006 }
2007}
2008impl From<GetPromptRequest> for MessageFromClient {
2009 fn from(value: GetPromptRequest) -> Self {
2010 MessageFromClient::RequestFromClient(value.into())
2011 }
2012}
2013impl From<ListToolsRequest> for MessageFromClient {
2014 fn from(value: ListToolsRequest) -> Self {
2015 MessageFromClient::RequestFromClient(value.into())
2016 }
2017}
2018impl From<CallToolRequest> for MessageFromClient {
2019 fn from(value: CallToolRequest) -> Self {
2020 MessageFromClient::RequestFromClient(value.into())
2021 }
2022}
2023impl From<SetLevelRequest> for MessageFromClient {
2024 fn from(value: SetLevelRequest) -> Self {
2025 MessageFromClient::RequestFromClient(value.into())
2026 }
2027}
2028impl From<CompleteRequest> for MessageFromClient {
2029 fn from(value: CompleteRequest) -> Self {
2030 MessageFromClient::RequestFromClient(value.into())
2031 }
2032}
2033impl From<CancelledNotification> for NotificationFromClient {
2034 fn from(value: CancelledNotification) -> Self {
2035 Self::ClientNotification(value.into())
2036 }
2037}
2038impl From<InitializedNotification> for NotificationFromClient {
2039 fn from(value: InitializedNotification) -> Self {
2040 Self::ClientNotification(value.into())
2041 }
2042}
2043impl From<ProgressNotification> for NotificationFromClient {
2044 fn from(value: ProgressNotification) -> Self {
2045 Self::ClientNotification(value.into())
2046 }
2047}
2048impl From<RootsListChangedNotification> for NotificationFromClient {
2049 fn from(value: RootsListChangedNotification) -> Self {
2050 Self::ClientNotification(value.into())
2051 }
2052}
2053impl From<CancelledNotification> for ClientJsonrpcNotification {
2054 fn from(value: CancelledNotification) -> Self {
2055 Self::new(value.into())
2056 }
2057}
2058impl From<InitializedNotification> for ClientJsonrpcNotification {
2059 fn from(value: InitializedNotification) -> Self {
2060 Self::new(value.into())
2061 }
2062}
2063impl From<ProgressNotification> for ClientJsonrpcNotification {
2064 fn from(value: ProgressNotification) -> Self {
2065 Self::new(value.into())
2066 }
2067}
2068impl From<RootsListChangedNotification> for ClientJsonrpcNotification {
2069 fn from(value: RootsListChangedNotification) -> Self {
2070 Self::new(value.into())
2071 }
2072}
2073impl From<CancelledNotification> for MessageFromClient {
2074 fn from(value: CancelledNotification) -> Self {
2075 MessageFromClient::NotificationFromClient(value.into())
2076 }
2077}
2078impl From<InitializedNotification> for MessageFromClient {
2079 fn from(value: InitializedNotification) -> Self {
2080 MessageFromClient::NotificationFromClient(value.into())
2081 }
2082}
2083impl From<ProgressNotification> for MessageFromClient {
2084 fn from(value: ProgressNotification) -> Self {
2085 MessageFromClient::NotificationFromClient(value.into())
2086 }
2087}
2088impl From<RootsListChangedNotification> for MessageFromClient {
2089 fn from(value: RootsListChangedNotification) -> Self {
2090 MessageFromClient::NotificationFromClient(value.into())
2091 }
2092}
2093impl From<Result> for ResultFromClient {
2094 fn from(value: Result) -> Self {
2095 Self::ClientResult(value.into())
2096 }
2097}
2098impl From<CreateMessageResult> for ResultFromClient {
2099 fn from(value: CreateMessageResult) -> Self {
2100 Self::ClientResult(value.into())
2101 }
2102}
2103impl From<ListRootsResult> for ResultFromClient {
2104 fn from(value: ListRootsResult) -> Self {
2105 Self::ClientResult(value.into())
2106 }
2107}
2108impl From<Result> for MessageFromClient {
2109 fn from(value: Result) -> Self {
2110 MessageFromClient::ResultFromClient(value.into())
2111 }
2112}
2113impl From<CreateMessageResult> for MessageFromClient {
2114 fn from(value: CreateMessageResult) -> Self {
2115 MessageFromClient::ResultFromClient(value.into())
2116 }
2117}
2118impl From<ListRootsResult> for MessageFromClient {
2119 fn from(value: ListRootsResult) -> Self {
2120 MessageFromClient::ResultFromClient(value.into())
2121 }
2122}
2123#[allow(non_camel_case_types)]
2125pub enum SdkErrorCodes {
2126 CONNECTION_CLOSED = -32000,
2127 REQUEST_TIMEOUT = -32001,
2128}
2129impl core::fmt::Display for SdkErrorCodes {
2130 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2131 match self {
2132 SdkErrorCodes::CONNECTION_CLOSED => write!(f, "Connection closed"),
2133 SdkErrorCodes::REQUEST_TIMEOUT => write!(f, "Request timeout"),
2134 }
2135 }
2136}
2137impl From<SdkErrorCodes> for i64 {
2138 fn from(code: SdkErrorCodes) -> Self {
2139 code as i64
2140 }
2141}
2142#[derive(Debug)]
2143pub struct SdkError {
2144 pub code: i64,
2146 pub data: ::std::option::Option<::serde_json::Value>,
2148 pub message: ::std::string::String,
2150}
2151impl core::fmt::Display for SdkError {
2152 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2153 write!(f, "MCP error {}: {}", self.code, self.message)
2154 }
2155}
2156impl std::error::Error for SdkError {
2157 fn description(&self) -> &str {
2158 &self.message
2159 }
2160}
2161impl SdkError {
2162 pub fn new(
2163 error_code: SdkErrorCodes,
2164 message: ::std::string::String,
2165 data: ::std::option::Option<::serde_json::Value>,
2166 ) -> Self {
2167 Self {
2168 code: error_code.into(),
2169 data,
2170 message,
2171 }
2172 }
2173 pub fn connection_closed() -> Self {
2174 Self {
2175 code: SdkErrorCodes::CONNECTION_CLOSED.into(),
2176 data: None,
2177 message: SdkErrorCodes::CONNECTION_CLOSED.to_string(),
2178 }
2179 }
2180 pub fn request_timeout(timeout: u128) -> Self {
2181 Self {
2182 code: SdkErrorCodes::REQUEST_TIMEOUT.into(),
2183 data: Some(json!({ "timeout" : timeout })),
2184 message: SdkErrorCodes::REQUEST_TIMEOUT.to_string(),
2185 }
2186 }
2187}
2188#[allow(non_camel_case_types)]
2190pub enum RpcErrorCodes {
2191 PARSE_ERROR = -32700isize,
2192 INVALID_REQUEST = -32600isize,
2193 METHOD_NOT_FOUND = -32601isize,
2194 INVALID_PARAMS = -32602isize,
2195 INTERNAL_ERROR = -32603isize,
2196}
2197impl From<RpcErrorCodes> for i64 {
2198 fn from(code: RpcErrorCodes) -> Self {
2199 code as i64
2200 }
2201}
2202impl RpcError {
2203 pub fn new(
2220 error_code: RpcErrorCodes,
2221 message: ::std::string::String,
2222 data: ::std::option::Option<::serde_json::Value>,
2223 ) -> Self {
2224 Self {
2225 code: error_code.into(),
2226 data,
2227 message,
2228 }
2229 }
2230 pub fn method_not_found() -> Self {
2241 Self {
2242 code: RpcErrorCodes::METHOD_NOT_FOUND.into(),
2243 data: None,
2244 message: "Method not found".to_string(),
2245 }
2246 }
2247 pub fn invalid_params() -> Self {
2257 Self {
2258 code: RpcErrorCodes::INVALID_PARAMS.into(),
2259 data: None,
2260 message: "Invalid params".to_string(),
2261 }
2262 }
2263 pub fn invalid_request() -> Self {
2273 Self {
2274 code: RpcErrorCodes::INVALID_REQUEST.into(),
2275 data: None,
2276 message: "Invalid request".to_string(),
2277 }
2278 }
2279 pub fn internal_error() -> Self {
2289 Self {
2290 code: RpcErrorCodes::INTERNAL_ERROR.into(),
2291 data: None,
2292 message: "Internal error".to_string(),
2293 }
2294 }
2295 pub fn parse_error() -> Self {
2305 Self {
2306 code: RpcErrorCodes::PARSE_ERROR.into(),
2307 data: None,
2308 message: "Parse error".to_string(),
2309 }
2310 }
2311 pub fn with_message(mut self, message: String) -> Self {
2321 self.message = message;
2322 self
2323 }
2324 pub fn with_data(mut self, data: ::std::option::Option<::serde_json::Value>) -> Self {
2335 self.data = data;
2336 self
2337 }
2338}
2339impl std::error::Error for RpcError {
2340 fn description(&self) -> &str {
2341 &self.message
2342 }
2343}
2344impl Display for RpcError {
2345 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2346 write!(
2347 f,
2348 "{}",
2349 serde_json::to_string(self).unwrap_or_else(|err| format!("Serialization error: {}", err))
2350 )
2351 }
2352}
2353impl FromStr for RpcError {
2354 type Err = RpcError;
2355 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
2356 serde_json::from_str(s)
2357 .map_err(|error| RpcError::parse_error().with_data(Some(json!({ "details" : error.to_string() }))))
2358 }
2359}
2360impl JsonrpcError {
2362 pub fn create(
2363 id: RequestId,
2364 error_code: RpcErrorCodes,
2365 error_message: ::std::string::String,
2366 error_data: ::std::option::Option<::serde_json::Value>,
2367 ) -> Self {
2368 Self::new(RpcError::new(error_code, error_message, error_data), id)
2369 }
2370}
2371impl From<CancelledNotification> for NotificationFromServer {
2372 fn from(value: CancelledNotification) -> Self {
2373 Self::ServerNotification(value.into())
2374 }
2375}
2376impl From<ProgressNotification> for NotificationFromServer {
2377 fn from(value: ProgressNotification) -> Self {
2378 Self::ServerNotification(value.into())
2379 }
2380}
2381impl From<ResourceListChangedNotification> for NotificationFromServer {
2382 fn from(value: ResourceListChangedNotification) -> Self {
2383 Self::ServerNotification(value.into())
2384 }
2385}
2386impl From<ResourceUpdatedNotification> for NotificationFromServer {
2387 fn from(value: ResourceUpdatedNotification) -> Self {
2388 Self::ServerNotification(value.into())
2389 }
2390}
2391impl From<PromptListChangedNotification> for NotificationFromServer {
2392 fn from(value: PromptListChangedNotification) -> Self {
2393 Self::ServerNotification(value.into())
2394 }
2395}
2396impl From<ToolListChangedNotification> for NotificationFromServer {
2397 fn from(value: ToolListChangedNotification) -> Self {
2398 Self::ServerNotification(value.into())
2399 }
2400}
2401impl From<LoggingMessageNotification> for NotificationFromServer {
2402 fn from(value: LoggingMessageNotification) -> Self {
2403 Self::ServerNotification(value.into())
2404 }
2405}
2406impl From<CancelledNotification> for ServerJsonrpcNotification {
2407 fn from(value: CancelledNotification) -> Self {
2408 Self::new(value.into())
2409 }
2410}
2411impl From<ProgressNotification> for ServerJsonrpcNotification {
2412 fn from(value: ProgressNotification) -> Self {
2413 Self::new(value.into())
2414 }
2415}
2416impl From<ResourceListChangedNotification> for ServerJsonrpcNotification {
2417 fn from(value: ResourceListChangedNotification) -> Self {
2418 Self::new(value.into())
2419 }
2420}
2421impl From<ResourceUpdatedNotification> for ServerJsonrpcNotification {
2422 fn from(value: ResourceUpdatedNotification) -> Self {
2423 Self::new(value.into())
2424 }
2425}
2426impl From<PromptListChangedNotification> for ServerJsonrpcNotification {
2427 fn from(value: PromptListChangedNotification) -> Self {
2428 Self::new(value.into())
2429 }
2430}
2431impl From<ToolListChangedNotification> for ServerJsonrpcNotification {
2432 fn from(value: ToolListChangedNotification) -> Self {
2433 Self::new(value.into())
2434 }
2435}
2436impl From<LoggingMessageNotification> for ServerJsonrpcNotification {
2437 fn from(value: LoggingMessageNotification) -> Self {
2438 Self::new(value.into())
2439 }
2440}
2441impl From<CancelledNotification> for MessageFromServer {
2442 fn from(value: CancelledNotification) -> Self {
2443 MessageFromServer::NotificationFromServer(value.into())
2444 }
2445}
2446impl From<ProgressNotification> for MessageFromServer {
2447 fn from(value: ProgressNotification) -> Self {
2448 MessageFromServer::NotificationFromServer(value.into())
2449 }
2450}
2451impl From<ResourceListChangedNotification> for MessageFromServer {
2452 fn from(value: ResourceListChangedNotification) -> Self {
2453 MessageFromServer::NotificationFromServer(value.into())
2454 }
2455}
2456impl From<ResourceUpdatedNotification> for MessageFromServer {
2457 fn from(value: ResourceUpdatedNotification) -> Self {
2458 MessageFromServer::NotificationFromServer(value.into())
2459 }
2460}
2461impl From<PromptListChangedNotification> for MessageFromServer {
2462 fn from(value: PromptListChangedNotification) -> Self {
2463 MessageFromServer::NotificationFromServer(value.into())
2464 }
2465}
2466impl From<ToolListChangedNotification> for MessageFromServer {
2467 fn from(value: ToolListChangedNotification) -> Self {
2468 MessageFromServer::NotificationFromServer(value.into())
2469 }
2470}
2471impl From<LoggingMessageNotification> for MessageFromServer {
2472 fn from(value: LoggingMessageNotification) -> Self {
2473 MessageFromServer::NotificationFromServer(value.into())
2474 }
2475}
2476impl From<PingRequest> for RequestFromServer {
2477 fn from(value: PingRequest) -> Self {
2478 Self::ServerRequest(value.into())
2479 }
2480}
2481impl From<CreateMessageRequest> for RequestFromServer {
2482 fn from(value: CreateMessageRequest) -> Self {
2483 Self::ServerRequest(value.into())
2484 }
2485}
2486impl From<ListRootsRequest> for RequestFromServer {
2487 fn from(value: ListRootsRequest) -> Self {
2488 Self::ServerRequest(value.into())
2489 }
2490}
2491impl From<PingRequest> for MessageFromServer {
2492 fn from(value: PingRequest) -> Self {
2493 MessageFromServer::RequestFromServer(value.into())
2494 }
2495}
2496impl From<CreateMessageRequest> for MessageFromServer {
2497 fn from(value: CreateMessageRequest) -> Self {
2498 MessageFromServer::RequestFromServer(value.into())
2499 }
2500}
2501impl From<ListRootsRequest> for MessageFromServer {
2502 fn from(value: ListRootsRequest) -> Self {
2503 MessageFromServer::RequestFromServer(value.into())
2504 }
2505}
2506impl From<Result> for ResultFromServer {
2507 fn from(value: Result) -> Self {
2508 Self::ServerResult(value.into())
2509 }
2510}
2511impl From<InitializeResult> for ResultFromServer {
2512 fn from(value: InitializeResult) -> Self {
2513 Self::ServerResult(value.into())
2514 }
2515}
2516impl From<ListResourcesResult> for ResultFromServer {
2517 fn from(value: ListResourcesResult) -> Self {
2518 Self::ServerResult(value.into())
2519 }
2520}
2521impl From<ListResourceTemplatesResult> for ResultFromServer {
2522 fn from(value: ListResourceTemplatesResult) -> Self {
2523 Self::ServerResult(value.into())
2524 }
2525}
2526impl From<ReadResourceResult> for ResultFromServer {
2527 fn from(value: ReadResourceResult) -> Self {
2528 Self::ServerResult(value.into())
2529 }
2530}
2531impl From<ListPromptsResult> for ResultFromServer {
2532 fn from(value: ListPromptsResult) -> Self {
2533 Self::ServerResult(value.into())
2534 }
2535}
2536impl From<GetPromptResult> for ResultFromServer {
2537 fn from(value: GetPromptResult) -> Self {
2538 Self::ServerResult(value.into())
2539 }
2540}
2541impl From<ListToolsResult> for ResultFromServer {
2542 fn from(value: ListToolsResult) -> Self {
2543 Self::ServerResult(value.into())
2544 }
2545}
2546impl From<CallToolResult> for ResultFromServer {
2547 fn from(value: CallToolResult) -> Self {
2548 Self::ServerResult(value.into())
2549 }
2550}
2551impl From<CompleteResult> for ResultFromServer {
2552 fn from(value: CompleteResult) -> Self {
2553 Self::ServerResult(value.into())
2554 }
2555}
2556impl From<Result> for MessageFromServer {
2557 fn from(value: Result) -> Self {
2558 MessageFromServer::ResultFromServer(value.into())
2559 }
2560}
2561impl From<InitializeResult> for MessageFromServer {
2562 fn from(value: InitializeResult) -> Self {
2563 MessageFromServer::ResultFromServer(value.into())
2564 }
2565}
2566impl From<ListResourcesResult> for MessageFromServer {
2567 fn from(value: ListResourcesResult) -> Self {
2568 MessageFromServer::ResultFromServer(value.into())
2569 }
2570}
2571impl From<ListResourceTemplatesResult> for MessageFromServer {
2572 fn from(value: ListResourceTemplatesResult) -> Self {
2573 MessageFromServer::ResultFromServer(value.into())
2574 }
2575}
2576impl From<ReadResourceResult> for MessageFromServer {
2577 fn from(value: ReadResourceResult) -> Self {
2578 MessageFromServer::ResultFromServer(value.into())
2579 }
2580}
2581impl From<ListPromptsResult> for MessageFromServer {
2582 fn from(value: ListPromptsResult) -> Self {
2583 MessageFromServer::ResultFromServer(value.into())
2584 }
2585}
2586impl From<GetPromptResult> for MessageFromServer {
2587 fn from(value: GetPromptResult) -> Self {
2588 MessageFromServer::ResultFromServer(value.into())
2589 }
2590}
2591impl From<ListToolsResult> for MessageFromServer {
2592 fn from(value: ListToolsResult) -> Self {
2593 MessageFromServer::ResultFromServer(value.into())
2594 }
2595}
2596impl From<CallToolResult> for MessageFromServer {
2597 fn from(value: CallToolResult) -> Self {
2598 MessageFromServer::ResultFromServer(value.into())
2599 }
2600}
2601impl From<CompleteResult> for MessageFromServer {
2602 fn from(value: CompleteResult) -> Self {
2603 MessageFromServer::ResultFromServer(value.into())
2604 }
2605}
2606impl FromMessage<InitializeRequest> for ClientMessage {
2607 fn from_message(message: InitializeRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2608 let request_id =
2609 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2610 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2611 }
2612}
2613impl ToMessage<ClientMessage> for InitializeRequest {
2614 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2615 ClientMessage::from_message(self, request_id)
2616 }
2617}
2618impl FromMessage<PingRequest> for ClientMessage {
2619 fn from_message(message: PingRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2620 let request_id =
2621 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2622 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2623 }
2624}
2625impl ToMessage<ClientMessage> for PingRequest {
2626 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2627 ClientMessage::from_message(self, request_id)
2628 }
2629}
2630impl FromMessage<ListResourcesRequest> for ClientMessage {
2631 fn from_message(message: ListResourcesRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2632 let request_id =
2633 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2634 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2635 }
2636}
2637impl ToMessage<ClientMessage> for ListResourcesRequest {
2638 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2639 ClientMessage::from_message(self, request_id)
2640 }
2641}
2642impl FromMessage<ListResourceTemplatesRequest> for ClientMessage {
2643 fn from_message(
2644 message: ListResourceTemplatesRequest,
2645 request_id: Option<RequestId>,
2646 ) -> std::result::Result<Self, RpcError> {
2647 let request_id =
2648 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2649 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2650 }
2651}
2652impl ToMessage<ClientMessage> for ListResourceTemplatesRequest {
2653 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2654 ClientMessage::from_message(self, request_id)
2655 }
2656}
2657impl FromMessage<ReadResourceRequest> for ClientMessage {
2658 fn from_message(message: ReadResourceRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2659 let request_id =
2660 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2661 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2662 }
2663}
2664impl ToMessage<ClientMessage> for ReadResourceRequest {
2665 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2666 ClientMessage::from_message(self, request_id)
2667 }
2668}
2669impl FromMessage<SubscribeRequest> for ClientMessage {
2670 fn from_message(message: SubscribeRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2671 let request_id =
2672 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2673 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2674 }
2675}
2676impl ToMessage<ClientMessage> for SubscribeRequest {
2677 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2678 ClientMessage::from_message(self, request_id)
2679 }
2680}
2681impl FromMessage<UnsubscribeRequest> for ClientMessage {
2682 fn from_message(message: UnsubscribeRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2683 let request_id =
2684 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2685 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2686 }
2687}
2688impl ToMessage<ClientMessage> for UnsubscribeRequest {
2689 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2690 ClientMessage::from_message(self, request_id)
2691 }
2692}
2693impl FromMessage<ListPromptsRequest> for ClientMessage {
2694 fn from_message(message: ListPromptsRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2695 let request_id =
2696 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2697 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2698 }
2699}
2700impl ToMessage<ClientMessage> for ListPromptsRequest {
2701 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2702 ClientMessage::from_message(self, request_id)
2703 }
2704}
2705impl FromMessage<GetPromptRequest> for ClientMessage {
2706 fn from_message(message: GetPromptRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2707 let request_id =
2708 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2709 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2710 }
2711}
2712impl ToMessage<ClientMessage> for GetPromptRequest {
2713 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2714 ClientMessage::from_message(self, request_id)
2715 }
2716}
2717impl FromMessage<ListToolsRequest> for ClientMessage {
2718 fn from_message(message: ListToolsRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2719 let request_id =
2720 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2721 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2722 }
2723}
2724impl ToMessage<ClientMessage> for ListToolsRequest {
2725 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2726 ClientMessage::from_message(self, request_id)
2727 }
2728}
2729impl FromMessage<CallToolRequest> for ClientMessage {
2730 fn from_message(message: CallToolRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2731 let request_id =
2732 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2733 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2734 }
2735}
2736impl ToMessage<ClientMessage> for CallToolRequest {
2737 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2738 ClientMessage::from_message(self, request_id)
2739 }
2740}
2741impl FromMessage<SetLevelRequest> for ClientMessage {
2742 fn from_message(message: SetLevelRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2743 let request_id =
2744 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2745 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2746 }
2747}
2748impl ToMessage<ClientMessage> for SetLevelRequest {
2749 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2750 ClientMessage::from_message(self, request_id)
2751 }
2752}
2753impl FromMessage<CompleteRequest> for ClientMessage {
2754 fn from_message(message: CompleteRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2755 let request_id =
2756 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2757 Ok(ClientMessage::Request(ClientJsonrpcRequest::new(request_id, message.into())))
2758 }
2759}
2760impl ToMessage<ClientMessage> for CompleteRequest {
2761 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2762 ClientMessage::from_message(self, request_id)
2763 }
2764}
2765impl FromMessage<Result> for ClientMessage {
2766 fn from_message(message: Result, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2767 let request_id =
2768 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2769 Ok(ClientMessage::Response(ClientJsonrpcResponse::new(
2770 request_id,
2771 message.into(),
2772 )))
2773 }
2774}
2775impl ToMessage<ClientMessage> for Result {
2776 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2777 ClientMessage::from_message(self, request_id)
2778 }
2779}
2780impl FromMessage<CreateMessageResult> for ClientMessage {
2781 fn from_message(message: CreateMessageResult, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2782 let request_id =
2783 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2784 Ok(ClientMessage::Response(ClientJsonrpcResponse::new(
2785 request_id,
2786 message.into(),
2787 )))
2788 }
2789}
2790impl ToMessage<ClientMessage> for CreateMessageResult {
2791 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2792 ClientMessage::from_message(self, request_id)
2793 }
2794}
2795impl FromMessage<ListRootsResult> for ClientMessage {
2796 fn from_message(message: ListRootsResult, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2797 let request_id =
2798 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2799 Ok(ClientMessage::Response(ClientJsonrpcResponse::new(
2800 request_id,
2801 message.into(),
2802 )))
2803 }
2804}
2805impl ToMessage<ClientMessage> for ListRootsResult {
2806 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2807 ClientMessage::from_message(self, request_id)
2808 }
2809}
2810impl FromMessage<CancelledNotification> for ClientMessage {
2811 fn from_message(message: CancelledNotification, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2812 if request_id.is_some() {
2813 return Err(
2814 RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string())
2815 );
2816 }
2817 Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into())))
2818 }
2819}
2820impl ToMessage<ClientMessage> for CancelledNotification {
2821 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2822 ClientMessage::from_message(self, request_id)
2823 }
2824}
2825impl FromMessage<InitializedNotification> for ClientMessage {
2826 fn from_message(message: InitializedNotification, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2827 if request_id.is_some() {
2828 return Err(
2829 RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string())
2830 );
2831 }
2832 Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into())))
2833 }
2834}
2835impl ToMessage<ClientMessage> for InitializedNotification {
2836 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2837 ClientMessage::from_message(self, request_id)
2838 }
2839}
2840impl FromMessage<ProgressNotification> for ClientMessage {
2841 fn from_message(message: ProgressNotification, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2842 if request_id.is_some() {
2843 return Err(
2844 RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string())
2845 );
2846 }
2847 Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into())))
2848 }
2849}
2850impl ToMessage<ClientMessage> for ProgressNotification {
2851 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2852 ClientMessage::from_message(self, request_id)
2853 }
2854}
2855impl FromMessage<RootsListChangedNotification> for ClientMessage {
2856 fn from_message(
2857 message: RootsListChangedNotification,
2858 request_id: Option<RequestId>,
2859 ) -> std::result::Result<Self, RpcError> {
2860 if request_id.is_some() {
2861 return Err(
2862 RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string())
2863 );
2864 }
2865 Ok(ClientMessage::Notification(ClientJsonrpcNotification::new(message.into())))
2866 }
2867}
2868impl ToMessage<ClientMessage> for RootsListChangedNotification {
2869 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ClientMessage, RpcError> {
2870 ClientMessage::from_message(self, request_id)
2871 }
2872}
2873impl FromMessage<PingRequest> for ServerMessage {
2874 fn from_message(message: PingRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2875 let request_id =
2876 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2877 Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into())))
2878 }
2879}
2880impl ToMessage<ServerMessage> for PingRequest {
2881 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
2882 ServerMessage::from_message(self, request_id)
2883 }
2884}
2885impl FromMessage<CreateMessageRequest> for ServerMessage {
2886 fn from_message(message: CreateMessageRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2887 let request_id =
2888 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2889 Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into())))
2890 }
2891}
2892impl ToMessage<ServerMessage> for CreateMessageRequest {
2893 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
2894 ServerMessage::from_message(self, request_id)
2895 }
2896}
2897impl FromMessage<ListRootsRequest> for ServerMessage {
2898 fn from_message(message: ListRootsRequest, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2899 let request_id =
2900 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2901 Ok(ServerMessage::Request(ServerJsonrpcRequest::new(request_id, message.into())))
2902 }
2903}
2904impl ToMessage<ServerMessage> for ListRootsRequest {
2905 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
2906 ServerMessage::from_message(self, request_id)
2907 }
2908}
2909impl FromMessage<Result> for ServerMessage {
2910 fn from_message(message: Result, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2911 let request_id =
2912 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2913 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
2914 request_id,
2915 message.into(),
2916 )))
2917 }
2918}
2919impl ToMessage<ServerMessage> for Result {
2920 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
2921 ServerMessage::from_message(self, request_id)
2922 }
2923}
2924impl FromMessage<InitializeResult> for ServerMessage {
2925 fn from_message(message: InitializeResult, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2926 let request_id =
2927 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2928 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
2929 request_id,
2930 message.into(),
2931 )))
2932 }
2933}
2934impl ToMessage<ServerMessage> for InitializeResult {
2935 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
2936 ServerMessage::from_message(self, request_id)
2937 }
2938}
2939impl FromMessage<ListResourcesResult> for ServerMessage {
2940 fn from_message(message: ListResourcesResult, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2941 let request_id =
2942 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2943 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
2944 request_id,
2945 message.into(),
2946 )))
2947 }
2948}
2949impl ToMessage<ServerMessage> for ListResourcesResult {
2950 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
2951 ServerMessage::from_message(self, request_id)
2952 }
2953}
2954impl FromMessage<ListResourceTemplatesResult> for ServerMessage {
2955 fn from_message(
2956 message: ListResourceTemplatesResult,
2957 request_id: Option<RequestId>,
2958 ) -> std::result::Result<Self, RpcError> {
2959 let request_id =
2960 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2961 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
2962 request_id,
2963 message.into(),
2964 )))
2965 }
2966}
2967impl ToMessage<ServerMessage> for ListResourceTemplatesResult {
2968 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
2969 ServerMessage::from_message(self, request_id)
2970 }
2971}
2972impl FromMessage<ReadResourceResult> for ServerMessage {
2973 fn from_message(message: ReadResourceResult, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2974 let request_id =
2975 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
2976 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
2977 request_id,
2978 message.into(),
2979 )))
2980 }
2981}
2982impl ToMessage<ServerMessage> for ReadResourceResult {
2983 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
2984 ServerMessage::from_message(self, request_id)
2985 }
2986}
2987impl FromMessage<ListPromptsResult> for ServerMessage {
2988 fn from_message(message: ListPromptsResult, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
2989 let request_id =
2990 request_id.ok_or_else(|| RpcError::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 ListPromptsResult {
2998 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
2999 ServerMessage::from_message(self, request_id)
3000 }
3001}
3002impl FromMessage<GetPromptResult> for ServerMessage {
3003 fn from_message(message: GetPromptResult, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
3004 let request_id =
3005 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
3006 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
3007 request_id,
3008 message.into(),
3009 )))
3010 }
3011}
3012impl ToMessage<ServerMessage> for GetPromptResult {
3013 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
3014 ServerMessage::from_message(self, request_id)
3015 }
3016}
3017impl FromMessage<ListToolsResult> for ServerMessage {
3018 fn from_message(message: ListToolsResult, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
3019 let request_id =
3020 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
3021 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
3022 request_id,
3023 message.into(),
3024 )))
3025 }
3026}
3027impl ToMessage<ServerMessage> for ListToolsResult {
3028 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
3029 ServerMessage::from_message(self, request_id)
3030 }
3031}
3032impl FromMessage<CallToolResult> for ServerMessage {
3033 fn from_message(message: CallToolResult, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
3034 let request_id =
3035 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
3036 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
3037 request_id,
3038 message.into(),
3039 )))
3040 }
3041}
3042impl ToMessage<ServerMessage> for CallToolResult {
3043 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
3044 ServerMessage::from_message(self, request_id)
3045 }
3046}
3047impl FromMessage<CompleteResult> for ServerMessage {
3048 fn from_message(message: CompleteResult, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
3049 let request_id =
3050 request_id.ok_or_else(|| RpcError::internal_error().with_message("request_id is None!".to_string()))?;
3051 Ok(ServerMessage::Response(ServerJsonrpcResponse::new(
3052 request_id,
3053 message.into(),
3054 )))
3055 }
3056}
3057impl ToMessage<ServerMessage> for CompleteResult {
3058 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
3059 ServerMessage::from_message(self, request_id)
3060 }
3061}
3062impl FromMessage<CancelledNotification> for ServerMessage {
3063 fn from_message(message: CancelledNotification, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
3064 if request_id.is_some() {
3065 return Err(
3066 RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string())
3067 );
3068 }
3069 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3070 }
3071}
3072impl ToMessage<ServerMessage> for CancelledNotification {
3073 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
3074 ServerMessage::from_message(self, request_id)
3075 }
3076}
3077impl FromMessage<ProgressNotification> for ServerMessage {
3078 fn from_message(message: ProgressNotification, request_id: Option<RequestId>) -> std::result::Result<Self, RpcError> {
3079 if request_id.is_some() {
3080 return Err(
3081 RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string())
3082 );
3083 }
3084 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3085 }
3086}
3087impl ToMessage<ServerMessage> for ProgressNotification {
3088 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
3089 ServerMessage::from_message(self, request_id)
3090 }
3091}
3092impl FromMessage<ResourceListChangedNotification> for ServerMessage {
3093 fn from_message(
3094 message: ResourceListChangedNotification,
3095 request_id: Option<RequestId>,
3096 ) -> std::result::Result<Self, RpcError> {
3097 if request_id.is_some() {
3098 return Err(
3099 RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string())
3100 );
3101 }
3102 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3103 }
3104}
3105impl ToMessage<ServerMessage> for ResourceListChangedNotification {
3106 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
3107 ServerMessage::from_message(self, request_id)
3108 }
3109}
3110impl FromMessage<ResourceUpdatedNotification> for ServerMessage {
3111 fn from_message(
3112 message: ResourceUpdatedNotification,
3113 request_id: Option<RequestId>,
3114 ) -> std::result::Result<Self, RpcError> {
3115 if request_id.is_some() {
3116 return Err(
3117 RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string())
3118 );
3119 }
3120 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3121 }
3122}
3123impl ToMessage<ServerMessage> for ResourceUpdatedNotification {
3124 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
3125 ServerMessage::from_message(self, request_id)
3126 }
3127}
3128impl FromMessage<PromptListChangedNotification> for ServerMessage {
3129 fn from_message(
3130 message: PromptListChangedNotification,
3131 request_id: Option<RequestId>,
3132 ) -> std::result::Result<Self, RpcError> {
3133 if request_id.is_some() {
3134 return Err(
3135 RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string())
3136 );
3137 }
3138 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3139 }
3140}
3141impl ToMessage<ServerMessage> for PromptListChangedNotification {
3142 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
3143 ServerMessage::from_message(self, request_id)
3144 }
3145}
3146impl FromMessage<ToolListChangedNotification> for ServerMessage {
3147 fn from_message(
3148 message: ToolListChangedNotification,
3149 request_id: Option<RequestId>,
3150 ) -> std::result::Result<Self, RpcError> {
3151 if request_id.is_some() {
3152 return Err(
3153 RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string())
3154 );
3155 }
3156 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3157 }
3158}
3159impl ToMessage<ServerMessage> for ToolListChangedNotification {
3160 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
3161 ServerMessage::from_message(self, request_id)
3162 }
3163}
3164impl FromMessage<LoggingMessageNotification> for ServerMessage {
3165 fn from_message(
3166 message: LoggingMessageNotification,
3167 request_id: Option<RequestId>,
3168 ) -> std::result::Result<Self, RpcError> {
3169 if request_id.is_some() {
3170 return Err(
3171 RpcError::internal_error().with_message("request_id expected to be None for Notifications!".to_string())
3172 );
3173 }
3174 Ok(ServerMessage::Notification(ServerJsonrpcNotification::new(message.into())))
3175 }
3176}
3177impl ToMessage<ServerMessage> for LoggingMessageNotification {
3178 fn to_message(self, request_id: Option<RequestId>) -> std::result::Result<ServerMessage, RpcError> {
3179 ServerMessage::from_message(self, request_id)
3180 }
3181}
3182impl TryFrom<RequestFromClient> for InitializeRequest {
3183 type Error = RpcError;
3184 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3185 let matched_type: ClientRequest = value.try_into()?;
3186 if let ClientRequest::InitializeRequest(result) = matched_type {
3187 Ok(result)
3188 } else {
3189 Err(RpcError::internal_error().with_message("Not a InitializeRequest".to_string()))
3190 }
3191 }
3192}
3193impl TryFrom<RequestFromClient> for PingRequest {
3194 type Error = RpcError;
3195 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3196 let matched_type: ClientRequest = value.try_into()?;
3197 if let ClientRequest::PingRequest(result) = matched_type {
3198 Ok(result)
3199 } else {
3200 Err(RpcError::internal_error().with_message("Not a PingRequest".to_string()))
3201 }
3202 }
3203}
3204impl TryFrom<RequestFromClient> for ListResourcesRequest {
3205 type Error = RpcError;
3206 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3207 let matched_type: ClientRequest = value.try_into()?;
3208 if let ClientRequest::ListResourcesRequest(result) = matched_type {
3209 Ok(result)
3210 } else {
3211 Err(RpcError::internal_error().with_message("Not a ListResourcesRequest".to_string()))
3212 }
3213 }
3214}
3215impl TryFrom<RequestFromClient> for ListResourceTemplatesRequest {
3216 type Error = RpcError;
3217 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3218 let matched_type: ClientRequest = value.try_into()?;
3219 if let ClientRequest::ListResourceTemplatesRequest(result) = matched_type {
3220 Ok(result)
3221 } else {
3222 Err(RpcError::internal_error().with_message("Not a ListResourceTemplatesRequest".to_string()))
3223 }
3224 }
3225}
3226impl TryFrom<RequestFromClient> for ReadResourceRequest {
3227 type Error = RpcError;
3228 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3229 let matched_type: ClientRequest = value.try_into()?;
3230 if let ClientRequest::ReadResourceRequest(result) = matched_type {
3231 Ok(result)
3232 } else {
3233 Err(RpcError::internal_error().with_message("Not a ReadResourceRequest".to_string()))
3234 }
3235 }
3236}
3237impl TryFrom<RequestFromClient> for SubscribeRequest {
3238 type Error = RpcError;
3239 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3240 let matched_type: ClientRequest = value.try_into()?;
3241 if let ClientRequest::SubscribeRequest(result) = matched_type {
3242 Ok(result)
3243 } else {
3244 Err(RpcError::internal_error().with_message("Not a SubscribeRequest".to_string()))
3245 }
3246 }
3247}
3248impl TryFrom<RequestFromClient> for UnsubscribeRequest {
3249 type Error = RpcError;
3250 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3251 let matched_type: ClientRequest = value.try_into()?;
3252 if let ClientRequest::UnsubscribeRequest(result) = matched_type {
3253 Ok(result)
3254 } else {
3255 Err(RpcError::internal_error().with_message("Not a UnsubscribeRequest".to_string()))
3256 }
3257 }
3258}
3259impl TryFrom<RequestFromClient> for ListPromptsRequest {
3260 type Error = RpcError;
3261 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3262 let matched_type: ClientRequest = value.try_into()?;
3263 if let ClientRequest::ListPromptsRequest(result) = matched_type {
3264 Ok(result)
3265 } else {
3266 Err(RpcError::internal_error().with_message("Not a ListPromptsRequest".to_string()))
3267 }
3268 }
3269}
3270impl TryFrom<RequestFromClient> for GetPromptRequest {
3271 type Error = RpcError;
3272 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3273 let matched_type: ClientRequest = value.try_into()?;
3274 if let ClientRequest::GetPromptRequest(result) = matched_type {
3275 Ok(result)
3276 } else {
3277 Err(RpcError::internal_error().with_message("Not a GetPromptRequest".to_string()))
3278 }
3279 }
3280}
3281impl TryFrom<RequestFromClient> for ListToolsRequest {
3282 type Error = RpcError;
3283 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3284 let matched_type: ClientRequest = value.try_into()?;
3285 if let ClientRequest::ListToolsRequest(result) = matched_type {
3286 Ok(result)
3287 } else {
3288 Err(RpcError::internal_error().with_message("Not a ListToolsRequest".to_string()))
3289 }
3290 }
3291}
3292impl TryFrom<RequestFromClient> for CallToolRequest {
3293 type Error = RpcError;
3294 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3295 let matched_type: ClientRequest = value.try_into()?;
3296 if let ClientRequest::CallToolRequest(result) = matched_type {
3297 Ok(result)
3298 } else {
3299 Err(RpcError::internal_error().with_message("Not a CallToolRequest".to_string()))
3300 }
3301 }
3302}
3303impl TryFrom<RequestFromClient> for SetLevelRequest {
3304 type Error = RpcError;
3305 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3306 let matched_type: ClientRequest = value.try_into()?;
3307 if let ClientRequest::SetLevelRequest(result) = matched_type {
3308 Ok(result)
3309 } else {
3310 Err(RpcError::internal_error().with_message("Not a SetLevelRequest".to_string()))
3311 }
3312 }
3313}
3314impl TryFrom<RequestFromClient> for CompleteRequest {
3315 type Error = RpcError;
3316 fn try_from(value: RequestFromClient) -> std::result::Result<Self, Self::Error> {
3317 let matched_type: ClientRequest = value.try_into()?;
3318 if let ClientRequest::CompleteRequest(result) = matched_type {
3319 Ok(result)
3320 } else {
3321 Err(RpcError::internal_error().with_message("Not a CompleteRequest".to_string()))
3322 }
3323 }
3324}
3325impl TryFrom<ResultFromClient> for Result {
3326 type Error = RpcError;
3327 fn try_from(value: ResultFromClient) -> std::result::Result<Self, Self::Error> {
3328 let matched_type: ClientResult = value.try_into()?;
3329 if let ClientResult::Result(result) = matched_type {
3330 Ok(result)
3331 } else {
3332 Err(RpcError::internal_error().with_message("Not a Result".to_string()))
3333 }
3334 }
3335}
3336impl TryFrom<ResultFromClient> for CreateMessageResult {
3337 type Error = RpcError;
3338 fn try_from(value: ResultFromClient) -> std::result::Result<Self, Self::Error> {
3339 let matched_type: ClientResult = value.try_into()?;
3340 if let ClientResult::CreateMessageResult(result) = matched_type {
3341 Ok(result)
3342 } else {
3343 Err(RpcError::internal_error().with_message("Not a CreateMessageResult".to_string()))
3344 }
3345 }
3346}
3347impl TryFrom<ResultFromClient> for ListRootsResult {
3348 type Error = RpcError;
3349 fn try_from(value: ResultFromClient) -> std::result::Result<Self, Self::Error> {
3350 let matched_type: ClientResult = value.try_into()?;
3351 if let ClientResult::ListRootsResult(result) = matched_type {
3352 Ok(result)
3353 } else {
3354 Err(RpcError::internal_error().with_message("Not a ListRootsResult".to_string()))
3355 }
3356 }
3357}
3358impl TryFrom<NotificationFromClient> for CancelledNotification {
3359 type Error = RpcError;
3360 fn try_from(value: NotificationFromClient) -> std::result::Result<Self, Self::Error> {
3361 let matched_type: ClientNotification = value.try_into()?;
3362 if let ClientNotification::CancelledNotification(result) = matched_type {
3363 Ok(result)
3364 } else {
3365 Err(RpcError::internal_error().with_message("Not a CancelledNotification".to_string()))
3366 }
3367 }
3368}
3369impl TryFrom<NotificationFromClient> for InitializedNotification {
3370 type Error = RpcError;
3371 fn try_from(value: NotificationFromClient) -> std::result::Result<Self, Self::Error> {
3372 let matched_type: ClientNotification = value.try_into()?;
3373 if let ClientNotification::InitializedNotification(result) = matched_type {
3374 Ok(result)
3375 } else {
3376 Err(RpcError::internal_error().with_message("Not a InitializedNotification".to_string()))
3377 }
3378 }
3379}
3380impl TryFrom<NotificationFromClient> for ProgressNotification {
3381 type Error = RpcError;
3382 fn try_from(value: NotificationFromClient) -> std::result::Result<Self, Self::Error> {
3383 let matched_type: ClientNotification = value.try_into()?;
3384 if let ClientNotification::ProgressNotification(result) = matched_type {
3385 Ok(result)
3386 } else {
3387 Err(RpcError::internal_error().with_message("Not a ProgressNotification".to_string()))
3388 }
3389 }
3390}
3391impl TryFrom<NotificationFromClient> for RootsListChangedNotification {
3392 type Error = RpcError;
3393 fn try_from(value: NotificationFromClient) -> std::result::Result<Self, Self::Error> {
3394 let matched_type: ClientNotification = value.try_into()?;
3395 if let ClientNotification::RootsListChangedNotification(result) = matched_type {
3396 Ok(result)
3397 } else {
3398 Err(RpcError::internal_error().with_message("Not a RootsListChangedNotification".to_string()))
3399 }
3400 }
3401}
3402impl TryFrom<RequestFromServer> for PingRequest {
3403 type Error = RpcError;
3404 fn try_from(value: RequestFromServer) -> std::result::Result<Self, Self::Error> {
3405 let matched_type: ServerRequest = value.try_into()?;
3406 if let ServerRequest::PingRequest(result) = matched_type {
3407 Ok(result)
3408 } else {
3409 Err(RpcError::internal_error().with_message("Not a PingRequest".to_string()))
3410 }
3411 }
3412}
3413impl TryFrom<RequestFromServer> for CreateMessageRequest {
3414 type Error = RpcError;
3415 fn try_from(value: RequestFromServer) -> std::result::Result<Self, Self::Error> {
3416 let matched_type: ServerRequest = value.try_into()?;
3417 if let ServerRequest::CreateMessageRequest(result) = matched_type {
3418 Ok(result)
3419 } else {
3420 Err(RpcError::internal_error().with_message("Not a CreateMessageRequest".to_string()))
3421 }
3422 }
3423}
3424impl TryFrom<RequestFromServer> for ListRootsRequest {
3425 type Error = RpcError;
3426 fn try_from(value: RequestFromServer) -> std::result::Result<Self, Self::Error> {
3427 let matched_type: ServerRequest = value.try_into()?;
3428 if let ServerRequest::ListRootsRequest(result) = matched_type {
3429 Ok(result)
3430 } else {
3431 Err(RpcError::internal_error().with_message("Not a ListRootsRequest".to_string()))
3432 }
3433 }
3434}
3435impl TryFrom<ResultFromServer> for Result {
3436 type Error = RpcError;
3437 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3438 let matched_type: ServerResult = value.try_into()?;
3439 if let ServerResult::Result(result) = matched_type {
3440 Ok(result)
3441 } else {
3442 Err(RpcError::internal_error().with_message("Not a Result".to_string()))
3443 }
3444 }
3445}
3446impl TryFrom<ResultFromServer> for InitializeResult {
3447 type Error = RpcError;
3448 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3449 let matched_type: ServerResult = value.try_into()?;
3450 if let ServerResult::InitializeResult(result) = matched_type {
3451 Ok(result)
3452 } else {
3453 Err(RpcError::internal_error().with_message("Not a InitializeResult".to_string()))
3454 }
3455 }
3456}
3457impl TryFrom<ResultFromServer> for ListResourcesResult {
3458 type Error = RpcError;
3459 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3460 let matched_type: ServerResult = value.try_into()?;
3461 if let ServerResult::ListResourcesResult(result) = matched_type {
3462 Ok(result)
3463 } else {
3464 Err(RpcError::internal_error().with_message("Not a ListResourcesResult".to_string()))
3465 }
3466 }
3467}
3468impl TryFrom<ResultFromServer> for ListResourceTemplatesResult {
3469 type Error = RpcError;
3470 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3471 let matched_type: ServerResult = value.try_into()?;
3472 if let ServerResult::ListResourceTemplatesResult(result) = matched_type {
3473 Ok(result)
3474 } else {
3475 Err(RpcError::internal_error().with_message("Not a ListResourceTemplatesResult".to_string()))
3476 }
3477 }
3478}
3479impl TryFrom<ResultFromServer> for ReadResourceResult {
3480 type Error = RpcError;
3481 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3482 let matched_type: ServerResult = value.try_into()?;
3483 if let ServerResult::ReadResourceResult(result) = matched_type {
3484 Ok(result)
3485 } else {
3486 Err(RpcError::internal_error().with_message("Not a ReadResourceResult".to_string()))
3487 }
3488 }
3489}
3490impl TryFrom<ResultFromServer> for ListPromptsResult {
3491 type Error = RpcError;
3492 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3493 let matched_type: ServerResult = value.try_into()?;
3494 if let ServerResult::ListPromptsResult(result) = matched_type {
3495 Ok(result)
3496 } else {
3497 Err(RpcError::internal_error().with_message("Not a ListPromptsResult".to_string()))
3498 }
3499 }
3500}
3501impl TryFrom<ResultFromServer> for GetPromptResult {
3502 type Error = RpcError;
3503 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3504 let matched_type: ServerResult = value.try_into()?;
3505 if let ServerResult::GetPromptResult(result) = matched_type {
3506 Ok(result)
3507 } else {
3508 Err(RpcError::internal_error().with_message("Not a GetPromptResult".to_string()))
3509 }
3510 }
3511}
3512impl TryFrom<ResultFromServer> for ListToolsResult {
3513 type Error = RpcError;
3514 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3515 let matched_type: ServerResult = value.try_into()?;
3516 if let ServerResult::ListToolsResult(result) = matched_type {
3517 Ok(result)
3518 } else {
3519 Err(RpcError::internal_error().with_message("Not a ListToolsResult".to_string()))
3520 }
3521 }
3522}
3523impl TryFrom<ResultFromServer> for CallToolResult {
3524 type Error = RpcError;
3525 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3526 let matched_type: ServerResult = value.try_into()?;
3527 if let ServerResult::CallToolResult(result) = matched_type {
3528 Ok(result)
3529 } else {
3530 Err(RpcError::internal_error().with_message("Not a CallToolResult".to_string()))
3531 }
3532 }
3533}
3534impl TryFrom<ResultFromServer> for CompleteResult {
3535 type Error = RpcError;
3536 fn try_from(value: ResultFromServer) -> std::result::Result<Self, Self::Error> {
3537 let matched_type: ServerResult = value.try_into()?;
3538 if let ServerResult::CompleteResult(result) = matched_type {
3539 Ok(result)
3540 } else {
3541 Err(RpcError::internal_error().with_message("Not a CompleteResult".to_string()))
3542 }
3543 }
3544}
3545impl TryFrom<NotificationFromServer> for CancelledNotification {
3546 type Error = RpcError;
3547 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3548 let matched_type: ServerNotification = value.try_into()?;
3549 if let ServerNotification::CancelledNotification(result) = matched_type {
3550 Ok(result)
3551 } else {
3552 Err(RpcError::internal_error().with_message("Not a CancelledNotification".to_string()))
3553 }
3554 }
3555}
3556impl TryFrom<NotificationFromServer> for ProgressNotification {
3557 type Error = RpcError;
3558 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3559 let matched_type: ServerNotification = value.try_into()?;
3560 if let ServerNotification::ProgressNotification(result) = matched_type {
3561 Ok(result)
3562 } else {
3563 Err(RpcError::internal_error().with_message("Not a ProgressNotification".to_string()))
3564 }
3565 }
3566}
3567impl TryFrom<NotificationFromServer> for ResourceListChangedNotification {
3568 type Error = RpcError;
3569 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3570 let matched_type: ServerNotification = value.try_into()?;
3571 if let ServerNotification::ResourceListChangedNotification(result) = matched_type {
3572 Ok(result)
3573 } else {
3574 Err(RpcError::internal_error().with_message("Not a ResourceListChangedNotification".to_string()))
3575 }
3576 }
3577}
3578impl TryFrom<NotificationFromServer> for ResourceUpdatedNotification {
3579 type Error = RpcError;
3580 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3581 let matched_type: ServerNotification = value.try_into()?;
3582 if let ServerNotification::ResourceUpdatedNotification(result) = matched_type {
3583 Ok(result)
3584 } else {
3585 Err(RpcError::internal_error().with_message("Not a ResourceUpdatedNotification".to_string()))
3586 }
3587 }
3588}
3589impl TryFrom<NotificationFromServer> for PromptListChangedNotification {
3590 type Error = RpcError;
3591 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3592 let matched_type: ServerNotification = value.try_into()?;
3593 if let ServerNotification::PromptListChangedNotification(result) = matched_type {
3594 Ok(result)
3595 } else {
3596 Err(RpcError::internal_error().with_message("Not a PromptListChangedNotification".to_string()))
3597 }
3598 }
3599}
3600impl TryFrom<NotificationFromServer> for ToolListChangedNotification {
3601 type Error = RpcError;
3602 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3603 let matched_type: ServerNotification = value.try_into()?;
3604 if let ServerNotification::ToolListChangedNotification(result) = matched_type {
3605 Ok(result)
3606 } else {
3607 Err(RpcError::internal_error().with_message("Not a ToolListChangedNotification".to_string()))
3608 }
3609 }
3610}
3611impl TryFrom<NotificationFromServer> for LoggingMessageNotification {
3612 type Error = RpcError;
3613 fn try_from(value: NotificationFromServer) -> std::result::Result<Self, Self::Error> {
3614 let matched_type: ServerNotification = value.try_into()?;
3615 if let ServerNotification::LoggingMessageNotification(result) = matched_type {
3616 Ok(result)
3617 } else {
3618 Err(RpcError::internal_error().with_message("Not a LoggingMessageNotification".to_string()))
3619 }
3620 }
3621}
3622impl CallToolResultContentItem {
3623 pub fn text_content(text: ::std::string::String, annotations: ::std::option::Option<Annotations>) -> Self {
3625 TextContent::new(text, annotations).into()
3626 }
3627 pub fn image_content(
3629 data: ::std::string::String,
3630 mime_type: ::std::string::String,
3631 annotations: ::std::option::Option<Annotations>,
3632 ) -> Self {
3633 ImageContent::new(data, mime_type, annotations).into()
3634 }
3635 pub fn audio_content(
3637 data: ::std::string::String,
3638 mime_type: ::std::string::String,
3639 annotations: ::std::option::Option<Annotations>,
3640 ) -> Self {
3641 AudioContent::new(data, mime_type, annotations).into()
3642 }
3643 pub fn embedded_resource(resource: EmbeddedResourceResource, annotations: ::std::option::Option<Annotations>) -> Self {
3645 EmbeddedResource::new(resource, annotations).into()
3646 }
3647 pub fn content_type(&self) -> &str {
3649 match self {
3650 CallToolResultContentItem::TextContent(text_content) => text_content.type_(),
3651 CallToolResultContentItem::ImageContent(image_content) => image_content.type_(),
3652 CallToolResultContentItem::AudioContent(audio_content) => audio_content.type_(),
3653 CallToolResultContentItem::EmbeddedResource(embedded_resource) => embedded_resource.type_(),
3654 }
3655 }
3656 pub fn as_text_content(&self) -> std::result::Result<&TextContent, RpcError> {
3658 match &self {
3659 CallToolResultContentItem::TextContent(text_content) => Ok(text_content),
3660 _ => Err(RpcError::internal_error().with_message(format!(
3661 "Invalid conversion, \"{}\" is not a {}",
3662 self.content_type(),
3663 "TextContent"
3664 ))),
3665 }
3666 }
3667 pub fn as_image_content(&self) -> std::result::Result<&ImageContent, RpcError> {
3669 match &self {
3670 CallToolResultContentItem::ImageContent(image_content) => Ok(image_content),
3671 _ => Err(RpcError::internal_error().with_message(format!(
3672 "Invalid conversion, \"{}\" is not a {}",
3673 self.content_type(),
3674 "ImageContent"
3675 ))),
3676 }
3677 }
3678 pub fn as_audio_content(&self) -> std::result::Result<&AudioContent, RpcError> {
3680 match &self {
3681 CallToolResultContentItem::AudioContent(audio_content) => Ok(audio_content),
3682 _ => Err(RpcError::internal_error().with_message(format!(
3683 "Invalid conversion, \"{}\" is not a {}",
3684 self.content_type(),
3685 "AudioContent"
3686 ))),
3687 }
3688 }
3689 pub fn as_embedded_resource(&self) -> std::result::Result<&EmbeddedResource, RpcError> {
3691 match &self {
3692 CallToolResultContentItem::EmbeddedResource(embedded_resource) => Ok(embedded_resource),
3693 _ => Err(RpcError::internal_error().with_message(format!(
3694 "Invalid conversion, \"{}\" is not a {}",
3695 self.content_type(),
3696 "EmbeddedResource"
3697 ))),
3698 }
3699 }
3700}
3701impl CallToolResult {
3702 pub fn text_content(text: ::std::string::String, annotations: ::std::option::Option<Annotations>) -> Self {
3703 Self {
3704 content: vec![TextContent::new(text, annotations).into()],
3705 is_error: None,
3706 meta: None,
3707 }
3708 }
3709 pub fn image_content(
3710 data: ::std::string::String,
3711 mime_type: ::std::string::String,
3712 annotations: ::std::option::Option<Annotations>,
3713 ) -> Self {
3714 Self {
3715 content: vec![ImageContent::new(data, mime_type, annotations).into()],
3716 is_error: None,
3717 meta: None,
3718 }
3719 }
3720 pub fn audio_content(
3721 data: ::std::string::String,
3722 mime_type: ::std::string::String,
3723 annotations: ::std::option::Option<Annotations>,
3724 ) -> Self {
3725 Self {
3726 content: vec![AudioContent::new(data, mime_type, annotations).into()],
3727 is_error: None,
3728 meta: None,
3729 }
3730 }
3731 pub fn embedded_resource(resource: EmbeddedResourceResource, annotations: ::std::option::Option<Annotations>) -> Self {
3732 Self {
3733 content: vec![EmbeddedResource::new(resource, annotations).into()],
3734 is_error: None,
3735 meta: None,
3736 }
3737 }
3738 pub fn with_error(error: CallToolError) -> Self {
3740 Self {
3741 content: vec![CallToolResultContentItem::TextContent(TextContent::new(
3742 error.to_string(),
3743 None,
3744 ))],
3745 is_error: Some(true),
3746 meta: None,
3747 }
3748 }
3749 pub fn with_meta(mut self, meta: Option<serde_json::Map<String, Value>>) -> Self {
3751 self.meta = meta;
3752 self
3753 }
3754}
3755#[cfg(test)]
3757mod tests {
3758 use super::*;
3759 use serde_json::json;
3760
3761 #[test]
3762 fn test_detect_message_type() {
3763 let message = ClientJsonrpcRequest::new(RequestId::Integer(0), PingRequest::new(None).into());
3765 let result = detect_message_type(&json!(message));
3766 assert!(matches!(result, MessageTypes::Request));
3767
3768 let result = detect_message_type(&json!({
3771 "id":0,
3772 "method":"add_numbers",
3773 "params":{},
3774 "jsonrpc":"2.0"
3775 }));
3776 assert!(matches!(result, MessageTypes::Request));
3777
3778 let message = ClientJsonrpcNotification::new(RootsListChangedNotification::new(None).into());
3780 let result = detect_message_type(&json!(message));
3781 assert!(matches!(result, MessageTypes::Notification));
3782
3783 let result = detect_message_type(&json!({
3785 "method":"notifications/email_sent",
3786 "jsonrpc":"2.0"
3787 }));
3788 assert!(matches!(result, MessageTypes::Notification));
3789
3790 let message = ClientJsonrpcResponse::new(
3792 RequestId::Integer(0),
3793 ListRootsResult {
3794 meta: None,
3795 roots: vec![],
3796 }
3797 .into(),
3798 );
3799 let result = detect_message_type(&json!(message));
3800 assert!(matches!(result, MessageTypes::Response));
3801
3802 let result = detect_message_type(&json!({
3805 "id":1,
3806 "jsonrpc":"2.0",
3807 "result":"{}",
3808 }));
3809 assert!(matches!(result, MessageTypes::Response));
3810
3811 let message = JsonrpcError::create(
3813 RequestId::Integer(0),
3814 RpcErrorCodes::INVALID_PARAMS,
3815 "Invalid params!".to_string(),
3816 None,
3817 );
3818 let result = detect_message_type(&json!(message));
3819 assert!(matches!(result, MessageTypes::Error));
3820
3821 let result = detect_message_type(&json!({}));
3823 assert!(matches!(result, MessageTypes::Request));
3824 }
3825}