fix: cleanup zombie processes for child process client (#156)

* feat(client): cleanup of zombie processes in child process client

Using process wrap library, Process Group for Unix and Job Object for Windows

* fix: install extra dep based on feature, update examples take owned command

Install process-wrap if transport-child-process feature is enabled. Update examples to take ownership
of the command instead of mutable reference

* fix: update other examples, comments and readme to take ownership of command

Updated more examples, comments and readme to take command instead of mutable ref.
Also small fix in cargo toml of example to use local path.

* refactor: add configure command ext

Added configure command ext to tokio process command so that
you can use .configure() to use inline commands for mcp stdio client.
Added warning if start kill process fails
This commit is contained in:
Humberto Yusta Gómez 2025-05-18 12:38:47 +02:00 • committed by GitHub
commit 64995cc40c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 115 additions and 77 deletions

View file

@ -22,17 +22,17 @@ rmcp = { git = "https://github.com/modelcontextprotocol/rust-sdk", branch = "mai
### Quick start ### Quick start
Start a client in one line: Start a client:
```rust, ignore ```rust, ignore
use rmcp::{ServiceExt, transport::TokioChildProcess}; use rmcp::{ServiceExt, transport::{TokioChildProcess, ConfigureCommandExt}};
use tokio::process::Command; use tokio::process::Command;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = ().serve( let client = ().serve(TokioChildProcess::new(Command::new("npx").configure(|cmd| {
TokioChildProcess::new(Command::new("npx").arg("-y").arg("@modelcontextprotocol/server-everything"))? cmd.arg("-y").arg("@modelcontextprotocol/server-everything");
).await?; }))?).await?;
Ok(()) Ok(())
} }
``` ```
@ -92,7 +92,7 @@ let server = service.serve(transport).await?;
Once the server is initialized, you can send requests or notifications: Once the server is initialized, you can send requests or notifications:
```rust, ignore ```rust, ignore
// request // request
let roots = server.list_roots().await?; let roots = server.list_roots().await?;
// or send notification // or send notification

View file

