cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
websocket_transport.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [arookieofc]
2
3#pragma once
4
14
15#include <chrono>
16#include <cstddef>
17#include <cstdint>
18#include <memory>
19#include <optional>
20#include <string>
21#include <string_view>
22#include <unordered_map>
23
26#include "cxxmcp/roles.hpp"
28
29namespace mcp::transport {
30
35 std::string uri;
36
38 std::string host = "127.0.0.1";
39
41 int port = 80;
42
44 std::string path = "/mcp";
45
47 std::unordered_map<std::string, std::string> headers;
48
50 std::optional<std::string> auth_header;
51
53 std::chrono::milliseconds timeout{30000};
54
56 bool auto_reconnect = true;
57
60
62 std::chrono::milliseconds reconnect_initial_delay{1000};
63
65 std::chrono::milliseconds reconnect_max_delay{30000};
66
69
71 time_t ping_interval_sec = 30;
72
75};
76
80 std::string listen_host = "127.0.0.1";
81
83 int listen_port = 0;
84
86 std::string path = "/mcp";
87
89 std::size_t max_connections = 1024;
90
92 time_t ping_interval_sec = 30;
93
96};
97
117 public:
121
124
126 WebSocketClientTransport& operator=(const WebSocketClientTransport&) = delete;
127
129 std::string_view name() const noexcept override;
130
133 protocol::Json diagnostics() const override;
134
140 core::Result<core::Unit> send(TxMessage message) override;
141
145 core::Result<std::optional<RxMessage>> receive() override;
146
148 core::Result<core::Unit> close() override;
149
150 private:
151 class Impl;
152 std::unique_ptr<Impl> impl_;
153};
154
176 public:
180
183
185 WebSocketServerTransport& operator=(const WebSocketServerTransport&) = delete;
186
188 std::string_view name() const noexcept override;
189
192 protocol::Json diagnostics() const override;
193
198 core::Result<core::Unit> send(TxMessage message) override;
199
203 core::Result<std::optional<RxMessage>> receive() override;
204
206 core::Result<core::Unit> close() override;
207
209 void wait_until_ready() override;
210
211 private:
212 class Impl;
213 std::unique_ptr<Impl> impl_;
214};
215
216} // namespace mcp::transport
Minimal message-level transport contract shared by MCP roles.
Definition transport.hpp:38
virtual void wait_until_ready()
Blocks until the transport is ready to process messages.
Definition transport.hpp:77
WebSocket-based MCP client transport.
Definition websocket_transport.hpp:116
protocol::Json diagnostics() const override
Returns structured diagnostics including connection state and stats.
~WebSocketClientTransport() override
Stops the reader thread and closes the WebSocket connection.
core::Result< core::Unit > send(TxMessage message) override
Sends a JSON-RPC message over the WebSocket connection.
std::string_view name() const noexcept override
Returns "websocket-client".
WebSocketClientTransport(WebSocketClientTransportOptions options)
Constructs a client transport with the given options.
core::Result< core::Unit > close() override
Closes the transport and unblocks any blocked receive().
core::Result< std::optional< RxMessage > > receive() override
Blocks until the next inbound JSON-RPC message is available.
WebSocket-based MCP server transport.
Definition websocket_transport.hpp:175
~WebSocketServerTransport() override
Stops the HTTP server and closes all WebSocket connections.
WebSocketServerTransport(WebSocketServerTransportOptions options)
Constructs a server transport with the given options.
std::string_view name() const noexcept override
Returns "websocket-server".
Shared JSON, JSON-RPC, error, cancellation, and progress model types.
Shared result and error primitives used by the public cxxmcp SDK.
Role markers shared by peer, service, and transport SDK boundaries.
Configuration for the WebSocket client transport.
Definition websocket_transport.hpp:32
std::chrono::milliseconds reconnect_initial_delay
Initial backoff delay before the first reconnect attempt.
Definition websocket_transport.hpp:62
int port
Remote port (used when uri is empty).
Definition websocket_transport.hpp:41
double reconnect_backoff_multiplier
Backoff multiplier applied after each failed attempt.
Definition websocket_transport.hpp:68
std::optional< std::string > auth_header
Optional Authorization header value (e.g. "Bearer <token>").
Definition websocket_transport.hpp:50
int max_missed_pongs
Maximum missed pongs before considering the connection dead.
Definition websocket_transport.hpp:74
std::string path
WebSocket path (used when uri is empty).
Definition websocket_transport.hpp:44
time_t ping_interval_sec
WebSocket ping interval in seconds (0 = disabled).
Definition websocket_transport.hpp:71
std::unordered_map< std::string, std::string > headers
Extra HTTP headers sent during the WebSocket upgrade handshake.
Definition websocket_transport.hpp:47
std::string host
Remote host (used when uri is empty).
Definition websocket_transport.hpp:38
int max_reconnect_attempts
Maximum consecutive reconnection attempts. 0 = unlimited.
Definition websocket_transport.hpp:59
std::chrono::milliseconds reconnect_max_delay
Maximum backoff delay (cap for exponential growth).
Definition websocket_transport.hpp:65
std::string uri
Full ws:// or wss:// URI for the MCP endpoint.
Definition websocket_transport.hpp:35
std::chrono::milliseconds timeout
Per-operation timeout for the WebSocket connection.
Definition websocket_transport.hpp:53
bool auto_reconnect
Enable automatic reconnection on disconnect.
Definition websocket_transport.hpp:56
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 max_missed_pongs
Maximum missed pongs before dropping a client.
Definition websocket_transport.hpp:95
std::string listen_host
Interface address to bind.
Definition websocket_transport.hpp:80
std::size_t max_connections
Maximum concurrent WebSocket connections. 0 = unlimited.
Definition websocket_transport.hpp:89
time_t ping_interval_sec
WebSocket ping interval in seconds (0 = disabled).
Definition websocket_transport.hpp:92
int listen_port
TCP port to listen on.
Definition websocket_transport.hpp:83
Role-generic MCP transport contract for SDK peer/service layers.