Report per-server MCP startup durations (#63613)

## Why

Required MCP servers can finish at different times even when their startup
notifications arrive together. Notification arrival times cannot show how long
each server took to start.

## What changed

Measure each startup attempt and expose its elapsed milliseconds as
`durationMs` on `ready` and `failed` updates from
`mcpServer/startupStatus/updated`. Successful Codex Apps reconnects report
their own duration. Reused connections, `starting`, and `cancelled` updates
carry `null`. The protocol accepts notifications from older servers that omit
the field.

## Testing

Cover distinct durations for fast and slow required servers, timing through
lease registration for newly opened shared connections, and a fresh duration
after Codex Apps recovery.

GitOrigin-RevId: 39c0c418149dd2b220828be04d998085f57acf2d
This commit is contained in:
JonLuca De Caro
2026-11-24 11:05:19 +00:00
committed by copyberry
parent 32119bac26
commit abc92e6bd4
34 changed files with 344 additions and 61 deletions
+8
View File
@@ -588,6 +588,14 @@ switching models. Setting `friendly` or `pragmatic` can replace a previous
thread's existing instructions or change explicitly supplied base instructions.
The old `features.personality` flag is ignored.
# MCP server startup
`mcpServer/startupStatus/updated` reports each server's startup state. On
`ready` or `failed`, `durationMs` gives the elapsed milliseconds for that
server's startup attempt, even when notifications arrive together. It is null
when there is no new measurement, including for reused connections, `starting`
and `cancelled`. Older servers omit the field.
# MCP server capabilities
`mcpServerStatus/list` returns `serverCapabilities` for each initialized MCP server
@@ -243,6 +243,7 @@ pub(crate) async fn apply_bespoke_event_handling(
name: update.server,
environment_id: update.environment_id,
status,
duration_ms: update.duration_ms,
error,
failure_reason,
};
@@ -150,7 +150,7 @@ async fn desktop_socket_mcp_startup(
Ok(())
}
async fn recording_widget_server() -> (MockServer, serde_json::Value) {
async fn recording_widget_server(initialize_delay: Duration) -> (MockServer, serde_json::Value) {
let server = MockServer::start().await;
let tool = json!({
"name": "open_widget",
@@ -179,15 +179,71 @@ async fn recording_widget_server() -> (MockServer, serde_json::Value) {
}),
_ => return ResponseTemplate::new(202),
};
ResponseTemplate::new(200).set_body_json(json!({
let response = ResponseTemplate::new(200).set_body_json(json!({
"jsonrpc": "2.0", "id": request["id"], "result": result
}))
}));
if request["method"] == "initialize" {
response.set_delay(initialize_delay)
} else {
response
}
})
.mount(&server)
.await;
(server, tool)
}
#[tokio::test]
async fn required_mcp_servers_report_independent_startup_durations() -> Result<()> {
const DELAY_MS: u64 = 500;
let (fast, _) = recording_widget_server(Duration::ZERO).await;
let (slow, _) = recording_widget_server(Duration::from_millis(DELAY_MS)).await;
let codex_home = TempDir::new()?;
mock_responses_config(&fast.uri())
.with_root_config("mcp_oauth_credentials_store = \"file\"")
.with_extra_config(&format!(
r#"[mcp_servers.fast]
url = "{}/mcp"
required = true
[mcp_servers.slow]
url = "{}/mcp"
required = true"#,
fast.uri(),
slow.uri()
))
.write(codex_home.path())?;
let mut app = TestAppServer::builder()
.with_codex_home(codex_home.path())
.build_initialized()
.await?;
// Required servers finish before thread/start returns and the client sees their updates.
let thread = app.start_thread(ThreadStartParams::default()).await?.thread;
let mut durations = BTreeMap::new();
for name in ["fast", "slow"] {
let ready = timeout(
DEFAULT_READ_TIMEOUT,
app.read_stream_until_matching_notification("required MCP ready", |notification| {
notification.method == "mcpServer/startupStatus/updated"
&& notification.params.as_ref().is_some_and(|params| {
params["threadId"] == thread.id
&& params["name"] == name
&& params["status"] == "ready"
})
}),
)
.await??;
let duration_ms = ready.params.expect("ready params")["durationMs"]
.as_u64()
.expect("measured duration");
durations.insert(name, duration_ms);
}
assert!(durations["slow"] >= DELAY_MS, "{durations:?}");
assert!(durations["fast"] < durations["slow"], "{durations:?}");
Ok(())
}
#[test_case("http_headers = { Authorization = \"Bearer fixed-test-token\" }", 1; "fixed authorization")]
#[test_case("", 2; "stored OAuth authority")]
#[tokio::test]
@@ -195,7 +251,7 @@ async fn mcp_server_status_discovers_thread_inventory(
headers: &str,
expected_lightweight_initializations: usize,
) -> Result<()> {
let (server, tool) = recording_widget_server().await;
let (server, tool) = recording_widget_server(Duration::ZERO).await;
let codex_home = TempDir::new()?;
mock_responses_config(&server.uri())
.with_root_config("mcp_oauth_credentials_store = \"file\"")
@@ -298,7 +354,7 @@ async fn mcp_server_status_discovers_thread_inventory(
#[tokio::test]
async fn mcp_server_status_list_discovers_only_the_requested_page() -> Result<()> {
let (server, _) = recording_widget_server().await;
let (server, _) = recording_widget_server(Duration::ZERO).await;
let codex_home = TempDir::new()?;
mock_responses_config(&server.uri())
.with_extra_config(&format!(
@@ -1866,16 +1922,19 @@ async fn mcp_server_status_keeps_same_named_environment_inventory_separate() ->
})
})
.await?;
let params = notification.params.expect("notes ready params");
let duration_ms = params["durationMs"].as_u64().expect("measured duration");
assert_eq!(
notification.params,
Some(serde_json::json!({
params,
json!({
"threadId": thread.id,
"name": "notes",
"environmentId": "env-a",
"status": "ready",
"durationMs": duration_ms,
"error": null,
"failureReason": null,
}))
})
);
let response: ListMcpServerStatusResponse = app
.request(|request_id| ClientRequest::McpServerStatusList {
@@ -1522,6 +1522,7 @@ async fn thread_start_emits_mcp_server_status_updated_notifications() -> Result<
name: "optional_broken".to_string(),
environment_id: None,
status: McpServerStartupState::Starting,
duration_ms: None,
error: None,
failure_reason: None,
}
@@ -1556,6 +1557,7 @@ async fn thread_start_emits_mcp_server_status_updated_notifications() -> Result<
assert_eq!(failed.thread_id, Some(start_response.thread.id));
assert_eq!(failed.name, "optional_broken");
assert_eq!(failed.status, McpServerStartupState::Failed);
assert!(failed.duration_ms.is_some());
assert_eq!(failed.failure_reason, None);
assert!(
failed