@ -45,6 +45,8 @@ url = { version = "2.4", optional = true }
# For tower compatibility # For tower compatibility
tower-service = { version = "0.3", optional = true } tower-service = { version = "0.3", optional = true }
# for child process transport
process-wrap = { version = "8.2", features = ["tokio1"], optional = true}
# for ws transport # for ws transport
# tokio-tungstenite ={ version = "0.26", optional = true } # tokio-tungstenite ={ version = "0.26", optional = true }
@ -89,7 +91,11 @@ transport-streamable-http-client = [
transport-async-rw = ["tokio/io-util", "tokio-util/codec"] transport-async-rw = ["tokio/io-util", "tokio-util/codec"]
transport-io = ["transport-async-rw", "tokio/io-std"] transport-io = ["transport-async-rw", "tokio/io-std"]
transport-child-process = ["transport-async-rw", "tokio/process"] transport-child-process = [
"transport-async-rw",
"tokio/process",
"dep:process-wrap",
]
transport-sse-server = [ transport-sse-server = [
"transport-async-rw", "transport-async-rw",
"axum", "axum",

View file

@ -58,15 +58,13 @@
//! //!
//! ```rust //! ```rust
//! use anyhow::Result; //! use anyhow::Result;
//! use rmcp::{model::CallToolRequestParam, service::ServiceExt, transport::TokioChildProcess}; //! use rmcp::{model::CallToolRequestParam, service::ServiceExt, transport::{TokioChildProcess, ConfigureCommandExt}};
//! use tokio::process::Command; //! use tokio::process::Command;
//! //!
//! async fn client() -> Result<()> { //! async fn client() -> Result<()> {
//! let service = () //! let service = ().serve(TokioChildProcess::new(Command::new("uvx").configure(|cmd| {
//! .serve(TokioChildProcess::new( //! cmd.arg("mcp-server-git");
//! Command::new("uvx").arg("mcp-server-git"), //! }))?).await?;
//! )?)
//! .await?;
//! //!
//! // Initialize //! // Initialize
//! let server_info = service.peer_info(); //! let server_info = service.peer_info();

View file

@ -84,7 +84,7 @@ pub use worker::WorkerTransport;
pub mod child_process; pub mod child_process;
#[cfg(feature = "transport-child-process")] #[cfg(feature = "transport-child-process")]
#[cfg_attr(docsrs, doc(cfg(feature = "transport-child-process")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-child-process")))]
pub use child_process::TokioChildProcess; pub use child_process::{ConfigureCommandExt, TokioChildProcess};
#[cfg(feature = "transport-io")] #[cfg(feature = "transport-io")]
#[cfg_attr(docsrs, doc(cfg(feature = "transport-io")))] #[cfg_attr(docsrs, doc(cfg(feature = "transport-io")))]

View file

@ -1,3 +1,4 @@
use process_wrap::tokio::{TokioChildWrapper, TokioCommandWrap};
use tokio::{ use tokio::{
io::AsyncRead, io::AsyncRead,
process::{ChildStdin, ChildStdout}, process::{ChildStdin, ChildStdout},
@ -7,29 +8,41 @@ use super::{IntoTransport, Transport};
use crate::service::ServiceRole; use crate::service::ServiceRole;
pub(crate) fn child_process( pub(crate) fn child_process(
mut child: tokio::process::Child, mut child: Box<dyn TokioChildWrapper>,
) -> std::io::Result<(tokio::process::Child, (ChildStdout, ChildStdin))> { ) -> std::io::Result<(Box<dyn TokioChildWrapper>, (ChildStdout, ChildStdin))> {
if child.stdin.is_none() { let child_stdin = match child.inner_mut().stdin().take() {
return Err(std::io::Error::other("std in was taken")); Some(stdin) => stdin,
} None => return Err(std::io::Error::other("std in was taken")),
if child.stdout.is_none() { };
return Err(std::io::Error::other("std out was taken")); let child_stdout = match child.inner_mut().stdout().take() {
} Some(stdout) => stdout,
let child_stdin = child.stdin.take().expect("already checked"); None => return Err(std::io::Error::other("std out was taken")),
let child_stdout = child.stdout.take().expect("already checked"); };
Ok((child, (child_stdout, child_stdin))) Ok((child, (child_stdout, child_stdin)))
} }
pub struct TokioChildProcess { pub struct TokioChildProcess {
child: tokio::process::Child, child: ChildWithCleanup,
child_stdin: ChildStdin, child_stdin: ChildStdin,
child_stdout: ChildStdout, child_stdout: ChildStdout,
} }
pub struct ChildWithCleanup {
inner: Box<dyn TokioChildWrapper>,
}
impl Drop for ChildWithCleanup {
fn drop(&mut self) {
if let Err(e) = self.inner.start_kill() {
tracing::warn!("Failed to kill child process: {e}");
}
}
}
// we hold the child process with stdout, for it's easier to implement AsyncRead // we hold the child process with stdout, for it's easier to implement AsyncRead
pin_project_lite::pin_project! { pin_project_lite::pin_project! {
pub struct TokioChildProcessOut { pub struct TokioChildProcessOut {
child: tokio::process::Child, child: ChildWithCleanup,
#[pin] #[pin]
child_stdout: ChildStdout, child_stdout: ChildStdout,
} }
@ -46,14 +59,18 @@ impl AsyncRead for TokioChildProcessOut {
} }
impl TokioChildProcess { impl TokioChildProcess {
pub fn new(child: &mut tokio::process::Command) -> std::io::Result<Self> { pub fn new(mut command: tokio::process::Command) -> std::io::Result<Self> {
child command
.kill_on_drop(true)
.stdin(std::process::Stdio::piped()) .stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped()); .stdout(std::process::Stdio::piped());
let (child, (child_stdout, child_stdin)) = child_process(child.spawn()?)?; let mut command_wrap = TokioCommandWrap::from(command);
#[cfg(unix)]
command_wrap.wrap(process_wrap::tokio::ProcessGroup::leader());
#[cfg(windows)]
command_wrap.wrap(process_wrap::tokio::JobObject);
let (child, (child_stdout, child_stdin)) = child_process(command_wrap.spawn()?)?;
Ok(Self { Ok(Self {
child, child: ChildWithCleanup { inner: child },
child_stdin, child_stdin,
child_stdout, child_stdout,
}) })
@ -82,3 +99,14 @@ impl<R: ServiceRole> IntoTransport<R, std::io::Error, ()> for TokioChildProcess
) )
} }
} }
pub trait ConfigureCommandExt {
fn configure(self, f: impl FnOnce(&mut Self)) -> Self;
}
impl ConfigureCommandExt for tokio::process::Command {
fn configure(mut self, f: impl FnOnce(&mut Self)) -> Self {
f(&mut self);
self
}
}

View file

@ -2,7 +2,7 @@ use rmcp::{
ServiceExt, ServiceExt,
service::QuitReason, service::QuitReason,
transport::{ transport::{
SseServer, StreamableHttpClientTransport, TokioChildProcess, ConfigureCommandExt, SseServer, StreamableHttpClientTransport, TokioChildProcess,
streamable_http_server::axum::StreamableHttpServer, streamable_http_server::axum::StreamableHttpServer,
}, },
}; };
@ -59,9 +59,10 @@ async fn test_with_js_server() -> anyhow::Result<()> {
.spawn()? .spawn()?
.wait() .wait()
.await?; .await?;
let transport = TokioChildProcess::new( let transport =
tokio::process::Command::new("node").arg("tests/test_with_js/server.js"), TokioChildProcess::new(tokio::process::Command::new("node").configure(|cmd| {
)?; cmd.arg("tests/test_with_js/server.js");
}))?;
let client = ().serve(transport).await?; let client = ().serve(transport).await?;
let resources = client.list_all_resources().await?; let resources = client.list_all_resources().await?;

View file

@ -1,6 +1,6 @@
use rmcp::{ use rmcp::{
ServiceExt, ServiceExt,
transport::{SseServer, TokioChildProcess}, transport::{ConfigureCommandExt, SseServer, TokioChildProcess},
}; };
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
mod common; mod common;
@ -54,11 +54,9 @@ async fn test_with_python_server() -> anyhow::Result<()> {
.spawn()? .spawn()?
.wait() .wait()
.await?; .await?;
let transport = TokioChildProcess::new( let transport = TokioChildProcess::new(tokio::process::Command::new("uv").configure(|cmd| {
tokio::process::Command::new("uv") cmd.arg("run").arg("tests/test_with_python/server.py");
.arg("run") }))?;
.arg("tests/test_with_python/server.py"),
)?;
let client = ().serve(transport).await?; let client = ().serve(transport).await?;
let resources = client.list_all_resources().await?; let resources = client.list_all_resources().await?;

View file

@ -17,12 +17,12 @@ rmcp = { git = "https://github.com/modelcontextprotocol/rust-sdk", branch = "mai
### 快速上手 ### 快速上手
一行代码启动客户端: 一行代码启动客户端:
```rust ```rust
use rmcp::{ServiceExt, transport::TokioChildProcess}; use rmcp::{ServiceExt, transport::{TokioChildProcess, ConfigureCommandExt}};
use tokio::process::Command; use tokio::process::Command;
let client = ().serve( let client = ().serve(TokioChildProcess::new(Command::new("npx").configure(|cmd| {
TokioChildProcess::new(Command::new("npx").arg("-y").arg("@modelcontextprotocol/server-everything"))? cmd.arg("-y").arg("@modelcontextprotocol/server-everything");
).await?; }))?).await?;
``` ```
#### 1. 构建传输层 #### 1. 构建传输层
@ -63,7 +63,7 @@ let server = service.serve(transport).await?;
一旦服务初始化完成,你可以发送请求或通知: 一旦服务初始化完成,你可以发送请求或通知:
```rust, ignore ```rust, ignore
// 请求 // 请求
let roots = server.list_roots().await?; let roots = server.list_roots().await?;
// 或发送通知 // 或发送通知

View file

@ -1,7 +1,11 @@
use std::collections::HashMap; use std::collections::HashMap;
use anyhow::Result; use anyhow::Result;
use rmcp::{model::CallToolRequestParam, service::ServiceExt, transport::TokioChildProcess}; use rmcp::{
model::CallToolRequestParam,
service::ServiceExt,
transport::{ConfigureCommandExt, TokioChildProcess},
};
use tokio::process::Command; use tokio::process::Command;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
@ -20,9 +24,11 @@ async fn main() -> Result<()> {
for idx in 0..10 { for idx in 0..10 {
let service = () let service = ()
.into_dyn() .into_dyn()
.serve(TokioChildProcess::new( .serve(TokioChildProcess::new(Command::new("uvx").configure(
Command::new("uvx").arg("mcp-server-git"), |cmd| {
)?) cmd.arg("mcp-client-git");
},
))?)
.await?; .await?;
client_list.insert(idx, service); client_list.insert(idx, service);
} }

View file

@ -3,7 +3,7 @@ use rmcp::{
ServiceExt, ServiceExt,
model::{CallToolRequestParam, GetPromptRequestParam, ReadResourceRequestParam}, model::{CallToolRequestParam, GetPromptRequestParam, ReadResourceRequestParam},
object, object,
transport::TokioChildProcess, transport::{ConfigureCommandExt, TokioChildProcess},
}; };
use tokio::process::Command; use tokio::process::Command;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
@ -21,11 +21,11 @@ async fn main() -> Result<()> {
// Start server // Start server
let service = () let service = ()
.serve(TokioChildProcess::new( .serve(TokioChildProcess::new(Command::new("npx").configure(
Command::new("npx") |cmd| {
.arg("-y") cmd.arg("-y").arg("@modelcontextprotocol/server-everything");
.arg("@modelcontextprotocol/server-everything"), },
)?) ))?)
.await?; .await?;
// Initialize // Initialize

View file

@ -1,5 +1,9 @@
use anyhow::Result; use anyhow::Result;
use rmcp::{model::CallToolRequestParam, service::ServiceExt, transport::TokioChildProcess}; use rmcp::{
model::CallToolRequestParam,
service::ServiceExt,
transport::{ConfigureCommandExt, TokioChildProcess},
};
use tokio::process::Command; use tokio::process::Command;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
@ -14,17 +18,14 @@ async fn main() -> Result<()> {
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();
let service = () let service = ()
.serve(TokioChildProcess::new( .serve(TokioChildProcess::new(Command::new("uvx").configure(
Command::new("uvx").arg("mcp-server-git"), |cmd| {
)?) cmd.arg("mcp-server-git");
},
))?)
.await?; .await?;
// or // or serve_client((), TokioChildProcess::new(cmd)?).await?;
// serve_client(
// (),
// TokioChildProcess::new(Command::new("uvx").arg("mcp-server-git"))?,
// )
// .await?;
// Initialize // Initialize
let server_info = service.peer_info(); let server_info = service.peer_info();

View file

@ -1,6 +1,6 @@
use std::{collections::HashMap, process::Stdio}; use std::{collections::HashMap, process::Stdio};
use rmcp::{RoleClient, ServiceExt, service::RunningService}; use rmcp::{RoleClient, ServiceExt, service::RunningService, transport::ConfigureCommandExt};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::mcp_adaptor::McpManager; use crate::mcp_adaptor::McpManager;
@ -70,10 +70,9 @@ impl McpServerTransportConfig {
envs, envs,
} => { } => {
let transport = rmcp::transport::TokioChildProcess::new( let transport = rmcp::transport::TokioChildProcess::new(
tokio::process::Command::new(command) tokio::process::Command::new(command).configure(|cmd| {
.args(args) cmd.args(args).envs(envs).stderr(Stdio::null());
.envs(envs) }),
.stderr(Stdio::null()),
)?; )?;
().serve(transport).await? ().serve(transport).await?
} }

View file

@ -1,7 +1,7 @@
use std::{collections::HashMap, path::Path, process::Stdio}; use std::{collections::HashMap, path::Path, process::Stdio};
use anyhow::Result; use anyhow::Result;
use rmcp::{RoleClient, ServiceExt, service::RunningService}; use rmcp::{RoleClient, ServiceExt, service::RunningService, transport::ConfigureCommandExt};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -55,11 +55,12 @@ impl McpServerTransportConfig {
envs, envs,
} => { } => {
let transport = rmcp::transport::child_process::TokioChildProcess::new( let transport = rmcp::transport::child_process::TokioChildProcess::new(
tokio::process::Command::new(command) tokio::process::Command::new(command).configure(|cmd| {
.args(args) cmd.args(args)
.envs(envs) .envs(envs)
.stderr(Stdio::inherit()) .stderr(Stdio::inherit())
.stdout(Stdio::inherit()), .stdout(Stdio::inherit());
}),
)?; )?;
().serve(transport).await? ().serve(transport).await?
} }