Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(mcp): list only code mode tools on the public server (#8950)
Wolf Mermelstein67fdcab86b0e
diff --git a/crates/ai_tools/src/lib.rs b/crates/ai_tools/src/lib.rsindex f7f670fe46..78f9a678a0 100644--- a/crates/ai_tools/src/lib.rs+++ b/crates/ai_tools/src/lib.rs@@ -274,12 +274,6 @@ pub fn tools_for_agent_code_mode() -> ToolSetWithPrompt {tools_for_inner(AiHost::AgentSession, true)}-/// Public MCP tools for users with code mode: SDK-covered tools are hidden in-/// favor of `ExecuteCode`, as in agent sessions.-pub fn tools_for_mcp_code_mode() -> ToolSetWithPrompt {- tools_for_inner(AiHost::Mcp, true)-}-/// Existing agent-session tools exposed through the code-mode capability policy.pub fn code_mode_tools() -> Arc<AiToolSet> {tools_for(AiHost::AgentSession).toolsetdiff --git a/crates/ai_tools/src/test.rs b/crates/ai_tools/src/test.rsindex 4f3cc73977..8bd64e7193 100644--- a/crates/ai_tools/src/test.rs+++ b/crates/ai_tools/src/test.rs@@ -770,29 +770,3 @@ fn conversation_tools_only_advertise_new_sessions() {}assert!(!subagent_toolset().tools.contains_key("ReadChat"));}--#[test]-fn mcp_code_mode_hides_sdk_covered_tools_and_keeps_the_rest() {- let standard = tools_for(AiHost::Mcp).toolset;- let code = tools_for_mcp_code_mode().toolset;- for hidden in ["ReadContent", "ReadChannelMessages", "ListCalendarEvents"] {- assert!(standard.tools.contains_key(hidden));- assert!(- !code.tools.contains_key(hidden),- "{hidden} should be hidden"- );- }- for kept in [- "NameSearch",- "ContentSearch",- "ListEntities",- "EditDocument",- ] {- assert!(code.tools.contains_key(kept), "{kept} should stay direct");- }- assert!(- code.tools- .keys()- .all(|name| standard.tools.contains_key(name))- );-}diff --git a/docs/agent-guide/ai-chat.md b/docs/agent-guide/ai-chat.mdindex 76eab56123..9b0710310d 100644--- a/docs/agent-guide/ai-chat.md+++ b/docs/agent-guide/ai-chat.md@@ -1113,18 +1113,22 @@ provider usage and completed writes may remain after cancellation.The public workspace MCP (`mcp-server[-dev].macro.com`) evaluates the same`ai-code-mode` flag for the OAuth user on `initialize`, `tools/list` and-`tools/call`, cached per user for a minute. Enabled users see `ExecuteCode`,-`DescribeCodeTools` and `ReadCodeDocs` in place of the SDK-covered native tools,-and the server instructions tell the client to call `DescribeCodeTools` with a-`task`, then `ReadCodeDocs` for the symbols it uses, before writing a program. Programs export `async function main({macro})` and return-exactly `{data}` (an empty `reviews: []` is dropped). There is no session,-execution journal, review stack or `execution_id`; the receipt carries status,-observed SDK activity, result and error. Runs act as the user with no agent-attribution and no owner approval card: the MCP client asks before each tool-call. SDK writes, including email sends, take effect immediately. Every native-tool stays callable; MCP clients cache tool lists, so a flag change appears after-reconnecting. Public runs use their own Deno budget (four running, eight queued-per replica) so external traffic cannot take agent sessions' slots.+`tools/call`, cached per user for a minute. Enabled users see only+`ExecuteCode`, `DescribeCodeTools` and `ReadCodeDocs` on `tools/list` — not the+individual Macro tools — and the server instructions tell the client to call+`DescribeCodeTools` with a `task`, then `ReadCodeDocs` for the symbols it uses,+before writing a program. Programs export `async function main({macro})` and+return exactly `{data}` (an empty `reviews: []` is dropped). There is no+session, execution journal, review stack or `execution_id`; the receipt carries+status, observed SDK activity, result and error. Runs act as the user with no+agent attribution and no owner approval card: the MCP client asks before each+tool call. SDK writes, including email sends, take effect immediately. Hidden+native tools stay callable if a client already knows their names; MCP clients+cache tool lists, so a flag change appears after reconnecting. Public runs use+their own Deno budget (four running, eight queued per replica) so external+traffic cannot take agent sessions' slots. Agent-session code mode is+unchanged: it still keeps a reduced direct toolset for search, document+editing, and other work outside the SDK.To verify locally, run Claude Code against the stack's MCP with`tooling/scripts/claude-local-mcp.sh --instance <name> --email <email>`. It mintsdiff --git a/docs/design/public-mcp-code-mode.md b/docs/design/public-mcp-code-mode.mdindex 1fe8c6bf76..02f68c6dbe 100644--- a/docs/design/public-mcp-code-mode.md+++ b/docs/design/public-mcp-code-mode.md@@ -62,10 +62,11 @@ Evaluate `ai-code-mode` for the OAuth user in `initialize`, `list_tools` and `careusing `agent_inmem::domain::tool_mode::FlaggedToolMode`.- Off: unchanged.-- On: `tools_for_inner(AiHost::Mcp, true)` (new `ai_tools::tools_for_mcp_code_mode`),- which already drops SDK-covered tools via `covered_by_code_sdk`, plus- `ExecuteCode` and `DescribeCodeTools`. `initialize` (which, unlike- `get_info`, sees the request) appends code-mode instructions.+- On: `tools/list` is `ExecuteCode`, `DescribeCodeTools` and `ReadCodeDocs` only+ (`agent_code_mode::inbound::toolset::user_toolset`). Individual Macro tools+ are not advertised. `initialize` (which, unlike `get_info`, sees the+ request) replaces the standard tool-catalog instructions with code-mode+ instructions.- `call_tool` rejects `ExecuteCode` / `DescribeCodeTools` for unflagged users.Hidden native tools stay callable; they are the same tools.diff --git a/services/agent_harness_service/AGENTS.md b/services/agent_harness_service/AGENTS.mdindex e14ac41288..746794b43a 100644--- a/services/agent_harness_service/AGENTS.md+++ b/services/agent_harness_service/AGENTS.md@@ -24,8 +24,9 @@ Pulumi stacks; they do not host extra agent processes.- `src/internal_mcp.rs`: the internal MCP server exposed to running sessions.- `src/public_mcp.rs` + `src/public_mcp/`: public workspace MCP and OAuth;reuses the harness tool context and retains bearer authentication. Users with- `ai-code-mode` get `ExecuteCode`/`DescribeCodeTools`/`ReadCodeDocs` in place of SDK-covered- tools (`tool_service.rs`); the flag is cached per user (`tool_mode_cache.rs`).+ `ai-code-mode` get only `ExecuteCode`/`DescribeCodeTools`/`ReadCodeDocs` on+ `tools/list` (`tool_service.rs`); hidden native tools stay callable. The flag+ is cached per user (`tool_mode_cache.rs`).`src/public_mcp/analytics.rs` sends PostHog `$mcp_initialize` /`$mcp_tool_call` events (PostHog MCP analytics schema) in the background,with the user's team as the `team` group. At most 64 sends run at oncediff --git a/services/agent_harness_service/src/main.rs b/services/agent_harness_service/src/main.rsindex c1776acc38..bbbb0fce13 100644--- a/services/agent_harness_service/src/main.rs+++ b/services/agent_harness_service/src/main.rs@@ -636,8 +636,8 @@ async fn run() -> anyhow::Result<()> {}None => None,};- // `ai-code-mode` decides, per user, whether agent sessions and the public- // MCP present the SDK in place of the tools it covers.+ // `ai-code-mode` decides, per user, whether agent sessions hide+ // SDK-covered tools and whether the public MCP lists only code mode.let code_mode_selector: Option<Arc<dyn agent_inmem::domain::tool_mode::ToolModeSelector>> =match (&code_runner, config.posthog_api_key.value()) {(Some(_), Some(key)) => {diff --git a/services/agent_harness_service/src/public_mcp.rs b/services/agent_harness_service/src/public_mcp.rsindex 0157e88618..7a5821a34b 100644--- a/services/agent_harness_service/src/public_mcp.rs+++ b/services/agent_harness_service/src/public_mcp.rs@@ -74,7 +74,6 @@ pub async fn router(let tools = ai_tools::tools_for(ai_tools::AiHost::Mcp).toolset;let code_mode = code_mode.map(|code_mode| {Arc::new(CodeModeSurface {- native: ai_tools::tools_for_mcp_code_mode().toolset,tools: Arc::new(agent_code_mode::inbound::toolset::user_toolset()),service: code_mode.service,cognition: tool_context.cognition_tool_context.service.clone(),diff --git a/services/agent_harness_service/src/public_mcp/code_mode_instructions.md b/services/agent_harness_service/src/public_mcp/code_mode_instructions.mdindex f26b3a3cda..abb5ba0959 100644--- a/services/agent_harness_service/src/public_mcp/code_mode_instructions.md+++ b/services/agent_harness_service/src/public_mcp/code_mode_instructions.md@@ -1 +1 @@-Code mode is on. ExecuteCode runs a JavaScript program against the full Macro SDK as the user; prefer it for multi-step, bulk or filtered work, and for anything the direct tools do not cover. Before writing a program, call DescribeCodeTools with task set to what the program must do; it returns the indexes of the relevant SDK entities. Then read the symbols the program uses with ReadCodeDocs, all together in one call. Do not guess SDK methods or options. If a program fails with an SDK error, read the symbol involved and fix the program. Direct tools remain for search (NameSearch, ContentSearch, ListEntities), document editing and other work outside the SDK.+Code mode is on. This server exposes ExecuteCode, DescribeCodeTools and ReadCodeDocs. ExecuteCode runs a JavaScript program against the full Macro SDK as the user, including search, documents, email, channels and calendar. Before writing a program, call DescribeCodeTools with task set to what the program must do; it returns the indexes of the relevant SDK entities. Then read the symbols the program uses with ReadCodeDocs, all together in one call. Do not guess SDK methods or options. If a program fails with an SDK error, read the symbol involved and fix the program.diff --git a/services/agent_harness_service/src/public_mcp/tool_service.rs b/services/agent_harness_service/src/public_mcp/tool_service.rsindex 4206967c54..6028dbd2cb 100644--- a/services/agent_harness_service/src/public_mcp/tool_service.rs+++ b/services/agent_harness_service/src/public_mcp/tool_service.rs@@ -59,10 +59,8 @@ impl CalledBy for () {fn called_by(self, _caller: &ToolCaller) -> Self {}}-/// Code mode for flagged users: SDK-covered native tools give way to the SDK.-pub struct CodeModeSurface<Context> {- /// Native tools still listed in code mode, from `tools_for_mcp_code_mode`.- pub native: Arc<AsyncToolCollection<Context>>,+/// Code mode for flagged users: `tools/list` is the SDK entrypoints only.+pub struct CodeModeSurface {/// `ExecuteCode`, `DescribeCodeTools` and `ReadCodeDocs`.pub tools: Arc<AsyncToolCollection<UserCodeModeToolContext>>,/// User-scoped execution.@@ -88,7 +86,7 @@ pub struct AuthenticatedToolService<Context> {/// Static file CDN used for attachments returned by authorized channel reads.static_file_base_url: url::Url,/// Present when this deployment can run code.- code_mode: Option<Arc<CodeModeSurface<Context>>>,+ code_mode: Option<Arc<CodeModeSurface>>,/// Records sessions and tool calls to product analytics.analytics: McpAnalytics,}@@ -113,7 +111,7 @@ impl<Context> AuthenticatedToolService<Context> {}/// Offer code mode to users with the `ai-code-mode` flag.- pub fn with_code_mode(mut self, code_mode: Arc<CodeModeSurface<Context>>) -> Self {+ pub fn with_code_mode(mut self, code_mode: Arc<CodeModeSurface>) -> Self {self.code_mode = Some(code_mode);self}@@ -122,13 +120,12 @@ impl<Context> AuthenticatedToolService<Context> {definitions(&self.toolset).collect()}- /// Tools listed for a mode. Code mode lists the reduced native set plus the- /// SDK tools; every native tool stays callable either way.+ /// Tools listed for a mode. Code mode lists only `ExecuteCode` and+ /// `DescribeCodeTools`. Native tools stay callable if a client already+ /// knows their names.fn tool_definitions_for(&self, mode: ToolMode) -> Vec<Tool> {match (mode, &self.code_mode) {- (ToolMode::Code, Some(code_mode)) => definitions(&code_mode.native)- .chain(definitions(&code_mode.tools))- .collect(),+ (ToolMode::Code, Some(code_mode)) => definitions(&code_mode.tools).collect(),_ => self.tool_definitions(),}}@@ -229,11 +226,13 @@ wherecontext.peer.set_peer_info(request);}let mut info = self.get_info();- if self.tool_mode(&user_id).await == ToolMode::Code- && let Some(instructions) = info.instructions.as_mut()- {- instructions.push_str("\n\n");- instructions.push_str(include_str!("code_mode_instructions.md"));+ if self.tool_mode(&user_id).await == ToolMode::Code {+ let base_url = self.item_base_url.trim_end_matches('/');+ info.instructions = Some(format!(+ "{}\n\n{}",+ include_str!("code_mode_instructions.md").trim(),+ prompt::mcp_instructions(base_url),+ ));}Ok(info)}diff --git a/services/agent_harness_service/src/public_mcp/tool_service/test.rs b/services/agent_harness_service/src/public_mcp/tool_service/test.rsindex 51e51e242f..630dbb6345 100644--- a/services/agent_harness_service/src/public_mcp/tool_service/test.rs+++ b/services/agent_harness_service/src/public_mcp/tool_service/test.rs@@ -271,7 +271,8 @@ mod code_mode {use agent_code_mode::domain::{CodeModeError, ExecutionStatus, UserCodeMode, UserExecutionReceipt,};- use agent_inmem::domain::tool_mode::{ToolMode, ToolModeSelector};+ use agent_inmem::domain::tool_mode::ToolModeSelector;+ use ai_toolset::{ToolAnnotated, ToolAnnotations};use axum::{body::{Body, to_bytes},http::Request,@@ -280,6 +281,7 @@ mod code_mode {use rmcp::transport::streamable_http_server::{StreamableHttpServerConfig, StreamableHttpService, session::local::LocalSessionManager,};+ use serde::Deserialize;use std::{future::Future, pin::Pin, sync::Mutex};use tokio_util::sync::CancellationToken;use tower::ServiceExt;@@ -330,9 +332,108 @@ mod code_mode {}}+ /// Direct tools that used to leak next to code mode on `tools/list`.+ #[derive(Deserialize, schemars::JsonSchema)]+ #[schemars(title = "NameSearch", description = "Search by name.")]+ struct NameSearch {}+ impl ToolAnnotated for NameSearch {+ const ANNOTATIONS: ToolAnnotations = ToolAnnotations::read_only("Search by name");+ }+ #[async_trait::async_trait]+ impl ai_toolset::AsyncTool<()> for NameSearch {+ type Output = serde_json::Value;+ async fn call(+ &self,+ _: ai_toolset::ServiceContext<()>,+ _: ai_toolset::RequestContext,+ ) -> ai_toolset::ToolResult<Self::Output> {+ Ok(serde_json::json!({"ok": true}))+ }+ }++ #[derive(Deserialize, schemars::JsonSchema)]+ #[schemars(title = "ReadContent", description = "Read a document.")]+ struct ReadContent {}+ impl ToolAnnotated for ReadContent {+ const ANNOTATIONS: ToolAnnotations = ToolAnnotations::read_only("Read document");+ }+ #[async_trait::async_trait]+ impl ai_toolset::AsyncTool<()> for ReadContent {+ type Output = serde_json::Value;+ async fn call(+ &self,+ _: ai_toolset::ServiceContext<()>,+ _: ai_toolset::RequestContext,+ ) -> ai_toolset::ToolResult<Self::Output> {+ Ok(serde_json::json!({"ok": true}))+ }+ }++ #[derive(Deserialize, schemars::JsonSchema)]+ #[schemars(title = "EditDocument", description = "Edit a document.")]+ struct EditDocument {}+ impl ToolAnnotated for EditDocument {+ const ANNOTATIONS: ToolAnnotations = ToolAnnotations::destructive("Edit document");+ }+ #[async_trait::async_trait]+ impl ai_toolset::AsyncTool<()> for EditDocument {+ type Output = serde_json::Value;+ async fn call(+ &self,+ _: ai_toolset::ServiceContext<()>,+ _: ai_toolset::RequestContext,+ ) -> ai_toolset::ToolResult<Self::Output> {+ Ok(serde_json::json!({"ok": true}))+ }+ }++ #[derive(Deserialize, schemars::JsonSchema)]+ #[schemars(title = "ListCalendarEvents", description = "List calendar events.")]+ struct ListCalendarEvents {}+ impl ToolAnnotated for ListCalendarEvents {+ const ANNOTATIONS: ToolAnnotations = ToolAnnotations::read_only("List calendar events");+ }+ #[async_trait::async_trait]+ impl ai_toolset::AsyncTool<()> for ListCalendarEvents {+ type Output = serde_json::Value;+ async fn call(+ &self,+ _: ai_toolset::ServiceContext<()>,+ _: ai_toolset::RequestContext,+ ) -> ai_toolset::ToolResult<Self::Output> {+ Ok(serde_json::json!({"ok": true}))+ }+ }++ const DIRECT_TOOLS: [&str; 4] = [+ "EditDocument",+ "ListCalendarEvents",+ "NameSearch",+ "ReadContent",+ ];++ fn catalog() -> Arc<AsyncToolCollection<()>> {+ Arc::new(+ AsyncToolCollection::new()+ .add_tool::<NameSearch, ()>()+ .add_tool::<ReadContent, ()>()+ .add_tool::<EditDocument, ()>()+ .add_tool::<ListCalendarEvents, ()>(),+ )+ }++ fn catalog_service() -> AuthenticatedToolService<()> {+ AuthenticatedToolService::new(+ catalog(),+ (),+ "https://macro.com".to_owned(),+ url::Url::parse("https://static-file-service.macro.com").unwrap(),+ McpAnalytics::disabled(),+ )+ }+fn app(runs: Arc<dyn UserCodeMode>) -> axum::Router {let surface = Arc::new(CodeModeSurface {- native: Arc::new(AsyncToolCollection::new()),tools: Arc::new(agent_code_mode::inbound::toolset::user_toolset()),service: runs,cognition: None,@@ -343,7 +444,7 @@ mod code_mode {config.stateful_mode = false;config.json_response = true;let service = StreamableHttpService::new(- move || Ok(empty_service().with_code_mode(surface.clone())),+ move || Ok(catalog_service().with_code_mode(surface.clone())),Arc::new(LocalSessionManager::default()),config,);@@ -428,8 +529,19 @@ mod code_mode {tool_names(&flagged),["DescribeCodeTools", "ExecuteCode", "ReadCodeDocs"]);+ for leaked in DIRECT_TOOLS {+ assert!(+ !tool_names(&flagged).iter().any(|name| name == leaked),+ "{leaked} must not appear on the code-mode catalog"+ );+ }let unflagged = rpc(app.clone(), UNFLAGGED, "tools/list", serde_json::json!({})).await;- assert!(tool_names(&unflagged).is_empty());+ assert_eq!(tool_names(&unflagged), DIRECT_TOOLS);+ assert!(+ !tool_names(&unflagged)+ .iter()+ .any(|name| name == "ExecuteCode")+ );let initialize = serde_json::json!({"protocolVersion": "2025-06-18",@@ -437,19 +549,26 @@ mod code_mode {"clientInfo": {"name": "test", "version": "1"},});let flagged = rpc(app.clone(), FLAGGED, "initialize", initialize.clone()).await;- assert!(- flagged["result"]["instructions"]- .as_str()- .unwrap()- .contains("Code mode is on")- );+ let flagged_instructions = flagged["result"]["instructions"].as_str().unwrap();+ assert!(flagged_instructions.contains("Code mode is on"));+ assert!(flagged_instructions.contains("ExecuteCode"));+ assert!(flagged_instructions.contains("DescribeCodeTools"));+ for leaked in [+ "NameSearch",+ "ReadContent",+ "ContentSearch",+ "Direct tools remain",+ ] {+ assert!(+ !flagged_instructions.contains(leaked),+ "code-mode initialize must not advertise {leaked}"+ );+ }let unflagged = rpc(app, UNFLAGGED, "initialize", initialize).await;- assert!(- !unflagged["result"]["instructions"]- .as_str()- .unwrap()- .contains("Code mode is on")- );+ let unflagged_instructions = unflagged["result"]["instructions"].as_str().unwrap();+ assert!(!unflagged_instructions.contains("Code mode is on"));+ assert!(unflagged_instructions.contains("ContentSearch"));+ assert!(unflagged_instructions.contains("NameSearch"));}#[tokio::test]@@ -465,7 +584,7 @@ mod code_mode {assert!(unflagged.get("error").is_some());assert!(runs.0.lock().unwrap().is_empty());- let flagged = rpc(app, FLAGGED, "tools/call", call).await;+ let flagged = rpc(app.clone(), FLAGGED, "tools/call", call).await;assert_eq!(flagged["result"]["structuredContent"]["status"],"succeeded"@@ -527,6 +646,22 @@ mod code_mode {assert!(runs.1.lock().unwrap().is_empty());}+ #[tokio::test]+ async fn hidden_native_tools_remain_callable_in_code_mode() {+ let app = app(Arc::new(Runs::default()));+ let result = rpc(+ app,+ FLAGGED,+ "tools/call",+ serde_json::json!({"name": "NameSearch", "arguments": {}}),+ )+ .await;+ assert_eq!(+ result["result"]["structuredContent"],+ serde_json::json!({"ok": true})+ );+ }+#[test]fn code_mode_tools_meet_directory_requirements() {for (name, tool) in agent_code_mode::inbound::toolset::user_toolset()