pub trait McpDispatch<R, S>:
Send
+ Sync
+ 'staticwhere
R: McpMessage + Clone + Send + Sync + DeserializeOwned + 'static,
S: Clone + Send + Sync + Serialize + 'static,{
// Required method
fn send<'life0, 'async_trait>(
&'life0 self,
message: S,
request_id: Option<RequestId>,
request_timeout: Option<Duration>,
) -> Pin<Box<dyn Future<Output = Result<Option<R>, TransportError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait;
}
Expand description
A trait for sending MCP messages.
It is intended to be implemented by types that send messages in the MCP protocol, such as servers or clients.
The McpDispatch
trait requires two associated types:
R
: The type of the response, which must implement theMcpMessage
trait and be capable of deserialization.S
: The type of the message to send, which must be serializable and cloneable.
Both associated types R
and S
must be Send
, Sync
, and 'static
to ensure they can be used
safely in an asynchronous context and across threads.
§Associated Types
R
: The response type, which must implement theMcpMessage
trait, beClone
,Send
,Sync
, and be deserializable (DeserializeOwned
).S
: The type of the message to send, which must beClone
,Send
,Sync
, and serializable (Serialize
).
§Methods
§send
Sends a raw message represented by type S
and optionally includes a request_id
.
The method returns a TransportResult<Option<R>>
, where:
Option<R>
: The response, which can beNone
or contain the response of typeR
.TransportResult
: Represents the result of the operation, which can include success or failure.
§Arguments
message
: The message to send, of typeS
, which will be serialized before transmission.request_id
: An optionalRequestId
to associate with this message. It can be used for tracking or correlating the request with its response.
§Example
let sender: Box<dyn McpDispatch<MyResponse, MyMessage>> = …; let result = sender.send(my_message, Some(request_id)).await;
Required Methods§
Sourcefn send<'life0, 'async_trait>(
&'life0 self,
message: S,
request_id: Option<RequestId>,
request_timeout: Option<Duration>,
) -> Pin<Box<dyn Future<Output = Result<Option<R>, TransportError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn send<'life0, 'async_trait>(
&'life0 self,
message: S,
request_id: Option<RequestId>,
request_timeout: Option<Duration>,
) -> Pin<Box<dyn Future<Output = Result<Option<R>, TransportError>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Sends a raw message represented by type S
and optionally includes a request_id
.
The request_id
is used when sending a message in response to an MCP request.
It should match the request_id
of the original request.