Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/transport/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn tokio::io::AsyncWrite + Send + Sync + Unpin>) -> Self {
// Create a channel for synchronized writing
let (writer_tx, mut writer_rx) = mpsc::channel::<String>(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);
Expand Down Expand Up @@ -60,6 +64,15 @@ impl StdioTransport {
transport
}

pub fn with_reader_and_writer(
reader: Box<dyn tokio::io::AsyncRead + Send + Sync + Unpin>,
writer: Box<dyn tokio::io::AsyncWrite + Send + Sync + Unpin>,
) -> 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 {
Expand Down