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(mcp): emit URLs not mention tags in MCP responses (macro-1917) (#4191)

Eric Hayesd387c375c4d4
diff --git a/infra/stacks/mcp-server/Pulumi.dev.yaml b/infra/stacks/mcp-server/Pulumi.dev.yaml
index 99ca540e6f..106388ae95 100644
--- a/infra/stacks/mcp-server/Pulumi.dev.yaml
+++ b/infra/stacks/mcp-server/Pulumi.dev.yaml
@@ -1,6 +1,7 @@
config:
aws-native:region: us-east-1
aws:region: us-east-1
+ mcp-server:app_base_url: https://dev.macro.com
mcp-server:fusionauth_base_url: https://fusionauth-dev.macro.com
mcp-server:fusionauth_client_id: fusionauth-client-id-key-dev
mcp-server:fusionauth_client_secret: fusionauth-client-secret-dev
diff --git a/infra/stacks/mcp-server/Pulumi.prod.yaml b/infra/stacks/mcp-server/Pulumi.prod.yaml
index f8e159d771..542ba58284 100644
--- a/infra/stacks/mcp-server/Pulumi.prod.yaml
+++ b/infra/stacks/mcp-server/Pulumi.prod.yaml
@@ -1,6 +1,7 @@
config:
aws-native:region: us-east-1
aws:region: us-east-1
+ mcp-server:app_base_url: https://macro.com
mcp-server:fusionauth_base_url: https://auth.macro.com
mcp-server:fusionauth_client_id: fusionauth-client-id-key-prod
mcp-server:fusionauth_client_secret: fusionauth-client-secret-prod
diff --git a/infra/stacks/mcp-server/index.ts b/infra/stacks/mcp-server/index.ts
index e46c8ddbc2..9004c5e418 100644
--- a/infra/stacks/mcp-server/index.ts
+++ b/infra/stacks/mcp-server/index.ts
@@ -38,6 +38,10 @@ const FUSIONAUTH_CLIENT_ID = aws.secretsmanager
const FUSIONAUTH_BASE_URL = config.require('fusionauth_base_url');
const FUSIONAUTH_ISSUER = config.require('fusionauth_issuer');
+// Base URL of the Macro web app, used to build links to Macro items in MCP
+// responses. Consumed by mcp_service as the `APP_BASE_URL` env var.
+const APP_BASE_URL = config.require('app_base_url');
+
const FUSIONAUTH_CLIENT_SECRET = config.require('fusionauth_client_secret');
const fusionauthClientSecretArn: pulumi.Output<string> = aws.secretsmanager
.getSecretVersionOutput({ secretId: FUSIONAUTH_CLIENT_SECRET })
@@ -136,6 +140,10 @@ const mcpServer = new McpServer(`mcp-server-${stack}`, {
name: 'ISSUER',
value: pulumi.interpolate`${FUSIONAUTH_ISSUER}`,
},
+ {
+ name: 'APP_BASE_URL',
+ value: APP_BASE_URL,
+ },
// MCP OAuth / FusionAuth
{
name: 'MCP_PUBLIC_URL',
diff --git a/rust/cloud-storage/mcp_service/src/main.rs b/rust/cloud-storage/mcp_service/src/main.rs
index 4df1bec0d0..7b68465552 100644
--- a/rust/cloud-storage/mcp_service/src/main.rs
+++ b/rust/cloud-storage/mcp_service/src/main.rs
@@ -25,6 +25,12 @@ maybe_env_vars! {
struct Port;
}
+macro_env_var::env_var! {
+ /// Base URL of the Macro web app (e.g. `https://macro.com`), used to build
+ /// links to Macro items in MCP responses. Read from `APP_BASE_URL`.
+ pub struct AppBaseUrl;
+}
+
#[tokio::main]
#[tracing::instrument(err)]
async fn main() -> anyhow::Result<()> {
@@ -32,6 +38,13 @@ async fn main() -> anyhow::Result<()> {
let context = build_context().await?;
+ // Base URL of the Macro web app, used to build links to Macro items in MCP
+ // responses. Loaded from the `APP_BASE_URL` environment variable via the
+ // macro_env_var infra.
+ let item_base_url = AppBaseUrl::new()
+ .context("failed to read APP_BASE_URL")?
+ .to_string();
+
// Create the MCP service with authenticated tool handler
let mcp_service = StreamableHttpService::new(
move || {
@@ -40,6 +53,7 @@ async fn main() -> anyhow::Result<()> {
tools.toolset,
context.tool_context.clone(),
context.db.clone(),
+ item_base_url.clone(),
))
},
Arc::new(LocalSessionManager::default()),
diff --git a/rust/cloud-storage/mcp_service/src/tool_service.rs b/rust/cloud-storage/mcp_service/src/tool_service.rs
index 9c8433eae2..9e79c37890 100644
--- a/rust/cloud-storage/mcp_service/src/tool_service.rs
+++ b/rust/cloud-storage/mcp_service/src/tool_service.rs
@@ -20,15 +20,25 @@ pub struct AuthenticatedToolService<Context> {
toolset: Arc<AsyncToolCollection<Context>>,
context: Context,
db: PgPool,
+ /// Base URL of the Macro web app used to build links to Macro items in MCP
+ /// responses (e.g. `https://macro.com`). Comes from the `APP_BASE_URL`
+ /// environment variable.
+ item_base_url: String,
}
impl<Context> AuthenticatedToolService<Context> {
/// Creates a new authenticated tool service.
- pub fn new(toolset: Arc<AsyncToolCollection<Context>>, context: Context, db: PgPool) -> Self {
+ pub fn new(
+ toolset: Arc<AsyncToolCollection<Context>>,
+ context: Context,
+ db: PgPool,
+ item_base_url: String,
+ ) -> Self {
Self {
toolset,
context,
db,
+ item_base_url,
}
}
@@ -104,13 +114,14 @@ where
.with_description(
"Search, read, and create content across documents, emails, and messages in Macro.",
);
+ let base_url = self.item_base_url.trim_end_matches('/');
info.instructions = Some(format!(
"This server provides tools for interacting with a user's Macro workspace. \
Use ContentSearch and NameSearch to find entities. \
Use ReadContent, ReadMetadata, and ReadThread to read them. \
Use CreateDocument to create new documents. \
Use ListEntities to browse recent items.\n\n{}",
- prompt::MCP_INSTRUCTIONS,
+ prompt::mcp_instructions(base_url),
));
info
}
diff --git a/rust/cloud-storage/mcp_service/src/tool_service/test.rs b/rust/cloud-storage/mcp_service/src/tool_service/test.rs
index c8094fd855..a4fe2d64a9 100644
--- a/rust/cloud-storage/mcp_service/src/tool_service/test.rs
+++ b/rust/cloud-storage/mcp_service/src/tool_service/test.rs
@@ -7,7 +7,12 @@ fn empty_service() -> AuthenticatedToolService<()> {
let db = PgPoolOptions::new()
.connect_lazy("postgres://localhost/unused")
.expect("lazy pool creation should not fail");
- AuthenticatedToolService::new(Arc::new(AsyncToolCollection::new()), (), db)
+ AuthenticatedToolService::new(
+ Arc::new(AsyncToolCollection::new()),
+ (),
+ db,
+ "https://macro.com".to_owned(),
+ )
}
#[tokio::test]
@@ -50,6 +55,36 @@ async fn server_instructions_describe_available_workflows() {
}
}
+#[tokio::test]
+async fn server_instructions_link_items_as_urls_not_mention_tags() {
+ let instructions = empty_service()
+ .get_info()
+ .instructions
+ .expect("server should provide MCP instructions");
+
+ // MCP responses must link items with plain URLs built from the app base URL.
+ assert!(
+ instructions.contains("https://macro.com/app/"),
+ "instructions should build item links from the app base url"
+ );
+ // And must steer the model away from the in-app mention markup.
+ assert!(
+ instructions.contains("<m-document-mention>"),
+ "instructions should reference the mention tag it is forbidding"
+ );
+ assert!(
+ instructions.contains("Do NOT emit"),
+ "instructions should forbid emitting mention tags in MCP responses"
+ );
+ // Lists of items should be rendered as a table with number/name/link columns.
+ for column in ["number", "name", "link"] {
+ assert!(
+ instructions.contains(column),
+ "instructions should describe the {column} table column"
+ );
+ }
+}
+
#[tokio::test]
async fn empty_toolset_lists_no_tools() {
assert!(empty_service().tool_definitions().is_empty());
diff --git a/rust/cloud-storage/prompt/src/lib.rs b/rust/cloud-storage/prompt/src/lib.rs
index 8441ed5ada..a241c58b2b 100644
--- a/rust/cloud-storage/prompt/src/lib.rs
+++ b/rust/cloud-storage/prompt/src/lib.rs
@@ -10,6 +10,7 @@ pub mod channel_mention;
pub mod citations;
pub mod do_not;
pub mod math;
+pub mod mcp_item_links;
pub mod mentions;
pub mod tone;
pub mod tool_usage;
@@ -30,13 +31,69 @@ pub static BASE_PROMPT: ComposedPrompt = tone::PROMPT
/// appended.
pub static TOOL_USE_PROMPT: ComposedPrompt = BASE_PROMPT.compose(&tool_usage::PROMPT);
-/// Instructions surfaced to external MCP clients via the server `instructions`
-/// field. Carries the formatting/correctness rules Macro features depend on —
-/// mentions, citations, do-not rules, and Macro terms — so that AI used through
-/// MCP produces valid Macro mentions and citations. Deliberately omits chat
-/// tone/style and tool-use instructions, which belong to the host client, not
-/// to Macro.
-pub static MCP_INSTRUCTIONS: ComposedPrompt = mentions::PROMPT
- .compose(&citations::PROMPT)
+/// Citation, do-not, and Macro-terms rules surfaced to external MCP clients,
+/// composed together. These are static; the item-linking rules are not, because
+/// they depend on the runtime app base URL — see [`mcp_instructions`].
+///
+/// Deliberately omits the in-app [`mentions`] section (MCP clients cannot render
+/// `<m-document-mention>` tags) as well as chat tone/style and tool-use
+/// instructions, which belong to the host client, not to Macro.
+static MCP_STATIC_INSTRUCTIONS: ComposedPrompt = citations::PROMPT
.compose(&do_not::PROMPT)
.compose(&about_macro::PROMPT);
+
+/// Builds the instructions surfaced to external MCP clients via the server
+/// `instructions` field.
+///
+/// Carries the formatting/correctness rules Macro features depend on so that AI
+/// used through MCP produces valid output. Item links are rendered as plain
+/// Markdown URLs (built from `base_url`, the runtime `APP_BASE_URL` value) and
+/// lists of items as Markdown tables — NOT the in-app `<m-document-mention>`
+/// markup, which MCP clients cannot render. `base_url` should already have any
+/// trailing slash trimmed.
+pub fn mcp_instructions(base_url: &str) -> String {
+ format!(
+ "{}\n{MCP_STATIC_INSTRUCTIONS}",
+ mcp_item_links::render(base_url)
+ )
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn mcp_instructions_link_items_as_urls_built_from_base_url() {
+ let instructions = mcp_instructions("https://macro.com");
+
+ // Item links are plain URLs built from the base url, including a
+ // worked example for a document.
+ assert!(instructions.contains("https://macro.com/app/<type>/<id>"));
+ assert!(instructions.contains("[Name](https://macro.com/app/md/<id>)"));
+ }
+
+ #[test]
+ fn mcp_instructions_forbid_mention_tags_but_in_app_prompt_keeps_them() {
+ let instructions = mcp_instructions("https://macro.com");
+
+ // MCP responses must steer away from the in-app mention markup...
+ assert!(instructions.contains("Do NOT emit `<m-document-mention>`"));
+
+ // ...while the in-app base prompt still instructs the model to use it.
+ let in_app = BASE_PROMPT.to_string();
+ assert!(in_app.contains("<m-document-mention>"));
+ assert!(in_app.contains("use XML mention tags"));
+ }
+
+ #[test]
+ fn mcp_instructions_describe_item_table_columns() {
+ let instructions = mcp_instructions("https://macro.com");
+
+ for column in ["number", "name", "link"] {
+ assert!(
+ instructions.contains(column),
+ "instructions should describe the {column} table column"
+ );
+ }
+ }
+}
diff --git a/rust/cloud-storage/prompt/src/mcp_item_links.rs b/rust/cloud-storage/prompt/src/mcp_item_links.rs
new file mode 100644
index 0000000000..c5c62a2989
--- /dev/null
+++ b/rust/cloud-storage/prompt/src/mcp_item_links.rs
@@ -0,0 +1,38 @@
+//! Rules for linking to Macro items in responses sent to external MCP clients.
+//!
+//! Unlike the in-app [`crate::mentions`] section — which tells the model to emit
+//! `<m-document-mention>` XML tags that only render inside the Macro app — MCP
+//! responses are rendered by third-party clients that cannot resolve that
+//! markup. So over MCP the model must link items as plain Markdown URLs built
+//! from the app base URL, and list multiple items as a Markdown table.
+//!
+//! The base URL is only known at runtime (from `APP_BASE_URL`), so this section
+//! is rendered by [`render`] rather than declared as a `'static` prompt.
+
+static TITLE: &str = "Linking to and listing Macro items";
+
+/// Renders the MCP item-linking section, interpolating `base_url` (already
+/// trimmed of any trailing slash) into the example URLs. The output matches the
+/// `# {title}\n{body}` shape of the other prompt sections so it composes
+/// cleanly with them.
+pub fn render(base_url: &str) -> String {
+ format!(
+ "# {TITLE}\n\
+ When referring the user to a Macro item (document, channel, chat, \
+ project, task, or email thread) in your responses, write a plain URL of \
+ the form `{base_url}/app/<type>/<id>`, where `<type>` is the item's type \
+ — `md` for a document, `channel`, `chat`, `project`, `task`, or `email` \
+ for an email thread — and `<id>` is the item id. Render it as a normal \
+ Markdown link, e.g. `[Name]({base_url}/app/md/<id>)`.\n\
+ \n\
+ Do NOT emit `<m-document-mention>` XML tags or any other Macro internal \
+ mention/markup format in these responses. Those only render inside the \
+ Macro app and appear as raw text to MCP clients. This rule overrides any \
+ other instruction about mention tags: over MCP, always link items with \
+ plain Markdown URLs, never mention tags.\n\
+ \n\
+ When you list multiple Macro items, present them as a Markdown table with \
+ the columns `number`, `name`, and `link`, where `link` is the \
+ `{base_url}/app/<type>/<id>` URL for each item.\n"
+ )
+}