mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Enable MCP connection sharing by default (#62552)
## Why Local stdio discovery needs to accommodate legacy servers that return an empty response to `server/discover`, take longer to initialize than the discovery watchdog allows, or require `initialize` as their first request. ## What changed Mark `mcp_connection_sharing` stable and enable it by default. Treat an empty discovery response as an unsupported method and let legacy initialization use the configured startup timeout. Give legacy restarts a fresh timeout and keep modern version retries within the original deadline. ## Testing Cover empty discovery responses, slow initialization on the original connection, and restarts after ignored or rejected initialization. Explicitly select legacy mode in tests that depend on legacy behavior. GitOrigin-RevId: 04d87ddfe02fac8491f879ce420bf294dc725128
This commit is contained in:
@@ -1019,6 +1019,7 @@ async fn mcp_server_status_list_waits_for_live_stdio_metadata_before_using_cache
|
||||
let pid_file = codex_home.path().join("mcp.pid");
|
||||
std::fs::write(&barrier_file, "ready")?;
|
||||
mock_responses_config(&server.uri())
|
||||
.disable_feature(Feature::McpConnectionSharing)
|
||||
.with_extra_config(&format!(
|
||||
r#"[mcp_servers.cached-stdio]
|
||||
command = {}
|
||||
@@ -1092,6 +1093,7 @@ async fn dormant_thread_status_returns_live_inventory_without_waking_the_thread(
|
||||
let server = create_mock_responses_server_sequence_unchecked(Vec::new()).await;
|
||||
let codex_home = TempDir::new()?;
|
||||
mock_responses_config(&server.uri())
|
||||
.disable_feature(Feature::McpConnectionSharing)
|
||||
.enable_feature(Feature::McpLazyStdioStartup)
|
||||
.with_extra_config(&format!(
|
||||
"[mcp_servers.memo]\ncommand = {}\nenabled_tools = [\"echo\"]",
|
||||
|
||||
@@ -7739,6 +7739,13 @@ async fn code_mode_node_repl_text_evidence_is_visible_only_to_guardian(
|
||||
.features
|
||||
.set_enabled(Feature::GuardianNodeReplTranscriptImages, transcript_images)
|
||||
.expect("configure Guardian transcript images");
|
||||
if check_detail {
|
||||
// The invalid image and its valid successor exceed the modern stdio frame limit.
|
||||
config
|
||||
.features
|
||||
.disable(Feature::McpConnectionSharing)
|
||||
.expect("use legacy stdio for the oversized image response");
|
||||
}
|
||||
let mcp: McpServerConfig = serde_json::from_value(serde_json::json!({
|
||||
"command": mcp_server_bin,
|
||||
"environment_id": remote_aware_environment_id(),
|
||||
|
||||
@@ -582,6 +582,10 @@ async fn mcp_tool_hook_starts_cached_model_hidden_tools(
|
||||
.expect("enable bundled plugin fixture");
|
||||
})
|
||||
.with_config(move |config| {
|
||||
config
|
||||
.features
|
||||
.disable(Feature::McpConnectionSharing)
|
||||
.expect("use a cacheable legacy catalog");
|
||||
config
|
||||
.features
|
||||
.enable(Feature::Plugins)
|
||||
@@ -748,6 +752,10 @@ async fn mcp_tool_hook_timeout_includes_cached_server_startup() -> Result<()> {
|
||||
.expect("write model-hidden MCP tool hook config");
|
||||
})
|
||||
.with_config(move |config| {
|
||||
config
|
||||
.features
|
||||
.disable(Feature::McpConnectionSharing)
|
||||
.expect("use a cacheable legacy catalog");
|
||||
enable_hooks_and_rmcp_server(
|
||||
config,
|
||||
rmcp_test_server_bin,
|
||||
|
||||
@@ -616,6 +616,10 @@ pub(super) async fn cached_stdio_first_use_scenario() -> anyhow::Result<Vec<Valu
|
||||
.with_model_info_override("gpt-5.4", |model| model.supports_search_tool = false)
|
||||
.with_config(move |config| {
|
||||
core_test_support::test_codex::configure_scenario_catalog(config);
|
||||
config
|
||||
.features
|
||||
.disable(Feature::McpConnectionSharing)
|
||||
.expect("use a cacheable legacy catalog");
|
||||
config.update_plan_enabled = true;
|
||||
config.permissions.approval_policy = Constrained::allow_any(AskForApproval::Never);
|
||||
config
|
||||
|
||||
@@ -71,6 +71,10 @@ async fn mcp_resource_messages(tool_mode: ToolMode) -> Result<()> {
|
||||
.with_config(move |config| {
|
||||
super::configure_scenario_catalog(config);
|
||||
config.code_mode.disable_in_process_fallback = true;
|
||||
config
|
||||
.features
|
||||
.disable(Feature::McpConnectionSharing)
|
||||
.expect("use a cacheable legacy catalog");
|
||||
config
|
||||
.features
|
||||
.enable(Feature::CodeModeHost)
|
||||
|
||||
@@ -764,6 +764,10 @@ async fn token_budget_context_injects_plain_thread_hint_text(cached_subagent: bo
|
||||
let test = test_codex()
|
||||
.with_model_info_override("gpt-5.5", |model| model.use_responses_lite = true)
|
||||
.with_config(move |config| {
|
||||
config
|
||||
.features
|
||||
.set_enabled(Feature::McpConnectionSharing, !cached_subagent)
|
||||
.expect("use a cacheable legacy catalog for the subagent");
|
||||
config.model_context_window = Some(CONFIGURED_CONTEXT_WINDOW);
|
||||
config
|
||||
.features
|
||||
|
||||
@@ -1819,8 +1819,8 @@ pub const FEATURES: &[FeatureSpec] = &[
|
||||
FeatureSpec {
|
||||
id: Feature::McpConnectionSharing,
|
||||
key: "mcp_connection_sharing",
|
||||
stage: Stage::UnderDevelopment,
|
||||
default_enabled: false,
|
||||
stage: Stage::Stable,
|
||||
default_enabled: true,
|
||||
},
|
||||
FeatureSpec {
|
||||
id: Feature::McpSharedReplCwd,
|
||||
|
||||
@@ -49,8 +49,12 @@ fn main() -> Result<()> {
|
||||
| "legacy-reject-and-exit"
|
||||
| "legacy-init-first"
|
||||
| "legacy-reject-late-initialize"
|
||||
| "legacy-empty-discover"
|
||||
| "legacy-late"
|
||||
| "legacy-silent"
|
||||
| "legacy-slow"
|
||||
| "legacy-ignore-initialize-after-discover"
|
||||
| "legacy-delayed-init-first"
|
||||
| "legacy-versions"
|
||||
| "oversized-stdout"
|
||||
| "oversized-stdout-legacy"
|
||||
@@ -103,6 +107,12 @@ fn main() -> Result<()> {
|
||||
"server/discover" if mode == "legacy-late" => {
|
||||
late_discovery_id = Some(message["id"].clone());
|
||||
}
|
||||
"server/discover" if mode == "legacy-empty-discover" => {
|
||||
write_message(
|
||||
&mut stdout,
|
||||
json!({"jsonrpc": "2.0", "id": message["id"], "result": {}}),
|
||||
)?;
|
||||
}
|
||||
// The first helper makes its version offer only after it receives initialize.
|
||||
"server/discover"
|
||||
if mode == "modern-late"
|
||||
@@ -174,7 +184,15 @@ fn main() -> Result<()> {
|
||||
}),
|
||||
)?;
|
||||
}
|
||||
"initialize" if mode == "legacy-reject-late-initialize" && index != 0 => {
|
||||
"initialize"
|
||||
if matches!(
|
||||
mode.as_str(),
|
||||
"legacy-reject-late-initialize" | "legacy-delayed-init-first"
|
||||
) && index != 0 =>
|
||||
{
|
||||
if mode == "legacy-delayed-init-first" {
|
||||
std::thread::sleep(std::time::Duration::from_millis(1100));
|
||||
}
|
||||
write_message(
|
||||
&mut stdout,
|
||||
json!({"jsonrpc": "2.0", "id": message["id"], "error": {
|
||||
@@ -182,8 +200,18 @@ fn main() -> Result<()> {
|
||||
}}),
|
||||
)?;
|
||||
}
|
||||
"initialize" if mode == "legacy-ignore-initialize-after-discover" && index != 0 => {}
|
||||
"initialize" => {
|
||||
assert!(is_legacy);
|
||||
if mode == "legacy-delayed-init-first" {
|
||||
std::thread::sleep(std::time::Duration::from_millis(800));
|
||||
}
|
||||
if matches!(mode.as_str(), "legacy-slow" | "legacy-empty-discover") {
|
||||
assert_ne!(index, 0, "initialize should use the original helper");
|
||||
}
|
||||
if mode == "legacy-slow" {
|
||||
std::thread::sleep(std::time::Duration::from_millis(3500));
|
||||
}
|
||||
if mode == "legacy-late" {
|
||||
let id = late_discovery_id
|
||||
.take()
|
||||
|
||||
@@ -18,8 +18,6 @@ use rmcp::model::ElicitRequest;
|
||||
use rmcp::model::ElicitRequestParams;
|
||||
use rmcp::model::ElicitationSchema;
|
||||
use rmcp::model::Implementation;
|
||||
use rmcp::model::InitializeRequestParams;
|
||||
use rmcp::model::InitializeResult;
|
||||
use rmcp::model::InputRequest;
|
||||
use rmcp::model::InputRequiredResult;
|
||||
use rmcp::model::JsonObject;
|
||||
@@ -48,7 +46,6 @@ struct TestToolServer {
|
||||
tools: Arc<Vec<Tool>>,
|
||||
resources: Arc<Vec<Resource>>,
|
||||
resource_templates: Arc<Vec<ResourceTemplate>>,
|
||||
supports_openai_form_elicitation: Arc<AtomicBool>,
|
||||
tools_discovered: Arc<AtomicBool>,
|
||||
invalidated_listing: Arc<AtomicBool>,
|
||||
}
|
||||
@@ -216,7 +213,6 @@ impl TestToolServer {
|
||||
tools: Arc::new(tools),
|
||||
resources: Arc::new(resources),
|
||||
resource_templates: Arc::new(resource_templates),
|
||||
supports_openai_form_elicitation: Arc::new(AtomicBool::new(false)),
|
||||
tools_discovered: Arc::default(),
|
||||
invalidated_listing: Arc::default(),
|
||||
}
|
||||
@@ -543,28 +539,6 @@ struct ImageScenarioArgs {
|
||||
}
|
||||
|
||||
impl ServerHandler for TestToolServer {
|
||||
async fn initialize(
|
||||
&self,
|
||||
request: InitializeRequestParams,
|
||||
context: rmcp::service::RequestContext<rmcp::service::RoleServer>,
|
||||
) -> Result<InitializeResult, McpError> {
|
||||
if let Ok(barrier_file) = std::env::var(INITIALIZE_BARRIER_FILE_ENV) {
|
||||
while !std::path::Path::new(&barrier_file).is_file() {
|
||||
sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
}
|
||||
self.supports_openai_form_elicitation.store(
|
||||
request
|
||||
.capabilities
|
||||
.extensions
|
||||
.as_ref()
|
||||
.is_some_and(|extensions| extensions.contains_key("openai/form")),
|
||||
Ordering::Relaxed,
|
||||
);
|
||||
context.peer.set_peer_info(request);
|
||||
Ok(self.get_info())
|
||||
}
|
||||
|
||||
fn get_info(&self) -> ServerConfig {
|
||||
let mut capabilities = ServerCapabilities::builder()
|
||||
.enable_tools()
|
||||
@@ -851,9 +825,10 @@ impl ServerHandler for TestToolServer {
|
||||
}
|
||||
}
|
||||
"client_capabilities" => Ok(Self::structured_result(json!({
|
||||
"supportsOpenaiFormElicitation": self
|
||||
.supports_openai_form_elicitation
|
||||
.load(Ordering::Relaxed),
|
||||
"supportsOpenaiFormElicitation": context
|
||||
.client_capabilities()
|
||||
.and_then(|capabilities| capabilities.extensions)
|
||||
.is_some_and(|extensions| extensions.contains_key("openai/form")),
|
||||
}))),
|
||||
"sandbox_meta" | "get_codex_security_daybreak_access" | "get_daybreak_access" => Ok(
|
||||
Self::structured_result(serde_json::Value::Object(context.meta.0.0)),
|
||||
@@ -1244,6 +1219,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Run the server with STDIO transport. If the client disconnects we simply
|
||||
// bubble up the error so the process exits.
|
||||
let service = TestToolServer::new();
|
||||
// Hold both server/discover and initialize behind the startup barrier.
|
||||
if let Some(barrier_file) = std::env::var_os(INITIALIZE_BARRIER_FILE_ENV) {
|
||||
while !std::path::Path::new(&barrier_file).is_file() {
|
||||
sleep(Duration::from_millis(/*millis*/ 10)).await;
|
||||
}
|
||||
}
|
||||
let running = service.serve(stdio()).await?;
|
||||
|
||||
// A test can close an initialized transport without killing an arbitrary PID.
|
||||
|
||||
@@ -8,7 +8,9 @@ use std::time::Duration;
|
||||
|
||||
use futures::FutureExt;
|
||||
use rmcp::model::ClientRequest;
|
||||
use rmcp::model::DiscoverRequestMethod;
|
||||
use rmcp::model::ErrorCode;
|
||||
use rmcp::model::ErrorData;
|
||||
use rmcp::model::JsonRpcMessage;
|
||||
use rmcp::model::RequestId;
|
||||
use rmcp::model::ServerResult;
|
||||
@@ -105,6 +107,19 @@ impl Discovery {
|
||||
// protocol error can be classified as a discovery reply without an ID.
|
||||
if matches_probe || (idless && (!legacy || modern)) {
|
||||
self.probe_id = None;
|
||||
// Some legacy helpers answer unknown methods with {}. Let rmcp initialize
|
||||
// on this pipe, or skip the empty reply if initialize is already in flight.
|
||||
if !matches!(self.outcome, Some(DiscoveryOutcome::Reply))
|
||||
&& let JsonRpcMessage::Response(response) = reply
|
||||
&& matches!(response.result, ServerResult::EmptyResult(_))
|
||||
{
|
||||
*message = Some(JsonRpcMessage::error(
|
||||
ErrorData::method_not_found::<DiscoverRequestMethod>(),
|
||||
Some(response.id.clone()),
|
||||
));
|
||||
self.outcome = Some(DiscoveryOutcome::RetryLegacy);
|
||||
return legacy;
|
||||
}
|
||||
if legacy {
|
||||
// Auto sent initialize before receiving the reply to its earlier probe.
|
||||
if !modern && matches!(reply, JsonRpcMessage::Error(_)) {
|
||||
@@ -235,6 +250,9 @@ impl Transport<RoleClient> for LocalStdioTransport {
|
||||
match transport.into_legacy_pipes() {
|
||||
Ok((stdout, stdin)) => {
|
||||
probe.initialize_id = Some(request.id.clone());
|
||||
// Discovery must not terminate an initialize that is still within the
|
||||
// configured startup timeout.
|
||||
probe.retry_at = None;
|
||||
self.transport = StdioTransport::Legacy(AsyncRwTransport::new(stdout, stdin));
|
||||
}
|
||||
Err(error) => return futures::future::err(error).boxed(),
|
||||
|
||||
@@ -15,6 +15,7 @@ use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
use crate::AuthProviderResolver;
|
||||
use crate::local_stdio_transport::DiscoveryOutcome;
|
||||
use anyhow::Context;
|
||||
use anyhow::Result;
|
||||
use anyhow::anyhow;
|
||||
@@ -249,6 +250,19 @@ impl Drop for ElicitationPauseGuard {
|
||||
}
|
||||
}
|
||||
|
||||
/// Bounds one handshake attempt, or leaves it unbounded when no timeout was configured.
|
||||
async fn handshake_timeout<T>(
|
||||
timeout: Option<Duration>,
|
||||
handshake: impl Future<Output = T>,
|
||||
) -> Result<T> {
|
||||
match timeout {
|
||||
Some(duration) => time::timeout(duration, handshake)
|
||||
.await
|
||||
.with_context(|| format!("timed out handshaking with MCP server after {duration:?}")),
|
||||
None => Ok(handshake.await),
|
||||
}
|
||||
}
|
||||
|
||||
async fn active_time_timeout<T, Fut>(
|
||||
duration: Duration,
|
||||
mut pause_state: watch::Receiver<bool>,
|
||||
@@ -1713,11 +1727,37 @@ impl RmcpClient {
|
||||
),
|
||||
};
|
||||
|
||||
let started = time::Instant::now();
|
||||
let handshake = async {
|
||||
let mut result = transport.await;
|
||||
if let (Some((mut reply, process)), Err(error)) = (discovery, &result)
|
||||
&& let Some(mode) = stdio_handshake::restart_mode(error, reply.try_recv().ok())?
|
||||
{
|
||||
let initial = handshake_timeout(timeout, transport).await;
|
||||
let mode = if let Some((reply, _)) = discovery.as_mut() {
|
||||
let outcome = reply.try_recv();
|
||||
match &initial {
|
||||
Ok(Err(error)) => stdio_handshake::restart_mode(error, outcome.ok())?,
|
||||
Err(_)
|
||||
if matches!(
|
||||
outcome,
|
||||
Ok(DiscoveryOutcome::RetryLegacy)
|
||||
| Err(tokio::sync::oneshot::error::TryRecvError::Closed)
|
||||
) =>
|
||||
{
|
||||
Some(McpProtocolMode::Legacy)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let (Some(mode), Some((_, process))) = (mode, discovery) else {
|
||||
return initial?.map_err(|source| HandshakeError { source }.into());
|
||||
};
|
||||
let retry_timeout = if mode == McpProtocolMode::Legacy {
|
||||
timeout
|
||||
} else {
|
||||
// A modern version re-offer stays within the original deadline.
|
||||
timeout.map(|limit| limit.saturating_sub(started.elapsed()))
|
||||
};
|
||||
handshake_timeout(retry_timeout, async {
|
||||
process
|
||||
.terminate(StdioTermination::Restart)
|
||||
.await
|
||||
@@ -1733,19 +1773,13 @@ impl RmcpClient {
|
||||
}
|
||||
mode => mode.client_lifecycle(),
|
||||
};
|
||||
result = connect_stdio(fresh, lifecycle).await;
|
||||
}
|
||||
result.map_err(|source| HandshakeError { source }.into())
|
||||
};
|
||||
let service_result = match timeout {
|
||||
Some(duration) => time::timeout(duration, handshake)
|
||||
.await
|
||||
.with_context(|| {
|
||||
format!("timed out handshaking with MCP server after {duration:?}")
|
||||
})
|
||||
.flatten(),
|
||||
None => handshake.await,
|
||||
connect_stdio(fresh, lifecycle)
|
||||
.await
|
||||
.map_err(|source| HandshakeError { source }.into())
|
||||
})
|
||||
.await?
|
||||
};
|
||||
let service_result = handshake.await;
|
||||
let service = match service_result {
|
||||
Ok(service) => service,
|
||||
Err(error) => {
|
||||
|
||||
@@ -81,6 +81,17 @@ async fn exercise_stdio_server(
|
||||
let observed_elicitations = Arc::clone(&elicitation_count);
|
||||
let legacy_session = server_mode.starts_with("legacy");
|
||||
let handshake_timeout = match protocol_mode {
|
||||
McpProtocolMode::DiscoverLocalStdio if server_mode == "legacy-slow" => {
|
||||
Duration::from_secs(6)
|
||||
}
|
||||
McpProtocolMode::DiscoverLocalStdio
|
||||
if matches!(
|
||||
server_mode,
|
||||
"legacy-ignore-initialize-after-discover" | "legacy-delayed-init-first"
|
||||
) =>
|
||||
{
|
||||
Duration::from_secs(2)
|
||||
}
|
||||
// Discovery can wait for a silent probe, then stop and relaunch the helper.
|
||||
McpProtocolMode::DiscoverLocalStdio => Duration::from_secs(30),
|
||||
McpProtocolMode::Legacy | McpProtocolMode::V20260728 => Duration::from_secs(5),
|
||||
@@ -253,6 +264,7 @@ async fn unmarked_stdio_helpers_recover_from_failed_or_late_discovery() -> anyho
|
||||
"legacy-init-first",
|
||||
"legacy-reject-late-initialize",
|
||||
"legacy-reject-and-exit",
|
||||
"legacy-empty-discover",
|
||||
"legacy-late",
|
||||
"legacy-silent",
|
||||
"legacy-versions",
|
||||
@@ -270,6 +282,39 @@ async fn unmarked_stdio_helpers_recover_from_failed_or_late_discovery() -> anyho
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn legacy_initialize_can_outlive_the_discovery_watchdog() -> anyhow::Result<()> {
|
||||
exercise_stdio_server(
|
||||
"legacy-slow",
|
||||
McpProtocolMode::DiscoverLocalStdio,
|
||||
/*opt_in*/ false,
|
||||
/*use_executor*/ false,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn unresponsive_legacy_initialize_can_still_restart() -> anyhow::Result<()> {
|
||||
exercise_stdio_server(
|
||||
"legacy-ignore-initialize-after-discover",
|
||||
McpProtocolMode::DiscoverLocalStdio,
|
||||
/*opt_in*/ false,
|
||||
/*use_executor*/ false,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn failed_legacy_initialize_gives_its_replacement_a_fresh_timeout() -> anyhow::Result<()> {
|
||||
exercise_stdio_server(
|
||||
"legacy-delayed-init-first",
|
||||
McpProtocolMode::DiscoverLocalStdio,
|
||||
/*opt_in*/ false,
|
||||
/*use_executor*/ false,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn modern_stdio_safely_falls_back_to_legacy_elicitation() -> anyhow::Result<()> {
|
||||
exercise_stdio_server(
|
||||
|
||||
Reference in New Issue
Block a user