|
1 | 1 | import * as acp from "@agentclientprotocol/sdk"; |
2 | 2 | import type { SessionState } from "./CodexAcpServer"; |
3 | 3 | import type { ElicitationHandler } from "./CodexAppServerClient"; |
| 4 | +import type { ServerNotification } from "./app-server"; |
4 | 5 | import type { |
| 6 | + ItemCompletedNotification, |
| 7 | + ItemStartedNotification, |
5 | 8 | McpServerElicitationRequestParams, |
6 | 9 | McpServerElicitationRequestResponse, |
7 | 10 | } from "./app-server/v2"; |
8 | 11 | import { logger } from "./Logger"; |
9 | | -import type { PendingMcpApprovals } from "./PendingMcpApprovals"; |
10 | 12 |
|
11 | 13 | // Standard elicitation options (non-tool-call approval). |
12 | 14 | const ELICITATION_OPTIONS: acp.PermissionOption[] = [ |
@@ -70,16 +72,42 @@ function buildToolApprovalOptions(persistOptions: Set<PersistValue>): acp.Permis |
70 | 72 | export class CodexElicitationHandler implements ElicitationHandler { |
71 | 73 | private readonly connection: acp.AgentSideConnection; |
72 | 74 | private readonly sessionState: SessionState; |
73 | | - private readonly pendingMcpApprovals: PendingMcpApprovals | undefined; |
| 75 | + // In Rust, the MCP elicitation handler receives ElicitationRequestEvent directly from the MCP |
| 76 | + // protocol layer, where id is set to "mcp_tool_call_approval_<call_id>" — the call ID is extracted |
| 77 | + // by stripping that prefix. |
| 78 | + // |
| 79 | + // In TypeScript, Codex speaks the app-server JSON-RPC protocol (v2), where |
| 80 | + // McpServerElicitationRequestParams omits elicitationId for form mode, so the MCP-level ID never |
| 81 | + // reaches the client. |
| 82 | + // |
| 83 | + // Workaround: before requesting approval, Codex emits an item/started notification with an |
| 84 | + // mcpToolCall item carrying the call id and server name. We store (threadId, serverName) → callId |
| 85 | + // here so the elicitation request can correlate back to the already-rendered tool call item. |
| 86 | + // |
| 87 | + // Multiple calls are safe because Codex requests approval synchronously — it blocks on one tool |
| 88 | + // call's elicitation before starting the next, so there is at most one pending approval per |
| 89 | + // (threadId, serverName). |
| 90 | + private readonly pendingMcpApprovals = new Map<string, string>(); |
74 | 91 |
|
75 | | - constructor( |
76 | | - connection: acp.AgentSideConnection, |
77 | | - sessionState: SessionState, |
78 | | - pendingMcpApprovals?: PendingMcpApprovals |
79 | | - ) { |
| 92 | + constructor(connection: acp.AgentSideConnection, sessionState: SessionState) { |
80 | 93 | this.connection = connection; |
81 | 94 | this.sessionState = sessionState; |
82 | | - this.pendingMcpApprovals = pendingMcpApprovals; |
| 95 | + } |
| 96 | + |
| 97 | + handleNotification(notification: ServerNotification): void { |
| 98 | + switch (notification.method) { |
| 99 | + case "item/started": |
| 100 | + this.handleItemStarted(notification.params); |
| 101 | + return; |
| 102 | + case "item/completed": |
| 103 | + this.handleItemCompleted(notification.params); |
| 104 | + return; |
| 105 | + case "serverRequest/resolved": |
| 106 | + this.clearThread(notification.params.threadId); |
| 107 | + return; |
| 108 | + default: |
| 109 | + return; |
| 110 | + } |
83 | 111 | } |
84 | 112 |
|
85 | 113 | async handleElicitation( |
@@ -121,7 +149,7 @@ export class CodexElicitationHandler implements ElicitationHandler { |
121 | 149 |
|
122 | 150 | if (params.mode === "form") { |
123 | 151 | const correlatedCallId = isToolApproval |
124 | | - ? this.pendingMcpApprovals?.pop(params.threadId, params.serverName) |
| 152 | + ? this.popPendingApproval(params.threadId, params.serverName) |
125 | 153 | : undefined; |
126 | 154 | if (correlatedCallId !== undefined) { |
127 | 155 | // The tool call item is already visible in the IDE conversation history because |
@@ -193,4 +221,41 @@ export class CodexElicitationHandler implements ElicitationHandler { |
193 | 221 | } |
194 | 222 | return { action: "decline", content: null, _meta: null }; |
195 | 223 | } |
| 224 | + |
| 225 | + private handleItemStarted(event: ItemStartedNotification): void { |
| 226 | + if (event.item.type !== "mcpToolCall") { |
| 227 | + return; |
| 228 | + } |
| 229 | + this.pendingMcpApprovals.set(this.key(event.threadId, event.item.server), event.item.id); |
| 230 | + } |
| 231 | + |
| 232 | + private handleItemCompleted(event: ItemCompletedNotification): void { |
| 233 | + if (event.item.type !== "mcpToolCall") { |
| 234 | + return; |
| 235 | + } |
| 236 | + this.popPendingApproval(event.threadId, event.item.server); |
| 237 | + } |
| 238 | + |
| 239 | + private popPendingApproval(threadId: string, serverName: string): string | undefined { |
| 240 | + const key = this.key(threadId, serverName); |
| 241 | + const callId = this.pendingMcpApprovals.get(key); |
| 242 | + this.pendingMcpApprovals.delete(key); |
| 243 | + return callId; |
| 244 | + } |
| 245 | + |
| 246 | + private clearThread(threadId: string): void { |
| 247 | + for (const key of this.pendingMcpApprovals.keys()) { |
| 248 | + if (this.belongsToThread(key, threadId)) { |
| 249 | + this.pendingMcpApprovals.delete(key); |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + private key(threadId: string, serverName: string): string { |
| 255 | + return `${threadId}:${serverName}`; |
| 256 | + } |
| 257 | + |
| 258 | + private belongsToThread(key: string, threadId: string): boolean { |
| 259 | + return key.startsWith(`${threadId}:`); |
| 260 | + } |
196 | 261 | } |
0 commit comments