mcp-runner 0.3.1

A Rust library for running and interacting with Model Context Protocol (MCP) servers locally
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use super::json_rpc::{JsonRpcMessage, JsonRpcRequest, JsonRpcResponse, error_codes};
use crate::error::{Error, Result};
use crate::transport::Transport;
use async_process::{ChildStdin, ChildStdout};
use async_trait::async_trait;
use futures_lite::io::{AsyncReadExt, AsyncWriteExt};
use serde_json::Value;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tokio::sync::oneshot;
use tokio::task::JoinHandle;
use tracing::{self, Instrument, span};
use uuid::Uuid;

/// StdioTransport provides communication with an MCP server via standard I/O.
///
/// This implementation uses JSON-RPC over standard input/output to communicate with
/// an MCP server. It handles concurrent requests using a background task for reading
/// responses and dispatches them to the appropriate handler.
/// Most public methods are instrumented with `tracing` spans.
///
/// # Example
///
/// ```no_run
/// use mcp_runner::transport::StdioTransport;
/// use mcp_runner::error::Result;
/// use async_process::{Child, Command};
/// use serde_json::json;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     // Start a child process
///     let mut child = Command::new("mcp-server")
///         .stdin(std::process::Stdio::piped())
///         .stdout(std::process::Stdio::piped())
///         .spawn()
///         .expect("Failed to start MCP server");
///     
///     // Take ownership of stdin/stdout
///     let stdin = child.stdin.take().expect("Failed to get stdin");
///     let stdout = child.stdout.take().expect("Failed to get stdout");
///     
///     // Create transport
///     let transport = StdioTransport::new("example-server".to_string(), stdin, stdout);
///     
///     // Initialize the transport
///     transport.initialize().await?;
///     
///     // List available tools
///     let tools = transport.list_tools().await?;
///     println!("Available tools: {:?}", tools);
///     
///     Ok(())
/// }
/// ```
pub struct StdioTransport {
    /// Server name
    name: String,
    /// Child process stdin
    stdin: Arc<Mutex<ChildStdin>>,
    /// Response handlers
    response_handlers: Arc<Mutex<HashMap<String, oneshot::Sender<JsonRpcResponse>>>>,
    /// Task handle for reading from stdout
    reader_task: Option<JoinHandle<()>>,
}

