The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
chore: files changed crates/openhuman-core/src/mcp/server/module.rs,crates/openhuman-core/src/mcp/se
Steven Enamakelce655c4a33a5
diff --git a/crates/openhuman-core/src/mcp/server/module.rs b/crates/openhuman-core/src/mcp/server/module.rsindex 1208b40f1b..8431346dc3 100644--- a/crates/openhuman-core/src/mcp/server/module.rs+++ b/crates/openhuman-core/src/mcp/server/module.rs@@ -1,6 +1,8 @@//! TinyMCP server-protocol adapter over the published bus contract.use std::{+ future::Future,+ pin::Pin,sync::{atomic::{AtomicBool, AtomicU64, Ordering},Arc,@@ -11,7 +13,7 @@ use std::{use async_trait::async_trait;use serde_json::{Map, Value};use tinymcp_bus::{- names, ServerHostReply, ServerHttpResponseMetadata, ServerHttpSessionConfig,+ names, ServerHostCall, ServerHostReply, ServerHttpResponseMetadata, ServerHttpSessionConfig,ServerHttpSessionRequest, ServerInput, ServerOperationRef, ServerOperationSnapshot,ServerOperationState, ServerSessionConfig,};@@ -71,6 +73,9 @@ pub(super) trait ServerPort: Send + Sync {) -> Result<ServerHttpResponseMetadata, ModuleCallError>;}+type HostCallbackFuture = Pin<Box<dyn Future<Output = ServerHostReply> + Send + 'static>>;+type HostCallbackDispatcher = Arc<dyn Fn(ServerHostCall) -> HostCallbackFuture + Send + Sync>;+struct TinyMcpPort {client: ModuleClient,}@@ -149,6 +154,7 @@ impl ServerPort for TinyMcpPort {pub(super) struct ServerSession {port: Arc<dyn ServerPort>,+ callback_dispatch: HostCallbackDispatcher,id: String,next_operation: AtomicU64,pending_input: tokio::sync::Mutex<Option<PendingInput>>,@@ -204,6 +210,19 @@ impl ServerSession {}async fn open_with_port(port: Arc<dyn ServerPort>) -> Result<Self, ServerError> {+ Self::open_with_port_and_dispatch(port, |call| Box::pin(handler::dispatch(call))).await+ }++ async fn open_with_port_and_dispatch<F, Fut>(+ port: Arc<dyn ServerPort>,+ dispatch: F,+ ) -> Result<Self, ServerError>+ where+ F: Fn(ServerHostCall) -> Fut + Send + Sync + 'static,+ Fut: Future<Output = ServerHostReply> + Send + 'static,+ {+ let callback_dispatch: HostCallbackDispatcher =+ Arc::new(move |call| Box::pin(dispatch(call)));let (info, source_type_prefix, resources) = handler::declarations();let sequence = NEXT_SESSION_ID.fetch_add(1, Ordering::Relaxed).max(1);let id = port@@ -217,6 +236,7 @@ impl ServerSession {.map_err(ServerError::Module)?;Ok(Self {port,+ callback_dispatch,id,next_operation: AtomicU64::new(1),pending_input: tokio::sync::Mutex::new(None),@@ -248,6 +268,7 @@ impl ServerSession {.map_err(ServerError::Module)?;Ok(Self {port,+ callback_dispatch: Arc::new(|call| Box::pin(handler::dispatch(call))),id,next_operation: AtomicU64::new(1),pending_input: tokio::sync::Mutex::new(None),@@ -364,7 +385,7 @@ impl ServerSession {{*pending = Some(PendingCallback::Running {id: callback_id.clone(),- task: tokio::spawn(handler::dispatch(call)),+ task: tokio::spawn((self.callback_dispatch)(call)),});}let result = match pending.as_mut() {diff --git a/crates/openhuman-core/src/mcp/server/module_tests.rs b/crates/openhuman-core/src/mcp/server/module_tests.rsindex d133a35811..91bd888635 100644--- a/crates/openhuman-core/src/mcp/server/module_tests.rs+++ b/crates/openhuman-core/src/mcp/server/module_tests.rs@@ -2,6 +2,16 @@ use super::*;use std::sync::Mutex;use tinymcp_bus::ServerHostCall;+struct CallbackDrop(Option<tokio::sync::oneshot::Sender<()>>);++impl Drop for CallbackDrop {+ fn drop(&mut self) {+ if let Some(sender) = self.0.take() {+ let _ = sender.send(());+ }+ }+}+#[derive(Default)]struct PortState {opened: Option<ServerSessionConfig>,@@ -199,6 +209,62 @@ async fn server_session_cancellation_reaches_module_owner() {assert!(port.0.lock().unwrap().cancelled);}+#[tokio::test]+async fn closing_session_aborts_and_joins_a_running_host_callback() {+ let port = Arc::new(FixturePort(Mutex::new(PortState::default())));+ let (started_tx, started_rx) = tokio::sync::oneshot::channel();+ let started_tx = Arc::new(Mutex::new(Some(started_tx)));+ let (dropped_tx, dropped_rx) = tokio::sync::oneshot::channel();+ let dropped_tx = Arc::new(Mutex::new(Some(dropped_tx)));+ let release = CancellationToken::new();+ let dispatch = {+ let started_tx = Arc::clone(&started_tx);+ let dropped_tx = Arc::clone(&dropped_tx);+ let release = release.clone();+ move |_call| {+ let started = started_tx.lock().unwrap().take();+ let dropped = dropped_tx.lock().unwrap().take();+ let release = release.clone();+ async move {+ let _drop_notice = CallbackDrop(dropped);+ if let Some(started) = started {+ let _ = started.send(());+ }+ release.cancelled().await;+ ServerHostReply::Success { value: Value::Null }+ }+ }+ };+ let session = Arc::new(+ ServerSession::open_with_port_and_dispatch(port, dispatch)+ .await+ .unwrap(),+ );+ let submit_session = Arc::clone(&session);+ let submit = tokio::spawn(async move {+ submit_session+ .submit(+ Map::new(),+ r#"{"jsonrpc":"2.0","id":1,"method":"resources/read"}"#.to_owned(),+ &CancellationToken::new(),+ )+ .await+ });+ started_rx.await.expect("host callback started");++ session.close().await.unwrap();+ let callback_was_joined = dropped_rx.try_recv().is_ok();+ // Let the pre-fix implementation finish its detached task so a RED run+ // does not leave work running after the test returns.+ release.cancel();+ let _ = submit.await.unwrap();++ assert!(+ callback_was_joined,+ "terminal session close must abort and join an in-flight host callback"+ );+}+#[tokio::test]async fn server_session_fails_closed_when_module_is_unavailable() {let port = Arc::new(FixturePort(Mutex::new(PortState {