Skip to content

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

refactor(mcp): drain supervisor events from the module

Steven Enamakel8106a14850f1
diff --git a/crates/openhuman-core/src/mcp/module_client.rs b/crates/openhuman-core/src/mcp/module_client.rs
index c502f22a01..6ec606bf33 100644
--- a/crates/openhuman-core/src/mcp/module_client.rs
+++ b/crates/openhuman-core/src/mcp/module_client.rs
@@ -22,6 +22,7 @@ use serde_json::Value;
use tinymcp_bus::{
config::{McpClientConfig, OpenConfiguredRequest, OpenConfiguredResponse, TransitionStatus},
names,
+ SupervisorBatch,
};
use crate::{
@@ -181,6 +182,48 @@ impl McpConfiguredClient {
Ok(client)
}
+ /// A snapshot of configured workspaces currently retained by the host.
+ /// The supervisor only drains open objects; it never creates a workspace.
+ pub(crate) fn open_clients() -> Vec<(PathBuf, Arc<Self>)> {
+ CLIENTS
+ .get()
+ .map(|clients| {
+ clients
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .iter()
+ .map(|(workspace, client)| (workspace.clone(), Arc::clone(client)))
+ .collect()
+ })
+ .unwrap_or_default()
+ }
+
+ /// Drain observations from the module-owned supervisor for an open child.
+ /// An object that is opening or closing is skipped and will be revisited
+ /// on the next host poll.
+ pub(crate) async fn drain_supervisor_events(
+ &self,
+ limit: usize,
+ ) -> Result<Option<SupervisorBatch>, ModuleCallError> {
+ let state = self.state.lock().await;
+ let State::Open(object) = &*state else {
+ return Ok(None);
+ };
+ let args = serde_json::to_value((limit,)).map_err(|_| ModuleCallError::ModuleFault)?;
+ let reply = self
+ .port
+ .call_object(
+ &object.object_path,
+ names::methods::DRAIN_SUPERVISOR_EVENTS,
+ args,
+ false,
+ )
+ .await?;
+ serde_json::from_value(reply)
+ .map(Some)
+ .map_err(|_| ModuleCallError::ModuleFault)
+ }
+
/// Register this runtime's cleanup. The flag is reset by the drained hook,
/// so a later embedded-runtime start can register a fresh one.
pub(crate) fn register_shutdown_hook() {
diff --git a/crates/openhuman-core/src/mcp/registry/mod.rs b/crates/openhuman-core/src/mcp/registry/mod.rs
index 4767bcdcc1..6014bd9504 100644
--- a/crates/openhuman-core/src/mcp/registry/mod.rs
+++ b/crates/openhuman-core/src/mcp/registry/mod.rs
@@ -347,82 +347,64 @@ pub mod connections {
#[cfg(feature = "mcp")]
pub mod boot {
use crate::config::Config;
- use crate::mcp::host;
+ use crate::mcp::{host, module_client::McpConfiguredClient};
/// Connects every enabled installed server.
///
/// 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::lookup(config) {
- Ok(service) => service,
+ let client = match McpConfiguredClient::for_config(config) {
+ Ok(client) => client,
Err(error) => {
tracing::warn!("[mcp] the service could not be opened: {error}");
return;
}
};
- let client = host::client_config(config);
- let outcome = tinymcp::registry::connect_installed_servers(
- service.dynamic().store(),
- service.dynamic().connections(),
- service.dynamic().oauth(),
- &client.client_identity,
- client.proxy.as_ref(),
- )
- .await;
-
- tracing::info!(
- connected = outcome.connected,
- failed = outcome.failed,
- skipped = outcome.skipped,
- "[mcp] startup connect finished"
- );
+ // Opening the configured object starts TinyMCP's owned maintenance
+ // task, whose boot pass connects installed servers. A static catalog
+ // read forces that open without recreating connection policy here.
+ if let Err(error) = client
+ .call::<serde_json::Value>(
+ &host::client_config(config),
+ tinymcp_bus::names::methods::STATIC_LIST,
+ (),
+ )
+ .await
+ {
+ tracing::warn!("[mcp] startup module open/connect pass failed: {error}");
+ }
}
}
/// Keeping installed servers connected.
#[cfg(feature = "mcp")]
pub mod supervisor {
- use crate::mcp::host;
+ use std::time::Duration;
+
+ use crate::mcp::{module_client::McpConfiguredClient, registry::supervisor_events};
/// Runs the reconnect supervisor until the process ends.
///
- /// One task for every host the process has opened, driven by
- /// `tinymcp::Supervisor::run_many`: the connection map is per-workspace,
- /// and a host opened after boot — a workspace switch — is supervised from
- /// the tick after it appears, with its backoff state kept per workspace.
- /// The first tick is delayed a whole interval so it does not race the
- /// startup connect pass.
+ /// One task drains events from every open TinyMCP child. Probe scheduling,
+ /// reconnection, and per-server backoff remain inside the owning module.
+ /// The first poll is delayed a whole interval so it does not race startup.
pub async fn run() {
- tinymcp::Supervisor::run_many(
- tinymcp::SupervisorConfig::default(),
- // Every host currently open. The identity and proxy each was
- // opened with ride along, so a reconnect dials the way the host's
- // own connections do.
- || {
- host::all_hosts()
- .into_iter()
- .map(
- |(workspace, registry, identity, proxy)| tinymcp::SupervisedHost {
- key: workspace,
- registry,
- identity,
- proxy,
- },
- )
- .collect()
- },
- // What the tick observed becomes this domain's events, so a probe
- // outcome reaches the Event Log and a server that stays down
- // reaches the user (#5931). The workspace goes with them: a
- // subscriber that persists or announces one must not take a
- // switched-away workspace's outage for its own.
- |workspace, report| {
- super::supervisor_events::publish(workspace, report);
- },
- )
- .await;
+ loop {
+ tokio::time::sleep(Duration::from_secs(60)).await;
+ for (workspace, client) in McpConfiguredClient::open_clients() {
+ match client.drain_supervisor_events(256).await {
+ Ok(Some(batch)) => {
+ supervisor_events::publish_batch(&workspace, &batch);
+ }
+ Ok(None) => {}
+ Err(error) => {
+ tracing::debug!(?error, workspace = ?workspace, "[mcp] supervisor drain failed");
+ }
+ }
+ }
+ }
}
}
diff --git a/crates/openhuman-core/src/mcp/registry/supervisor_events.rs b/crates/openhuman-core/src/mcp/registry/supervisor_events.rs
index e0113e97f8..4512758f0d 100644
--- a/crates/openhuman-core/src/mcp/registry/supervisor_events.rs
+++ b/crates/openhuman-core/src/mcp/registry/supervisor_events.rs
@@ -23,7 +23,7 @@
use std::path::Path;
-use tinymcp_bus::{ProbeOutcome, SupervisorEvent, TickReport};
+use tinymcp_bus::{ProbeOutcome, SupervisorBatch, SupervisorEvent, TickReport};
use crate::core::bus::BUS;
use crate::core::events::DomainEvent;
@@ -160,6 +160,31 @@ pub fn publish(workspace: &Path, report: &TickReport) -> usize {
published
}
+/// Publishes events drained from TinyMCP's module-owned maintenance task.
+/// Dropped observations remain visible in logs instead of being silently lost.
+pub fn publish_batch(workspace: &Path, batch: &SupervisorBatch) -> usize {
+ if batch.dropped > 0 {
+ tracing::warn!(
+ dropped = batch.dropped,
+ workspace = ?workspace,
+ "[mcp] module supervisor event queue overflowed"
+ );
+ }
+ let events = domain_events_for(workspace, &batch.events);
+ let published = events.len();
+ if published > 0 {
+ tracing::debug!(
+ observations = batch.events.len(),
+ published,
+ "{LOG_PREFIX} module supervisor observations published"
+ );
+ }
+ for event in events {
+ BUS.publish(event);
+ }
+ published
+}
+
#[cfg(test)]
#[path = "supervisor_events_tests.rs"]
mod tests;