Report HTTP MCP startup problems clearly in codex exec (#62906)

## Why

Low-level transport errors can clutter stderr and include endpoint
URLs with credentials. An optional HTTP MCP server can also still be
starting when its tools are omitted from a turn.

## What changed

- Warn once per Streamable HTTP connection when startup fails or the
  server is omitted from the turn because startup is still pending.
  Include the server name and a short reason without the raw transport
  error.
- Filter redundant MCP worker errors from default `codex exec` stderr
  and preserve caller control through `RUST_LOG`.
- Add server context to errors when startup blocks a tool call or
  resource operation, when a tool call finds a disconnected server,
  or when HTTP session recovery fails.

## Testing

Add regression coverage for startup warnings, credential omission,
explicit `RUST_LOG` diagnostics, and an optional server that becomes
ready in time to be included in the turn.

GitOrigin-RevId: 1fd012a5e73f205c3d8735adde395c36672c205c
This commit is contained in:
Eric Traut
2026-11-21 05:57:16 +00:00
committed by copyberry
parent f63f8b9bc6
commit ddfaef91b2
13 changed files with 370 additions and 34 deletions
@@ -4,7 +4,148 @@ use core_test_support::responses;
use core_test_support::test_codex_exec::test_codex_exec;
use predicates::str::contains;
use pretty_assertions::assert_eq;
use serde_json::Value;
use serde_json::json;
use std::fs;
use std::net::TcpListener;
use wiremock::Mock;
use wiremock::Request;
use wiremock::ResponseTemplate;
use wiremock::matchers::body_partial_json;
use wiremock::matchers::method;
use wiremock::matchers::path;
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn optional_http_mcp_failure_is_reported_once_without_endpoint_credentials()
-> anyhow::Result<()> {
let test = test_codex_exec();
let listener = TcpListener::bind("127.0.0.1:0")?;
let address = listener.local_addr()?;
drop(listener);
let mcp = responses::start_mock_server().await;
Mock::given(method("POST"))
.and(path("/mcp"))
.and(body_partial_json(json!({"method": "initialize"})))
.respond_with(|request: &Request| {
let body: Value = request.body_json().unwrap();
ResponseTemplate::new(200).set_body_json(json!({
"jsonrpc": "2.0",
"id": body["id"],
"result": {
"protocolVersion": "2025-06-18",
"capabilities": {},
"serverInfo": {"name": "test", "version": "1"}
}
}))
})
.mount(&mcp)
.await;
Mock::given(method("POST"))
.and(path("/mcp"))
.and(body_partial_json(
json!({"method": "notifications/initialized"}),
))
.respond_with(ResponseTemplate::new(307).insert_header(
"location",
format!("http://{address}/mcp?apiKey=TEST_SECRET"),
))
.expect(1..)
.mount(&mcp)
.await;
Mock::given(method("POST"))
.and(path("/status"))
.respond_with(
ResponseTemplate::new(503).set_body_string("Auth required; upstream HTTP 404"),
)
.mount(&mcp)
.await;
fs::write(
test.home_path().join("config.toml"),
format!(
"mcp_optional_startup_grace_ms = 0\n\n[mcp_servers.deadhttp]\nurl = 'http://{address}/mcp?apiKey=TEST_SECRET'\n\n[mcp_servers.notification]\nurl = '{0}/mcp?apiKey=TEST_SECRET'\n\n[mcp_servers.status]\nurl = '{0}/status'\n",
mcp.uri(),
),
)?;
let server = responses::start_mock_server().await;
responses::mount_sse_once(&server, responses::sse_completed("resp_1")).await;
let result = test
.cmd_with_server(&server)
.env_remove("RUST_LOG")
.args(["--ephemeral", "--skip-git-repo-check", "hello"])
.assert()
.success();
let stderr = std::str::from_utf8(&result.get_output().stderr)?;
let mut warnings = stderr
.lines()
.filter_map(|line| {
line.split_once("codex_mcp_startup: ")
.map(|(_, warning)| warning)
})
.collect::<Vec<_>>();
warnings.sort_unstable();
insta::assert_snapshot!(warnings.join("\n"), @r#"
MCP server unavailable server_name="deadhttp" reason="HTTP request failed"
MCP server unavailable server_name="notification" reason="initialization failed"
MCP server unavailable server_name="status" reason="HTTP response error"
"#);
assert!(!stderr.contains("rmcp::transport::worker"));
assert!(!stderr.contains("TEST_SECRET"));
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn optional_http_mcp_pending_at_grace_is_reported() -> anyhow::Result<()> {
let test = test_codex_exec();
let listener = TcpListener::bind("127.0.0.1:0")?;
let address = listener.local_addr()?;
drop(listener);
fs::write(
test.home_path().join("config.toml"),
format!(
"mcp_optional_startup_grace_ms = 20\n[mcp_servers.deadhttp]\nurl = 'http://{address}/mcp'\n"
),
)?;
let server = responses::start_mock_server().await;
responses::mount_sse_once(&server, responses::sse_completed("resp_1")).await;
let result = test
.cmd_with_server(&server)
.env_remove("RUST_LOG")
.args(["--ephemeral", "--skip-git-repo-check", "hello"])
.assert()
.success();
let stderr = std::str::from_utf8(&result.get_output().stderr)?;
let warnings = stderr
.lines()
.filter_map(|line| {
line.split_once("codex_mcp_startup: ")
.map(|(_, warning)| warning)
})
.collect::<Vec<_>>();
insta::assert_snapshot!(warnings.join("\n"), @"MCP server unavailable server_name=\"deadhttp\" reason=\"startup still pending; omitted from this turn\"");
assert!(!stderr.contains("rmcp::transport::worker"));
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn explicit_rust_log_preserves_http_mcp_worker_diagnostics() -> anyhow::Result<()> {
let test = test_codex_exec();
let listener = TcpListener::bind("127.0.0.1:0")?;
let address = listener.local_addr()?;
drop(listener);
fs::write(
test.home_path().join("config.toml"),
format!("[mcp_servers.deadhttp]\nurl = 'http://{address}/mcp'\n"),
)?;
let server = responses::start_mock_server().await;
responses::mount_sse_once(&server, responses::sse_completed("resp_1")).await;
test.cmd_with_server(&server)
.env("RUST_LOG", "rmcp::transport::worker=trace")
.args(["--ephemeral", "--skip-git-repo-check", "hello"])
.assert()
.success()
.stderr(contains("worker quit with fatal:"));
Ok(())
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn exits_non_zero_when_required_mcp_server_fails_to_initialize() -> anyhow::Result<()> {