Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(agents): hand live sessions newly connected MCP servers via session/resume (#7871)

Wolf Mermelstein0335a50ea896
diff --git a/crates/agent_harness/src/domain/model.rs b/crates/agent_harness/src/domain/model.rs
index 8788bae923..57f681af0f 100644
--- a/crates/agent_harness/src/domain/model.rs
+++ b/crates/agent_harness/src/domain/model.rs
@@ -218,6 +218,16 @@ impl AgentKind {
!matches!(self, Self::External)
}
+ /// Whether this kind's agent is handed the session's whole egress server
+ /// list ([`SandboxEgress::acp_servers`]) when it attaches - the list a
+ /// live session is refreshed to when the owner's connections change.
+ /// Codex is handed none, and an external runtime only the internal and
+ /// preview servers.
+ #[must_use]
+ pub fn takes_egress_mcp_servers(self) -> bool {
+ !matches!(self, Self::CodexCloud | Self::External)
+ }
+
/// Whether this kind's sessions work in a repository.
///
/// The runtime's nature, and what a persona is taken to be for until it
@@ -802,6 +812,20 @@ impl SandboxEgress {
format!("Bearer {}", self.session_token)
}
+ /// The session token [`Self::acp_servers`] stamped on `servers`, read
+ /// back off the internal server every rendering of the list carries.
+ pub fn session_token_in(servers: &[AcpMcpServer]) -> Option<String> {
+ servers.iter().find_map(|server| match server {
+ AcpMcpServer::Http(server) if server.name == INTERNAL_MCP_NAME => server
+ .headers
+ .iter()
+ .find(|header| header.name.eq_ignore_ascii_case("authorization"))
+ .and_then(|header| header.value.strip_prefix("Bearer "))
+ .map(str::to_owned),
+ _ => None,
+ })
+ }
+
/// The sandbox environment this becomes.
///
/// Unsized on purpose: a third variable should be one more line here and
diff --git a/crates/agent_harness/src/domain/ports.rs b/crates/agent_harness/src/domain/ports.rs
index a06b4b54a5..b7103a91a5 100644
--- a/crates/agent_harness/src/domain/ports.rs
+++ b/crates/agent_harness/src/domain/ports.rs
@@ -380,7 +380,8 @@ pub trait RuntimeConnections: Send + Sync + 'static {
/// A port rather than domain code because both halves are adapter work the
/// domain has no business knowing: signing a JWT needs a key, and enumerating
/// the owner's MCP servers needs their rows. What the domain keeps is *when* -
-/// at spawn and native turn boundaries, for the session's own owner and policy.
+/// at spawn, at reattach, and before each prompt to a live session, for the
+/// session's own owner and policy.
pub trait SandboxEgressProvisioner: Send + Sync + 'static {
/// Internal session tools at an address reachable by an external runtime.
fn external_mcp_servers(
diff --git a/crates/agent_harness/src/domain/service.rs b/crates/agent_harness/src/domain/service.rs
index 21c1182ce0..65abfcc7af 100644
--- a/crates/agent_harness/src/domain/service.rs
+++ b/crates/agent_harness/src/domain/service.rs
@@ -22,7 +22,7 @@ mod lifecycle;
mod lifecycle_events;
mod open;
mod queue;
-pub mod session_mcp;
+mod session_mcp;
mod warm;
use std::sync::Arc;
diff --git a/crates/agent_harness/src/domain/service/deliver.rs b/crates/agent_harness/src/domain/service/deliver.rs
index 8bc0cb95e0..fb74a0f176 100644
--- a/crates/agent_harness/src/domain/service/deliver.rs
+++ b/crates/agent_harness/src/domain/service/deliver.rs
@@ -61,6 +61,7 @@ where
if action.occupies_turn() {
self.admit_session_id(session_id).await?;
+ self.refresh_mcp_servers(session_id).await;
}
match self
diff --git a/crates/agent_harness/src/domain/service/open.rs b/crates/agent_harness/src/domain/service/open.rs
index 9b90634ce4..3286d1413a 100644
--- a/crates/agent_harness/src/domain/service/open.rs
+++ b/crates/agent_harness/src/domain/service/open.rs
@@ -378,10 +378,10 @@ where
.await?;
self.publish_opened(&session).await;
- let mcp_servers = if runtime.kind == AgentKind::CodexCloud {
- Vec::new()
- } else {
+ let mcp_servers = if runtime.kind.takes_egress_mcp_servers() {
egress.sandbox.acp_servers()
+ } else {
+ Vec::new()
};
let container = match self
.containers
diff --git a/crates/agent_harness/src/domain/service/session_mcp.rs b/crates/agent_harness/src/domain/service/session_mcp.rs
index fd202e42e3..4152e7ce3c 100644
--- a/crates/agent_harness/src/domain/service/session_mcp.rs
+++ b/crates/agent_harness/src/domain/service/session_mcp.rs
@@ -1,128 +1,95 @@
-//! Resolve current connections without changing a session's selected-app policy.
-use crate::domain::{model::SandboxEgress, ports::SandboxEgressProvisioner};
-use agent_egress::domain::model::SessionToken;
-use agent_session::domain::{model::AgentSessionId, ports::AgentSessionRepo};
+//! Keeping a live session's MCP servers in step with what its owner has
+//! connected: an app connected in settings mid-session reaches the agent on
+//! its next prompt, without a new session.
-/// Authenticate the existing session credential and rebuild its permitted list.
-#[tracing::instrument(skip_all, fields(session_id = %id), err)]
-pub async fn refresh<R: AgentSessionRepo, P: SandboxEgressProvisioner>(
- sessions: &R,
- provisioner: &P,
- id: AgentSessionId,
- token: SessionToken,
-) -> anyhow::Result<SandboxEgress> {
- let session = sessions
- .find_by_egress_token_hash(&token.hash())
- .await?
- .ok_or_else(|| anyhow::anyhow!("Unknown session credential"))?;
- anyhow::ensure!(
- session.id == id && !session.is_archived,
- "Session is not available for connector refresh"
- );
- Ok(provisioner
- .restore(
- session.owner_user()?,
- token.as_str().to_owned(),
- &session.mcp_servers,
- )
- .await?)
-}
-
-#[cfg(test)]
-mod test {
- use super::*;
- use crate::domain::{error::Result, model::ProvisionedEgress};
- use agent_session::{
- domain::model::AgentMcpServers,
- testing::{InMemoryAgentSessionRepo, test_agent_session},
- };
- use macro_user_id::user_id::MacroUserIdStr;
- use std::sync::Mutex;
+use super::*;
+use crate::domain::model::SandboxEgress;
- #[derive(Default)]
- struct RecordingProvisioner(Mutex<Vec<(String, AgentMcpServers)>>);
- impl SandboxEgressProvisioner for RecordingProvisioner {
- async fn provision(
- &self,
- _: AgentSessionId,
- _: &MacroUserIdStr<'static>,
- _: &AgentMcpServers,
- ) -> Result<ProvisionedEgress> {
- panic!("refresh must not mint credentials")
- }
- async fn restore(
- &self,
- owner: &MacroUserIdStr<'static>,
- token: String,
- selection: &AgentMcpServers,
- ) -> Result<SandboxEgress> {
- self.0
- .lock()
- .unwrap()
- .push((owner.to_string(), selection.clone()));
- Ok(SandboxEgress {
- session_token: token,
- ..crate::testing::helpers::egress::test_egress()
- })
+impl<
+ Sessions,
+ Containers,
+ Announcer,
+ Runtimes,
+ PromptContext,
+ PromptComposer,
+ Egress,
+ Lifecycle,
+ Mentions,
+ Notifier,
+>
+ AgentHarnessInner<
+ Sessions,
+ Containers,
+ Announcer,
+ Runtimes,
+ PromptContext,
+ PromptComposer,
+ Egress,
+ Lifecycle,
+ Mentions,
+ Notifier,
+ >
+where
+ Sessions: AgentSessionService,
+ Containers: ContainerManager,
+ Announcer: SessionAnnouncer,
+ Runtimes: RuntimeConnections,
+ PromptContext: MessagePromptContext,
+ PromptComposer: AgentPromptComposer,
+ Egress: SandboxEgressProvisioner,
+ Lifecycle: AgentSessionLifecyclePublisher,
+ Mentions: PromptMentions,
+ Notifier: AgentSessionNotifier,
+{
+ /// Hand the session's live agent its server list as it stands now, if
+ /// that differs from the list last requested for it. A list the agent
+ /// could not take is not requested again until it changes.
+ ///
+ /// Called before a prompt is delivered, so the prompt runs with whatever
+ /// the owner connected since the session attached. The list is rebuilt
+ /// around the session's existing token, under the session's own app
+ /// selection - the same listing a reattach does. A session not attached
+ /// here needs nothing: delivery attaches it with a fresh list.
+ ///
+ /// A failure leaves the agent on the servers it has and the prompt goes
+ /// out regardless; the next prompt tries again.
+ pub(super) async fn refresh_mcp_servers(&self, session_id: AgentSessionId) {
+ if let Err(error) = self.try_refresh_mcp_servers(session_id).await {
+ tracing::warn!(
+ error = ?error,
+ %session_id,
+ "could not refresh a session's MCP servers; the prompt runs with the ones it has"
+ );
}
}
- #[tokio::test]
- async fn refresh_preserves_owner_selection_and_credential() {
- for selection in [
- AgentMcpServers::OwnerConnections,
- AgentMcpServers::Selected { servers: vec![] },
- ] {
- let repo = InMemoryAgentSessionRepo::new();
- let mut session = test_agent_session(AgentSessionId::new());
- session.mcp_servers = selection.clone();
- let id = session.id;
- let owner = session.owner_user().unwrap().to_string();
- repo.insert_session(session);
- repo.set_egress_token_hash(id, &SessionToken::new("credential").hash())
- .await
- .unwrap();
- let provisioner = RecordingProvisioner::default();
- let result = refresh(&repo, &provisioner, id, SessionToken::new("credential"))
- .await
- .unwrap();
- assert_eq!(result.session_token, "credential");
- assert_eq!(*provisioner.0.lock().unwrap(), vec![(owner, selection)]);
+ async fn try_refresh_mcp_servers(&self, session_id: AgentSessionId) -> Result<()> {
+ let Some(attached) = self.sessions.attached_mcp_servers(session_id) else {
+ return Ok(());
+ };
+ let session = self.sessions.get_session(session_id).await?;
+ if !AgentKind::for_session(session.bot_id, &session.harness).takes_egress_mcp_servers() {
+ return Ok(());
}
- }
-
- #[tokio::test]
- async fn unknown_mismatched_and_archived_sessions_cannot_refresh() {
- let repo = InMemoryAgentSessionRepo::new();
- let mut session = test_agent_session(AgentSessionId::new());
- let id = session.id;
- repo.insert_session(session.clone());
- repo.set_egress_token_hash(id, &SessionToken::new("credential").hash())
- .await
- .unwrap();
- let provisioner = RecordingProvisioner::default();
- assert!(
- refresh(&repo, &provisioner, id, SessionToken::new("unknown"))
- .await
- .is_err()
- );
- assert!(
- refresh(
- &repo,
- &provisioner,
- AgentSessionId::new(),
- SessionToken::new("credential")
- )
- .await
- .is_err()
- );
- session.is_archived = true;
- repo.insert_session(session);
- assert!(
- refresh(&repo, &provisioner, id, SessionToken::new("credential"))
- .await
- .is_err()
+ let Some(token) = SandboxEgress::session_token_in(&attached) else {
+ return Ok(());
+ };
+ let current = self
+ .egress
+ .restore(session.owner_user()?, token, &session.mcp_servers)
+ .await?
+ .acp_servers();
+ if current == attached {
+ return Ok(());
+ }
+ tracing::info!(
+ %session_id,
+ servers = current.len(),
+ "handing a live session its owner's current MCP servers"
);
- assert!(provisioner.0.lock().unwrap().is_empty());
+ self.sessions
+ .replace_mcp_servers(session_id, current)
+ .await?;
+ Ok(())
}
}
diff --git a/crates/agent_harness/src/domain/service/test.rs b/crates/agent_harness/src/domain/service/test.rs
index fab75831e2..5bd39ca10c 100644
--- a/crates/agent_harness/src/domain/service/test.rs
+++ b/crates/agent_harness/src/domain/service/test.rs
@@ -450,6 +450,19 @@ fn harness_with_policies_and_mentions(
permission_policies,
HarnessDefaultCodingAgents,
mentions,
+ EgressProvisionerMock::new(),
+ )
+}
+
+/// A harness whose egress advertises whatever the test connects on `egress`.
+fn harness_with_egress(egress: EgressProvisionerMock) -> (TestBench, TurnSignals) {
+ harness_with_ports(
+ PromptContextMock::default(),
+ PromptComposerMock::default(),
+ KindDefaultPolicies,
+ HarnessDefaultCodingAgents,
+ PromptMentionsMock::new(),
+ egress,
)
}
@@ -460,6 +473,7 @@ fn harness_with_coding_choice(chosen: bool) -> (TestBench, TurnSignals) {
KindDefaultPolicies,
ChosenCodingAgents(chosen),
PromptMentionsMock::new(),
+ EgressProvisionerMock::new(),
)
}
@@ -469,6 +483,7 @@ fn harness_with_ports(
permission_policies: impl crate::domain::ports::PermissionPolicySource,
coding_agents: impl crate::domain::ports::CodingAgentSource,
mentions: PromptMentionsMock,
+ egress: EgressProvisionerMock,
) -> (TestBench, TurnSignals) {
let repo = InMemoryAgentSessionRepo::new();
let containers = MockContainerManager::new();
@@ -496,7 +511,7 @@ fn harness_with_ports(
TestConnections::new(MirrorBindings, Arc::clone(&runtimes)),
prompt_context,
prompt_composer,
- EgressProvisionerMock::new(),
+ egress,
NoPeers,
permission_policies,
coding_agents,
@@ -1697,6 +1712,101 @@ async fn live_sandboxed_coder_session(
container
}
+/// An app the owner connects while a session is live reaches its agent before
+/// the next prompt does: the live ACP session is resumed with the new list.
+#[tokio::test]
+async fn an_app_connected_mid_session_reaches_the_agent_before_the_next_prompt() {
+ let egress = EgressProvisionerMock::new();
+ let ((service, _repo, containers, _announcer, _runtimes), mut turns) =
+ harness_with_egress(egress.clone());
+ let id = AgentSessionId::new();
+ let mut command = open_command();
+ command.bot_id = bot_id::MACRO_CODER_BOT_ID;
+ mention_origin_mut(&mut command).sender = staff_sender();
+ let open = service.execute(id, HarnessCommand::Open(command));
+ let drive = async {
+ let container = containers
+ .container(containers.first_spawned().await)
+ .expect("the spawned container is findable");
+ let agent = container.agent();
+ container.sends_ready();
+ agent.wait_for_requests(1).await;
+ agent.completes_initialize(
+ InitializeResponse::new(PROTOCOL_VERSION).agent_capabilities(
+ AgentCapabilities::new().session_capabilities(
+ SessionCapabilities::new().resume(SessionResumeCapabilities::new()),
+ ),
+ ),
+ );
+ agent.wait_for_requests(2).await;
+ agent.opens_session(NewSessionResponse::new("acp-test"));
+ agent.completes_prompt().await;
+ container
+ };
+ let (opened, container) = tokio::join!(open, drive);
+ opened.expect("sandboxed coder session should open");
+ turns.settled(id).await;
+ let agent = container.agent();
+ let ClientRequest::NewSessionRequest(opened) = &agent.received_requests()[1] else {
+ panic!("expected session/new");
+ };
+ let names = |servers: &[agent_client_protocol::schema::v1::McpServer]| -> Vec<String> {
+ servers
+ .iter()
+ .map(|server| match server {
+ agent_client_protocol::schema::v1::McpServer::Http(http) => http.name.clone(),
+ other => panic!("expected HTTP MCP, got {other:?}"),
+ })
+ .collect()
+ };
+ assert_eq!(
+ names(&opened.mcp_servers),
+ ["macro", "macro_internal", "macro-preview"]
+ );
+
+ egress.connect("linear");
+ let prompted = service.execute(
+ id,
+ HarnessCommand::Deliver(forward_message("file it in linear")),
+ );
+ let resume = async {
+ agent.wait_for_requests(4).await;
+ let ClientRequest::ResumeSessionRequest(resumed) = &agent.received_requests()[3] else {
+ panic!("expected session/resume before the prompt");
+ };
+ assert_eq!(resumed.session_id.to_string(), "acp-test");
+ assert_eq!(
+ names(&resumed.mcp_servers),
+ ["macro", "macro_internal", "macro-preview", "linear"]
+ );
+ agent.resumes_session(ResumeSessionResponse::new());
+ agent.completes_prompt().await;
+ };
+ let (prompted, ()) = tokio::join!(prompted, resume);
+ prompted.expect("the prompt reaches the resumed session");
+ turns.settled(id).await;
+ assert_eq!(
+ prompts(&agent)[1],
+ vec![ContentBlock::from(context_prompt("file it in linear"))]
+ );
+
+ // Nothing changed since: the next prompt goes straight out.
+ let prompted = service.execute(
+ id,
+ HarnessCommand::Deliver(forward_message("and assign it to me")),
+ );
+ let answered = async {
+ agent.wait_for_requests(6).await;
+ agent.completes_prompt().await;
+ };
+ let (prompted, ()) = tokio::join!(prompted, answered);
+ prompted.expect("the prompt reaches the session");
+ assert!(matches!(
+ &agent.received_requests()[5],
+ ClientRequest::PromptRequest(_)
+ ));
+}
+
#[tokio::test]
async fn changing_the_model_persists_it_and_tells_the_running_agent() {
let (service, repo, containers, _announcer, _runtimes) = harness();
diff --git a/crates/agent_harness/src/domain/service/warm.rs b/crates/agent_harness/src/domain/service/warm.rs
index 5caa581bfa..40b3483b8e 100644
--- a/crates/agent_harness/src/domain/service/warm.rs
+++ b/crates/agent_harness/src/domain/service/warm.rs
@@ -268,10 +268,10 @@ where
.await;
}
- let mcp_servers = if kind == AgentKind::CodexCloud {
- Vec::new()
- } else {
+ let mcp_servers = if kind.takes_egress_mcp_servers() {
egress.sandbox.acp_servers()
+ } else {
+ Vec::new()
};
let container = match self
.inner
diff --git a/crates/agent_harness/src/testing/helpers/agent.rs b/crates/agent_harness/src/testing/helpers/agent.rs
index 29c9cbe2b7..20de05d6e7 100644
--- a/crates/agent_harness/src/testing/helpers/agent.rs
+++ b/crates/agent_harness/src/testing/helpers/agent.rs
@@ -291,6 +291,13 @@ impl FakeAgent {
ClientRequest::InitializeRequest(_) => {
progress.initializing = Some(request.id.clone());
}
+ // Resuming the open session again is how a live agent
+ // is handed new MCP servers.
+ ClientRequest::ResumeSessionRequest(_)
+ if progress.stage == Stage::SessionOpen =>
+ {
+ progress.opening = Some(request.id.clone());
+ }
ClientRequest::NewSessionRequest(_)
| ClientRequest::LoadSessionRequest(_)
| ClientRequest::ResumeSessionRequest(_) => {
diff --git a/crates/agent_harness/src/testing/helpers/egress.rs b/crates/agent_harness/src/testing/helpers/egress.rs
index 0bc3517921..d49cfffc29 100644
--- a/crates/agent_harness/src/testing/helpers/egress.rs
+++ b/crates/agent_harness/src/testing/helpers/egress.rs
@@ -2,6 +2,7 @@
use std::sync::{Arc, Mutex};
+use agent_egress::domain::model::McpServerSlug;
use agent_session::domain::model::{AgentMcpServers, AgentSessionId};
use macro_user_id::user_id::MacroUserIdStr;
@@ -18,6 +19,8 @@ pub type RecordedProvisioning = (AgentSessionId, String, AgentMcpServers);
#[derive(Clone, Default)]
pub struct EgressProvisionerMock {
provisioned: Arc<Mutex<Vec<RecordedProvisioning>>>,
+ /// The apps every environment advertises, as the owner's connections.
+ connected: Arc<Mutex<Vec<McpServerSlug>>>,
}
impl EgressProvisionerMock {
@@ -36,6 +39,27 @@ impl EgressProvisionerMock {
.expect("egress mock lock should not be poisoned")
.clone()
}
+
+ /// Advertise `app` in every environment from now on, as an owner who has
+ /// just connected it would see.
+ pub fn connect(&self, app: &str) {
+ self.connected
+ .lock()
+ .expect("egress mock lock should not be poisoned")
+ .push(McpServerSlug::parse(app).expect("a valid app slug"));
+ }
+
+ fn egress(&self, session_token: String) -> SandboxEgress {
+ SandboxEgress {
+ session_token,
+ mcp_servers: self
+ .connected
+ .lock()
+ .expect("egress mock lock should not be poisoned")
+ .clone(),
+ ..test_egress()
+ }
+ }
}
impl SandboxEgressProvisioner for EgressProvisionerMock {
@@ -51,7 +75,7 @@ impl SandboxEgressProvisioner for EgressProvisionerMock {
.push((session, owner.to_string(), selection.clone()));
Ok(ProvisionedEgress {
- sandbox: test_egress(),
+ sandbox: self.egress(test_egress().session_token),
session_token_hash: "test-token-hash".to_owned(),
})
}
@@ -62,10 +86,7 @@ impl SandboxEgressProvisioner for EgressProvisionerMock {
session_token: String,
_selection: &AgentMcpServers,
) -> Result<SandboxEgress> {
- Ok(SandboxEgress {
- session_token,
- ..test_egress()
- })
+ Ok(self.egress(session_token))
}
}
diff --git a/crates/agent_inmem/src/domain/agent.rs b/crates/agent_inmem/src/domain/agent.rs
index 36e3efcf20..d7439147c0 100644
--- a/crates/agent_inmem/src/domain/agent.rs
+++ b/crates/agent_inmem/src/domain/agent.rs
@@ -140,8 +140,6 @@ pub struct AgentState {
/// The tools of those servers, once dialed; `None` until then or when
/// there were none.
pub mcp_tools: Mutex<Option<RemoteMcpToolSet>>,
- /// Attached entries retain the credential used to resolve live connections.
- pub mcp_servers: Mutex<Vec<AcpMcpServer>>,
/// 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.
@@ -156,9 +154,10 @@ pub struct AgentState {
impl AgentState {
/// Dial the servers a session request carried and keep their tools for
- /// the next turn. 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.
+ /// every turn until another `session/resume` hands over a new list.
+ /// 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();
@@ -179,23 +178,25 @@ impl AgentState {
/// 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>) {
- *self
- .mcp_servers
- .lock()
- .expect("mcp servers lock should not be poisoned") = servers.clone();
let state = Arc::clone(self);
let handle = tokio::spawn(async move {
state.connect_mcp(servers).await;
});
- *self
+ // A resume that hands over new servers supersedes a connect still in
+ // flight; left running, the older list could land last.
+ if let Some(superseded) = self
.mcp_connect
.lock()
- .expect("mcp connect lock should not be poisoned") = Some(handle);
+ .expect("mcp connect lock should not be poisoned")
+ .replace(handle)
+ {
+ superseded.abort();
+ }
}
- /// Wait for the initial listing, then refresh permitted connections so
- /// authorization or disconnection takes effect without a new session.
- async fn mcp_tools_for_turn(&self) -> anyhow::Result<Option<RemoteMcpToolSet>> {
+ /// 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()
@@ -204,19 +205,10 @@ impl AgentState {
if let Some(handle) = handle {
let _ = handle.await;
}
- let advertised = self
- .mcp_servers
- .lock()
- .expect("mcp servers lock should not be poisoned")
- .clone();
- if let Some(servers) = self.mcp.refresh_dyn(self.session_id, advertised).await? {
- self.connect_mcp(servers).await;
- }
- Ok(self
- .mcp_tools
+ self.mcp_tools
.lock()
.expect("mcp tools lock should not be poisoned")
- .clone())
+ .clone()
}
fn expect_session(&self, requested: &SessionId) -> Result<(), AcpError> {
@@ -892,7 +884,7 @@ async fn run_turn(
let mcp_tools = tokio::select! {
biased;
_ = cancel.cancelled() => return Ok(StopReason::Cancelled),
- tools = state.mcp_tools_for_turn() => tools.map_err(|error| AcpError::new(-32603, format!("Could not refresh connector tools: {error}")))?,
+ tools = state.mcp_tools_for_turn() => tools,
};
let TurnInput {
messages,
diff --git a/crates/agent_inmem/src/domain/agent/test.rs b/crates/agent_inmem/src/domain/agent/test.rs
index 496a2c769c..39e5bad133 100644
--- a/crates/agent_inmem/src/domain/agent/test.rs
+++ b/crates/agent_inmem/src/domain/agent/test.rs
@@ -145,7 +145,6 @@ async fn record<Engine: TurnEngine, Out>(
turn_lock: tokio::sync::Mutex::new(()),
mcp: Arc::new(crate::domain::mcp::NoMcpServers),
mcp_tools: std::sync::Mutex::new(None),
- mcp_servers: std::sync::Mutex::new(Vec::new()),
mcp_connect: std::sync::Mutex::new(None),
client_renders_forms: AtomicBool::new(false),
enable_dev_commands: true,
@@ -1495,7 +1494,6 @@ async fn serve_with_mcp(
enable_dev_commands: false,
mcp,
mcp_tools: std::sync::Mutex::new(None),
- mcp_servers: std::sync::Mutex::new(Vec::new()),
mcp_connect: std::sync::Mutex::new(None),
client_renders_forms: AtomicBool::new(false),
});
@@ -1523,83 +1521,6 @@ async fn serve_with_mcp(
(connection, session, abort)
}
-#[derive(Default)]
-struct RefreshingConnector {
- connected: AtomicBool,
- fail: AtomicBool,
- asked: std::sync::Mutex<Vec<Vec<String>>>,
-}
-
-impl crate::domain::mcp::McpToolConnector for Arc<RefreshingConnector> {
- async fn refresh(
- &self,
- _: AgentSessionId,
- advertised: Vec<AcpMcpServer>,
- ) -> anyhow::Result<Option<Vec<AcpMcpServer>>> {
- anyhow::ensure!(!self.fail.load(Ordering::SeqCst), "refresh failed");
- assert!(
- !advertised.is_empty(),
- "retain the attached session credential"
- );
- let servers = if self.connected.load(Ordering::SeqCst) {
- vec![AcpMcpServer::Http(
- agent_client_protocol::schema::v1::McpServerHttp::new(
- "notion",
- "https://egress.test/mcp/notion",
- ),
- )]
- } else {
- vec![]
- };
- Ok(Some(servers))
- }
- async fn connect(
- &self,
- servers: Vec<agent_client_protocol::schema::v1::McpServerHttp>,
- ) -> Option<RemoteMcpToolSet> {
- self.asked
- .lock()
- .unwrap()
- .push(servers.into_iter().map(|server| server.name).collect());
- None
- }
-}
-
-#[tokio::test]
-async fn each_turn_refreshes_newly_connected_apps_without_recreating_the_session() {
- let connector = Arc::new(RefreshingConnector::default());
- let state = Arc::new(AgentState {
- session_id: AgentSessionId::new(),
- owner: model_owner::Owner::User(MacroUserIdStr::try_from_email("[email protected]").unwrap()),
- engine: Arc::new(ScriptedEngine::new(vec![])),
- admission: Arc::new(DisabledAiAdmissionService),
- model_access: Arc::new(crate::testing::TestModelAccess::paid()),
- store: Arc::new(SessionStore::new()),
- active_cancel: std::sync::Mutex::new(Vec::new()),
- turn_lock: tokio::sync::Mutex::new(()),
- enable_dev_commands: false,
- mcp: Arc::new(connector.clone()),
- mcp_tools: std::sync::Mutex::new(None),
- mcp_servers: std::sync::Mutex::new(Vec::new()),
- mcp_connect: std::sync::Mutex::new(None),
- client_renders_forms: AtomicBool::new(false),
- });
- state.start_connect_mcp(test_mcp_servers());
- state.mcp_tools_for_turn().await.unwrap();
- assert!(connector.asked.lock().unwrap().last().unwrap().is_empty());
- connector.connected.store(true, Ordering::SeqCst);
- state.mcp_tools_for_turn().await.unwrap();
- assert_eq!(
- connector.asked.lock().unwrap().last().unwrap(),
- &vec!["notion".to_string()]
- );
- connector.connected.store(false, Ordering::SeqCst);
- state.mcp_tools_for_turn().await.unwrap();
- assert!(connector.asked.lock().unwrap().last().unwrap().is_empty());
- connector.fail.store(true, Ordering::SeqCst);
- assert!(state.mcp_tools_for_turn().await.is_err());
-}
-
/// The servers `session/new` carries are dialed in the background, minus
/// Macro's own, whose tools this runtime already has natively.
#[tokio::test]
@@ -1629,7 +1550,6 @@ 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_servers: std::sync::Mutex::new(Vec::new()),
mcp_connect: std::sync::Mutex::new(None),
client_renders_forms: AtomicBool::new(false),
});
@@ -1719,7 +1639,6 @@ async fn first_prompt_waits_for_background_mcp_connect() {
enable_dev_commands: false,
mcp: Arc::new(Arc::clone(&connector)),
mcp_tools: std::sync::Mutex::new(None),
- mcp_servers: std::sync::Mutex::new(Vec::new()),
mcp_connect: std::sync::Mutex::new(None),
client_renders_forms: AtomicBool::new(false),
});
@@ -1824,7 +1743,6 @@ where
client_renders_forms: AtomicBool::new(false),
mcp: Arc::new(crate::domain::mcp::NoMcpServers),
mcp_tools: std::sync::Mutex::new(None),
- mcp_servers: std::sync::Mutex::new(Vec::new()),
mcp_connect: std::sync::Mutex::new(None),
enable_dev_commands,
});
diff --git a/crates/agent_inmem/src/domain/mcp.rs b/crates/agent_inmem/src/domain/mcp.rs
index be3cf6f61b..a37f732ffd 100644
--- a/crates/agent_inmem/src/domain/mcp.rs
+++ b/crates/agent_inmem/src/domain/mcp.rs
@@ -9,21 +9,8 @@
//! app they have not connected with a result the model can act on.
use agent_client_protocol::schema::v1::{McpServer as AcpMcpServer, McpServerHttp};
-use agent_session::domain::model::AgentSessionId;
-use futures::future::BoxFuture;
use mcp_toolset::RemoteMcpToolSet;
-/// Resolves the session's current permitted servers before a new turn.
-pub trait SessionMcpSource: Send + Sync + 'static {
- /// The advertised list carries the existing session credential. Implementations
- /// must resolve its owner and policy before adding any connected app.
- fn servers<'a>(
- &'a self,
- session: AgentSessionId,
- advertised: Vec<AcpMcpServer>,
- ) -> std::pin::Pin<Box<dyn Future<Output = anyhow::Result<Vec<AcpMcpServer>>> + Send + 'a>>;
-}
-
#[cfg(test)]
mod test;
@@ -62,14 +49,6 @@ pub fn dialable_servers(servers: Vec<AcpMcpServer>) -> Vec<McpServerHttp> {
/// A port because it is transport work: the production adapter speaks
/// streamable HTTP through the egress proxy, tests hand back nothing.
pub trait McpToolConnector: Send + Sync + 'static {
- /// Refresh supported runtimes; other connectors retain their attached list.
- fn refresh(
- &self,
- _session: AgentSessionId,
- _advertised: Vec<AcpMcpServer>,
- ) -> impl Future<Output = anyhow::Result<Option<Vec<AcpMcpServer>>>> + Send {
- async { Ok(None) }
- }
/// Connect to every server, skipping any that fail, and return the tools
/// found. `None` when no server yielded any tool.
///
@@ -91,12 +70,6 @@ pub trait McpToolConnector: Send + Sync + 'static {
/// Erased form of [`McpToolConnector`] for storage on the agent state.
pub trait DynMcpToolConnector: Send + Sync + 'static {
- /// See [`McpToolConnector::refresh`].
- fn refresh_dyn(
- &self,
- session: AgentSessionId,
- advertised: Vec<AcpMcpServer>,
- ) -> BoxFuture<'_, anyhow::Result<Option<Vec<AcpMcpServer>>>>;
/// See [`McpToolConnector::connect`].
fn connect_dyn(
&self,
@@ -108,13 +81,6 @@ pub trait DynMcpToolConnector: Send + Sync + 'static {
}
impl<C: McpToolConnector> DynMcpToolConnector for C {
- fn refresh_dyn(
- &self,
- session: AgentSessionId,
- advertised: Vec<AcpMcpServer>,
- ) -> BoxFuture<'_, anyhow::Result<Option<Vec<AcpMcpServer>>>> {
- Box::pin(self.refresh(session, advertised))
- }
fn connect_dyn(
&self,
servers: Vec<McpServerHttp>,
diff --git a/crates/agent_inmem/src/outbound/acp_mcp.rs b/crates/agent_inmem/src/outbound/acp_mcp.rs
index 3f35f17bf9..1fffcb1d4e 100644
--- a/crates/agent_inmem/src/outbound/acp_mcp.rs
+++ b/crates/agent_inmem/src/outbound/acp_mcp.rs
@@ -70,7 +70,6 @@ fn place_header(name: &str, value: &str) -> Option<HeaderPlacement> {
pub struct AcpMcpConnector<Client> {
client: Client,
pool: Arc<ServerPool>,
- source: Option<Arc<dyn crate::domain::mcp::SessionMcpSource>>,
}
impl<Client> AcpMcpConnector<Client>
@@ -93,17 +92,7 @@ where
}
});
}
- Self {
- client,
- pool,
- source: None,
- }
- }
-
- /// Refresh the session's permitted app list before each native turn.
- pub fn with_source(mut self, source: Arc<dyn crate::domain::mcp::SessionMcpSource>) -> Self {
- self.source = Some(source);
- self
+ Self { client, pool }
}
/// The pooled session for `server`, dialing it only when this connector
@@ -188,16 +177,6 @@ impl<Client> McpToolConnector for AcpMcpConnector<Client>
where
Client: StreamableHttpClient + Send + Sync,
{
- async fn refresh(
- &self,
- session: agent_session::domain::model::AgentSessionId,
- advertised: Vec<agent_client_protocol::schema::v1::McpServer>,
- ) -> anyhow::Result<Option<Vec<agent_client_protocol::schema::v1::McpServer>>> {
- match &self.source {
- Some(source) => source.servers(session, advertised).await.map(Some),
- None => Ok(None),
- }
- }
#[tracing::instrument(
skip_all,
fields(
diff --git a/crates/agent_inmem/src/outbound/manager.rs b/crates/agent_inmem/src/outbound/manager.rs
index dc817cdcc8..f6769c3db1 100644
--- a/crates/agent_inmem/src/outbound/manager.rs
+++ b/crates/agent_inmem/src/outbound/manager.rs
@@ -193,7 +193,6 @@ impl InMemAgentManager {
turn_lock: tokio::sync::Mutex::new(()),
mcp: Arc::clone(&self.mcp),
mcp_tools: std::sync::Mutex::new(None),
- mcp_servers: std::sync::Mutex::new(Vec::new()),
mcp_connect: std::sync::Mutex::new(None),
client_renders_forms: AtomicBool::new(false),
enable_dev_commands: self.enable_dev_commands,
diff --git a/crates/agent_session/src/domain/model.rs b/crates/agent_session/src/domain/model.rs
index b41d7b78ef..7f144f8a16 100644
--- a/crates/agent_session/src/domain/model.rs
+++ b/crates/agent_session/src/domain/model.rs
@@ -178,10 +178,10 @@ pub enum SessionStatus {
}
/// Which Pipedream MCP servers a session is handed: the agent's own choice,
-/// snapshotted onto the session at creation like `instructions`. The native
-/// runtime refreshes connections within this policy before each turn. Other
-/// ACP runtimes receive the list on attach; editing the agent's selection
-/// applies to its next session.
+/// snapshotted onto the session at creation like `instructions`. The servers
+/// it resolves to are re-listed before each prompt, so an app the owner
+/// connects mid-session reaches a live agent that can `session/resume`;
+/// editing the agent's selection applies to its next session.
pub use bots::domain::models::{AgentMcpServer, AgentMcpServers};
/// Caller-provided values required to create an agent session.
diff --git a/crates/agent_session/src/domain/service.rs b/crates/agent_session/src/domain/service.rs
index 511df2ff37..cc98d04e2a 100644
--- a/crates/agent_session/src/domain/service.rs
+++ b/crates/agent_session/src/domain/service.rs
@@ -33,7 +33,7 @@ use std::sync::Arc;
use agent_client_protocol::RawJsonRpcMessage;
use agent_client_protocol::schema::v1::{
- RequestId, Response, SessionId, SetSessionConfigOptionResponse,
+ McpServer, RequestId, Response, SessionId, SetSessionConfigOptionResponse,
};
use agent_fold::domain::lifecycle::LifecycleFold;
use agent_fold::domain::model::{Author, FoldedMessage, MessagePart, TurnState};
@@ -73,7 +73,7 @@ use super::ports::{
Appended, NoInheritedSessionAccess, NoOpAgentSessionNameGenerator, NoOpToolCatalog,
SessionOwnership, SessionToolCatalog, SessionTurnObserver, SessionViewAccess,
};
-use super::session::actors::{SessionActor, SessionCommand, Stepped};
+use super::session::actors::{SessionActor, SessionCommand, SessionMessage, Stepped};
use super::session::{CloseReason, Input};
use crate::domain::events::{AgentSessionLifecycleEvent, SessionRenamedMetadata};
@@ -96,7 +96,11 @@ const HANDSHAKE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30
const HANDSHAKE_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(50);
struct ActiveSession {
- commands: Option<mpsc::Sender<SessionCommand>>,
+ commands: Option<mpsc::Sender<SessionMessage>>,
+ /// The MCP servers last requested for the live connection: the
+ /// attachment's, until [`AgentSessionService::replace_mcp_servers`]. An
+ /// agent that cannot resume, or refused, still has its previous list.
+ mcp_servers: Vec<McpServer>,
stopped: watch::Receiver<bool>,
marker: Arc<()>,
deleting: bool,
@@ -272,6 +276,22 @@ pub trait AgentSessionService: Send + Sync + 'static {
action_id: AgentActionId,
) -> impl Future<Output = Result<()>> + Send;
+ /// The MCP servers last requested for the session's live connection on
+ /// this instance; `None` when no connection is attached here. An agent
+ /// that could not take a requested list keeps its previous one until its
+ /// next attach.
+ fn attached_mcp_servers(&self, id: AgentSessionId) -> Option<Vec<McpServer>>;
+
+ /// Hand the session's agent `servers` from now on. The live connection
+ /// re-resumes its ACP session with them before the next prompt reaches
+ /// the agent, if the agent can resume; an agent that cannot keeps the
+ /// servers it has until its next attach.
+ fn replace_mcp_servers(
+ &self,
+ id: AgentSessionId,
+ servers: Vec<McpServer>,
+ ) -> impl Future<Output = Result<()>> + Send;
+
/// The session an incoming channel context routes to, if any.
fn find_for_thread(
&self,
@@ -495,6 +515,7 @@ impl<R, Folds, Rt, Namer> AgentSessionServiceImpl<R, Folds, Rt, Namer> {
Entry::Vacant(entry) => {
entry.insert(ActiveSession {
commands: None,
+ mcp_servers: Vec::new(),
stopped,
marker: marker.clone(),
deleting: false,
@@ -540,6 +561,7 @@ impl<R, Folds, Rt, Namer> AgentSessionServiceImpl<R, Folds, Rt, Namer> {
activate(claim)?;
}
active.commands = Some(commands.clone());
+ active.mcp_servers = attachment.mcp_servers.clone();
active.transport_closed = attachment.closed.clone();
drop(active);
let (marker, stopped_tx) = reservation.commit();
@@ -611,14 +633,14 @@ impl<R, Folds, Rt, Namer> AgentSessionServiceImpl<R, Folds, Rt, Namer> {
otel.status_description = tracing::field::Empty,
);
if commands
- .send(SessionCommand {
+ .send(SessionMessage::Action(SessionCommand {
user_id,
action,
action_id,
completed,
span,
enqueued_at: tokio::time::Instant::now(),
- })
+ }))
.await
.is_err()
{
@@ -699,6 +721,7 @@ impl<R, Folds, Rt, Namer> AgentSessionServiceImpl<R, Folds, Rt, Namer> {
let marker = Arc::new(());
entry.insert(ActiveSession {
commands: None,
+ mcp_servers: Vec::new(),
stopped: stopped.clone(),
marker: marker.clone(),
deleting,
@@ -1025,6 +1048,32 @@ where
Ok(())
}
+ fn attached_mcp_servers(&self, id: AgentSessionId) -> Option<Vec<McpServer>> {
+ self.active
+ .get(&id)
+ .filter(|active| active.commands.is_some())
+ .map(|active| active.mcp_servers.clone())
+ }
+
+ async fn replace_mcp_servers(&self, id: AgentSessionId, servers: Vec<McpServer>) -> Result<()> {
+ let commands = {
+ let mut active = self
+ .active
+ .get_mut(&id)
+ .ok_or(AgentSessionError::Disconnected(id))?;
+ let commands = active
+ .commands
+ .clone()
+ .ok_or(AgentSessionError::Disconnected(id))?;
+ active.mcp_servers = servers.clone();
+ commands
+ };
+ commands
+ .send(SessionMessage::ReplaceMcpServers(servers))
+ .await
+ .map_err(|_| AgentSessionError::Disconnected(id))
+ }
+
async fn session_bot(&self, id: BotId) -> Result<SessionBot> {
self.repo.session_bot(id).await
}
diff --git a/crates/agent_session/src/domain/service/test.rs b/crates/agent_session/src/domain/service/test.rs
index 50996a4729..1aea18bac0 100644
--- a/crates/agent_session/src/domain/service/test.rs
+++ b/crates/agent_session/src/domain/service/test.rs
@@ -1179,14 +1179,14 @@ async fn cancellation_does_not_drop_an_effect_batch_after_machine_mutation() {
let (completed, result) = oneshot::channel();
commands
- .send(SessionCommand {
+ .send(SessionMessage::Action(SessionCommand {
user_id: None,
action: AgentAction::prompt("keep dispatching"),
action_id: AgentActionId::mint(),
completed,
span: tracing::info_span!("test.command"),
enqueued_at: tokio::time::Instant::now(),
- })
+ }))
.await
.unwrap();
entered.notified().await;
@@ -1263,14 +1263,14 @@ async fn live_inbound_logs_do_not_reuse_the_expired_handshake_deadline() {
release.notify_one();
let (completed, result) = oneshot::channel();
commands
- .send(SessionCommand {
+ .send(SessionMessage::Action(SessionCommand {
user_id: None,
action: AgentAction::prompt("keep working"),
action_id: AgentActionId::mint(),
completed,
span: tracing::info_span!("test.command"),
enqueued_at: tokio::time::Instant::now(),
- })
+ }))
.await
.unwrap();
entered.notified().await;
@@ -1923,14 +1923,14 @@ async fn assert_restore_persistence_failure_does_not_send_prompt(failure: Restor
);
let (completed, completion) = oneshot::channel();
commands
- .send(SessionCommand {
+ .send(SessionMessage::Action(SessionCommand {
user_id: None,
action: AgentAction::prompt("must remain unsent"),
action_id: AgentActionId::mint(),
completed,
span: tracing::Span::none(),
enqueued_at: tokio::time::Instant::now(),
- })
+ }))
.await
.unwrap();
let queued = actor.next_input().await;
@@ -2102,14 +2102,14 @@ async fn a_prompt_turn_is_traced_as_an_agent_span_under_its_command() {
let action_id = AgentActionId::mint();
let (completed, result) = oneshot::channel();
commands
- .send(SessionCommand {
+ .send(SessionMessage::Action(SessionCommand {
user_id: None,
action: AgentAction::prompt("what time is it?"),
action_id,
completed,
span: command_span,
enqueued_at: tokio::time::Instant::now(),
- })
+ }))
.await
.unwrap();
let prompt = outbound_rx.recv().await.expect("prompt is dispatched");
@@ -2244,7 +2244,7 @@ async fn a_command_span_records_its_wait_for_the_handshake() {
);
let send_prompt = |name: &'static str| {
let (completed, result) = oneshot::channel();
- let command = SessionCommand {
+ let command = SessionMessage::Action(SessionCommand {
user_id: None,
action: AgentAction::prompt(name),
action_id: AgentActionId::mint(),
@@ -2257,7 +2257,7 @@ async fn a_command_span_records_its_wait_for_the_handshake() {
agent.command.handshake_wait_ms = tracing::field::Empty,
),
enqueued_at: tokio::time::Instant::now(),
- };
+ });
(command, result)
};
diff --git a/crates/agent_session/src/domain/session/actors.rs b/crates/agent_session/src/domain/session/actors.rs
index 1924fb3935..e92183940a 100644
--- a/crates/agent_session/src/domain/session/actors.rs
+++ b/crates/agent_session/src/domain/session/actors.rs
@@ -59,6 +59,15 @@ pub(crate) struct SessionCommand {
pub(crate) enqueued_at: Instant,
}
+/// What reaches a session's actor through its command channel. One channel,
+/// so a server change and the prompt behind it arrive in the order sent.
+pub(crate) enum SessionMessage {
+ /// Deliver an action.
+ Action(SessionCommand),
+ /// Hand the agent these MCP servers from now on.
+ ReplaceMcpServers(Vec<McpServer>),
+}
+
pub(crate) struct SessionCompletion {
completed: oneshot::Sender<Result<()>>,
span: tracing::Span,
@@ -87,7 +96,7 @@ pub(crate) struct SessionActor<Connector: AgentConnector, Logs> {
outbound: Connector::Sender,
inbound: Connector::Receiver,
logs: Logs,
- commands: mpsc::Receiver<SessionCommand>,
+ commands: mpsc::Receiver<SessionMessage>,
/// This connection's handshake gate: published to when this session runs
/// the handshake, watched for when another session runs it.
handshake: watch::Sender<HandshakeStatus>,
@@ -127,7 +136,7 @@ where
permission_policy: PermissionPolicy,
connector: Connector,
logs: Logs,
- commands: mpsc::Receiver<SessionCommand>,
+ commands: mpsc::Receiver<SessionMessage>,
handshake: watch::Sender<HandshakeStatus>,
turn_observer: Arc<dyn SessionTurnObserver>,
tool_catalog: Arc<dyn SessionToolCatalog>,
@@ -237,7 +246,8 @@ where
}
},
command = self.commands.recv() => match command {
- Some(SessionCommand { user_id, action, action_id, completed, span, enqueued_at }) => {
+ Some(SessionMessage::ReplaceMcpServers(servers)) => Input::ReplaceMcpServers(servers),
+ Some(SessionMessage::Action(SessionCommand { user_id, action, action_id, completed, span, enqueued_at })) => {
span.record(
"agent.command.queue_wait_ms",
elapsed_ms(enqueued_at),
diff --git a/crates/agent_session/src/domain/session/session.rs b/crates/agent_session/src/domain/session/session.rs
index 600ae215e9..5ee7520584 100644
--- a/crates/agent_session/src/domain/session/session.rs
+++ b/crates/agent_session/src/domain/session/session.rs
@@ -62,6 +62,13 @@ pub struct SessionMachine<Token> {
/// `session/resume`, and `session/load` alike, because the agent process
/// behind a reconnect is fresh and holds no server from before.
mcp_servers: Vec<McpServer>,
+ /// `mcp_servers` changed after the request that opened this session went
+ /// out, so the agent still has the old list. Settled by re-resuming the
+ /// live session between turns; see [`Self::begin_mcp_refresh`].
+ mcp_refresh_required: bool,
+ /// What the connection's `initialize` said about restoring sessions;
+ /// `None` until this machine opens. A refresh needs `session/resume`.
+ restore: Option<SessionRestoreSupport>,
permission_policy: PermissionPolicy,
/// Only for a newly created ACP session; restored sessions retain their model.
initial_model: Option<String>,
@@ -91,6 +98,8 @@ impl<Token> SessionMachine<Token> {
reload_required: false,
workspace,
mcp_servers,
+ mcp_refresh_required: false,
+ restore: None,
permission_policy,
initial_model: None,
outstanding_permissions: HashMap::new(),
@@ -117,6 +126,8 @@ impl<Token> SessionMachine<Token> {
reload_required: false,
workspace,
mcp_servers,
+ mcp_refresh_required: false,
+ restore: None,
permission_policy,
initial_model: None,
outstanding_permissions: HashMap::new(),
@@ -198,10 +209,82 @@ impl<Token> SessionMachine<Token> {
self.begin_opening(restore, &mut effects);
effects
}
+ Input::ReplaceMcpServers(servers) => self.on_mcp_servers_replaced(servers),
Input::Closed(reason) => self.on_closed(reason),
}
}
+ fn on_mcp_servers_replaced(&mut self, servers: Vec<McpServer>) -> Vec<Effect<Token>> {
+ let mut effects = Vec::new();
+ if servers == self.mcp_servers {
+ return effects;
+ }
+ self.mcp_servers = servers;
+ match self.phase {
+ // The opening request has not gone out yet, and will carry them.
+ SessionPhase::Booting | SessionPhase::Initializing { .. } | SessionPhase::Dead => {}
+ SessionPhase::Opening { .. } | SessionPhase::ConfiguringModel { .. } => {
+ self.mcp_refresh_required = true;
+ }
+ SessionPhase::Live { .. } => {
+ self.mcp_refresh_required = true;
+ self.begin_mcp_refresh(&mut effects);
+ }
+ }
+ effects
+ }
+
+ /// Re-resume the live session so the agent picks up `mcp_servers`.
+ ///
+ /// Only between turns and with no question open: a resume mid-turn would
+ /// race the prompt it interrupts, so a turn in flight leaves the refresh
+ /// for its end, and a held elicitation for its answer. Actions
+ /// arriving meanwhile queue behind the resume, so the next prompt runs
+ /// with the new servers. An agent that cannot resume, or refuses, is not
+ /// asked again until the list changes. Returns whether a resume went out.
+ fn begin_mcp_refresh(&mut self, effects: &mut Vec<Effect<Token>>) -> bool {
+ if !self.mcp_refresh_required || self.in_flight_turn.is_some() {
+ return false;
+ }
+ let SessionPhase::Live {
+ session_id,
+ elicitation: None,
+ } = &self.phase
+ else {
+ return false;
+ };
+ let session_id = session_id.clone();
+ self.mcp_refresh_required = false;
+ if !self.restore.is_some_and(|restore| restore.resume) {
+ tracing::warn!(
+ id = %self.id,
+ "the agent cannot resume a session; its new MCP servers wait for the next attach"
+ );
+ return false;
+ }
+ match self.build_session_request(
+ self.resume_request(session_id.clone()),
+ SessionOpening::Refresh(session_id),
+ ) {
+ Ok((resume, request_id, kind)) => {
+ effects.push(Effect::Send {
+ from: None,
+ message: ToRuntimeMessage::Acp(AcpMessage(resume)),
+ });
+ self.phase = SessionPhase::Opening { request_id, kind };
+ true
+ }
+ Err(error) => {
+ tracing::error!(
+ error = ?error,
+ id = %self.id,
+ "could not build the session/resume that hands over new MCP servers"
+ );
+ false
+ }
+ }
+ }
+
/// Open on a connection somebody else initialized.
///
/// Ignored unless still booting: the machine that ran the handshake is
@@ -291,6 +374,7 @@ impl<Token> SessionMachine<Token> {
}
}
+ let answers_elicitation = matches!(action, AgentAction::RespondElicitation(_));
// Through the queue even when live, so an action can never overtake
// one accepted earlier. (A completed flush leaves the queue empty, so
// the flush below sends exactly this action.)
@@ -301,6 +385,10 @@ impl<Token> SessionMachine<Token> {
token,
});
self.flush(&session_id, &mut effects);
+ // A refresh held back by the question can go now that it is answered.
+ if answers_elicitation {
+ self.begin_mcp_refresh(&mut effects);
+ }
effects
}
@@ -396,6 +484,8 @@ impl<Token> SessionMachine<Token> {
self.cancel_outstanding_permissions(effects);
if self.reload_required {
self.begin_reload(effects);
+ } else {
+ self.begin_mcp_refresh(effects);
}
return;
}
@@ -457,6 +547,9 @@ impl<Token> SessionMachine<Token> {
/// Ask the agent for this session, however it has to be established.
fn begin_opening(&mut self, restore: SessionRestoreSupport, effects: &mut Vec<Effect<Token>>) {
+ self.restore = Some(restore);
+ // Whichever request opens the session carries the current list.
+ self.mcp_refresh_required = false;
let opening = match self.resume_session_id.clone() {
Some(session_id) if restore.resume && !self.reload_required => {
self.build_resume_session_request(session_id)
@@ -487,6 +580,23 @@ impl<Token> SessionMachine<Token> {
}
fn on_session_opened(&mut self, frame: &RawJsonRpcMessage, effects: &mut Vec<Effect<Token>>) {
+ if let SessionPhase::Opening {
+ kind: SessionOpening::Refresh(session_id),
+ ..
+ } = &self.phase
+ {
+ // The session was live before this resume and the agent still
+ // holds it either way; a refusal only means the old servers stay.
+ let session_id = session_id.clone();
+ if !matches!(frame, RawJsonRpcMessage::Response(Response::Result { .. })) {
+ tracing::warn!(
+ id = %self.id,
+ "the agent refused new MCP servers; the session keeps the ones it had"
+ );
+ }
+ self.finish_opening(session_id, effects);
+ return;
+ }
let RawJsonRpcMessage::Response(Response::Result { result, .. }) = frame else {
self.die(StopReason::SessionRefused, effects);
return;
@@ -519,6 +629,7 @@ impl<Token> SessionMachine<Token> {
}
(session_id, false)
}
+ SessionOpening::Refresh(_) => unreachable!("answered before the shared opening path"),
SessionOpening::Load(session_id) => {
if let Err(error) = serde_json::from_value::<LoadSessionResponse>(result.clone()) {
self.die(
@@ -610,6 +721,9 @@ impl<Token> SessionMachine<Token> {
self.begin_reload(effects);
return;
}
+ if self.begin_mcp_refresh(effects) {
+ return;
+ }
self.flush(&session_id, effects);
}
@@ -635,13 +749,17 @@ impl<Token> SessionMachine<Token> {
agent_client_protocol::Error,
> {
self.build_session_request(
- ResumeSessionRequest::new(session_id.clone(), self.workspace.clone())
- .mcp_servers(self.mcp_servers.clone())
- .meta(self.session_meta()),
+ self.resume_request(session_id.clone()),
SessionOpening::Resume(session_id),
)
}
+ fn resume_request(&self, session_id: SessionId) -> ResumeSessionRequest {
+ ResumeSessionRequest::new(session_id, self.workspace.clone())
+ .mcp_servers(self.mcp_servers.clone())
+ .meta(self.session_meta())
+ }
+
fn build_load_session_request(
&mut self,
session_id: SessionId,
diff --git a/crates/agent_session/src/domain/session/tests.rs b/crates/agent_session/src/domain/session/tests.rs
index aae2cc8c74..7c842db185 100644
--- a/crates/agent_session/src/domain/session/tests.rs
+++ b/crates/agent_session/src/domain/session/tests.rs
@@ -2,10 +2,11 @@
//! tokio, no mocks, no waiting - `Token` is a plain integer.
use agent_client_protocol::schema::v1::{
- AgentCapabilities, InitializeResponse, NewSessionResponse, PermissionOption,
- PermissionOptionKind, RequestId, RequestPermissionOutcome, RequestPermissionRequest,
- RequestPermissionResponse, Response, ResumeSessionResponse, SelectedPermissionOutcome,
- SessionCapabilities, SessionResumeCapabilities, ToolCallUpdate, ToolCallUpdateFields,
+ AgentCapabilities, InitializeResponse, McpServer, McpServerHttp, NewSessionResponse,
+ PermissionOption, PermissionOptionKind, RequestId, RequestPermissionOutcome,
+ RequestPermissionRequest, RequestPermissionResponse, Response, ResumeSessionResponse,
+ SelectedPermissionOutcome, SessionCapabilities, SessionResumeCapabilities, ToolCallUpdate,
+ ToolCallUpdateFields,
};
use agent_client_protocol::{JsonRpcMessage, RawJsonRpcMessage};
use agent_runtime_protocol::domain::action::{
@@ -1915,4 +1916,228 @@ mod elicitation {
assert_eq!(machine.pending_elicitation(), None);
assert_eq!(machine.status(), RuntimeStatus::Dead);
}
+
+ #[test]
+ fn new_mcp_servers_held_behind_an_elicitation_go_out_once_it_is_answered() {
+ let mut machine = live_resumable_machine();
+ machine.handle(create(RequestId::Number(0), form_for("acp-42")));
+
+ let replaced = machine.handle(Input::ReplaceMcpServers(vec![connected_app("linear")]));
+ assert!(replaced.is_empty(), "the agent is waiting on its question");
+
+ let answered = machine.handle(answer(
+ ElicitationRequestId::Number(0),
+ ElicitationAnswer::Decline,
+ 1,
+ ));
+
+ assert_eq!(
+ sent_responses(&answered)
+ .into_iter()
+ .map(|(id, _)| id)
+ .collect::<Vec<_>>(),
+ [RequestId::Number(0)]
+ );
+ assert_eq!(sent_methods(&answered), ["session/resume"]);
+ assert!(matches!(
+ answered[..],
+ [
+ Effect::Send { .. },
+ Effect::Complete {
+ token: 1,
+ result: Ok(())
+ },
+ Effect::Send { .. },
+ ]
+ ));
+ }
+}
+
+/// A live session on an agent that offers `session/resume`; its `session/new`
+/// was request 1.
+fn live_resumable_machine() -> SessionMachine<u32> {
+ let mut machine = machine();
+ machine.handle(acp_ready());
+ machine.handle(initialized_with(
+ InitializeResponse::new(PROTOCOL_VERSION).agent_capabilities(
+ AgentCapabilities::new().session_capabilities(
+ SessionCapabilities::new().resume(SessionResumeCapabilities::new()),
+ ),
+ ),
+ ));
+ machine.handle(session_opened("acp-42"));
+ machine
+}
+
+fn connected_app(name: &str) -> McpServer {
+ McpServer::Http(McpServerHttp::new(
+ name,
+ format!("https://egress.example.com/mcp/{name}"),
+ ))
+}
+
+#[test]
+fn new_mcp_servers_resume_a_live_session_before_its_next_prompt() {
+ let mut machine = live_resumable_machine();
+
+ let replaced = machine.handle(Input::ReplaceMcpServers(vec![connected_app("linear")]));
+
+ let [
+ Effect::Send {
+ from: None,
+ message: ToRuntimeMessage::Acp(AcpMessage(RawJsonRpcMessage::Request(resume))),
+ },
+ ] = &replaced[..]
+ else {
+ panic!("expected exactly the resume, got {replaced:?}");
+ };
+ assert_eq!(&*resume.method, "session/resume");
+ assert_eq!(resume.id, request_id(2));
+ assert_eq!(
+ resume.params.clone().map(|params| params.into_value()),
+ Some(serde_json::json!({
+ "sessionId": "acp-42",
+ "cwd": "/workspace",
+ "mcpServers": [{
+ "type": "http",
+ "name": "linear",
+ "url": "https://egress.example.com/mcp/linear",
+ "headers": [],
+ }],
+ "_meta": { "macro.com/agentSessionId": AgentSessionId::TEST_A.to_string() },
+ }))
+ );
+ assert_eq!(machine.status(), RuntimeStatus::Handshaking);
+
+ let queued = machine.handle(command("use linear", 1));
+ assert!(queued.is_empty(), "the prompt waits for the resume");
+
+ let resumed = machine.handle(frame(RawJsonRpcMessage::response(
+ request_id(2),
+ Ok(serde_json::to_value(ResumeSessionResponse::new()).unwrap()),
+ )));
+ assert_eq!(sent_methods(&resumed), ["session/prompt"]);
+ assert!(matches!(
+ resumed[..],
+ [
+ Effect::Log { .. },
+ Effect::Send { .. },
+ Effect::Complete {
+ token: 1,
+ result: Ok(())
+ }
+ ]
+ ));
+ assert_eq!(machine.status().session_id().unwrap().to_string(), "acp-42");
+}
+
+#[test]
+fn new_mcp_servers_wait_for_the_running_turn_to_end() {
+ let mut machine = live_resumable_machine();
+ let prompt = sent_request_ids(&machine.handle(command("working", 1)))[0].clone();
+
+ let replaced = machine.handle(Input::ReplaceMcpServers(vec![connected_app("linear")]));
+ assert!(replaced.is_empty(), "nothing interrupts the turn");
+
+ let ended = machine.handle(frame(RawJsonRpcMessage::response(
+ prompt,
+ Ok(serde_json::json!({ "stopReason": "end_turn" })),
+ )));
+ assert_eq!(sent_methods(&ended), ["session/resume"]);
+}
+
+#[test]
+fn the_same_mcp_servers_send_nothing() {
+ let mut machine = live_resumable_machine();
+
+ let replaced = machine.handle(Input::ReplaceMcpServers(Vec::new()));
+
+ assert!(replaced.is_empty());
+ assert!(matches!(machine.status(), RuntimeStatus::Live { .. }));
+}
+
+#[test]
+fn a_refused_mcp_refresh_keeps_the_session_live_and_sends_what_waited() {
+ let mut machine = live_resumable_machine();
+ machine.handle(Input::ReplaceMcpServers(vec![connected_app("linear")]));
+ machine.handle(command("use linear", 1));
+
+ let refused = machine.handle(frame(RawJsonRpcMessage::response(
+ request_id(2),
+ Err(agent_client_protocol::Error::internal_error()),
+ )));
+
+ assert_eq!(sent_methods(&refused), ["session/prompt"]);
+ assert!(
+ !refused
+ .iter()
+ .any(|effect| matches!(effect, Effect::Stop { .. }))
+ );
+ assert_eq!(machine.status().session_id().unwrap().to_string(), "acp-42");
+}
+
+#[test]
+fn an_agent_that_cannot_resume_keeps_its_mcp_servers_until_the_next_attach() {
+ let mut machine = machine();
+ begin_opening(&mut machine);
+ machine.handle(session_opened("acp-42"));
+
+ let replaced = machine.handle(Input::ReplaceMcpServers(vec![connected_app("linear")]));
+
+ assert!(replaced.is_empty());
+ assert!(matches!(machine.status(), RuntimeStatus::Live { .. }));
+}
+
+#[test]
+fn mcp_servers_replaced_while_opening_resume_once_live() {
+ let mut machine = machine();
+ machine.handle(acp_ready());
+ machine.handle(initialized_with(
+ InitializeResponse::new(PROTOCOL_VERSION).agent_capabilities(
+ AgentCapabilities::new().session_capabilities(
+ SessionCapabilities::new().resume(SessionResumeCapabilities::new()),
+ ),
+ ),
+ ));
+ machine.handle(command("use linear", 1));
+
+ let replaced = machine.handle(Input::ReplaceMcpServers(vec![connected_app("linear")]));
+ assert!(replaced.is_empty(), "session/new is already on the wire");
+
+ let opened = machine.handle(session_opened("acp-42"));
+ assert_eq!(sent_methods(&opened), ["session/resume"]);
+ let resumed = machine.handle(frame(RawJsonRpcMessage::response(
+ request_id(2),
+ Ok(serde_json::to_value(ResumeSessionResponse::new()).unwrap()),
+ )));
+ assert_eq!(sent_methods(&resumed), ["session/prompt"]);
+}
+
+#[test]
+fn mcp_servers_replaced_before_the_handshake_ride_on_session_new() {
+ let mut machine = machine();
+ machine.handle(acp_ready());
+ machine.handle(Input::ReplaceMcpServers(vec![connected_app("linear")]));
+
+ let opening = machine.handle(initialized());
+
+ let [
+ Effect::Log { .. },
+ Effect::Initialized { .. },
+ Effect::Send {
+ message: ToRuntimeMessage::Acp(AcpMessage(RawJsonRpcMessage::Request(new))),
+ ..
+ },
+ ] = &opening[..]
+ else {
+ panic!("expected session/new, got {opening:?}");
+ };
+ assert_eq!(
+ new.params
+ .clone()
+ .map(|params| params.into_value()["mcpServers"][0]["name"].clone()),
+ Some(serde_json::json!("linear"))
+ );
+ let opened = machine.handle(session_opened("acp-42"));
+ assert!(!sent_methods(&opened).contains(&"session/resume".to_owned()));
}
diff --git a/crates/agent_session/src/domain/session/types.rs b/crates/agent_session/src/domain/session/types.rs
index bea1135a99..7b84c1d6bc 100644
--- a/crates/agent_session/src/domain/session/types.rs
+++ b/crates/agent_session/src/domain/session/types.rs
@@ -1,6 +1,6 @@
//! The machine's vocabulary: phases, inputs, and effects.
-use agent_client_protocol::schema::v1::{PermissionOptionId, RequestId, SessionId};
+use agent_client_protocol::schema::v1::{McpServer, PermissionOptionId, RequestId, SessionId};
use agent_runtime_protocol::domain::action::{AgentAction, AgentActionId};
use agent_runtime_protocol::domain::schema::v0::{ToRuntimeMessage, ToServerMessage};
use macro_user_id::user_id::MacroUserIdStr;
@@ -83,6 +83,11 @@ pub(super) enum SessionOpening {
New,
Resume(SessionId),
Load(SessionId),
+ /// A `session/resume` of the session already live on this connection,
+ /// sent only to hand the agent a new MCP server list. Unlike a resume
+ /// that opens the connection, a refusal leaves the session live on the
+ /// servers it already had.
+ Refresh(SessionId),
}
/// What the agent said it can do about a session it has seen before, distilled
@@ -220,6 +225,10 @@ pub enum Input<Token> {
/// Handshake from this transport.
context: std::sync::Arc<InitializationContext>,
},
+ /// The MCP servers the agent should have from now on. A live session is
+ /// re-resumed with them between turns; one still opening takes them on
+ /// the request it has yet to send, or re-resumes once live.
+ ReplaceMcpServers(Vec<McpServer>),
/// The connection is over. Idempotent: a dead machine ignores it.
Closed(CloseReason),
}
diff --git a/docs/AGENT_GUIDE/ai-chat.md b/docs/AGENT_GUIDE/ai-chat.md
index 8ada40ab43..2008b3166e 100644
--- a/docs/AGENT_GUIDE/ai-chat.md
+++ b/docs/AGENT_GUIDE/ai-chat.md
@@ -15,8 +15,11 @@ not establish that the requested action is supported.
For a suitable unconnected app, the assistant offers **Connect <app>**. Clicking
it opens the hosted authorization flow over the agent transcript. After the
backend verifies and registers the connection, the agent session sends a visible
-continuation message in the same conversation. The native Macro runtime refreshes
-the session’s permitted connector tools before the next turn. The unsent draft
+continuation message in the same conversation. Before that message reaches the
+agent, the harness hands the live session its permitted connector servers again
+(a `session/resume`), so the next turn can use the new app. The native Macro
+runtime and sandboxed coding sessions support this; a runtime that cannot resume
+a session picks the app up when it next attaches. The unsent draft
is preserved. Closing authorization or a failed registration sends no message.
If the conversation changed or was closed during auth, there is no automatic
continuation; the connected app remains available for the next request.
diff --git a/services/agent_harness_service/Cargo.toml b/services/agent_harness_service/Cargo.toml
index 7d70a7e5a3..7b195caba0 100644
--- a/services/agent_harness_service/Cargo.toml
+++ b/services/agent_harness_service/Cargo.toml
@@ -17,7 +17,6 @@ name = "agent_harness_service_doppler_config"
path = "src/doppler_config.rs"
[dependencies]
-agent-client-protocol.workspace = true
ai_billing = { path = "../../crates/ai_billing", features = ["pg-admission"] }
roles_and_permissions = { path = "../../crates/roles_and_permissions", default-features = false, features = ["outbound"] }
teams = { path = "../../crates/teams", default-features = false, features = ["outbound"] }
diff --git a/services/agent_harness_service/src/main.rs b/services/agent_harness_service/src/main.rs
index 8decba4d0f..f9402f5341 100644
--- a/services/agent_harness_service/src/main.rs
+++ b/services/agent_harness_service/src/main.rs
@@ -21,7 +21,6 @@ mod model_providers;
mod permission_policy;
mod routine_sessions;
mod runtime_commands;
-mod session_mcp;
mod trigger;
#[cfg(test)]
@@ -512,17 +511,10 @@ async fn run() -> anyhow::Result<()> {
// server's session for the life of the egress token, so a replaced agent
// task and the catalog's listing reuse the handshake instead of opening
// a second set of clients and dropping them when the listing ends.
- let mcp_connector = Arc::new(
- AcpMcpConnector::new(EgressMcpClient::new(Arc::clone(&egress), &egress_base_url))
- .with_source(Arc::new(session_mcp::SessionConnectors {
- sessions: session_repo.clone(),
- provisioner: Arc::new(EgressProvisioner::new(
- Arc::clone(&mcp_connections),
- Arc::clone(&mcp_servers),
- &egress_base_url,
- )),
- })),
- );
+ let mcp_connector = Arc::new(AcpMcpConnector::new(EgressMcpClient::new(
+ Arc::clone(&egress),
+ &egress_base_url,
+ )));
let tool_catalog: Arc<dyn agent_session::domain::ports::SessionToolCatalog> =
Arc::new(McpToolCatalog::new(Arc::clone(&mcp_connector)));
let sessions = sessions.with_tool_catalog(Arc::clone(&tool_catalog));
diff --git a/services/agent_harness_service/src/session_mcp.rs b/services/agent_harness_service/src/session_mcp.rs
deleted file mode 100644
index 46736978f4..0000000000
--- a/services/agent_harness_service/src/session_mcp.rs
+++ /dev/null
@@ -1,48 +0,0 @@
-//! Adapt the native runtime's advertised credential to the harness refresh use case.
-use agent_client_protocol::schema::v1::McpServer;
-use agent_egress::domain::model::SessionToken;
-use agent_harness::domain::ports::SandboxEgressProvisioner;
-use agent_inmem::domain::mcp::SessionMcpSource;
-use agent_session::domain::{model::AgentSessionId, ports::AgentSessionRepo};
-use std::{pin::Pin, sync::Arc};
-
-pub struct SessionConnectors<R, P> {
- pub sessions: R,
- pub provisioner: Arc<P>,
-}
-
-impl<R: AgentSessionRepo, P: SandboxEgressProvisioner> SessionMcpSource
- for SessionConnectors<R, P>
-{
- fn servers<'a>(
- &'a self,
- session: AgentSessionId,
- advertised: Vec<McpServer>,
- ) -> Pin<Box<dyn Future<Output = anyhow::Result<Vec<McpServer>>> + Send + 'a>> {
- Box::pin(async move {
- let token = advertised
- .iter()
- .find_map(|server| match server {
- McpServer::Http(server)
- if server.name == agent_inmem::domain::mcp::MACRO_MCP_NAME =>
- {
- server
- .headers
- .iter()
- .find(|header| header.name.eq_ignore_ascii_case("authorization"))
- .and_then(|header| header.value.strip_prefix("Bearer "))
- }
- _ => None,
- })
- .ok_or_else(|| anyhow::anyhow!("Session MCP credential is missing"))?;
- let egress = agent_harness::domain::service::session_mcp::refresh(
- &self.sessions,
- self.provisioner.as_ref(),
- session,
- SessionToken::new(token.to_owned()),
- )
- .await?;
- Ok(egress.acp_servers())
- })
- }
-}