pub struct SseClientTransport<C>where
C: SseClient,{ /* private fields */ }Expand description
A client-agnostic SSE transport for RMCP that supports Server-Sent Events.
This transport allows you to choose your preferred HTTP client implementation
by implementing the SseClient trait. The transport handles SSE streaming
and automatic reconnection.
§Usage
§Using reqwest
ⓘ
use rmcp::transport::SseClientTransport;
// Enable the reqwest feature in Cargo.toml:
// rmcp = { version = "0.5", features = ["transport-sse-client-reqwest"] }
let transport = SseClientTransport::start("http://localhost:8000/sse").await?;§Using a custom HTTP client
ⓘ
use rmcp::transport::sse_client::{SseClient, SseClientTransport, SseClientConfig};
use std::sync::Arc;
use futures::stream::BoxStream;
use rmcp::model::ClientJsonRpcMessage;
use sse_stream::{Sse, Error as SseError};
use http::Uri;
#[derive(Clone)]
struct MyHttpClient;
#[derive(Debug, thiserror::Error)]
struct MyError;
impl std::fmt::Display for MyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "MyError")
}
}
impl SseClient for MyHttpClient {
type Error = MyError;
async fn post_message(
&self,
_uri: Uri,
_message: ClientJsonRpcMessage,
_auth_token: Option<String>,
) -> Result<(), rmcp::transport::sse_client::SseTransportError<Self::Error>> {
todo!()
}
async fn get_stream(
&self,
_uri: Uri,
_last_event_id: Option<String>,
_auth_token: Option<String>,
) -> Result<BoxStream<'static, Result<Sse, SseError>>, rmcp::transport::sse_client::SseTransportError<Self::Error>> {
todo!()
}
}
let config = SseClientConfig {
sse_endpoint: "http://localhost:8000/sse".into(),
..Default::default()
};
let transport = SseClientTransport::start_with_client(MyHttpClient, config).await?;§Feature Flags
transport-sse-client: Base feature providing the generic transport infrastructuretransport-sse-client-reqwest: Includes reqwest HTTP client support with convenience methods
Implementations§
Source§impl<C> SseClientTransport<C>where
C: SseClient,
impl<C> SseClientTransport<C>where
C: SseClient,
pub async fn start_with_client( client: C, config: SseClientConfig, ) -> Result<SseClientTransport<C>, SseTransportError<<C as SseClient>::Error>>
Source§impl SseClientTransport<Client>
impl SseClientTransport<Client>
Sourcepub async fn start(
uri: impl Into<Arc<str>>,
) -> Result<SseClientTransport<Client>, SseTransportError<Error>>
pub async fn start( uri: impl Into<Arc<str>>, ) -> Result<SseClientTransport<Client>, SseTransportError<Error>>
Creates a new transport using reqwest with the specified SSE endpoint.
This is a convenience method that creates a transport using the default
reqwest client. This method is only available when the
transport-sse-client-reqwest feature is enabled.
§Arguments
uri- The SSE endpoint to connect to
§Example
use rmcp::transport::SseClientTransport;
// Enable the reqwest feature in Cargo.toml:
// rmcp = { version = "0.5", features = ["transport-sse-client-reqwest"] }
let transport = SseClientTransport::start("http://localhost:8000/sse").await?;§Feature requirement
This method requires the transport-sse-client-reqwest feature.
Trait Implementations§
Source§impl<C> Debug for SseClientTransport<C>
impl<C> Debug for SseClientTransport<C>
Source§impl<C> Transport<RoleClient> for SseClientTransport<C>where
C: SseClient,
impl<C> Transport<RoleClient> for SseClientTransport<C>where
C: SseClient,
type Error = SseTransportError<<C as SseClient>::Error>
Source§async fn receive(
&mut self,
) -> Option<JsonRpcMessage<ServerRequest, ServerResult, ServerNotification>>
async fn receive( &mut self, ) -> Option<JsonRpcMessage<ServerRequest, ServerResult, ServerNotification>>
Receive a message from the transport, this operation is sequential.
Source§fn send(
&mut self,
item: JsonRpcMessage<<RoleClient as ServiceRole>::Req, <RoleClient as ServiceRole>::Resp, <RoleClient as ServiceRole>::Not>,
) -> impl Future<Output = Result<(), <SseClientTransport<C> as Transport<RoleClient>>::Error>> + Send + 'static
fn send( &mut self, item: JsonRpcMessage<<RoleClient as ServiceRole>::Req, <RoleClient as ServiceRole>::Resp, <RoleClient as ServiceRole>::Not>, ) -> impl Future<Output = Result<(), <SseClientTransport<C> as Transport<RoleClient>>::Error>> + Send + 'static
Send a message to the transport Read more
Source§async fn close(
&mut self,
) -> Result<(), <SseClientTransport<C> as Transport<RoleClient>>::Error>
async fn close( &mut self, ) -> Result<(), <SseClientTransport<C> as Transport<RoleClient>>::Error>
Close the transport
fn name() -> Cow<'static, str>
Auto Trait Implementations§
impl<C> Freeze for SseClientTransport<C>where
C: Freeze,
impl<C> !RefUnwindSafe for SseClientTransport<C>
impl<C> Send for SseClientTransport<C>
impl<C> !Sync for SseClientTransport<C>
impl<C> Unpin for SseClientTransport<C>where
C: Unpin,
impl<C> !UnwindSafe for SseClientTransport<C>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more