Skip to main content

prism_mcp_rs/transport/
mod.rs

1//! Transport layer implementations
2//!
3//! This module provides concrete implementations of the transport traits
4//! for different communication protocols including STDIO, HTTP, and WebSocket.
5//!
6//! # When to Use Transport Directly
7//!
8//! Most users should use the high-level server/client APIs (`McpServer`, `McpClient`)
9//! which handle transport details automatically. Use Transport directly only when:
10//!
11//! - **Custom Transport**: Implementing new transport mechanisms
12//! - **Transport Middleware**: Adding logging, metrics, or transformations
13//! - **Fine-Grained Control**: Managing connection lifecycle manually
14//! - **Protocol Bridging**: Connecting different transport types
15//!
16//! # Available Transports
17//!
18//! ## STDIO Transport (Default)
19
20//! ```no_run
21//! # #[cfg(feature = "stdio")]
22//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
23//! use prism_mcp_rs::transport::StdioServerTransport;
24//! use prism_mcp_rs::server::McpServer;
25//!
26//! let transport = StdioServerTransport::new();
27//! let server = McpServer::new("server".to_string(), "1.0.0".to_string());
28//! // server.run_with_transport(transport).await?;
29//! # Ok(())
30//! # }
31//! ```
32//!
33//! ## HTTP Transport
34
35//! ```no_run
36//! # #[cfg(feature = "http")]
37//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
38//! // Requires "http" feature in Cargo.toml:
39//! // prism-mcp-rs = { version = "3", features = ["http"] }
40//! use prism_mcp_rs::transport::HttpServerTransport;
41//!
42//! // HttpServerTransport takes a bind address directly
43//! let transport = HttpServerTransport::new("127.0.0.1:8080");
44//! # Ok(())
45//! # }
46//! ```
47//!
48//! ## WebSocket Transport
49
50//! ```no_run
51//! # #[cfg(feature = "websocket")]
52//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
53//! // Requires "websocket" feature in Cargo.toml:
54//! // prism-mcp-rs = { version = "3", features = ["websocket"] }
55//! use prism_mcp_rs::transport::WebSocketServerTransport;
56//!
57//! // WebSocketServerTransport uses new() method, not bind()
58//! let transport = WebSocketServerTransport::new("127.0.0.1:9000");
59//! # Ok(())
60//! # }
61//! ```
62
63//!
64//! # Transport Traits
65//!
66//! The transport layer is built on several traits:
67//!
68//! - [`Transport`]: Core bidirectional message passing
69//! - [`ServerTransport`]: Server-specific transport operations
70//! - [`ReconnectableTransport`]: Auto-reconnection support
71//! - [`EventEmittingTransport`]: Event notification system
72//! - [`FilterableTransport`]: Message filtering and transformation
73//!
74//! # Advanced Features
75//!
76//! - **Authentication**: HTTP transport supports various auth mechanisms
77//! - **Compression**: Streaming HTTP supports gzip/brotli/zstd (requires "compression" feature)
78//! - **Prism Chunked Endpoints**: Proprietary legacy-only helpers (requires "chunked-encoding" feature)
79//! - **MCP 2026 Subscriptions**: Request-scoped SSE is built into the standard HTTP transport
80//! - **Legacy Server-Sent Events**: Compatibility events route (requires "sse" feature)
81//! - **Reconnection**: Automatic reconnection with backoff
82//! - **Metrics**: Performance monitoring and statistics
83//! - **HTTP/2**: Multiplexed streaming support (requires "http2" feature)
84//!
85//! # Implementation Note
86//!
87//! The Transport trait is primarily an internal abstraction. Unless you're
88//! implementing custom transports or need very specific control, use the
89//! high-level APIs provided by `McpServer` and `McpClient`.
90
91pub mod endpoint_pool;
92pub mod traits;
93
94#[cfg(feature = "stdio")]
95pub mod stdio;
96
97#[cfg(feature = "http")]
98pub mod http;
99
100#[cfg(feature = "http")]
101pub mod http_auth;
102
103#[cfg(feature = "websocket")]
104pub mod websocket;
105
106// Advanced HTTP transport features (chunking, compression, HTTP/2)
107#[cfg(any(
108    feature = "chunked-encoding",
109    feature = "compression",
110    feature = "http2"
111))]
112pub mod streaming_http;
113
114// Re-export commonly used types
115pub use endpoint_pool::{is_request_idempotent, EndpointPoolConfig, EndpointPoolTransport};
116pub use traits::{
117    ClientSubscription, ConnectionState, EventEmittingTransport, FilterableTransport,
118    ReconnectConfig, ReconnectableTransport, ServerTransport, Transport, TransportConfig,
119    TransportEvent, TransportStats,
120};
121
122// Re-export transport implementations when features are enabled
123#[cfg(feature = "stdio")]
124pub use stdio::{StdioClientTransport, StdioServerTransport};
125
126#[cfg(feature = "http")]
127pub use http::{HttpClientTransport, HttpServerTransport};
128
129#[cfg(all(feature = "http", feature = "tls"))]
130pub use http::{MtlsClientConfig, MtlsServerConfig};
131
132#[cfg(feature = "http")]
133pub use http_auth::{AuthorizedHttpTransport, AuthorizedHttpTransportBuilder};
134
135#[cfg(feature = "http")]
136pub mod http_convenience;
137
138#[cfg(feature = "http")]
139pub use http_convenience::{
140    ConnectionStats, ErrorMetrics, HttpClientTransportBuilder, HttpEndpoints, PerformanceMetrics,
141    RetryConfig, RetryPolicy, ServerInfo, TransportMetrics,
142};
143
144#[cfg(all(feature = "http", test))]
145mod http_convenience_test;
146
147#[cfg(feature = "websocket")]
148pub use websocket::{WebSocketClientTransport, WebSocketServerTransport};
149
150// Chunked encoding and streaming features
151#[cfg(feature = "chunked-encoding")]
152pub use streaming_http::{
153    ContentAnalyzer, ContentType, StreamingAnalysis, StreamingConfig, StreamingHttpClientTransport,
154    StreamingStrategy,
155};
156
157// Compression features
158#[cfg(feature = "compression")]
159pub use streaming_http::CompressionType;
160
161// HTTP/2 specific re-exports
162#[cfg(feature = "http2")]
163pub use streaming_http::{Http2Config, Http2StreamManager, PushPromise, StreamInfo, StreamState};