mcpx 0.1.5

A Rust SDK for the Model Context Protocol (MCP)
Documentation
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Client message handler implementation

use std::sync::Arc;
use tokio::sync::{mpsc, RwLock};
use log::{debug, error, info, warn};

use crate::error::Error;
use crate::protocol::{
    RequestId, ProgressToken,
    JSONRPCMessage, JSONRPCRequest, JSONRPCResponse, JSONRPCError, JSONRPCNotification,
    roots::ListRootsRequest,
    logging::LoggingLevel,
};

use super::{ClientEvent, ClientOptions, ServerCapabilities, state::ClientState, state::PendingRequest};
use dashmap::DashMap;

/// Handles incoming messages for a client
pub(crate) struct ClientMessageHandler {
    /// Client state
    state: Arc<RwLock<ClientState>>,
    /// Pending requests
    pending_requests: Arc<DashMap<RequestId, PendingRequest>>,
    /// Event sender
    event_sender: mpsc::Sender<ClientEvent>,
    /// Server capabilities
    server_capabilities: Arc<RwLock<Option<ServerCapabilities>>>,
    /// Client options
    options: ClientOptions,
}

impl ClientMessageHandler {
    /// Create a new client message handler
    pub fn new(
        state: Arc<RwLock<ClientState>>,
        pending_requests: Arc<DashMap<RequestId, PendingRequest>>,
        event_sender: mpsc::Sender<ClientEvent>,
        server_capabilities: Arc<RwLock<Option<ServerCapabilities>>>,
        options: ClientOptions,
    ) -> Self {
        Self {
            state,
            pending_requests,
            event_sender,
            server_capabilities,
            options,
        }
    }

    /// Handle an incoming message from the server
    pub async fn handle_message(&self, message: JSONRPCMessage) -> Result<(), Error> {
        match message {
            JSONRPCMessage::Request(request) => self.handle_request(request).await,
            JSONRPCMessage::Response(response) => self.handle_response(response).await,
            JSONRPCMessage::Error(error) => self.handle_error(error).await,
            JSONRPCMessage::Notification(notification) => {
                self.handle_notification(notification).await
            }
            _ => Err(Error::ProtocolError("Unsupported message type".to_string())),
        }
    }

    /// Handle a request from the server
    async fn handle_request(&self, request: JSONRPCRequest) -> Result<(), Error> {
        debug!("Handling request: {}", request.method);

        match request.method.as_str() {
            "ping" => {
                // Handle ping request - just respond with empty result
                // We don't need to forward this to the application
                self.respond_success(request.id, serde_json::json!({})).await
            }
            "roots/list" => {
                // Roots list request - need to emit an event for the application to handle
                if !self.options.capabilities.roots {
                    return self.respond_error(
                        request.id,
                        -32601, // Method not found
                        "Client does not support roots".to_string(),
                        None,
                    ).await;
                }

                // Forward to application via event
                self.event_sender
                    .send(ClientEvent::RootsChanged)
                    .await
                    .map_err(|_| Error::InternalError("Failed to send event".to_string()))?;

                // If auto-acknowledge is enabled, send empty roots list
                if self.options.auto_acknowledge_roots_changed {
                    self.respond_success(
                        request.id,
                        serde_json::json!({ "roots": [] }),
                    ).await?;
                }

                Ok(())
            }
            "sampling/createMessage" => {
                // Sampling request - need to emit an event for the application to handle
                if !self.options.capabilities.sampling {
                    return self.respond_error(
                        request.id,
                        -32601, // Method not found
                        "Client does not support sampling".to_string(),
                        None,
                    ).await;
                }

                // This is handled by the application, which should respond directly
                // For now, return error
                self.respond_error(
                    request.id,
                    -32601, // Method not found
                    "Sampling not implemented".to_string(),
                    None,
                ).await
            }
            _ => {
                // Unknown request
                self.respond_error(
                    request.id,
                    -32601, // Method not found
                    format!("Method not found: {}", request.method),
                    None,
                ).await
            }
        }
    }

