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 fn send_notification<T: Serialize + Send>(
21 &mut self,
22 method: &str,
23 notification: T,
24 ) -> impl Future<Output = anyhow::Result<()>> + Send;
25
26 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 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 fn sink(&self) -> impl RPCSink + Clone + Send + 'static;
45 fn progress(
47 &mut self,
48 ) -> impl std::future::Future<Output = anyhow::Result<Option<JSONRPCMessage>>> + Send;
49}