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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
* Create a local SSE server that proxies requests to a stdio MCP server.
*/
use rmcp::{
model::{
CallToolRequestParam, CallToolResult, ClientInfo, Content, Implementation, ListToolsResult,
PaginatedRequestParam, ServerInfo,
},
service::{RequestContext, RunningService},
Error, RoleClient, RoleServer, ServerHandler,
};
use std::sync::Arc;
use tokio::sync::Mutex;
use tracing::debug;
/// A proxy handler that forwards requests to a client based on the server's capabilities
#[derive(Clone)]
pub struct ProxyHandler {
client: Arc<Mutex<RunningService<RoleClient, ClientInfo>>>,
// Store the server's capabilities to avoid locking the client on every get_info call
cached_info: Arc<ServerInfo>,
}
impl ServerHandler for ProxyHandler {
fn get_info(&self) -> ServerInfo {
// Return the cached server info with capabilities
self.cached_info.as_ref().clone()
}
async fn list_tools(
&self,
request: PaginatedRequestParam,
_context: RequestContext<RoleServer>,
) -> Result<ListToolsResult, Error> {
let client = self.client.clone();
let guard = client.lock().await;
// Check if the server has tools capability and forward the request
match self.cached_info.capabilities.tools {
Some(_) => {
match guard.list_tools(request).await {
// Forward request to client
Ok(result) => {
debug!(
"Proxying list_tools response with {} tools",
result.tools.len()
);
Ok(result)
}
Err(err) => {
tracing::error!("Error listing tools: {:?}", err);
// Return empty list instead of error
Ok(ListToolsResult::default())
}
}
}
None => {
// Server doesn't support tools, return empty list
tracing::error!("Server doesn't support tools capability");
Ok(ListToolsResult::default())
}
}
}
async fn call_tool(
&self,
request: CallToolRequestParam,
_context: RequestContext<RoleServer>,
) -> Result<CallToolResult, Error> {
let client = self.client.clone();
let guard = client.lock().await;
// Check if the server has tools capability and forward the request
match self.cached_info.capabilities.tools {
Some(_) => {
match guard.call_tool(request.clone()).await {
Ok(result) => {
debug!("Tool call succeeded");
Ok(result)
}
Err(err) => {
tracing::error!("Error calling tool: {:?}", err);
// Return an error result instead of propagating the error
Ok(CallToolResult::error(vec![Content::text(format!(
"Error: {}",
err
))]))
}
}
}
None => {
tracing::error!("Server doesn't support tools capability");
Ok(CallToolResult::error(vec![Content::text(
"Server doesn't support tools capability",
)]))
}
}
}
async fn list_resources(
&self,
request: PaginatedRequestParam,
_context: RequestContext<RoleServer>,
) -> Result<rmcp::model::ListResourcesResult, Error> {
// Get a lock on the client
let client = self.client.clone();
let guard = client.lock().await;
// Check if the server has resources capability and forward the request
match self.cached_info.capabilities.resources {
Some(_) => {
// Forward request to client
match guard.list_resources(request).await {
Ok(result) => {
debug!("Proxying list_resources response");
Ok(result)
}
Err(err) => {
tracing::error!("Error listing resources: {:?}", err);
// Return empty list instead of error
Ok(rmcp::model::ListResourcesResult::default())
}
}
}
None => {
// Server doesn't support resources, return empty list
tracing::error!("Server doesn't support resources capability");
Ok(rmcp::model::ListResourcesResult::default())
}
}
}
async fn read_resource(
&self,
request: rmcp::model::ReadResourceRequestParam,
_context: RequestContext<RoleServer>,
) -> Result<rmcp::model::ReadResourceResult, Error> {
// Get a lock on the client
let client = self.client.clone();
let guard = client.lock().await;
// Check if the server has resources capability and forward the request
match self.cached_info.capabilities.resources {
Some(_) => {
// Forward request to client
match guard
.read_resource(rmcp::model::ReadResourceRequestParam {
uri: request.uri.clone(),
})
.await
{
Ok(result) => {
debug!("Proxying read_resource response for {}", request.uri);
Ok(result)
}
Err(err) => {
tracing::error!("Error reading resource: {:?}", err);
Err(Error::internal_error(
format!("Error reading resource: {}", err),
None,
))
}
}
}
None => {
// Server doesn't support resources, return error
tracing::error!("Server doesn't support resources capability");
Err(Error::internal_error(
"Server doesn't support resources capability".to_string(),
None,
))
}
}
}
async fn list_resource_templates(
&self,
request: PaginatedRequestParam,
_context: RequestContext<RoleServer>,
) -> Result<rmcp::model::ListResourceTemplatesResult, Error> {
// Get a lock on the client
let client = self.client.clone();
let guard = client.lock().await;
// Check if the server has resources capability and forward the request
match self.cached_info.capabilities.resources {
Some(_) => {
// Forward request to client
match guard.list_resource_templates(request).await {
Ok(result) => {
debug!("Proxying list_resource_templates response");
Ok(result)
}
Err(err) => {
tracing::error!("Error listing resource templates: {:?}", err);
// Return empty list instead of error
Ok(rmcp::model::ListResourceTemplatesResult::default())
}
}
}
None => {
// Server doesn't support resources, return empty list
tracing::error!("Server doesn't support resources capability");
Ok(rmcp::model::ListResourceTemplatesResult::default())
}
}
}
async fn list_prompts(
&self,
request: PaginatedRequestParam,
_context: RequestContext<RoleServer>,
) -> Result<rmcp::model::ListPromptsResult, Error> {
// Get a lock on the client
let client = self.client.clone();
let guard = client.lock().await;
// Check if the server has prompts capability and forward the request
match self.cached_info.capabilities.prompts {
Some(_) => {
// Forward request to client
match guard.list_prompts(request).await {
Ok(result) => {
debug!("Proxying list_prompts response");
Ok(result)
}
Err(err) => {
tracing::error!("Error listing prompts: {:?}", err);
// Return empty list instead of error
Ok(rmcp::model::ListPromptsResult::default())
}
}
}
None => {
// Server doesn't support prompts, return empty list
tracing::error!("Server doesn't support prompts capability");
Ok(rmcp::model::ListPromptsResult::default())
}
}
}
async fn get_prompt(
&self,
request: rmcp::model::GetPromptRequestParam,
_context: RequestContext<RoleServer>,
) -> Result<rmcp::model::GetPromptResult, Error> {
// Get a lock on the client
let client = self.client.clone();
let guard = client.lock().await;
// Check if the server has prompts capability and forward the request
match self.cached_info.capabilities.prompts {
Some(_) => {
// Forward request to client
match guard.get_prompt(request).await {
Ok(result) => {
debug!("Proxying get_prompt response");
Ok(result)
}
Err(err) => {
tracing::error!("Error getting prompt: {:?}", err);
Err(Error::internal_error(
format!("Error getting prompt: {}", err),
None,
))
}
}
}
None => {
// Server doesn't support prompts, return error
tracing::error!("Server doesn't support prompts capability");
Err(Error::internal_error(
"Server doesn't support prompts capability".to_string(),
None,
))
}
}
}
async fn complete(
&self,
request: rmcp::model::CompleteRequestParam,
_context: RequestContext<RoleServer>,
) -> Result<rmcp::model::CompleteResult, Error> {
// Get a lock on the client
let client = self.client.clone();
let guard = client.lock().await;
// Forward request to client
match guard.complete(request).await {
Ok(result) => {
debug!("Proxying complete response");
Ok(result)
}
Err(err) => {
tracing::error!("Error completing: {:?}", err);
Err(Error::internal_error(
format!("Error completing: {}", err),
None,
))
}
}
}
async fn on_progress(&self, notification: rmcp::model::ProgressNotificationParam) {
// Get a lock on the client
let client = self.client.clone();
let guard = client.lock().await;
match guard.notify_progress(notification).await {
Ok(_) => {
debug!("Proxying progress notification");
}
Err(err) => {
tracing::error!("Error notifying progress: {:?}", err);
}
}
}
async fn on_cancelled(&self, notification: rmcp::model::CancelledNotificationParam) {
// Get a lock on the client
let client = self.client.clone();
let guard = client.lock().await;
match guard.notify_cancelled(notification).await {
Ok(_) => {
debug!("Proxying cancelled notification");
}
Err(err) => {
tracing::error!("Error notifying cancelled: {:?}", err);
}
}
}
}
impl ProxyHandler {
pub fn new(client: RunningService<RoleClient, ClientInfo>) -> Self {
let peer_info = client.peer_info();
// Create a ServerInfo object that forwards the server's capabilities
let cached_info = ServerInfo {
protocol_version: peer_info.protocol_version.clone(),
server_info: Implementation {
name: peer_info.server_info.name.clone(),
version: peer_info.server_info.version.clone(),
},
instructions: peer_info.instructions.clone(),
capabilities: peer_info.capabilities.clone(),
};
Self {
client: Arc::new(Mutex::new(client)),
cached_info: Arc::new(cached_info),
}
}
}