|
| 1 | +// noinspection ES6RedundantAwait |
| 2 | + |
| 3 | +import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; |
| 4 | +import path from "node:path"; |
| 5 | +import fs from "node:fs"; |
| 6 | +import os from "node:os"; |
| 7 | +import type {McpServerStdio} from "@agentclientprotocol/sdk"; |
| 8 | +import {startCodexConnection} from "../../CodexJsonRpcConnection"; |
| 9 | +import {createBaseTestFixture, removeDirectoryWithRetry, type TestFixture} from "../acp-test-utils"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Reproduces the bug in CodexAcpClient.createSessionConfig where it does not |
| 13 | + * resolve conflicts between MCP servers defined in the global codex config.toml |
| 14 | + * and MCP servers supplied through the ACP protocol. |
| 15 | + * |
| 16 | + * Setup: |
| 17 | + * 1. Write a url-based MCP server "shared-mcp" into the codex home config.toml |
| 18 | + * (acts as the user's globally configured MCP). |
| 19 | + * 2. Open a new ACP session passing a command-type MCP with the same name |
| 20 | + * "shared-mcp" via mcpServers. |
| 21 | + * |
| 22 | + * Expectation: |
| 23 | + * /mcp must still list "shared-mcp" as a single, well-formed entry. Currently |
| 24 | + * the override built by createSessionConfig replaces mcp_servers wholesale, |
| 25 | + * so the url-based config and the command-type override clash on the codex |
| 26 | + * side and produce a broken merged entry. |
| 27 | + */ |
| 28 | +describe('MCP config merge across global config and ACP request', { timeout: 40_000 }, () => { |
| 29 | + |
| 30 | + let codexHome: string; |
| 31 | + let fixture: TestFixture; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + vi.clearAllMocks(); |
| 35 | + |
| 36 | + const configToml = ` |
| 37 | +[mcp_servers.shared-mcp] |
| 38 | +url = "https://example.com/mcp" |
| 39 | +`; |
| 40 | + codexHome = fs.mkdtempSync(path.join(os.tmpdir(), "codex-acp-mcp-merge-")); |
| 41 | + fs.writeFileSync(path.join(codexHome, "config.toml"), configToml, "utf8"); |
| 42 | + |
| 43 | + const codexConnection = startCodexConnection(undefined, { |
| 44 | + ...process.env, |
| 45 | + CODEX_HOME: codexHome, |
| 46 | + }); |
| 47 | + |
| 48 | + fixture = createBaseTestFixture({ |
| 49 | + connection: codexConnection.connection, |
| 50 | + getExitCode: () => codexConnection.process.exitCode, |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + afterEach(() => { |
| 55 | + removeDirectoryWithRetry(codexHome); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should preserve the global url-based MCP when ACP passes a command-type MCP with the same name', async () => { |
| 59 | + const codexAcpAgent = fixture.getCodexAcpAgent(); |
| 60 | + await codexAcpAgent.initialize({protocolVersion: 1}); |
| 61 | + |
| 62 | + fixture.getCodexAcpClient().authRequired = vi.fn().mockResolvedValue(false); |
| 63 | + |
| 64 | + const conflictingMcp: McpServerStdio = { |
| 65 | + name: "shared-mcp", |
| 66 | + command: "./node_modules/.bin/mcp-hello-world", |
| 67 | + args: ["example"], |
| 68 | + env: [{name: "example", value: "example"}], |
| 69 | + }; |
| 70 | + |
| 71 | + const newSessionResponse = await codexAcpAgent.newSession({ |
| 72 | + cwd: "", |
| 73 | + mcpServers: [conflictingMcp], |
| 74 | + }); |
| 75 | + fixture.clearAcpConnectionDump(); |
| 76 | + |
| 77 | + await codexAcpAgent.prompt({ |
| 78 | + sessionId: newSessionResponse.sessionId, |
| 79 | + prompt: [{type: "text", text: "/mcp"}], |
| 80 | + }); |
| 81 | + |
| 82 | + const transportDump = fixture.getAcpConnectionDump([]); |
| 83 | + expect(transportDump).contain("Configured MCP servers:"); |
| 84 | + expect(transportDump).contain("- shared-mcp"); |
| 85 | + }); |
| 86 | +}); |
0 commit comments