mirror of
https://github.com/openai/codex.git
synced 2026-11-30 00:10:49 +00:00
## What changed - With `tui_extensions_statusline` enabled, resolve the saved MCP provider and read its resource with a snapshot of the displayed thread's statusline state. - Show initialization and error messages, validate the JSON UI tree response, and preserve provider content through native statusline refreshes. Ignore stale results when the thread, provider, or working directory changes. - Request workspace headlines and thread usage for custom providers even when no built-in statusline items are selected. ## Testing Add unit coverage for response validation and footer lifecycle, plus end-to-end tests for embedded and remote app servers, startup delays, current thread metadata, and rejection of project-level provider overrides. GitOrigin-RevId: 80a46ec780c1f1536f1fd4749f468629627dedf6
258 lines
9.2 KiB
Rust
258 lines
9.2 KiB
Rust
//! MCP startup state and status handling for the chat widget.
|
|
//!
|
|
//! The app server reports MCP server startup as per-server status updates. This
|
|
//! module tracks only the starts and outcomes the TUI receives, including
|
|
//! partial startup rounds after attaching to a running app server.
|
|
//! Each active round includes every outcome it receives, even if the client has
|
|
//! already shown the same diagnostic in an earlier round.
|
|
//! Initial and runtime diagnostics remain in retained history and the warnings viewer;
|
|
//! compact history hides their full text behind the persistent warning notice.
|
|
//! Server-name/environment pairs keep same-named registrations independent.
|
|
|
|
use crate::app_server_approval_conversions::mcp_server_label;
|
|
use codex_app_server_protocol::McpServerStartupFailureReason;
|
|
use codex_app_server_protocol::McpServerStartupState;
|
|
use codex_app_server_protocol::McpServerStatusUpdatedNotification;
|
|
|
|
use super::ChatWidget;
|
|
|
|
const MCP_STARTUP_SINGLE_HEADER_PREFIX: &str = "Booting MCP server:";
|
|
const MCP_STARTUP_MULTI_HEADER_PREFIX: &str = "Starting MCP servers";
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub(crate) enum McpStartupStatus {
|
|
Starting,
|
|
Ready,
|
|
Failed {
|
|
error: String,
|
|
failure_reason: Option<McpServerStartupFailureReason>,
|
|
},
|
|
Cancelled,
|
|
}
|
|
|
|
impl McpStartupStatus {
|
|
fn is_diagnostic(&self) -> bool {
|
|
matches!(self, Self::Failed { .. } | Self::Cancelled)
|
|
}
|
|
}
|
|
|
|
impl ChatWidget {
|
|
/// The server announces eager starts before outcomes. On attach or after lag,
|
|
/// an outcome can arrive without its corresponding start.
|
|
fn update_mcp_startup_status(
|
|
&mut self,
|
|
server: (String, Option<String>),
|
|
status: McpStartupStatus,
|
|
) {
|
|
let report_diagnostic = if status.is_diagnostic() {
|
|
self.mcp_startup_last_diagnostics
|
|
.insert(server.clone(), status.clone())
|
|
.as_ref()
|
|
!= Some(&status)
|
|
} else {
|
|
self.mcp_startup_last_diagnostics.remove(&server);
|
|
false
|
|
};
|
|
if report_diagnostic {
|
|
self.report_mcp_startup_failure(&server, &status);
|
|
}
|
|
if self.mcp_startup_status.is_none() && !matches!(status, McpStartupStatus::Starting) {
|
|
if report_diagnostic {
|
|
self.report_mcp_startup_summary([(&server, &status)]);
|
|
}
|
|
return;
|
|
}
|
|
|
|
self.mcp_startup_status
|
|
.get_or_insert_default()
|
|
.insert(server, status);
|
|
if let Some(header) = self.mcp_startup_status_header() {
|
|
self.update_task_running_state();
|
|
self.set_status_header(header);
|
|
self.request_redraw();
|
|
} else {
|
|
self.finish_mcp_startup();
|
|
}
|
|
}
|
|
|
|
pub(super) fn mcp_startup_status_header(&self) -> Option<String> {
|
|
let current = self.mcp_startup_status.as_ref()?;
|
|
let total = current.len();
|
|
let mut starting: Vec<_> = current
|
|
.iter()
|
|
.filter_map(|(server, state)| {
|
|
if matches!(state, McpStartupStatus::Starting) {
|
|
Some(server)
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
.collect();
|
|
starting.sort();
|
|
let completed = total.saturating_sub(starting.len());
|
|
let max_to_show = 3;
|
|
let mut to_show: Vec<String> = starting
|
|
.iter()
|
|
.take(max_to_show)
|
|
.map(|(server_name, environment_id)| {
|
|
mcp_server_label(server_name, environment_id.as_deref())
|
|
})
|
|
.collect();
|
|
if starting.len() > max_to_show {
|
|
to_show.push("…".to_string());
|
|
}
|
|
let first = to_show.first()?;
|
|
Some(if total > 1 {
|
|
format!(
|
|
"{MCP_STARTUP_MULTI_HEADER_PREFIX} ({completed}/{total}): {}",
|
|
to_show.join(", ")
|
|
)
|
|
} else {
|
|
format!("{MCP_STARTUP_SINGLE_HEADER_PREFIX} {first}")
|
|
})
|
|
}
|
|
|
|
fn report_mcp_startup_failure(
|
|
&mut self,
|
|
(server_name, environment_id): &(String, Option<String>),
|
|
status: &McpStartupStatus,
|
|
) {
|
|
if let McpStartupStatus::Failed {
|
|
error,
|
|
failure_reason,
|
|
} = status
|
|
{
|
|
self.add_mcp_startup_warning(
|
|
vec![error.clone()],
|
|
[mcp_server_label(server_name, environment_id.as_deref())],
|
|
*failure_reason,
|
|
);
|
|
}
|
|
}
|
|
|
|
fn report_mcp_startup_summary<'a>(
|
|
&mut self,
|
|
outcomes: impl IntoIterator<Item = (&'a (String, Option<String>), &'a McpStartupStatus)>,
|
|
) {
|
|
let mut failed = Vec::new();
|
|
let mut cancelled = Vec::new();
|
|
for ((server_name, environment_id), status) in outcomes {
|
|
let server = mcp_server_label(server_name, environment_id.as_deref());
|
|
match status {
|
|
McpStartupStatus::Failed { .. } => failed.push(server),
|
|
McpStartupStatus::Cancelled => cancelled.push(server),
|
|
McpStartupStatus::Starting | McpStartupStatus::Ready => {}
|
|
}
|
|
}
|
|
if !cancelled.is_empty() {
|
|
cancelled.sort();
|
|
self.add_mcp_startup_warning(
|
|
vec![format!(
|
|
"MCP startup interrupted. The following servers were not initialized: {}",
|
|
cancelled.join(", ")
|
|
)],
|
|
cancelled,
|
|
/*failure_reason*/ None,
|
|
);
|
|
}
|
|
if !failed.is_empty() {
|
|
failed.sort();
|
|
self.add_mcp_startup_warning(
|
|
vec![format!(
|
|
"MCP startup incomplete (failed: {})",
|
|
failed.join(", ")
|
|
)],
|
|
failed,
|
|
/*failure_reason*/ None,
|
|
);
|
|
}
|
|
}
|
|
|
|
pub(super) fn finish_mcp_startup(&mut self) {
|
|
let mcp_startup_owned_status = self.status_header_is_mcp_startup_owned();
|
|
if let Some(settled) = self.mcp_startup_status.take() {
|
|
self.report_mcp_startup_summary(&settled);
|
|
}
|
|
self.update_task_running_state();
|
|
if self.input_queue.user_turn_pending_start {
|
|
self.bottom_pane.set_task_running(/*running*/ true);
|
|
}
|
|
if self.bottom_pane.is_task_running() && mcp_startup_owned_status {
|
|
self.restore_reasoning_status_header();
|
|
}
|
|
self.maybe_send_next_queued_input();
|
|
self.request_redraw();
|
|
}
|
|
|
|
pub(crate) fn finish_mcp_startup_after_lag(&mut self) {
|
|
if self.mcp_startup_status.is_some() {
|
|
self.finish_mcp_startup();
|
|
}
|
|
// A missed Starting may have begun a new attempt with the same error.
|
|
self.mcp_startup_last_diagnostics.clear();
|
|
}
|
|
|
|
pub(super) fn status_header_is_mcp_startup_owned(&self) -> bool {
|
|
self.status_state
|
|
.current_status
|
|
.header
|
|
.starts_with(MCP_STARTUP_SINGLE_HEADER_PREFIX)
|
|
|| self
|
|
.status_state
|
|
.current_status
|
|
.header
|
|
.starts_with(MCP_STARTUP_MULTI_HEADER_PREFIX)
|
|
}
|
|
|
|
fn add_mcp_startup_warning(
|
|
&mut self,
|
|
messages: Vec<String>,
|
|
servers: impl IntoIterator<Item = String>,
|
|
failure_reason: Option<McpServerStartupFailureReason>,
|
|
) {
|
|
self.add_to_history(crate::history_cell::StartupWarningsCell::mcp(
|
|
messages,
|
|
servers,
|
|
failure_reason,
|
|
));
|
|
}
|
|
|
|
/// Update startup state and retry installed-app discovery when its MCP server becomes ready.
|
|
pub(super) fn on_mcp_server_status_updated(
|
|
&mut self,
|
|
notification: McpServerStatusUpdatedNotification,
|
|
) {
|
|
if notification.status == McpServerStartupState::Ready
|
|
&& let Some(footer) = &self.statusline_read
|
|
{
|
|
footer.server_ready(¬ification.name);
|
|
}
|
|
let refresh_connector_mentions = notification.name == "codex_apps"
|
|
&& notification.environment_id.is_none()
|
|
&& notification.status == McpServerStartupState::Ready;
|
|
let status = match notification.status {
|
|
McpServerStartupState::Starting => McpStartupStatus::Starting,
|
|
McpServerStartupState::Ready => McpStartupStatus::Ready,
|
|
McpServerStartupState::Failed => {
|
|
let server =
|
|
mcp_server_label(¬ification.name, notification.environment_id.as_deref());
|
|
McpStartupStatus::Failed {
|
|
failure_reason: notification.failure_reason,
|
|
error: match notification.error {
|
|
Some(error) if notification.environment_id.is_some() => {
|
|
format!("{server}: {error}")
|
|
}
|
|
Some(error) => error,
|
|
None => format!("MCP client for `{server}` failed to start"),
|
|
},
|
|
}
|
|
}
|
|
McpServerStartupState::Cancelled => McpStartupStatus::Cancelled,
|
|
};
|
|
self.update_mcp_startup_status((notification.name, notification.environment_id), status);
|
|
if refresh_connector_mentions {
|
|
self.refresh_connector_mentions(/*force_refresh*/ false);
|
|
}
|
|
}
|
|
}
|