impl StdioTransport {
    /// Creates a new StdioTransport instance.
    ///
    /// This constructor takes ownership of the child process's stdin and stdout,
    /// and sets up a background task to process incoming JSON-RPC messages.
    /// This method is instrumented with `tracing`.
    ///
    /// # Arguments
    ///
    /// * `name` - A name for this transport, typically the server name
    /// * `stdin` - The child process's stdin handle
    /// * `stdout` - The child process's stdout handle
    ///
    /// # Returns
    ///
    /// A new `StdioTransport` instance
    #[tracing::instrument(skip(stdin, stdout), fields(name = %name))]
    pub fn new(name: String, stdin: ChildStdin, mut stdout: ChildStdout) -> Self {
        tracing::debug!("Creating new StdioTransport");
        let stdin = Arc::new(Mutex::new(stdin));
        let response_handlers = Arc::new(Mutex::new(HashMap::<
            String,
            oneshot::Sender<JsonRpcResponse>,
        >::new()));

        // Clone for the reader task
        let response_handlers_clone = Arc::clone(&response_handlers);

        // Create the span for the reader task explicitly
        let reader_span = span!(tracing::Level::INFO, "stdout_reader", name = %name);

        // Spawn a task to read from stdout
        // Use .instrument() on the future
        let reader_task = tokio::spawn(async move {
            tracing::debug!("Stdout reader task started");
            // Process stdout line by line
            let mut buffer = Vec::new();
            let mut buf = [0u8; 1];

            loop {
                // Try to read a single byte
                match stdout.read(&mut buf).await {
                    Ok(0) => {
                        tracing::debug!("Stdout reached EOF");
                        break;
                    } // EOF
                    Ok(_) => {
                        if buf[0] == b'\n' {
                            // Process the line
                            if let Ok(line) = String::from_utf8(buffer.clone()) {
                                let trimmed_line = line.trim();
                                if trimmed_line.is_empty() {
                                    // Ignore empty lines
                                    buffer.clear();
                                    continue;
                                }

                                // Check if the line looks like a JSON object before attempting to parse
                                if !trimmed_line.starts_with('{') {
                                    tracing::trace!(output = "stdout", line = %trimmed_line, "Ignoring non-JSON line");
                                    buffer.clear();
                                    continue;
                                }

                                // Attempt to parse as JSON-RPC
                                tracing::trace!(output = "stdout", line = %trimmed_line, "Attempting to parse line as JSON-RPC");
                                match serde_json::from_str::<JsonRpcMessage>(trimmed_line) {
                                    Ok(JsonRpcMessage::Response(response)) => {
                                        // Get ID as string
                                        let id_str = match &response.id {
                                            Value::String(s) => s.clone(),
                                            Value::Number(n) => n.to_string(),
                                            _ => {
                                                tracing::warn!(response_id = ?response.id, "Received response with unexpected ID type");
                                                continue;
                                            }
                                        };
                                        tracing::debug!(response_id = %id_str, "Received JSON-RPC response");

                                        // Send response to handler - handle lock errors gracefully
                                        if let Ok(mut handlers) = response_handlers_clone.lock() {
                                            if let Some(sender) = handlers.remove(&id_str) {
                                                if sender.send(response).is_err() {
                                                    tracing::warn!(response_id = %id_str, "Response handler dropped before response could be sent");
                                                }
                                            } else {
                                                tracing::warn!(response_id = %id_str, "Received response for unknown or timed out request");
                                            }
                                        } else {
                                            tracing::error!("Response handler lock poisoned!");
                                        }
                                    }
                                    Ok(JsonRpcMessage::Request(req)) => {
                                        tracing::warn!(method = %req.method, "Received unexpected JSON-RPC request from server");
                                    }
                                    Ok(JsonRpcMessage::Notification(notif)) => {
                                        tracing::debug!(method = %notif.method, "Received JSON-RPC notification from server");
                                    }
                                    Err(e) => {
                                        // Keep WARN for lines that start like JSON but fail to parse
                                        tracing::warn!(line = %trimmed_line, error = %e, "Failed to parse potential JSON-RPC message");
                                    }
                                }
                            } else {
                                // Log if line is not valid UTF-8
                                tracing::warn!(bytes = ?buffer, "Received non-UTF8 data on stdout");
                            }
                            buffer.clear();
                        } else {
                            buffer.push(buf[0]);
                        }
                    }
                    Err(e) => {
                        tracing::error!(error = %e, "Error reading from stdout");
                        break;
                    } // Error
                }
            }
            tracing::debug!("Stdout reader task finished");
        }.instrument(reader_span)); // Apply the span to the future

        Self {
            name,
            stdin,
            response_handlers,
            reader_task: Some(reader_task),
        }
    }

    /// Gets the name of the server associated with this transport.
    ///
    /// # Returns
    ///
    /// A string slice containing the server name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Writes data to the child process's stdin.
    ///
    /// This is a helper function that handles the complexity of writing to
    /// stdin in a thread-safe and non-blocking way.
    /// This method is instrumented with `tracing`.
    ///
    /// # Arguments
    ///
    /// * `data` - The bytes to write to stdin
    ///
    /// # Returns
    ///
    /// A `Result<()>` indicating success or failure
    #[tracing::instrument(skip(self, data), fields(name = %self.name))]
    async fn write_to_stdin(&self, data: Vec<u8>) -> Result<()> {
        tracing::trace!(bytes_len = data.len(), "Writing to stdin");
        let stdin_clone = self.stdin.clone();

        tokio::task::spawn_blocking(move || -> Result<()> {
            let stdin_lock = stdin_clone
                .lock()
                .map_err(|_| Error::Communication("Failed to acquire stdin lock".to_string()))?;

            let mut stdin = stdin_lock;

            futures_lite::future::block_on(async {
                stdin.write_all(&data).await.map_err(|e| {
                    Error::Communication(format!("Failed to write to stdin: {}", e))
                })?;
                stdin
                    .flush()
                    .await
                    .map_err(|e| Error::Communication(format!("Failed to flush stdin: {}", e)))?;
                Ok::<(), Error>(())
            })?;

            Ok(())
        })
        .await
        .map_err(|e| {
            tracing::error!(error = %e, "Stdin write task panicked");
            Error::Communication(format!("Task join error: {}", e))
        })??;

        tracing::trace!("Finished writing to stdin");
        Ok(())
    }

