Skip to content

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

feat(host): fall back to single open workspace in try_service

Steven Enamakela4b0abbb6920
diff --git a/src/openhuman/mcp/host.rs b/src/openhuman/mcp/host.rs
index 8321025054..3485720651 100644
--- a/src/openhuman/mcp/host.rs
+++ b/src/openhuman/mcp/host.rs
@@ -181,21 +181,38 @@ pub fn init(config: &Config) -> anyhow::Result<()> {
Ok(())
}
-/// The default workspace's service, or `None` before [`init`] has run.
+/// The service for the paths that have no `Config` to resolve by.
///
-/// Only for the paths that have no `Config` to resolve by. Anything holding one
-/// should call [`for_config`] instead, which cannot answer `None` and cannot
-/// answer from the wrong workspace.
+/// Anything holding one should call [`for_config`] instead, which cannot answer
+/// `None` and cannot answer from the wrong workspace.
+///
+/// Resolves in two steps. The workspace [`init`] opened wins, because that is
+/// the one this process booted against. Failing that — nothing has booted, but
+/// something opened a service anyway — a *single* open workspace is
+/// unambiguously the one meant, so it answers that. With several open and no
+/// default, there is no honest answer and it says so.
+///
+/// The second step is what makes the connection map readable from a caller that
+/// never booted: the agent's tool registry asks which MCP tools are live, and it
+/// asks by server id alone. Without it, a process that opened a service through
+/// [`for_config`] would still report nothing connected.
#[must_use]
pub fn try_service() -> Option<Arc<McpHost>> {
- let workspace = DEFAULT_WORKSPACE.get()?;
-
let hosts = HOSTS
.get()?
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
- hosts.get(workspace).map(Arc::clone)
+ if let Some(workspace) = DEFAULT_WORKSPACE.get() {
+ if let Some(service) = hosts.get(workspace) {
+ return Some(Arc::clone(service));
+ }
+ }
+
+ match hosts.len() {
+ 1 => hosts.values().next().map(Arc::clone),
+ _ => None,
+ }
}
/// The default workspace's service, erroring when it is not up yet.
diff --git a/src/openhuman/mcp/host_tests.rs b/src/openhuman/mcp/host_tests.rs
index 59cc943395..7d01dc6aae 100644
--- a/src/openhuman/mcp/host_tests.rs
+++ b/src/openhuman/mcp/host_tests.rs
@@ -284,3 +284,31 @@ fn two_workspaces_get_two_services() {
assert!(tinymcp::Store::path_for(first_dir.path()).exists());
assert!(tinymcp::Store::path_for(second_dir.path()).exists());
}
+
+#[test]
+fn the_only_open_service_answers_a_caller_with_no_configuration() {
+ // The agent's tool registry asks which MCP tools are live, and it asks by
+ // server id alone. A process that opened a service without booting — a
+ // test, or a host driving the library directly — would otherwise be told
+ // nothing is connected while a connection sits in the map.
+ //
+ // Shares the process map with the other cases here, so it asserts the
+ // resolution rather than a specific workspace: with several open and no
+ // default set, there is no honest answer and `None` is correct.
+ let temporary = tempfile::tempdir().expect("tempdir");
+ let mut config = config_without_docs();
+ config.workspace_dir = temporary.path().to_path_buf();
+
+ let opened = super::for_config(&config).expect("the service opens");
+
+ match super::try_service() {
+ Some(resolved) => assert!(
+ std::sync::Arc::ptr_eq(&resolved, &opened) || super::DEFAULT_WORKSPACE.get().is_some(),
+ "resolved a service that is neither the only one open nor the default"
+ ),
+ None => assert!(
+ super::DEFAULT_WORKSPACE.get().is_none(),
+ "answered None even though a default workspace is set"
+ ),
+ }
+}