mmcp_protocol/
port.rs

1use serde::{Serialize, de::DeserializeOwned};
2
3use crate::mcp::{JSONRPCError, JSONRPCMessage, RequestId};
4
5#[derive(thiserror::Error, Debug)]
6pub enum RPCPortError {
7    #[error("serialization error: {0}")]
8    Serialize(#[from] serde_json::Error),
9    #[error("failed to serialize result to object got {0}")]
10    SerializeNotObject(serde_json::Value),
11}
12
13pub trait RPCSink {
14    fn send_message(
15        &mut self,
16        message: JSONRPCMessage,
17    ) -> impl Future<Output = anyhow::Result<()>> + Send;
18
19    /// Send a notification to the peer.
20    fn send_notification<T: Serialize + Send>(
21        &mut self,
22        method: &str,
23        notification: T,
24    ) -> impl Future<Output = anyhow::Result<()>> + Send;
25
26    /// Send a response to the peer.
27    fn send_response<T: Serialize + Send>(
28        &mut self,
29        request_id: RequestId,
30        response: T,
31    ) -> impl Future<Output = anyhow::Result<()>> + Send;
32
33    /// Send a request to the peer and wait for a response.
34    fn request<T: Serialize + Send, R: DeserializeOwned + Send>(
35        &mut self,
36        request_id: RequestId,
37        method: &str,
38        request: T,
39    ) -> impl Future<Output = anyhow::Result<Result<R, JSONRPCError>>> + Send;
40}
41
42pub trait RPCPort {
43    /// Get the sink for sending messages to the peer.
44    fn sink(&self) -> impl RPCSink + Clone + Send + 'static;
45    /// Fetch a message from the peer. Handling commands in the background which sent by the `RPCSink`.
46    fn progress(
47        &mut self,
48    ) -> impl std::future::Future<Output = anyhow::Result<Option<JSONRPCMessage>>> + Send;
49}