mcpkit_transport/
lib.rs

1//! Transport abstractions for the MCP SDK.
2//!
3//! This crate provides transport layer implementations for the MCP protocol.
4//! Transports handle the low-level details of sending and receiving JSON-RPC
5//! messages between MCP clients and servers.
6//!
7//! # Overview
8//!
9//! The transport layer is responsible for:
10//!
11//! - Serializing and deserializing JSON-RPC messages
12//! - Managing connection lifecycle
13//! - Providing different transport implementations (stdio, HTTP, WebSocket)
14//!
15//! # Available Transports
16//!
17//! | Transport | Use Case | Feature Flag |
18//! |-----------|----------|--------------|
19//! | [`stdio::SyncStdioTransport`] | Subprocess communication (CLI tools) | Always available |
20//! | [`memory::MemoryTransport`] | Testing and in-process communication | Requires runtime feature |
21//! | [`spawn::SpawnedTransport`] | Spawn MCP servers as subprocesses | `tokio-runtime` |
22//! | [`http::HttpTransport`] | HTTP client for streamable HTTP servers | Always available |
23//! | [`http::HttpTransportListener`] | HTTP server (Streamable HTTP) | `http` feature |
24//! | [`websocket::WebSocketTransport`] | WebSocket client with reconnection | Always available |
25//! | [`websocket::WebSocketListener`] | WebSocket server | Always available |
26//! | `grpc::GrpcTransport` | gRPC client with bidirectional streaming | `grpc` feature |
27//! | `unix::UnixTransport` | Unix domain sockets (local IPC) | Unix platforms only |
28//! | `windows::NamedPipeTransport` | Windows named pipes (local IPC) | Windows only |
29//!
30//! ## Quick Reference
31//!
32//! **For CLI tools / subprocess servers:**
33//! ```ignore
34//! // Client spawning an MCP server
35//! let transport = SpawnedTransport::spawn("my-mcp-server", &[]).await?;
36//!
37//! // Server reading from stdin/stdout
38//! let transport = SyncStdioTransport::new();
39//! ```
40//!
41//! **For HTTP (Streamable HTTP transport):**
42//! ```ignore
43//! // Client
44//! let transport = HttpTransport::connect("http://localhost:8080/mcp").await?;
45//!
46//! // Server (requires `http` feature)
47//! let listener = HttpTransportListener::bind("0.0.0.0:8080").await?;
48//! ```
49//!
50//! **For WebSocket:**
51//! ```ignore
52//! // Client
53//! let transport = WebSocketTransport::connect("ws://localhost:8080/mcp").await?;
54//!
55//! // Server
56//! let listener = WebSocketListener::bind("0.0.0.0:8080").await?;
57//! ```
58//!
59//! **For testing:**
60//! ```ignore
61//! let (client, server) = MemoryTransport::pair();
62//! ```
63//!
64//! # Runtime Support
65//!
66//! This crate supports multiple async runtimes through feature flags:
67//!
68//! - `tokio-runtime` (default): Use Tokio for async I/O
69//! - `smol-runtime`: Use smol for async I/O
70//!
71//! # Example
72//!
73//! ```no_run
74//! use mcpkit_transport::{Transport, SpawnedTransport};
75//!
76//! #[tokio::main]
77//! async fn main() -> Result<(), mcpkit_transport::TransportError> {
78//!     // Spawn an MCP server as a subprocess
79//!     let transport = SpawnedTransport::spawn("my-mcp-server", &[] as &[&str]).await?;
80//!
81//!     // Send and receive messages
82//!     while let Some(msg) = transport.recv().await? {
83//!         // Handle the message
84//!     }
85//!
86//!     transport.close().await?;
87//!     Ok(())
88//! }
89//! ```
90
91#![deny(missing_docs)]
92
93pub mod error;
94pub mod http;
95pub mod memory;
96pub mod middleware;
97pub mod pool;
98pub mod runtime;
99pub mod spawn;
100pub mod stdio;
101pub mod telemetry;
102pub mod traits;
103pub mod websocket;
104
105#[cfg(feature = "grpc")]
106pub mod grpc;
107
108#[cfg(unix)]
109pub mod unix;
110
111#[cfg(windows)]
112pub mod windows;
113
114// Re-export commonly used types
115pub use error::TransportError;
116pub use traits::{Transport, TransportExt, TransportListener, TransportMetadata};
117
118// Re-export bytes types for zero-copy message handling
119pub use bytes::{Bytes, BytesMut};
120
121// Runtime-agnostic transports - available when ANY runtime is enabled
122#[cfg(any(feature = "tokio-runtime", feature = "smol-runtime"))]
123pub use memory::MemoryTransport;
124
125// Note: StdioTransport has runtime-specific type parameters, so we re-export
126// the module rather than a specific type alias
127pub use stdio::SyncStdioTransport;
128
129// HTTP transport (always export config/builder, listener only with http feature)
130#[cfg(feature = "http")]
131pub use http::HttpTransportListener;
132pub use http::{HttpTransport, HttpTransportBuilder, HttpTransportConfig};
133
134// WebSocket transport
135pub use websocket::{
136    ConnectionState, ExponentialBackoff, WebSocketConfig, WebSocketListener, WebSocketServerConfig,
137    WebSocketTransport, WebSocketTransportBuilder,
138};
139
140// Unix socket transport
141#[cfg(unix)]
142pub use unix::{UnixListener, UnixSocketConfig, UnixTransport, UnixTransportBuilder};
143
144// Windows named pipe transport
145#[cfg(windows)]
146pub use windows::{NamedPipeBuilder, NamedPipeConfig, NamedPipeServer, NamedPipeTransport};
147
148// gRPC transport (requires `grpc` feature)
149#[cfg(feature = "grpc")]
150pub use grpc::{GrpcConfig, GrpcTransport};
151
152// Connection pooling
153pub use pool::{Pool, PoolConfig, PoolStats, PooledConnection};
154
155// Subprocess spawning
156#[cfg(feature = "tokio-runtime")]
157pub use spawn::{SpawnedTransport, SpawnedTransportBuilder};
158
159// Telemetry
160pub use telemetry::{
161    LatencyHistogram, MetricsSnapshot, TelemetryConfig, TelemetryLayer, TelemetryMetrics,
162    TelemetryTransport,
163};
164
165// OpenTelemetry integration (requires `opentelemetry` feature)
166#[cfg(feature = "opentelemetry")]
167pub use telemetry::otel::{OtelConfig, TracingGuard, init_tracing, init_tracing_default};
168
169// Prometheus metrics (requires `prometheus` feature)
170#[cfg(feature = "prometheus")]
171pub use telemetry::prom::{McpMetrics, MetricsExporter, create_default_metrics};
172
173/// Prelude module for convenient imports.
174pub mod prelude {
175    pub use crate::error::TransportError;
176    pub use crate::traits::{Transport, TransportExt, TransportListener, TransportMetadata};
177
178    #[cfg(any(feature = "tokio-runtime", feature = "smol-runtime"))]
179    pub use crate::memory::MemoryTransport;
180
181    pub use crate::stdio::SyncStdioTransport;
182
183    // HTTP
184    pub use crate::http::{HttpTransport, HttpTransportConfig};
185
186    // WebSocket
187    pub use crate::websocket::{WebSocketConfig, WebSocketServerConfig, WebSocketTransport};
188
189    // Unix
190    #[cfg(unix)]
191    pub use crate::unix::{UnixListener, UnixTransport};
192
193    // Windows
194    #[cfg(windows)]
195    pub use crate::windows::{NamedPipeServer, NamedPipeTransport};
196
197    // Pool
198    pub use crate::pool::{Pool, PoolConfig, PooledConnection};
199
200    // Subprocess spawning
201    #[cfg(feature = "tokio-runtime")]
202    pub use crate::spawn::{SpawnedTransport, SpawnedTransportBuilder};
203}