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.

chore(agents): wire custom MCP servers through the agent egress proxy (#7419)

Wolf Mermelstein17144f662a76
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.json
index 1224fb1db5..f7398ad0fe 100644
--- a/.github/workspace-dep-closures.json
+++ b/.github/workspace-dep-closures.json
@@ -222,6 +222,7 @@
"crates/macro_tower_layers",
"crates/macro_user_id",
"crates/macro_uuid",
+ "crates/mcp_client",
"crates/mcp_toolset",
"crates/mention_utils",
"crates/messages",
@@ -360,6 +361,7 @@
"crates/macro_tower_layers",
"crates/macro_user_id",
"crates/macro_uuid",
+ "crates/mcp_client",
"crates/mcp_toolset",
"crates/mention_utils",
"crates/messages",
@@ -9651,6 +9653,7 @@
"crates/macro_tower_layers",
"crates/macro_user_id",
"crates/macro_uuid",
+ "crates/mcp_client",
"crates/mcp_toolset",
"crates/mention_utils",
"crates/messages",
diff --git a/Cargo.lock b/Cargo.lock
index ef327a587e..5aeb6a4b47 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -274,10 +274,12 @@ dependencies = [
"macro_auth",
"macro_db_client",
"macro_user_id",
+ "mcp_client",
"model_owner",
"pipedream_mcp",
"rand 0.9.4",
"reqwest 0.13.4",
+ "rmcp",
"rootcause",
"serde",
"serde_json",
@@ -360,6 +362,7 @@ dependencies = [
"macro_tower_layers",
"macro_user_id",
"macro_uuid",
+ "mcp_client",
"messages",
"mockall",
"model-entity",
@@ -452,6 +455,7 @@ dependencies = [
"macro_tower_layers",
"macro_user_id",
"macro_uuid",
+ "mcp_client",
"messages",
"notification",
"pipedream_mcp",
diff --git a/crates/agent_egress/Cargo.toml b/crates/agent_egress/Cargo.toml
index 778f71f9bb..23fec56c41 100644
--- a/crates/agent_egress/Cargo.toml
+++ b/crates/agent_egress/Cargo.toml
@@ -35,6 +35,10 @@ macro_auth = { path = "../macro_auth" }
# The owner's `User` row, which carries the FusionAuth id the exchange needs.
macro_db_client = { path = "../macro_db_client" }
macro_user_id = { path = "../macro_user_id" }
+# The rows holding the owner's custom (URL-added) MCP servers and their
+# encrypted grants, and the refresh that keeps a grant live. Port and domain
+# service only; the Postgres adapter is the composition root's to build.
+mcp_client = { path = "../mcp_client" }
model_owner = { path = "../model_owner" }
# The rows saying which apps an owner connected, and the client that turns one
# into Pipedream's MCP endpoint plus the headers scoping a call to it.
@@ -54,6 +58,8 @@ workspace-hack = { version = "0.1", path = "../workspace-hack" }
[dev-dependencies]
anyhow = { workspace = true }
futures = { workspace = true }
+# Building the stored OAuth grants the custom-server resolver reads.
+rmcp = { workspace = true, features = ["auth"] }
tokio = { workspace = true, features = ["macros", "rt"] }
uuid = { workspace = true }
tower = { workspace = true }
diff --git a/crates/agent_egress/src/domain/error.rs b/crates/agent_egress/src/domain/error.rs
index dbbd0fa0b0..49ebb1094c 100644
--- a/crates/agent_egress/src/domain/error.rs
+++ b/crates/agent_egress/src/domain/error.rs
@@ -1,6 +1,6 @@
//! Failures a proxied call can end in.
-use crate::domain::model::{McpServerSlug, RepoSlug};
+use crate::domain::model::{CustomMcpServerKey, McpServerSlug, RepoSlug};
/// Everything that can stop a sandbox's request from reaching its upstream.
///
@@ -43,6 +43,13 @@ pub enum EgressError {
#[error("no MCP server named {0}")]
UnknownServer(McpServerSlug),
+ /// The key names none of the owner's enabled custom servers. Unlike a
+ /// Pipedream slug there is nothing to address without a row: the URL
+ /// *is* the row, so a key that matches none - or matches one the owner
+ /// turned off - has nowhere to go.
+ #[error("no custom MCP server with key {0}")]
+ UnknownCustomServer(CustomMcpServerKey),
+
/// A request body the proxy had to read was bigger than it will read.
/// Only reachable on the path that answers `tools/call` for an
/// unconnected app; every other request streams through unread.
diff --git a/crates/agent_egress/src/domain/model.rs b/crates/agent_egress/src/domain/model.rs
index eb85b18cb0..8ac3b78d87 100644
--- a/crates/agent_egress/src/domain/model.rs
+++ b/crates/agent_egress/src/domain/model.rs
@@ -152,7 +152,10 @@ impl fmt::Debug for BearerToken {
/// Two shapes because two upstreams disagree: MCP servers take an OAuth
/// bearer, and GitHub's git endpoints take Basic with the installation token
/// as the *password* - git's credential helper protocol has no other way to
-/// carry one.
+/// carry one. The third is the absence of either: a custom MCP server the
+/// owner added by URL without connecting an account is dialed bare, exactly
+/// as the in-process client dials it, and the proxy still strips the
+/// sandbox's own token off the way through.
#[derive(Clone, PartialEq, Eq)]
pub enum UpstreamCredential {
/// `Authorization: Bearer <token>`.
@@ -164,22 +167,26 @@ pub enum UpstreamCredential {
/// The password half - the credential proper.
secret: String,
},
+ /// No `Authorization` at all.
+ Anonymous,
}
impl UpstreamCredential {
/// This credential as an `Authorization` header value, marked sensitive
- /// so the http stack keeps it out of its own logging.
+ /// so the http stack keeps it out of its own logging. `None` for
+ /// [`UpstreamCredential::Anonymous`], which stamps nothing.
///
/// The single place a credential becomes a header, so it is also the
/// single place validation happens: a secret carrying a newline could
/// otherwise inject a header of its own choosing into the upstream
/// request, and `HeaderValue` is what refuses it.
- pub fn header_value(&self) -> Result<HeaderValue, EgressError> {
+ pub fn header_value(&self) -> Result<Option<HeaderValue>, EgressError> {
let rendered = match self {
Self::Bearer(token) => format!("Bearer {}", token.as_str()),
Self::Basic { username, secret } => {
format!("Basic {}", BASE64.encode(format!("{username}:{secret}")))
}
+ Self::Anonymous => return Ok(None),
};
let mut value = HeaderValue::from_str(&rendered).map_err(|error| {
@@ -188,7 +195,7 @@ impl UpstreamCredential {
))
})?;
value.set_sensitive(true);
- Ok(value)
+ Ok(Some(value))
}
}
@@ -197,6 +204,7 @@ impl fmt::Debug for UpstreamCredential {
match self {
Self::Bearer(_) => f.write_str("Bearer([REDACTED])"),
Self::Basic { username, .. } => write!(f, "Basic({username}, [REDACTED])"),
+ Self::Anonymous => f.write_str("Anonymous"),
}
}
}
@@ -274,13 +282,26 @@ pub struct McpServerListing {
pub name: String,
}
-/// How a destination resolved: to a call the owner's grant backs, or to one
-/// that can be addressed for the owner without any grant behind it.
+/// One of the owner's custom MCP servers, as the harness advertises it: the
+/// key the proxy routes on and the name the owner gave the server.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct CustomMcpServerListing {
+ /// The key derived from the server's URL; see [`CustomMcpServerKey`].
+ pub key: CustomMcpServerKey,
+ /// The owner's display name for it, e.g. `Internal wiki`.
+ pub name: String,
+}
+
+/// How a destination resolved: to a call the owner's grant backs, to one
+/// that can be addressed for the owner without any grant behind it, or to
+/// one whose grant has died.
///
/// The second exists because Pipedream scopes a call by user id and app slug
/// alone, so an app the owner never connected is still *addressable* - and
-/// listing its tools works, only calling them does not. The service decides
-/// what to do with that; the resolver only reports it.
+/// listing its tools works, only calling them does not. The third is a custom
+/// server's: the owner connected it once, and the stored grant no longer
+/// yields a token - expired with no refresh token, or refused on refresh. The
+/// service decides what to do with either; the resolver only reports it.
#[derive(Clone, Debug)]
pub enum McpResolution {
/// The owner holds an enabled connection for this destination.
@@ -288,6 +309,15 @@ pub enum McpResolution {
/// The owner holds no enabled connection, but the upstream can be
/// addressed for them anyway.
Unconnected(UpstreamCall),
+ /// The owner's connection exists but its grant is dead, so nothing can
+ /// be stamped. The call is bare; the name is what the owner calls the
+ /// server, carried here because nothing else on the path knows it.
+ Disconnected {
+ /// The upstream, with no credential.
+ call: UpstreamCall,
+ /// The owner's display name for the server.
+ name: String,
+ },
}
/// The most a JSON-RPC request body may be for the proxy to read it.
@@ -356,6 +386,31 @@ pub fn not_connected_tool_result(
name = name,
tag = CONNECT_APP_TAG,
);
+ tool_result_response(id, text)
+}
+
+/// The tool result the proxy answers a `tools/call` with when the session
+/// owner's grant for a custom MCP server has died.
+///
+/// No connect-button markup: that chip opens the Pipedream catalog, and a
+/// custom server is reconnected from its own row under Agents → Connections.
+/// Same shape as [`not_connected_tool_result`] otherwise, for the same
+/// reason - a `result` the model reads, not a transport error it never sees.
+/// The name is the owner's own, off their row, never anything the request
+/// carried.
+pub fn disconnected_tool_result(name: &str, id: serde_json::Value) -> ProxyResponse {
+ let text = format!(
+ "{name} is a custom MCP server the person running this session added, but its \
+ connection has expired, so this tool cannot run yet. Tell them so in your reply, \
+ and ask them to open Agents → Connections, find {name} under Custom MCP, and use \
+ Reconnect. Close your reply by asking them to let you know once they have done so \
+ - when they do, call this tool again; nothing else needs to change.",
+ );
+ tool_result_response(id, text)
+}
+
+/// A JSON-RPC response carrying `text` as an `isError` tool result for `id`.
+fn tool_result_response(id: serde_json::Value, text: String) -> ProxyResponse {
let body = serde_json::json!({
"jsonrpc": "2.0",
"id": id,
@@ -514,6 +569,64 @@ impl fmt::Display for McpServerSlug {
}
}
+/// How many hex characters of the URL digest a [`CustomMcpServerKey`] keeps.
+///
+/// 128 bits: the namespace is one owner's handful of rows, so this is far
+/// past where two of their URLs could meet, and short enough to read in a
+/// log line.
+const CUSTOM_KEY_HEX_LEN: usize = 32;
+
+/// The identifier a sandbox calls one of its owner's custom MCP servers by.
+///
+/// A custom server is a row keyed by the URL the owner typed in, and a URL is
+/// not a path segment - so the key is the SHA-256 digest of that URL,
+/// lowercase hex, truncated to [`CUSTOM_KEY_HEX_LEN`]. Derived, unlike
+/// [`McpServerSlug`], but derived in exactly one place: the harness
+/// advertises [`CustomMcpServerKey::for_url`] of each row's URL and the
+/// resolver matches the same function against the owner's rows, so there is
+/// no second reading for the two ends to disagree over.
+///
+/// One-way on purpose. The sandbox cannot turn a key back into a URL, and the
+/// resolver never parses one out of it: a key names nothing until it meets
+/// the owner's own rows, which is the property every other destination here
+/// has too.
+#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
+pub struct CustomMcpServerKey(String);
+
+impl CustomMcpServerKey {
+ /// The key for a server stored under `url`, verbatim as the row holds it.
+ pub fn for_url(url: &str) -> Self {
+ let digest: String = Sha256::digest(url.as_bytes())
+ .iter()
+ .map(|byte| format!("{byte:02x}"))
+ .collect();
+ Self(digest[..CUSTOM_KEY_HEX_LEN].to_owned())
+ }
+
+ /// Accept a key from a request path.
+ ///
+ /// Exactly the shape [`Self::for_url`] produces and nothing else;
+ /// rejected rather than normalized, like every other path segment here.
+ pub fn parse(segment: &str) -> Option<Self> {
+ let valid = segment.len() == CUSTOM_KEY_HEX_LEN
+ && segment
+ .chars()
+ .all(|character| matches!(character, '0'..='9' | 'a'..='f'));
+ valid.then(|| Self(segment.to_owned()))
+ }
+
+ /// The key as it appears in a path.
+ pub fn as_str(&self) -> &str {
+ &self.0
+ }
+}
+
+impl fmt::Display for CustomMcpServerKey {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.write_str(&self.0)
+ }
+}
+
/// Which half of git's smart-HTTP protocol a request is doing.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GitService {
@@ -621,6 +734,7 @@ impl EgressTarget {
Self::McpServer(McpDestination::Macro) => "macro".to_owned(),
Self::McpServer(McpDestination::Preview) => "macro-preview".to_owned(),
Self::McpServer(McpDestination::Connected(slug)) => slug.as_str().to_owned(),
+ Self::McpServer(McpDestination::Custom(key)) => format!("custom {key}"),
Self::GitHubGit { endpoint } => format!("git {}", endpoint.path_and_query()),
}
}
@@ -628,9 +742,10 @@ impl EgressTarget {
/// Which MCP server a request names.
///
-/// Macro's own server and the owner's connected apps live on different
-/// routes (`/mcp-macro` vs `/mcp/{slug}`), so they can never collide: there
-/// is no reserved word to shadow, and no connected app a name could hide.
+/// Macro's own server, the owner's connected apps, and the owner's custom
+/// servers live on different routes (`/mcp-macro`, `/mcp/{slug}`,
+/// `/mcp-custom/{key}`), so they can never collide: there is no reserved
+/// word to shadow, and no connected app a name could hide.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum McpDestination {
/// Internal, session-scoped live preview tools.
@@ -639,6 +754,9 @@ pub enum McpDestination {
Macro,
/// One of the owner's Pipedream-connected apps.
Connected(McpServerSlug),
+ /// One of the owner's custom MCP servers - added by URL, with Macro's
+ /// own OAuth client holding the grant.
+ Custom(CustomMcpServerKey),
}
/// The route Macro's own MCP server is served on.
@@ -647,6 +765,9 @@ pub const MACRO_MCP_PATH: &str = "/mcp-macro";
/// The route prefix a connected app's slug follows.
pub const CONNECTED_MCP_PATH_PREFIX: &str = "/mcp/";
+/// The route prefix a custom server's key follows.
+pub const CUSTOM_MCP_PATH_PREFIX: &str = "/mcp-custom/";
+
impl McpDestination {
/// Read a destination off a proxy URL's path.
///
@@ -660,6 +781,9 @@ impl McpDestination {
if path == MACRO_MCP_PATH {
return Some(Self::Macro);
}
+ if let Some(key) = path.strip_prefix(CUSTOM_MCP_PATH_PREFIX) {
+ return CustomMcpServerKey::parse(key).map(Self::Custom);
+ }
let slug = path.strip_prefix(CONNECTED_MCP_PATH_PREFIX)?;
McpServerSlug::parse(slug).map(Self::Connected)
}
@@ -719,6 +843,16 @@ impl UpstreamCall {
)
}
+ /// A call with no credential at all.
+ ///
+ /// For a custom MCP server the owner added without connecting an
+ /// account. Still https-only: nothing of the owner's rides on this call,
+ /// but the MCP session id and whatever the server answers do, and the
+ /// URL is typed in by a person.
+ pub fn anonymous(url: Url) -> Result<Self, EgressError> {
+ Self::new(url, UpstreamCredential::Anonymous)
+ }
+
fn new(url: Url, authorization: UpstreamCredential) -> Result<Self, EgressError> {
if url.scheme() != "https" {
return Err(EgressError::InsecureUpstream(url));
diff --git a/crates/agent_egress/src/domain/model/test.rs b/crates/agent_egress/src/domain/model/test.rs
index dcb65d134d..1949c4950d 100644
--- a/crates/agent_egress/src/domain/model/test.rs
+++ b/crates/agent_egress/src/domain/model/test.rs
@@ -194,7 +194,8 @@ fn a_basic_credential_renders_as_github_expects() {
secret: "ghs-installation-token".to_owned(),
}
.header_value()
- .expect("header value");
+ .expect("header value")
+ .expect("a credential to stamp");
assert!(value.is_sensitive());
assert_eq!(
@@ -319,13 +320,26 @@ fn a_git_endpoint_round_trips_to_its_path() {
fn the_stamped_credential_is_marked_sensitive() {
let value = UpstreamCredential::Bearer(BearerToken::new("dd-oauth-token"))
.header_value()
- .expect("header value");
+ .expect("header value")
+ .expect("a credential to stamp");
assert!(value.is_sensitive());
assert_eq!(value.to_str().expect("ascii"), "Bearer dd-oauth-token");
assert_eq!(format!("{value:?}"), "Sensitive");
}
+/// A server the owner added without connecting an account is dialed bare:
+/// there is no header to render, and nothing to redact.
+#[test]
+fn an_anonymous_credential_stamps_nothing() {
+ let value = UpstreamCredential::Anonymous
+ .header_value()
+ .expect("header value");
+
+ assert!(value.is_none());
+ assert_eq!(format!("{:?}", UpstreamCredential::Anonymous), "Anonymous");
+}
+
/// A credential that could carry a newline could inject a header of its own
/// choosing into the upstream request. `HeaderValue` refuses it.
#[test]
@@ -422,4 +436,49 @@ fn an_mcp_destination_is_read_off_the_proxy_path() {
assert_eq!(McpDestination::from_path("/mcp/a/b"), None);
assert_eq!(McpDestination::from_path("/mcp/"), None);
assert_eq!(McpDestination::from_path("/git/info/refs"), None);
+
+ let key = CustomMcpServerKey::for_url("https://wiki.example.com/mcp");
+ assert_eq!(
+ McpDestination::from_path(&format!("/mcp-custom/{key}")),
+ Some(McpDestination::Custom(key))
+ );
+ assert_eq!(McpDestination::from_path("/mcp-custom/"), None);
+ assert_eq!(McpDestination::from_path("/mcp-custom/not-a-key"), None);
+}
+
+/// The key is the one derivation in this crate, so it has to be the same at
+/// both ends: deterministic, exactly as long as it says, and read back only
+/// in the shape it is written.
+#[test]
+fn a_custom_server_key_is_a_fixed_width_digest_of_the_url() {
+ let key = CustomMcpServerKey::for_url("https://wiki.example.com/mcp");
+
+ assert_eq!(
+ key,
+ CustomMcpServerKey::for_url("https://wiki.example.com/mcp")
+ );
+ assert_ne!(
+ key,
+ CustomMcpServerKey::for_url("https://wiki.example.com/mcp/")
+ );
+ assert_eq!(key.as_str().len(), 32);
+ assert!(
+ key.as_str()
+ .chars()
+ .all(|character| matches!(character, '0'..='9' | 'a'..='f'))
+ );
+ assert_eq!(CustomMcpServerKey::parse(key.as_str()), Some(key.clone()));
+
+ // Rejected rather than repaired: wrong length, wrong case, wrong charset.
+ assert_eq!(CustomMcpServerKey::parse(&key.as_str()[..31]), None);
+ assert_eq!(CustomMcpServerKey::parse(&format!("{key}0")), None);
+ assert_eq!(
+ CustomMcpServerKey::parse(&key.as_str().to_uppercase()),
+ None
+ );
+ assert_eq!(
+ CustomMcpServerKey::parse("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"),
+ None
+ );
+ assert_eq!(CustomMcpServerKey::parse(""), None);
}
diff --git a/crates/agent_egress/src/domain/service.rs b/crates/agent_egress/src/domain/service.rs
index 68df7667f7..0175706afc 100644
--- a/crates/agent_egress/src/domain/service.rs
+++ b/crates/agent_egress/src/domain/service.rs
@@ -6,10 +6,10 @@ use http_body_util::{BodyExt, Full};
use crate::domain::error::EgressError;
use crate::domain::model::{
- EgressTarget, MAX_MCP_REQUEST_BYTES, McpDestination, McpResolution, McpServerSlug,
- ProxyRequest, ProxyResponse, SessionToken, TOOLS_CALL_METHOD, ensure_method_allowed,
- is_macro_staff, not_connected_tool_result, peek_json_rpc, sanitize_request_headers,
- sanitize_response_headers,
+ EgressTarget, MAX_MCP_REQUEST_BYTES, McpDestination, McpResolution, ProxyRequest,
+ ProxyResponse, SessionToken, TOOLS_CALL_METHOD, UpstreamCall, disconnected_tool_result,
+ ensure_method_allowed, is_macro_staff, not_connected_tool_result, peek_json_rpc,
+ sanitize_request_headers, sanitize_response_headers,
};
use crate::domain::observed_body::{ObservedBody, StreamIdentity};
use crate::domain::ports::{Forwarder, GithubTokens, McpCredentials, SessionAuthority};
@@ -140,28 +140,63 @@ where
}
}
- EgressTarget::McpServer(destination @ McpDestination::Macro) => {
- match self.credentials.resolve(&grant.owner, destination).await? {
- McpResolution::Connected(call) | McpResolution::Unconnected(call) => call,
- }
- }
- EgressTarget::McpServer(destination @ McpDestination::Connected(slug)) => {
- match self.credentials.resolve(&grant.owner, destination).await? {
- McpResolution::Connected(call) => call,
- // The owner has no grant for this app, but it can still
- // be addressed for them: the handshake and tool listing
- // go through, and a tool call is answered here with a
- // result the model can act on.
- McpResolution::Unconnected(call) => {
- let name = grant.display_name(slug);
- match Self::answer_unconnected(slug, &name, request).await? {
- Unconnected::Answered(response) => return Ok(response),
- Unconnected::Forward(forwarded) => {
- request = forwarded;
- call
- }
+ EgressTarget::McpServer(
+ destination @ (McpDestination::Macro
+ | McpDestination::Connected(_)
+ | McpDestination::Custom(_)),
+ ) => {
+ let (call, answer): (UpstreamCall, Option<Answer>) = match self
+ .credentials
+ .resolve(&grant.owner, destination)
+ .await?
+ {
+ McpResolution::Connected(call) => (call, None),
+ // The owner has no grant for this app, but it can
+ // still be addressed for them: the handshake and tool
+ // listing go through, and a tool call is answered
+ // here with a result the model can act on. Only a
+ // Pipedream app resolves this way; Macro's own server
+ // has no grant to lack, and a custom server with no
+ // row is refused by its resolver outright.
+ McpResolution::Unconnected(call) => match destination {
+ McpDestination::Connected(slug) => {
+ let name = grant.display_name(slug);
+ let slug = slug.clone();
+ let answer: Answer = Box::new(move |id| {
+ tracing::info!(
+ app = %slug,
+ "answering tools/call for an app the owner has not connected"
+ );
+ not_connected_tool_result(&slug, &name, id)
+ });
+ (call, Some(answer))
}
+ _ => (call, None),
+ },
+ // The owner connected this server once and the grant
+ // has since died. Same treatment: the handshake is
+ // forwarded bare, in case the server allows it, and a
+ // tool call is answered with how to reconnect.
+ McpResolution::Disconnected { call, name } => {
+ let answer: Answer = Box::new(move |id| {
+ tracing::info!(
+ server = %name,
+ "answering tools/call for a server whose grant has died"
+ );
+ disconnected_tool_result(&name, id)
+ });
+ (call, Some(answer))
}
+ };
+ match answer {
+ None => call,
+ Some(answer) => match Self::answer_unconnected(answer, request).await? {
+ Unconnected::Answered(response) => return Ok(response),
+ Unconnected::Forward(forwarded) => {
+ request = forwarded;
+ call
+ }
+ },
}
}
EgressTarget::GitHubGit { endpoint } => {
@@ -207,9 +242,9 @@ where
// upstream sees is the one resolved for the owner, whatever the
// sandbox sent.
sanitize_request_headers(request.headers_mut());
- request
- .headers_mut()
- .insert(AUTHORIZATION, call.authorization().header_value()?);
+ if let Some(authorization) = call.authorization().header_value()? {
+ request.headers_mut().insert(AUTHORIZATION, authorization);
+ }
// Scoping headers are half of the credential - for Pipedream they are
// what says whose account the bearer spends - so they get the same
// treatment: stamped from the resolved call after the strip, never
@@ -266,7 +301,7 @@ where
}
}
-/// What became of a request to an app the owner has not connected.
+/// What became of a request to a server the owner has no live grant for.
enum Unconnected {
/// The proxy answered it itself; nothing goes upstream.
Answered(ProxyResponse),
@@ -275,6 +310,10 @@ enum Unconnected {
Forward(ProxyRequest),
}
+/// How a `tools/call` with the given JSON-RPC id is answered in the
+/// upstream's place.
+type Answer = Box<dyn FnOnce(serde_json::Value) -> ProxyResponse + Send>;
+
impl<Sessions, Credentials, Tokens, Forward>
EgressServiceImpl<Sessions, Credentials, Tokens, Forward>
where
@@ -283,19 +322,18 @@ where
Tokens: GithubTokens,
Forward: Forwarder,
{
- /// An app the owner has not connected: forward everything except
+ /// A server the owner has no live grant for: forward everything except
/// `tools/call`.
///
/// `initialize`, `tools/list`, notifications, the GET event stream and
/// DELETE all go to the upstream addressed for the owner, so the agent's
- /// client completes its handshake and sees the app's real tools from the
- /// first turn. A `tools/call` is answered here with a tool result that
- /// names the app and how to connect it - and the moment the owner does,
- /// the same advertised server resolves as connected and calls flow
- /// through, with nothing re-attached.
+ /// client completes its handshake and sees the server's real tools from
+ /// the first turn. A `tools/call` is answered here with `answer` - a tool
+ /// result that names the server and how to connect it - and the moment
+ /// the owner does, the same advertised server resolves as connected and
+ /// calls flow through, with nothing re-attached.
async fn answer_unconnected(
- slug: &McpServerSlug,
- name: &str,
+ answer: Answer,
request: ProxyRequest,
) -> Result<Unconnected, EgressError> {
if *request.method() != Method::POST {
@@ -321,13 +359,7 @@ where
if let Some(call) = peek_json_rpc(&bytes)
&& call.method == TOOLS_CALL_METHOD
{
- tracing::info!(
- app = %slug,
- "answering tools/call for an app the owner has not connected"
- );
- return Ok(Unconnected::Answered(not_connected_tool_result(
- slug, name, call.id,
- )));
+ return Ok(Unconnected::Answered(answer(call.id)));
}
Ok(Unconnected::Forward(ProxyRequest::from_parts(
diff --git a/crates/agent_egress/src/domain/service/test.rs b/crates/agent_egress/src/domain/service/test.rs
index c9eda96186..7137550fc0 100644
--- a/crates/agent_egress/src/domain/service/test.rs
+++ b/crates/agent_egress/src/domain/service/test.rs
@@ -1,7 +1,8 @@
use super::*;
use crate::domain::model::{
- AgentSessionId, BearerToken, GitEndpoint, GitService, McpDestination, McpServerListing,
- McpServerSlug, ProxyBody, RepoSlug, SessionGrant, UpstreamCall, UpstreamCredential,
+ AgentSessionId, BearerToken, CustomMcpServerKey, GitEndpoint, GitService, McpDestination,
+ McpServerListing, McpServerSlug, ProxyBody, RepoSlug, SessionGrant, UpstreamCall,
+ UpstreamCredential,
};
use http::header::{AUTHORIZATION, HeaderMap, HeaderName, HeaderValue};
use http::{Method, StatusCode};
@@ -75,6 +76,11 @@ enum Knowledge {
Connected,
/// Addressable for the owner, but no grant behind it.
Unconnected,
+ /// Connected once; the grant has since died. Answered bare, with the
+ /// owner's name for the server, the way the custom adapter reports one.
+ Disconnected,
+ /// A server the owner added without an account: connected, dialed bare.
+ Anonymous,
/// Nothing to address at all.
Unknown,
}
@@ -130,6 +136,22 @@ impl SpyCredentials {
}
}
+ /// A custom server whose grant has died.
+ fn disconnected() -> Self {
+ Self {
+ known: Knowledge::Disconnected,
+ ..Self::at(WIKI)
+ }
+ }
+
+ /// A custom server the owner added without connecting an account.
+ fn anonymous() -> Self {
+ Self {
+ known: Knowledge::Anonymous,
+ ..Self::at(WIKI)
+ }
+ }
+
fn empty() -> Self {
Self {
asked: Mutex::default(),
@@ -140,6 +162,9 @@ impl SpyCredentials {
}
}
+/// The URL of the owner's one custom server in these tests.
+const WIKI: &str = "https://wiki.example.com/mcp";
+
impl McpCredentials for SpyCredentials {
async fn resolve(
&self,
@@ -150,6 +175,9 @@ impl McpCredentials for SpyCredentials {
McpDestination::Connected(slug) => slug.clone(),
McpDestination::Preview => unreachable!("preview must never resolve owner credentials"),
McpDestination::Macro => McpServerSlug::parse("macro").expect("slug"),
+ // Recorded under a fixed name: the key is a digest, and the
+ // tests only care that the custom destination was resolved.
+ McpDestination::Custom(_) => McpServerSlug::parse("custom").expect("slug"),
};
self.asked
.lock()
@@ -160,15 +188,25 @@ impl McpCredentials for SpyCredentials {
return Err(EgressError::UnknownServer(slug.clone()));
}
- let call = UpstreamCall::bearer(
- Url::parse(&self.url).expect("url"),
- BearerToken::new("upstream-token"),
- )?
- .scoped_by(self.scope.clone());
+ let url = Url::parse(&self.url).expect("url");
+ if self.known == Knowledge::Anonymous {
+ return Ok(McpResolution::Connected(UpstreamCall::anonymous(url)?));
+ }
+ if self.known == Knowledge::Disconnected {
+ return Ok(McpResolution::Disconnected {
+ call: UpstreamCall::anonymous(url)?,
+ name: "Team wiki".to_owned(),
+ });
+ }
+
+ let call = UpstreamCall::bearer(url, BearerToken::new("upstream-token"))?
+ .scoped_by(self.scope.clone());
Ok(match self.known {
Knowledge::Connected => McpResolution::Connected(call),
Knowledge::Unconnected => McpResolution::Unconnected(call),
- Knowledge::Unknown => unreachable!("returned above"),
+ Knowledge::Disconnected | Knowledge::Anonymous | Knowledge::Unknown => {
+ unreachable!("returned above")
+ }
})
}
}
@@ -513,6 +551,173 @@ fn datadog() -> EgressTarget {
))
}
+fn wiki() -> EgressTarget {
+ EgressTarget::McpServer(McpDestination::Custom(CustomMcpServerKey::for_url(WIKI)))
+}
+
+/// A custom server with a live grant proxies like a connected app: the
+/// owner's bearer is stamped on in place of the session token.
+#[tokio::test]
+async fn a_connected_custom_server_is_forwarded_with_the_owners_bearer() {
+ let service = EgressServiceImpl::new(
+ StubSessions::granting(),
+ SpyCredentials::at(WIKI),
+ SpyGithubTokens::default(),
+ SpyForwarder::answering(&[]),
+ );
+
+ let response = service
+ .proxy(
+ &SessionToken::new("session-token"),
+ wiki(),
+ json_request(r#"{"jsonrpc":"2.0","id":1,"method":"tools/call"}"#),
+ )
+ .await
+ .expect("proxied");
+
+ assert_eq!(response.status(), StatusCode::ACCEPTED);
+ assert_eq!(
+ *service.credentials.asked.lock().expect("lock"),
+ [(owner().to_string(), "custom".to_owned())]
+ );
+ service.forward.forwarded(|parts| {
+ assert_eq!(parts.uri.to_string(), WIKI);
+ assert_eq!(
+ parts
+ .headers
+ .get_all(AUTHORIZATION)
+ .iter()
+ .collect::<Vec<_>>(),
+ ["Bearer upstream-token"]
+ );
+ });
+}
+
+/// A custom server the owner added without connecting an account is dialed
+/// bare - and bare means bare: the session token is stripped and nothing is
+/// stamped in its place.
+#[tokio::test]
+async fn an_anonymous_custom_server_is_forwarded_without_any_authorization() {
+ let service = EgressServiceImpl::new(
+ StubSessions::granting(),
+ SpyCredentials::anonymous(),
+ SpyGithubTokens::default(),
+ SpyForwarder::answering(&[]),
+ );
+
+ let response = service
+ .proxy(
+ &SessionToken::new("session-token"),
+ wiki(),
+ request(
+ Method::POST,
+ &[
+ ("authorization", "Bearer session-token"),
+ ("mcp-session-id", "s1"),
+ ],
+ ),
+ )
+ .await
+ .expect("proxied");
+
+ assert_eq!(response.status(), StatusCode::ACCEPTED);
+ service.forward.forwarded(|parts| {
+ assert_eq!(parts.uri.to_string(), WIKI);
+ assert!(parts.headers.get(AUTHORIZATION).is_none());
+ assert_eq!(parts.headers["mcp-session-id"], "s1");
+ });
+}
+
+/// A `tools/call` to a custom server whose grant has died is answered by the
+/// proxy under the owner's own name for the server, telling the model how
+/// to get it reconnected; nothing reaches the upstream, and there is no
+/// connect chip, since that chip opens the Pipedream catalog.
+#[tokio::test]
+async fn a_tools_call_to_a_disconnected_custom_server_is_answered_locally() {
+ let service = EgressServiceImpl::new(
+ StubSessions::granting(),
+ SpyCredentials::disconnected(),
+ SpyGithubTokens::default(),
+ SpyForwarder::answering(&[]),
+ );
+
+ let response = service
+ .proxy(
+ &SessionToken::new("token"),
+ wiki(),
+ json_request(
+ r#"{"jsonrpc":"2.0","id":"call-9","method":"tools/call","params":{"name":"search","arguments":{"secret":"do-not-echo"}}}"#,
+ ),
+ )
+ .await
+ .expect("answered");
+
+ assert!(!service.forward.was_called());
+ assert_eq!(response.status(), StatusCode::OK);
+ let body = body_json(response).await;
+ assert_eq!(body["id"], "call-9");
+ assert_eq!(body["result"]["isError"], true);
+ let text = body["result"]["content"][0]["text"]
+ .as_str()
+ .expect("a text content block");
+ assert!(text.contains("Team wiki is a custom MCP server"), "{text}");
+ assert!(text.contains("Reconnect"), "{text}");
+ assert!(!text.contains("m-connect-app"), "{text}");
+ assert!(!text.contains("do-not-echo"), "{text}");
+}
+
+/// Everything but `tools/call` is forwarded bare for a disconnected custom
+/// server, so a server that allows an anonymous handshake still lists its
+/// tools and the model gets to see the reconnect answer.
+#[tokio::test]
+async fn the_handshake_of_a_disconnected_custom_server_is_forwarded_bare() {
+ let service = EgressServiceImpl::new(
+ StubSessions::granting(),
+ SpyCredentials::disconnected(),
+ SpyGithubTokens::default(),
+ SpyForwarder::answering(&[]),
+ );
+
+ let response = service
+ .proxy(
+ &SessionToken::new("token"),
+ wiki(),
+ json_request(r#"{"jsonrpc":"2.0","id":1,"method":"tools/list"}"#),
+ )
+ .await
+ .expect("forwarded");
+
+ assert_eq!(response.status(), StatusCode::ACCEPTED);
+ service.forward.forwarded(|parts| {
+ assert_eq!(parts.uri.to_string(), WIKI);
+ assert!(parts.headers.get(AUTHORIZATION).is_none());
+ });
+}
+
+/// The resolver's refusal of a key that names none of the owner's servers
+/// passes through, and nothing is forwarded.
+#[tokio::test]
+async fn an_unknown_custom_server_is_refused() {
+ let service = EgressServiceImpl::new(
+ StubSessions::granting(),
+ SpyCredentials::empty(),
+ SpyGithubTokens::default(),
+ SpyForwarder::answering(&[]),
+ );
+
+ let error = service
+ .proxy(
+ &SessionToken::new("token"),
+ wiki(),
+ request(Method::POST, &[]),
+ )
+ .await
+ .expect_err("refused");
+
+ assert!(matches!(error, EgressError::UnknownServer(_)));
+ assert!(!service.forward.was_called());
+}
+
#[tokio::test]
async fn addresses_the_request_at_the_resolved_upstream_and_passes_the_status_back() {
let service = EgressServiceImpl::new(
@@ -877,6 +1082,7 @@ async fn replaces_the_sandboxs_basic_credential_on_a_git_request() {
}
.header_value()
.expect("header value")
+ .expect("a credential to stamp")
.to_str()
.expect("ascii")],
"exactly one credential",
diff --git a/crates/agent_egress/src/inbound/axum_router.rs b/crates/agent_egress/src/inbound/axum_router.rs
index 1b27c2bfc4..3011445c49 100644
--- a/crates/agent_egress/src/inbound/axum_router.rs
+++ b/crates/agent_egress/src/inbound/axum_router.rs
@@ -19,8 +19,8 @@ use std::sync::Arc;
use crate::domain::error::EgressError;
use crate::domain::model::{
- BoxError, EgressTarget, GitEndpoint, McpDestination, McpServerSlug, ProxyRequest,
- ProxyResponse, SessionToken,
+ BoxError, CustomMcpServerKey, EgressTarget, GitEndpoint, McpDestination, McpServerSlug,
+ ProxyRequest, ProxyResponse, SessionToken,
};
use crate::domain::service::EgressService;
@@ -64,6 +64,7 @@ where
Router::new()
.route("/health", get(health))
.route("/mcp/{slug}", any(mcp_handler::<Service>))
+ .route("/mcp-custom/{key}", any(custom_mcp_handler::<Service>))
.route("/mcp-macro", any(macro_mcp_handler::<Service>))
.route("/mcp-preview", any(preview_mcp_handler::<Service>))
.route(
@@ -98,6 +99,23 @@ where
mcp_proxy(state, McpDestination::Connected(slug), request).await
}
+/// One of the owner's custom servers, on its own route: a key is not a slug,
+/// and keeping the two apart means neither can be mistaken for the other.
+#[tracing::instrument(skip_all, err, fields(%key))]
+async fn custom_mcp_handler<Service>(
+ State(state): State<EgressRouterState<Service>>,
+ Path(key): Path<String>,
+ request: Request,
+) -> Result<Response, EgressError>
+where
+ Service: EgressService,
+{
+ let key = CustomMcpServerKey::parse(&key)
+ .ok_or_else(|| EgressError::Unroutable(format!("{key} is not a server key")))?;
+
+ mcp_proxy(state, McpDestination::Custom(key), request).await
+}
+
#[tracing::instrument(skip_all, err)]
async fn preview_mcp_handler<Service: EgressService>(
State(state): State<EgressRouterState<Service>>,
@@ -253,7 +271,9 @@ impl IntoResponse for EgressError {
fn into_response(self) -> Response {
let status = match &self {
Self::Unauthenticated(_) | Self::SessionClosed => StatusCode::UNAUTHORIZED,
- Self::UnknownServer(_) | Self::Unroutable(_) => StatusCode::NOT_FOUND,
+ Self::UnknownServer(_) | Self::UnknownCustomServer(_) | Self::Unroutable(_) => {
+ StatusCode::NOT_FOUND
+ }
Self::RepoUnavailable(_) => StatusCode::FORBIDDEN,
Self::MethodNotAllowed(_) => StatusCode::METHOD_NOT_ALLOWED,
Self::RequestTooLarge => StatusCode::PAYLOAD_TOO_LARGE,
@@ -276,7 +296,9 @@ impl IntoResponse for EgressError {
}
Self::SessionClosed => "This session is no longer open.",
Self::Unroutable(_) => "Nothing is served at that path.",
- Self::UnknownServer(_) => "No such connected MCP server.",
+ Self::UnknownServer(_) | Self::UnknownCustomServer(_) => {
+ "No such connected MCP server."
+ }
Self::RepoUnavailable(_) => {
"This session's repository is not reachable with Macro's GitHub App."
}
diff --git a/crates/agent_egress/src/inbound/axum_router/test.rs b/crates/agent_egress/src/inbound/axum_router/test.rs
index c6a852b389..972d042894 100644
--- a/crates/agent_egress/src/inbound/axum_router/test.rs
+++ b/crates/agent_egress/src/inbound/axum_router/test.rs
@@ -76,6 +76,9 @@ impl EgressService for SpyService {
}
Some(EgressError::SessionClosed) => Err(EgressError::SessionClosed),
Some(EgressError::RequestTooLarge) => Err(EgressError::RequestTooLarge),
+ Some(EgressError::UnknownCustomServer(key)) => {
+ Err(EgressError::UnknownCustomServer(key.clone()))
+ }
Some(EgressError::Upstream(_)) => {
Err(EgressError::Upstream(rootcause::report!("unreachable")))
}
@@ -129,6 +132,38 @@ async fn routes_a_slug_to_an_mcp_target() {
);
}
+/// A custom server's key rides its own route, and only a key-shaped segment
+/// is routed: anything else is "nothing served here", with no hint about
+/// which of the two it was.
+#[tokio::test]
+async fn routes_a_key_to_a_custom_mcp_target() {
+ let key = CustomMcpServerKey::for_url("https://wiki.example.com/mcp");
+ let service = SpyService::accepting();
+ let response = call(
+ &service,
+ get(&format!("/mcp-custom/{key}"), Some("Bearer session")),
+ )
+ .await;
+
+ assert_eq!(response.status(), StatusCode::ACCEPTED);
+ assert_eq!(
+ service.targets(),
+ [EgressTarget::McpServer(McpDestination::Custom(key))]
+ );
+
+ let response = call(
+ &service,
+ get("/mcp-custom/not-a-key", Some("Bearer session")),
+ )
+ .await;
+ assert_eq!(response.status(), StatusCode::NOT_FOUND);
+ assert_eq!(
+ service.targets().len(),
+ 1,
+ "an unparseable key never reaches the service"
+ );
+}
+
/// git speaks to us through a credential helper, which can only present a
/// token as a Basic password. Both spellings have to reach the same session.
#[tokio::test]
@@ -213,6 +248,10 @@ async fn maps_each_refusal_to_the_status_that_tells_the_agent_what_to_do() {
StatusCode::BAD_GATEWAY,
),
(EgressError::RequestTooLarge, StatusCode::PAYLOAD_TOO_LARGE),
+ (
+ EgressError::UnknownCustomServer(CustomMcpServerKey::for_url("https://x.example")),
+ StatusCode::NOT_FOUND,
+ ),
] {
let service = SpyService::refusing(error);
let response = call(&service, get("/mcp/datadog", Some("Bearer session"))).await;
diff --git a/crates/agent_egress/src/lib.rs b/crates/agent_egress/src/lib.rs
index 34c8130034..eceb68924c 100644
--- a/crates/agent_egress/src/lib.rs
+++ b/crates/agent_egress/src/lib.rs
@@ -24,7 +24,9 @@
//! 3. one of two credential ports resolves the destination:
//! [`ports::McpCredentials`] finds the named app among *that owner's*
//! Pipedream connections and produces our project bearer plus the
-//! `x-pd-*` headers that pin it to that owner and app, or
+//! `x-pd-*` headers that pin it to that owner and app - or, for a server
+//! the owner added by URL, finds it among *that owner's* custom servers
+//! and produces a fresh bearer from the stored OAuth grant - or
//! [`ports::GithubTokens`] mints an installation token scoped to the
//! session's own repository
//! 4. the service strips the sandbox's own credentials from the request and
@@ -40,6 +42,9 @@
//! the owner's own rows, so it cannot ask to act as anyone its owner is not -
//! which matters doubly here, because the Pipedream bearer alone could act as
//! anyone, and the header saying *who* is stamped from the session's grant.
+//! For a custom server it names a digest of the server's URL, which likewise
+//! resolves only through the owner's own rows: the URL and the grant stay
+//! here, and the sandbox cannot point the proxy at a URL of its choosing.
//! For git it names nothing at all: the repository comes from the session's
//! grant, and the endpoint from a three-entry allowlist.
diff --git a/crates/agent_egress/src/outbound/custom_mcp.rs b/crates/agent_egress/src/outbound/custom_mcp.rs
new file mode 100644
index 0000000000..38cad2135f
--- /dev/null
+++ b/crates/agent_egress/src/outbound/custom_mcp.rs
@@ -0,0 +1,139 @@
+//! Resolving a key to one of the owner's custom MCP servers.
+//!
+//! A custom server is one the owner added by URL under Agents → Connections,
+//! with Macro's own OAuth client holding the grant - the native stack in
+//! `mcp_client`, as opposed to the Pipedream one. The rows carry the owner's
+//! encrypted tokens, which is exactly why this traffic must flow through the
+//! proxy: the token is the owner's, the sandbox must never hold it, and
+//! somebody has to refresh it when it expires.
+//!
+//! Both halves come from `mcp_client`'s own port and service - the same rows
+//! the chat tool path reads, the same refresh-and-persist the in-process
+//! connector runs - so a server connected in Macro is a server the sandbox
+//! can reach, with nothing to keep in sync.
+
+use macro_user_id::user_id::MacroUserIdStr;
+use mcp_client::domain::models::McpServerRecord;
+use mcp_client::domain::ports::McpServerStore;
+use mcp_client::domain::service::{ServerAccess, server_access};
+use std::sync::Arc;
+use url::Url;
+
+use crate::domain::error::EgressError;
+use crate::domain::model::{
+ BearerToken, CustomMcpServerKey, McpDestination, McpResolution, UpstreamCall,
+};
+use crate::domain::ports::McpCredentials;
+
+#[cfg(test)]
+mod test;
+
+/// Layers the owner's custom MCP servers over another resolver.
+///
+/// [`McpDestination::Custom`] is answered here; everything else passes
+/// straight through to `Inner`. The destinations arrive from different
+/// routes, so there is no name for a connected app to collide with.
+pub struct WithCustomMcp<Inner, Servers> {
+ inner: Inner,
+ servers: Arc<Servers>,
+}
+
+impl<Inner, Servers> WithCustomMcp<Inner, Servers>
+where
+ Inner: McpCredentials,
+ Servers: McpServerStore,
+{
+ /// Wrap `inner`, answering [`McpDestination::Custom`] from the rows in
+ /// `servers`.
+ pub fn new(inner: Inner, servers: Arc<Servers>) -> Self {
+ Self { inner, servers }
+ }
+
+ /// The owner's enabled server whose URL hashes to `key`, if they hold
+ /// one.
+ ///
+ /// Scoped to the owner by the store call itself, and filtered to
+ /// `enabled` here because the store does not: a disabled server is one
+ /// the owner turned off, and turning it off has to take the sandbox's
+ /// access with it.
+ async fn record(
+ &self,
+ owner: &MacroUserIdStr<'static>,
+ key: &CustomMcpServerKey,
+ ) -> Result<Option<McpServerRecord>, EgressError> {
+ Ok(self
+ .servers
+ .list(owner)
+ .await
+ .map_err(|error| {
+ EgressError::Internal(rootcause::report!(
+ "could not list custom MCP servers: {error:?}"
+ ))
+ })?
+ .into_iter()
+ .filter(|record| record.enabled)
+ // The same derivation the provisioner advertised, over the same
+ // column; equality is the whole match.
+ .find(|record| CustomMcpServerKey::for_url(&record.url) == *key))
+ }
+}
+
+impl<Inner, Servers> McpCredentials for WithCustomMcp<Inner, Servers>
+where
+ Inner: McpCredentials,
+ Servers: McpServerStore,
+{
+ #[tracing::instrument(skip_all, err, fields(%owner, ?destination))]
+ async fn resolve(
+ &self,
+ owner: &MacroUserIdStr<'static>,
+ destination: &McpDestination,
+ ) -> Result<McpResolution, EgressError> {
+ let McpDestination::Custom(key) = destination else {
+ return self.inner.resolve(owner, destination).await;
+ };
+
+ // Unlike a Pipedream slug there is nothing to address without a row:
+ // the URL *is* the row.
+ let record = self
+ .record(owner, key)
+ .await?
+ .ok_or_else(|| EgressError::UnknownCustomServer(key.clone()))?;
+
+ // The row's URL is the owner's typing, stored verbatim. One that is
+ // not a URL could never have been connected, so it is a broken row
+ // rather than a refusal of anything the sandbox asked for.
+ let url = Url::parse(&record.url).map_err(|error| {
+ EgressError::Internal(rootcause::report!(
+ "custom MCP server {} has a URL that does not parse: {error}",
+ record.server_name
+ ))
+ })?;
+
+ // Token freshness is the service's: a live token comes straight off
+ // the row, an expiring one is refreshed and the rotated grant written
+ // back for whoever reads it next.
+ let access = server_access(&record, Arc::clone(&self.servers))
+ .await
+ .map_err(|error| {
+ EgressError::Upstream(rootcause::report!(
+ "could not refresh the grant for custom MCP server {}: {error}",
+ record.server_name
+ ))
+ })?;
+
+ // Typed at the source, but not yet vetted: `UpstreamCall`'s
+ // constructors are what refuse a non-https endpoint, and they are the
+ // only way to pair this URL with a credential - or with none.
+ Ok(match access {
+ ServerAccess::Anonymous => McpResolution::Connected(UpstreamCall::anonymous(url)?),
+ ServerAccess::Bearer(token) => {
+ McpResolution::Connected(UpstreamCall::bearer(url, BearerToken::new(token))?)
+ }
+ ServerAccess::AuthorizationRequired => McpResolution::Disconnected {
+ call: UpstreamCall::anonymous(url)?,
+ name: record.server_name,
+ },
+ })
+ }
+}
diff --git a/crates/agent_egress/src/outbound/custom_mcp/test.rs b/crates/agent_egress/src/outbound/custom_mcp/test.rs
new file mode 100644
index 0000000000..d7fddc47cd
--- /dev/null
+++ b/crates/agent_egress/src/outbound/custom_mcp/test.rs
@@ -0,0 +1,257 @@
+use super::*;
+use crate::domain::model::{McpServerSlug, UpstreamCredential};
+use mcp_client::domain::models::StoredCredentials;
+use rmcp::transport::auth::OAuthTokenResponse;
+use std::sync::Mutex;
+use std::time::{SystemTime, UNIX_EPOCH};
+
+fn owner() -> MacroUserIdStr<'static> {
+ MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id")
+}
+
+fn connected(slug: &str) -> McpDestination {
+ McpDestination::Connected(McpServerSlug::parse(slug).expect("a valid slug"))
+}
+
+fn custom(url: &str) -> McpDestination {
+ McpDestination::Custom(CustomMcpServerKey::for_url(url))
+}
+
+/// Records what reached it; the tests only care that delegation happened.
+#[derive(Default)]
+struct SpyInner {
+ asked: Mutex<Vec<String>>,
+}
+
+impl McpCredentials for &SpyInner {
+ async fn resolve(
+ &self,
+ _owner: &MacroUserIdStr<'static>,
+ destination: &McpDestination,
+ ) -> Result<McpResolution, EgressError> {
+ let McpDestination::Connected(slug) = destination else {
+ unreachable!("the decorator answers custom destinations itself");
+ };
+ self.asked.lock().expect("lock").push(slug.to_string());
+ Err(EgressError::UnknownServer(slug.clone()))
+ }
+}
+
+/// The owner's rows, verbatim. Lists only: resolution never writes unless a
+/// token is refreshed, and nothing here is near expiry.
+struct FixedServers {
+ records: Vec<McpServerRecord>,
+ listed_for: Mutex<Vec<String>>,
+}
+
+impl FixedServers {
+ fn holding(records: Vec<McpServerRecord>) -> Arc<Self> {
+ Arc::new(Self {
+ records,
+ listed_for: Mutex::default(),
+ })
+ }
+}
+
+impl McpServerStore for FixedServers {
+ type Err = std::convert::Infallible;
+
+ async fn save(&self, _record: &McpServerRecord) -> Result<(), Self::Err> {
+ unreachable!("nothing here refreshes a token")
+ }
+
+ async fn load(
+ &self,
+ _user_id: &MacroUserIdStr<'static>,
+ _server_url: &str,
+ ) -> Result<Option<McpServerRecord>, Self::Err> {
+ unreachable!("resolution lists, never loads one")
+ }
+
+ async fn delete(
+ &self,
+ _user_id: &MacroUserIdStr<'static>,
+ _server_url: &str,
+ ) -> Result<(), Self::Err> {
+ unreachable!("resolution never deletes")
+ }
+
+ async fn list(
+ &self,
+ user_id: &MacroUserIdStr<'static>,
+ ) -> Result<Vec<McpServerRecord>, Self::Err> {
+ self.listed_for
+ .lock()
+ .expect("lock")
+ .push(user_id.to_string());
+ Ok(self.records.clone())
+ }
+}
+
+fn record(url: &str, name: &str, credentials: Option<StoredCredentials>) -> McpServerRecord {
+ McpServerRecord {
+ user_id: owner(),
+ url: url.to_owned(),
+ server_name: name.to_owned(),
+ credentials,
+ enabled: true,
+ }
+}
+
+fn now_epoch_secs() -> u64 {
+ SystemTime::now()
+ .duration_since(UNIX_EPOCH)
+ .expect("after the epoch")
+ .as_secs()
+}
+
+/// A grant whose access token has an hour left.
+fn live_grant(access_token: &str) -> StoredCredentials {
+ let response: OAuthTokenResponse = serde_json::from_value(serde_json::json!({
+ "access_token": access_token,
+ "token_type": "bearer",
+ "expires_in": 3600,
+ }))
+ .expect("a token response");
+ StoredCredentials::new(
+ "client-id".to_owned(),
+ Some(response),
+ Vec::new(),
+ Some(now_epoch_secs()),
+ )
+}
+
+/// An authorization that registered a client and never exchanged a code:
+/// nothing to present, and nothing to refresh with.
+fn dead_grant() -> StoredCredentials {
+ StoredCredentials::new("client-id".to_owned(), None, Vec::new(), Some(1))
+}
+
+const WIKI: &str = "https://wiki.example.com/mcp";
+
+#[tokio::test]
+async fn a_connected_server_resolves_to_its_url_with_the_owners_bearer() {
+ let servers = FixedServers::holding(vec![record(WIKI, "Wiki", Some(live_grant("wiki-token")))]);
+ let inner = SpyInner::default();
+ let credentials = WithCustomMcp::new(&inner, Arc::clone(&servers));
+
+ let McpResolution::Connected(call) = credentials
+ .resolve(&owner(), &custom(WIKI))
+ .await
+ .expect("resolved")
+ else {
+ panic!("a live grant is connected");
+ };
+
+ assert_eq!(call.url().as_str(), WIKI);
+ assert_eq!(
+ *call.authorization(),
+ UpstreamCredential::Bearer(BearerToken::new("wiki-token"))
+ );
+ assert!(call.scope_headers().is_empty());
+ assert_eq!(
+ *servers.listed_for.lock().expect("lock"),
+ [owner().to_string()]
+ );
+ assert!(inner.asked.lock().expect("lock").is_empty());
+}
+
+/// A server added without connecting an account is dialed bare, as the
+/// in-process client dials it; the proxy still strips the session token.
+#[tokio::test]
+async fn a_server_with_no_account_resolves_anonymously() {
+ let servers = FixedServers::holding(vec![record(WIKI, "Wiki", None)]);
+ let inner = SpyInner::default();
+ let credentials = WithCustomMcp::new(&inner, servers);
+
+ let McpResolution::Connected(call) = credentials
+ .resolve(&owner(), &custom(WIKI))
+ .await
+ .expect("resolved")
+ else {
+ panic!("a bare server is connected");
+ };
+
+ assert_eq!(*call.authorization(), UpstreamCredential::Anonymous);
+}
+
+/// A grant that yields no token is reported as disconnected, with the
+/// owner's own name for the server so the model can say which to reconnect.
+#[tokio::test]
+async fn a_dead_grant_is_reported_disconnected_by_name() {
+ let servers = FixedServers::holding(vec![record(WIKI, "Team wiki", Some(dead_grant()))]);
+ let inner = SpyInner::default();
+ let credentials = WithCustomMcp::new(&inner, servers);
+
+ let McpResolution::Disconnected { call, name } = credentials
+ .resolve(&owner(), &custom(WIKI))
+ .await
+ .expect("resolved")
+ else {
+ panic!("a dead grant is disconnected");
+ };
+
+ assert_eq!(name, "Team wiki");
+ assert_eq!(call.url().as_str(), WIKI);
+ assert_eq!(*call.authorization(), UpstreamCredential::Anonymous);
+}
+
+/// Only the owner's rows answer, and only enabled ones: a key that matches
+/// nobody's row, or a row the owner turned off, has nowhere to go.
+#[tokio::test]
+async fn an_unknown_or_disabled_key_is_refused() {
+ let mut disabled = record(WIKI, "Wiki", None);
+ disabled.enabled = false;
+ let servers = FixedServers::holding(vec![disabled]);
+ let inner = SpyInner::default();
+ let credentials = WithCustomMcp::new(&inner, servers);
+
+ let refusal = credentials
+ .resolve(&owner(), &custom(WIKI))
+ .await
+ .expect_err("disabled");
+ assert!(
+ matches!(refusal, EgressError::UnknownCustomServer(key) if key == CustomMcpServerKey::for_url(WIKI))
+ );
+
+ let refusal = credentials
+ .resolve(&owner(), &custom("https://nobody.example.com/mcp"))
+ .await
+ .expect_err("unknown");
+ assert!(matches!(refusal, EgressError::UnknownCustomServer(_)));
+}
+
+/// The owner typed the URL, so https is not an assumption: a cleartext
+/// server is refused rather than having a token put on the wire for it.
+#[tokio::test]
+async fn a_cleartext_server_is_refused() {
+ const PLAIN: &str = "http://wiki.example.com/mcp";
+ let servers = FixedServers::holding(vec![record(PLAIN, "Wiki", Some(live_grant("t")))]);
+ let inner = SpyInner::default();
+ let credentials = WithCustomMcp::new(&inner, servers);
+
+ let refusal = credentials
+ .resolve(&owner(), &custom(PLAIN))
+ .await
+ .expect_err("cleartext");
+ assert!(matches!(refusal, EgressError::InsecureUpstream(_)));
+}
+
+#[tokio::test]
+async fn every_other_destination_delegates_to_the_inner_resolver() {
+ let servers = FixedServers::holding(vec![record(WIKI, "Wiki", None)]);
+ let inner = SpyInner::default();
+ let credentials = WithCustomMcp::new(&inner, Arc::clone(&servers));
+
+ let refusal = credentials
+ .resolve(&owner(), &connected("linear"))
+ .await
+ .expect_err("the spy refuses everything");
+
+ assert!(matches!(refusal, EgressError::UnknownServer(_)));
+ assert_eq!(*inner.asked.lock().expect("lock"), ["linear"]);
+ assert!(
+ servers.listed_for.lock().expect("lock").is_empty(),
+ "a Pipedream slug must never read the custom rows"
+ );
+}
diff --git a/crates/agent_egress/src/outbound/mod.rs b/crates/agent_egress/src/outbound/mod.rs
index e4491a4ef9..ea79c5da99 100644
--- a/crates/agent_egress/src/outbound/mod.rs
+++ b/crates/agent_egress/src/outbound/mod.rs
@@ -3,6 +3,9 @@
/// Minting a scoped GitHub App installation credential.
pub mod github_tokens;
+/// Resolving an owner's custom (URL-added) MCP servers to upstream calls.
+pub mod custom_mcp;
+
/// Answering the reserved `macro` slug with Macro's own MCP server.
pub mod macro_mcp;
diff --git a/crates/agent_harness/Cargo.toml b/crates/agent_harness/Cargo.toml
index 4e6c3dc98b..951e0885fe 100644
--- a/crates/agent_harness/Cargo.toml
+++ b/crates/agent_harness/Cargo.toml
@@ -43,6 +43,8 @@ github = { path = "../github", default-features = false, features = [
] }
kafka_util = { path = "../kafka_util" }
macro_env_var = { path = "../macro_env_var" }
+# The owner's custom MCP server rows, listed so a sandbox can be told they exist.
+mcp_client = { path = "../mcp_client" }
pipedream_mcp = { path = "../pipedream_mcp" }
macro_event_broker = { path = "../macro_event_broker", features = ["outbound"] }
macro_authorization = { path = "../macro_authorization", features = ["axum"] }
diff --git a/crates/agent_harness/src/domain/model.rs b/crates/agent_harness/src/domain/model.rs
index d48dd03ed0..9b8d4ed479 100644
--- a/crates/agent_harness/src/domain/model.rs
+++ b/crates/agent_harness/src/domain/model.rs
@@ -1,7 +1,9 @@
//! Commands and values used by the harness domain.
use agent_client_protocol::schema::v1::{HttpHeader, McpServer as AcpMcpServer, McpServerHttp};
-use agent_egress::domain::model::{McpServerSlug, RepoSlug};
+use agent_egress::domain::model::{
+ CUSTOM_MCP_PATH_PREFIX, CustomMcpServerKey, CustomMcpServerListing, McpServerSlug, RepoSlug,
+};
use agent_fold::domain::model::{StopReason, TurnSignal};
use agent_runtime_protocol::domain::action::{AgentAction, AgentActionId, PromptAttachment};
use agent_session::domain::model::{AgentMcpServers, AgentSessionId, MessageId, SandboxSize};
@@ -804,6 +806,10 @@ pub struct SandboxEgress {
/// the owner has connected them. Macro's own server is not listed: every
/// session has it, on its own route.
pub mcp_servers: Vec<McpServerSlug>,
+ /// The owner's own MCP servers - the ones added by URL under "Custom
+ /// MCP" - by the key the proxy resolves. The sandbox never sees a
+ /// server's URL or credential; both stay behind the key.
+ pub custom_servers: Vec<CustomMcpServerListing>,
}
/// Where the sandbox finds the egress proxy.
@@ -840,6 +846,11 @@ impl SandboxEgress {
format!("{}/mcp-macro", self.base_url)
}
+ /// Where the proxy serves the owner's custom server behind `key`.
+ pub fn custom_mcp_url(&self, key: &CustomMcpServerKey) -> String {
+ format!("{}{CUSTOM_MCP_PATH_PREFIX}{key}", self.base_url)
+ }
+
/// The `Authorization` value presented on every proxied call.
pub fn authorization_header(&self) -> String {
format!("Bearer {}", self.session_token)
@@ -859,13 +870,19 @@ impl SandboxEgress {
]
}
- /// The workspace and internal MCP servers, followed by the owner's apps,
- /// as `(name, url)` pairs.
+ /// The workspace and internal MCP servers, followed by the owner's apps
+ /// and then the owner's custom servers, as `(name, url)` pairs.
///
/// The one enumeration behind both renderings - [`Self::acp_servers`] and
/// the Cursor API's - so the two can never advertise different sets.
+ ///
+ /// Every name is unique and non-empty, because the runtimes key servers
+ /// by name and refuse a list that repeats one. App slugs already are; a
+ /// custom server's name is whatever the owner typed, so it is reduced to
+ /// the characters tool namespaces tolerate and, if that still collides
+ /// with an earlier entry, suffixed with a piece of its key.
pub fn server_entries(&self) -> impl Iterator<Item = (String, String)> + '_ {
- [
+ let mut entries: Vec<(String, String)> = vec![
(MACRO_MCP_NAME.to_owned(), self.macro_mcp_url()),
(
INTERNAL_MCP_NAME.to_owned(),
@@ -875,13 +892,17 @@ impl SandboxEgress {
"macro-preview".to_owned(),
format!("{}/mcp-preview", self.base_url),
),
- ]
- .into_iter()
- .chain(
+ ];
+ entries.extend(
self.mcp_servers
.iter()
.map(|slug| (slug.as_str().to_owned(), self.mcp_url(slug))),
- )
+ );
+ for listing in &self.custom_servers {
+ let name = unique_server_name(&listing.name, &listing.key, &entries);
+ entries.push((name, self.custom_mcp_url(&listing.key)));
+ }
+ entries.into_iter()
}
/// Session-scoped internal tools, also supplied to external runtimes.
@@ -941,6 +962,42 @@ pub struct ProvisionedEgress {
pub sandbox: SandboxEgress,
}
+/// The name a custom server is advertised under among `taken` entries.
+///
+/// Owner-typed names are reduced to `[A-Za-z0-9_-]`, which is what agents
+/// accept in a tool namespace, and an empty result reads as `custom`. A name
+/// that then matches an earlier entry gets a key suffix, so two servers the
+/// owner named alike stay distinguishable and neither can shadow Macro's own.
+fn unique_server_name(name: &str, key: &CustomMcpServerKey, taken: &[(String, String)]) -> String {
+ const DEFAULT_NAME: &str = "custom";
+ const KEY_SUFFIX_LEN: usize = 8;
+
+ let mut sanitized: String = name
+ .trim()
+ .chars()
+ .map(|character| {
+ if character.is_ascii_alphanumeric() || matches!(character, '_' | '-') {
+ character
+ } else {
+ '_'
+ }
+ })
+ .collect();
+ if sanitized.is_empty() {
+ sanitized = DEFAULT_NAME.to_owned();
+ }
+ let is_taken = |candidate: &str| taken.iter().any(|(existing, _)| existing == candidate);
+ if !is_taken(&sanitized) {
+ return sanitized;
+ }
+ let key = key.as_str();
+ let suffixed = format!("{sanitized}_{}", &key[..KEY_SUFFIX_LEN.min(key.len())]);
+ if !is_taken(&suffixed) {
+ return suffixed;
+ }
+ format!("{sanitized}_{key}")
+}
+
impl std::fmt::Debug for SandboxEgress {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
@@ -948,6 +1005,7 @@ impl std::fmt::Debug for SandboxEgress {
.field("base_url", &self.base_url)
.field("session_token", &"[REDACTED]")
.field("mcp_servers", &self.mcp_servers)
+ .field("custom_servers", &self.custom_servers)
.finish()
}
}
diff --git a/crates/agent_harness/src/outbound/egress.rs b/crates/agent_harness/src/outbound/egress.rs
index edfdf40339..0bde1ac1b5 100644
--- a/crates/agent_harness/src/outbound/egress.rs
+++ b/crates/agent_harness/src/outbound/egress.rs
@@ -1,7 +1,8 @@
//! Handing a sandbox its one secret, and telling opencode where to spend it.
//!
-//! Two adapter concerns the domain has no business with: minting a token, and
-//! listing the owner's Pipedream-connected apps, which needs their rows.
+//! Three adapter concerns the domain has no business with: minting a token,
+//! listing the owner's Pipedream-connected apps, and listing the owner's own
+//! custom MCP servers - the last two need their rows.
//!
//! Both the minting and the hashing are `agent_egress`'s, not a second
//! implementation of either. The two ends of this token are written in
@@ -9,8 +10,11 @@
//! each end reads at runtime as "every request from every sandbox is
//! unauthenticated".
-use agent_egress::domain::model::{McpServerSlug, SessionToken};
+use agent_egress::domain::model::{
+ CustomMcpServerKey, CustomMcpServerListing, McpServerSlug, SessionToken,
+};
use macro_user_id::user_id::MacroUserIdStr;
+use mcp_client::domain::ports::McpServerStore;
use pipedream_mcp::domain::ports::ConnectionStore;
use std::sync::Arc;
@@ -23,21 +27,28 @@ use agent_session::domain::model::{AgentMcpServers, AgentSessionId};
mod test;
/// Mints session tokens and gathers the MCP servers a sandbox may dial.
-pub struct EgressProvisioner<Connections> {
+pub struct EgressProvisioner<Connections, Servers> {
connections: Arc<Connections>,
+ custom_servers: Arc<Servers>,
base_url: String,
external_base_url: Option<String>,
}
-impl<Connections> EgressProvisioner<Connections>
+impl<Connections, Servers> EgressProvisioner<Connections, Servers>
where
Connections: ConnectionStore,
+ Servers: McpServerStore,
{
- /// Build the provisioner over the Pipedream connection store and the
- /// egress proxy's public address.
- pub fn new(connections: Arc<Connections>, base_url: impl Into<String>) -> Self {
+ /// Build the provisioner over the Pipedream connection store, the custom
+ /// MCP server store, and the egress proxy's public address.
+ pub fn new(
+ connections: Arc<Connections>,
+ custom_servers: Arc<Servers>,
+ base_url: impl Into<String>,
+ ) -> Self {
Self {
connections,
+ custom_servers,
base_url: base_url.into().trim_end_matches('/').to_owned(),
external_base_url: None,
}
@@ -117,11 +128,55 @@ where
);
Ok(slugs)
}
+
+ /// The owner's custom MCP servers to advertise for one session.
+ ///
+ /// Only under [`AgentMcpServers::OwnerConnections`]: a session that runs
+ /// with the owner's connections gets the owner's own servers too, by the
+ /// same reasoning. A [`AgentMcpServers::Selected`] list is an agent
+ /// author's deliberate choice of apps, and the owner's private servers
+ /// are not something an author can select, so none are added behind it.
+ ///
+ /// A server the owner turned off is left out, as with apps. One that is
+ /// on but whose connection has lapsed is still listed: the proxy answers
+ /// its calls with a tool result naming the fix, which the model can act
+ /// on, where an absent tool could only be silently missing.
+ async fn advertised_custom(
+ &self,
+ owner: &MacroUserIdStr<'static>,
+ selection: &AgentMcpServers,
+ ) -> Result<Vec<CustomMcpServerListing>> {
+ let AgentMcpServers::OwnerConnections = selection else {
+ return Ok(Vec::new());
+ };
+ let listings: Vec<CustomMcpServerListing> = self
+ .custom_servers
+ .list(owner)
+ .await
+ .map_err(|error| {
+ HarnessError::Egress(rootcause::report!(
+ "could not list custom MCP servers: {error:?}"
+ ))
+ })?
+ .into_iter()
+ .filter(|record| record.enabled)
+ .map(|record| CustomMcpServerListing {
+ key: CustomMcpServerKey::for_url(&record.url),
+ name: record.server_name,
+ })
+ .collect();
+ tracing::debug!(
+ custom_servers = listings.len(),
+ "advertising custom MCP servers"
+ );
+ Ok(listings)
+ }
}
-impl<Connections> SandboxEgressProvisioner for EgressProvisioner<Connections>
+impl<Connections, Servers> SandboxEgressProvisioner for EgressProvisioner<Connections, Servers>
where
Connections: ConnectionStore,
+ Servers: McpServerStore,
{
fn external_mcp_servers(
&self,
@@ -152,6 +207,7 @@ where
base_url: self.base_url.clone(),
session_token: token.as_str().to_owned(),
mcp_servers: self.advertised(owner, selection).await?,
+ custom_servers: self.advertised_custom(owner, selection).await?,
},
})
}
@@ -167,6 +223,7 @@ where
base_url: self.base_url.clone(),
session_token,
mcp_servers: self.advertised(owner, selection).await?,
+ custom_servers: self.advertised_custom(owner, selection).await?,
})
}
}
diff --git a/crates/agent_harness/src/outbound/egress/test.rs b/crates/agent_harness/src/outbound/egress/test.rs
index 51bd9ad7f8..ae50282237 100644
--- a/crates/agent_harness/src/outbound/egress/test.rs
+++ b/crates/agent_harness/src/outbound/egress/test.rs
@@ -1,5 +1,6 @@
use super::*;
use agent_session::domain::model::AgentMcpServer;
+use mcp_client::domain::models::McpServerRecord;
fn slug(name: &str) -> McpServerSlug {
McpServerSlug::parse(name).expect("a valid app slug")
@@ -41,6 +42,49 @@ impl ConnectionStore for FixedConnections {
}
}
+struct FixedServers(Vec<McpServerRecord>);
+
+impl McpServerStore for FixedServers {
+ type Err = std::convert::Infallible;
+
+ async fn save(&self, _record: &McpServerRecord) -> Result<(), Self::Err> {
+ unreachable!("provisioning never writes")
+ }
+
+ async fn load(
+ &self,
+ _user_id: &MacroUserIdStr<'static>,
+ _server_url: &str,
+ ) -> Result<Option<McpServerRecord>, Self::Err> {
+ unreachable!("provisioning lists, never loads one")
+ }
+
+ async fn delete(
+ &self,
+ _user_id: &MacroUserIdStr<'static>,
+ _server_url: &str,
+ ) -> Result<(), Self::Err> {
+ unreachable!("provisioning never deletes")
+ }
+
+ async fn list(
+ &self,
+ _user_id: &MacroUserIdStr<'static>,
+ ) -> Result<Vec<McpServerRecord>, Self::Err> {
+ Ok(self.0.clone())
+ }
+}
+
+fn custom_server(url: &str, name: &str, enabled: bool) -> McpServerRecord {
+ McpServerRecord {
+ user_id: MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),
+ url: url.to_owned(),
+ server_name: name.to_owned(),
+ credentials: None,
+ enabled,
+ }
+}
+
fn connection(app_slug: &str, enabled: bool) -> pipedream_mcp::domain::models::PipedreamConnection {
pipedream_mcp::domain::models::PipedreamConnection {
user_id: MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),
@@ -63,6 +107,7 @@ async fn lists_enabled_app_slugs_verbatim() {
connection("datadog", false),
connection("Not A Slug!", true),
])),
+ Arc::new(FixedServers(vec![])),
"https://egress.macro.com",
);
@@ -90,6 +135,7 @@ async fn lists_enabled_app_slugs_verbatim() {
async fn restore_wraps_an_existing_token_in_a_fresh_listing() {
let provisioner = EgressProvisioner::new(
Arc::new(FixedConnections(vec![connection("linear", true)])),
+ Arc::new(FixedServers(vec![])),
"https://egress.macro.com",
);
@@ -134,6 +180,7 @@ async fn a_selected_list_is_advertised_regardless_of_connections() {
connection("datadog", true),
connection("linear", false),
])),
+ Arc::new(FixedServers(vec![])),
"https://egress.macro.com",
);
@@ -176,6 +223,7 @@ async fn a_selected_list_is_advertised_regardless_of_connections() {
async fn an_empty_selection_offers_nothing_of_the_owners() {
let provisioner = EgressProvisioner::new(
Arc::new(FixedConnections(vec![connection("datadog", true)])),
+ Arc::new(FixedServers(vec![])),
"https://egress.macro.com",
);
@@ -195,9 +243,143 @@ fn egress(slugs: &[&str]) -> SandboxEgress {
base_url: "https://egress.macro.com".to_owned(),
session_token: "session-token".to_owned(),
mcp_servers: slugs.iter().map(|name| slug(name)).collect(),
+ custom_servers: Vec::new(),
+ }
+}
+
+fn listing(url: &str, name: &str) -> CustomMcpServerListing {
+ CustomMcpServerListing {
+ key: CustomMcpServerKey::for_url(url),
+ name: name.to_owned(),
}
}
+/// The owner's enabled custom servers ride along with their connections,
+/// keyed by the digest of their URL rather than the URL itself, and a server
+/// the owner turned off is absent. The lapsed-connection case is not
+/// distinguished here: the record's credentials never reach the listing.
+#[tokio::test]
+async fn lists_the_owners_enabled_custom_servers_by_key() {
+ let provisioner = EgressProvisioner::new(
+ Arc::new(FixedConnections(vec![connection("linear", true)])),
+ Arc::new(FixedServers(vec![
+ custom_server("https://wiki.example.com/mcp", "Internal wiki", true),
+ custom_server("https://old.example.com/mcp", "Retired", false),
+ ])),
+ "https://egress.macro.com",
+ );
+
+ let provisioned = provisioner
+ .provision(
+ AgentSessionId::new(),
+ &MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),
+ &AgentMcpServers::OwnerConnections,
+ )
+ .await
+ .expect("provisioned");
+
+ assert_eq!(
+ provisioned.sandbox.custom_servers,
+ [listing("https://wiki.example.com/mcp", "Internal wiki")]
+ );
+ let printed = format!("{:?}", provisioned.sandbox);
+ assert!(!printed.contains("wiki.example.com"), "{printed}");
+
+ let restored = provisioner
+ .restore(
+ &MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),
+ "already-minted-token".to_owned(),
+ &AgentMcpServers::OwnerConnections,
+ )
+ .await
+ .expect("restored");
+ assert_eq!(
+ restored.custom_servers,
+ [listing("https://wiki.example.com/mcp", "Internal wiki")]
+ );
+}
+
+/// A selected app list is an author's choice of apps; the owner's private
+/// servers are not added behind it.
+#[tokio::test]
+async fn a_selected_list_carries_none_of_the_owners_custom_servers() {
+ let provisioner = EgressProvisioner::new(
+ Arc::new(FixedConnections(vec![])),
+ Arc::new(FixedServers(vec![custom_server(
+ "https://wiki.example.com/mcp",
+ "Internal wiki",
+ true,
+ )])),
+ "https://egress.macro.com",
+ );
+
+ let provisioned = provisioner
+ .provision(
+ AgentSessionId::new(),
+ &MacroUserIdStr::try_from_email("[email protected]").expect("a valid user id"),
+ &selected(&["notion"]),
+ )
+ .await
+ .expect("provisioned");
+
+ assert!(provisioned.sandbox.custom_servers.is_empty());
+}
+
+/// Custom servers are advertised after the apps, on the proxy's custom
+/// route, under a name reduced to what tool namespaces tolerate - and a
+/// name that would repeat an earlier entry, Macro's own included, is made
+/// unique with a piece of the server's key rather than dropped or allowed
+/// to shadow.
+#[test]
+fn custom_servers_are_advertised_under_unique_sanitized_names() {
+ let mut egress = egress(&["linear"]);
+ egress.custom_servers = vec![
+ listing("https://wiki.example.com/mcp", "Internal wiki"),
+ listing("https://a.example.com/mcp", "macro"),
+ listing("https://b.example.com/mcp", " "),
+ listing("https://c.example.com/mcp", "linear"),
+ listing("https://d.example.com/mcp", "Internal wiki"),
+ ];
+ let key = |url: &str| CustomMcpServerKey::for_url(url);
+
+ let entries: Vec<(String, String)> = egress.server_entries().collect();
+
+ let expected_tail = [
+ (
+ "Internal_wiki".to_owned(),
+ key("https://wiki.example.com/mcp"),
+ ),
+ (
+ format!("macro_{}", &key("https://a.example.com/mcp").as_str()[..8]),
+ key("https://a.example.com/mcp"),
+ ),
+ ("custom".to_owned(), key("https://b.example.com/mcp")),
+ (
+ format!("linear_{}", &key("https://c.example.com/mcp").as_str()[..8]),
+ key("https://c.example.com/mcp"),
+ ),
+ (
+ format!(
+ "Internal_wiki_{}",
+ &key("https://d.example.com/mcp").as_str()[..8]
+ ),
+ key("https://d.example.com/mcp"),
+ ),
+ ]
+ .map(|(name, key)| (name, format!("https://egress.macro.com/mcp-custom/{key}")));
+ assert_eq!(entries[4..], expected_tail);
+
+ let mut names: Vec<&String> = entries.iter().map(|(name, _)| name).collect();
+ let total = names.len();
+ names.sort();
+ names.dedup();
+ assert_eq!(names.len(), total, "every advertised name is unique");
+ assert!(
+ entries.iter().all(|(name, _)| !name.is_empty()),
+ "every advertised name is non-empty"
+ );
+}
+
/// Every server points at the proxy and carries the session token rather than
/// any upstream credential; Macro's own server leads the list on its own
/// route.
@@ -310,6 +492,7 @@ fn external_runtime_uses_host_reachable_urls_with_the_existing_session_credentia
use agent_client_protocol::schema::v1::McpServer;
let provisioner = EgressProvisioner::new(
Arc::new(FixedConnections(vec![])),
+ Arc::new(FixedServers(vec![])),
"http://agent-harness-service:8102",
)
.with_external_base_url(Some("http://localhost:28102/".into()));
diff --git a/crates/agent_harness/src/testing/helpers/egress.rs b/crates/agent_harness/src/testing/helpers/egress.rs
index a1d156d28e..0bc3517921 100644
--- a/crates/agent_harness/src/testing/helpers/egress.rs
+++ b/crates/agent_harness/src/testing/helpers/egress.rs
@@ -76,5 +76,6 @@ pub fn test_egress() -> SandboxEgress {
base_url: "https://egress.test".to_owned(),
session_token: "test-session-token".to_owned(),
mcp_servers: Vec::new(),
+ custom_servers: Vec::new(),
}
}
diff --git a/crates/agent_inmem/src/outbound/egress_mcp/test.rs b/crates/agent_inmem/src/outbound/egress_mcp/test.rs
index 1584f14167..9f780035d5 100644
--- a/crates/agent_inmem/src/outbound/egress_mcp/test.rs
+++ b/crates/agent_inmem/src/outbound/egress_mcp/test.rs
@@ -1,6 +1,6 @@
use std::sync::Mutex;
-use agent_egress::domain::model::McpServerSlug;
+use agent_egress::domain::model::{CustomMcpServerKey, McpServerSlug};
use mcp_toolset::client_info;
use rmcp::ServiceExt as _;
use rmcp::transport::StreamableHttpClientTransport;
@@ -209,6 +209,28 @@ async fn the_macro_route_names_macros_own_server() {
);
}
+/// The custom route carries the owner's server by its URL key, the same way
+/// the harness advertises it, so the in-memory agent reaches a custom server
+/// through the proxy exactly as a sandboxed one does.
+#[tokio::test]
+async fn the_custom_route_names_the_owners_server_by_key() {
+ let egress = Arc::new(StubEgress::default());
+ let key = CustomMcpServerKey::for_url("https://wiki.example.com/mcp");
+ let uri: &'static str = Box::leak(format!("{BASE_URL}/mcp-custom/{key}").into_boxed_str());
+
+ address_only(&egress, BASE_URL, uri)
+ .await
+ .expect("405 is fine");
+
+ assert_eq!(
+ egress.seen(),
+ vec![(
+ "session-token".to_owned(),
+ EgressTarget::McpServer(McpDestination::Custom(key))
+ )]
+ );
+}
+
/// A `delete_session` the stub answers with 405, which the client reads as
/// "nothing to delete": the cheapest way to exercise the address step alone.
async fn address_only(
diff --git a/crates/mcp_client/src/domain/service/mod.rs b/crates/mcp_client/src/domain/service/mod.rs
index b53bfa29d9..5b7a6bf465 100644
--- a/crates/mcp_client/src/domain/service/mod.rs
+++ b/crates/mcp_client/src/domain/service/mod.rs
@@ -1,7 +1,11 @@
/// Write-through OAuth credential store for MCP connections.
pub mod persisting_credential_store;
+/// A live credential for a stored server, for callers that stamp headers
+/// themselves.
+pub mod server_access;
/// MCP tool set and combined tool set for the AI loop.
pub mod toolset;
pub use persisting_credential_store::PersistingCredentialStore;
+pub use server_access::{ServerAccess, server_access, unexpired_access_token};
pub use toolset::{CombinedToolSet, McpToolSet};
diff --git a/crates/mcp_client/src/domain/service/server_access.rs b/crates/mcp_client/src/domain/service/server_access.rs
new file mode 100644
index 0000000000..de325fb08a
--- /dev/null
+++ b/crates/mcp_client/src/domain/service/server_access.rs
@@ -0,0 +1,121 @@
+//! A live credential for a stored MCP server, for a caller that stamps
+//! headers itself rather than dialing through rmcp's client.
+//!
+//! [`crate::domain::models::McpServerRecord::connect`] hands rmcp a credential
+//! store and lets its transport refresh lazily on the way through. A proxy
+//! that only forwards bytes cannot do that - it needs the bearer *now*, as a
+//! value - so this is the same refresh-and-persist story, inverted: read the
+//! stored grant, hand back its token if it is still good, and otherwise run
+//! rmcp's refresh through the same write-through store so the rotated grant
+//! lands in the database for whoever reads it next.
+
+use std::sync::Arc;
+
+use oauth2::TokenResponse as _;
+use rmcp::transport::auth::{AuthError, AuthorizationManager};
+
+use crate::domain::models::{McpServerRecord, StoredCredentials};
+use crate::domain::ports::McpServerStore;
+use crate::domain::service::PersistingCredentialStore;
+
+#[cfg(test)]
+mod test;
+
+/// How long before expiry a stored access token stops being handed out.
+///
+/// The same margin rmcp's own `get_access_token` applies, so a token this
+/// returns is one rmcp would also have used as-is.
+const REFRESH_BUFFER_SECS: u64 = 30;
+
+/// What a stored server grant yields right now.
+#[derive(Clone, PartialEq, Eq)]
+pub enum ServerAccess {
+ /// The server was added without connecting an account: it is dialed
+ /// bare, exactly as the in-process client dials it.
+ Anonymous,
+ /// A bearer good for at least [`REFRESH_BUFFER_SECS`] more seconds.
+ Bearer(String),
+ /// The stored grant no longer yields a token - expired with no refresh
+ /// token, refused on refresh, or an authorization that never finished -
+ /// and only the owner re-authorizing can mend it.
+ AuthorizationRequired,
+}
+
+impl std::fmt::Debug for ServerAccess {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ Self::Anonymous => f.write_str("Anonymous"),
+ Self::Bearer(_) => f.write_str("Bearer([REDACTED])"),
+ Self::AuthorizationRequired => f.write_str("AuthorizationRequired"),
+ }
+ }
+}
+
+/// Whether `credentials` carry an access token good for at least
+/// [`REFRESH_BUFFER_SECS`] more seconds, and that token if so.
+///
+/// `None` when the token is missing, expired, or about to be. A token whose
+/// expiry is unknown - stored before `token_received_at` was tracked, or
+/// issued without `expires_in` - counts as good, as it does in rmcp: there is
+/// nothing to compare against, and the upstream will say if it is wrong.
+pub fn unexpired_access_token(
+ credentials: &StoredCredentials,
+ now_epoch_secs: u64,
+) -> Option<String> {
+ let token = credentials.token_response.as_ref()?;
+ if let (Some(expires_in), Some(received_at)) =
+ (token.expires_in(), credentials.token_received_at)
+ {
+ let elapsed = now_epoch_secs.saturating_sub(received_at);
+ let remaining = expires_in.as_secs().saturating_sub(elapsed);
+ if remaining < REFRESH_BUFFER_SECS {
+ return None;
+ }
+ }
+ Some(token.access_token().secret().to_owned())
+}
+
+/// The credential `record` yields right now, refreshing and persisting the
+/// grant through `store` if that is what it takes.
+///
+/// Only the refresh touches the network, and only when the stored token is
+/// expired or about to be: rmcp's manager discovers the server's OAuth
+/// metadata before it can refresh, and paying that on every call would turn
+/// a header lookup into three round trips.
+#[tracing::instrument(skip_all, err, fields(server = %record.server_name))]
+pub async fn server_access<S: McpServerStore>(
+ record: &McpServerRecord,
+ store: Arc<S>,
+) -> Result<ServerAccess, AuthError> {
+ let Some(credentials) = &record.credentials else {
+ return Ok(ServerAccess::Anonymous);
+ };
+ if credentials.token_response.is_none() {
+ // An authorization that started and never finished: the client is
+ // registered but no token was ever exchanged.
+ return Ok(ServerAccess::AuthorizationRequired);
+ }
+ if let Some(token) = unexpired_access_token(credentials, now_epoch_secs()) {
+ return Ok(ServerAccess::Bearer(token));
+ }
+
+ tracing::info!("stored access token has expired or is about to; refreshing");
+ let mut manager = AuthorizationManager::new(&*record.url).await?;
+ let credential_store = PersistingCredentialStore::new(record.clone(), store);
+ credential_store.seed(credentials.clone()).await?;
+ manager.set_credential_store(credential_store);
+ manager.initialize_from_store().await?;
+
+ match manager.get_access_token().await {
+ Ok(token) => Ok(ServerAccess::Bearer(token)),
+ Err(AuthError::AuthorizationRequired) => Ok(ServerAccess::AuthorizationRequired),
+ Err(error) => Err(error),
+ }
+}
+
+fn now_epoch_secs() -> u64 {
+ std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .map(|elapsed| elapsed.as_secs())
+ .unwrap_or_default()
+}
diff --git a/crates/mcp_client/src/domain/service/server_access/test.rs b/crates/mcp_client/src/domain/service/server_access/test.rs
new file mode 100644
index 0000000000..05d6c7e279
--- /dev/null
+++ b/crates/mcp_client/src/domain/service/server_access/test.rs
@@ -0,0 +1,150 @@
+use super::*;
+use crate::domain::models::MacroUserIdStr;
+use macro_user_id::cowlike::CowLike;
+use rmcp::transport::auth::OAuthTokenResponse;
+
+/// A store nothing here should ever reach: every case below is answered
+/// from the record alone.
+struct UntouchedStore;
+
+impl McpServerStore for UntouchedStore {
+ type Err = anyhow::Error;
+
+ async fn save(&self, _record: &McpServerRecord) -> Result<(), Self::Err> {
+ unreachable!("a fresh or absent grant is never persisted")
+ }
+
+ async fn load(
+ &self,
+ _user_id: &MacroUserIdStr<'static>,
+ _server_url: &str,
+ ) -> Result<Option<McpServerRecord>, Self::Err> {
+ unreachable!()
+ }
+
+ async fn delete(
+ &self,
+ _user_id: &MacroUserIdStr<'static>,
+ _server_url: &str,
+ ) -> Result<(), Self::Err> {
+ unreachable!()
+ }
+
+ async fn list(
+ &self,
+ _user_id: &MacroUserIdStr<'static>,
+ ) -> Result<Vec<McpServerRecord>, Self::Err> {
+ unreachable!()
+ }
+}
+
+fn record(credentials: Option<StoredCredentials>) -> McpServerRecord {
+ McpServerRecord {
+ user_id: MacroUserIdStr::parse_from_str("macro|[email protected]")
+ .unwrap()
+ .into_owned(),
+ url: "https://mcp.example.com/mcp".to_owned(),
+ server_name: "Example".to_owned(),
+ credentials,
+ enabled: true,
+ }
+}
+
+fn token(access_token: &str, expires_in: Option<u64>) -> OAuthTokenResponse {
+ let mut response = serde_json::json!({
+ "access_token": access_token,
+ "token_type": "bearer",
+ });
+ if let Some(expires_in) = expires_in {
+ response["expires_in"] = expires_in.into();
+ }
+ serde_json::from_value(response).expect("a token response")
+}
+
+fn credentials(token: Option<OAuthTokenResponse>, received_at: Option<u64>) -> StoredCredentials {
+ StoredCredentials::new("client-id".to_owned(), token, Vec::new(), received_at)
+}
+
+#[test]
+fn a_token_with_time_left_is_handed_out() {
+ let stored = credentials(Some(token("live", Some(3600))), Some(1_000));
+ assert_eq!(
+ unexpired_access_token(&stored, 1_000 + 3600 - REFRESH_BUFFER_SECS),
+ Some("live".to_owned())
+ );
+}
+
+#[test]
+fn a_token_inside_the_refresh_buffer_is_not() {
+ let stored = credentials(Some(token("stale", Some(3600))), Some(1_000));
+ assert_eq!(
+ unexpired_access_token(&stored, 1_000 + 3600 - REFRESH_BUFFER_SECS + 1),
+ None
+ );
+ assert_eq!(unexpired_access_token(&stored, 1_000 + 7200), None);
+}
+
+/// Credentials stored before expiry was tracked carry no `expires_in` or
+/// `token_received_at`; there is nothing to compare, so the token stands.
+#[test]
+fn a_token_of_unknown_expiry_is_handed_out() {
+ let no_expiry = credentials(Some(token("eternal", None)), Some(1_000));
+ assert_eq!(
+ unexpired_access_token(&no_expiry, u64::MAX),
+ Some("eternal".to_owned())
+ );
+ let no_receipt = credentials(Some(token("undated", Some(60))), None);
+ assert_eq!(
+ unexpired_access_token(&no_receipt, u64::MAX),
+ Some("undated".to_owned())
+ );
+}
+
+#[test]
+fn no_token_response_yields_nothing() {
+ assert_eq!(unexpired_access_token(&credentials(None, Some(1)), 1), None);
+}
+
+#[tokio::test]
+async fn a_server_added_without_an_account_is_anonymous() {
+ let access = server_access(&record(None), Arc::new(UntouchedStore))
+ .await
+ .unwrap();
+ assert_eq!(access, ServerAccess::Anonymous);
+}
+
+#[tokio::test]
+async fn an_authorization_that_never_finished_requires_one() {
+ let access = server_access(
+ &record(Some(credentials(None, Some(1)))),
+ Arc::new(UntouchedStore),
+ )
+ .await
+ .unwrap();
+ assert_eq!(access, ServerAccess::AuthorizationRequired);
+}
+
+/// A live token comes straight off the record: no discovery, no refresh,
+/// nothing written.
+#[tokio::test]
+async fn a_live_token_is_returned_without_touching_the_network() {
+ let now = now_epoch_secs();
+ let access = server_access(
+ &record(Some(credentials(
+ Some(token("live", Some(3600))),
+ Some(now),
+ ))),
+ Arc::new(UntouchedStore),
+ )
+ .await
+ .unwrap();
+ assert_eq!(access, ServerAccess::Bearer("live".to_owned()));
+}
+
+#[test]
+fn debug_redacts_the_bearer() {
+ assert_eq!(
+ format!("{:?}", ServerAccess::Bearer("secret".to_owned())),
+ "Bearer([REDACTED])"
+ );
+}
diff --git a/docs/AGENT_GUIDE/surfaces.md b/docs/AGENT_GUIDE/surfaces.md
index 604c9902f1..0b1c5e60b6 100644
--- a/docs/AGENT_GUIDE/surfaces.md
+++ b/docs/AGENT_GUIDE/surfaces.md
@@ -2063,6 +2063,14 @@ Provider and custom-server More menus contain Disable, Reconnect, and Disconnect
custom servers also offer Rename. Disabled grants show Enable. Unauthenticated
custom servers show Connect and Remove. Disconnect/Remove require confirmation.
Adding a custom MCP saves its name and URL; Connect on its row starts OAuth.
+Enabled custom servers are offered to the owner's agent sessions (Cursor, Claude,
+Codex, macrod, in-memory) alongside connected apps, through the same session
+egress path: the sandbox sees the server under its name and a URL key, never the
+server's address or token. A disabled server is not offered. If a server's
+connection has expired, its tool calls return a message telling the agent to
+have the owner use Reconnect under Custom MCP; there is no `Connect` chip for
+custom servers. Agents configured with a fixed app selection do not receive the
+owner's custom servers.
An agent reply's `Connect <app>` chip still starts that app's connection flow.
Cursor stays in Agents → Runtimes with its API key and default model controls; it is not
featured or offered in the Connections catalog. Personal Gmail and GitHub account
diff --git a/services/agent_harness_service/Cargo.toml b/services/agent_harness_service/Cargo.toml
index 969520cc60..71c13025e5 100644
--- a/services/agent_harness_service/Cargo.toml
+++ b/services/agent_harness_service/Cargo.toml
@@ -61,6 +61,7 @@ github_pull_requests = { path = "../../crates/github_pull_requests", default-fea
"s3",
"service",
] }
+mcp_client = { path = "../../crates/mcp_client" }
pipedream_mcp = { path = "../../crates/pipedream_mcp" }
url = { workspace = true }
contacts = { path = "../../crates/contacts" }
diff --git a/services/agent_harness_service/src/config.rs b/services/agent_harness_service/src/config.rs
index c6222ddd41..a27c47a1d6 100644
--- a/services/agent_harness_service/src/config.rs
+++ b/services/agent_harness_service/src/config.rs
@@ -38,6 +38,11 @@ macro_env_var::env_vars!(
pub struct PipedreamClientSecret;
/// The Pipedream Connect project ID (`proj_...`).
pub struct PipedreamProjectId;
+ /// AES key the `mcp_servers` rows' OAuth credentials are encrypted with -
+ /// the same one `document_cognition_service` writes them with, so the
+ /// custom MCP servers a person added in Macro are the ones the egress
+ /// proxy can refresh and stamp for their sandboxes.
+ pub struct McpCredentialsKeySecretName;
);
macro_env_var::maybe_env_vars!(
@@ -180,6 +185,8 @@ pub struct Config {
pub pipedream_mcp_url: String,
/// RSA key Macro API tokens are signed with.
pub macro_api_token_private_secret_key: LocalOrRemoteSecret<MacroApiTokenPrivateSecretKey>,
+ /// AES key the custom MCP servers' stored OAuth credentials are encrypted with.
+ pub mcp_credentials_key_secret_name: LocalOrRemoteSecret<McpCredentialsKeySecretName>,
/// Issuer stamped into minted Macro API tokens.
pub macro_api_token_issuer: MacroApiTokenIssuer,
/// S3 bucket the Changes pane's patches are stored in, one object per
diff --git a/services/agent_harness_service/src/main.rs b/services/agent_harness_service/src/main.rs
index 96e4bad89f..c924c8711d 100644
--- a/services/agent_harness_service/src/main.rs
+++ b/services/agent_harness_service/src/main.rs
@@ -33,6 +33,7 @@ use agent_changes::inbound::axum_router::AgentChangesRouterState;
use agent_changes::outbound::postgres::PgChangesetRepo;
use agent_changes::outbound::s3::S3ChangesetBlobStore;
use agent_egress::domain::service::EgressServiceImpl;
+use agent_egress::outbound::custom_mcp::WithCustomMcp;
use agent_egress::outbound::forwarder::ReqwestForwarder;
use agent_egress::outbound::github_tokens::GithubAppTokens;
use agent_egress::outbound::macro_mcp::{MacroApiTokenSigner, WithMacroMcp};
@@ -136,6 +137,8 @@ use macro_service_urls::{
AgentHarnessEgressUrl, ConnectionGatewayUrl, LexicalServiceUrl, McpServiceUrl,
StaticFileServiceUrl,
};
+use mcp_client::domain::models::AesKey;
+use mcp_client::outbound::pg_server_repo::PgServerRepo;
use model_providers::{CursorModels, InMemoryModels, MacrodModels, VisibleHarnessAccess};
use permission_policy::PgPermissionPolicySource;
use pipedream_mcp::outbound::api::{PipedreamClient, PipedreamConfig};
@@ -383,6 +386,12 @@ async fn run() -> anyhow::Result<()> {
// connected in Macro is an app the sandbox can reach, with nothing to
// keep in sync. The rows hold no secrets - Pipedream owns the grants.
let mcp_connections = Arc::new(PgConnectionRepo::new(pool.clone()));
+ // Custom MCP servers - the ones a person added by URL - in the same rows
+ // document_cognition_service manages, under the same encryption key, so
+ // the proxy can refresh a stored grant and persist what comes back.
+ let mcp_credentials_key = AesKey::try_from(config.mcp_credentials_key_secret_name.as_ref())
+ .context("invalid MCP credentials encryption key")?;
+ let mcp_servers = Arc::new(PgServerRepo::new(pool.clone(), mcp_credentials_key));
// The client that addresses Pipedream's remote MCP server, built from the
// same credentials `document_cognition_service` uses.
@@ -400,11 +409,15 @@ async fn run() -> anyhow::Result<()> {
.context("failed to build Pipedream client")?;
// Every session's MCP servers: Macro's own under the reserved `macro`
- // slug, then the owner's Pipedream connections. The `macro` credential is
- // signed inline with the same key authentication_service holds; what this
- // process hands out is always single-user and minutes from expiry.
+ // slug, the owner's custom servers by URL key, then the owner's Pipedream
+ // connections. The `macro` credential is signed inline with the same key
+ // authentication_service holds; what this process hands out is always
+ // single-user and minutes from expiry.
let mcp_credentials = WithMacroMcp::new(
- PipedreamMcpCredentials::new(Arc::clone(&mcp_connections), pipedream),
+ WithCustomMcp::new(
+ PipedreamMcpCredentials::new(Arc::clone(&mcp_connections), pipedream),
+ Arc::clone(&mcp_servers),
+ ),
MacroApiTokenSigner::new(
pool.clone(),
config.macro_api_token_issuer.as_ref(),
@@ -743,7 +756,11 @@ async fn run() -> anyhow::Result<()> {
.context("egress URL needs a host")?
.to_owned(),
),
- EgressProvisioner::new(Arc::clone(&mcp_connections), egress_base_url.clone()),
+ EgressProvisioner::new(
+ Arc::clone(&mcp_connections),
+ Arc::clone(&mcp_servers),
+ egress_base_url.clone(),
+ ),
claude_cloud_agents::inbound::acp::attach,
);
let containers = RoutedContainerManager::new(
@@ -933,8 +950,12 @@ async fn run() -> anyhow::Result<()> {
),
prompt_context,
prompt_composer,
- EgressProvisioner::new(Arc::clone(&mcp_connections), egress_base_url)
- .with_external_base_url(config.external_egress_base_url.clone()),
+ EgressProvisioner::new(
+ Arc::clone(&mcp_connections),
+ Arc::clone(&mcp_servers),
+ egress_base_url,
+ )
+ .with_external_base_url(config.external_egress_base_url.clone()),
RedisCommandForwarder::new(redis.clone()),
PgPermissionPolicySource::new(PgBotsRepo::new(pool.clone())),
PgCodingAgentSource::new(PgBotsRepo::new(pool.clone())),