From 20ae33977b6ce85109d20d868c678feb99ac9a33 Mon Sep 17 00:00:00 2001 From: BasilYes Date: Tue, 12 Aug 2025 09:09:08 +0300 Subject: [PATCH] feat: create StdioTransport with reader and writer --- src/transport/stdio.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/transport/stdio.rs b/src/transport/stdio.rs index 46bfa3e..ab5ea7e 100644 --- a/src/transport/stdio.rs +++ b/src/transport/stdio.rs @@ -24,12 +24,16 @@ impl Default for StdioTransport { impl StdioTransport { /// Create a new stdio transport using stdin and stdout pub fn new() -> Self { + StdioTransport::with_writer(Box::new(tokio::io::stdout())) + } + + pub fn with_writer(writer: Box) -> Self { // Create a channel for synchronized writing let (writer_tx, mut writer_rx) = mpsc::channel::(32); // Spawn a dedicated writer task that processes one message at a time tokio::spawn(async move { - let mut writer = tokio::io::BufWriter::new(tokio::io::stdout()); + let mut writer = tokio::io::BufWriter::new(writer); while let Some(message) = writer_rx.recv().await { if let Err(e) = writer.write_all(message.as_bytes()).await { eprintln!("Error writing to stdout: {}", e); @@ -60,6 +64,15 @@ impl StdioTransport { transport } + pub fn with_reader_and_writer( + reader: Box, + writer: Box, + ) -> Self { + let mut transport = Self::with_writer(writer); + transport.reader = BufReader::new(reader); + transport + } + /// Handle an error by calling the error callback if set fn handle_error(&self, error: &MCPError) { if let Some(callback) = &self.on_error {