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 + Sync + Send>>>,
message_id_counter: Arc<AtomicI64>,
request_timeout: Duration,
) -> MessageDispatcher<R>
pub fn new( pending_requests: Arc<Mutex<HashMap<RequestId, Sender<R>>>>, writable_std: Mutex<Pin<Box<dyn AsyncWrite + Sync + Send>>>, 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.
Sourcepub fn request_id_for_message(
&self,
message: &impl McpMessage,
request_id: Option<RequestId>,
) -> Option<RequestId>
pub fn request_id_for_message( &self, message: &impl McpMessage, request_id: Option<RequestId>, ) -> Option<RequestId>
Determines the request ID for an outgoing MCP message.
For requests, generates a new ID using the internal counter. For responses or errors,
uses the provided request_id
. Notifications receive no ID.
§Arguments
message
- The MCP message to evaluate.request_id
- An optional existing request ID (required for responses/errors).
§Returns
An Option<RequestId>
: Some
for requests or responses/errors, None
for notifications.
pub fn next_request_id(&self) -> RequestId
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,
) -> 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,
) -> 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,
) -> 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,
) -> 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.