Skip to content

Commit ee0e1e2

Browse files
committed
feat(rmcp): Add authorization header support for the streamable http client
This allows the streamable http client to send an authorization header when making requests. Closes #387
1 parent 7135f25 commit ee0e1e2

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
@@ -130,8 +130,18 @@ impl StreamableHttpClientTransport<reqwest::Client> {
130130
reqwest::Client::default(),
131131
StreamableHttpClientTransportConfig {
132132
uri: uri.into(),
133+
auth_header: None,
133134
..Default::default()
134135
},
135136
)
136137
}
138+
139+
/// Build this transport form a config
140+
///
141+
/// # Arguments
142+
///
143+
/// * `config` - The config to use with this transport
144+
pub fn from_config(config: StreamableHttpClientTransportConfig) -> Self {
145+
StreamableHttpClientTransport::with_client(reqwest::Client::default(), config)
146+
}
137147
}

crates/rmcp/src/transport/streamable_http_client.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,12 @@ impl<C: StreamableHttpClient> Worker for StreamableHttpClientWorker<C> {
282282
let _ = responder.send(Ok(()));
283283
let (message, session_id) = self
284284
.client
285-
.post_message(config.uri.clone(), initialize_request, None, None)
285+
.post_message(
286+
config.uri.clone(),
287+
initialize_request,
288+
None,
289+
self.config.auth_header,
290+
)
286291
.await
287292
.map_err(WorkerQuitReason::fatal_context("send initialize request"))?
288293
.expect_initialized::<C::Error>()
@@ -339,7 +344,7 @@ impl<C: StreamableHttpClient> Worker for StreamableHttpClientWorker<C> {
339344
config.uri.clone(),
340345
initialized_notification.message,
341346
session_id.clone(),
342-
None,
347+
config.auth_header.clone(),
343348
)
344349
.await
345350
.map_err(WorkerQuitReason::fatal_context(
@@ -426,7 +431,12 @@ impl<C: StreamableHttpClient> Worker for StreamableHttpClientWorker<C> {
426431
let WorkerSendRequest { message, responder } = send_request;
427432
let response = self
428433
.client
429-
.post_message(config.uri.clone(), message, session_id.clone(), None)
434+
.post_message(
435+
config.uri.clone(),
436+
message,
437+
session_id.clone(),
438+
config.auth_header.clone(),
439+
)
430440
.await;
431441
let send_result = match response {
432442
Err(e) => Err(e),
@@ -505,6 +515,8 @@ pub struct StreamableHttpClientTransportConfig {
505515
pub channel_buffer_capacity: usize,
506516
/// if true, the transport will not require a session to be established
507517
pub allow_stateless: bool,
518+
/// The value to send in the authorization header
519+
pub auth_header: Option<String>,
508520
}
509521

510522
impl StreamableHttpClientTransportConfig {
@@ -514,6 +526,17 @@ impl StreamableHttpClientTransportConfig {
514526
..Default::default()
515527
}
516528
}
529+
530+
/// Set the authorization header to send with requests
531+
///
532+
/// # Arguments
533+
///
534+
/// * `value` - The value to set
535+
pub fn auth_header<T: Into<String>>(mut self, value: T) -> Self {
536+
// set our authorization header
537+
self.auth_header = Some(value.into());
538+
self
539+
}
517540
}
518541

519542
impl Default for StreamableHttpClientTransportConfig {
@@ -523,6 +546,7 @@ impl Default for StreamableHttpClientTransportConfig {
523546
retry_config: Arc::new(ExponentialBackoff::default()),
524547
channel_buffer_capacity: 16,
525548
allow_stateless: true,
549+
auth_header: None,
526550
}
527551
}
528552
}

0 commit comments

Comments
 (0)