Resolve HTTP MCP bearer tokens in executor environments (#39926)

## Why

Executor-owned HTTP MCP servers need to read their bearer credentials from the
selected executor environment instead of the host process.

## What changed

- Preserve `bearer_token_env_var` for executor-owned HTTP MCP configurations and
  resolve it when the executor sends each request.
- Extend delegated HTTP headers with executor-local environment references while
  rejecting missing, empty, or protected credential variables.
- Keep transport-provided bearer authentication compatible with MCP redirect and
  OAuth handling without sending a placeholder authorization value.

## Testing

- Cover authenticated executor-owned MCP requests end to end.
- Cover delegated header resolution and rejection of protected variables.
- Cover parsing executor-owned bearer configuration and transport-provided bearer
  behavior.

GitOrigin-RevId: 442bf7382198ffb69eba797eb36d4c74faabda88
This commit is contained in:
jif authored and copyberry committed 2026-08-21 12:50:15 +00:00
1 parent ff0e95007c
commit 93c54bca38
21 files changed
+361 -57

No files matched your search

@@ -711,6 +711,7 @@ fn protocol_headers(headers: &HeaderMap) -> Vec<HttpHeader> {
Some(HttpHeader {
name: name.as_str().to_string(),
value: std::str::from_utf8(value.as_bytes()).ok()?.to_string(),
value_env_var: None,
})
})
.collect()
@@ -107,10 +107,12 @@ fn selects_bearer_challenge_from_a_later_www_authenticate_field_value() {
HttpHeader {
name: "www-authenticate".to_string(),
value: r#"Basic realm="example""#.to_string(),
value_env_var: None,
},
HttpHeader {
name: "WWW-Authenticate".to_string(),
value: r#"Bearer error="insufficient_scope", scope="files:read""#.to_string(),
value_env_var: None,
},
];
@@ -25,6 +25,7 @@ fn protocol_headers_preserve_utf8_values() {
vec![HttpHeader {
name: "x-plugin-name".to_string(),
value: "café".to_string(),
value_env_var: None,
}]
);
}
@@ -166,6 +166,7 @@ impl SameOriginRedirectHttpClient {
params.headers.push(HttpHeader {
name: "referer".to_string(),
value: referer.to_string(),
value_env_var: None,
});
}
@@ -38,6 +38,7 @@ fn headers<const N: usize>(headers: [(&str, &str); N]) -> Vec<HttpHeader> {
.map(|(name, value)| HttpHeader {
name: name.to_string(),
value: value.to_string(),
value_env_var: None,
})
.collect()
}
+1
View File
@@ -185,6 +185,7 @@ impl HttpHeadersClient {
value: std::str::from_utf8(value.as_bytes())
.map_err(|error| ExecServerError::HttpRequest(error.to_string()))?
.to_string(),
value_env_var: None,
});
}
if let Some(deadline) = deadline {
+1
View File
@@ -52,6 +52,7 @@ pub use rmcp_client::ElicitationResponse;
pub use rmcp_client::ListToolsWithConnectorIdResult;
pub use rmcp_client::RmcpClient;
pub use rmcp_client::SendElicitation;
pub use rmcp_client::StreamableHttpBearerToken;
pub use rmcp_client::ToolWithConnectorId;
pub use startup_error::is_authentication_required_error;
pub use stdio_server_launcher::ExecutorStdioServerLauncher;
@@ -169,6 +169,7 @@ impl OAuthHttpClientAdapter {
Ok(HttpHeader {
name: name.as_str().to_string(),
value: value.to_str().map_err(oauth_http_client_error)?.to_string(),
value_env_var: None,
})
})
.collect::<Result<Vec<_>, OAuthHttpClientError>>()?;
+18 -6
View File
@@ -130,6 +130,15 @@ enum ClientState {
Closed,
}
/// Bearer authentication applied directly or by the selected HTTP transport.
#[derive(Clone)]
pub enum StreamableHttpBearerToken {
/// A token already resolved in the current process.
Resolved(String),
/// The HTTP client attaches credentials when it sends each request.
ProvidedByHttpClient,
}
#[derive(Clone)]
enum TransportRecipe {
InProcess {
@@ -142,7 +151,7 @@ enum TransportRecipe {
StreamableHttp {
server_name: String,
url: String,
bearer_token: Option<String>,
bearer_token: Option<StreamableHttpBearerToken>,
http_headers: Option<HashMap<String, String>>,
env_http_headers: Option<HashMap<String, String>>,
store_mode: OAuthCredentialsStoreMode,
@@ -514,7 +523,7 @@ impl RmcpClient {
Self::new_streamable_http_client_with_protocol_mode_and_redirect_mode(
server_name,
url,
bearer_token,
bearer_token.map(StreamableHttpBearerToken::Resolved),
http_headers,
env_http_headers,
store_mode,
@@ -531,7 +540,7 @@ impl RmcpClient {
pub async fn new_streamable_http_client_with_protocol_mode_and_redirect_mode(
server_name: &str,
url: &str,
bearer_token: Option<String>,
bearer_token: Option<StreamableHttpBearerToken>,
http_headers: Option<HashMap<String, String>>,
env_http_headers: Option<HashMap<String, String>>,
store_mode: OAuthCredentialsStoreMode,
@@ -1012,7 +1021,10 @@ impl RmcpClient {
redirect_mode,
initialize_deadline,
} => {
let has_configured_headers = http_headers
let has_configured_headers = matches!(
bearer_token,
Some(StreamableHttpBearerToken::ProvidedByHttpClient)
) || http_headers
.as_ref()
.is_some_and(|headers| !headers.is_empty())
|| env_http_headers
@@ -1133,8 +1145,8 @@ impl RmcpClient {
} else {
let mut http_config =
StreamableHttpClientTransportConfig::with_uri(url.clone());
if let Some(bearer_token) = bearer_token.clone() {
http_config = http_config.auth_header(bearer_token);
if let Some(StreamableHttpBearerToken::Resolved(bearer_token)) = bearer_token {
http_config = http_config.auth_header(bearer_token.clone());
}
let transport = StreamableHttpClientTransport::with_client(