mirror of
https://github.com/openai/codex.git
synced 2026-11-23 07:17:11 +00:00
Preserve MCP authentication challenges on tool calls (#42552)
## Why When silent OAuth refresh cannot recover a rejected MCP tool call, the caller needs the server's authentication challenge to initiate interactive login. The rejected call must not be replayed automatically after refresh has failed. ## What changed - Convert `401 Unauthorized` responses with `WWW-Authenticate` headers into an MCP tool error containing `mcp/www_authenticate` metadata. - Preserve multiple authentication challenges by combining all `WWW-Authenticate` header values. - Keep successful silent refresh behavior while avoiding replay when refresh fails. ## Testing Added coverage for successful and failed refresh, multiple authentication headers, propagation through agent tool-call events, and no-replay behavior. GitOrigin-RevId: c560cff6286d7ab09b1f5c6cf3618d4650cde47d
This commit is contained in:
1 parent
6d7f6dcd22
commit
0650d6d1ca
5 files changed
+365
-11
No files matched your search
@@ -275,12 +275,19 @@ impl StreamableHttpClient for StreamableHttpClientAdapter {
|
||||
StreamableHttpClientAdapterError::SessionExpired404,
|
||||
));
|
||||
}
|
||||
if response.status == StatusCode::UNAUTHORIZED.as_u16()
|
||||
&& let Some(header) = response_header(&response.headers, WWW_AUTHENTICATE)
|
||||
{
|
||||
return Err(StreamableHttpError::AuthRequired(AuthRequiredError::new(
|
||||
header,
|
||||
)));
|
||||
if response.status == StatusCode::UNAUTHORIZED.as_u16() {
|
||||
let challenges = response
|
||||
.headers
|
||||
.iter()
|
||||
.filter(|header| header.name.eq_ignore_ascii_case(WWW_AUTHENTICATE.as_str()))
|
||||
.map(|header| header.value.as_str())
|
||||
.collect::<Vec<_>>();
|
||||
if !challenges.is_empty() {
|
||||
// RFC 9110 allows combining these list-based fields; keep challenges after the first.
|
||||
return Err(StreamableHttpError::AuthRequired(AuthRequiredError::new(
|
||||
challenges.join(", "),
|
||||
)));
|
||||
}
|
||||
}
|
||||
if response.status == StatusCode::FORBIDDEN.as_u16()
|
||||
&& let Some(challenge) = insufficient_scope_challenge(&response.headers)
|
||||
|
||||
@@ -29,6 +29,7 @@ use rmcp::model::CallToolRequestParams;
|
||||
use rmcp::model::CallToolResult;
|
||||
use rmcp::model::ClientNotification;
|
||||
use rmcp::model::ClientRequest;
|
||||
use rmcp::model::ContentBlock;
|
||||
use rmcp::model::CustomNotification;
|
||||
use rmcp::model::CustomRequest;
|
||||
use rmcp::model::ElicitRequestParams;
|
||||
@@ -39,6 +40,7 @@ use rmcp::model::InitializeRequestParams;
|
||||
use rmcp::model::ListResourceTemplatesResult;
|
||||
use rmcp::model::ListResourcesResult;
|
||||
use rmcp::model::ListToolsResult;
|
||||
use rmcp::model::MetaObject;
|
||||
use rmcp::model::PaginatedRequestParams;
|
||||
use rmcp::model::ProtocolVersion;
|
||||
use rmcp::model::ReadResourceRequestParams;
|
||||
@@ -54,6 +56,7 @@ use rmcp::service::ClientServiceExt;
|
||||
use rmcp::service::RequestHandle;
|
||||
use rmcp::service::RoleClient;
|
||||
use rmcp::service::RunningService;
|
||||
use rmcp::service::ServiceError;
|
||||
use rmcp::transport::AuthorizationManager;
|
||||
use rmcp::transport::StreamableHttpClientTransport;
|
||||
use rmcp::transport::auth::AuthClient;
|
||||
@@ -811,7 +814,7 @@ impl RmcpClient {
|
||||
let mut rmcp_params = CallToolRequestParams::new(name);
|
||||
rmcp_params.arguments = arguments;
|
||||
let requested_modern = self.protocol_mode == McpProtocolMode::V20260728;
|
||||
let result = self
|
||||
match self
|
||||
.run_service_operation("tools/call", timeout, move |service| {
|
||||
let mut rmcp_params = rmcp_params.clone();
|
||||
let meta = meta.clone();
|
||||
@@ -844,9 +847,36 @@ impl RmcpClient {
|
||||
}
|
||||
.boxed()
|
||||
})
|
||||
.await?;
|
||||
self.persist_oauth_tokens().await;
|
||||
Ok(result)
|
||||
.await
|
||||
{
|
||||
Ok(result) => {
|
||||
self.persist_oauth_tokens().await;
|
||||
Ok(result)
|
||||
}
|
||||
Err(error) => {
|
||||
let Some(ClientOperationError::Service(ServiceError::TransportSend(transport))) =
|
||||
error.downcast_ref()
|
||||
else {
|
||||
return Err(error);
|
||||
};
|
||||
let Some(StreamableHttpError::AuthRequired(challenge)) =
|
||||
transport
|
||||
.error
|
||||
.downcast_ref::<StreamableHttpError<StreamableHttpClientAdapterError>>()
|
||||
else {
|
||||
return Err(error);
|
||||
};
|
||||
// The transport has already handled automatic refresh. Preserve the challenge
|
||||
// for interactive login without replaying the rejected tool call.
|
||||
let mut result =
|
||||
CallToolResult::error(vec![ContentBlock::text("Authentication required")]);
|
||||
result.meta = Some(MetaObject::from(serde_json::Map::from_iter([(
|
||||
"mcp/www_authenticate".to_string(),
|
||||
serde_json::json!([challenge.www_authenticate_header]),
|
||||
)])));
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_custom_notification(
|
||||
|
||||
Reference in new issue
Block a user