rs-fast-mcp 0.2.0

High-performance, async-first Rust implementation of 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use crate::error::FastMCPError;
use crate::mcp::types::{JsonRpcMessage, JsonRpcRequest, JsonRpcResponse, RequestId};

use dashmap::DashMap;
use serde_json::Value;
use std::sync::Arc;
use std::sync::atomic::{AtomicI64, Ordering};
use tokio::sync::oneshot;
use tracing::{debug, error, warn};

pub mod builder;

pub mod transport;
pub use crate::client::transport::ClientTransport;
pub mod auth;

type RequestHandler = Box<
    dyn Fn(
            JsonRpcRequest,
        ) -> std::pin::Pin<
            Box<dyn std::future::Future<Output = Result<Option<Value>, FastMCPError>> + Send>,
        > + Send
        + Sync,
>;

pub struct Client {
    transport: Arc<Box<dyn ClientTransport>>,
    request_id_counter: AtomicI64,
    pending_requests:
        Arc<DashMap<RequestId, oneshot::Sender<Result<JsonRpcResponse, FastMCPError>>>>,
    request_handlers: Arc<DashMap<String, RequestHandler>>,
    timeout: Option<std::time::Duration>,
}

impl Client {
    pub fn new(transport: Box<dyn ClientTransport>) -> Self {
        let transport = Arc::new(transport);
        let pending_requests = Arc::new(DashMap::new());
        let request_handlers: Arc<DashMap<String, RequestHandler>> = Arc::new(DashMap::new());

        let client = Self {
            transport: transport.clone(),
            request_id_counter: AtomicI64::new(1),
            pending_requests: pending_requests.clone(),
            request_handlers: request_handlers.clone(),
            timeout: None,
        };

        client.start_background_loop(transport, pending_requests, request_handlers);
        client
    }

    pub fn builder(transport: Box<dyn ClientTransport>) -> crate::client::builder::ClientBuilder {
        crate::client::builder::ClientBuilder::new(transport)
    }

    pub(crate) fn set_timeout(&mut self, timeout: Option<std::time::Duration>) {
        self.timeout = timeout;
    }

    fn start_background_loop(
        &self,
        transport: Arc<Box<dyn ClientTransport>>,
        pending_requests: Arc<
            DashMap<RequestId, oneshot::Sender<Result<JsonRpcResponse, FastMCPError>>>,
        >,
        request_handlers: Arc<DashMap<String, RequestHandler>>,
    ) {
        let transport_clone = transport.clone();
        tokio::spawn(async move {
            loop {
                match transport.receive().await {
                    Ok(message) => {
                        match message {
                            JsonRpcMessage::Response(response) => {
                                if let Some((_, sender)) = pending_requests.remove(&response.id) {
                                    let _ = sender.send(Ok(response));
                                } else {
                                    warn!(
                                        "Received response for unknown request ID: {:?}",
                                        response.id
                                    );
                                }
                            }
                            JsonRpcMessage::Error(err_msg) => {
                                // ErrorResponse also has ID
                                if let Some((_, sender)) = pending_requests.remove(&err_msg.id) {
                                    let err = FastMCPError::JsonRpcError {
                                        code: err_msg.error.code,
                                        message: err_msg.error.message,
                                        data: err_msg.error.data,
                                    };
                                    let _ = sender.send(Err(err));
                                } else {
                                    warn!(
                                        "Received error for unknown request ID: {:?}",
                                        err_msg.id
                                    );
                                }
                            }
                            JsonRpcMessage::Request(request) => {
                                // Handle incoming request (e.g. Sampling, Ping, Roots)
                                let method = request.method.clone();
                                let id = request.id.clone();
                                let handlers = request_handlers.clone();
                                let transport = transport_clone.clone();

                                tokio::spawn(async move {
                                    let result = if let Some(handler) = handlers.get(&method) {
                                        handler(request).await
                                    } else {
                                        // Default: Method Not Found?
                                        Err(FastMCPError::InvalidRequest(format!(
                                            "Method not found: {}",
                                            method
                                        )))
                                    };

                                    // Send Response back
                                    let response = match result {
                                        Ok(res_opt) => {
                                            // Result successful
                                            JsonRpcMessage::Response(JsonRpcResponse {
                                                jsonrpc: "2.0".to_string(),
                                                id,
                                                result: res_opt.unwrap_or(serde_json::Value::Null),
                                            })
                                        }
                                        Err(e) => {
                                            // Send Error
                                            use crate::mcp::types::ErrorData;
                                            use crate::mcp::types::JsonRpcError;
                                            JsonRpcMessage::Error(JsonRpcError {
                                                jsonrpc: "2.0".to_string(),
                                                id,
                                                error: ErrorData {
                                                    code: -32603, // Internal Error default?
                                                    message: e.to_string(),
                                                    data: None,
                                                },
                                            })
                                        }
                                    };

                                    if let Err(e) = transport.send(response).await {
                                        error!("Failed to send response: {}", e);
                                    }
                                });
                            }
                            JsonRpcMessage::Notification(notif) => {
                                debug!("Received notification: {}", notif.method);
                                // Currently just log, or dispatch to handlers?
                            }
                        }
                    }
                    Err(e) => {
                        error!("Transport receive error: {}. Stopping client loop.", e);
                        break;
                    }
                }
            }
        });
    }

