Skip to content

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

refactor(mcp): move agent close helpers into module client

Steven Enamakelfc48e0aac9f3
diff --git a/crates/openhuman-core/src/mcp/host.rs b/crates/openhuman-core/src/mcp/host.rs
index 05ccb159b2..ad2e0c1949 100644
--- a/crates/openhuman-core/src/mcp/host.rs
+++ b/crates/openhuman-core/src/mcp/host.rs
@@ -60,36 +60,6 @@ pub async fn close_agent(_workspace_dir: &std::path::Path, _agent_id: &str) -> b
#[cfg(not(feature = "mcp"))]
pub fn schedule_agent_close(_workspace_dir: &std::path::Path, _agent_id: &str) {}
-/// Closes an already-cached agent-scoped MCP client and returns only after the
-/// module acknowledges cleanup. A failed close remains unavailable in the
-/// cache so a later retry or runtime shutdown can finish it.
-#[cfg(feature = "mcp")]
-pub async fn close_agent(workspace_dir: &std::path::Path, agent_id: &str) -> bool {
- crate::mcp::module_client::McpConfiguredClient::close_agent_data_dir(
- &workspace_dir.join("agents").join(agent_id),
- )
- .await
-}
-
-/// Schedules cleanup for a dropped agent without opening a client. The exact
-/// cached client is marked retired before the cleanup task is spawned.
-#[cfg(feature = "mcp")]
-pub fn schedule_agent_close(workspace_dir: &std::path::Path, agent_id: &str) {
- crate::mcp::module_client::McpConfiguredClient::schedule_agent_close_data_dir(
- &workspace_dir.join("agents").join(agent_id),
- );
-}
-
-/// The MCP feature is disabled, so there is no configured client to close.
-#[cfg(not(feature = "mcp"))]
-pub async fn close_agent(_workspace_dir: &std::path::Path, _agent_id: &str) -> bool {
- true
-}
-
-/// The MCP feature is disabled, so there is no configured client to close.
-#[cfg(not(feature = "mcp"))]
-pub fn schedule_agent_close(_workspace_dir: &std::path::Path, _agent_id: &str) {}
-
/// Converts the MCP section of product configuration to the module contract.
pub fn client_config(config: &Config) -> tinymcp_bus::McpClientConfig {
let mut client = tinymcp_bus::McpClientConfig {
diff --git a/crates/openhuman-core/src/mcp/module_client.rs b/crates/openhuman-core/src/mcp/module_client.rs
index f68394c6ac..c80d97f943 100644
--- a/crates/openhuman-core/src/mcp/module_client.rs
+++ b/crates/openhuman-core/src/mcp/module_client.rs
@@ -245,6 +245,10 @@ impl McpConfiguredClient {
ModuleClient::report_unavailable(MODULE);
return Err(ModuleCallError::Unavailable);
}
+ if current_agent_context_is_removed() {
+ ModuleClient::report_unavailable(MODULE);
+ return Err(ModuleCallError::Unavailable);
+ }
let data_dir = crate::core::runtime::agent_scope_dir(config);
let clients = CLIENTS.get_or_init(|| Mutex::new(HashMap::new()));
@@ -413,6 +417,19 @@ impl McpConfiguredClient {
});
}
+ pub(crate) fn agent_close_pending(data_dir: &std::path::Path) -> bool {
+ CLIENTS
+ .get()
+ .and_then(|clients| {
+ clients
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .get(data_dir)
+ .map(|client| client.retiring.load(Ordering::Acquire))
+ })
+ .unwrap_or(false)
+ }
+
fn retire_cached_client(data_dir: &std::path::Path) -> Option<Arc<Self>> {
let mut clients = CLIENTS
.get()
@@ -824,6 +841,17 @@ impl McpConfiguredClient {
}
}
+fn current_agent_context_is_removed() -> bool {
+ let Some(context) = crate::core::runtime::CoreContext::current() else {
+ return false;
+ };
+ context.session_agent().is_some()
+ && context
+ .host_overrides()
+ .and_then(|overrides| overrides.approval_scope())
+ .is_some_and(|scope| scope.is_closed())
+}
+
#[cfg(test)]
#[path = "module_client_tests.rs"]
mod tests;