Files
codex/codex-rs/rmcp-client/src/local_stdio_transport.rs
T
Charlie Marsh ea7e0714da Fix relative MCP server spawning on macOS (#42117)
## Why

Rust falls back to `fork` when a macOS command combines a relative executable
path with a working directory, due to a historical `posix_spawnp` issue. This
can make local MCP server startup less reliable.

## What changed

- Spawn relative macOS MCP executables directly with `posix_spawn`, preserving
  the configured path, `argv[0]`, working directory, environment, stdio, and
  process group.
- Keep the existing launcher for `PATH` lookup and executable text files without
  a shebang.
- Use one local child wrapper for legacy and 2026-07-28 protocol framing, with
  cancellation-safe waiting and cleanup that kills and reaps dropped children.

## Testing

Add macOS regression coverage for relative scripts, argument and descriptor
preservation, launch errors, cancelled waits, and cleanup after runtime shutdown.

GitOrigin-RevId: 1cbc6b90aed6f67e0a38f551964d80496ebc4471
2026-09-01 18:53:06 +00:00

98 lines
3.3 KiB
Rust

//! Protocol framing and child lifetime for locally spawned MCP servers.
//!
//! Process creation is platform-specific; protocol selection and shutdown are shared.
use std::future::Future;
use std::io;
use std::time::Duration;
use futures::FutureExt;
use rmcp::service::RoleClient;
use rmcp::service::RxJsonRpcMessage;
use rmcp::service::TxJsonRpcMessage;
use rmcp::transport::Transport;
use rmcp::transport::async_rw::AsyncRwTransport;
use tokio::process::ChildStderr;
use tokio::process::ChildStdin;
use tokio::process::ChildStdout;
use tokio::process::Command;
use crate::bounded_stdio_transport::BoundedStdioTransport;
use crate::local_child;
use crate::local_child::LocalChild;
use crate::protocol_mode::McpProtocolMode;
pub(super) struct LocalStdioTransport {
child: LocalChild,
transport: StdioTransport,
}
enum StdioTransport {
/// Preserve rmcp's existing framing for servers using the initialize handshake.
Legacy(AsyncRwTransport<RoleClient, ChildStdout, ChildStdin>),
/// Bound frames and skip messages unknown to the client during 2026-07-28 discovery.
V20260728(BoundedStdioTransport),
}
impl LocalStdioTransport {
pub(super) fn spawn(
command: Command,
program_name: String,
protocol_mode: McpProtocolMode,
) -> io::Result<(Self, Option<ChildStderr>)> {
let mut child = local_child::spawn(command)?;
let stdin = child
.stdin
.take()
.ok_or_else(|| io::Error::other("MCP server stdin was not piped"))?;
let stdout = child
.stdout
.take()
.ok_or_else(|| io::Error::other("MCP server stdout was not piped"))?;
let stderr = child.stderr.take();
let transport = match protocol_mode {
McpProtocolMode::Legacy => StdioTransport::Legacy(AsyncRwTransport::new(stdout, stdin)),
McpProtocolMode::V20260728 => {
StdioTransport::V20260728(BoundedStdioTransport::new(stdin, stdout, program_name))
}
};
Ok((Self { child, transport }, stderr))
}
pub(super) fn id(&self) -> Option<u32> {
self.child.id()
}
}
impl Transport<RoleClient> for LocalStdioTransport {
type Error = io::Error;
fn send(
&mut self,
item: TxJsonRpcMessage<RoleClient>,
) -> impl Future<Output = io::Result<()>> + Send + 'static {
match &mut self.transport {
StdioTransport::Legacy(transport) => transport.send(item).boxed(),
StdioTransport::V20260728(transport) => transport.send(item).boxed(),
}
}
fn receive(&mut self) -> impl Future<Output = Option<RxJsonRpcMessage<RoleClient>>> + Send {
match &mut self.transport {
StdioTransport::Legacy(transport) => transport.receive().boxed(),
StdioTransport::V20260728(transport) => transport.receive().boxed(),
}
}
async fn close(&mut self) -> io::Result<()> {
match &mut self.transport {
StdioTransport::Legacy(transport) => transport.close().await?,
StdioTransport::V20260728(transport) => transport.close().await?,
}
match tokio::time::timeout(Duration::from_secs(3), self.child.wait()).await {
Ok(status) => status.map(|_| ()),
Err(_) => self.child.kill().await,
}
}
}