Skip to content
Open
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
9 changes: 7 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/ApprovalOptionId.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export const ApprovalOptionId = {
AllowOnce: "allow_once",
AllowAlways: "allow_always",
AllowForSession: "allow_for_session",
AllowPersist: "allow_persist",
AllowCommandPrefixRule: "allow_command_prefix_rule",
ApplyNetworkPolicyAmendment: "apply_network_policy_amendment",
RejectOnce: "reject_once",
} as const;

Expand Down
19 changes: 19 additions & 0 deletions src/CodexAcpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {logger} from "./Logger";
import type {
AccountLoginCompletedNotification,
AccountUpdatedNotification,
ConfigEdit,
GetAccountResponse,
ListMcpServerStatusParams,
ListMcpServerStatusResponse,
Expand Down Expand Up @@ -322,6 +323,24 @@ export class CodexAcpClient {
return new Set(Object.keys(mcpServers));
}

// Makes an ACP-provided MCP server durable so Codex core can append
// mcp_servers.<server>.tools.<tool>.approval_mode afterward:
// https://github.com/openai/codex/blob/main/codex-rs/core/src/mcp_tool_call.rs
async persistMcpServer(mcpServer: McpServer): Promise<void> {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also save mcp servers in global config?
How exactly this affect cli agent?

const edits: ConfigEdit[] = [{
keyPath: `mcp_servers.${mcpServer.name}`,
value: this.createMcpSeverConfig(mcpServer),
mergeStrategy: "upsert",
}];

await this.codexClient.configBatchWrite({
edits,
filePath: null,
expectedVersion: null,
reloadUserConfig: true,
});
}

getModelProvider(): string | null {
return this.gatewayConfig?.modelProvider ?? this.modelProvider;
}
Expand Down
27 changes: 10 additions & 17 deletions src/CodexAcpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface SessionState {
cwd: string;
fastModeEnabled: boolean;
currentModelSupportsFast: boolean;
sessionMcpServers?: Array<string>;
sessionMcpServers: Map<string, acp.McpServer>;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't change to map to simplify .get() in single place where performance issue is not expected.

}

interface PendingMcpStartupSession {
Expand Down Expand Up @@ -171,7 +171,6 @@ export class CodexAcpServer implements acp.Agent {

const account = await this.getActiveAccount();
const {sessionId, currentModelId, models} = sessionMetadata;
const sessionMcpServers = this.resolveSessionMcpServers(requestedMcpServers, "sessionId" in request);
const currentModel = this.findCurrentModel(models, currentModelId);
const currentModelSupportsFast = modelSupportsFast(currentModel);
const sessionState: SessionState = {
Expand All @@ -189,7 +188,7 @@ export class CodexAcpServer implements acp.Agent {
cwd: request.cwd,
fastModeEnabled: sessionMetadata.currentServiceTier === "fast",
currentModelSupportsFast: currentModelSupportsFast,
sessionMcpServers: sessionMcpServers,
sessionMcpServers: this.createSessionMcpServers(requestedMcpServers, "sessionId" in request),
}
this.sessions.set(sessionId, sessionState);

Expand Down Expand Up @@ -431,7 +430,6 @@ export class CodexAcpServer implements acp.Agent {

const account = await this.getActiveAccount();
const {sessionId, currentModelId, models, thread} = sessionMetadata;
const sessionMcpServers = this.resolveSessionMcpServers(requestedMcpServers, true);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not against, but is there a reason to mix these such refactorings with changes (inline constant)?

const currentModel = this.findCurrentModel(models, currentModelId);
const currentModelSupportsFast = modelSupportsFast(currentModel);
const sessionState: SessionState = {
Expand All @@ -449,7 +447,7 @@ export class CodexAcpServer implements acp.Agent {
cwd: request.cwd,
fastModeEnabled: sessionMetadata.currentServiceTier === "fast",
currentModelSupportsFast: currentModelSupportsFast,
sessionMcpServers: sessionMcpServers,
sessionMcpServers: this.createSessionMcpServers(requestedMcpServers, true),
};
this.sessions.set(sessionId, sessionState);

Expand Down Expand Up @@ -703,24 +701,23 @@ export class CodexAcpServer implements acp.Agent {
return sessionState;
}

private resolveSessionMcpServers(
private createSessionMcpServers(
mcpServers: Array<acp.McpServer>,
recoverFromStartup: boolean,
): Array<string> {
): Map<string, acp.McpServer> {
// Explicit MCP servers from the request are the primary source of truth for the session.
const requestedServerNames = getRequestedMcpServerNames(mcpServers);
if (requestedServerNames.length > 0) {
return requestedServerNames;
if (mcpServers.length > 0) {
return new Map(mcpServers.map(server => [server.name, server]));
}
// Fresh sessions without MCP config should not inherit any session MCP state.
if (!recoverFromStartup) {
return [];
return new Map();
}
// Without a thread-scoped startup completion event, loadSession/resumeSession can no longer
// recover omitted session MCP server names. Treat the session set as unknown unless ACP
// explicitly provided mcpServers in the request.
logger.log("Skipping MCP server recovery for load/resume without explicit mcpServers");
return [];
return new Map();
}

private publishMcpStartupStatusAsync(sessionId: string): void {
Expand Down Expand Up @@ -781,7 +778,7 @@ export class CodexAcpServer implements acp.Agent {
try {
const eventHandler = new CodexEventHandler(this.connection, sessionState);
const approvalHandler = new CodexApprovalHandler(this.connection, sessionState);
const elicitationHandler = new CodexElicitationHandler(this.connection, sessionState);
const elicitationHandler = new CodexElicitationHandler(this.connection, sessionState, this.codexAcpClient);
await this.codexAcpClient.subscribeToSessionEvents(params.sessionId,
(event) => {
elicitationHandler.handleNotification(event);
Expand Down Expand Up @@ -934,7 +931,3 @@ export class CodexAcpServer implements acp.Agent {
}
}
}

function getRequestedMcpServerNames(mcpServers: Array<acp.McpServer>): Array<string> {
return Array.from(new Set(mcpServers.map(server => server.name)));
}
6 changes: 6 additions & 0 deletions src/CodexAppServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {
AccountLoginCompletedNotification, AccountUpdatedNotification,
ConfigReadParams,
ConfigReadResponse,
ConfigBatchWriteParams,
ConfigWriteResponse,
GetAccountParams,
GetAccountResponse,
ListMcpServerStatusParams,
Expand Down Expand Up @@ -221,6 +223,10 @@ export class CodexAppServerClient {
return await this.sendRequest({ method: "config/read", params: params });
}

async configBatchWrite(params: ConfigBatchWriteParams): Promise<ConfigWriteResponse> {
return await this.sendRequest({ method: "config/batchWrite", params });
}

getMcpServerStartupVersion(): number {
return this.mcpServerStartupVersion;
}
Expand Down
Loading
Loading