feat(rmcp): add authorization header support for the streamable http client (#390)

This allows the streamable http client to send an authorization header
when making requests.

Closes #387
This commit is contained in:
mjcarson 2025-08-29 05:58:56 -05:00 committed by GitHub
parent 357671cc5c
commit 6853143684
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 3 deletions

View file

@ -160,8 +160,18 @@ impl StreamableHttpClientTransport<reqwest::Client> {
reqwest::Client::default(),
StreamableHttpClientTransportConfig {
uri: uri.into(),
auth_header: None,
..Default::default()
},
)
}
/// Build this transport form a config
///
/// # Arguments
///
/// * `config` - The config to use with this transport
pub fn from_config(config: StreamableHttpClientTransportConfig) -> Self {
StreamableHttpClientTransport::with_client(reqwest::Client::default(), config)
}
}

View file

@ -277,7 +277,12 @@ impl<C: StreamableHttpClient> Worker for StreamableHttpClientWorker<C> {
let _ = responder.send(Ok(()));
let (message, session_id) = self
.client
.post_message(config.uri.clone(), initialize_request, None, None)
.post_message(
config.uri.clone(),
initialize_request,
None,
self.config.auth_header,
)
.await
.map_err(WorkerQuitReason::fatal_context("send initialize request"))?
.expect_initialized::<C::Error>()
@ -334,7 +339,7 @@ impl<C: StreamableHttpClient> Worker for StreamableHttpClientWorker<C> {
config.uri.clone(),
initialized_notification.message,
session_id.clone(),
None,
config.auth_header.clone(),
)
.await
.map_err(WorkerQuitReason::fatal_context(
@ -421,7 +426,12 @@ impl<C: StreamableHttpClient> Worker for StreamableHttpClientWorker<C> {
let WorkerSendRequest { message, responder } = send_request;
let response = self
.client
.post_message(config.uri.clone(), message, session_id.clone(), None)
.post_message(
config.uri.clone(),
message,
session_id.clone(),
config.auth_header.clone(),
)
.await;
let send_result = match response {
Err(e) => Err(e),
@ -661,6 +671,8 @@ pub struct StreamableHttpClientTransportConfig {
pub channel_buffer_capacity: usize,
/// if true, the transport will not require a session to be established
pub allow_stateless: bool,
/// The value to send in the authorization header
pub auth_header: Option<String>,
}
impl StreamableHttpClientTransportConfig {
@ -670,6 +682,17 @@ impl StreamableHttpClientTransportConfig {
..Default::default()
}
}
/// Set the authorization header to send with requests
///
/// # Arguments
///
/// * `value` - The value to set
pub fn auth_header<T: Into<String>>(mut self, value: T) -> Self {
// set our authorization header
self.auth_header = Some(value.into());
self
}
}
impl Default for StreamableHttpClientTransportConfig {
@ -679,6 +702,7 @@ impl Default for StreamableHttpClientTransportConfig {
retry_config: Arc::new(ExponentialBackoff::default()),
channel_buffer_capacity: 16,
allow_stateless: true,
auth_header: None,
}
}
}