From 1917211fe89bc4cabe9ad33e3affef0be807c6e3 Mon Sep 17 00:00:00 2001 From: Eric Ning Date: Wed, 3 Jun 2026 11:47:55 -0700 Subject: [PATCH] Use installed connectors for plugin read auth state --- codex-rs/app-server/README.md | 2 +- .../src/request_processors/plugins.rs | 80 ++++++------------- .../app-server/tests/suite/v2/plugin_read.rs | 57 +++++++++++-- codex-rs/chatgpt/src/connectors.rs | 29 +++++++ 4 files changed, 108 insertions(+), 60 deletions(-) diff --git a/codex-rs/app-server/README.md b/codex-rs/app-server/README.md index dc02ff8c04..b0b1adb8b9 100644 --- a/codex-rs/app-server/README.md +++ b/codex-rs/app-server/README.md @@ -203,7 +203,7 @@ Example with notification opt-out: - `marketplace/upgrade` — upgrade all configured Git plugin marketplaces, or one named marketplace when `marketplaceName` is provided. Returns selected marketplace names, upgraded roots, and per-marketplace errors. - `plugin/list` — list discovered plugin marketplaces and plugin state, including effective marketplace install/auth policy metadata, plugin `availability` (`AVAILABLE` by default or `DISABLED_BY_ADMIN` for remote plugins blocked upstream), fail-open `marketplaceLoadErrors` entries for marketplace files that could not be parsed or loaded, and best-effort `featuredPluginIds` for the official curated marketplace. `interface.category` uses the marketplace category when present; otherwise it falls back to the plugin manifest category (**under development; do not call from production clients yet**). - `plugin/installed` — list installed plugin rows plus any explicitly requested local install-suggestion plugin names, without fetching the broader remote catalog. Mention surfaces can use this narrower view when they need plugin mention payloads rather than plugin-page discovery data (**under development; do not call from production clients yet**). -- `plugin/read` — read one plugin by `marketplacePath` plus `pluginName`, returning marketplace info, a list-style `summary`, manifest descriptions/interface metadata, and bundled skills/hooks/apps/MCP server names. Returned plugin skills include their current `enabled` state after local config filtering; bundled hooks are returned as lightweight declaration summaries keyed for correlation with `hooks/list`. Plugin app summaries also include `needsAuth` when the server can determine connector accessibility (**under development; do not call from production clients yet**). +- `plugin/read` — read one plugin by `marketplacePath` plus `pluginName`, returning marketplace info, a list-style `summary`, manifest descriptions/interface metadata, and bundled skills/hooks/apps/MCP server names. Returned plugin skills include their current `enabled` state after local config filtering; bundled hooks are returned as lightweight declaration summaries keyed for correlation with `hooks/list`. Plugin app summaries also include `needsAuth` when the server can determine whether the connector is installed (**under development; do not call from production clients yet**). - `plugin/skill/read` — read remote plugin skill markdown on demand by `remoteMarketplaceName`, `remotePluginId`, and `skillName`. This lets clients preview uninstalled remote plugin skills without downloading the plugin bundle. - `skills/changed` — notification emitted when watched local skill files change. - `app/list` — list available apps. diff --git a/codex-rs/app-server/src/request_processors/plugins.rs b/codex-rs/app-server/src/request_processors/plugins.rs index 2335694856..005712b4bd 100644 --- a/codex-rs/app-server/src/request_processors/plugins.rs +++ b/codex-rs/app-server/src/request_processors/plugins.rs @@ -1011,13 +1011,7 @@ impl PluginRequestProcessor { } None => None, }; - let environment_manager = self.thread_manager.environment_manager(); - let app_summaries = load_plugin_app_summaries( - &config, - &outcome.plugin.apps, - Arc::clone(&environment_manager), - ) - .await; + let app_summaries = load_plugin_app_summaries(&config, &outcome.plugin.apps).await; let visible_skills = outcome .plugin .skills @@ -1092,13 +1086,7 @@ impl PluginRequestProcessor { .cloned() .map(codex_plugin::AppConnectorId) .collect::>(); - let environment_manager = self.thread_manager.environment_manager(); - let app_summaries = load_plugin_app_summaries( - &config, - &plugin_apps, - Arc::clone(&environment_manager), - ) - .await; + let app_summaries = load_plugin_app_summaries(&config, &plugin_apps).await; remote_plugin_detail_to_info(remote_detail, app_summaries) } }; @@ -1832,58 +1820,42 @@ impl PluginRequestProcessor { async fn load_plugin_app_summaries( config: &Config, plugin_apps: &[codex_plugin::AppConnectorId], - environment_manager: Arc, ) -> Vec { if plugin_apps.is_empty() { return Vec::new(); } - let connectors = - match connectors::list_all_connectors_with_options(config, /*force_refetch*/ false).await { - Ok(connectors) => connectors, - Err(err) => { - warn!("failed to load app metadata for plugin/read: {err:#}"); - connectors::list_cached_all_connectors(config) - .await - .unwrap_or_default() - } - }; + let (connectors_result, installed_connector_ids_result) = tokio::join!( + connectors::list_all_connectors_with_options(config, /*force_refetch*/ false), + connectors::list_installed_connector_ids(config), + ); + let connectors = match connectors_result { + Ok(connectors) => connectors, + Err(err) => { + warn!("failed to load app metadata for plugin/read: {err:#}"); + connectors::list_cached_all_connectors(config) + .await + .unwrap_or_default() + } + }; let plugin_connectors = connectors::connectors_for_plugin_apps(connectors, plugin_apps); - let accessible_connectors = - match connectors::list_accessible_connectors_from_mcp_tools_with_environment_manager( - config, - /*force_refetch*/ false, - environment_manager, - ) - .await - { - Ok(status) if status.codex_apps_ready => status.connectors, - Ok(_) => { - return plugin_connectors - .into_iter() - .map(AppSummary::from) - .collect(); - } - Err(err) => { - warn!("failed to load app auth state for plugin/read: {err:#}"); - return plugin_connectors - .into_iter() - .map(AppSummary::from) - .collect(); - } - }; - - let accessible_ids = accessible_connectors - .iter() - .map(|connector| connector.id.as_str()) - .collect::>(); + let installed_connector_ids = match installed_connector_ids_result { + Ok(installed_connector_ids) => installed_connector_ids, + Err(err) => { + warn!("failed to load installed apps for plugin/read: {err:#}"); + return plugin_connectors + .into_iter() + .map(AppSummary::from) + .collect(); + } + }; plugin_connectors .into_iter() .map(|connector| { - let needs_auth = !accessible_ids.contains(connector.id.as_str()); + let needs_auth = !installed_connector_ids.contains(connector.id.as_str()); AppSummary { id: connector.id, name: connector.name, diff --git a/codex-rs/app-server/tests/suite/v2/plugin_read.rs b/codex-rs/app-server/tests/suite/v2/plugin_read.rs index 1f1c217ee5..57ebc9017f 100644 --- a/codex-rs/app-server/tests/suite/v2/plugin_read.rs +++ b/codex-rs/app-server/tests/suite/v2/plugin_read.rs @@ -1433,8 +1433,9 @@ async fn plugin_read_returns_app_needs_auth() -> Result<()> { plugin_display_names: Vec::new(), }, ]; - let tools = vec![connector_tool("beta", "Beta App")?]; - let (server_url, server_handle) = start_apps_server(connectors, tools).await?; + let tools = vec![connector_tool("alpha", "Alpha App")?]; + let (server_url, server_handle) = + start_apps_server(connectors, vec!["beta".to_string()], tools).await?; let codex_home = TempDir::new()?; write_connectors_config(codex_home.path(), &server_url)?; @@ -1758,7 +1759,8 @@ plugins = true #[derive(Clone)] struct AppsServerState { - response: Arc>, + directory_response: Arc>, + installed_response: Arc>, } #[derive(Clone)] @@ -1794,12 +1796,19 @@ impl ServerHandler for PluginReadMcpServer { async fn start_apps_server( connectors: Vec, + installed_connector_ids: Vec, tools: Vec, ) -> Result<(String, JoinHandle<()>)> { let state = Arc::new(AppsServerState { - response: Arc::new(StdMutex::new( + directory_response: Arc::new(StdMutex::new( json!({ "apps": connectors, "next_token": null }), )), + installed_response: Arc::new(StdMutex::new(json!({ + "connectors": installed_connector_ids + .into_iter() + .map(|id| json!({ "id": id })) + .collect::>() + }))), }); let tools = Arc::new(StdMutex::new(tools)); @@ -1823,6 +1832,10 @@ async fn start_apps_server( "/connectors/directory/list_workspace", get(list_directory_connectors), ) + .route( + "/connectors/directory/list_installed", + get(list_installed_connectors), + ) .with_state(state) .nest_service("/api/codex/apps", mcp_service); @@ -1856,7 +1869,41 @@ async fn list_directory_connectors( Err(StatusCode::BAD_REQUEST) } else { let response = state - .response + .directory_response + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .clone(); + Ok(Json(response)) + } +} + +async fn list_installed_connectors( + State(state): State>, + headers: HeaderMap, + uri: Uri, +) -> Result { + let bearer_ok = headers + .get(AUTHORIZATION) + .and_then(|value| value.to_str().ok()) + .is_some_and(|value| value == "Bearer chatgpt-token"); + let account_ok = headers + .get("chatgpt-account-id") + .and_then(|value| value.to_str().ok()) + .is_some_and(|value| value == "account-123"); + let query_ok = uri.query().is_some_and(|query| { + query.split('&').any(|pair| pair == "include_actions=false") + && query + .split('&') + .any(|pair| pair == "include_admin_sync=false") + }); + + if !bearer_ok || !account_ok { + Err(StatusCode::UNAUTHORIZED) + } else if !query_ok { + Err(StatusCode::BAD_REQUEST) + } else { + let response = state + .installed_response .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .clone(); diff --git a/codex-rs/chatgpt/src/connectors.rs b/codex-rs/chatgpt/src/connectors.rs index 63b34e7355..6fb368b426 100644 --- a/codex-rs/chatgpt/src/connectors.rs +++ b/codex-rs/chatgpt/src/connectors.rs @@ -26,6 +26,16 @@ use codex_plugin::AppConnectorId; const DIRECTORY_CONNECTORS_TIMEOUT: Duration = Duration::from_secs(60); +#[derive(serde::Deserialize)] +struct InstalledConnectorsResponse { + connectors: Vec, +} + +#[derive(serde::Deserialize)] +struct InstalledConnector { + id: String, +} + async fn apps_enabled(config: &Config) -> bool { let auth_manager = AuthManager::shared_from_config(config, /*enable_codex_api_key_env*/ false).await; @@ -128,6 +138,25 @@ pub async fn list_all_connectors_with_options( )) } +pub async fn list_installed_connector_ids(config: &Config) -> anyhow::Result> { + if !apps_enabled(config).await { + return Ok(HashSet::new()); + } + + let response = chatgpt_get_request_with_timeout::( + config, + "/connectors/directory/list_installed?include_actions=false&include_admin_sync=false" + .to_string(), + Some(DIRECTORY_CONNECTORS_TIMEOUT), + ) + .await?; + Ok(response + .connectors + .into_iter() + .map(|connector| connector.id) + .collect()) +} + fn connector_directory_cache_context( config: &Config, auth: &CodexAuth,