From 07da67d435cbac17dd43469c29cd44df426056ca Mon Sep 17 00:00:00 2001 From: Michael Paquette Date: Tue, 25 Feb 2025 20:54:31 -0700 Subject: [PATCH 1/4] feat: Support absolute and relative stdio paths --- crates/mcp-client/src/transport/stdio.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/mcp-client/src/transport/stdio.rs b/crates/mcp-client/src/transport/stdio.rs index 59d900540..10080d471 100644 --- a/crates/mcp-client/src/transport/stdio.rs +++ b/crates/mcp-client/src/transport/stdio.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::sync::Arc; use tokio::process::{Child, ChildStderr, ChildStdin, ChildStdout, Command}; +use std::path::{Path, PathBuf}; use async_trait::async_trait; use mcp_core::protocol::JsonRpcMessage; @@ -195,8 +196,26 @@ impl StdioTransport { } } + fn resolve_command_path(&self) -> Result { + let command = &self.command; + let path = Path::new(command); + + if path.is_absolute() { + return Ok(command.to_string()); + } + + match std::env::current_dir() { + Ok(cwd) => { + let abs_path = cwd.join(path); + Ok(abs_path.to_string_lossy().into_owned()) + } + Err(e) => Err(Error::StdioProcessError(format!("Failed to get current directory: {}", e))) + } + } + async fn spawn_process(&self) -> Result<(Child, ChildStdin, ChildStdout, ChildStderr), Error> { - let mut command = Command::new(&self.command); + let resolved_command = self.resolve_command_path()?; + let mut command = Command::new(&resolved_command); command .envs(&self.env) .args(&self.args) From df5002e5a27c435517177be199859e316fcfebf8 Mon Sep 17 00:00:00 2001 From: Michael Paquette Date: Tue, 25 Feb 2025 20:58:00 -0700 Subject: [PATCH 2/4] feat: Support absolute and relative stdio paths --- crates/mcp-client/src/transport/stdio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/mcp-client/src/transport/stdio.rs b/crates/mcp-client/src/transport/stdio.rs index 10080d471..69f9440e1 100644 --- a/crates/mcp-client/src/transport/stdio.rs +++ b/crates/mcp-client/src/transport/stdio.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::sync::Arc; use tokio::process::{Child, ChildStderr, ChildStdin, ChildStdout, Command}; -use std::path::{Path, PathBuf}; +use std::path::Path; use async_trait::async_trait; use mcp_core::protocol::JsonRpcMessage; From 49b7b20b87d06d4726ae4892954b62583f9fa29e Mon Sep 17 00:00:00 2001 From: Michael Paquette Date: Thu, 27 Feb 2025 13:32:26 -0700 Subject: [PATCH 3/4] add valid path check, restructure method, fix cargo fmt --- crates/mcp-client/src/transport/stdio.rs | 29 +++++++++++++++--------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/crates/mcp-client/src/transport/stdio.rs b/crates/mcp-client/src/transport/stdio.rs index 69f9440e1..73a18f630 100644 --- a/crates/mcp-client/src/transport/stdio.rs +++ b/crates/mcp-client/src/transport/stdio.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; +use std::path::Path; use std::sync::Arc; use tokio::process::{Child, ChildStderr, ChildStdin, ChildStdout, Command}; -use std::path::Path; use async_trait::async_trait; use mcp_core::protocol::JsonRpcMessage; @@ -199,18 +199,25 @@ impl StdioTransport { fn resolve_command_path(&self) -> Result { let command = &self.command; let path = Path::new(command); - - if path.is_absolute() { - return Ok(command.to_string()); - } - match std::env::current_dir() { - Ok(cwd) => { - let abs_path = cwd.join(path); - Ok(abs_path.to_string_lossy().into_owned()) - } - Err(e) => Err(Error::StdioProcessError(format!("Failed to get current directory: {}", e))) + let abs_path = if path.is_absolute() { + path.to_path_buf() + } else { + std::env::current_dir() + .map_err(|e| { + Error::StdioProcessError(format!("Failed to get current directory: {}", e)) + })? + .join(path) + }; + + if !abs_path.exists() { + return Err(Error::StdioProcessError(format!( + "Command not found at path: {}", + abs_path.display() + ))); } + + Ok(abs_path.to_string_lossy().into_owned()) } async fn spawn_process(&self) -> Result<(Child, ChildStdin, ChildStdout, ChildStderr), Error> { From fdb1f990d6ba397893bad88e8c81e5977760fb34 Mon Sep 17 00:00:00 2001 From: Michael Paquette Date: Thu, 27 Feb 2025 13:37:51 -0700 Subject: [PATCH 4/4] rm unneeded variable declaration --- crates/mcp-client/src/transport/stdio.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/mcp-client/src/transport/stdio.rs b/crates/mcp-client/src/transport/stdio.rs index 73a18f630..a378fda6a 100644 --- a/crates/mcp-client/src/transport/stdio.rs +++ b/crates/mcp-client/src/transport/stdio.rs @@ -221,8 +221,7 @@ impl StdioTransport { } async fn spawn_process(&self) -> Result<(Child, ChildStdin, ChildStdout, ChildStderr), Error> { - let resolved_command = self.resolve_command_path()?; - let mut command = Command::new(&resolved_command); + let mut command = Command::new(&self.resolve_command_path()?); command .envs(&self.env) .args(&self.args)