Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
chore: don't block inmem session/new on MCP listing (#7563)
Wolf Mermelstein840263a6e11a
diff --git a/crates/agent_inmem/src/domain/agent.rs b/crates/agent_inmem/src/domain/agent.rsindex 2f4b35f832..a89283b6f7 100644--- a/crates/agent_inmem/src/domain/agent.rs+++ b/crates/agent_inmem/src/domain/agent.rs@@ -133,6 +133,10 @@ pub struct AgentState {/// The tools of those servers, once dialed; `None` until then or when/// there were none.pub mcp_tools: Mutex<Option<RemoteMcpToolSet>>,+ /// In-flight `connect_mcp` from `session/new` / `session/resume`. The+ /// handshake does not join it; the first turn does, so SearchTools still+ /// sees the catalog.+ pub mcp_connect: Mutex<Option<tokio::task::JoinHandle<()>>>,/// Whether the client advertised `elicitation.form` on `initialize`. The/// protocol forbids asking a mode the client did not advertise.pub client_renders_forms: AtomicBool,@@ -143,9 +147,9 @@ pub struct AgentState {impl AgentState {/// Dial the servers a session request carried and keep their tools for- /// every turn that follows. Done at `session/new`/`session/resume`, the- /// same moment a sandboxed harness connects its servers, so the first- /// turn already has them.+ /// every turn that follows. Started at `session/new`/`session/resume` so+ /// the handshake is not held by `tools/list`; the first turn joins the+ /// same work so SearchTools still has the catalog.async fn connect_mcp(&self, servers: Vec<AcpMcpServer>) {let servers = dialable_servers(servers);let requested = servers.len();@@ -163,7 +167,30 @@ impl AgentState {.expect("mcp tools lock should not be poisoned") = tools;}- fn current_mcp_tools(&self) -> Option<RemoteMcpToolSet> {+ /// Start [`Self::connect_mcp`] without joining it. `session/new` and+ /// `session/resume` call this so create can return while listing runs.+ fn start_connect_mcp(self: &Arc<Self>, servers: Vec<AcpMcpServer>) {+ let state = Arc::clone(self);+ let handle = tokio::spawn(async move {+ state.connect_mcp(servers).await;+ });+ *self+ .mcp_connect+ .lock()+ .expect("mcp connect lock should not be poisoned") = Some(handle);+ }++ /// The tools the next turn should compose, waiting out an in-flight+ /// connect so the searchable catalog is not empty on turn one.+ async fn mcp_tools_for_turn(&self) -> Option<RemoteMcpToolSet> {+ let handle = self+ .mcp_connect+ .lock()+ .expect("mcp connect lock should not be poisoned")+ .take();+ if let Some(handle) = handle {+ let _ = handle.await;+ }self.mcp_tools.lock().expect("mcp tools lock should not be poisoned")@@ -528,7 +555,7 @@ pub async fn serve(state: Arc<AgentState>, acp: AcpChannel) -> Result<(), AcpErrlet state = Arc::clone(&state);let acp_id = SessionId::new(macro_uuid::generate_uuid_v7().to_string());state.bind_acp_session(acp_id.clone(), false);- state.connect_mcp(request.mcp_servers).await;+ state.start_connect_mcp(request.mcp_servers);let responded = responder.respond(NewSessionResponse::new(acp_id.clone()).config_options(state.session_config_options()),@@ -549,7 +576,7 @@ pub async fn serve(state: Arc<AgentState>, acp: AcpChannel) -> Result<(), AcpErr// attach replayed the frame log back into it (see// `domain::replay`).state.bind_acp_session(request.session_id.clone(), true);- state.connect_mcp(request.mcp_servers).await;+ state.start_connect_mcp(request.mcp_servers);let responded = responder.respond(ResumeSessionResponse::new().config_options(state.session_config_options()),);@@ -753,6 +780,11 @@ async fn run_turn("agent.turn.admission_wait_ms",elapsed_ms(admission_started),);+ let mcp_tools = tokio::select! {+ biased;+ _ = cancel.cancelled() => return Ok(StopReason::Cancelled),+ tools = state.mcp_tools_for_turn() => tools,+ };let TurnInput {messages,model,@@ -771,7 +803,7 @@ async fn run_turn(identity,instructions,messages,- mcp_tools: state.current_mcp_tools(),+ mcp_tools,cancel: cancel.clone(),user_input: requester.clone()diff --git a/crates/agent_inmem/src/domain/agent/test.rs b/crates/agent_inmem/src/domain/agent/test.rsindex f0c37d838f..24aaf013f4 100644--- a/crates/agent_inmem/src/domain/agent/test.rs+++ b/crates/agent_inmem/src/domain/agent/test.rs@@ -88,6 +88,7 @@ async fn with_admission<Engine: TurnEngine, Out>(turn_lock: tokio::sync::Mutex::new(()),mcp: Arc::new(crate::domain::mcp::NoMcpServers),mcp_tools: std::sync::Mutex::new(None),+ mcp_connect: std::sync::Mutex::new(None),client_renders_forms: AtomicBool::new(false),enable_dev_commands: true,});@@ -854,8 +855,121 @@ impl crate::domain::mcp::McpToolConnector for Arc<SpyConnector> {}}-/// The servers `session/new` carries are dialed then and there, minus Macro's-/// own, whose tools this runtime already has natively.+async fn wait_until_asked(spy: &SpyConnector) {+ tokio::time::timeout(Duration::from_secs(1), async {+ loop {+ if !spy.asked.lock().expect("asked lock").is_empty() {+ return;+ }+ tokio::task::yield_now().await;+ }+ })+ .await+ .expect("connect should be spawned after session/new");+}++/// Holds `connect` until `release` is sent, and signals `started` when it+/// begins. Used to prove the handshake does not join listing, and the first+/// turn does.+struct HoldConnector {+ started: tokio::sync::mpsc::Sender<()>,+ release: tokio::sync::Mutex<Option<tokio::sync::oneshot::Receiver<()>>>,+}++impl crate::domain::mcp::McpToolConnector for Arc<HoldConnector> {+ async fn connect(+ &self,+ _servers: Vec<agent_client_protocol::schema::v1::McpServerHttp>,+ ) -> Option<mcp_toolset::RemoteMcpToolSet> {+ let _ = self.started.send(()).await;+ if let Some(release) = self.release.lock().await.take() {+ let _ = release.await;+ }+ None+ }+}++fn hold_connector() -> (+ Arc<HoldConnector>,+ tokio::sync::mpsc::Receiver<()>,+ tokio::sync::oneshot::Sender<()>,+) {+ let (started_tx, started_rx) = tokio::sync::mpsc::channel(1);+ let (release_tx, release_rx) = tokio::sync::oneshot::channel();+ let connector = Arc::new(HoldConnector {+ started: started_tx,+ release: tokio::sync::Mutex::new(Some(release_rx)),+ });+ (connector, started_rx, release_tx)+}++fn test_mcp_servers() -> Vec<agent_client_protocol::schema::v1::McpServer> {+ use agent_client_protocol::schema::v1::{HttpHeader, McpServer as AcpMcpServer, McpServerHttp};++ ["macro", "linear", "notion"]+ .into_iter()+ .map(|name| {+ AcpMcpServer::Http(+ McpServerHttp::new(name, format!("https://egress.test/mcp/{name}")).headers(vec![+ HttpHeader::new("Authorization", "Bearer session-token"),+ ]),+ )+ })+ .collect()+}++async fn serve_with_mcp(+ mcp: Arc<dyn crate::domain::mcp::DynMcpToolConnector>,+ engine: Arc<dyn TurnEngine>,+) -> (ConnectionTo<Agent>, SessionId, tokio::task::AbortHandle) {+ let store = Arc::new(SessionStore::new());+ let session_id = AgentSessionId::new();+ store.insert(+ session_id,+ crate::domain::session::SessionState::new("anthropic/claude-sonnet-5-5".into()),+ );+ let state = Arc::new(AgentState {+ session_id,+ owner: model_owner::Owner::User(+ MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),+ ),+ engine,+ admission: Arc::new(DisabledAiAdmissionService),+ store,+ active_cancel: std::sync::Mutex::new(Vec::new()),+ turn_lock: tokio::sync::Mutex::new(()),+ enable_dev_commands: false,+ mcp,+ mcp_tools: std::sync::Mutex::new(None),+ mcp_connect: std::sync::Mutex::new(None),+ client_renders_forms: AtomicBool::new(false),+ });+ let (client_channel, agent_channel) = AcpChannel::duplex();+ let agent = tokio::spawn(serve(state, agent_channel));+ let abort = agent.abort_handle();+ let (connection, session) = Client+ .builder()+ .connect_with(+ client_channel,+ async move |connection: ConnectionTo<Agent>| {+ connection+ .send_request(InitializeRequest::new(ProtocolVersion::V1))+ .block_task()+ .await?;+ let opened = connection+ .send_request(NewSessionRequest::new("/").mcp_servers(test_mcp_servers()))+ .block_task()+ .await?;+ Ok((connection, opened.session_id))+ },+ )+ .await+ .expect("initialize and session/new should succeed");+ (connection, session, abort)+}++/// The servers `session/new` carries are dialed in the background, minus+/// Macro's own, whose tools this runtime already has natively.#[tokio::test]async fn session_new_dials_the_advertised_servers_except_macros_own() {use agent_client_protocol::schema::v1::{HttpHeader, McpServer as AcpMcpServer, McpServerHttp};@@ -882,6 +996,7 @@ async fn session_new_dials_the_advertised_servers_except_macros_own() {enable_dev_commands: false,mcp: Arc::new(Arc::clone(&spy)),mcp_tools: std::sync::Mutex::new(None),+ mcp_connect: std::sync::Mutex::new(None),client_renders_forms: AtomicBool::new(false),});let (client_channel, agent_channel) = AcpChannel::duplex();@@ -915,6 +1030,7 @@ async fn session_new_dials_the_advertised_servers_except_macros_own() {).await.expect("the scripted client should run clean");+ wait_until_asked(&spy).await;agent.abort();assert_eq!(@@ -923,6 +1039,101 @@ async fn session_new_dials_the_advertised_servers_except_macros_own() {);}+/// `session/new` answers while MCP listing is still held, so create is not+/// blocked on `tools/list`.+#[tokio::test]+async fn session_new_does_not_wait_for_mcp_connect() {+ let (connector, mut started, _release) = hold_connector();+ let setup = tokio::spawn(async move {+ serve_with_mcp(+ Arc::new(Arc::clone(&connector)),+ Arc::new(ScriptedEngine::new(Vec::new())) as Arc<dyn TurnEngine>,+ )+ .await+ });+ started.recv().await.expect("connect should start");+ let (_connection, _session, abort) = tokio::time::timeout(Duration::from_millis(200), setup)+ .await+ .expect("session/new must not wait for MCP listing")+ .expect("the handshake task should finish");+ abort.abort();+}++/// The first prompt joins the in-flight connect so SearchTools still sees+/// the catalog, even though `session/new` did not wait.+#[tokio::test]+async fn first_prompt_waits_for_background_mcp_connect() {+ let (connector, mut started, release) = hold_connector();+ let engine = Arc::new(ScriptedEngine::new(Vec::new()));+ let store = Arc::new(SessionStore::new());+ let session_id = AgentSessionId::new();+ store.insert(+ session_id,+ crate::domain::session::SessionState::new("anthropic/claude-sonnet-5-5".into()),+ );+ let state = Arc::new(AgentState {+ session_id,+ owner: model_owner::Owner::User(+ MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),+ ),+ engine: Arc::clone(&engine) as Arc<dyn TurnEngine>,+ admission: Arc::new(DisabledAiAdmissionService),+ store,+ active_cancel: std::sync::Mutex::new(Vec::new()),+ turn_lock: tokio::sync::Mutex::new(()),+ enable_dev_commands: false,+ mcp: Arc::new(Arc::clone(&connector)),+ mcp_tools: std::sync::Mutex::new(None),+ mcp_connect: std::sync::Mutex::new(None),+ client_renders_forms: AtomicBool::new(false),+ });+ let (client_channel, agent_channel) = AcpChannel::duplex();+ let agent = tokio::spawn(serve(state, agent_channel));++ Client+ .builder()+ .connect_with(+ client_channel,+ async move |connection: ConnectionTo<Agent>| {+ connection+ .send_request(InitializeRequest::new(ProtocolVersion::V1))+ .block_task()+ .await?;+ let opened = connection+ .send_request(NewSessionRequest::new("/").mcp_servers(test_mcp_servers()))+ .block_task()+ .await?;+ started.recv().await.expect("connect should start");++ let mut prompt = std::pin::pin!(+ connection+ .send_request(text_prompt(&opened.session_id, "hi"))+ .block_task()+ );+ tokio::select! {+ biased;+ result = &mut prompt => {+ panic!("the prompt finished before MCP listing was released: {result:?}");+ }+ () = tokio::time::sleep(Duration::from_millis(50)) => {}+ }+ assert!(+ engine.requests().is_empty(),+ "the turn must not start until MCP listing finishes"+ );+ release.send(()).expect("release the held connect");+ prompt+ .await+ .expect("the prompt should complete after MCP listing");+ assert_eq!(engine.requests().len(), 1);+ Ok(())+ },+ )+ .await+ .expect("the scripted client should run clean");+ agent.abort();+}+/// Like [`with_agent`], but the client advertises form elicitation and/// answers every `elicitation/create` with `answer`, recording what it was/// asked.@@ -976,6 +1187,7 @@ whereclient_renders_forms: AtomicBool::new(false),mcp: Arc::new(crate::domain::mcp::NoMcpServers),mcp_tools: std::sync::Mutex::new(None),+ mcp_connect: std::sync::Mutex::new(None),enable_dev_commands,});diff --git a/crates/agent_inmem/src/outbound/manager.rs b/crates/agent_inmem/src/outbound/manager.rsindex b35905dfc0..31fb477359 100644--- a/crates/agent_inmem/src/outbound/manager.rs+++ b/crates/agent_inmem/src/outbound/manager.rs@@ -181,6 +181,7 @@ impl InMemAgentManager {turn_lock: tokio::sync::Mutex::new(()),mcp: Arc::clone(&self.mcp),mcp_tools: std::sync::Mutex::new(None),+ mcp_connect: std::sync::Mutex::new(None),client_renders_forms: AtomicBool::new(false),enable_dev_commands: self.enable_dev_commands,});