Struct MessageDispatcher

Source
pub struct MessageDispatcher<R> { /* private fields */ }
Expand description

Provides a dispatcher for sending MCP messages and handling responses.

MessageDispatcher facilitates MCP communication by managing message sending, request tracking, and response handling. It supports both client-to-server and server-to-client message flows through implementations of the McpDispatch trait. The dispatcher uses a transport mechanism (e.g., stdin/stdout) to serialize and send messages, and it tracks pending requests with a configurable timeout mechanism for asynchronous responses.

Implementations§

Source§

impl<R> MessageDispatcher<R>

Source

pub fn new( pending_requests: Arc<Mutex<HashMap<RequestId, Sender<R>>>>, writable_std: Mutex<Pin<Box<dyn AsyncWrite + Send + Sync>>>, message_id_counter: Arc<AtomicI64>, request_timeout: Duration, ) -> MessageDispatcher<R>

Creates a new MessageDispatcher instance with the given configuration.

§Arguments
  • pending_requests - A thread-safe map for storing pending request IDs and their response channels.
  • writable_std - A mutex-protected, pinned writer (e.g., stdout) for sending serialized messages.
  • message_id_counter - An atomic counter for generating unique request IDs.
  • request_timeout - The timeout duration in milliseconds for awaiting responses.
§Returns

A new MessageDispatcher instance configured for MCP message handling.

Trait Implementations§

Source§

impl McpDispatch<ClientMessage, MessageFromServer> for MessageDispatcher<ClientMessage>

Source§

fn send<'life0, 'async_trait>( &'life0 self, message: MessageFromServer, request_id: Option<RequestId>, request_timeout: Option<Duration>, ) -> Pin<Box<dyn Future<Output = Result<Option<ClientMessage>, TransportError>> + Send + 'async_trait>>
where 'life0: 'async_trait, MessageDispatcher<ClientMessage>: 'async_trait,

Sends a message from the server to the client and awaits a response if applicable.

Serializes the MessageFromServer to JSON, writes it to the transport, and waits for a ClientMessage response if the message is a request. Notifications and responses return Ok(None).

§Arguments
  • message - The server message to send.
  • request_id - An optional request ID (used for responses/errors, None for requests).
§Returns

A TransportResult containing Some(ClientMessage) for requests with a response, or None for notifications/responses, or an error if the operation fails.

§Errors

Returns a TransportError if serialization, writing, or timeout occurs.

Source§

impl McpDispatch<ServerMessage, MessageFromClient> for MessageDispatcher<ServerMessage>

Source§

fn send<'life0, 'async_trait>( &'life0 self, message: MessageFromClient, request_id: Option<RequestId>, request_timeout: Option<Duration>, ) -> Pin<Box<dyn Future<Output = Result<Option<ServerMessage>, TransportError>> + Send + 'async_trait>>
where 'life0: 'async_trait, MessageDispatcher<ServerMessage>: 'async_trait,

Sends a message from the client to the server and awaits a response if applicable.

Serializes the MessageFromClient to JSON, writes it to the transport, and waits for a ServerMessage response if the message is a request. Notifications and responses return Ok(None).

§Arguments
  • message - The client message to send.
  • request_id - An optional request ID (used for responses/errors, None for requests).
§Returns

A TransportResult containing Some(ServerMessage) for requests with a response, or None for notifications/responses, or an error if the operation fails.

§Errors

Returns a TransportError if serialization, writing, or timeout occurs.

Auto Trait Implementations§

§

impl<R> !Freeze for MessageDispatcher<R>

§

impl<R> !RefUnwindSafe for MessageDispatcher<R>

§

impl<R> Send for MessageDispatcher<R>
where R: Send,

§

impl<R> Sync for MessageDispatcher<R>
where R: Send,

§

impl<R> Unpin for MessageDispatcher<R>

§

impl<R> !UnwindSafe for MessageDispatcher<R>

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<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<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
Source§

impl<T> ErasedDestructor for T
where T: 'static,