Expand description
Transport abstractions for the MCP SDK.
This crate provides transport layer implementations for the MCP protocol. Transports handle the low-level details of sending and receiving JSON-RPC messages between MCP clients and servers.
§Overview
The transport layer is responsible for:
- Serializing and deserializing JSON-RPC messages
- Managing connection lifecycle
- Providing different transport implementations (stdio, HTTP, WebSocket)
§Available Transports
| Transport | Use Case | Feature Flag |
|---|---|---|
stdio::SyncStdioTransport | Subprocess communication (CLI tools) | Always available |
memory::MemoryTransport | Testing and in-process communication | Requires runtime feature |
spawn::SpawnedTransport | Spawn MCP servers as subprocesses | tokio-runtime |
http::HttpTransport | HTTP client for streamable HTTP servers | Always available |
http::HttpTransportListener | HTTP server (Streamable HTTP) | http feature |
websocket::WebSocketTransport | WebSocket client with reconnection | Always available |
websocket::WebSocketListener | WebSocket server | Always available |
grpc::GrpcTransport | gRPC client with bidirectional streaming | grpc feature |
unix::UnixTransport | Unix domain sockets (local IPC) | Unix platforms only |
windows::NamedPipeTransport | Windows named pipes (local IPC) | Windows only |
§Quick Reference
For CLI tools / subprocess servers:
ⓘ
// Client spawning an MCP server
let transport = SpawnedTransport::spawn("my-mcp-server", &[]).await?;
// Server reading from stdin/stdout
let transport = SyncStdioTransport::new();For HTTP (Streamable HTTP transport):
ⓘ
// Client
let transport = HttpTransport::connect("http://localhost:8080/mcp").await?;
// Server (requires `http` feature)
let listener = HttpTransportListener::bind("0.0.0.0:8080").await?;For WebSocket:
ⓘ
// Client
let transport = WebSocketTransport::connect("ws://localhost:8080/mcp").await?;
// Server
let listener = WebSocketListener::bind("0.0.0.0:8080").await?;For testing:
ⓘ
let (client, server) = MemoryTransport::pair();§Runtime Support
This crate supports multiple async runtimes through feature flags:
tokio-runtime(default): Use Tokio for async I/Osmol-runtime: Use smol for async I/O
§Example
use mcpkit_transport::{Transport, SpawnedTransport};
#[tokio::main]
async fn main() -> Result<(), mcpkit_transport::TransportError> {
// Spawn an MCP server as a subprocess
let transport = SpawnedTransport::spawn("my-mcp-server", &[] as &[&str]).await?;
// Send and receive messages
while let Some(msg) = transport.recv().await? {
// Handle the message
}
transport.close().await?;
Ok(())
}Re-exports§
pub use error::TransportError;pub use traits::Transport;pub use traits::TransportExt;pub use traits::TransportListener;pub use traits::TransportMetadata;pub use memory::MemoryTransport;pub use stdio::SyncStdioTransport;pub use http::HttpTransportListener;pub use http::HttpTransport;pub use http::HttpTransportBuilder;pub use http::HttpTransportConfig;pub use websocket::ConnectionState;pub use websocket::ExponentialBackoff;pub use websocket::WebSocketConfig;pub use websocket::WebSocketListener;pub use websocket::WebSocketServerConfig;pub use websocket::WebSocketTransport;pub use websocket::WebSocketTransportBuilder;pub use unix::UnixListener;pub use unix::UnixSocketConfig;pub use unix::UnixTransport;pub use unix::UnixTransportBuilder;pub use grpc::GrpcConfig;pub use grpc::GrpcTransport;pub use pool::Pool;pub use pool::PoolConfig;pub use pool::PoolStats;pub use pool::PooledConnection;pub use spawn::SpawnedTransport;pub use spawn::SpawnedTransportBuilder;pub use telemetry::LatencyHistogram;pub use telemetry::MetricsSnapshot;pub use telemetry::TelemetryConfig;pub use telemetry::TelemetryLayer;pub use telemetry::TelemetryMetrics;pub use telemetry::TelemetryTransport;pub use telemetry::otel::OtelConfig;pub use telemetry::otel::TracingGuard;pub use telemetry::otel::init_tracing;pub use telemetry::otel::init_tracing_default;pub use telemetry::prom::McpMetrics;pub use telemetry::prom::MetricsExporter;pub use telemetry::prom::create_default_metrics;
Modules§
- error
- Transport error types.
- grpc
- gRPC transport for MCP communication.
- http
- HTTP transport with Server-Sent Events (SSE) streaming.
- memory
- In-memory transport for testing.
- middleware
- Tower-compatible middleware for MCP transports.
- pool
- Connection pooling for MCP transports.
- prelude
- Prelude module for convenient imports.
- runtime
- Runtime abstraction layer for async I/O.
- spawn
- Subprocess spawning utilities for stdio transport.
- stdio
- Standard I/O transport implementation.
- telemetry
- OpenTelemetry integration for MCP transports.
- traits
- Transport traits for the MCP protocol.
- unix
- Unix domain socket transport for MCP.
- websocket
- WebSocket transport for MCP.