pub struct WebSocketTransport { /* private fields */ }Expand description
WebSocket transport for MCP servers
Provides full-duplex communication over WebSocket.
WebSocket is a tower-mcp custom transport binding, not a standard
2026-07-28 MCP transport. JSON-RPC request bodies remain the source of
truth for final per-request metadata. The optional mcp.version.*
subprotocol is an upgrade-time compatibility hint constrained by this
transport’s exact ProtocolSupport allow-list.
Connection and cancellation semantics remain those of this custom binding:
a WebSocket close terminates the connection, and reconnecting a stored
session closes the older socket. It does not currently cancel an in-flight
handler. notifications/cancelled is processed between requests, so it
cannot interrupt a handler that is already executing. Final
subscriptions/listen multiplexing is not implemented on this binding.
Implementations§
Source§impl WebSocketTransport
impl WebSocketTransport
Sourcepub fn with_sampling(self) -> Self
pub fn with_sampling(self) -> Self
Enable sampling support for this transport.
When sampling is enabled, tool handlers can use ctx.sample() to
request LLM completions from connected clients.
Sourcepub fn protocol_support(self, support: ProtocolSupport) -> Self
pub fn protocol_support(self, support: ProtocolSupport) -> Self
Set the exact protocol versions this custom binding accepts.
Sourcepub fn protocol_versions<I, V>(
self,
versions: I,
) -> Result<Self, ProtocolSupportError>
pub fn protocol_versions<I, V>( self, versions: I, ) -> Result<Self, ProtocolSupportError>
Construct and set an exact runtime protocol-version allow-list.
Sourcepub fn oauth(self, metadata: ProtectedResourceMetadata) -> Self
pub fn oauth(self, metadata: ProtectedResourceMetadata) -> Self
Configure OAuth 2.1 Protected Resource Metadata for this transport.
When set, adds a GET endpoint at the resource’s path-aware RFC 9728
well-known location. This method only serves metadata; prefer
Self::into_oauth_router for a complete protected setup.
§Example
use tower_mcp::oauth::ProtectedResourceMetadata;
use tower_mcp::transport::websocket::WebSocketTransport;
use tower_mcp::McpRouter;
let metadata = ProtectedResourceMetadata::new("https://mcp.example.com")
.authorization_server("https://auth.example.com")
.scope("mcp:read");
let router = McpRouter::new().server_info("my-server", "1.0.0");
let transport = WebSocketTransport::new(router).oauth(metadata);Sourcepub fn into_oauth_router<V>(
self,
validator: V,
metadata: ProtectedResourceMetadata,
policy: ScopePolicy,
) -> Result<Router, ProtectedResourceMetadataError>where
V: TokenValidator,
pub fn into_oauth_router<V>(
self,
validator: V,
metadata: ProtectedResourceMetadata,
policy: ScopePolicy,
) -> Result<Router, ProtectedResourceMetadataError>where
V: TokenValidator,
Build a fully protected OAuth WebSocket resource-server router.
This validates and serves Protected Resource Metadata, authenticates WebSocket upgrades, enforces the canonical resource audience, and installs fail-closed per-operation scope checks.
Sourcepub fn into_oauth_router_at<V>(
self,
path: &str,
validator: V,
metadata: ProtectedResourceMetadata,
policy: ScopePolicy,
) -> Result<Router, ProtectedResourceMetadataError>where
V: TokenValidator,
pub fn into_oauth_router_at<V>(
self,
path: &str,
validator: V,
metadata: ProtectedResourceMetadata,
policy: ScopePolicy,
) -> Result<Router, ProtectedResourceMetadataError>where
V: TokenValidator,
Build a path-mounted, fully protected OAuth WebSocket router.
Sourcepub fn layer<L>(self, layer: L) -> Self
pub fn layer<L>(self, layer: L) -> Self
Apply a tower middleware layer to MCP request processing.
The layer is applied to the McpRouter service within each session,
wrapping the Service<RouterRequest> pipeline. This allows middleware
like timeouts, rate limiting, or custom instrumentation to be applied
at the MCP request level.
Middleware errors are automatically converted into JSON-RPC error responses, so the transport’s error handling remains unchanged.
§Example
use std::time::Duration;
use tower::ServiceBuilder;
use tower::timeout::TimeoutLayer;
use tower_mcp::McpRouter;
use tower_mcp::transport::websocket::WebSocketTransport;
let router = McpRouter::new().server_info("my-server", "1.0.0");
let transport = WebSocketTransport::new(router)
.layer(
ServiceBuilder::new()
.layer(TimeoutLayer::new(Duration::from_secs(30)))
.concurrency_limit(10)
.into_inner(),
);Sourcepub fn into_router(self) -> Router
pub fn into_router(self) -> Router
Build the axum router for this transport
Sourcepub fn into_router_at(self, path: &str) -> Router
pub fn into_router_at(self, path: &str) -> Router
Build an axum router mounted at a specific path