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.

fix(macrod): forward configured MCP connections to external agents (#8378)

Eric Hayes5155ef31aac7
diff --git a/crates/agent_harness/AGENTS.md b/crates/agent_harness/AGENTS.md
index f370abeefc..3646d2346c 100644
--- a/crates/agent_harness/AGENTS.md
+++ b/crates/agent_harness/AGENTS.md
@@ -41,6 +41,10 @@ between the sandbox and the rest of the product.
- Add a trigger source: `crates/agent_trigger` (context) →
`src/domain/trigger_router.rs` → `src/inbound/kafka.rs`.
+External runtimes receive the session's configured MCP selection when binding,
+including enabled owner connections and custom servers under `OwnerConnections`.
+Use `external_mcp_servers` to preserve the host-reachable egress URL override.
+
## Checks
- `cargo test -p agent_harness` (`SQLX_OFFLINE` unset).
diff --git a/crates/agent_harness/src/domain/model.rs b/crates/agent_harness/src/domain/model.rs
index 5ae4bc5d4f..62db76b72a 100644
--- a/crates/agent_harness/src/domain/model.rs
+++ b/crates/agent_harness/src/domain/model.rs
@@ -221,8 +221,9 @@ impl AgentKind {
/// 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.
+ /// Codex Cloud is handed none. External runtimes receive the whole list
+ /// through `SandboxEgressProvisioner::external_mcp_servers` when they bind,
+ /// but do not participate in live refresh.
#[must_use]
pub fn takes_egress_mcp_servers(self) -> bool {
!matches!(self, Self::CodexCloud | Self::External)
diff --git a/crates/agent_harness/src/domain/ports.rs b/crates/agent_harness/src/domain/ports.rs
index c7540e2877..dea99b66a8 100644
--- a/crates/agent_harness/src/domain/ports.rs
+++ b/crates/agent_harness/src/domain/ports.rs
@@ -397,12 +397,12 @@ pub trait RuntimeConnections: Send + Sync + 'static {
/// 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.
+ /// The session's MCP servers at addresses reachable by an external runtime.
fn external_mcp_servers(
&self,
egress: &SandboxEgress,
) -> Vec<agent_client_protocol::schema::v1::McpServer> {
- vec![egress.internal_mcp_server(), egress.preview_mcp_server()]
+ egress.acp_servers()
}
/// The egress environment for one session, on behalf of `owner`, and the
diff --git a/crates/agent_harness/src/domain/service/deliver.rs b/crates/agent_harness/src/domain/service/deliver.rs
index 2590982eff..7f9a810190 100644
--- a/crates/agent_harness/src/domain/service/deliver.rs
+++ b/crates/agent_harness/src/domain/service/deliver.rs
@@ -134,13 +134,7 @@ where
}
let egress = self
.egress
- .provision(
- session_id,
- session.owner_user()?,
- &AgentMcpServers::Selected {
- servers: Vec::new(),
- },
- )
+ .provision(session_id, session.owner_user()?, &session.mcp_servers)
.await?;
self.sessions
.set_egress_token_hash(session_id, &egress.session_token_hash)
diff --git a/crates/agent_harness/src/domain/service/open.rs b/crates/agent_harness/src/domain/service/open.rs
index 97a1ba343f..12341d407d 100644
--- a/crates/agent_harness/src/domain/service/open.rs
+++ b/crates/agent_harness/src/domain/service/open.rs
@@ -83,13 +83,19 @@ where
})?;
}
let defaults = self.inner.defaults.for_bot(request.bot_id);
- let (model, harness, profile_instructions) = match request.profile {
+ let (model, harness, profile_instructions, mcp_servers) = match request.profile {
Some(profile) => (
profile.model,
profile.harness,
Some(profile.instructions).filter(|value| !value.trim().is_empty()),
+ profile.mcp_servers,
+ ),
+ None => (
+ defaults.model.clone(),
+ defaults.harness.clone(),
+ None,
+ AgentMcpServers::OwnerConnections,
),
- None => (defaults.model.clone(), defaults.harness.clone(), None),
};
let instructions = self
.inner
@@ -117,9 +123,8 @@ where
workspace: request.workspace,
sandbox_size: SandboxSize::Default,
instructions,
- // No egress, so no MCP servers of ours to select from.
- mcp_servers: AgentMcpServers::OwnerConnections,
- // Mint the internal-tool credential when an authenticated
+ mcp_servers,
+ // Mint the session MCP credential when an authenticated
// runtime binds, and rotate it on each subsequent binding.
egress_token_hash: None,
// The thread linkage is the caller's claim, not an observed
diff --git a/crates/agent_harness/src/domain/service/test.rs b/crates/agent_harness/src/domain/service/test.rs
index 1e73f8b23b..d5407ff36e 100644
--- a/crates/agent_harness/src/domain/service/test.rs
+++ b/crates/agent_harness/src/domain/service/test.rs
@@ -4,6 +4,7 @@
mod chat_reply;
mod coding_preferences;
+mod external_mcp;
mod owned_purge;
mod presentation;
mod quota;
@@ -3135,101 +3136,6 @@ async fn prompt(
.await
}
-#[tokio::test]
-async fn an_external_open_provisions_nothing_and_prompts_nobody() {
- const INSTRUCTIONS: &str = "Never force-push.";
- let composer = PromptComposerMock::default();
- let (service, repo, containers, announcer, runtimes) =
- harness_with_edges(PromptContextMock::default(), composer.clone());
- let mut request = open_external_request("/home/operator/code");
- request.profile = Some(agent_session::domain::ports::ManagedAgentProfile {
- model: String::new(),
- harness: harness_id::MACROD_HARNESS_SLUG.to_owned(),
- instructions: INSTRUCTIONS.to_owned(),
- mcp_servers: AgentMcpServers::OwnerConnections,
- });
-
- let session = service
- .open_external_session(request)
- .await
- .expect("an external open needs no runtime yet");
-
- // The row exists with the stated workspace, but nothing was provisioned,
- // nothing was announced, and no prompt has gone anywhere: the runtime
- // delivers the first prompt itself through the control endpoint after
- // dialing in.
- assert_eq!(session.workspace, "/home/operator/code");
- assert_eq!(containers.spawned(), 0);
- assert!(announcer.announced().is_empty());
-
- // The operator's runtime dials in for the bot. Nothing happens to the
- // session: no handshake, no ACP session, nothing sent - a session nobody
- // is prompting costs the runtime nothing.
- let runtime = ContainerMock::default();
- runtimes.attach(harness_for_bot(session.bot_id), runtime.clone());
- assert!(runtime.agent().received_requests().is_empty());
- assert!(
- repo.get(session.id)
- .await
- .expect("the session row exists")
- .acp_session_id
- .is_none()
- );
-
- // The runtime forwards the mention through control: that is what binds the
- // session to the connection, handshakes, and lands the prompt.
- let prompted = service.control_event(
- session.id,
- ControlEvent {
- action: AgentAction::prompt("@claude fix the failing test"),
- action_id: None,
- actor: Some(sender()),
- context: None,
- },
- );
- let (result, ()) = tokio::join!(prompted, complete_bound_handshake(&runtime));
- result.expect("the first prompt binds the session and reaches the runtime");
- let requests = runtime.agent().received_requests();
- let ClientRequest::NewSessionRequest(open) = &requests[1] else {
- panic!("expected session/new")
- };
- assert_eq!(open.mcp_servers.len(), 2);
- let agent_client_protocol::schema::v1::McpServer::Http(preview) = &open.mcp_servers[1] else {
- panic!("expected preview HTTP MCP");
- };
- assert_eq!(preview.name, "macro-preview");
- assert!(preview.url.ends_with("/mcp-preview"));
- let agent_client_protocol::schema::v1::McpServer::Http(server) = &open.mcp_servers[0] else {
- panic!("expected HTTP MCP")
- };
- assert_eq!(server.name, "macro_internal");
- assert!(server.url.ends_with("/mcp/internal"));
- assert!(
- server
- .headers
- .iter()
- .any(|header| header.name == "Authorization")
- );
-
- assert_eq!(
- prompts(&runtime.agent()),
- [vec![ContentBlock::from("@claude fix the failing test")]]
- );
- // Prompt delivery is ordered behind the `session/new` response, so the
- // negotiated ACP session id has been persisted by now.
- let row = repo.get(session.id).await.expect("the session row exists");
- assert_eq!(row.acp_session_id, Some(SessionId::new("acp-test")));
- assert_eq!(row.instructions.as_deref(), Some(INSTRUCTIONS));
- assert_eq!(
- composer.calls(),
- [(
- "@claude fix the failing test".to_owned(),
- Some(INSTRUCTIONS.to_owned()),
- None,
- )]
- );
-}
-
#[tokio::test]
async fn a_macrod_session_selects_its_saved_model_before_the_first_prompt() {
for accepted in [true, false] {
diff --git a/crates/agent_harness/src/domain/service/test/external_mcp.rs b/crates/agent_harness/src/domain/service/test/external_mcp.rs
new file mode 100644
index 0000000000..aa914dd0cf
--- /dev/null
+++ b/crates/agent_harness/src/domain/service/test/external_mcp.rs
@@ -0,0 +1,147 @@
+use super::*;
+
+#[tokio::test]
+async fn external_binding_preserves_an_explicit_agent_mcp_selection() {
+ let (service, _repo, _containers, _announcer, runtimes) = harness();
+ let selection = AgentMcpServers::Selected {
+ servers: vec![agent_session::domain::model::AgentMcpServer {
+ app_slug: "linear".to_owned(),
+ server_name: "Linear".to_owned(),
+ }],
+ };
+ let mut request = open_external_request("/home/operator/code");
+ request.profile = Some(agent_session::domain::ports::ManagedAgentProfile {
+ model: String::new(),
+ harness: harness_id::MACROD_HARNESS_SLUG.to_owned(),
+ instructions: String::new(),
+ mcp_servers: selection.clone(),
+ });
+ let session = service.open_external_session(request).await.unwrap();
+ let runtime = ContainerMock::default();
+ runtimes.attach(harness_for_bot(session.bot_id), runtime.clone());
+ let prompted = service.control_event(
+ session.id,
+ ControlEvent {
+ action: AgentAction::prompt("list my issues"),
+ action_id: None,
+ actor: Some(sender()),
+ context: None,
+ },
+ );
+ let (result, ()) = tokio::join!(prompted, complete_bound_handshake(&runtime));
+ result.expect("the prompt reaches the external runtime");
+ assert_eq!(
+ service.inner.egress.provisioned(),
+ [(session.id, sender().to_string(), selection)]
+ );
+}
+
+#[tokio::test]
+async fn an_external_open_provisions_nothing_and_prompts_nobody() {
+ const INSTRUCTIONS: &str = "Never force-push.";
+ let composer = PromptComposerMock::default();
+ let (service, repo, containers, announcer, runtimes) =
+ harness_with_edges(PromptContextMock::default(), composer.clone());
+ service.inner.egress.connect("datadog");
+ let mut request = open_external_request("/home/operator/code");
+ request.profile = Some(agent_session::domain::ports::ManagedAgentProfile {
+ model: String::new(),
+ harness: harness_id::MACROD_HARNESS_SLUG.to_owned(),
+ instructions: INSTRUCTIONS.to_owned(),
+ mcp_servers: AgentMcpServers::OwnerConnections,
+ });
+
+ let session = service
+ .open_external_session(request)
+ .await
+ .expect("an external open needs no runtime yet");
+
+ // The row exists with the stated workspace, but nothing was provisioned,
+ // nothing was announced, and no prompt has gone anywhere: the runtime
+ // delivers the first prompt itself through the control endpoint after
+ // dialing in.
+ assert_eq!(session.workspace, "/home/operator/code");
+ assert_eq!(containers.spawned(), 0);
+ assert!(announcer.announced().is_empty());
+
+ // The operator's runtime dials in for the bot. Nothing happens to the
+ // session: no handshake, no ACP session, nothing sent - a session nobody
+ // is prompting costs the runtime nothing.
+ let runtime = ContainerMock::default();
+ runtimes.attach(harness_for_bot(session.bot_id), runtime.clone());
+ assert!(runtime.agent().received_requests().is_empty());
+ assert!(
+ repo.get(session.id)
+ .await
+ .expect("the session row exists")
+ .acp_session_id
+ .is_none()
+ );
+
+ // The runtime forwards the mention through control: that is what binds the
+ // session to the connection, handshakes, and lands the prompt.
+ let prompted = service.control_event(
+ session.id,
+ ControlEvent {
+ action: AgentAction::prompt("@claude fix the failing test"),
+ action_id: None,
+ actor: Some(sender()),
+ context: None,
+ },
+ );
+ let (result, ()) = tokio::join!(prompted, complete_bound_handshake(&runtime));
+ result.expect("the first prompt binds the session and reaches the runtime");
+ let requests = runtime.agent().received_requests();
+ let ClientRequest::NewSessionRequest(open) = &requests[1] else {
+ panic!("expected session/new")
+ };
+ assert_eq!(
+ service.inner.egress.provisioned(),
+ [(
+ session.id,
+ sender().to_string(),
+ AgentMcpServers::OwnerConnections
+ )]
+ );
+ assert_eq!(open.mcp_servers.len(), 4);
+ let agent_client_protocol::schema::v1::McpServer::Http(datadog) = &open.mcp_servers[3] else {
+ panic!("expected Datadog HTTP MCP");
+ };
+ assert_eq!(datadog.name, "datadog");
+ assert_eq!(datadog.url, "https://egress.test/mcp/datadog");
+ assert_eq!(datadog.headers[0].value, "Bearer test-session-token");
+ let agent_client_protocol::schema::v1::McpServer::Http(preview) = &open.mcp_servers[2] else {
+ panic!("expected preview HTTP MCP");
+ };
+ assert_eq!(preview.name, "macro-preview");
+ assert!(preview.url.ends_with("/mcp-preview"));
+ let agent_client_protocol::schema::v1::McpServer::Http(server) = &open.mcp_servers[1] else {
+ panic!("expected HTTP MCP")
+ };
+ assert_eq!(server.name, "macro_internal");
+ assert!(server.url.ends_with("/mcp/internal"));
+ assert!(
+ server
+ .headers
+ .iter()
+ .any(|header| header.name == "Authorization")
+ );
+
+ assert_eq!(
+ prompts(&runtime.agent()),
+ [vec![ContentBlock::from("@claude fix the failing test")]]
+ );
+ // Prompt delivery is ordered behind the `session/new` response, so the
+ // negotiated ACP session id has been persisted by now.
+ let row = repo.get(session.id).await.expect("the session row exists");
+ assert_eq!(row.acp_session_id, Some(SessionId::new("acp-test")));
+ assert_eq!(row.instructions.as_deref(), Some(INSTRUCTIONS));
+ assert_eq!(
+ composer.calls(),
+ [(
+ "@claude fix the failing test".to_owned(),
+ Some(INSTRUCTIONS.to_owned()),
+ None,
+ )]
+ );
+}
diff --git a/crates/agent_harness/src/outbound/egress.rs b/crates/agent_harness/src/outbound/egress.rs
index e52b380a63..3ab71e0a99 100644
--- a/crates/agent_harness/src/outbound/egress.rs
+++ b/crates/agent_harness/src/outbound/egress.rs
@@ -118,10 +118,7 @@ where
if let Some(url) = &self.external_base_url {
external.base_url = url.clone();
}
- vec![
- external.internal_mcp_server(),
- external.preview_mcp_server(),
- ]
+ external.acp_servers()
}
#[tracing::instrument(err, skip(self), fields(%session, %owner))]
diff --git a/crates/agent_harness/src/outbound/egress/test.rs b/crates/agent_harness/src/outbound/egress/test.rs
index e433bd8e54..3216138805 100644
--- a/crates/agent_harness/src/outbound/egress/test.rs
+++ b/crates/agent_harness/src/outbound/egress/test.rs
@@ -496,14 +496,24 @@ fn external_runtime_uses_host_reachable_urls_with_the_existing_session_credentia
"http://agent-harness-service:8102",
)
.with_external_base_url(Some("http://localhost:28102/".into()));
- let sandbox = egress(&[]);
+ let mut sandbox = egress(&["datadog"]);
+ let custom = listing("https://wiki.example.com/mcp", "Wiki");
+ let custom_path = format!("/mcp-custom/{}", custom.key);
+ sandbox.custom_servers.push(custom);
let servers = provisioner.external_mcp_servers(&sandbox);
- assert_eq!(servers.len(), 2);
- for (server, path) in servers.iter().zip(["/mcp/internal", "/mcp-preview"]) {
+ assert_eq!(servers.len(), 5);
+ for (server, (name, path)) in servers.iter().zip([
+ ("macro", "/mcp-macro"),
+ ("macro_internal", "/mcp/internal"),
+ ("macro-preview", "/mcp-preview"),
+ ("datadog", "/mcp/datadog"),
+ ("Wiki", custom_path.as_str()),
+ ]) {
let McpServer::Http(server) = server else {
panic!("expected HTTP MCP");
};
assert_eq!(server.url, format!("http://localhost:28102{path}"));
+ assert_eq!(server.name, name);
assert_eq!(server.headers[0].value, sandbox.authorization_header());
}
assert_eq!(sandbox.base_url, "https://egress.macro.com");
diff --git a/crates/coding_agent_worker/AGENTS.md b/crates/coding_agent_worker/AGENTS.md
index c0a8c4b7b9..2f96df8b3c 100644
--- a/crates/coding_agent_worker/AGENTS.md
+++ b/crates/coding_agent_worker/AGENTS.md
@@ -60,3 +60,5 @@ the local ACP process (Claude Code, Codex, ...), and drives native
- `README.md` in this directory is the native herdr behavior contract —
update it alongside `src/herdr/` changes.
+- MCP server selection is supplied by `agent_harness` when the session binds;
+ native launch forwards that list, with credentials refreshed on launch/resume.
diff --git a/crates/coding_agent_worker/README.md b/crates/coding_agent_worker/README.md
index 97bb7b84dd..dd1904ee0d 100644
--- a/crates/coding_agent_worker/README.md
+++ b/crates/coding_agent_worker/README.md
@@ -79,6 +79,11 @@ visible dialog, and the same Herdr state generation. Sign-in prompts and other
unknown dialogs require native interaction. Herdr does not offer an atomic
compare-and-send-key operation, so native interaction remains the authority.
+Macro supplies the session's configured MCP connections when macrod binds it:
+owner-connection sessions include enabled connected apps and custom MCP servers;
+explicit agent selections retain their selected apps. All connections use the
+session credential through Macro's egress proxy.
+
Claude receives MCP configuration in a private file. Codex receives per-launch
MCP overrides; HTTP credentials come from private environment files sourced in
the native pane, and stdio servers use private launch scripts. This requires a