mcp-core 0.1.50

A Rust library implementing the Modern 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
use crate::protocol::{Protocol, ProtocolBuilder, RequestOptions};
use crate::transport::{
    JsonRpcError, JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, Message, RequestId,
    Transport,
};
use crate::types::ErrorCode;
use anyhow::Result;
use async_trait::async_trait;
use std::future::Future;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::pin::Pin;
use std::process::Command;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::time::timeout;
use tracing::debug;

/// Client transport that communicates with an MCP server over standard I/O.
///
/// The `ClientStdioTransport` launches a child process specified by the provided
/// program and arguments, then communicates with it using the standard input and output
/// streams. It implements the `Transport` trait to send requests and receive responses
/// over these streams.
///
/// This transport is useful for:
/// - Running local MCP servers as child processes
/// - Command-line tools that need to communicate with MCP servers
/// - Testing and development scenarios
///
/// # Example
///
/// ```
/// use mcp_core::transport::{ClientStdioTransport, Transport};
/// use anyhow::Result;
///
/// async fn example() -> Result<()> {
///     let transport = ClientStdioTransport::new("my-mcp-server", &["--flag"])?;
///     transport.open().await?;
///     // Use transport...
///     transport.close().await?;
///     Ok(())
/// }
/// ```
#[derive(Clone)]
pub struct ClientStdioTransport {
    protocol: Protocol,
    stdin: Arc<Mutex<Option<BufWriter<std::process::ChildStdin>>>>,
    stdout: Arc<Mutex<Option<BufReader<std::process::ChildStdout>>>>,
    child: Arc<Mutex<Option<std::process::Child>>>,
    program: String,
    args: Vec<String>,
}

impl ClientStdioTransport {
    /// Creates a new `ClientStdioTransport` instance.
    ///
    /// # Arguments
    ///
    /// * `program` - The path or name of the program to execute
    /// * `args` - Command-line arguments to pass to the program
    ///
    /// # Returns
    ///
    /// A `Result` containing the new transport instance if successful
    pub fn new(program: &str, args: &[&str]) -> Result<Self> {
        Ok(ClientStdioTransport {
            protocol: ProtocolBuilder::new().build(),
            stdin: Arc::new(Mutex::new(None)),
            stdout: Arc::new(Mutex::new(None)),
            child: Arc::new(Mutex::new(None)),
            program: program.to_string(),
            args: args.iter().map(|&s| s.to_string()).collect(),
        })
    }
}

#[async_trait()]
impl Transport for ClientStdioTransport {
    /// Opens the transport by launching the child process and setting up the communication channels.
    ///
    /// This method:
    /// 1. Spawns the child process with the configured program and arguments
    /// 2. Sets up pipes for stdin and stdout
    /// 3. Starts a background task for handling incoming messages
    ///
    /// # Returns
    ///
    /// A `Result` indicating success or failure
    async fn open(&self) -> Result<()> {
        debug!("ClientStdioTransport: Opening transport");
        let mut child = Command::new(&self.program)
            .args(&self.args)
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .spawn()?;

        let stdin = child
            .stdin
            .take()
            .ok_or_else(|| anyhow::anyhow!("Child process stdin not available"))?;
        let stdout = child
            .stdout
            .take()
            .ok_or_else(|| anyhow::anyhow!("Child process stdout not available"))?;

        {
            let mut stdin_lock = self.stdin.lock().await;
            *stdin_lock = Some(BufWriter::new(stdin));
        }
        {
            let mut stdout_lock = self.stdout.lock().await;
            *stdout_lock = Some(BufReader::new(stdout));
        }
        {
            let mut child_lock = self.child.lock().await;
            *child_lock = Some(child);
        }

        // Spawn a background task to continuously poll messages.
        let transport_clone = self.clone();
        tokio::spawn(async move {
            loop {
                match transport_clone.poll_message().await {
                    Ok(Some(message)) => match message {
                        Message::Request(request) => {
                            let response = transport_clone.protocol.handle_request(request).await;
                            let _ = transport_clone
                                .send_response(response.id, response.result, response.error)
                                .await;
                        }
                        Message::Notification(notification) => {
                            let _ = transport_clone
                                .protocol
                                .handle_notification(notification)
                                .await;
                        }
                        Message::Response(response) => {
                            transport_clone.protocol.handle_response(response).await;
                        }
                    },
                    Ok(None) => break, // EOF encountered.
                    Err(e) => {
                        debug!("ClientStdioTransport: Error polling message: {:?}", e);
                        break;
                    }
                }
            }
        });
        Ok(())
    }