    pub fn register_handler<F, Fut>(&self, method: &str, handler: F)
    where
        F: Fn(JsonRpcRequest) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = Result<Option<Value>, FastMCPError>> + Send + 'static,
    {
        self.request_handlers.insert(
            method.to_string(),
            Box::new(move |req| Box::pin(handler(req))),
        );
    }

    pub fn register_sampling_handler<F, Fut>(&self, handler: F)
    where
        F: Fn(crate::mcp::types::CreateMessageRequestParams) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<
                Output = Result<crate::mcp::types::CreateMessageResult, FastMCPError>,
            > + Send
            + 'static,
    {
        let handler = Arc::new(handler);
        self.register_handler("sampling/createMessage", move |req| {
            let handler = handler.clone();
            Box::pin(async move {
                let params_val = req
                    .params
                    .ok_or(FastMCPError::InvalidRequest("Missing params".to_string()))?;
                let params: crate::mcp::types::CreateMessageRequestParams =
                    serde_json::from_value(params_val).map_err(FastMCPError::Json)?;

                let result = handler(params).await?;
                Ok(Some(
                    serde_json::to_value(result).map_err(FastMCPError::Json)?,
                ))
            })
        });
    }

    pub fn register_roots_list_handler<F, Fut>(&self, handler: F)
    where
        F: Fn() -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = Result<crate::mcp::types::ListRootsResult, FastMCPError>>
            + Send
            + 'static,
    {
        let handler = Arc::new(handler);
        self.register_handler("roots/list", move |_req| {
            let handler = handler.clone();
            Box::pin(async move {
                let result = handler().await?;
                Ok(Some(
                    serde_json::to_value(result).map_err(FastMCPError::Json)?,
                ))
            })
        });
    }

    fn next_id(&self) -> RequestId {
        RequestId::Int(self.request_id_counter.fetch_add(1, Ordering::SeqCst))
    }

    pub async fn request(
        &self,
        method: &str,
        params: Option<Value>,
    ) -> Result<Value, FastMCPError> {
        let id = self.next_id();
        let req = JsonRpcRequest {
            jsonrpc: "2.0".to_string(),
            method: method.to_string(),
            params,
            id: id.clone(),
            transport_metadata: None,
        };

        let (tx, rx) = oneshot::channel();
        self.pending_requests.insert(id, tx);

        self.transport.send(JsonRpcMessage::Request(req)).await?;

        // Wait for response with optional timeout
        let rx_outer_result = if let Some(duration) = self.timeout {
            match tokio::time::timeout(duration, rx).await {
                Ok(res) => {
                    res.map_err(|_| FastMCPError::InvalidRequest("Sender dropped".to_string()))
                }
                Err(_) => Err(FastMCPError::InvalidRequest(
                    "Request timed out".to_string(),
                )),
            }
        } else {
            rx.await
                .map_err(|_| FastMCPError::InvalidRequest("Sender dropped".to_string()))
        };

        match rx_outer_result {
            Ok(res_inner) => match res_inner {
                Ok(resp) => Ok(resp.result),
                Err(e) => Err(e),
            },
            Err(e) => Err(e),
        }
    }

    pub async fn list_tools(&self) -> Result<Vec<crate::mcp::types::Tool>, FastMCPError> {
        let result = self.request("tools/list", None).await?;
        let tools_val = result.get("tools").ok_or(FastMCPError::InvalidRequest(
            "Missing 'tools' field".to_string(),
        ))?;
        serde_json::from_value(tools_val.clone()).map_err(FastMCPError::Json)
    }

    pub async fn call_tool(
        &self,
        name: &str,
        arguments: Value,
    ) -> Result<crate::tools::tool::ToolResult, FastMCPError> {
        let params = serde_json::json!({
            "name": name,
            "arguments": arguments
        });
        let result = self.request("tools/call", Some(params)).await?;
        serde_json::from_value(result).map_err(FastMCPError::Json)
    }

    pub async fn list_resources(&self) -> Result<Vec<crate::mcp::types::Resource>, FastMCPError> {
        let result = self.request("resources/list", None).await?;
        let res_val = result.get("resources").ok_or(FastMCPError::InvalidRequest(
            "Missing 'resources' field".to_string(),
        ))?;
        serde_json::from_value(res_val.clone()).map_err(FastMCPError::Json)
    }

    pub async fn read_resource(
        &self,
        uri: &str,
    ) -> Result<Vec<crate::mcp::types::ResourceContents>, FastMCPError> {
        let params = serde_json::json!({
            "uri": uri
        });
        let result = self.request("resources/read", Some(params)).await?;
        let contents_val = result.get("contents").ok_or(FastMCPError::InvalidRequest(
            "Missing 'contents' field".to_string(),
        ))?;
        serde_json::from_value(contents_val.clone()).map_err(FastMCPError::Json)
    }

    pub async fn list_prompts(&self) -> Result<Vec<crate::mcp::types::Prompt>, FastMCPError> {
        let result = self.request("prompts/list", None).await?;
        let prompts_val = result.get("prompts").ok_or(FastMCPError::InvalidRequest(
            "Missing 'prompts' field".to_string(),
        ))?;
        serde_json::from_value(prompts_val.clone()).map_err(FastMCPError::Json)
    }

    // Using Value for PromptResult if GetPromptResult is missing
    pub async fn get_prompt(
        &self,
        name: &str,
        arguments: Option<Value>,
    ) -> Result<Value, FastMCPError> {
        let params = serde_json::json!({
            "name": name,
            "arguments": arguments.unwrap_or(serde_json::json!({}))
        });
        self.request("prompts/get", Some(params)).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::server::core::FastMCPServer;

    use async_trait::async_trait;
    use serde_json::json;
    use std::sync::Arc;

    use tokio::sync::Mutex;
    use tokio::sync::mpsc;

    #[derive(Debug)]
    struct LoopbackTransport {
        server: FastMCPServer,
        tx: mpsc::Sender<JsonRpcMessage>,
        rx: Mutex<mpsc::Receiver<JsonRpcMessage>>,
    }

    impl LoopbackTransport {
        fn new(server: FastMCPServer) -> Self {
            let (tx, rx) = mpsc::channel(100);
            Self {
                server,
                tx,
                rx: Mutex::new(rx),
            }
        }
    }

    #[async_trait]
    impl ClientTransport for LoopbackTransport {
        async fn send(&self, message: JsonRpcMessage) -> Result<(), FastMCPError> {
            if let JsonRpcMessage::Request(req) = message {
                // Logic: server.handle_request returns JsonRpcResponse.
                // Loopback needs to convert response back to JsonRpcMessage::Response and put in channel.
                match self.server.handle_request(req).await {
                    Ok(resp) => {
                        let _ = self.tx.send(JsonRpcMessage::Response(resp)).await;
                    }
                    Err(_e) => {
                        // If internal server error, send Error response?
                        // Or just log? Client expects a response if it sent a request.
                        // Construct error response.
                        // For test simplicity, assume success or simple error.
                        // But request ID is needed for error response.
                        // If handle_request fails at top level, we might not have ID?
                        // handle_request usually returns JsonRpcResponse even for errors if possible.
                        // FastMCPServer::handle_request returns Result<JsonRpcResponse, FastMCPError>.
                        // If Err, it means catastrophic failure or bad request structure where we can't reply?
                    }
                }
            }
            Ok(())
        }

        async fn receive(&self) -> Result<JsonRpcMessage, FastMCPError> {
            let mut rx = self.rx.lock().await;
            rx.recv().await.ok_or(FastMCPError::new("Channel closed"))
        }
    }

    #[tokio::test]
    async fn test_client_server_integration() {
        // Setup Server
        let server = FastMCPServer::new("test-server", "1.0");
        let tool = crate::tools::tool::Tool {
            name: "echo".to_string(),
            description: None,
            enabled: true,
            key: None,
            title: None,
            meta: None,
            tags: std::collections::HashSet::new(),
            data: crate::tools::tool::ToolKind::Function(crate::tools::tool::ToolFunction {
                name: "echo".to_string(),
                description: None,
                input_schema: json!({ "type": "object" }),
                output_schema: None,
                compiled_schema: None,
                fn_handler: Arc::new(Box::new(|_, args| {
                    Box::pin(async move {
                        Ok(crate::tools::tool::ToolResult {
                            content: vec![],
                            structured_content: Some(args),
                        })
                    })
                        as std::pin::Pin<
                            Box<
                                dyn std::future::Future<
                                        Output = Result<
                                            crate::tools::tool::ToolResult,
                                            FastMCPError,
                                        >,
                                    > + Send,
                            >,
                        >
                }) as crate::tools::tool::ToolHandler),
            }),
        };
        server.add_tool(tool).unwrap();

        // Setup Client
        let transport = Box::new(LoopbackTransport::new(server));
        let client = Client::new(transport);

        // Test list_tools
        let tools = client.list_tools().await.expect("Failed to list tools");
        assert_eq!(tools.len(), 1);
        assert_eq!(tools[0].base_metadata.name, "echo");

        // Test call_tool
        let result = client
            .call_tool("echo", json!({ "msg": "hello" }))
            .await
            .expect("Failed to call tool");
        let output = result.structured_content.unwrap();
        assert_eq!(output["msg"], "hello");
    }
}