Skip to content

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

fix(mcp): read an agent's connections without creating its MCP host (#7032)

oxoxDevc1e430d1f273
diff --git a/crates/openhuman-core/src/mcp/host.rs b/crates/openhuman-core/src/mcp/host.rs
index 612b4653b3..47bcea804e 100644
--- a/crates/openhuman-core/src/mcp/host.rs
+++ b/crates/openhuman-core/src/mcp/host.rs
@@ -227,6 +227,36 @@ pub fn for_config(config: &Config) -> anyhow::Result<Arc<McpHost>> {
Ok(service)
}
+/// The service for `config` when one exists, without creating an agent's.
+///
+/// Outside an agent context this is [`for_config`]. Under one, an agent that
+/// never installed a server has no store on disk, and a read (listing
+/// connections or tools) must not create one: it answers with an error the
+/// read paths already treat as "nothing connected".
+///
+/// # Errors
+///
+/// When the agent has no host yet, or [`for_config`] fails.
+pub fn lookup(config: &Config) -> anyhow::Result<Arc<McpHost>> {
+ let key = host_key(config);
+ if key != config.workspace_dir {
+ let open = HOSTS.get().and_then(|hosts| {
+ hosts
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .get(&key)
+ .map(|entry| Arc::clone(&entry.host))
+ });
+ if let Some(host) = open {
+ return Ok(host);
+ }
+ if !Store::path_for(&key).exists() {
+ anyhow::bail!("the agent has no mcp host yet");
+ }
+ }
+ for_config(config)
+}
+
/// Opens the service for `config` and marks its workspace the default.
///
/// Called once, from the core startup path. A second call opens nothing new and
@@ -304,7 +334,7 @@ fn host_key(config: &Config) -> PathBuf {
/// be opened, which must not fall back to another agent's or the default one.
fn current_agent_host() -> Option<Option<Arc<McpHost>>> {
let agent = crate::core::runtime::agent_scope::current_agent_id()?;
- let opened = crate::core::runtime::CoreContext::with_current_embedder_config(for_config)?;
+ let opened = crate::core::runtime::CoreContext::with_current_embedder_config(lookup)?;
Some(match opened {
Ok(host) => Some(host),
Err(error) => {
diff --git a/crates/openhuman-core/src/mcp/host_agent_tests.rs b/crates/openhuman-core/src/mcp/host_agent_tests.rs
index fc538cd447..f1e4aba9c8 100644
--- a/crates/openhuman-core/src/mcp/host_agent_tests.rs
+++ b/crates/openhuman-core/src/mcp/host_agent_tests.rs
@@ -44,10 +44,11 @@ async fn the_ambient_service_under_an_agent_is_that_agents_host() {
let temporary = tempfile::tempdir().expect("tempdir");
let config = workspace_config(temporary.path());
- let (ambient, addressed) = CoreContext::scope(agent_context(&config, "gamma"), async {
+ let (addressed, ambient) = CoreContext::scope(agent_context(&config, "gamma"), async {
+ let addressed = for_config(&config).expect("the agent's host opens");
(
- try_service().expect("an agent always has a host"),
- for_config(&config).expect("the agent's host opens"),
+ addressed,
+ try_service().expect("an opened agent host is ambient"),
)
})
.await;
@@ -76,3 +77,18 @@ async fn an_evicted_agent_host_is_reopened_fresh() {
.await;
assert!(!Arc::ptr_eq(&first, &reopened));
}
+
+#[tokio::test]
+async fn reading_an_agents_connections_does_not_create_its_host() {
+ let temporary = tempfile::tempdir().expect("tempdir");
+ let config = workspace_config(temporary.path());
+
+ let (looked_up, ambient) = CoreContext::scope(agent_context(&config, "epsilon"), async {
+ (lookup(&config).is_err(), try_service().is_none())
+ })
+ .await;
+
+ assert!(looked_up, "no host exists for a fresh agent");
+ assert!(ambient, "the ambient lookup answers nothing connected");
+ assert!(!temporary.path().join("agents").join("epsilon").exists());
+}
diff --git a/crates/openhuman-core/src/mcp/registry/mod.rs b/crates/openhuman-core/src/mcp/registry/mod.rs
index d6b164db77..6ccfb450b3 100644
--- a/crates/openhuman-core/src/mcp/registry/mod.rs
+++ b/crates/openhuman-core/src/mcp/registry/mod.rs
@@ -108,7 +108,7 @@ pub mod connections {
/// [`disconnect_for_config`] exist: the ambient form resolves through the
/// process default, which stops answering once a second workspace is open.
pub async fn connected_overview_for_config(config: &Config) -> Vec<ConnectedServerOverview> {
- match host::for_config(config) {
+ match host::lookup(config) {
Ok(service) => service.dynamic().connected_overview().await,
Err(error) => {
tracing::debug!(
@@ -129,7 +129,7 @@ pub mod connections {
/// its connect finishes. Listing is not authorization — a call still
/// needs a live connection.
pub async fn cached_overview_for_config(config: &Config) -> Vec<ConnectedServerOverview> {
- let service = match host::for_config(config) {
+ let service = match host::lookup(config) {
Ok(service) => service,
Err(error) => {
tracing::debug!(?error, "[mcp] no host for workspace; no cached tools");
@@ -165,7 +165,7 @@ pub mod connections {
pub async fn all_connected_tools_for_config(
config: &Config,
) -> Vec<(String, String, tinymcp_bus::McpTool)> {
- match host::for_config(config) {
+ match host::lookup(config) {
Ok(service) => service.dynamic().connections().all_connected_tools().await,
Err(error) => {
tracing::debug!(?error, "[mcp] no host for workspace; reporting no tools");
@@ -206,7 +206,7 @@ pub mod connections {
config: &Config,
server_id: &str,
) -> Option<Vec<tinymcp_bus::McpTool>> {
- match host::for_config(config) {
+ match host::lookup(config) {
Ok(service) => service.dynamic().connections().tools_for(server_id).await,
Err(error) => {
tracing::debug!(?error, server_id, "[mcp] no host for workspace; no tools");
@@ -231,7 +231,7 @@ pub mod connections {
/// Whether a server has a live entry in `config`'s workspace.
pub async fn is_connected_for_config(config: &Config, server_id: &str) -> bool {
- match host::for_config(config) {
+ match host::lookup(config) {
Ok(service) => {
service
.dynamic()
@@ -252,7 +252,7 @@ pub mod connections {
/// Why a server's most recent attempt in `config`'s workspace hit a 401.
pub async fn auth_hint_for_config(config: &Config, server_id: &str) -> Option<&'static str> {
- match host::for_config(config) {
+ match host::lookup(config) {
Ok(service) => Some(
service
.dynamic()
@@ -320,7 +320,7 @@ pub mod connections {
/// through [`connect`] already named a workspace and should close over the
/// same one.
pub async fn disconnect_for_config(config: &Config, server_id: &str) -> bool {
- match host::for_config(config) {
+ match host::lookup(config) {
Ok(service) => service.dynamic().connections().disconnect(server_id).await,
Err(error) => {
tracing::debug!(?error, "[mcp] no host for workspace; nothing to disconnect");
@@ -331,7 +331,7 @@ pub mod connections {
/// The most recent failure message for a server in `config`'s workspace.
pub async fn last_error_for_config(config: &Config, server_id: &str) -> Option<String> {
- match host::for_config(config) {
+ match host::lookup(config) {
Ok(service) => service.dynamic().connections().last_error(server_id).await,
Err(error) => {
tracing::debug!(
@@ -356,7 +356,7 @@ pub mod boot {
/// Never fails: a server that cannot connect is logged and skipped, because
/// one broken third-party integration must not stop the core coming up.
pub async fn spawn_installed_servers(config: &Config) {
- let service = match host::for_config(config) {
+ let service = match host::lookup(config) {
Ok(service) => service,
Err(error) => {
tracing::warn!("[mcp] the service could not be opened: {error}");