Keep MCP event subscriptions alive after task unloading (#41899)

## Why

Event subscriptions may need to outlive the MCP runtime associated with a task.

## What changed

- Add `McpEventStreamOpener`, which retains the hosted Apps MCP connection settings and opens each event stream with an independent client.
- Keep subscription cancellation tied to account changes and removal of the task's event server, including when cancellation ownership is forwarded across runtime replacement.
- Expose the opener and cancellation forwarding through `McpResourceClient`.

## Testing

Add an HTTP transport test that shuts down the runtime, refreshes credentials for the same account, and verifies that a new event stream connects with the current authorization and receives notifications.

GitOrigin-RevId: e102aa382e63121661ff0567f7523515e459922d
This commit is contained in:
Evan Fannin authored and copyberry committed 2026-08-31 19:18:18 +00:00
1 parent d60560f14e
commit 53691bad98
11 files changed
+520 -15

No files matched your search

+24 -2
View File
@@ -33,6 +33,7 @@ use crate::binding::call_tool_result_from_rmcp;
use crate::catalog::McpServerSource;
use crate::elicitation::ElicitationRequestManager;
use crate::elicitation::ElicitationRequestRouter;
use crate::event_stream::EventStreamConnectionSettings;
use crate::mcp::CODEX_APPS_MCP_SERVER_NAME;
use crate::mcp::ToolPluginProvenance;
use crate::pagination::MAX_CODEX_APPS_TOOL_CATALOG_ITEMS;
@@ -184,6 +185,7 @@ impl McpServerView {
/// A published view over a set of running MCP server connections.
pub(crate) struct McpConnectionSet {
servers: HashMap<String, McpServerView>,
pub(crate) event_stream_connection: Option<Arc<EventStreamConnectionSettings>>,
disabled_servers: Vec<String>,
protocol_mode: crate::McpProtocolMode,
required_servers: Vec<String>,
@@ -237,6 +239,7 @@ impl McpConnectionSet {
let tool_plugin_provenance = crate::mcp::tool_plugin_provenance(&config);
let auth = auth.as_ref();
let mut servers = HashMap::new();
let mut event_stream_connection = None;
let disabled_servers = mcp_servers
.iter()
.filter(|(_, server)| !server.enabled())
@@ -276,9 +279,12 @@ impl McpConnectionSet {
let static_chatgpt_auth_provider = auth
.filter(|auth| auth.uses_codex_backend())
.map(codex_model_provider::auth_provider_from_auth);
let codex_apps_auth_provider = auth_manager.and_then(|auth_manager| {
let codex_apps_auth_provider = auth_manager.as_ref().and_then(|auth_manager| {
auth.filter(|auth| auth.uses_codex_backend()).map(|auth| {
codex_model_provider::auth_provider_from_auth_manager(auth_manager, auth)
codex_model_provider::auth_provider_from_auth_manager(
Arc::clone(auth_manager),
auth,
)
})
});
for (server_name, server) in mcp_servers
@@ -351,6 +357,20 @@ impl McpConnectionSet {
} else {
chatgpt_auth_provider_for_server(&server, chatgpt_auth_provider)
};
if is_host_owned_codex_apps {
event_stream_connection = Some(Arc::new(EventStreamConnectionSettings {
server: server.clone(),
store_mode,
keyring_backend_kind,
runtime_context: runtime_context.clone(),
resolved_environment: resolved_environment.clone(),
auth_provider: runtime_auth_provider.clone(),
auth_manager: auth_manager.clone(),
auth: auth.cloned(),
protocol_mode,
client_mcp_extensions: client_mcp_extensions.clone(),
}));
}
let connection_identity = McpServerConnectionIdentity::new(
&server_name,
&server,
@@ -682,6 +702,7 @@ impl McpConnectionSet {
}
let manager = Self {
servers,
event_stream_connection,
disabled_servers,
protocol_mode,
required_servers,
@@ -743,6 +764,7 @@ impl McpConnectionSet {
pub fn empty(prefix_mcp_tool_names: bool) -> Self {
Self {
servers: HashMap::new(),
event_stream_connection: None,
disabled_servers: Vec::new(),
protocol_mode: crate::McpProtocolMode::Legacy,
required_servers: Vec::new(),