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): give ChatGPT the standard tools under ai-code-mode (#9513)

Eric Hayesc41a3adc8b45
diff --git a/services/agent_harness_service/src/public_mcp/tool_service.rs b/services/agent_harness_service/src/public_mcp/tool_service.rs
index de8b6a1a66..279d05ac6d 100644
--- a/services/agent_harness_service/src/public_mcp/tool_service.rs
+++ b/services/agent_harness_service/src/public_mcp/tool_service.rs
@@ -130,7 +130,16 @@ impl<Context> AuthenticatedToolService<Context> {
}
}
- async fn tool_mode(&self, user_id: &MacroUserIdStr<'static>) -> ToolMode {
+ /// The mode for one request. ChatGPT always gets the standard tools: its
+ /// plugin directory refuses generic executors, whatever the user's flag.
+ async fn tool_mode(
+ &self,
+ user_id: &MacroUserIdStr<'static>,
+ extensions: &rmcp::model::Extensions,
+ ) -> ToolMode {
+ if caller(extensions).is_ok_and(|caller| may_be_chatgpt(&caller)) {
+ return ToolMode::Standard;
+ }
match &self.code_mode {
Some(code_mode) => code_mode.selector.select(user_id).await,
None => ToolMode::Standard,
@@ -165,6 +174,30 @@ fn caller(extensions: &rmcp::model::Extensions) -> Result<ToolCaller, rmcp::Erro
})
}
+/// Tools ChatGPT is never offered. Each one runs code or hands an open-ended
+/// task to an agent, and the plugin directory refuses generic executors.
+const HIDDEN_FROM_CHATGPT: [&str; 6] = [
+ "BashCodeExecution",
+ "CreateAgentSession",
+ "DispatchCodingAgent",
+ "ManageAgentSession",
+ "Subagent",
+ "TextEditorCodeExecution",
+];
+
+/// Whether an OAuth caller might be ChatGPT: registered under its name, or
+/// holding a token with no recorded client, which fails closed until the next
+/// refresh records one. The name is self-reported, which is fine here: this
+/// picks how tools are presented and grants nothing, and a client naming itself
+/// ChatGPT only loses code mode and the tools above.
+fn may_be_chatgpt(caller: &ToolCaller) -> bool {
+ match caller {
+ ToolCaller::McpClient { name: None } => true,
+ ToolCaller::McpClient { name: Some(name) } => name.eq_ignore_ascii_case("chatgpt"),
+ ToolCaller::Agent(_) | ToolCaller::ApiKey => false,
+ }
+}
+
#[cfg(test)]
mod test;
@@ -226,7 +259,7 @@ where
context.peer.set_peer_info(request);
}
let mut info = self.get_info();
- if self.tool_mode(&user_id).await == ToolMode::Code {
+ if self.tool_mode(&user_id, &context.extensions).await == ToolMode::Code {
let base_url = self.item_base_url.trim_end_matches('/');
info.instructions = Some(format!(
"{}\n\n{}",
@@ -243,10 +276,14 @@ where
context: rmcp::service::RequestContext<rmcp::RoleServer>,
) -> Result<ListToolsResult, rmcp::ErrorData> {
let user_id = Self::authenticated_user_id(&context.extensions)?;
- let mode = self.tool_mode(&user_id).await;
+ let mode = self.tool_mode(&user_id, &context.extensions).await;
+ let mut tools = self.tool_definitions_for(mode);
+ if caller(&context.extensions).is_ok_and(|caller| may_be_chatgpt(&caller)) {
+ tools.retain(|tool| !HIDDEN_FROM_CHATGPT.contains(&tool.name.as_ref()));
+ }
Ok(ListToolsResult {
- tools: self.tool_definitions_for(mode),
+ tools,
..Default::default()
})
}
@@ -296,6 +333,16 @@ where
Err(error) => return (ToolCallOutcome::InvalidRequest, Err(error)),
};
+ if may_be_chatgpt(&caller) && HIDDEN_FROM_CHATGPT.contains(&request.name.as_ref()) {
+ return (
+ ToolCallOutcome::InvalidRequest,
+ Err(rmcp::ErrorData::resource_not_found(
+ format!("tool {} not found", request.name),
+ None,
+ )),
+ );
+ }
+
let Some(arguments) = request.arguments.map(serde_json::Value::Object) else {
return (
ToolCallOutcome::InvalidRequest,
@@ -306,7 +353,7 @@ where
if let Some(code_mode) = &self.code_mode
&& code_mode.tools.tools.contains_key(request.name.as_ref())
{
- if self.tool_mode(&user_id).await != ToolMode::Code {
+ if self.tool_mode(&user_id, &context.extensions).await != ToolMode::Code {
return (
ToolCallOutcome::InvalidRequest,
Err(rmcp::ErrorData::resource_not_found(
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.rs
index 630dbb6345..14cc57048a 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
@@ -116,6 +116,14 @@ async fn empty_toolset_lists_no_tools() {
/// the applicable `readOnlyHint`/`destructiveHint`, and any tool name over 64
/// characters. Assert it against the toolset the server actually exposes, so a
/// newly added tool can't quietly regress the submission.
+#[test]
+fn tools_hidden_from_chatgpt_still_exist() {
+ let tools = ai_tools::tools_for(ai_tools::AiHost::Mcp);
+ for name in HIDDEN_FROM_CHATGPT {
+ assert!(tools.toolset.tools.contains_key(name), "{name} was renamed");
+ }
+}
+
#[test]
fn every_exposed_tool_meets_directory_requirements() {
let tools = ai_tools::tools_for(ai_tools::AiHost::Mcp);
@@ -405,7 +413,32 @@ mod code_mode {
}
}
- const DIRECT_TOOLS: [&str; 4] = [
+ #[derive(Deserialize, schemars::JsonSchema)]
+ #[schemars(title = "Subagent", description = "Delegate a task.")]
+ struct Subagent {}
+ impl ToolAnnotated for Subagent {
+ const ANNOTATIONS: ToolAnnotations = ToolAnnotations::destructive("Delegate a task");
+ }
+ #[async_trait::async_trait]
+ impl ai_toolset::AsyncTool<()> for Subagent {
+ 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; 5] = [
+ "EditDocument",
+ "ListCalendarEvents",
+ "NameSearch",
+ "ReadContent",
+ "Subagent",
+ ];
+ const CHATGPT_TOOLS: [&str; 4] = [
"EditDocument",
"ListCalendarEvents",
"NameSearch",
@@ -418,7 +451,8 @@ mod code_mode {
.add_tool::<NameSearch, ()>()
.add_tool::<ReadContent, ()>()
.add_tool::<EditDocument, ()>()
- .add_tool::<ListCalendarEvents, ()>(),
+ .add_tool::<ListCalendarEvents, ()>()
+ .add_tool::<Subagent, ()>(),
)
}
@@ -509,6 +543,94 @@ mod code_mode {
serde_json::from_slice(&bytes).unwrap()
}
+ fn mcp_client(name: &str) -> Option<ToolCaller> {
+ Some(ToolCaller::McpClient {
+ name: Some(name.to_owned()),
+ })
+ }
+
+ #[tokio::test]
+ async fn chatgpt_lists_standard_tools_for_a_flagged_user() {
+ let app = app(Arc::new(Runs::default()));
+ let listed = rpc_as(
+ app.clone(),
+ FLAGGED,
+ mcp_client("ChatGPT"),
+ "tools/list",
+ serde_json::json!({}),
+ )
+ .await;
+ assert_eq!(tool_names(&listed), CHATGPT_TOOLS);
+
+ let nameless = rpc_as(
+ app.clone(),
+ FLAGGED,
+ Some(ToolCaller::McpClient { name: None }),
+ "tools/list",
+ serde_json::json!({}),
+ )
+ .await;
+ assert_eq!(tool_names(&nameless), CHATGPT_TOOLS);
+
+ let other = rpc_as(
+ app,
+ FLAGGED,
+ mcp_client("Claude"),
+ "tools/list",
+ serde_json::json!({}),
+ )
+ .await;
+ assert!(tool_names(&other).contains(&"ExecuteCode".to_owned()));
+ }
+
+ #[tokio::test]
+ async fn chatgpt_cannot_call_a_hidden_tool() {
+ let response = rpc_as(
+ app(Arc::new(Runs::default())),
+ UNFLAGGED,
+ mcp_client("ChatGPT"),
+ "tools/call",
+ serde_json::json!({"name": "Subagent", "arguments": {}}),
+ )
+ .await;
+ assert!(
+ response["error"]["message"]
+ .as_str()
+ .is_some_and(|message| message.contains("not found")),
+ "{response}"
+ );
+
+ let other = rpc_as(
+ app(Arc::new(Runs::default())),
+ UNFLAGGED,
+ mcp_client("Claude"),
+ "tools/call",
+ serde_json::json!({"name": "Subagent", "arguments": {}}),
+ )
+ .await;
+ assert!(other.get("error").is_none(), "{other}");
+ }
+
+ #[tokio::test]
+ async fn chatgpt_cannot_run_code_for_a_flagged_user() {
+ let runs = Arc::new(Runs::default());
+ let response = rpc_as(
+ app(runs.clone()),
+ FLAGGED,
+ mcp_client("ChatGPT"),
+ "tools/call",
+ serde_json::json!({"name": "ExecuteCode", "arguments": {"source": "1"}}),
+ )
+ .await;
+ assert!(
+ response["error"]["message"]
+ .as_str()
+ .is_some_and(|message| message.contains("not found")),
+ "{response}"
+ );
+ assert!(runs.0.lock().unwrap().is_empty());
+ }
+
fn tool_names(response: &serde_json::Value) -> Vec<String> {
let mut names: Vec<String> = response["result"]["tools"]
.as_array()