Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/CodexAcpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,16 @@ export class CodexAcpClient {

private async getConfigMcpServerNames(projectPath: string): Promise<Set<string>> {
const response = await this.codexClient.configRead({ includeLayers: true, cwd: projectPath });
const mcpServers = response?.config?.["mcp_servers"];
if (!mcpServers || typeof mcpServers !== "object" || Array.isArray(mcpServers)) {
const effectiveMcpServers = response?.config?.["mcp_servers"];
const configLayers = response?.layers ?? [];
const layerMcpServers = configLayers.map(layer => {
return isJsonObject(layer.config) ? layer.config["mcp_servers"] : undefined;
});
const configuredMcpServers = [effectiveMcpServers, ...layerMcpServers].filter(isJsonObject);
if (configuredMcpServers.length === 0) {
return new Set();
}
return new Set(Object.keys(mcpServers));
return new Set(configuredMcpServers.flatMap(server => Object.keys(server)));
}

getModelProvider(): string | null {
Expand Down
35 changes: 32 additions & 3 deletions src/__tests__/CodexACPAgent/mcp-config-merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,30 @@ import type {McpServerStdio} from "@agentclientprotocol/sdk";
import {startCodexConnection} from "../../CodexJsonRpcConnection";
import {createBaseTestFixture, removeDirectoryWithRetry, type TestFixture} from "../acp-test-utils";

describe('MCP config merge across global config and ACP request', { timeout: 40_000 }, () => {
describe('MCP config merge across configured MCP servers and ACP request', { timeout: 40_000 }, () => {

let codexHome: string;
let projectPath: string;
let fixture: TestFixture;

beforeEach(() => {
vi.clearAllMocks();

const configToml = `
const globalConfig = `
[mcp_servers.shared-mcp]
url = "https://example.com/mcp"
`;

const projectConfig = `
[mcp_servers.project-mcp]
url = "https://example.com/mcp"
`;

codexHome = fs.mkdtempSync(path.join(os.tmpdir(), "codex-acp-mcp-merge-"));
fs.writeFileSync(path.join(codexHome, "config.toml"), configToml, "utf8");
fs.writeFileSync(path.join(codexHome, "config.toml"), globalConfig, "utf8");
projectPath = fs.mkdtempSync(path.join(os.tmpdir(), "codex-acp-mcp-project-"));
fs.mkdirSync(path.join(projectPath, ".codex"));
fs.writeFileSync(path.join(projectPath, ".codex", "config.toml"), projectConfig, "utf8");

const codexConnection = startCodexConnection(undefined, {
...process.env,
Expand All @@ -37,6 +47,7 @@ url = "https://example.com/mcp"
afterEach(() => {
vi.unstubAllEnvs();
removeDirectoryWithRetry(codexHome);
removeDirectoryWithRetry(projectPath);
});

it('should preserve the global url-based MCP when ACP passes a command-type MCP with the same name', async () => {
Expand Down Expand Up @@ -68,6 +79,24 @@ url = "https://example.com/mcp"
expect(transportDump).contain("- shared-mcp");
});

it('should preserve a project url-based MCP when ACP passes a command-type MCP with the same name', async () => {
const codexAcpAgent = fixture.getCodexAcpAgent();
await codexAcpAgent.initialize({protocolVersion: 1});
fixture.getCodexAcpClient().authRequired = vi.fn().mockResolvedValue(false);

const conflictingMcp = {
name: "project-mcp",
command: "./node_modules/.bin/mcp-hello-world",
args: ["example"],
env: [{name: "example", value: "example"}],
};

await expect(codexAcpAgent.newSession({
cwd: projectPath,
mcpServers: [conflictingMcp],
})).resolves.toBeDefined();
});

it('should not filter the conflicting ACP MCP when config filtering is disabled', async () => {
vi.stubEnv("DISABLE_MCP_CONFIG_FILTERING", "true");
const codexAcpAgent = fixture.getCodexAcpAgent();
Expand Down