    /// Closes the transport by terminating the child process and cleaning up resources.
    ///
    /// This method:
    /// 1. Kills the child process
    /// 2. Clears the stdin and stdout handles
    ///
    /// # Returns
    ///
    /// A `Result` indicating success or failure
    async fn close(&self) -> Result<()> {
        let mut child_lock = self.child.lock().await;
        if let Some(child) = child_lock.as_mut() {
            let _ = child.kill();
        }
        *child_lock = None;

        // Clear stdin and stdout
        *self.stdin.lock().await = None;
        *self.stdout.lock().await = None;

        Ok(())
    }

    /// Polls for incoming messages from the child process's stdout.
    ///
    /// This method reads a line from the child process's stdout and parses it
    /// as a JSON-RPC message.
    ///
    /// # Returns
    ///
    /// A `Result` containing an `Option<Message>`. `None` indicates EOF.
    async fn poll_message(&self) -> Result<Option<Message>> {
        debug!("ClientStdioTransport: Starting to receive message");

        // Take ownership of stdout temporarily
        let mut stdout_guard = self.stdout.lock().await;
        let mut stdout = stdout_guard
            .take()
            .ok_or_else(|| anyhow::anyhow!("Transport not opened"))?;

        // Drop the lock before spawning the blocking task
        drop(stdout_guard);

        // Use a blocking operation in a spawn_blocking task
        let (line_result, stdout) = tokio::task::spawn_blocking(move || {
            let mut line = String::new();
            let result = match stdout.read_line(&mut line) {
                Ok(0) => Ok(None), // EOF
                Ok(_) => Ok(Some(line)),
                Err(e) => Err(anyhow::anyhow!("Error reading line: {}", e)),
            };
            // Return both the result and the stdout so we can put it back
            (result, stdout)
        })
        .await?;

        // Put stdout back
        let mut stdout_guard = self.stdout.lock().await;
        *stdout_guard = Some(stdout);

        // Process the result
        match line_result? {
            Some(line) => {
                debug!(
                    "ClientStdioTransport: Received from process: {}",
                    line.trim()
                );
                let message: Message = serde_json::from_str(&line)?;
                debug!("ClientStdioTransport: Successfully parsed message");
                Ok(Some(message))
            }
            None => {
                debug!("ClientStdioTransport: Received EOF from process");
                Ok(None)
            }
        }
    }

