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
@@ -3139,6 +3139,15 @@
},
"McpServerStatusUpdatedNotification": {
"properties": {
"durationMs": {
"description": "Elapsed startup time for a measured attempt. Older servers omit this field.",
"format": "uint64",
"minimum": 0.0,
"type": [
"integer",
"null"
]
},
"environmentId": {
"description": "Registration scope, not necessarily the server's execution environment.",
"type": [
@@ -16327,6 +16327,15 @@
"McpServerStatusUpdatedNotification": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"durationMs": {
"description": "Elapsed startup time for a measured attempt. Older servers omit this field.",
"format": "uint64",
"minimum": 0.0,
"type": [
"integer",
"null"
]
},
"environmentId": {
"description": "Registration scope, not necessarily the server's execution environment.",
"type": [
@@ -12261,6 +12261,15 @@
"McpServerStatusUpdatedNotification": {
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"durationMs": {
"description": "Elapsed startup time for a measured attempt. Older servers omit this field.",
"format": "uint64",
"minimum": 0.0,
"type": [
"integer",
"null"
]
},
"environmentId": {
"description": "Registration scope, not necessarily the server's execution environment.",
"type": [
@@ -18,6 +18,15 @@
}
},
"properties": {
"durationMs": {
"description": "Elapsed startup time for a measured attempt. Older servers omit this field.",
"format": "uint64",
"minimum": 0.0,
"type": [
"integer",
"null"
]
},
"environmentId": {
"description": "Registration scope, not necessarily the server's execution environment.",
"type": [
@@ -8,4 +8,8 @@ export type McpServerStatusUpdatedNotification = { threadId: string | null, name
/**
* Registration scope, not necessarily the server's execution environment.
*/
environmentId: string | null, status: McpServerStartupState, error: string | null, failureReason: McpServerStartupFailureReason | null, };
environmentId: string | null, status: McpServerStartupState,
/**
* Elapsed startup time for a measured attempt. Older servers omit this field.
*/
durationMs?: number | null, error: string | null, failureReason: McpServerStartupFailureReason | null, };
@@ -2233,6 +2233,12 @@ mod tests {
.ok_or_else(|| anyhow::anyhow!("missing MCP login completion fixture"))?,
)?;
assert!(mcp_login_completion_ts.contains("loginId?: string | null"));
let mcp_status_update_ts = std::str::from_utf8(
fixture_tree
.get(Path::new("v2/McpServerStatusUpdatedNotification.ts"))
.ok_or_else(|| anyhow::anyhow!("missing MCP status update fixture"))?,
)?;
assert!(mcp_status_update_ts.contains("durationMs?: number | null"));
let typescript_index = std::str::from_utf8(
fixture_tree
.get(Path::new("index.ts"))
@@ -2428,6 +2434,8 @@ mod tests {
&& matches!(field_prefix.trim(), "startedAtMs?" | "completedAtMs?"))
|| (path == Path::new("v2/McpServerOauthLoginCompletedNotification.ts")
&& field_prefix.trim() == "loginId?")
|| (path == Path::new("v2/McpServerStatusUpdatedNotification.ts")
&& field_prefix.trim() == "durationMs?")
|| (path == Path::new("v2/McpServerStatus.ts")
&& field_prefix.trim() == "environmentId?")
|| (path == Path::new("v2/ProcessExitedNotification.ts")
@@ -394,6 +394,9 @@ pub struct McpServerStatusUpdatedNotification {
/// Registration scope, not necessarily the server's execution environment.
pub environment_id: Option<String>,
pub status: McpServerStartupState,
/// Elapsed startup time for a measured attempt. Older servers omit this field.
#[ts(type = "number | null", optional)]
pub duration_ms: Option<u64>,
pub error: Option<String>,
pub failure_reason: Option<McpServerStartupFailureReason>,
}
@@ -2783,7 +2783,7 @@ fn mcp_server_status_accepts_older_inventory_without_runtime_status() {
}
#[test]
fn mcp_server_status_updated_accepts_missing_thread_id() {
fn mcp_server_status_updated_accepts_missing_thread_id_and_duration() {
let notification: McpServerStatusUpdatedNotification = serde_json::from_value(json!({
"name": "optional_broken",
"status": "failed",
@@ -2796,6 +2796,7 @@ fn mcp_server_status_updated_accepts_missing_thread_id() {
name: "optional_broken".to_string(),
environment_id: None,
status: McpServerStartupState::Failed,
duration_ms: None,
error: Some("handshake failed".to_string()),
failure_reason: None,
};
@@ -2807,6 +2808,7 @@ fn mcp_server_status_updated_accepts_missing_thread_id() {
"name": "optional_broken",
"environmentId": null,
"status": "failed",
"durationMs": null,
"error": "handshake failed",
"failureReason": null,
})
@@ -2814,13 +2816,14 @@ fn mcp_server_status_updated_accepts_missing_thread_id() {
}
#[test]
fn mcp_server_status_updated_serializes_failure_reason() {
fn mcp_server_status_updated_serializes_failure_reason_and_duration() {
let notification =
ServerNotification::McpServerStatusUpdated(McpServerStatusUpdatedNotification {
thread_id: Some("thread-1".to_string()),
name: "expired-oauth".to_string(),
environment_id: Some("remote".to_string()),
status: McpServerStartupState::Failed,
duration_ms: Some(482),
error: Some("OAuth credentials expired".to_string()),
failure_reason: Some(McpServerStartupFailureReason::ReauthenticationRequired),
});
@@ -2834,6 +2837,7 @@ fn mcp_server_status_updated_serializes_failure_reason() {
"name": "expired-oauth",
"environmentId": "remote",
"status": "failed",
"durationMs": 482,
"error": "OAuth credentials expired",
"failureReason": "reauthenticationRequired",
},