Skip to content

Commit 6853143

Browse files
authored
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
1 parent 357671c commit 6853143

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

crates/rmcp/src/transport/common/reqwest/streamable_http_client.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,18 @@ impl StreamableHttpClientTransport<reqwest::Client> {
160160
reqwest::Client::default(),
161161
StreamableHttpClientTransportConfig {
162162
uri: uri.into(),
163+
auth_header: None,
163164
..Default::default()
164165
},
165166
)
166167
}
168+
169+
/// Build this transport form a config
170+
///
171+
/// # Arguments
172+
///
173+
/// * `config` - The config to use with this transport
174+
pub fn from_config(config: StreamableHttpClientTransportConfig) -> Self {
175+
StreamableHttpClientTransport::with_client(reqwest::Client::default(), config)
176+
}
167177
}

crates/rmcp/src/transport/streamable_http_client.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,12 @@ impl<C: StreamableHttpClient> Worker for StreamableHttpClientWorker<C> {
277277
let _ = responder.send(Ok(()));
278278
let (message, session_id) = self
279279
.client
280-
.post_message(config.uri.clone(), initialize_request, None, None)
280+
.post_message(
281+
config.uri.clone(),
282+
initialize_request,
283+
None,
284+
self.config.auth_header,
285+
)
281286
.await
282287
.map_err(WorkerQuitReason::fatal_context("send initialize request"))?
283288
.expect_initialized::<C::Error>()
@@ -334,7 +339,7 @@ impl<C: StreamableHttpClient> Worker for StreamableHttpClientWorker<C> {
334339
config.uri.clone(),
335340
initialized_notification.message,
336341
session_id.clone(),
337-
None,
342+
config.auth_header.clone(),
338343
)
339344
.await
340345
.map_err(WorkerQuitReason::fatal_context(
@@ -421,7 +426,12 @@ impl<C: StreamableHttpClient> Worker for StreamableHttpClientWorker<C> {
421426
let WorkerSendRequest { message, responder } = send_request;
422427
let response = self
423428
.client
424-
.post_message(config.uri.clone(), message, session_id.clone(), None)
429+
.post_message(
430+
config.uri.clone(),
431+
message,
432+
session_id.clone(),
433+
config.auth_header.clone(),
434+
)
425435
.await;
426436
let send_result = match response {
427437
Err(e) => Err(e),
@@ -661,6 +671,8 @@ pub struct StreamableHttpClientTransportConfig {
661671
pub channel_buffer_capacity: usize,
662672
/// if true, the transport will not require a session to be established
663673
pub allow_stateless: bool,
674+
/// The value to send in the authorization header
675+
pub auth_header: Option<String>,
664676
}
665677

666678
impl StreamableHttpClientTransportConfig {
@@ -670,6 +682,17 @@ impl StreamableHttpClientTransportConfig {
670682
..Default::default()
671683
}
672684
}
685+
686+
/// Set the authorization header to send with requests
687+
///
688+
/// # Arguments
689+
///
690+
/// * `value` - The value to set
691+
pub fn auth_header<T: Into<String>>(mut self, value: T) -> Self {
692+
// set our authorization header
693+
self.auth_header = Some(value.into());
694+
self
695+
}
673696
}
674697

675698
impl Default for StreamableHttpClientTransportConfig {
@@ -679,6 +702,7 @@ impl Default for StreamableHttpClientTransportConfig {
679702
retry_config: Arc::new(ExponentialBackoff::default()),
680703
channel_buffer_capacity: 16,
681704
allow_stateless: true,
705+
auth_header: None,
682706
}
683707
}
684708
}

0 commit comments

Comments
 (0)