cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
mcp::transport::WebSocketServerTransport Class Referencefinal

WebSocket-based MCP server transport. More...

#include <websocket_transport.hpp>

Inheritance diagram for mcp::transport::WebSocketServerTransport:
mcp::transport::Transport< Role >

Public Member Functions

 WebSocketServerTransport (WebSocketServerTransportOptions options)
 Constructs a server transport with the given options.
 
 ~WebSocketServerTransport () override
 Stops the HTTP server and closes all WebSocket connections.
 
 WebSocketServerTransport (const WebSocketServerTransport &)=delete
 
WebSocketServerTransportoperator= (const WebSocketServerTransport &)=delete
 
std::string_view name () const noexcept override
 Returns "websocket-server".
 
protocol::Json diagnostics () const override
 Returns structured diagnostics including connection count and stats.
 
core::Result< core::Unitsend (TxMessage message) override
 Sends a JSON-RPC message to the appropriate connected client.
 
core::Result< std::optional< RxMessage > > receive () override
 Blocks until the next inbound JSON-RPC message from any client.
 
core::Result< core::Unitclose () override
 Closes the transport, all connections, and unblocks receive().
 
void wait_until_ready () override
 Blocks until the server socket is bound and listening.
 

Additional Inherited Members

- Public Types inherited from mcp::transport::Transport< Role >
using TxMessage = typename MessageTraits< Role >::TxMessage
 
using RxMessage = typename MessageTraits< Role >::RxMessage
 

Detailed Description

WebSocket-based MCP server transport.

Accepts WebSocket connections on a configurable path and provides a full-duplex JSON-RCP channel for each connected client. Responses are automatically routed to the connection that sent the corresponding request.

Thread safety: send() is safe to call from multiple threads. receive() is sequential. close() is safe to call from any thread.

options.listen_port = 3001;
options.path = "/mcp";
WebSocketServerTransport transport(std::move(options));
transport.wait_until_ready();
while (auto msg = transport.receive()) {
// handle message
}
WebSocket-based MCP server transport.
Definition websocket_transport.hpp:175
Configuration for the WebSocket server transport.
Definition websocket_transport.hpp:78
std::string path
WebSocket endpoint path pattern (e.g. "/mcp").
Definition websocket_transport.hpp:86
int listen_port
TCP port to listen on.
Definition websocket_transport.hpp:83

Constructor & Destructor Documentation

◆ WebSocketServerTransport()

mcp::transport::WebSocketServerTransport::WebSocketServerTransport ( WebSocketServerTransportOptions  options)
explicit

Constructs a server transport with the given options.

Parameters
optionsListen address and WebSocket configuration.

Member Function Documentation

◆ close()

core::Result< core::Unit > mcp::transport::WebSocketServerTransport::close ( )
overridevirtual

Closes the transport, all connections, and unblocks receive().

Implements mcp::transport::Transport< Role >.

◆ diagnostics()

protocol::Json mcp::transport::WebSocketServerTransport::diagnostics ( ) const
overridevirtual

Returns structured diagnostics including connection count and stats.

Reimplemented from mcp::transport::Transport< Role >.

◆ name()

std::string_view mcp::transport::WebSocketServerTransport::name ( ) const
overridevirtualnoexcept

Returns "websocket-server".

Implements mcp::transport::Transport< Role >.

◆ receive()

core::Result< std::optional< RxMessage > > mcp::transport::WebSocketServerTransport::receive ( )
overridevirtual

Blocks until the next inbound JSON-RPC message from any client.

Returns std::nullopt when the transport is closed.

Implements mcp::transport::Transport< Role >.

◆ send()

core::Result< core::Unit > mcp::transport::WebSocketServerTransport::send ( TxMessage  message)
overridevirtual

Sends a JSON-RPC message to the appropriate connected client.

Responses are routed to the connection that sent the original request. Notifications are sent to the most recently active connection.

Implements mcp::transport::Transport< Role >.

◆ wait_until_ready()

void mcp::transport::WebSocketServerTransport::wait_until_ready ( )
overridevirtual

Blocks until the server socket is bound and listening.

Reimplemented from mcp::transport::Transport< Role >.


The documentation for this class was generated from the following file: