mirror of
https://github.com/getpaseo/paseo.git
synced 2026-12-01 04:36:26 +00:00
fix(codex): inject MCP servers as per-server config overrides (#11142)
Codex combines process-level -c overrides and thread/start request config
overrides sequentially in build_cli_overrides_layer. Sending a whole-table
mcp_servers object (including an empty {}) replaces the entire mcp_servers
table built from earlier -c mcp_servers.* flags, wiping out alias-configured
MCP servers.
Emit dotted mcp_servers.<name> keys (and omit MCP keys entirely when empty)
so Paseo-injected MCP servers and tool-policy preapprovals merge alongside
alias -c overrides and config.toml servers. Fall back to a single whole-table
mcp_servers only when a server name contains a dot, which Codex's
path.split('.') override parser cannot represent as one segment.
This commit is contained in:
@@ -149,26 +149,49 @@ describe("exact MCP preapproval mappings", () => {
|
||||
).toThrow("cannot preapprove exact MCP tools for unattended execution");
|
||||
});
|
||||
|
||||
test("Codex enables and approves only the granted server tool", () => {
|
||||
test("Codex enables and approves only the granted server tool without replacing mcp_servers", () => {
|
||||
expect(
|
||||
applyCodexToolPolicy(
|
||||
{
|
||||
mcp_servers: {
|
||||
hub: { url: "http://127.0.0.1/hub" },
|
||||
unrelated: { url: "http://127.0.0.1/unrelated" },
|
||||
},
|
||||
"mcp_servers.hub": { url: "http://127.0.0.1/hub" },
|
||||
"mcp_servers.unrelated": { url: "http://127.0.0.1/unrelated" },
|
||||
},
|
||||
hubPolicy,
|
||||
),
|
||||
).toEqual({
|
||||
"mcp_servers.hub": {
|
||||
url: "http://127.0.0.1/hub",
|
||||
enabled_tools: ["finish_execution"],
|
||||
default_tools_approval_mode: "prompt",
|
||||
tools: { finish_execution: { approval_mode: "approve" } },
|
||||
},
|
||||
"mcp_servers.unrelated": { url: "http://127.0.0.1/unrelated" },
|
||||
});
|
||||
});
|
||||
|
||||
test("Codex preapproval for an uninjected server uses dotted field keys and empty policy is a no-op", () => {
|
||||
expect(applyCodexToolPolicy({}, { preapproved: [] })).toEqual({});
|
||||
expect(
|
||||
applyCodexToolPolicy({ "mcp_servers.paseo": { url: "http://127.0.0.1/paseo" } }, hubPolicy),
|
||||
).toEqual({
|
||||
"mcp_servers.paseo": { url: "http://127.0.0.1/paseo" },
|
||||
"mcp_servers.hub.enabled_tools": ["finish_execution"],
|
||||
"mcp_servers.hub.default_tools_approval_mode": "prompt",
|
||||
"mcp_servers.hub.tools": { finish_execution: { approval_mode: "approve" } },
|
||||
});
|
||||
expect(
|
||||
applyCodexToolPolicy(
|
||||
{ "mcp_servers.paseo": { url: "http://127.0.0.1/paseo" } },
|
||||
{ preapproved: [{ kind: "mcp", server: "my.dotted", tool: "ping" }] },
|
||||
),
|
||||
).toEqual({
|
||||
mcp_servers: {
|
||||
hub: {
|
||||
url: "http://127.0.0.1/hub",
|
||||
enabled_tools: ["finish_execution"],
|
||||
paseo: { url: "http://127.0.0.1/paseo" },
|
||||
"my.dotted": {
|
||||
enabled_tools: ["ping"],
|
||||
default_tools_approval_mode: "prompt",
|
||||
tools: { finish_execution: { approval_mode: "approve" } },
|
||||
tools: { ping: { approval_mode: "approve" } },
|
||||
},
|
||||
unrelated: { url: "http://127.0.0.1/unrelated" },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1792,6 +1792,89 @@ describe("Codex app-server provider", () => {
|
||||
}
|
||||
});
|
||||
|
||||
test("injects MCP servers as per-server dotted overrides so alias -c mcp_servers.* entries survive", async () => {
|
||||
const appServer = createFakeCodexAppServer();
|
||||
const session = await createProviderWithFakeAppServer(appServer).createSession(
|
||||
createConfig({
|
||||
mcpServers: {
|
||||
paseo: { type: "http", url: "http://127.0.0.1:6767/mcp/agents?callerAgentId=agent-1" },
|
||||
"plugin-tool": { type: "stdio", command: "node", args: ["server.js"] },
|
||||
},
|
||||
}),
|
||||
);
|
||||
try {
|
||||
await session.startTurn("hello");
|
||||
const threadStart = appServer.requests().find((req) => req.method === "thread/start");
|
||||
const startConfig = (threadStart?.params as { config?: Record<string, unknown> } | undefined)
|
||||
?.config;
|
||||
expect(startConfig).toEqual({
|
||||
"mcp_servers.paseo": {
|
||||
url: "http://127.0.0.1:6767/mcp/agents?callerAgentId=agent-1",
|
||||
},
|
||||
"mcp_servers.plugin-tool": {
|
||||
command: "node",
|
||||
args: ["server.js"],
|
||||
},
|
||||
});
|
||||
expect(startConfig).not.toHaveProperty("mcp_servers");
|
||||
appServer.completeTurn();
|
||||
appServer.assertNoErrors();
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("omits mcp_servers from thread/start config when mcpServers is empty", async () => {
|
||||
const appServer = createFakeCodexAppServer();
|
||||
const session = await createProviderWithFakeAppServer(appServer).createSession(
|
||||
createConfig({ mcpServers: {} }),
|
||||
);
|
||||
try {
|
||||
await session.startTurn("hello");
|
||||
const threadStart = appServer.requests().find((req) => req.method === "thread/start");
|
||||
expect(
|
||||
(threadStart?.params as { config?: Record<string, unknown> } | undefined)?.config,
|
||||
).toBeUndefined();
|
||||
appServer.completeTurn();
|
||||
appServer.assertNoErrors();
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("falls back to whole-table mcp_servers when an injected server name contains a dot", async () => {
|
||||
const appServer = createFakeCodexAppServer();
|
||||
const session = await createProviderWithFakeAppServer(appServer).createSession(
|
||||
createConfig({
|
||||
mcpServers: {
|
||||
paseo: { type: "http", url: "http://127.0.0.1:6767/mcp/agents?callerAgentId=agent-1" },
|
||||
"my.dotted": { type: "http", url: "http://127.0.0.1:8080/mcp" },
|
||||
},
|
||||
}),
|
||||
);
|
||||
try {
|
||||
await session.startTurn("hello");
|
||||
const threadStart = appServer.requests().find((req) => req.method === "thread/start");
|
||||
const startConfig = (threadStart?.params as { config?: Record<string, unknown> } | undefined)
|
||||
?.config;
|
||||
expect(startConfig).toEqual({
|
||||
mcp_servers: {
|
||||
paseo: {
|
||||
url: "http://127.0.0.1:6767/mcp/agents?callerAgentId=agent-1",
|
||||
},
|
||||
"my.dotted": {
|
||||
url: "http://127.0.0.1:8080/mcp",
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(Object.keys(startConfig ?? {})).toEqual(["mcp_servers"]);
|
||||
appServer.completeTurn();
|
||||
appServer.assertNoErrors();
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("preapproves only granted tools on the injected Codex MCP server", async () => {
|
||||
const session = createSession({
|
||||
modeId: undefined,
|
||||
@@ -1813,21 +1896,27 @@ describe("Codex app-server provider", () => {
|
||||
|
||||
await session.startTurn("finish");
|
||||
|
||||
const turnStart = request.mock.calls.find(([method]) => method === "turn/start")?.[1];
|
||||
const turnStart = request.mock.calls.find(([method]) => method === "turn/start")?.[1] as {
|
||||
sandboxPolicy?: unknown;
|
||||
config?: Record<string, unknown>;
|
||||
};
|
||||
expect(turnStart).toMatchObject({
|
||||
sandboxPolicy: { type: "readOnly" },
|
||||
config: {
|
||||
sandbox_mode: "read-only",
|
||||
mcp_servers: {
|
||||
hub: {
|
||||
enabled_tools: ["finish_execution"],
|
||||
default_tools_approval_mode: "prompt",
|
||||
tools: { finish_execution: { approval_mode: "approve" } },
|
||||
},
|
||||
"mcp_servers.hub": {
|
||||
url: "http://127.0.0.1/hub",
|
||||
enabled_tools: ["finish_execution"],
|
||||
default_tools_approval_mode: "prompt",
|
||||
tools: { finish_execution: { approval_mode: "approve" } },
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(turnStart).not.toHaveProperty("config.mcp_servers.hub.tools.reply");
|
||||
expect(turnStart.config).not.toHaveProperty("mcp_servers");
|
||||
expect(
|
||||
(turnStart.config?.["mcp_servers.hub"] as { tools?: Record<string, unknown> } | undefined)
|
||||
?.tools,
|
||||
).not.toHaveProperty("reply");
|
||||
});
|
||||
|
||||
test("passes ephemeral: true to thread/start when constructed as ephemeral", async () => {
|
||||
@@ -3259,13 +3348,12 @@ describe("Codex app-server provider", () => {
|
||||
expect(forkConfigs).toEqual([
|
||||
expect.objectContaining({
|
||||
...customCodexConfig,
|
||||
mcp_servers: {
|
||||
paseo: expect.objectContaining({
|
||||
url: "http://127.0.0.1:6767/mcp/agents?callerAgentId=agent-1",
|
||||
}),
|
||||
},
|
||||
"mcp_servers.paseo": expect.objectContaining({
|
||||
url: "http://127.0.0.1:6767/mcp/agents?callerAgentId=agent-1",
|
||||
}),
|
||||
}),
|
||||
]);
|
||||
expect(forkConfigs[0]).not.toHaveProperty("mcp_servers");
|
||||
appServer.assertNoErrors();
|
||||
await session.close();
|
||||
},
|
||||
|
||||
@@ -6122,12 +6122,25 @@ export class CodexAppServerAgentSession implements AgentSession {
|
||||
if (this.deps.customCodexConfig) {
|
||||
Object.assign(innerConfig, this.deps.customCodexConfig);
|
||||
}
|
||||
if (this.config.mcpServers) {
|
||||
const mcpServers: Record<string, CodexMcpServerConfig> = {};
|
||||
for (const [name, serverConfig] of Object.entries(this.config.mcpServers)) {
|
||||
mcpServers[name] = toCodexMcpConfig(serverConfig);
|
||||
const mcpEntries = Object.entries(this.config.mcpServers ?? {});
|
||||
if (mcpEntries.length > 0) {
|
||||
// Emit per-server dotted overrides (`mcp_servers.<name>`) instead of a
|
||||
// whole-table `mcp_servers` object so alias-level `-c mcp_servers.*` CLI
|
||||
// overrides survive in Codex's `build_cli_overrides_layer`. Fall back to
|
||||
// a whole table only when a server name contains `.`, which Codex's
|
||||
// `path.split('.')` cannot address as a single segment.
|
||||
const hasDottedServerName = mcpEntries.some(([name]) => name.includes("."));
|
||||
if (hasDottedServerName) {
|
||||
const mcpServers: Record<string, CodexMcpServerConfig> = {};
|
||||
for (const [name, serverConfig] of mcpEntries) {
|
||||
mcpServers[name] = toCodexMcpConfig(serverConfig);
|
||||
}
|
||||
innerConfig.mcp_servers = mcpServers;
|
||||
} else {
|
||||
for (const [name, serverConfig] of mcpEntries) {
|
||||
innerConfig[`mcp_servers.${name}`] = toCodexMcpConfig(serverConfig);
|
||||
}
|
||||
}
|
||||
innerConfig.mcp_servers = mcpServers;
|
||||
}
|
||||
const configured = applyCodexToolPolicy(innerConfig, this.config.toolPolicy);
|
||||
return Object.keys(configured).length > 0 ? configured : null;
|
||||
|
||||
@@ -64,29 +64,73 @@ export const CodexProviderOptionsSchema = z
|
||||
|
||||
export type CodexProviderOptions = z.infer<typeof CodexProviderOptionsSchema>;
|
||||
|
||||
const MCP_SERVERS_PREFIX = "mcp_servers.";
|
||||
|
||||
function buildCodexToolApprovals(tools: string[]): Record<string, { approval_mode: "approve" }> {
|
||||
return Object.fromEntries(tools.map((tool) => [tool, { approval_mode: "approve" }]));
|
||||
}
|
||||
|
||||
function withCodexToolApprovals(
|
||||
serverConfig: Record<string, unknown>,
|
||||
tools: string[],
|
||||
): Record<string, unknown> {
|
||||
return {
|
||||
...serverConfig,
|
||||
enabled_tools: tools,
|
||||
default_tools_approval_mode: "prompt",
|
||||
tools: buildCodexToolApprovals(tools),
|
||||
};
|
||||
}
|
||||
|
||||
export function applyCodexToolPolicy(
|
||||
config: Record<string, unknown>,
|
||||
toolPolicy: ToolPolicy | undefined,
|
||||
): Record<string, unknown> {
|
||||
if (!toolPolicy) return config;
|
||||
const mcpServers = readRecord(config.mcp_servers);
|
||||
if (!toolPolicy || toolPolicy.preapproved.length === 0) return config;
|
||||
|
||||
const grantsByServer = new Map<string, string[]>();
|
||||
for (const grant of toolPolicy.preapproved) {
|
||||
const tools = grantsByServer.get(grant.server) ?? [];
|
||||
tools.push(grant.tool);
|
||||
grantsByServer.set(grant.server, tools);
|
||||
}
|
||||
for (const [server, tools] of grantsByServer) {
|
||||
const serverConfig = readRecord(mcpServers[server]);
|
||||
const approvals = Object.fromEntries(tools.map((tool) => [tool, { approval_mode: "approve" }]));
|
||||
mcpServers[server] = {
|
||||
...serverConfig,
|
||||
enabled_tools: tools,
|
||||
default_tools_approval_mode: "prompt",
|
||||
tools: approvals,
|
||||
};
|
||||
|
||||
// Codex's `apply_toml_override` splits dotted paths on `.`, so a server name
|
||||
// containing `.` cannot be addressed as `mcp_servers.<name>`. Keep all MCP
|
||||
// entries in one `mcp_servers` table in that case so Codex's unordered config
|
||||
// map cannot apply a whole-table override after a dotted one.
|
||||
const hasWholeTable = "mcp_servers" in config;
|
||||
const hasDottedGrantName = [...grantsByServer.keys()].some((server) => server.includes("."));
|
||||
if (hasWholeTable || hasDottedGrantName) {
|
||||
const next: Record<string, unknown> = {};
|
||||
const mcpServers = readRecord(config.mcp_servers);
|
||||
for (const [key, value] of Object.entries(config)) {
|
||||
if (key === "mcp_servers") continue;
|
||||
if (key.startsWith(MCP_SERVERS_PREFIX)) {
|
||||
mcpServers[key.slice(MCP_SERVERS_PREFIX.length)] = value;
|
||||
} else {
|
||||
next[key] = value;
|
||||
}
|
||||
}
|
||||
for (const [server, tools] of grantsByServer) {
|
||||
mcpServers[server] = withCodexToolApprovals(readRecord(mcpServers[server]), tools);
|
||||
}
|
||||
next.mcp_servers = mcpServers;
|
||||
return next;
|
||||
}
|
||||
return { ...config, mcp_servers: mcpServers };
|
||||
|
||||
const next: Record<string, unknown> = { ...config };
|
||||
for (const [server, tools] of grantsByServer) {
|
||||
const key = `${MCP_SERVERS_PREFIX}${server}`;
|
||||
if (key in next) {
|
||||
next[key] = withCodexToolApprovals(readRecord(next[key]), tools);
|
||||
} else {
|
||||
next[`${key}.enabled_tools`] = tools;
|
||||
next[`${key}.default_tools_approval_mode`] = "prompt";
|
||||
next[`${key}.tools`] = buildCodexToolApprovals(tools);
|
||||
}
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
function readRecord(value: unknown): Record<string, unknown> {
|
||||
|
||||
Reference in New Issue
Block a user