    /// Handle a response from the server
    async fn handle_response(&self, response: JSONRPCResponse) -> Result<(), Error> {
        debug!("Handling response for request: {:?}", response.id);

        // Find the pending request
        if let Some((_, pending)) = self.pending_requests.remove(&response.id) {
            // Send the response back to the waiting call
            if let Err(_) = pending.sender.send(JSONRPCMessage::Response(response)) {
                error!("Failed to send response to requester");
                return Err(Error::InternalError(
                    "Failed to send response to requester".to_string(),
                ));
            }
            Ok(())
        } else {
            warn!("Received response for unknown request: {:?}", response.id);
            Err(Error::ProtocolError(format!(
                "Received response for unknown request: {:?}",
                response.id
            )))
        }
    }

    /// Handle an error from the server
    async fn handle_error(&self, error: JSONRPCError) -> Result<(), Error> {
        debug!("Handling error for request: {:?}", error.id);

        // Find the pending request
        if let Some((_, pending)) = self.pending_requests.remove(&error.id) {
            // Send the error back to the waiting call
            if let Err(_) = pending.sender.send(JSONRPCMessage::Error(error)) {
                error!("Failed to send error to requester");
                return Err(Error::InternalError(
                    "Failed to send error to requester".to_string(),
                ));
            }
            Ok(())
        } else {
            warn!("Received error for unknown request: {:?}", error.id);
            Err(Error::ProtocolError(format!(
                "Received error for unknown request: {:?}",
                error.id
            )))
        }
    }

    /// Handle a notification from the server
    async fn handle_notification(&self, notification: JSONRPCNotification) -> Result<(), Error> {
        debug!("Handling notification: {}", notification.method);

        match notification.method.as_str() {
            "notifications/cancelled" => {
                // Handle cancellation notification
                self.handle_cancelled_notification(notification).await
            }
            "notifications/progress" => {
                // Handle progress notification
                self.handle_progress_notification(notification).await
            }
            "notifications/resources/list_changed" => {
                // Handle resources list changed notification
                self.handle_resources_changed_notification().await
            }
            "notifications/resources/updated" => {
                // Handle resource updated notification
                self.handle_resource_updated_notification(notification).await
            }
            "notifications/prompts/list_changed" => {
                // Handle prompts list changed notification
                self.handle_prompts_changed_notification().await
            }
            "notifications/tools/list_changed" => {
                // Handle tools list changed notification
                self.handle_tools_changed_notification().await
            }
            "notifications/message" => {
                // Handle logging message notification
                self.handle_logging_notification(notification).await
            }
            _ => {
                warn!("Received unknown notification: {}", notification.method);
                Err(Error::ProtocolError(format!(
                    "Received unknown notification: {}",
                    notification.method
                )))
            }
        }
    }

    /// Handle a cancelled notification
    async fn handle_cancelled_notification(
        &self,
        notification: JSONRPCNotification,
    ) -> Result<(), Error> {
        let params = notification
            .params
            .ok_or_else(|| Error::ProtocolError("Missing params in cancelled notification".to_string()))?;

        let request_id: RequestId = serde_json::from_value(params["requestId"].clone())
            .map_err(|e| Error::ParseError(e.to_string()))?;

        // Find and remove the pending request
        if let Some((_, _)) = self.pending_requests.remove(&request_id) {
            debug!("Request cancelled: {:?}", request_id);
            // We don't need to notify the requester, as the server just won't send a response
            Ok(())
        } else {
            warn!("Received cancellation for unknown request: {:?}", request_id);
            Err(Error::ProtocolError(format!(
                "Received cancellation for unknown request: {:?}",
                request_id
            )))
        }
    }

    /// Handle a progress notification
    async fn handle_progress_notification(
        &self,
        notification: JSONRPCNotification,
    ) -> Result<(), Error> {
        let params = notification
            .params
            .ok_or_else(|| Error::ProtocolError("Missing params in progress notification".to_string()))?;

        let token: ProgressToken = serde_json::from_value(params["progressToken"].clone())
            .map_err(|e| Error::ParseError(e.to_string()))?;

        let progress = params["progress"]
            .as_f64()
            .ok_or_else(|| Error::ParseError("Missing or invalid progress".to_string()))?;

        let total = params["total"].as_f64();

        let message = params["message"].as_str().map(|s| s.to_string());

        // We need to map the progress token to a request ID
        // For now, we'll use the token directly as the request ID
        let request_id = match &token {
            ProgressToken::String(s) => RequestId::String(s.clone()),
            ProgressToken::Integer(i) => RequestId::Integer(*i),
        };

        // Emit progress event
        self.event_sender
            .send(ClientEvent::Progress {
                request_id,
                token,
                progress,
                total,
                message,
            })
            .await
            .map_err(|_| Error::InternalError("Failed to send event".to_string()))?;

        Ok(())
    }

