File size: 44,188 Bytes
17f328f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 | //! Aggregates MCP server connections for Codex.
//!
//! [`McpConnectionSet`] is the private connection set behind
//! [`crate::McpRuntime`] and [`crate::McpBinding`]. It coordinates startup status
//! events, keeps server metadata, and aggregates tools and resources across
//! running RMCP clients.
#[path = "connection_manager/required.rs"]
mod required;
#[path = "connection_manager/resources.rs"]
mod resources;
#[path = "connection_manager/startup.rs"]
mod startup;
#[path = "connection_manager/status.rs"]
mod status;
#[path = "connection_manager/tool_catalog.rs"]
mod tool_catalog;
use startup::chatgpt_auth_provider_for_server;
use startup::emit_update;
use startup::mcp_init_error_display;
use startup::mcp_startup_failure_reason;
use startup::should_share_codex_apps_tools_cache;
pub(crate) use tool_catalog::BindingCatalogRevision;
pub use tool_catalog::tool_is_model_visible;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::OnceLock;
use std::sync::atomic::Ordering;
use std::time::Duration;
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::ToolPluginContext;
use crate::pagination::MAX_CODEX_APPS_TOOL_CATALOG_ITEMS;
use crate::pagination::MAX_MCP_CATALOG_ITEMS;
use crate::rmcp_client::AsyncManagedClient;
use crate::rmcp_client::DEFAULT_STARTUP_TIMEOUT;
use crate::rmcp_client::DEFAULT_TOOL_TIMEOUT;
use crate::rmcp_client::ManagedClient;
use crate::rmcp_client::StartupOutcomeError;
use crate::rmcp_client::prepare_codex_apps_tools_for_model;
use crate::rmcp_client::prepare_regular_mcp_tools_for_model;
use crate::runtime::McpPublicationGate;
use crate::runtime::McpRuntimeInput;
use crate::runtime::McpStartupPolicy;
use crate::server::McpServerConnectionIdentity;
use crate::server::McpServerMetadata;
use crate::tool_catalog_cache::McpToolCatalogCacheContext;
use crate::tools::ToolFilter;
use crate::tools::ToolInfo;
use crate::tools::filter_tools;
use crate::trusted_access::ENTITLEMENT_CONTEXT_KEY;
use crate::trusted_access::TrustedAccessContext;
use anyhow::Context;
use anyhow::Result;
use anyhow::anyhow;
use anyhow::bail;
use codex_config::McpServerTransportConfig;
use codex_diagnostics::Gauge;
use codex_diagnostics::GaugeGuard;
use codex_protocol::mcp::CallToolResult;
use codex_protocol::mcp::McpServerInfo;
use codex_protocol::protocol::Event;
use codex_protocol::protocol::EventMsg;
use codex_protocol::protocol::McpStartupCompleteEvent;
use codex_protocol::protocol::McpStartupFailure;
use codex_protocol::protocol::McpStartupFailureReason;
use codex_protocol::protocol::McpStartupStatus;
use codex_protocol::protocol::McpStartupUpdateEvent;
use codex_rmcp_client::determine_streamable_http_auth_status_from_credentials;
use tokio::sync::watch;
use tokio::task::JoinSet;
use tracing::warn;
static LIVE_CONNECTIONS: Gauge = Gauge::new("mcp.connections.live");
pub(crate) struct McpServerConnection {
identity: Option<McpServerConnectionIdentity>,
client: AsyncManagedClient,
// Startup-only budget; changing it must not replace a ready connection.
startup_timeout: Duration,
startup_trigger: Option<watch::Sender<bool>>,
_diagnostics_guard: GaugeGuard,
}
impl McpServerConnection {
async fn reusable_client(
&self,
desired: &McpServerConnectionIdentity,
) -> Option<ManagedClient> {
let current = self.identity.as_ref()?;
if !current.has_same_connection_config(desired) {
return None;
}
if !self.client.startup_complete.load(Ordering::Acquire) {
return None;
}
let client = self.client.client().await.ok()?;
if client.client.is_closed().await {
return None;
}
if current == desired {
if matches!(desired.oauth_credentials(), Ok(None))
&& tokio::time::timeout(Duration::ZERO, client.client.managed_oauth_credentials())
.await
.is_ok_and(|credentials| matches!(credentials, Some(Some(_))))
{
return None;
}
return Some(client);
}
let Ok(desired_credentials) = desired.oauth_credentials() else {
return Some(client);
};
let reusable = match client.client.managed_oauth_credentials().await {
Some(live_credentials) => live_credentials.as_ref() == desired_credentials,
None => current
.oauth_credentials()
.is_ok_and(|startup_credentials| startup_credentials == desired_credentials),
};
if reusable { Some(client) } else { None }
}
pub(crate) async fn client(&self) -> Result<ManagedClient, StartupOutcomeError> {
if let Some(startup_trigger) = &self.startup_trigger {
startup_trigger.send_replace(true);
}
self.client.client().await
}
async fn shutdown(&self) {
self.client.shutdown().await;
}
fn cancel_startup(&self) {
if !self.startup_is_dormant() && !self.client.startup_complete.load(Ordering::Acquire) {
self.client.cancel_token.cancel();
}
}
fn startup_is_dormant(&self) -> bool {
self.startup_trigger
.as_ref()
.is_some_and(|startup_trigger| !*startup_trigger.borrow())
}
}
impl Drop for McpServerConnection {
fn drop(&mut self) {
self.client.cancel_token.cancel();
}
}
#[derive(Clone)]
struct McpServerView {
connection: Arc<McpServerConnection>,
protocol_mode: crate::McpProtocolMode,
metadata: McpServerMetadata,
tool_filter: ToolFilter,
tool_timeout: Option<Duration>,
catalog_item_limit: usize,
}
impl McpServerView {
async fn listed_tools(
&self,
tool_plugin_context: &ToolPluginContext,
) -> Result<Vec<ToolInfo>, StartupOutcomeError> {
let tools = self.connection.client.listed_tools().await?;
let tools = filter_tools(tools, &self.tool_filter);
Ok(if self.connection.client.is_codex_apps_mcp_server {
prepare_codex_apps_tools_for_model(tools, tool_plugin_context)
} else {
prepare_regular_mcp_tools_for_model(tools, tool_plugin_context)
})
}
}
/// 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>,
required_servers: Vec<String>,
optional_startup_deadline: OnceLock<tokio::time::Instant>,
tool_plugin_context: Arc<ToolPluginContext>,
prefix_mcp_tool_names: bool,
non_prefixed_mcp_tool_servers: Vec<String>,
elicitation_requests: ElicitationRequestManager,
pub(crate) trusted_access: Option<TrustedAccessContext>,
}
impl McpConnectionSet {
/// Creates an MCP connection manager. Threadless callers can pass no `tx_event`; startup
/// notifications are then skipped and interactive elicitations are declined.
pub async fn new(
previous: Option<&Self>,
publication_gate: McpPublicationGate,
input: McpRuntimeInput,
elicitation_router: ElicitationRequestRouter,
) -> Self {
let trusted_access = TrustedAccessContext::from_runtime(&input);
let McpRuntimeInput {
startup_policy,
config,
plugins_available: _,
ready_selected_capability_roots: _,
mcp_servers,
submit_id,
tx_event,
startup_cancellation_token,
runtime_context,
codex_apps_tools_cache,
tool_catalog_cache,
codex_apps_tools_cache_key,
client_mcp_extensions,
auth,
auth_manager,
elicitation_reviewer,
elicitation_lifecycle,
} = input;
let store_mode = config.mcp_oauth_credentials_store_mode;
let keyring_backend_kind = config.auth_keyring_backend_kind;
let oauth_refresh_mode = config.oauth_refresh_mode;
let codex_home = config.codex_home.clone();
let prefix_mcp_tool_names = config.prefix_mcp_tool_names;
let non_prefixed_mcp_tool_servers = config.non_prefixed_mcp_tool_servers.clone();
let default_protocol_mode = config.protocol_mode;
let host_owned_apps_protocol_mode = config.host_owned_apps_protocol_mode;
let client_elicitation_capability = config.client_elicitation_capability.clone();
let tool_plugin_context = crate::mcp::tool_plugin_context(&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())
.map(|(name, _)| name.clone())
.collect();
let mut required_servers = mcp_servers
.iter()
.filter(|(_, server)| server.enabled() && server.required())
.map(|(server_name, _)| server_name.clone())
.collect::<Vec<_>>();
required_servers.sort();
let mut reused_ready = Vec::new();
let mut join_set = JoinSet::new();
// Explicit reconnects have no previous set and must replace their clients eagerly.
let allow_deferred_startup =
startup_policy == McpStartupPolicy::LazyWhenCached && previous.is_some();
let reusable_previous = previous.filter(|previous| {
!previous.servers.is_empty()
&& previous.elicitation_requests.update(
Arc::clone(&config),
elicitation_reviewer.clone(),
elicitation_lifecycle.clone(),
)
});
let elicitation_requests = if let Some(previous) = reusable_previous {
previous.elicitation_requests.clone()
} else {
ElicitationRequestManager::new(
Arc::clone(&config),
elicitation_reviewer,
elicitation_lifecycle,
elicitation_router,
)
};
let tool_plugin_context = Arc::new(tool_plugin_context);
let startup_submit_id = submit_id;
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.as_ref().and_then(|auth_manager| {
auth.filter(|auth| auth.uses_codex_backend()).map(|auth| {
codex_model_provider::auth_provider_from_auth_manager(
Arc::clone(auth_manager),
auth,
)
})
});
for (server_name, server) in mcp_servers
.into_iter()
.filter(|(_, server)| server.enabled())
{
let registration = config.mcp_server_catalog.server(&server_name);
let client_mcp_extensions = crate::client_capabilities::server_mcp_extensions(
&client_mcp_extensions,
&server_name,
registration,
);
let is_host_owned_codex_apps = registration.is_some_and(|server| {
server
.source()
.is_host_owned_apps(&server_name, server.config())
});
let host_plugin_root = registration.and_then(|server| match server.source() {
McpServerSource::Plugin(plugin) => plugin.host_root(),
McpServerSource::SelectedPlugin(_)
| McpServerSource::Config
| McpServerSource::Compatibility { .. }
| McpServerSource::Extension { .. } => None,
});
let catalog_item_limit = if is_host_owned_codex_apps {
MAX_CODEX_APPS_TOOL_CATALOG_ITEMS
} else {
MAX_MCP_CATALOG_ITEMS
};
let metadata = McpServerMetadata::from(&server);
let configured_config = server.config().clone();
let protocol_mode = if matches!(
&configured_config.transport,
McpServerTransportConfig::StreamableHttp { .. }
) {
registration
.and_then(crate::ResolvedMcpServer::protocol_mode)
.unwrap_or(if is_host_owned_codex_apps {
host_owned_apps_protocol_mode
} else {
default_protocol_mode
})
} else {
default_protocol_mode
};
let configured_tool_filter = ToolFilter::from_config(&configured_config);
let startup_timeout = configured_config
.startup_timeout_sec
.unwrap_or(DEFAULT_STARTUP_TIMEOUT);
let configured_tool_timeout = Some(
configured_config
.tool_timeout_sec
.unwrap_or(DEFAULT_TOOL_TIMEOUT),
);
let resolved_environment =
runtime_context.resolve_server_environment(&server_name, &configured_config);
// For built-in Codex Apps, `CODEX_CONNECTORS_TOKEN` is a debug
// override: it supplies runtime auth but bypasses the shared tools
// cache.
let uses_env_bearer_token = match &configured_config.transport {
McpServerTransportConfig::StreamableHttp {
bearer_token_env_var,
..
} => bearer_token_env_var.is_some(),
McpServerTransportConfig::Stdio { .. } => false,
};
let shares_codex_apps_tools_cache = is_host_owned_codex_apps
&& should_share_codex_apps_tools_cache(&server_name, uses_env_bearer_token);
let codex_apps_tools_cache_context = shares_codex_apps_tools_cache.then(|| {
// Only equivalent discovery inputs may share executable Apps tools.
let mut transport = configured_config.transport.clone();
if let McpServerTransportConfig::StreamableHttp {
http_headers: Some(headers),
..
} = &mut transport
{
// mcp_server_config_for_url in codex-rs/codex-mcp/src/mcp/mod.rs
// adds thread attribution that threadless discovery does not carry.
headers.retain(|name, _| !name.eq_ignore_ascii_case("originator"));
}
let mut scope = serde_json::json!([
transport,
&configured_config.auth,
protocol_mode.preferred_protocol_version().as_str(),
catalog_item_limit,
]);
scope.sort_all_objects();
codex_apps_tools_cache
.context(codex_home.clone(), codex_apps_tools_cache_key.clone())
.with_live_scope(scope.to_string())
});
// The reserved Codex Apps registration follows the shared
// AuthManager across refreshes. In the hosted-plugin path, this
// is the ChatGPT /ps/mcp connection. User-configured MCP
// registrations keep their existing configured auth path.
let chatgpt_auth_provider = if server_name == CODEX_APPS_MCP_SERVER_NAME {
codex_apps_auth_provider
.clone()
.or_else(|| static_chatgpt_auth_provider.clone())
} else {
static_chatgpt_auth_provider.clone()
};
// If Codex Apps has an env bearer token, that is its auth path. Do
// not also attach the ambient CodexAuth provider.
let runtime_auth_provider =
if server_name == CODEX_APPS_MCP_SERVER_NAME && uses_env_bearer_token {
None
} 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,
oauth_refresh_mode,
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,
host_plugin_root,
store_mode,
keyring_backend_kind,
oauth_refresh_mode,
&resolved_environment,
&runtime_context,
runtime_auth_provider.as_ref(),
auth,
shares_codex_apps_tools_cache
.then(|| (codex_home.clone(), codex_apps_tools_cache_key.clone())),
client_elicitation_capability.clone(),
client_mcp_extensions.clone(),
previous
.and_then(|previous| previous.servers.get(&server_name))
.and_then(|view| view.connection.identity.as_ref()),
);
let expected_protocol_mode = match &configured_config.transport {
McpServerTransportConfig::StreamableHttp { .. } => Some(protocol_mode),
McpServerTransportConfig::Stdio { .. }
if protocol_mode == crate::McpProtocolMode::Legacy =>
{
Some(crate::McpProtocolMode::Legacy)
}
McpServerTransportConfig::Stdio { env, .. } => match env
.as_ref()
.and_then(|variables| variables.get("CODEX_MCP_PROTOCOL_VERSION"))
{
None => Some(crate::McpProtocolMode::Legacy),
Some(version)
if version == rmcp::model::ProtocolVersion::V_2026_07_28.as_str() =>
{
Some(protocol_mode)
}
Some(_) => None,
},
};
if let Some(previous_view) =
reusable_previous.and_then(|previous| previous.servers.get(&server_name))
{
let connection = Arc::clone(&previous_view.connection);
let reusable_pending_startup = connection.identity.as_ref()
== Some(&connection_identity)
&& !connection.client.startup_complete.load(Ordering::Acquire)
&& connection.startup_timeout == startup_timeout
&& !connection.startup_is_dormant()
&& !connection.client.cancel_token.is_cancelled()
&& previous_view.catalog_item_limit == catalog_item_limit
&& expected_protocol_mode.is_some()
&& previous_view.protocol_mode == protocol_mode;
let unchanged_auth_failure = if connection.identity.as_ref()
== Some(&connection_identity)
&& connection_identity.oauth_store_was_contended
&& previous_view.protocol_mode == protocol_mode
&& connection.client.startup_complete.load(Ordering::Acquire)
{
connection
.client()
.await
.err()
.filter(StartupOutcomeError::is_authentication_required)
} else {
None
};
if reusable_pending_startup
|| unchanged_auth_failure.is_some()
|| connection
.reusable_client(&connection_identity)
.await
.is_some_and(|client| {
previous_view.catalog_item_limit == catalog_item_limit
&& expected_protocol_mode.is_some_and(|expected| {
client.client.protocol_mode() == expected
})
})
{
let pending_client =
reusable_pending_startup.then(|| connection.client.clone());
servers.insert(
server_name.clone(),
McpServerView {
connection,
protocol_mode,
metadata,
tool_filter: configured_tool_filter,
tool_timeout: configured_tool_timeout,
catalog_item_limit,
},
);
if let Some(error) = unchanged_auth_failure {
let reason = connection_identity
.oauth_credentials()
.ok()
.flatten()
.map(|_| McpStartupFailureReason::ReauthenticationRequired);
let status = McpStartupStatus::Failed {
error: mcp_init_error_display(
&server_name,
Some(&configured_config),
&error,
reason,
),
reason,
};
let tx_event = tx_event.clone();
let submit_id = startup_submit_id.clone();
let publication_gate = publication_gate.clone();
join_set.spawn(async move {
if !publication_gate.wait().await {
return (server_name, Err(StartupOutcomeError::Cancelled));
}
if let Some(tx_event) = tx_event.as_ref() {
for status in [McpStartupStatus::Starting, status] {
let _ = emit_update(
submit_id.as_str(),
tx_event,
McpStartupUpdateEvent {
server: server_name.clone(),
status,
},
)
.await;
}
}
(server_name, Err(error))
});
} else if let Some(client) = pending_client {
let publication_gate = publication_gate.clone();
join_set.spawn(async move {
if !publication_gate.wait().await {
return (server_name, Err(StartupOutcomeError::Cancelled));
}
(server_name, client.client().await)
});
} else {
reused_ready.push(server_name);
}
continue;
}
}
let cancel_token = startup_cancellation_token.child_token();
let tool_catalog_cache_context = if server_name == CODEX_APPS_MCP_SERVER_NAME {
None
} else if let Ok(environment) = resolved_environment.as_ref() {
tool_catalog_cache.context(
&server_name,
&configured_config,
&runtime_context,
environment.as_ref(),
(&client_elicitation_capability, &client_mcp_extensions),
Some((
&connection_identity,
protocol_mode,
server.is_agent_plugin(),
)),
)
} else {
None
};
let has_runtime_auth = runtime_auth_provider.is_some();
let async_managed_client = AsyncManagedClient::new(
server_name.clone(),
startup_submit_id.clone(),
server,
store_mode,
keyring_backend_kind,
oauth_refresh_mode,
cancel_token.clone(),
tx_event.clone(),
elicitation_requests.clone(),
codex_apps_tools_cache_context,
tool_catalog_cache_context,
runtime_context.clone(),
resolved_environment,
runtime_auth_provider,
client_elicitation_capability.clone(),
client_mcp_extensions.clone(),
auth_manager
.as_ref()
.filter(|_| {
matches!(
&configured_config.transport,
McpServerTransportConfig::Stdio { .. }
)
})
.map(|manager| manager.auth_change_state_receiver()),
protocol_mode,
catalog_item_limit,
);
let defer_startup = allow_deferred_startup
&& !tool_plugin_context.is_selected_plugin_mcp_server(&server_name)
&& async_managed_client
.tool_catalog_cache_context
.as_ref()
.and_then(McpToolCatalogCacheContext::current_tools)
.is_some_and(|tools| {
tools.into_iter().any(|tool| {
configured_tool_filter.allows(&tool.tool.name)
&& tool_is_model_visible(&tool)
})
});
let (startup_trigger, startup_receiver) = if defer_startup {
let (trigger, receiver) = watch::channel(false);
(Some(trigger), Some(receiver))
} else {
(None, None)
};
servers.insert(
server_name.clone(),
McpServerView {
connection: Arc::new(McpServerConnection {
identity: Some(connection_identity),
client: async_managed_client.clone(),
startup_timeout,
startup_trigger,
_diagnostics_guard: LIVE_CONNECTIONS.track(),
}),
protocol_mode,
metadata,
tool_filter: configured_tool_filter,
tool_timeout: configured_tool_timeout,
catalog_item_limit,
},
);
let tx_event = tx_event.clone();
let submit_id = startup_submit_id.clone();
let publication_gate = publication_gate.clone();
let startup = async move {
if let Some(mut startup_receiver) = startup_receiver
&& tokio::select! {
started = startup_receiver.wait_for(|started| *started) => started.is_err(),
() = cancel_token.cancelled() => true,
}
{
return (server_name, Err(StartupOutcomeError::Cancelled));
}
if !publication_gate.wait().await {
return (server_name, Err(StartupOutcomeError::Cancelled));
}
if let Some(tx_event) = tx_event.as_ref() {
let _ = emit_update(
submit_id.as_str(),
tx_event,
McpStartupUpdateEvent {
server: server_name.clone(),
status: McpStartupStatus::Starting,
},
)
.await;
}
let mut outcome = async_managed_client.client().await;
if cancel_token.is_cancelled() {
outcome = Err(StartupOutcomeError::Cancelled);
}
if let Some(tx_event) = tx_event.as_ref() {
let auth_state = match &outcome {
Err(error) if error.is_authentication_required() && !has_runtime_auth => {
match &configured_config.transport {
McpServerTransportConfig::StreamableHttp {
url,
bearer_token_env_var,
http_headers,
env_http_headers,
..
} => {
match determine_streamable_http_auth_status_from_credentials(
configured_config
.oauth_credential_name(&server_name)
.as_ref(),
url,
bearer_token_env_var.as_deref(),
http_headers.clone(),
env_http_headers.clone(),
store_mode,
keyring_backend_kind,
) {
Ok(auth_state) => auth_state,
Err(error) => {
warn!(
"failed to read stored auth status for MCP server `{server_name}`: {error:?}"
);
None
}
}
}
McpServerTransportConfig::Stdio { .. } => None,
}
}
Ok(_) | Err(_) => None,
};
if cancel_token.is_cancelled() {
outcome = Err(StartupOutcomeError::Cancelled);
}
let status = match &outcome {
Ok(_) => McpStartupStatus::Ready,
Err(StartupOutcomeError::Cancelled) => McpStartupStatus::Cancelled,
Err(error) => {
let reason = mcp_startup_failure_reason(auth_state, error);
let error_str = mcp_init_error_display(
server_name.as_str(),
Some(&configured_config),
error,
reason,
);
McpStartupStatus::Failed {
error: error_str,
reason,
}
}
};
let _ = emit_update(
submit_id.as_str(),
tx_event,
McpStartupUpdateEvent {
server: server_name.clone(),
status,
},
)
.await;
}
if cancel_token.is_cancelled() {
outcome = Err(StartupOutcomeError::Cancelled);
}
if matches!(&outcome, Err(StartupOutcomeError::Failed { .. })) {
async_managed_client.reconnect_failed_startup().await;
}
(server_name, outcome)
};
if defer_startup {
// Dormant servers must not hold the initial startup summary open.
tokio::spawn(startup);
} else {
join_set.spawn(startup);
}
}
let manager = Self {
servers,
event_stream_connection,
disabled_servers,
required_servers,
optional_startup_deadline: OnceLock::new(),
tool_plugin_context,
prefix_mcp_tool_names,
non_prefixed_mcp_tool_servers,
elicitation_requests: elicitation_requests.clone(),
trusted_access,
};
let summary_publication_gate = publication_gate;
tokio::spawn(async move {
let outcomes = join_set.join_all().await;
if let Some(tx_event) = tx_event {
if !summary_publication_gate.wait().await {
return;
}
let mut summary = McpStartupCompleteEvent {
ready: reused_ready,
..Default::default()
};
for server_name in &summary.ready {
let _ = emit_update(
startup_submit_id.as_str(),
&tx_event,
McpStartupUpdateEvent {
server: server_name.clone(),
status: McpStartupStatus::Ready,
},
)
.await;
}
for (server_name, outcome) in outcomes {
match outcome {
Ok(_) => summary.ready.push(server_name),
Err(StartupOutcomeError::Cancelled) => summary.cancelled.push(server_name),
Err(StartupOutcomeError::Failed { error, .. }) => {
summary.failed.push(McpStartupFailure {
server: server_name,
error,
})
}
}
}
let _ = tx_event
.send(Event {
id: startup_submit_id,
msg: EventMsg::McpStartupComplete(summary),
})
.await;
}
});
manager
}
pub fn empty(prefix_mcp_tool_names: bool) -> Self {
Self {
servers: HashMap::new(),
event_stream_connection: None,
disabled_servers: Vec::new(),
required_servers: Vec::new(),
optional_startup_deadline: OnceLock::new(),
tool_plugin_context: Arc::new(ToolPluginContext::default()),
prefix_mcp_tool_names,
non_prefixed_mcp_tool_servers: Vec::new(),
elicitation_requests: ElicitationRequestManager::default(),
trusted_access: None,
}
}
pub fn has_servers(&self) -> bool {
!self.servers.is_empty()
}
pub(crate) fn contains_server(&self, server_name: &str) -> bool {
self.servers.contains_key(server_name)
}
pub(crate) async fn authentication_failed_servers(&self) -> Vec<String> {
let mut failed_servers = Vec::new();
for (server_name, view) in &self.servers {
if view
.connection
.client
.startup_complete
.load(Ordering::Acquire)
&& let Err(error) = view.connection.client().await
&& error.is_authentication_required()
{
failed_servers.push(server_name.clone());
}
}
failed_servers
}
pub(crate) async fn updated_oauth_credentials_after_auth_failure(
&self,
config: &crate::McpConfig,
) -> Vec<String> {
let mut candidates = Vec::new();
for server_name in self.authentication_failed_servers().await {
if let Some(view) = self.servers.get(&server_name)
&& let Some(identity) = view.connection.identity.as_ref()
&& let Some(server) = config.mcp_server_catalog.server(&server_name)
{
candidates.push((server_name, identity.clone(), server.config().clone()));
}
}
if candidates.is_empty() {
return Vec::new();
}
match tokio::task::spawn_blocking(move || {
candidates
.into_iter()
.filter_map(|(server_name, identity, config)| {
identity
.oauth_credentials_changed(&server_name, &config)
.then_some(server_name)
})
.collect()
})
.await
{
Ok(recovered_servers) => recovered_servers,
Err(error) => {
warn!(%error, "failed to inspect stored MCP OAuth credentials");
Vec::new()
}
}
}
pub(crate) async fn wait_for_server_startup(&self, server_name: &str) -> bool {
let Some(view) = self.servers.get(server_name) else {
return false;
};
view.connection.client.ready_transport().is_some() || view.connection.client().await.is_ok()
}
/// Stop all MCP clients owned by this manager and terminate stdio server processes.
pub async fn shutdown(&self) {
let connections = self
.servers
.values()
.map(|view| Arc::clone(&view.connection))
.collect::<Vec<_>>();
// Keep cleanup alive if an interrupt cancels the refresh that requested it.
let shutdown_task = tokio::spawn(async move {
for connection in connections {
connection.shutdown().await;
}
});
if let Err(error) = shutdown_task.await {
warn!("MCP client shutdown task failed: {error}");
}
}
pub(crate) fn cancel_startup(&self) {
for view in self.servers.values() {
view.connection.cancel_startup();
}
}
pub fn plugin_id_for_mcp_server_name(&self, server_name: &str) -> Option<&str> {
self.tool_plugin_context
.plugin_id_for_mcp_server_name(server_name)
}
pub fn is_selected_plugin_mcp_server(&self, server_name: &str) -> bool {
self.tool_plugin_context
.is_selected_plugin_mcp_server(server_name)
}
pub async fn wait_for_server_ready(&self, server_name: &str, timeout: Duration) -> bool {
let Some(view) = self.servers.get(server_name) else {
return false;
};
match tokio::time::timeout(timeout, view.connection.client()).await {
Ok(Ok(_)) => true,
Ok(Err(_)) | Err(_) => false,
}
}
/// Invoke the tool indicated by the (server, tool) pair.
#[allow(clippy::too_many_arguments)]
pub async fn call_tool(
&self,
server: &str,
tool: &str,
environment_id: Option<&str>,
arguments: Option<serde_json::Value>,
mut meta: Option<serde_json::Value>,
requested_timeout: Option<Duration>,
wait_for_server: bool,
) -> Result<CallToolResult> {
let view = self
.servers
.get(server)
.ok_or_else(|| anyhow!("unknown MCP server '{server}'"))?;
if let Some(environment_id) = environment_id
&& view.metadata.environment_id != environment_id
{
bail!(
"MCP server `{server}` is running in environment `{}`, expected `{environment_id}`",
view.metadata.environment_id
);
}
if !view.tool_filter.allows(tool) {
return Err(anyhow!(
"tool '{tool}' is disabled for MCP server '{server}'"
));
}
let client = if wait_for_server {
view.connection
.client()
.await
.context("failed to get client")?
} else {
let client = view
.connection
.client
.ready_client()
.ok_or_else(|| anyhow!("MCP server '{server}' is not connected"))?;
if client.client.is_closed().await {
bail!("MCP server '{server}' is not connected");
}
client
};
let effective_timeout = match (view.tool_timeout, requested_timeout) {
(Some(server_timeout), Some(requested_timeout)) => {
Some(server_timeout.min(requested_timeout))
}
(server_timeout, requested_timeout) => server_timeout.or(requested_timeout),
};
// Direct callers cannot supply host-owned entitlement metadata, even for unlisted tools.
if let Some(serde_json::Value::Object(meta)) = meta.as_mut() {
meta.remove(ENTITLEMENT_CONTEXT_KEY);
}
let result: rmcp::model::CallToolResult = client
.client
.call_tool(tool.to_string(), arguments, meta, effective_timeout)
.await
.with_context(|| format!("tool call failed for `{server}/{tool}`"))?;
Ok(call_tool_result_from_rmcp(result))
}
/// Capabilities belong to the initialized connection, never a shared tool cache.
pub(crate) fn list_available_server_capabilities(&self) -> HashMap<String, serde_json::Value> {
self.servers
.iter()
.filter_map(|(name, view)| {
view.connection
.client
.server_capabilities
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.clone()
.map(|capabilities| (name.clone(), capabilities))
})
.collect()
}
/// Returns presentation metadata from the current connection.
/// Codex Apps metadata may come from its existing cache; regular MCP server information is
/// connection-specific, so pending regular clients are awaited.
pub(crate) async fn list_available_server_infos(&self) -> HashMap<String, McpServerInfo> {
let mut server_infos = HashMap::new();
for (server_name, view) in &self.servers {
let client = &view.connection.client;
if !client.startup_complete.load(Ordering::Acquire)
&& let Some(server_info) = client.cached_server_info.clone()
{
server_infos.insert(server_name.clone(), server_info);
continue;
}
match view.connection.client().await {
Ok(managed_client) => {
server_infos.insert(server_name.clone(), managed_client.server_info);
}
Err(_) => {
if let Some(server_info) = client.cached_server_info.clone() {
server_infos.insert(server_name.clone(), server_info);
}
}
}
}
server_infos
}
}
#[cfg(test)]
#[path = "connection_manager_tests.rs"]
pub(crate) mod tests;
|