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>>>,
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 + 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>
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,
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>
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,
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.