    /// Sends a JSON-RPC request and waits for a response.
    ///
    /// This method handles the details of sending a request, registering a response
    /// handler, and waiting for the response to arrive.
    /// This method is instrumented with `tracing`.
    ///
    /// # Arguments
    ///
    /// * `request` - The JSON-RPC request to send
    ///
    /// # Returns
    ///
    /// A `Result<JsonRpcResponse>` containing the response if successful
    #[tracing::instrument(skip(self, request), fields(name = %self.name, method = %request.method, request_id = ?request.id))]
    pub async fn send_request(&self, request: JsonRpcRequest) -> Result<JsonRpcResponse> {
        tracing::debug!("Sending JSON-RPC request");
        let id_str = match &request.id {
            Value::String(s) => s.clone(),
            Value::Number(n) => n.to_string(),
            _ => return Err(Error::Communication("Invalid request ID type".to_string())),
        };

        let (sender, receiver) = oneshot::channel();

        {
            let mut handlers = self.response_handlers.lock().map_err(|_| {
                Error::Communication("Failed to lock response handlers".to_string())
            })?;
            handlers.insert(id_str, sender);
        }

        let request_json = serde_json::to_string(&request)
            .map_err(|e| Error::Serialization(format!("Failed to serialize request: {}", e)))?;
        tracing::trace!(request_json = %request_json, "Sending request JSON");
        let request_bytes = request_json.into_bytes();
        let mut request_bytes_with_newline = request_bytes;
        request_bytes_with_newline.push(b'\n');

        self.write_to_stdin(request_bytes_with_newline).await?;

        tracing::debug!("Waiting for response");
        let response = receiver.await.map_err(|_| {
            tracing::warn!("Sender dropped before response received (likely timeout or closed)");
            Error::Communication("Failed to receive response".to_string())
        })?;

        if let Some(error) = &response.error {
            tracing::error!(error_code = error.code, error_message = %error.message, "Received JSON-RPC error response");
            return Err(Error::JsonRpc(error.message.clone()));
        }

        tracing::debug!("Received successful response");
        Ok(response)
    }

    /// Sends a JSON-RPC notification (no response expected).
    ///
    /// Unlike requests, notifications don't expect a response, so this method
    /// just sends the message without setting up a response handler.
    /// This method is instrumented with `tracing`.
    ///
    /// # Arguments
    ///
    /// * `notification` - The JSON-RPC notification to send
    ///
    /// # Returns
    ///
    /// A `Result<()>` indicating success or failure
    #[tracing::instrument(skip(self, notification), fields(name = %self.name, method = notification.get("method").and_then(|v| v.as_str())))]
    pub async fn send_notification(&self, notification: serde_json::Value) -> Result<()> {
        tracing::debug!("Sending JSON-RPC notification");
        let notification_json = serde_json::to_string(&notification).map_err(|e| {
            Error::Serialization(format!("Failed to serialize notification: {}", e))
        })?;
        tracing::trace!(notification_json = %notification_json, "Sending notification JSON");
        let notification_bytes = notification_json.into_bytes();
        let mut notification_bytes_with_newline = notification_bytes;
        notification_bytes_with_newline.push(b'\n');

        self.write_to_stdin(notification_bytes_with_newline).await
    }

    /// Initializes the MCP server.
    ///
    /// Sends the `notifications/initialized` notification to the server,
    /// indicating that the client is ready to communicate.
    /// This method is instrumented with `tracing`.
    ///
    /// # Returns
    ///
    /// A `Result<()>` indicating success or failure
    #[tracing::instrument(skip(self), fields(name = %self.name))]
    pub async fn initialize(&self) -> Result<()> {
        tracing::info!("Initializing MCP connection");
        let notification = serde_json::json!({
            "jsonrpc": "2.0",
            "method": "notifications/initialized"
        });

        self.send_notification(notification).await
    }

    /// Lists available tools provided by the MCP server.
    ///
    /// This method is instrumented with `tracing`.
    ///
    /// # Returns
    ///
    /// A `Result<Vec<Value>>` containing the list of tools if successful
    #[tracing::instrument(skip(self), fields(name = %self.name))]
    pub async fn list_tools(&self) -> Result<Vec<Value>> {
        tracing::debug!("Listing tools");
        let request_id = Uuid::new_v4().to_string();
        let request = JsonRpcRequest::list_tools(request_id);

        let response = self.send_request(request).await?;

        if let Some(Value::Object(result)) = response.result {
            if let Some(Value::Array(tools)) = result.get("tools") {
                return Ok(tools.clone());
            }
        }

        Ok(Vec::new())
    }

