use rmcp::ServerHandler;
use rmcp::model::{
ClientCapabilities, Implementation, InitializeRequestParams, ProtocolVersion,
ServerCapabilities, ServerInfo,
};
use rmcp::service::{
RequestContext, RoleServer, RxJsonRpcMessage, TxJsonRpcMessage, serve_directly,
};
use rmcp::transport::Transport;
use tokio::sync::mpsc;
struct NullServerTransport {
rx: mpsc::Receiver<RxJsonRpcMessage<RoleServer>>,
tx: mpsc::Sender<TxJsonRpcMessage<RoleServer>>,
}
impl Transport<RoleServer> for NullServerTransport {
type Error = std::io::Error;
fn send(
&mut self,
item: TxJsonRpcMessage<RoleServer>,
) -> impl Future<Output = Result<(), Self::Error>> + Send + 'static {
let tx = self.tx.clone();
async move {
let _ = tx.try_send(item);
Ok(())
}
}
fn receive(&mut self) -> impl Future<Output = Option<RxJsonRpcMessage<RoleServer>>> + Send {
self.rx.recv()
}
fn close(&mut self) -> impl Future<Output = Result<(), Self::Error>> + Send {
self.rx.close();
async { Ok(()) }
}
}
#[derive(Clone)]
struct NullHandler;
impl ServerHandler for NullHandler {
fn get_info(&self) -> ServerInfo {
ServerInfo::new(ServerCapabilities::builder().build())
}
}
pub(crate) fn test_request_context() -> RequestContext<RoleServer> {
let (inbound_producer, inbound) = mpsc::channel::<RxJsonRpcMessage<RoleServer>>(1);
drop(inbound_producer);
let (outbound, outbound_reader) = mpsc::channel::<TxJsonRpcMessage<RoleServer>>(1);
drop(outbound_reader);
let running = serve_directly(
NullHandler,
NullServerTransport {
rx: inbound,
tx: outbound,
},
Some(
InitializeRequestParams::new(
ClientCapabilities::default(),
Implementation::new("vipune-test", env!("CARGO_PKG_VERSION")),
)
.with_protocol_version(ProtocolVersion::LATEST),
),
);
let peer = running.peer().clone();
drop(running.cancel());
RequestContext::new(rmcp::model::RequestId::Number(1), peer)
}