pulseengine_mcp_transport/
lib.rs1pub mod batch;
33pub mod config;
34pub mod http;
35pub mod stdio;
36pub mod streamable_http;
37pub mod validation;
38pub mod websocket;
39
40#[cfg(test)]
41mod batch_tests;
42#[cfg(test)]
43mod config_tests;
44#[cfg(test)]
45mod http_test;
46#[cfg(test)]
47mod http_tests;
48#[cfg(test)]
49mod lib_tests;
50#[cfg(test)]
51mod stdio_tests;
52#[cfg(test)]
53mod streamable_http_tests;
54#[cfg(test)]
55mod validation_tests;
56#[cfg(test)]
57mod websocket_tests;
58
59use async_trait::async_trait;
60use pulseengine_mcp_protocol::{Request, Response};
61use thiserror::Error as ThisError;
63
64pub use config::TransportConfig;
65
66#[derive(Debug, ThisError)]
67pub enum TransportError {
68 #[error("Transport configuration error: {0}")]
69 Config(String),
70
71 #[error("Connection error: {0}")]
72 Connection(String),
73
74 #[error("Protocol error: {0}")]
75 Protocol(String),
76}
77
78pub type RequestHandler = Box<
80 dyn Fn(Request) -> std::pin::Pin<Box<dyn std::future::Future<Output = Response> + Send>>
81 + Send
82 + Sync,
83>;
84
85#[async_trait]
87pub trait Transport: Send + Sync {
88 async fn start(&mut self, handler: RequestHandler) -> std::result::Result<(), TransportError>;
89 async fn stop(&mut self) -> std::result::Result<(), TransportError>;
90 async fn health_check(&self) -> std::result::Result<(), TransportError>;
91}
92
93pub fn create_transport(
95 config: TransportConfig,
96) -> std::result::Result<Box<dyn Transport>, TransportError> {
97 match config {
98 TransportConfig::Stdio => Ok(Box::new(stdio::StdioTransport::new())),
99 TransportConfig::Http { port, .. } => Ok(Box::new(http::HttpTransport::new(port))),
100 TransportConfig::StreamableHttp { port, .. } => Ok(Box::new(
101 streamable_http::StreamableHttpTransport::new(port),
102 )),
103 TransportConfig::WebSocket { port, .. } => {
104 Ok(Box::new(websocket::WebSocketTransport::new(port)))
105 }
106 }
107}