use serde_json::Value;
use super::http::HttpTransport;
use super::jsonrpc::McpError;
use super::stdio::StdioTransport;
pub const MAX_MESSAGE_BYTES: usize = 16 * 1024 * 1024;
#[derive(Debug)]
pub enum Event {
Message(Value),
Error(String),
Closed,
}
pub type Events = tokio::sync::mpsc::UnboundedSender<Event>;
pub enum Transport {
Stdio(Box<StdioTransport>),
Http(HttpTransport),
}
impl Transport {
pub async fn start(&self, events: Events) -> Result<(), McpError> {
match self {
Transport::Stdio(stdio) => stdio.start(events).await,
Transport::Http(http) => http.start(events),
}
}
pub async fn send(&self, message: &Value) -> Result<(), McpError> {
match self {
Transport::Stdio(stdio) => stdio.send(message).await,
Transport::Http(http) => http.send(message).await,
}
}
pub async fn close(&self) {
match self {
Transport::Stdio(stdio) => stdio.close().await,
Transport::Http(http) => http.close().await,
}
}
pub fn set_protocol_version(&self, version: &str) {
if let Transport::Http(http) = self {
http.set_protocol_version(version);
}
}
pub fn stderr(&self) -> Option<String> {
match self {
Transport::Stdio(stdio) => Some(stdio.stderr()),
Transport::Http(_) => None,
}
}
}