openai/codex · error · anyhow::Error

MCP client not initialized

Error message

MCP client not initialized

What it means

State-machine guard in the internal service() accessor: the client is not in the Ready state (still connecting or already closed), so the request cannot be sent. The input at fault is a request issued before initialize completed or after shutdown.

Solutions

  1. Await initialize before issuing requests such as custom/event-stream calls
  2. Recreate the client if it was shut down
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at codex-rs/rmcp-client/src/rmcp_client.rs:928 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of openai/codex@339751715c (2026-08-25). Data as JSON: /api/errors/b6609234417f72a8. Report an issue: GitHub.

Appendix: source

Thrown at codex-rs/rmcp-client/src/rmcp_client.rs:928

        let handle = service
            .peer()
            .send_cancellable_request(
                ClientRequest::CustomRequest(request),
                rmcp::service::PeerRequestOptions::no_options(),
            )
            .await?;

        Ok(CancellableEventStreamRequest {
            handle,
            notifications,
        })
    }

    async fn service(&self) -> Result<Arc<RunningService<RoleClient, ElicitationClientService>>> {
        let guard = self.state.lock().await;
        match &*guard {
            ClientState::Ready { service, .. } => Ok(Arc::clone(service)),
            ClientState::Connecting { .. } => Err(anyhow!("MCP client not initialized")),
            ClientState::Closed => Err(anyhow!("MCP client is shut down")),
        }
    }

    async fn oauth_persistor(&self) -> Option<OAuthPersistor> {
        let guard = self.state.lock().await;
        match &*guard {
            ClientState::Ready {
                oauth: Some(runtime),
                ..
            } => Some(runtime.clone()),
            _ => None,
        }
    }

    /// Returns `None` when this client does not manage stored OAuth credentials.
    pub async fn managed_oauth_credentials(&self) -> Option<Option<StoredOAuthTokens>> {
        let persistor = self.oauth_persistor().await?;

View on GitHub (pinned to 339751715c)