    /// Sends a request to the child process and waits for a response.
    ///
    /// This method:
    /// 1. Creates a new request ID
    /// 2. Constructs a JSON-RPC request
    /// 3. Sends it to the child process's stdin
    /// 4. Waits for a response with the same ID
    ///
    /// # Arguments
    ///
    /// * `method` - The method name for the request
    /// * `params` - Optional parameters for the request
    /// * `options` - Request options (like timeout)
    ///
    /// # Returns
    ///
    /// A `Future` that resolves to a `Result` containing the response
    fn request(
        &self,
        method: &str,
        params: Option<serde_json::Value>,
        options: RequestOptions,
    ) -> Pin<Box<dyn Future<Output = Result<JsonRpcResponse>> + Send + Sync>> {
        let protocol = self.protocol.clone();
        let stdin_arc = self.stdin.clone();
        let method = method.to_owned();
        Box::pin(async move {
            let (id, rx) = protocol.create_request().await;
            let request = JsonRpcRequest {
                id,
                method,
                jsonrpc: Default::default(),
                params,
            };
            let serialized = serde_json::to_string(&request)?;
            debug!("ClientStdioTransport: Sending request: {}", serialized);

            // Get the stdin writer
            let mut stdin_guard = stdin_arc.lock().await;
            let mut stdin = stdin_guard
                .take()
                .ok_or_else(|| anyhow::anyhow!("Transport not opened"))?;

            // Use a blocking operation in a spawn_blocking task
            let stdin_result = tokio::task::spawn_blocking(move || {
                stdin.write_all(serialized.as_bytes())?;
                stdin.write_all(b"\n")?;
                stdin.flush()?;
                Ok::<_, anyhow::Error>(stdin)
            })
            .await??;

            // Put the writer back
            *stdin_guard = Some(stdin_result);

            debug!("ClientStdioTransport: Request sent successfully");
            let result = timeout(options.timeout, rx).await;
            match result {
                Ok(inner_result) => match inner_result {
                    Ok(response) => Ok(response),
                    Err(_) => {
                        protocol.cancel_response(id).await;
                        Ok(JsonRpcResponse {
                            id,
                            result: None,
                            error: Some(JsonRpcError {
                                code: ErrorCode::RequestTimeout as i32,
                                message: "Request cancelled".to_string(),
                                data: None,
                            }),
                            ..Default::default()
                        })
                    }
                },
                Err(_) => {
                    protocol.cancel_response(id).await;
                    Ok(JsonRpcResponse {
                        id,
                        result: None,
                        error: Some(JsonRpcError {
                            code: ErrorCode::RequestTimeout as i32,
                            message: "Request timed out".to_string(),
                            data: None,
                        }),
                        ..Default::default()
                    })
                }
            }
        })
    }

    /// Sends a response to a request previously received from the child process.
    ///
    /// # Arguments
    ///
    /// * `id` - The ID of the request being responded to
    /// * `result` - Optional successful result
    /// * `error` - Optional error information
    ///
    /// # Returns
    ///
    /// A `Result` indicating success or failure
    async fn send_response(
        &self,
        id: RequestId,
        result: Option<serde_json::Value>,
        error: Option<JsonRpcError>,
    ) -> Result<()> {
        let response = JsonRpcResponse {
            id,
            result,
            error,
            jsonrpc: Default::default(),
        };
        let serialized = serde_json::to_string(&response)?;
        debug!("ClientStdioTransport: Sending response: {}", serialized);

        // Get the stdin writer
        let mut stdin_guard = self.stdin.lock().await;
        let mut stdin = stdin_guard
            .take()
            .ok_or_else(|| anyhow::anyhow!("Transport not opened"))?;

        // Use a blocking operation in a spawn_blocking task
        let stdin_result = tokio::task::spawn_blocking(move || {
            stdin.write_all(serialized.as_bytes())?;
            stdin.write_all(b"\n")?;
            stdin.flush()?;
            Ok::<_, anyhow::Error>(stdin)
        })
        .await??;

        // Put the writer back
        *stdin_guard = Some(stdin_result);

        Ok(())
    }

    /// Sends a notification to the child process.
    ///
    /// Unlike requests, notifications do not expect a response.
    ///
    /// # Arguments
    ///
    /// * `method` - The method name for the notification
    /// * `params` - Optional parameters for the notification
    ///
    /// # Returns
    ///
    /// A `Result` indicating success or failure
    async fn send_notification(
        &self,
        method: &str,
        params: Option<serde_json::Value>,
    ) -> Result<()> {
        let notification = JsonRpcNotification {
            jsonrpc: Default::default(),
            method: method.to_owned(),
            params,
        };
        let serialized = serde_json::to_string(&notification)?;
        debug!("ClientStdioTransport: Sending notification: {}", serialized);

        // Get the stdin writer
        let mut stdin_guard = self.stdin.lock().await;
        let mut stdin = stdin_guard
            .take()
            .ok_or_else(|| anyhow::anyhow!("Transport not opened"))?;

        // Use a blocking operation in a spawn_blocking task
        let stdin_result = tokio::task::spawn_blocking(move || {
            stdin.write_all(serialized.as_bytes())?;
            stdin.write_all(b"\n")?;
            stdin.flush()?;
            Ok::<_, anyhow::Error>(stdin)
        })
        .await??;

        // Put the writer back
        *stdin_guard = Some(stdin_result);

        Ok(())
    }
}