From a4ae34320c182b675bfaa44dffa51cc42eb22cfd Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 23 Nov 2026 14:23:36 +0000 Subject: [PATCH] Box MCP transport setup and initialization retry futures (#63356) ## Why MCP startup and connection recovery both create transports and run the initialization handshake. They can share the same future polling code. ## What changed Return a `BoxFuture` from transport creation and box the entire initialization retry loop once per call. Startup and recovery use the same polling implementations in `rmcp-client`. GitOrigin-RevId: 582318654ed1d519a4cf25a09126a5a5b93a4b4e --- codex-rs/rmcp-client/src/rmcp_client.rs | 9 +++++++- .../rmcp-client/src/streamable_http_retry.rs | 22 ++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/codex-rs/rmcp-client/src/rmcp_client.rs b/codex-rs/rmcp-client/src/rmcp_client.rs index f3c24bb254..9e94788fc2 100644 --- a/codex-rs/rmcp-client/src/rmcp_client.rs +++ b/codex-rs/rmcp-client/src/rmcp_client.rs @@ -1430,7 +1430,14 @@ impl RmcpClient { Ok(()) } - async fn create_pending_transport( + // Keep the transport future's polling code in this crate for both startup and recovery. + fn create_pending_transport( + transport_recipe: &TransportRecipe, + ) -> BoxFuture<'_, Result> { + Box::pin(Self::create_pending_transport_inner(transport_recipe)) + } + + async fn create_pending_transport_inner( transport_recipe: &TransportRecipe, ) -> Result { match transport_recipe { diff --git a/codex-rs/rmcp-client/src/streamable_http_retry.rs b/codex-rs/rmcp-client/src/streamable_http_retry.rs index 131c9f20be..818cd11e0c 100644 --- a/codex-rs/rmcp-client/src/streamable_http_retry.rs +++ b/codex-rs/rmcp-client/src/streamable_http_retry.rs @@ -5,6 +5,7 @@ use std::time::Instant; use anyhow::Result; use anyhow::anyhow; use codex_exec_server::ExecServerError; +use futures::future::BoxFuture; use http::StatusCode; use rmcp::service::RoleClient; use rmcp::service::RunningService; @@ -23,15 +24,26 @@ use super::RmcpClient; const JSON_RPC_INTERNAL_ERROR_CODE: i64 = -32603; pub(super) const STREAMABLE_HTTP_RETRY_DELAYS_MS: [u64; 2] = [250, 1_000]; +type InitializedConnection = ( + Arc>, + Option, +); + impl RmcpClient { - pub(super) async fn connect_pending_transport_with_initialize_retries( + // Box the entire retry loop once so startup and recovery share the handshake code. + pub(super) fn connect_pending_transport_with_initialize_retries<'a>( + &'a self, + initial_transport: PendingTransport, + initialize_context: &'a InitializeContext, + ) -> BoxFuture<'a, Result> { + Box::pin(self.connect_with_retries(initial_transport, initialize_context)) + } + + async fn connect_with_retries( &self, initial_transport: PendingTransport, initialize_context: &InitializeContext, - ) -> Result<( - Arc>, - Option, - )> { + ) -> Result { let timeout = initialize_context.timeout; let should_retry = match &initial_transport { PendingTransport::InProcess { .. } | PendingTransport::Stdio { .. } => false,