    /// Calls a tool provided by the MCP server.
    ///
    /// This method is instrumented with `tracing`.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the tool to call
    /// * `args` - The arguments to pass to the tool
    ///
    /// # Returns
    ///
    /// A `Result<Value>` containing the tool's response if successful
    #[tracing::instrument(skip(self, args), fields(name = %self.name, tool_name = %name.as_ref()))]
    pub async fn call_tool(
        &self,
        name: impl AsRef<str> + std::fmt::Debug,
        args: Value,
    ) -> Result<Value> {
        tracing::debug!(args = ?args, "Calling tool");
        let request_id = Uuid::new_v4().to_string();
        let request = JsonRpcRequest::call_tool(request_id, name.as_ref().to_string(), args);

        let response = self.send_request(request).await?;

        response
            .result
            .ok_or_else(|| Error::Communication("No result in response".to_string()))
    }

    /// Lists available resources provided by the MCP server.
    ///
    /// This method is instrumented with `tracing`.
    ///
    /// # Returns
    ///
    /// A `Result<Vec<Value>>` containing the list of resources if successful
    #[tracing::instrument(skip(self), fields(name = %self.name))]
    pub async fn list_resources(&self) -> Result<Vec<Value>> {
        tracing::debug!("Listing resources");
        let request_id = Uuid::new_v4().to_string();
        let request = JsonRpcRequest::list_resources(request_id);

        let response = self.send_request(request).await?;

        if let Some(Value::Object(result)) = response.result {
            if let Some(Value::Array(resources)) = result.get("resources") {
                return Ok(resources.clone());
            }
        }

        Ok(Vec::new())
    }

    /// Retrieves a specific resource from the MCP server.
    ///
    /// This method is instrumented with `tracing`.
    ///
    /// # Arguments
    ///
    /// * `uri` - The URI of the resource to retrieve
    ///
    /// # Returns
    ///
    /// A `Result<Value>` containing the resource data if successful
    #[tracing::instrument(skip(self), fields(name = %self.name, uri = %uri.as_ref()))]
    pub async fn get_resource(&self, uri: impl AsRef<str> + std::fmt::Debug) -> Result<Value> {
        tracing::debug!("Getting resource");
        let request_id = Uuid::new_v4().to_string();
        let request = JsonRpcRequest::get_resource(request_id, uri.as_ref().to_string());

        let response = self.send_request(request).await?;

        response
            .result
            .ok_or_else(|| Error::Communication("No result in response".to_string()))
    }

    /// Closes the transport and cleans up resources.
    ///
    /// This method should be called when the transport is no longer needed
    /// to ensure proper cleanup of background tasks and resources.
    /// This method is instrumented with `tracing`.
    ///
    /// # Returns
    ///
    /// A `Result<()>` indicating success or failure
    #[tracing::instrument(skip(self), fields(name = %self.name))]
    pub async fn close(&mut self) -> Result<()> {
        tracing::info!("Closing transport");
        if let Some(task) = self.reader_task.take() {
            task.abort();
            let _ = task.await;
        }

        if let Ok(mut handlers) = self.response_handlers.lock() {
            for (_, sender) in handlers.drain() {
                let _ = sender.send(JsonRpcResponse {
                    jsonrpc: "2.0".to_string(),
                    id: Value::Null,
                    result: None,
                    error: Some(super::json_rpc::JsonRpcError {
                        code: error_codes::SERVER_ERROR,
                        message: "Connection closed".to_string(),
                        data: None,
                    }),
                });
            }
        }

        Ok(())
    }
}

#[async_trait]
impl Transport for StdioTransport {
    /// This method is instrumented with `tracing`.
    #[tracing::instrument(skip(self), fields(name = %self.name()))]
    async fn initialize(&self) -> Result<()> {
        self.initialize().await
    }

    /// This method is instrumented with `tracing`.
    #[tracing::instrument(skip(self), fields(name = %self.name()))]
    async fn list_tools(&self) -> Result<Vec<Value>> {
        self.list_tools().await
    }

    /// This method is instrumented with `tracing`.
    #[tracing::instrument(skip(self, args), fields(name = %self.name(), tool_name = %name))]
    async fn call_tool(&self, name: &str, args: Value) -> Result<Value> {
        self.call_tool(name.to_string(), args).await
    }

    /// This method is instrumented with `tracing`.
    #[tracing::instrument(skip(self), fields(name = %self.name()))]
    async fn list_resources(&self) -> Result<Vec<Value>> {
        self.list_resources().await
    }

    /// This method is instrumented with `tracing`.
    #[tracing::instrument(skip(self), fields(name = %self.name(), uri = %uri))]
    async fn get_resource(&self, uri: &str) -> Result<Value> {
        self.get_resource(uri.to_string()).await
    }
}