    /// Handle a resources list changed notification
    async fn handle_resources_changed_notification(&self) -> Result<(), Error> {
        // Check if server supports resources list changed notifications
        let caps = self.server_capabilities.read().await;
        if let Some(caps) = &*caps {
            if !caps.resources || !caps.resources_list_changed {
                return Err(Error::ProtocolError(
                    "Server sent resources/list_changed but doesn't support it".to_string(),
                ));
            }
        } else {
            return Err(Error::NotInitialized);
        }

        // Emit resources changed event
        self.event_sender
            .send(ClientEvent::ResourcesChanged)
            .await
            .map_err(|_| Error::InternalError("Failed to send event".to_string()))?;

        Ok(())
    }

    /// Handle a resource updated notification
    async fn handle_resource_updated_notification(
        &self,
        notification: JSONRPCNotification,
    ) -> Result<(), Error> {
        let params = notification
            .params
            .ok_or_else(|| Error::ProtocolError("Missing params in resource updated notification".to_string()))?;

        let uri = params["uri"]
            .as_str()
            .ok_or_else(|| Error::ParseError("Missing or invalid uri".to_string()))?
            .to_string();

        // Emit resource updated event
        self.event_sender
            .send(ClientEvent::ResourceUpdated { uri })
            .await
            .map_err(|_| Error::InternalError("Failed to send event".to_string()))?;

        Ok(())
    }

    /// Handle a prompts list changed notification
    async fn handle_prompts_changed_notification(&self) -> Result<(), Error> {
        // Check if server supports prompts list changed notifications
        let caps = self.server_capabilities.read().await;
        if let Some(caps) = &*caps {
            if !caps.prompts || !caps.prompts_list_changed {
                return Err(Error::ProtocolError(
                    "Server sent prompts/list_changed but doesn't support it".to_string(),
                ));
            }
        } else {
            return Err(Error::NotInitialized);
        }

        // Emit prompts changed event
        self.event_sender
            .send(ClientEvent::PromptsChanged)
            .await
            .map_err(|_| Error::InternalError("Failed to send event".to_string()))?;

        Ok(())
    }

    /// Handle a tools list changed notification
    async fn handle_tools_changed_notification(&self) -> Result<(), Error> {
        // Check if server supports tools list changed notifications
        let caps = self.server_capabilities.read().await;
        if let Some(caps) = &*caps {
            if !caps.tools || !caps.tools_list_changed {
                return Err(Error::ProtocolError(
                    "Server sent tools/list_changed but doesn't support it".to_string(),
                ));
            }
        } else {
            return Err(Error::NotInitialized);
        }

        // Emit tools changed event
        self.event_sender
            .send(ClientEvent::ToolsChanged)
            .await
            .map_err(|_| Error::InternalError("Failed to send event".to_string()))?;

        Ok(())
    }

    /// Handle a logging notification
    async fn handle_logging_notification(
        &self,
        notification: JSONRPCNotification,
    ) -> Result<(), Error> {
        let params = notification
            .params
            .ok_or_else(|| Error::ProtocolError("Missing params in logging notification".to_string()))?;

        let level: LoggingLevel = serde_json::from_value(params["level"].clone())
            .map_err(|e| Error::ParseError(e.to_string()))?;

        let logger = params["logger"].as_str().map(|s| s.to_string());

        let data = params["data"].clone();

        // Emit log message event
        self.event_sender
            .send(ClientEvent::LogMessage {
                level,
                logger,
                data,
            })
            .await
            .map_err(|_| Error::InternalError("Failed to send event".to_string()))?;

        Ok(())
    }

    /// Send a success response
    async fn respond_success(
        &self,
        id: RequestId,
        result: serde_json::Value,
    ) -> Result<(), Error> {
        // We would normally send a response through the transport
        // For now, just emit an error that this is not implemented
        Err(Error::InternalError("Response sending not implemented".to_string()))
    }

    /// Send an error response
    async fn respond_error(
        &self,
        id: RequestId,
        code: i32,
        message: String,
        data: Option<serde_json::Value>,
    ) -> Result<(), Error> {
        // We would normally send a response through the transport
        // For now, just emit an error that this is not implemented
        Err(Error::InternalError("Error sending not implemented".to_string()))
    }
}