Overlap remote MCP config reads with capability discovery (#65101)

## Why

MCP runtime refreshes waited for capability discovery and host MCP
configuration to finish before requesting HTTP MCP servers from the selected
remote environment. That serialized two reads that can be in flight together.

## What changed

Start the remote environment config read alongside host MCP configuration,
including executor capability discovery. Start the read's 10-second timeout
after host configuration is ready so a serial executor can queue the read
behind discovery without consuming its timeout. Merge the results with host
server precedence and requirements still enforced.

## Testing

Add V1 and V2 integration cases that hold both executor replies until both
requests arrive, then verify host precedence and requirements. Add a
paused-clock test that covers a queued read and confirms a stalled read
times out 10 seconds after discovery completes.

GitOrigin-RevId: d974f0f575559bfe6b756de11aa460399fdabf25
This commit is contained in:
Ben Romano
2026-12-01 22:57:35 +00:00
committed by copyberry
parent 6cc6cee6c5
commit 1aeab9e3d5
5 changed files with 380 additions and 186 deletions
+30 -17
View File
@@ -95,29 +95,27 @@ fn include_startup_preference(
}
impl Environment {
/// Reads executor-owned HTTP MCP servers from the selected project's current config.
/// Reads executor-owned HTTP MCP servers, allowing the full timeout after `timeout_start`.
pub async fn discover_http_mcp_servers(
&self,
cwd: PathUri,
timeout_start: impl Future<Output = ()>,
) -> Result<Vec<(String, McpServerConfig)>, ExecServerError> {
let (response, http_header_env_vars) =
tokio::time::timeout(std::time::Duration::from_secs(10), async {
let capabilities = self.info().await?.capabilities;
if !capabilities.environment_config_read {
return Ok((None, false));
}
self.read_environment_config(EnvironmentConfigReadParams {
cwd,
config_paths: vec![vec!["mcp_servers".to_string()]],
requirements_paths: vec![vec!["mcp_servers".to_string()]],
})
.await
.map(|response| (Some(response), capabilities.http_header_env_vars))
let request = async {
let capabilities = self.info().await?.capabilities;
if !capabilities.environment_config_read {
return Ok((None, false));
}
self.read_environment_config(EnvironmentConfigReadParams {
cwd,
config_paths: vec![vec!["mcp_servers".to_string()]],
requirements_paths: vec![vec!["mcp_servers".to_string()]],
})
.await
.map_err(|_| {
ExecServerError::Protocol("executor MCP discovery timed out".to_string())
})??;
.map(|response| (Some(response), capabilities.http_header_env_vars))
};
let (response, http_header_env_vars) =
mcp_discovery_with_timeout(request, timeout_start).await?;
let Some(response) = response else {
return Ok(Vec::new());
};
@@ -174,6 +172,21 @@ impl Environment {
}
}
async fn mcp_discovery_with_timeout<T>(
request: impl Future<Output = Result<T, ExecServerError>>,
timeout_start: impl Future<Output = ()>,
) -> Result<T, ExecServerError> {
let deadline = async {
timeout_start.await;
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
};
tokio::select! {
biased;
result = request => result,
() = deadline => Err(ExecServerError::Protocol("executor MCP discovery timed out".to_string())),
}
}
fn validate_paths(params: &EnvironmentConfigReadParams) -> Result<(), ReadEnvironmentConfigError> {
if params.config_paths.is_empty() && params.requirements_paths.is_empty() {
return Err(ReadEnvironmentConfigError::InvalidParams(
@@ -1,5 +1,8 @@
use super::*;
use pretty_assertions::assert_eq;
use std::time::Duration;
use tokio::time::Instant;
use tokio::time::sleep;
/// Legacy file and MDM values must still win after a CLI preference is projected.
#[test]
@@ -50,3 +53,29 @@ fn startup_preference_stays_below_both_managed_layers() -> anyhow::Result<()> {
}
Ok(())
}
#[tokio::test(start_paused = true)]
async fn config_queued_behind_discovery_keeps_its_original_timeout() {
// The executor handles discovery first, then finishes the pending config request.
let queued_config = async {
sleep(Duration::from_secs(14)).await;
Ok(())
};
let discovery = sleep(Duration::from_secs(12));
mcp_discovery_with_timeout(queued_config, discovery)
.await
.expect("a 2-second config read after discovery should finish");
let start = Instant::now();
let hung_config = std::future::pending::<Result<(), crate::ExecServerError>>();
let discovery = sleep(Duration::from_secs(12));
let error = mcp_discovery_with_timeout(hung_config, discovery)
.await
.expect_err("a stalled config read must time out");
assert!(
error
.to_string()
.contains("executor MCP discovery timed out")
);
assert_eq!(start.elapsed(), Duration::from_secs(22));
}