SseClientTransport

Struct SseClientTransport 

Source
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 infrastructure
  • transport-sse-client-reqwest: Includes reqwest HTTP client support with convenience methods

Implementations§

Source§

impl<C> SseClientTransport<C>
where C: SseClient,

Source§

impl SseClientTransport<Client>

Source

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>
where C: SseClient + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<C> Transport<RoleClient> for SseClientTransport<C>
where C: SseClient,

Source§

type Error = SseTransportError<<C as SseClient>::Error>

Source§

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

Send a message to the transport Read more
Source§

async fn close( &mut self, ) -> Result<(), <SseClientTransport<C> as Transport<RoleClient>>::Error>

Close the transport
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<R, T, E> IntoTransport<R, E, TransportAdapterIdentity> for T
where T: Transport<R, Error = E> + Send + 'static, R: ServiceRole, E: Error + Send + Sync + 'static,

Source§

fn into_transport(self) -> impl Transport<R, Error = E> + 'static

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more