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
49 changes: 25 additions & 24 deletions src/steps/add-mcp-server-to-clients/clients/__tests__/codex.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { CodexMCPClient } from '../codex';
import { getDefaultServerConfig } from '../../defaults';

jest.mock('node:child_process', () => ({
execSync: jest.fn(),
spawnSync: jest.fn(),
}));

jest.mock('../../defaults', () => ({
getDefaultServerConfig: jest.fn(),
}));

jest.mock('../../../../utils/analytics', () => ({
analytics: {
captureException: jest.fn(),
Expand All @@ -19,22 +14,12 @@ jest.mock('../../../../utils/analytics', () => ({
describe('CodexMCPClient', () => {
const { execSync, spawnSync } = require('node:child_process');
const analytics = require('../../../../utils/analytics').analytics;
const getDefaultServerConfigMock = getDefaultServerConfig as jest.Mock;

const spawnSyncMock = spawnSync as jest.Mock;
const execSyncMock = execSync as jest.Mock;

const mockConfig = {
command: 'npx',
args: ['-y', 'mcp-remote@latest', 'https://example.com'],
env: {
POSTHOG_AUTH_HEADER: 'Bearer phx_example',
},
};

beforeEach(() => {
jest.clearAllMocks();
getDefaultServerConfigMock.mockReturnValue(mockConfig);
});

describe('isClientSupported', () => {
Expand Down Expand Up @@ -78,7 +63,7 @@ describe('CodexMCPClient', () => {
});

describe('addServer', () => {
it('invokes codex mcp add with expected arguments', async () => {
it('invokes codex mcp add with --url and --bearer-token-env-var', async () => {
spawnSyncMock.mockReturnValue({ status: 0 });

const client = new CodexMCPClient();
Expand All @@ -91,15 +76,31 @@ describe('CodexMCPClient', () => {
'mcp',
'add',
'posthog',
'--env',
'POSTHOG_AUTH_HEADER=Bearer phx_example',
'--',
'npx',
'-y',
'mcp-remote@latest',
'https://example.com',
'--url',
'https://mcp.posthog.com/mcp',
'--bearer-token-env-var',
'POSTHOG_API_KEY',
],
{ stdio: 'ignore' },
expect.objectContaining({
stdio: 'ignore',
env: expect.objectContaining({
POSTHOG_API_KEY: 'phx_example',
}),
}),
);
});

it('omits auth in OAuth mode', async () => {
spawnSyncMock.mockReturnValue({ status: 0 });

const client = new CodexMCPClient();
const result = await client.addServer(undefined);

expect(result).toEqual({ success: true });
expect(spawnSyncMock).toHaveBeenCalledWith(
'codex',
['mcp', 'add', 'posthog', '--url', 'https://mcp.posthog.com/mcp'],
expect.objectContaining({ stdio: 'ignore' }),
);
});

Expand Down
27 changes: 10 additions & 17 deletions src/steps/add-mcp-server-to-clients/clients/codex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { z } from 'zod';
import { execSync, spawnSync } from 'node:child_process';

import { DefaultMCPClient } from '../MCPClient';
import { DefaultMCPClientConfig, getDefaultServerConfig } from '../defaults';
import { buildMCPUrl, DefaultMCPClientConfig } from '../defaults';

import { analytics } from '../../../utils/analytics';

Expand All @@ -11,7 +11,7 @@ export const CodexMCPConfig = DefaultMCPClientConfig;
export type CodexMCPConfig = z.infer<typeof DefaultMCPClientConfig>;

export class CodexMCPClient extends DefaultMCPClient {
name = 'Codex CLI';
name = 'Codex';

constructor() {
super();
Expand Down Expand Up @@ -57,29 +57,22 @@ export class CodexMCPClient extends DefaultMCPClient {
}

addServer(
apiKey: string,
apiKey?: string,
selectedFeatures?: string[],
local?: boolean,
): Promise<{ success: boolean }> {
const config = getDefaultServerConfig(
apiKey,
'sse',
selectedFeatures,
local,
);
const serverName = local ? 'posthog-local' : 'posthog';
const url = buildMCPUrl('streamable-http', selectedFeatures, local);

const args = ['mcp', 'add', serverName];
const args = ['mcp', 'add', serverName, '--url', url];

if (config.env) {
for (const [key, value] of Object.entries(config.env)) {
args.push('--env', `${key}=${value}`);
}
const env = { ...process.env };
if (apiKey) {
env.POSTHOG_API_KEY = apiKey;
args.push('--bearer-token-env-var', 'POSTHOG_API_KEY');
}

args.push('--', config.command, ...(config.args ?? []));

const result = spawnSync('codex', args, { stdio: 'ignore' });
const result = spawnSync('codex', args, { stdio: 'ignore', env });

if (result.error || result.status !== 0) {
analytics.captureException(
Expand Down
Loading