use rmcp::ServiceExt;
use tokio_util::sync::CancellationToken;
use super::Adapter;
#[derive(Debug, thiserror::Error)]
pub enum ServeError {
#[error("the stdio transport could not start: {0}")]
Start(String),
#[error("the service task failed: {0}")]
Join(String),
}
pub async fn serve(adapter: Adapter, shutdown: CancellationToken) -> Result<(), ServeError> {
let running = adapter
.serve(rmcp::transport::stdio())
.await
.map_err(|e| ServeError::Start(e.to_string()))?;
let stop = running.cancellation_token();
tokio::spawn(async move {
shutdown.cancelled().await;
stop.cancel();
});
let reason = running
.waiting()
.await
.map_err(|e| ServeError::Join(e.to_string()))?;
tracing::info!(event = "shutdown", reason = ?reason, "transport closed");
Ok(())
}