Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore(mcp): remove unused module client code

Steven Enamakela4dcdfa04198
diff --git a/crates/openhuman-core/src/mcp/module_client.rs b/crates/openhuman-core/src/mcp/module_client.rs
index d04e6df2af..d45ba73185 100644
--- a/crates/openhuman-core/src/mcp/module_client.rs
+++ b/crates/openhuman-core/src/mcp/module_client.rs
@@ -8,11 +8,10 @@
use std::time::Duration;
use std::{
- collections::HashMap,
path::PathBuf,
sync::{
atomic::{AtomicBool, Ordering},
- Arc, Mutex, OnceLock,
+ Arc,
},
};
@@ -21,6 +20,7 @@ use serde::{de::DeserializeOwned, Serialize};
use serde_json::{Map, Value};
use tinymcp_bus::{
agent_tools::{McpToolPresentation, PrepareAgentToolsRequest},
+ client_lifecycle::{CleanupAction, ConfiguredLifecycle, Fault, Poll, Retire, Step},
config::{McpClientConfig, OpenConfiguredRequest, OpenConfiguredResponse, TransitionStatus},
names, McpRemoteTool, ScrubStaticToolsRequest, ServerArgumentsValidation, SupervisorBatch,
ValidateServerArgumentsRequest,
@@ -34,49 +34,9 @@ use crate::{
const MODULE: &str = "tinymcp";
const TRANSITION_TIMEOUT: Duration = Duration::from_secs(30);
const TRANSITION_POLL_INTERVAL: Duration = Duration::from_millis(20);
-static CLIENTS: OnceLock<Mutex<HashMap<PathBuf, Arc<McpConfiguredClient>>>> = OnceLock::new();
-type CleanupAction = Arc<dyn Fn() -> bool + Send + Sync>;
-
-#[derive(Default)]
-struct CleanupReservation {
- after_close: Mutex<Option<CleanupAction>>,
-}
-
-static CLEANUP_RESERVATIONS: OnceLock<Mutex<HashMap<PathBuf, Arc<CleanupReservation>>>> =
- OnceLock::new();
static SHUTDOWN_REGISTERED: AtomicBool = AtomicBool::new(false);
static SHUTTING_DOWN: AtomicBool = AtomicBool::new(false);
-#[cfg(test)]
-static AFTER_CONTEXT_CHECK: OnceLock<Mutex<Option<(PathBuf, tokio::sync::oneshot::Sender<()>)>>> =
- OnceLock::new();
-
-#[derive(Clone)]
-struct OpenObject {
- config: McpClientConfig,
- object_path: String,
-}
-
-#[derive(Clone)]
-enum State {
- Closed,
- StartingOpen {
- config: McpClientConfig,
- previous: Option<OpenObject>,
- },
- Opening {
- config: McpClientConfig,
- previous: Option<OpenObject>,
- receipt: OpenConfiguredResponse,
- },
- Open(OpenObject),
- StartingClose(OpenObject),
- Closing {
- object: OpenObject,
- transition_id: String,
- },
-}
-
#[async_trait]
pub(crate) trait McpModulePort: Send + Sync {
async fn open_configured(
@@ -241,7 +201,7 @@ impl McpModulePort for TinyMcpPort {
pub(crate) struct McpConfiguredClient {
data_dir: String,
port: Box<dyn McpModulePort>,
- state: tokio::sync::Mutex<State>,
+ state: tokio::sync::Mutex<ConfiguredLifecycle>,
retiring: AtomicBool,
}
@@ -459,150 +419,65 @@ impl McpConfiguredClient {
/// retried from the retained transition state on the next call.
pub(crate) async fn close(&self) -> Result<(), ModuleCallError> {
let mut state = self.state.lock().await;
- match state.clone() {
- State::Closed => return Ok(()),
- State::Open(object) => *state = State::StartingClose(object),
- State::StartingOpen { .. } | State::Opening { .. } => {
- self.finish_open(&mut state).await?;
- if let State::Open(object) = state.clone() {
- *state = State::StartingClose(object);
- }
- }
- State::StartingClose(_) | State::Closing { .. } => {}
- }
- self.finish_close(&mut state).await
+ self.drive(&mut state, None).await.map(|_| ())
}
async fn ensure_open(
&self,
- state: &mut State,
+ state: &mut ConfiguredLifecycle,
requested: &McpClientConfig,
- ) -> Result<OpenObject, ModuleCallError> {
- loop {
- match state.clone() {
- State::Open(object) if object.config == *requested => return Ok(object),
- State::Open(object) => {
- *state = State::StartingOpen {
- config: requested.clone(),
- previous: Some(object),
- };
- }
- State::Closed => {
- *state = State::StartingOpen {
- config: requested.clone(),
- previous: None,
- };
- }
- State::StartingOpen { .. } | State::Opening { .. } => {
- self.finish_open(state).await?;
- }
- State::StartingClose(_) | State::Closing { .. } => {
- self.finish_close(state).await?;
- }
- }
- }
+ ) -> Result<tinymcp_bus::client_lifecycle::OpenObject, ModuleCallError> {
+ self.drive(state, Some(requested))
+ .await?
+ .ok_or(ModuleCallError::ModuleFault)
}
- async fn finish_open(&self, state: &mut State) -> Result<(), ModuleCallError> {
- let mut retried_terminal_transition = false;
+ /// Performs the bus call each `Step` of the module-owned lifecycle names
+ /// and feeds the reply back, until the target is open (`Some`) or closed.
+ async fn drive(
+ &self,
+ state: &mut ConfiguredLifecycle,
+ requested: Option<&McpClientConfig>,
+ ) -> Result<Option<tinymcp_bus::client_lifecycle::OpenObject>, ModuleCallError> {
loop {
- match state.clone() {
- State::StartingOpen { config, previous } => {
- // Record intent before awaiting the confidential call. If
- // the caller is cancelled after TinyMCP accepts it, retrying
- // this same configuration recovers the idempotent receipt.
- match self.port.open_configured(&self.data_dir, &config).await {
- Ok(receipt) => {
- *state = State::Opening {
- config,
- previous,
- receipt,
- };
- }
- Err(error) => {
- *state = previous.map_or(State::Closed, State::Open);
- return Err(error);
- }
- }
+ let step = match requested {
+ Some(config) => state.open_step(config),
+ None => state.close_step(),
+ };
+ match step {
+ Step::Ready(object) => return Ok(Some(object)),
+ Step::Closed => return Ok(None),
+ Step::CallOpen(config) => {
+ // The lifecycle recorded intent before this confidential
+ // call; retrying the same configuration recovers the
+ // module's idempotent receipt after a cancelled caller.
+ let reply = self.port.open_configured(&self.data_dir, &config).await;
+ state.open_replied(reply.as_ref().map(Clone::clone).map_err(|_| ()));
+ reply?;
}
- State::Opening {
- config,
- previous,
- receipt,
- } => {
- let status = self.poll_transition(&receipt.transition_id).await?;
- match status {
- TransitionStatus::Pending => continue,
- TransitionStatus::Complete => {
- *state = State::Open(OpenObject {
- config,
- object_path: receipt.object_path,
- });
- return Ok(());
- }
- TransitionStatus::Failed | TransitionStatus::Unknown => {
- if retried_terminal_transition {
- return Err(ModuleCallError::TransportFailed);
- }
- retried_terminal_transition = true;
- *state = State::StartingOpen { config, previous };
- }
- _ => return Err(ModuleCallError::ModuleFault),
- }
+ Step::PollOpen(id) => {
+ let status = self.poll_transition(&id).await?;
+ Self::settle(state.open_polled(status))?;
+ }
+ Step::CallClose => {
+ let reply = self.port.close(&self.data_dir).await;
+ state.close_replied(reply.as_ref().map(Clone::clone).map_err(|_| ()));
+ reply?;
}
- State::Open(_) | State::Closed => return Ok(()),
- State::StartingClose(_) | State::Closing { .. } => {
- return Err(ModuleCallError::TransportFailed);
+ Step::PollClose(id) => {
+ let status = self.poll_transition(&id).await?;
+ Self::settle(state.close_polled(status))?;
}
+ _ => return Err(ModuleCallError::ModuleFault),
}
}
}
- async fn finish_close(&self, state: &mut State) -> Result<(), ModuleCallError> {
- let mut retried_terminal_transition = false;
- loop {
- match state.clone() {
- State::Closed => return Ok(()),
- State::StartingClose(object) => {
- // The module returns the same pending receipt if a previous
- // Close reply was lost. `None` means cleanup already ended.
- match self.port.close(&self.data_dir).await {
- Ok(Some(transition_id)) => {
- *state = State::Closing {
- object,
- transition_id,
- };
- }
- Ok(None) => {
- *state = State::Closed;
- return Ok(());
- }
- Err(error) => return Err(error),
- }
- }
- State::Closing {
- object,
- transition_id,
- } => match self.poll_transition(&transition_id).await? {
- TransitionStatus::Pending => continue,
- TransitionStatus::Complete => {
- *state = State::Closed;
- return Ok(());
- }
- TransitionStatus::Failed | TransitionStatus::Unknown => {
- if retried_terminal_transition {
- return Err(ModuleCallError::TransportFailed);
- }
- retried_terminal_transition = true;
- *state = State::StartingClose(object);
- }
- _ => return Err(ModuleCallError::ModuleFault),
- },
- State::Open(object) => *state = State::StartingClose(object),
- State::StartingOpen { .. } | State::Opening { .. } => {
- return Err(ModuleCallError::TransportFailed);
- }
- }
+ fn settle(poll: Poll) -> Result<(), ModuleCallError> {
+ match poll {
+ Poll::Fault(Fault::Transport) => Err(ModuleCallError::TransportFailed),
+ Poll::Fault(_) => Err(ModuleCallError::ModuleFault),
+ _ => Ok(()),
}
}
@@ -637,3 +512,13 @@ fn current_agent_context_is_removed() -> bool {
#[cfg(test)]
#[path = "module_client_tests.rs"]
mod tests;
+
+impl Retire for McpConfiguredClient {
+ fn mark_retiring(&self) {
+ self.retiring.store(true, Ordering::Release);
+ }
+
+ fn is_retiring(&self) -> bool {
+ self.retiring.load(Ordering::Acquire)
+ }
+}