Skip to content

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

feat(mcp): let a host evict an agent's MCP host (#7032)

oxoxDevaa54b7519c28
diff --git a/crates/openhuman-core/src/mcp/host.rs b/crates/openhuman-core/src/mcp/host.rs
index 4eeef57fa7..612b4653b3 100644
--- a/crates/openhuman-core/src/mcp/host.rs
+++ b/crates/openhuman-core/src/mcp/host.rs
@@ -278,6 +278,20 @@ pub fn try_service() -> Option<Arc<McpHost>> {
resolve(DEFAULT_WORKSPACE.get().map(PathBuf::as_path), &hosts)
}
+/// Removes the host of agent `agent_id` on `workspace_dir` from the open set
+/// and hands it back, so the caller can close its connections. `None` when
+/// the agent never opened one.
+pub fn take_agent_host(workspace_dir: &Path, agent_id: &str) -> Option<Arc<McpHost>> {
+ let key = workspace_dir.join("agents").join(agent_id);
+ let entry = HOSTS
+ .get()?
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .remove(&key)?;
+ tracing::debug!(agent = %agent_id, "[mcp] agent host evicted");
+ Some(entry.host)
+}
+
/// Where the host for `config` keeps its stores: the workspace, or the
/// agent's own directory beneath it when an agent context is current.
fn host_key(config: &Config) -> PathBuf {
diff --git a/crates/openhuman-core/src/mcp/host_agent_tests.rs b/crates/openhuman-core/src/mcp/host_agent_tests.rs
index 72e9ebdfe2..fc538cd447 100644
--- a/crates/openhuman-core/src/mcp/host_agent_tests.rs
+++ b/crates/openhuman-core/src/mcp/host_agent_tests.rs
@@ -56,3 +56,23 @@ async fn the_ambient_service_under_an_agent_is_that_agents_host() {
let outside = for_config(&config).expect("the workspace host opens");
assert!(!Arc::ptr_eq(&ambient, &outside));
}
+
+#[tokio::test]
+async fn an_evicted_agent_host_is_reopened_fresh() {
+ let temporary = tempfile::tempdir().expect("tempdir");
+ let config = workspace_config(temporary.path());
+
+ let first = CoreContext::scope(agent_context(&config, "delta"), async {
+ for_config(&config).expect("delta's host opens")
+ })
+ .await;
+ let taken = take_agent_host(temporary.path(), "delta").expect("delta had a host");
+ assert!(Arc::ptr_eq(&first, &taken));
+ assert!(take_agent_host(temporary.path(), "delta").is_none());
+
+ let reopened = CoreContext::scope(agent_context(&config, "delta"), async {
+ for_config(&config).expect("delta's host reopens")
+ })
+ .await;
+ assert!(!Arc::ptr_eq(&first, &reopened));
+}