forked from modelcontextprotocol/rust-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.rs
More file actions
134 lines (124 loc) · 4.32 KB
/
client.rs
File metadata and controls
134 lines (124 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use crate::{
error::Error as McpError,
model::*,
service::{RequestContext, RoleClient, Service, ServiceRole},
};
impl<H: ClientHandler> Service<RoleClient> for H {
async fn handle_request(
&self,
request: <RoleClient as ServiceRole>::PeerReq,
context: RequestContext<RoleClient>,
) -> Result<<RoleClient as ServiceRole>::Resp, McpError> {
match request {
ServerRequest::PingRequest(_) => self.ping(context).await.map(ClientResult::empty),
ServerRequest::CreateMessageRequest(request) => self
.create_message(request.params, context)
.await
.map(ClientResult::CreateMessageResult),
ServerRequest::ListRootsRequest(_) => self
.list_roots(context)
.await
.map(ClientResult::ListRootsResult),
}
}
async fn handle_notification(
&self,
notification: <RoleClient as ServiceRole>::PeerNot,
) -> Result<(), McpError> {
match notification {
ServerNotification::CancelledNotification(notification) => {
self.on_cancelled(notification.params).await
}
ServerNotification::ProgressNotification(notification) => {
self.on_progress(notification.params).await
}
ServerNotification::LoggingMessageNotification(notification) => {
self.on_logging_message(notification.params).await
}
ServerNotification::ResourceUpdatedNotification(notification) => {
self.on_resource_updated(notification.params).await
}
ServerNotification::ResourceListChangedNotification(_notification_no_param) => {
self.on_resource_list_changed().await
}
ServerNotification::ToolListChangedNotification(_notification_no_param) => {
self.on_tool_list_changed().await
}
ServerNotification::PromptListChangedNotification(_notification_no_param) => {
self.on_prompt_list_changed().await
}
};
Ok(())
}
fn get_info(&self) -> <RoleClient as ServiceRole>::Info {
self.get_info()
}
}
#[allow(unused_variables)]
pub trait ClientHandler: Sized + Send + Sync + 'static {
fn ping(
&self,
context: RequestContext<RoleClient>,
) -> impl Future<Output = Result<(), McpError>> + Send + '_ {
std::future::ready(Ok(()))
}
fn create_message(
&self,
params: CreateMessageRequestParam,
context: RequestContext<RoleClient>,
) -> impl Future<Output = Result<CreateMessageResult, McpError>> + Send + '_ {
std::future::ready(Err(
McpError::method_not_found::<CreateMessageRequestMethod>(),
))
}
fn list_roots(
&self,
context: RequestContext<RoleClient>,
) -> impl Future<Output = Result<ListRootsResult, McpError>> + Send + '_ {
std::future::ready(Ok(ListRootsResult::default()))
}
fn on_cancelled(
&self,
params: CancelledNotificationParam,
) -> impl Future<Output = ()> + Send + '_ {
std::future::ready(())
}
fn on_progress(
&self,
params: ProgressNotificationParam,
) -> impl Future<Output = ()> + Send + '_ {
std::future::ready(())
}
fn on_logging_message(
&self,
params: LoggingMessageNotificationParam,
) -> impl Future<Output = ()> + Send + '_ {
std::future::ready(())
}
fn on_resource_updated(
&self,
params: ResourceUpdatedNotificationParam,
) -> impl Future<Output = ()> + Send + '_ {
std::future::ready(())
}
fn on_resource_list_changed(&self) -> impl Future<Output = ()> + Send + '_ {
std::future::ready(())
}
fn on_tool_list_changed(&self) -> impl Future<Output = ()> + Send + '_ {
std::future::ready(())
}
fn on_prompt_list_changed(&self) -> impl Future<Output = ()> + Send + '_ {
std::future::ready(())
}
fn get_info(&self) -> ClientInfo {
ClientInfo::default()
}
}
/// Do nothing, with default client info.
impl ClientHandler for () {}
/// Do nothing, with a specific client info.
impl ClientHandler for ClientInfo {
fn get_info(&self) -> ClientInfo {
self.clone()
}
}