use scopes_supported for OAuth when present on MCP servers (#14419)

Fixes [#8889](https://github.com/openai/codex/issues/8889).

## Summary
- Discover and use advertised MCP OAuth `scopes_supported` when no
explicit or configured scopes are present.
- Apply the same scope precedence across `mcp add`, `mcp login`, skill
dependency auto-login, and app-server MCP OAuth login.
- Keep discovered scopes ephemeral and non-persistent.
- Retry once without scopes for CLI and skill auto-login flows if the
OAuth provider rejects discovered scopes.

## Motivation
Some MCP servers advertise the scopes they expect clients to request
during OAuth, but Codex was ignoring that metadata and typically
starting OAuth with no scopes unless the user manually passed `--scopes`
or configured `server.scopes`.

That made compliant MCP servers harder to use out of the box and is the
behavior described in
[#8889](https://github.com/openai/codex/issues/8889).

This change also brings our behavior in line with the MCP authorization
spec's scope selection guidance:

https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization#scope-selection-strategy

## Behavior
Scope selection now follows this order everywhere:
1. Explicit request scopes / CLI `--scopes`
2. Configured `server.scopes`
3. Discovered `scopes_supported`
4. Legacy empty-scope behavior

Compatibility notes:
- Existing working setups keep the same behavior because explicit and
configured scopes still win.
- Discovered scopes are never written back into config or token storage.
- If discovery is missing, malformed, or empty, behavior falls back to
the previous empty-scope path.
- App-server login gets the same precedence rules, but does not add a
transparent retry path in this change.

## Implementation
- Extend streamable HTTP OAuth discovery to parse and normalize
`scopes_supported`.
- Add a shared MCP scope resolver in `core` so all login entrypoints use
the same precedence rules.
- Preserve provider callback errors from the OAuth flow so CLI/skill
flows can safely distinguish provider rejections from other failures.
- Reuse discovered scopes from the existing OAuth support check where
possible instead of persisting new config.
This commit is contained in:
jgershen-oai
2026-03-12 11:57:06 -07:00
committed by GitHub
parent fa26597689
commit 3e96c867fe
7 changed files with 522 additions and 38 deletions

View File

@@ -21,6 +21,11 @@ const DISCOVERY_TIMEOUT: Duration = Duration::from_secs(5);
const OAUTH_DISCOVERY_HEADER: &str = "MCP-Protocol-Version";
const OAUTH_DISCOVERY_VERSION: &str = "2024-11-05";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StreamableHttpOAuthDiscovery {
pub scopes_supported: Option<Vec<String>>,
}
/// Determine the authentication status for a streamable HTTP MCP server.
pub async fn determine_streamable_http_auth_status(
server_name: &str,
@@ -43,9 +48,9 @@ pub async fn determine_streamable_http_auth_status(
return Ok(McpAuthStatus::OAuth);
}
match supports_oauth_login_with_headers(url, &default_headers).await {
Ok(true) => Ok(McpAuthStatus::NotLoggedIn),
Ok(false) => Ok(McpAuthStatus::Unsupported),
match discover_streamable_http_oauth_with_headers(url, &default_headers).await {
Ok(Some(_)) => Ok(McpAuthStatus::NotLoggedIn),
Ok(None) => Ok(McpAuthStatus::Unsupported),
Err(error) => {
debug!(
"failed to detect OAuth support for MCP server `{server_name}` at {url}: {error:?}"
@@ -57,10 +62,24 @@ pub async fn determine_streamable_http_auth_status(
/// Attempt to determine whether a streamable HTTP MCP server advertises OAuth login.
pub async fn supports_oauth_login(url: &str) -> Result<bool> {
supports_oauth_login_with_headers(url, &HeaderMap::new()).await
Ok(discover_streamable_http_oauth(url, None, None)
.await?
.is_some())
}
async fn supports_oauth_login_with_headers(url: &str, default_headers: &HeaderMap) -> Result<bool> {
pub async fn discover_streamable_http_oauth(
url: &str,
http_headers: Option<HashMap<String, String>>,
env_http_headers: Option<HashMap<String, String>>,
) -> Result<Option<StreamableHttpOAuthDiscovery>> {
let default_headers = build_default_headers(http_headers, env_http_headers)?;
discover_streamable_http_oauth_with_headers(url, &default_headers).await
}
async fn discover_streamable_http_oauth_with_headers(
url: &str,
default_headers: &HeaderMap,
) -> Result<Option<StreamableHttpOAuthDiscovery>> {
let base_url = Url::parse(url)?;
// Use no_proxy to avoid a bug in the system-configuration crate that
@@ -99,7 +118,9 @@ async fn supports_oauth_login_with_headers(url: &str, default_headers: &HeaderMa
};
if metadata.authorization_endpoint.is_some() && metadata.token_endpoint.is_some() {
return Ok(true);
return Ok(Some(StreamableHttpOAuthDiscovery {
scopes_supported: normalize_scopes(metadata.scopes_supported),
}));
}
}
@@ -107,7 +128,7 @@ async fn supports_oauth_login_with_headers(url: &str, default_headers: &HeaderMa
debug!("OAuth discovery requests failed for {url}: {err:?}");
}
Ok(false)
Ok(None)
}
#[derive(Debug, Deserialize)]
@@ -116,6 +137,30 @@ struct OAuthDiscoveryMetadata {
authorization_endpoint: Option<String>,
#[serde(default)]
token_endpoint: Option<String>,
#[serde(default)]
scopes_supported: Option<Vec<String>>,
}
fn normalize_scopes(scopes_supported: Option<Vec<String>>) -> Option<Vec<String>> {
let scopes_supported = scopes_supported?;
let mut normalized = Vec::new();
for scope in scopes_supported {
let scope = scope.trim();
if scope.is_empty() {
continue;
}
let scope = scope.to_string();
if !normalized.contains(&scope) {
normalized.push(scope);
}
}
if normalized.is_empty() {
None
} else {
Some(normalized)
}
}
/// Implements RFC 8414 section 3.1 for discovering well-known oauth endpoints.
@@ -147,10 +192,50 @@ fn discovery_paths(base_path: &str) -> Vec<String> {
#[cfg(test)]
mod tests {
use super::*;
use axum::Json;
use axum::Router;
use axum::routing::get;
use pretty_assertions::assert_eq;
use serial_test::serial;
use std::collections::HashMap;
use std::ffi::OsString;
use tokio::task::JoinHandle;
struct TestServer {
url: String,
handle: JoinHandle<()>,
}
impl Drop for TestServer {
fn drop(&mut self) {
self.handle.abort();
}
}
async fn spawn_oauth_discovery_server(metadata: serde_json::Value) -> TestServer {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
.await
.expect("listener should bind");
let address = listener.local_addr().expect("listener should have address");
let app = Router::new().route(
"/.well-known/oauth-authorization-server/mcp",
get({
let metadata = metadata.clone();
move || {
let metadata = metadata.clone();
async move { Json(metadata) }
}
}),
);
let handle = tokio::spawn(async move {
axum::serve(listener, app).await.expect("server should run");
});
TestServer {
url: format!("http://{address}/mcp"),
handle,
}
}
struct EnvVarGuard {
key: String,
@@ -223,4 +308,56 @@ mod tests {
assert_eq!(status, McpAuthStatus::BearerToken);
}
#[tokio::test]
async fn discover_streamable_http_oauth_returns_normalized_scopes() {
let server = spawn_oauth_discovery_server(serde_json::json!({
"authorization_endpoint": "https://example.com/authorize",
"token_endpoint": "https://example.com/token",
"scopes_supported": ["profile", " email ", "profile", "", " "],
}))
.await;
let discovery = discover_streamable_http_oauth(&server.url, None, None)
.await
.expect("discovery should succeed")
.expect("oauth support should be detected");
assert_eq!(
discovery.scopes_supported,
Some(vec!["profile".to_string(), "email".to_string()])
);
}
#[tokio::test]
async fn discover_streamable_http_oauth_ignores_empty_scopes() {
let server = spawn_oauth_discovery_server(serde_json::json!({
"authorization_endpoint": "https://example.com/authorize",
"token_endpoint": "https://example.com/token",
"scopes_supported": ["", " "],
}))
.await;
let discovery = discover_streamable_http_oauth(&server.url, None, None)
.await
.expect("discovery should succeed")
.expect("oauth support should be detected");
assert_eq!(discovery.scopes_supported, None);
}
#[tokio::test]
async fn supports_oauth_login_does_not_require_scopes_supported() {
let server = spawn_oauth_discovery_server(serde_json::json!({
"authorization_endpoint": "https://example.com/authorize",
"token_endpoint": "https://example.com/token",
}))
.await;
let supported = supports_oauth_login(&server.url)
.await
.expect("support check should succeed");
assert!(supported);
}
}

View File

@@ -6,7 +6,9 @@ mod program_resolver;
mod rmcp_client;
mod utils;
pub use auth_status::StreamableHttpOAuthDiscovery;
pub use auth_status::determine_streamable_http_auth_status;
pub use auth_status::discover_streamable_http_oauth;
pub use auth_status::supports_oauth_login;
pub use codex_protocol::protocol::McpAuthStatus;
pub use oauth::OAuthCredentialsStoreMode;
@@ -15,6 +17,7 @@ pub use oauth::WrappedOAuthTokenResponse;
pub use oauth::delete_oauth_tokens;
pub(crate) use oauth::load_oauth_tokens;
pub use oauth::save_oauth_tokens;
pub use perform_oauth_login::OAuthProviderError;
pub use perform_oauth_login::OauthLoginHandle;
pub use perform_oauth_login::perform_oauth_login;
pub use perform_oauth_login::perform_oauth_login_return_url;

View File

@@ -39,6 +39,36 @@ impl Drop for CallbackServerGuard {
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OAuthProviderError {
error: Option<String>,
error_description: Option<String>,
}
impl OAuthProviderError {
pub fn new(error: Option<String>, error_description: Option<String>) -> Self {
Self {
error,
error_description,
}
}
}
impl std::fmt::Display for OAuthProviderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match (self.error.as_deref(), self.error_description.as_deref()) {
(Some(error), Some(error_description)) => {
write!(f, "OAuth provider returned `{error}`: {error_description}")
}
(Some(error), None) => write!(f, "OAuth provider returned `{error}`"),
(None, Some(error_description)) => write!(f, "OAuth error: {error_description}"),
(None, None) => write!(f, "OAuth provider returned an error"),
}
}
}
impl std::error::Error for OAuthProviderError {}
#[allow(clippy::too_many_arguments)]
pub async fn perform_oauth_login(
server_name: &str,
@@ -111,7 +141,7 @@ pub async fn perform_oauth_login_return_url(
fn spawn_callback_server(
server: Arc<Server>,
tx: oneshot::Sender<(String, String)>,
tx: oneshot::Sender<CallbackResult>,
expected_callback_path: String,
) {
tokio::task::spawn_blocking(move || {
@@ -125,17 +155,22 @@ fn spawn_callback_server(
if let Err(err) = request.respond(response) {
eprintln!("Failed to respond to OAuth callback: {err}");
}
if let Err(err) = tx.send((code, state)) {
if let Err(err) =
tx.send(CallbackResult::Success(OauthCallbackResult { code, state }))
{
eprintln!("Failed to send OAuth callback: {err:?}");
}
break;
}
CallbackOutcome::Error(description) => {
let response = Response::from_string(format!("OAuth error: {description}"))
.with_status_code(400);
CallbackOutcome::Error(error) => {
let response = Response::from_string(error.to_string()).with_status_code(400);
if let Err(err) = request.respond(response) {
eprintln!("Failed to respond to OAuth callback: {err}");
}
if let Err(err) = tx.send(CallbackResult::Error(error)) {
eprintln!("Failed to send OAuth callback error: {err:?}");
}
break;
}
CallbackOutcome::Invalid => {
let response =
@@ -149,14 +184,22 @@ fn spawn_callback_server(
});
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct OauthCallbackResult {
code: String,
state: String,
}
#[derive(Debug)]
enum CallbackResult {
Success(OauthCallbackResult),
Error(OAuthProviderError),
}
#[derive(Debug, PartialEq, Eq)]
enum CallbackOutcome {
Success(OauthCallbackResult),
Error(String),
Error(OAuthProviderError),
Invalid,
}
@@ -170,6 +213,7 @@ fn parse_oauth_callback(path: &str, expected_callback_path: &str) -> CallbackOut
let mut code = None;
let mut state = None;
let mut error = None;
let mut error_description = None;
for pair in query.split('&') {
@@ -183,6 +227,7 @@ fn parse_oauth_callback(path: &str, expected_callback_path: &str) -> CallbackOut
match key {
"code" => code = Some(decoded),
"state" => state = Some(decoded),
"error" => error = Some(decoded),
"error_description" => error_description = Some(decoded),
_ => {}
}
@@ -192,8 +237,8 @@ fn parse_oauth_callback(path: &str, expected_callback_path: &str) -> CallbackOut
return CallbackOutcome::Success(OauthCallbackResult { code, state });
}
if let Some(description) = error_description {
return CallbackOutcome::Error(description);
if error.is_some() || error_description.is_some() {
return CallbackOutcome::Error(OAuthProviderError::new(error, error_description));
}
CallbackOutcome::Invalid
@@ -230,7 +275,7 @@ impl OauthLoginHandle {
struct OauthLoginFlow {
auth_url: String,
oauth_state: OAuthState,
rx: oneshot::Receiver<(String, String)>,
rx: oneshot::Receiver<CallbackResult>,
guard: CallbackServerGuard,
server_name: String,
server_url: String,
@@ -384,10 +429,17 @@ impl OauthLoginFlow {
}
let result = async {
let (code, csrf_state) = timeout(self.timeout, &mut self.rx)
let callback = timeout(self.timeout, &mut self.rx)
.await
.context("timed out waiting for OAuth callback")?
.context("OAuth callback was cancelled")?;
let OauthCallbackResult {
code,
state: csrf_state,
} = match callback {
CallbackResult::Success(callback) => callback,
CallbackResult::Error(error) => return Err(anyhow!(error)),
};
self.oauth_state
.handle_callback(&code, &csrf_state)
@@ -462,6 +514,7 @@ mod tests {
use pretty_assertions::assert_eq;
use super::CallbackOutcome;
use super::OAuthProviderError;
use super::append_query_param;
use super::callback_path_from_redirect_uri;
use super::parse_oauth_callback;
@@ -484,6 +537,22 @@ mod tests {
assert!(matches!(parsed, CallbackOutcome::Invalid));
}
#[test]
fn parse_oauth_callback_returns_provider_error() {
let parsed = parse_oauth_callback(
"/callback?error=invalid_scope&error_description=scope%20rejected",
"/callback",
);
assert_eq!(
parsed,
CallbackOutcome::Error(OAuthProviderError::new(
Some("invalid_scope".to_string()),
Some("scope rejected".to_string()),
))
);
}
#[test]
fn callback_path_comes_from_redirect_uri() {
let path = callback_path_from_redirect_uri("https://example.com/oauth/callback")