Start required cached MCP servers lazily for subagents (#38217)

## Why

Subagents can reuse cached MCP tool definitions without opening another server
connection immediately, but marking a server as required previously forced eager
startup even when its cached tools were available.

## What changed

- Allow required MCP servers with usable cached tools to remain dormant under
  the lazy startup policy.
- Treat those cached tools as satisfying required-server validation and tool
  catalog capture until the server is needed.
- Preserve eager startup for required servers without cached tools and start a
  dormant server when one of its tools is called.

## Testing

Extend the cached HTTP MCP subagent test to cover both optional and required
servers, verifying that the required server stays dormant until its cached tool
is called.

GitOrigin-RevId: 48ea20b9ae447eef27461caee2320ea78465bb6a
This commit is contained in:
jif authored and copyberry committed 2026-08-12 16:50:23 +00:00
1 parent 91d6f48992
commit 7093e8c480
5 files changed
+13 -5

No files matched your search

@@ -493,7 +493,6 @@ impl McpConnectionSet {
catalog_item_limit, catalog_item_limit,
); );
let defer_startup = allow_deferred_startup let defer_startup = allow_deferred_startup
&& !configured_config.required
&& !tool_plugin_provenance.is_selected_plugin_mcp_server(&server_name) && !tool_plugin_provenance.is_selected_plugin_mcp_server(&server_name)
&& async_managed_client && async_managed_client
.tool_catalog_cache_context .tool_catalog_cache_context
@@ -23,6 +23,10 @@ impl McpConnectionSet {
}); });
continue; continue;
}; };
if view.connection.startup_is_dormant() && view.connection.client.has_cached_tools()
{
continue;
}
match view.connection.client().await { match view.connection.client().await {
Ok(_) => {} Ok(_) => {}
@@ -186,7 +186,8 @@ impl McpConnectionSet {
{ {
let required = self.required_servers.binary_search(server_name).is_ok(); let required = self.required_servers.binary_search(server_name).is_ok();
let has_cached_tools = view.connection.client.has_cached_tools(); let has_cached_tools = view.connection.client.has_cached_tools();
let must_wait_for_startup = required let must_wait_for_startup = (required
&& (!view.connection.startup_is_dormant() || !has_cached_tools))
|| self.is_selected_plugin_mcp_server(server_name) || self.is_selected_plugin_mcp_server(server_name)
|| required_servers || required_servers
.iter() .iter()
+2 -2
View File
@@ -48,12 +48,12 @@ use crate::server::EffectiveMcpServer;
use crate::tool_catalog_cache::McpToolCatalogCache; use crate::tool_catalog_cache::McpToolCatalogCache;
use crate::tools::ToolInfo; use crate::tools::ToolInfo;
/// Controls when one task starts its optional MCP servers. /// Controls when one task starts its eligible MCP servers.
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum McpStartupPolicy { pub enum McpStartupPolicy {
/// Start configured servers when their task's MCP runtime is published. /// Start configured servers when their task's MCP runtime is published.
Eager, Eager,
/// Start optional servers with cached tool definitions on first use. /// Start servers with cached tool definitions on first use.
LazyWhenCached, LazyWhenCached,
} }
+5 -1
View File
@@ -30,6 +30,7 @@ use core_test_support::wait_for_mcp_server;
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use serde_json::Value; use serde_json::Value;
use serde_json::json; use serde_json::json;
use test_case::test_case;
use super::rmcp_client::remote_aware_environment_id; use super::rmcp_client::remote_aware_environment_id;
use super::rmcp_client::remote_aware_stdio_server_bin; use super::rmcp_client::remote_aware_stdio_server_bin;
@@ -247,7 +248,9 @@ async fn mcp_calls_stay_bound_to_each_thread() -> anyhow::Result<()> {
} }
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn cached_http_mcp_starts_lazily_for_subagents() -> anyhow::Result<()> { #[test_case(false; "optional server")]
#[test_case(true; "required server")]
async fn cached_http_mcp_starts_lazily_for_subagents(required: bool) -> anyhow::Result<()> {
skip_if_no_network!(Ok(())); skip_if_no_network!(Ok(()));
let responses_server = responses::start_mock_server().await; let responses_server = responses::start_mock_server().await;
@@ -269,6 +272,7 @@ async fn cached_http_mcp_starts_lazily_for_subagents() -> anyhow::Result<()> {
"url": server_url, "url": server_url,
"http_headers": { "Authorization": "Bearer cached-http-test-token" }, "http_headers": { "Authorization": "Bearer cached-http-test-token" },
"enabled_tools": ["calendar_create_event"], "enabled_tools": ["calendar_create_event"],
"required": required,
"startup_timeout_sec": 10, "startup_timeout_sec": 10,
})) }))
.expect("HTTP MCP server configuration"), .expect("HTTP MCP server configuration"),