cxxmcp 1.2.4
C++ MCP SDK
Loading...
Searching...
No Matches
http_transport.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
7
8#include <chrono>
9#include <cstddef>
10#include <functional>
11#include <memory>
12#include <optional>
13#include <string>
14#include <unordered_map>
15#include <vector>
16
18
19namespace mcp::transport {
20
23 int status_code = 0;
24 std::string method;
25 std::unordered_map<std::string, std::string> headers;
26 std::optional<std::string> www_authenticate;
27};
28
31 std::function<std::optional<std::string>(
33
39 std::string uri;
40
42 std::string host;
43
45 int port = 80;
46
48 std::string path = "/";
49
52 std::unordered_map<std::string, std::string> headers;
53
57 std::optional<std::string> auth_header;
58
63
67 bool stateless = false;
68
70 std::chrono::milliseconds timeout{30000};
71};
72
82 public:
86
89 const StreamableHttpClientTransport&) = delete;
90
91 std::string_view name() const noexcept override;
92 protocol::Json diagnostics() const override;
93 core::Result<core::Unit> send(TxMessage message) override;
94 core::Result<std::optional<RxMessage>> receive() override;
95 core::Result<core::Unit> close() override;
96
97 private:
98 class Impl;
99
100 std::unique_ptr<Impl> impl_;
101};
102
106 std::string listen_host = "127.0.0.1";
107
109 int listen_port = 0;
110
112 std::string path = "/mcp";
113
115 std::optional<std::chrono::milliseconds> sse_retry;
116
120 bool enable_sse_polling = false;
121
124 std::chrono::milliseconds sse_disconnect_retry{5000};
125
127 std::vector<std::string> allowed_origins;
128
131 std::vector<std::string> allowed_hosts = {"localhost", "127.0.0.1", "::1"};
132
134 std::size_t max_pending_sse_events = 1024;
135
137 std::size_t max_pending_sse_bytes = 4 * 1024 * 1024;
138
140 std::size_t max_sse_replay_events = 256;
141
144 std::chrono::milliseconds request_timeout{30000};
145
148 std::size_t max_request_body_bytes = 10 * 1024 * 1024;
149
151 std::chrono::milliseconds read_timeout{30000};
152
154 std::chrono::milliseconds write_timeout{30000};
155
158 std::size_t max_sessions = 1024;
159
162 bool stateless = false;
163};
164
168 std::string session_id;
169 std::string remote_address;
170 std::unordered_map<std::string, std::string> headers;
171 std::optional<std::string> http_method;
172 std::optional<std::string> http_url;
173};
174
183 public:
187
190 const StreamableHttpServerTransport&) = delete;
191
192 std::string_view name() const noexcept override;
193 protocol::Json diagnostics() const override;
194 core::Result<core::Unit> send(TxMessage message) override;
195 core::Result<std::optional<RxMessage>> receive() override;
196 std::optional<StreamableHttpServerMessageContext> last_received_context()
197 const;
198 core::Result<core::Unit> close() override;
199 void wait_until_ready() override;
200
201 private:
202 class Impl;
203
204 std::unique_ptr<Impl> impl_;
205};
206
207} // namespace mcp::transport
Native transport-contract client for Streamable HTTP.
Definition http_transport.hpp:81
protocol::Json diagnostics() const override
Structured implementation diagnostics.
core::Result< core::Unit > close() override
Closes the transport and unblocks receive() where possible.
std::string_view name() const noexcept override
Human-readable transport name for diagnostics.
core::Result< core::Unit > send(TxMessage message) override
Sends one JSON-RPC message to the peer.
core::Result< std::optional< RxMessage > > receive() override
Receives the next JSON-RPC message from the peer.
Native transport-contract server for Streamable HTTP.
Definition http_transport.hpp:182
std::string_view name() const noexcept override
Human-readable transport name for diagnostics.
Minimal message-level transport contract shared by MCP roles.
Definition transport.hpp:38
HTTP auth challenge observed by Streamable HTTP client transport.
Definition http_transport.hpp:22
Configuration for a Streamable HTTP client transport.
Definition http_transport.hpp:35
std::string uri
Full HTTP or HTTPS URI for the MCP endpoint.
Definition http_transport.hpp:39
std::unordered_map< std::string, std::string > headers
Extra request headers sent on every outbound HTTP request, including Streamable HTTP POST,...
Definition http_transport.hpp:52
int port
Remote TCP port.
Definition http_transport.hpp:45
std::string path
HTTP path used for MCP requests.
Definition http_transport.hpp:48
std::string host
Remote host name or IP address.
Definition http_transport.hpp:42
StreamableHttpAuthRefreshHandler auth_refresh_handler
Optional refresh hook invoked once for a 401 response before surfacing the auth failure.
Definition http_transport.hpp:62
std::chrono::milliseconds timeout
Connect, read, and write timeout used by the transport.
Definition http_transport.hpp:70
bool stateless
Enable stateless MCP HTTP mode.
Definition http_transport.hpp:67
std::optional< std::string > auth_header
Optional bearer token inserted as Authorization: Bearer <token> on every outbound HTTP request.
Definition http_transport.hpp:57
HTTP metadata captured for the most recently received server-side Streamable HTTP message.
Definition http_transport.hpp:167
Configuration for a Streamable HTTP server transport.
Definition http_transport.hpp:104
std::optional< std::chrono::milliseconds > sse_retry
Optional SSE retry interval hint for the priming event.
Definition http_transport.hpp:115
std::vector< std::string > allowed_origins
Optional Origin allow-list. Empty means Origin is not restricted.
Definition http_transport.hpp:127
std::function< std::optional< std::string >(const StreamableHttpAuthChallenge &)> StreamableHttpAuthRefreshHandler
Application hook used for one-shot bearer refresh on HTTP 401.
Definition http_transport.hpp:32
Role-generic MCP transport contract for SDK peer/service layers.