@@ -10,7 +10,7 @@ import {CodexApprovalHandler} from "./CodexApprovalHandler";
1010import { CodexAuthMethods , type CodexAuthRequest } from "./CodexAuthMethod" ;
1111import { CodexAcpClient , type SessionMetadata , type SessionMetadataWithThread } from "./CodexAcpClient" ;
1212import { ACPSessionConnection , type UpdateSessionEvent } from "./ACPSessionConnection" ;
13- import type { Account , CollabAgentToolCallStatus , Model , RateLimitSnapshot , Thread , ThreadItem , UserInput , ReasoningEffortOption } from "./app-server/v2" ;
13+ import type { Account , CollabAgentToolCallStatus , Model , Thread , ThreadItem , UserInput , ReasoningEffortOption } from "./app-server/v2" ;
1414import type { RateLimitsMap } from "./RateLimitsMap" ;
1515import type { InputModality , ReasoningEffort } from "./app-server" ;
1616import { ModelId } from "./ModelId" ;
@@ -127,7 +127,6 @@ export class CodexAcpServer implements acp.Agent {
127127
128128 async getOrCreateSession ( request : acp . NewSessionRequest | acp . ResumeSessionRequest ) : Promise < [ SessionId , SessionModelState , SessionModeState ] > {
129129 await this . checkAuthorization ( ) ;
130- const pendingMcpServers : Promise < Array < string > > = this . codexAcpClient . awaitMcpServers ( ) ;
131130
132131 let sessionMetadata : SessionMetadata ;
133132 if ( "sessionId" in request ) {
@@ -139,9 +138,8 @@ export class CodexAcpServer implements acp.Agent {
139138 }
140139
141140 const accountResponse = await this . runWithProcessCheck ( ( ) => this . codexAcpClient . getAccount ( ) ) ;
142- const { sessionId, currentModelId, models} = sessionMetadata ;
143- logger . log ( `Waiting MCP servers to start...` )
144- const sessionMcpServers = await pendingMcpServers ;
141+ const { sessionId, currentModelId, models, mcpStartupVersion} = sessionMetadata ;
142+ const sessionMcpServers = await this . resolveSessionMcpServers ( request . mcpServers ?? [ ] , mcpStartupVersion , "sessionId" in request ) ;
145143 const currentModel = this . findCurrentModel ( models , currentModelId ) ;
146144 const sessionState : SessionState = {
147145 sessionId : sessionId ,
@@ -332,17 +330,15 @@ export class CodexAcpServer implements acp.Agent {
332330 thread : Thread ;
333331 } > {
334332 await this . checkAuthorization ( ) ;
335- const pendingMcpServers : Promise < Array < string > > = this . codexAcpClient . awaitMcpServers ( ) ;
336333
337334 logger . log ( `Load existing session: ${ request . sessionId } ...` ) ;
338335 const sessionMetadata : SessionMetadataWithThread = await this . runWithProcessCheck ( ( ) =>
339336 this . codexAcpClient . loadSession ( request )
340337 ) ;
341338
342339 const accountResponse = await this . runWithProcessCheck ( ( ) => this . codexAcpClient . getAccount ( ) ) ;
343- const { sessionId, currentModelId, models, thread} = sessionMetadata ;
344- logger . log ( "Waiting MCP servers to start..." ) ;
345- const sessionMcpServers = await pendingMcpServers ;
340+ const { sessionId, currentModelId, models, thread, mcpStartupVersion} = sessionMetadata ;
341+ const sessionMcpServers = await this . resolveSessionMcpServers ( request . mcpServers ?? [ ] , mcpStartupVersion , true ) ;
346342 const currentModel = this . findCurrentModel ( models , currentModelId ) ;
347343 const sessionState : SessionState = {
348344 sessionId : sessionId ,
@@ -601,6 +597,22 @@ export class CodexAcpServer implements acp.Agent {
601597 return sessionState ;
602598 }
603599
600+ private async resolveSessionMcpServers (
601+ mcpServers : Array < acp . McpServer > ,
602+ mcpStartupVersion : number ,
603+ recoverFromStartup : boolean ,
604+ ) : Promise < Array < string > > {
605+ const requestedServerNames = getRequestedMcpServerNames ( mcpServers ) ;
606+ if ( requestedServerNames . length > 0 ) {
607+ return requestedServerNames ;
608+ }
609+ if ( ! recoverFromStartup ) {
610+ return [ ] ;
611+ }
612+ logger . log ( "Recovering MCP servers from startup state..." ) ;
613+ return await this . runWithProcessCheck ( ( ) => this . codexAcpClient . awaitMcpStartup ( mcpStartupVersion ) ) ;
614+ }
615+
604616 async prompt ( params : acp . PromptRequest ) : Promise < acp . PromptResponse > {
605617 logger . log ( "Prompt received" , {
606618 sessionId : params . sessionId ,
@@ -747,3 +759,7 @@ export class CodexAcpServer implements acp.Agent {
747759 }
748760 }
749761}
762+
763+ function getRequestedMcpServerNames ( mcpServers : Array < acp . McpServer > ) : Array < string > {
764+ return Array . from ( new Set ( mcpServers . map ( server => server . name ) ) ) ;
765+ }
0 commit comments