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>
impl<R> MessageDispatcher<R>
Sourcepub fn new(
pending_requests: Arc<Mutex<HashMap<RequestId, Sender<R>>>>,
writable_std: Mutex<Pin<Box<dyn AsyncWrite + Send + Sync>>>,
request_timeout: Duration,
) -> MessageDispatcher<R>
pub fn new( pending_requests: Arc<Mutex<HashMap<RequestId, Sender<R>>>>, writable_std: Mutex<Pin<Box<dyn AsyncWrite + Send + Sync>>>, 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.
pub fn new_with_acknowledgement( pending_requests: Arc<Mutex<HashMap<RequestId, Sender<R>>>>, writable_tx: Sender<(String, Sender<Result<(), TransportError>>)>, request_timeout: Duration, ) -> MessageDispatcher<R>
Sourcepub fn make_resumable(
&mut self,
session_id: String,
stream_id: String,
event_store: Arc<dyn EventStore>,
)
pub fn make_resumable( &mut self, session_id: String, stream_id: String, event_store: Arc<dyn EventStore>, )
Supports resumability for streamable HTTP transports by setting the session ID, stream ID, and event store.
Trait Implementations§
Source§impl McpDispatch<ClientMessages, ServerMessages, ClientMessage, ServerMessage> for MessageDispatcher<ClientMessage>
impl McpDispatch<ClientMessages, ServerMessages, ClientMessage, ServerMessage> for MessageDispatcher<ClientMessage>
Source§fn send_message<'life0, 'async_trait>(
&'life0 self,
messages: ServerMessages,
request_timeout: Option<Duration>,
) -> Pin<Box<dyn Future<Output = Result<Option<ClientMessages>, TransportError>> + Send + 'async_trait>>where
'life0: 'async_trait,
MessageDispatcher<ClientMessage>: 'async_trait,
fn send_message<'life0, 'async_trait>(
&'life0 self,
messages: ServerMessages,
request_timeout: Option<Duration>,
) -> Pin<Box<dyn Future<Output = Result<Option<ClientMessages>, 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 ServerMessages to JSON, writes it to the transport, and waits for a
ClientMessages response if the message is a request. Notifications and responses return
Ok(None).
§Arguments
messages- The client message to send, coulld be a single message or batch.
§Returns
A TransportResult containing Some(ClientMessages) 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§fn write_str<'life0, 'life1, 'async_trait>(
&'life0 self,
payload: &'life1 str,
skip_store: bool,
) -> Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
MessageDispatcher<ClientMessage>: 'async_trait,
fn write_str<'life0, 'life1, 'async_trait>(
&'life0 self,
payload: &'life1 str,
skip_store: bool,
) -> Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
MessageDispatcher<ClientMessage>: 'async_trait,
Writes a string payload to the underlying asynchronous writable stream, appending a newline character and flushing the stream afterward.
fn send<'life0, 'async_trait>(
&'life0 self,
message: ServerMessage,
request_timeout: Option<Duration>,
) -> Pin<Box<dyn Future<Output = Result<Option<ClientMessage>, TransportError>> + Send + 'async_trait>>where
'life0: 'async_trait,
MessageDispatcher<ClientMessage>: 'async_trait,
fn send_batch<'life0, 'async_trait>(
&'life0 self,
message: Vec<ServerMessage>,
request_timeout: Option<Duration>,
) -> Pin<Box<dyn Future<Output = Result<Option<Vec<ClientMessage>>, TransportError>> + Send + 'async_trait>>where
'life0: 'async_trait,
MessageDispatcher<ClientMessage>: 'async_trait,
Source§impl McpDispatch<ServerMessages, ClientMessages, ServerMessage, ClientMessage> for MessageDispatcher<ServerMessage>
impl McpDispatch<ServerMessages, ClientMessages, ServerMessage, ClientMessage> for MessageDispatcher<ServerMessage>
Source§fn send_message<'life0, 'async_trait>(
&'life0 self,
messages: ClientMessages,
request_timeout: Option<Duration>,
) -> Pin<Box<dyn Future<Output = Result<Option<ServerMessages>, TransportError>> + Send + 'async_trait>>where
'life0: 'async_trait,
MessageDispatcher<ServerMessage>: 'async_trait,
fn send_message<'life0, 'async_trait>(
&'life0 self,
messages: ClientMessages,
request_timeout: Option<Duration>,
) -> Pin<Box<dyn Future<Output = Result<Option<ServerMessages>, 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 ClientMessages to JSON, writes it to the transport, and waits for a
ServerMessages response if the message is a request. Notifications and responses return
Ok(None).
§Arguments
messages- The client message to send, coulld be a single message or batch.
§Returns
A TransportResult containing Some(ServerMessages) 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§fn write_str<'life0, 'life1, 'async_trait>(
&'life0 self,
payload: &'life1 str,
_skip_store: bool,
) -> Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
MessageDispatcher<ServerMessage>: 'async_trait,
fn write_str<'life0, 'life1, 'async_trait>(
&'life0 self,
payload: &'life1 str,
_skip_store: bool,
) -> Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
MessageDispatcher<ServerMessage>: 'async_trait,
Writes a string payload to the underlying asynchronous writable stream, appending a newline character and flushing the stream afterward.