cxxmcp 1.1.6
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
65 std::chrono::milliseconds timeout{30000};
66};
67
77 public:
81
84 const StreamableHttpClientTransport&) = delete;
85
86 std::string_view name() const noexcept override;
87 protocol::Json diagnostics() const override;
88 core::Result<core::Unit> send(TxMessage message) override;
89 core::Result<std::optional<RxMessage>> receive() override;
90 core::Result<core::Unit> close() override;
91
92 private:
93 class Impl;
94
95 std::unique_ptr<Impl> impl_;
96};
97
101 std::string listen_host = "127.0.0.1";
102
104 int listen_port = 0;
105
107 std::string path = "/mcp";
108
110 std::optional<std::chrono::milliseconds> sse_retry;
111
115 bool enable_sse_polling = false;
116
119 std::chrono::milliseconds sse_disconnect_retry{5000};
120
122 std::vector<std::string> allowed_origins;
123
126 std::vector<std::string> allowed_hosts = {"localhost", "127.0.0.1", "::1"};
127
129 std::size_t max_pending_sse_events = 1024;
130
132 std::size_t max_pending_sse_bytes = 4 * 1024 * 1024;
133
135 std::size_t max_sse_replay_events = 256;
136
139 std::chrono::milliseconds request_timeout{30000};
140
143 std::size_t max_request_body_bytes = 10 * 1024 * 1024;
144
146 std::chrono::milliseconds read_timeout{30000};
147
149 std::chrono::milliseconds write_timeout{30000};
150
153 std::size_t max_sessions = 1024;
154
157 bool stateless = false;
158};
159
163 std::string session_id;
164 std::string remote_address;
165 std::unordered_map<std::string, std::string> headers;
166 std::optional<std::string> http_method;
167 std::optional<std::string> http_url;
168};
169
178 public:
182
185 const StreamableHttpServerTransport&) = delete;
186
187 std::string_view name() const noexcept override;
188 protocol::Json diagnostics() const override;
189 core::Result<core::Unit> send(TxMessage message) override;
190 core::Result<std::optional<RxMessage>> receive() override;
191 std::optional<StreamableHttpServerMessageContext> last_received_context()
192 const;
193 core::Result<core::Unit> close() override;
194 void wait_until_ready() override;
195
196 private:
197 class Impl;
198
199 std::unique_ptr<Impl> impl_;
200};
201
202} // namespace mcp::transport
Native transport-contract client for Streamable HTTP.
Definition http_transport.hpp:76
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:177
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:65
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:162
Configuration for a Streamable HTTP server transport.
Definition http_transport.hpp:99
std::optional< std::chrono::milliseconds > sse_retry
Optional SSE retry interval hint for the priming event.
Definition http_transport.hpp:110
std::vector< std::string > allowed_origins
Optional Origin allow-list. Empty means Origin is not restricted.
Definition http_transport.hpp:122
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.