Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

fix(codex): restore namespace MCP tools + hosted-tool whitelist (regression from #1581) (#1715)

vanminhphe6e7f8d402bf
diff --git a/open-sse/executors/codex.ts b/open-sse/executors/codex.ts
index ff9bd55489..7930888432 100644
--- a/open-sse/executors/codex.ts
+++ b/open-sse/executors/codex.ts
@@ -410,6 +410,23 @@ function stripStoredItemReferences(body: Record<string, unknown>): void {
}
}
+// Responses-API hosted tool types that OpenAI/Codex executes server-side.
+// These arrive shaped as `{ type, ...params }` with no `function` object and no `name` —
+// e.g. Codex CLI injects `{ type: "image_generation", output_format: "png" }` or
+// `{ type: "namespace", name: "mcp__atlassian__", tools: [...] }` for MCP tool groups.
+// Keep them through `normalizeCodexTools` so upstream can execute them.
+const CODEX_HOSTED_TOOL_TYPES: ReadonlySet<string> = new Set([
+ "image_generation",
+ "web_search",
+ "web_search_preview",
+ "file_search",
+ "computer",
+ "computer_use_preview",
+ "code_interpreter",
+ "mcp",
+ "local_shell",
+]);
+
function normalizeCodexTools(body: Record<string, unknown>): void {
if (!Array.isArray(body.tools)) return;
@@ -420,7 +437,33 @@ function normalizeCodexTools(body: Record<string, unknown>): void {
}
const tool = toolValue as Record<string, unknown>;
- if (tool.type !== "function") {
+ const toolType = typeof tool.type === "string" ? tool.type : "";
+
+ // Preserve namespace tools (MCP tool groups used by Codex/OpenAI Responses API).
+ // Codex API supports them natively; register sub-tool names for tool_choice validation.
+ if (toolType === "namespace") {
+ if (Array.isArray(tool.tools)) {
+ for (const st of tool.tools as unknown[]) {
+ if (st && typeof st === "object" && !Array.isArray(st)) {
+ const subTool = st as Record<string, unknown>;
+ const name = typeof subTool.name === "string" ? subTool.name.trim() : "";
+ if (name) validToolNames.add(name);
+ }
+ }
+ }
+ return true;
+ }
+
+ if (toolType !== "function") {
+ const hasFunctionObject = tool.function && typeof tool.function === "object";
+ const hasName = typeof tool.name === "string";
+ if (!toolType || hasFunctionObject || hasName) {
+ return false;
+ }
+ if (CODEX_HOSTED_TOOL_TYPES.has(toolType)) {
+ return true;
+ }
+ console.debug(`[Codex] dropping unknown hosted tool type: ${toolType}`);
return false;
}
diff --git a/tests/unit/executor-codex.test.ts b/tests/unit/executor-codex.test.ts
index 78d262bd2b..68b5ed6997 100644
--- a/tests/unit/executor-codex.test.ts
+++ b/tests/unit/executor-codex.test.ts
@@ -353,6 +353,51 @@ test("CodexExecutor.execute falls back to HTTP when websocket transport is unava
}
});
+test("CodexExecutor.transformRequest preserves namespace MCP tools and hosted tool types", () => {
+ // Regression: PR #1581 đã vô tình xoá nhánh `namespace` + whitelist hosted tools
+ // trong normalizeCodexTools, khiến MCP tool group (vd. mcp__atlassian__) bị strip
+ // trước khi forward lên Codex Responses API. Test này khoá lại hành vi đúng.
+ const executor = new CodexExecutor();
+ const result = executor.transformRequest(
+ "gpt-5.4",
+ {
+ model: "gpt-5.4",
+ input: [],
+ tools: [
+ { type: "function", name: "exec_command", parameters: { type: "object" } },
+ {
+ type: "namespace",
+ name: "mcp__atlassian__",
+ description: "Tools in the mcp__atlassian__ namespace.",
+ tools: [
+ { type: "function", name: "jira_get_issue", parameters: { type: "object" } },
+ { type: "function", name: "jira_search", parameters: { type: "object" } },
+ ],
+ },
+ { type: "image_generation", output_format: "png" },
+ { type: "web_search" },
+ { type: "unknown_hosted_tool" },
+ ],
+ tool_choice: { type: "function", name: "jira_get_issue" },
+ },
+ false,
+ {}
+ );
+
+ const types = (result.tools as Array<Record<string, unknown>>).map((tool) => tool.type);
+ assert.deepEqual(types, ["function", "namespace", "image_generation", "web_search"]);
+
+ const namespaceTool = (result.tools as Array<Record<string, unknown>>).find(
+ (tool) => tool.type === "namespace"
+ );
+ assert.equal((namespaceTool as { name: string }).name, "mcp__atlassian__");
+ assert.equal(((namespaceTool as { tools: unknown[] }).tools ?? []).length, 2);
+
+ // tool_choice trỏ vào sub-tool của namespace phải được giữ nguyên (không bị xoá
+ // do tên nằm trong namespace.tools[*].name đã được đăng ký vào validToolNames).
+ assert.deepEqual(result.tool_choice, { type: "function", name: "jira_get_issue" });
+});
+
test("CodexExecutor maps Codex websocket error events to response.failed SSE", () => {
const raw = JSON.stringify({
type: "error",