cxxmcp 1.3.0
C++ MCP SDK
Loading...
Searching...
No Matches
transport.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
5#include <functional>
6#include <memory>
7#include <optional>
8#include <string>
9#include <string_view>
10#include <unordered_map>
11
17
25
26namespace mcp::server {
27
28class Transport;
29class ClientPeer;
36
45 std::string session_id;
47 std::string remote_address;
49 std::unordered_map<std::string, std::string> headers;
51 std::optional<std::string> http_method;
53 std::optional<std::string> http_url;
55 std::optional<AuthIdentity> auth_identity;
57 Transport* transport = nullptr;
61 std::weak_ptr<void> transport_lifetime;
62
66 SessionClient client() const noexcept;
67};
68
74using RequestHandler = std::function<core::Result<protocol::JsonRpcResponse>(
75 const protocol::JsonRpcRequest&, const SessionContext&)>;
76
84inline std::string_view request_wire_version(
85 const protocol::JsonRpcRequest& request,
86 const SessionContext& context) noexcept {
87 if (request.meta.has_value() && request.meta->is_object() &&
88 request.meta->contains("io.modelcontextprotocol/protocolVersion") &&
89 request.meta->at("io.modelcontextprotocol/protocolVersion").is_string()) {
90 return request.meta->at("io.modelcontextprotocol/protocolVersion")
91 .get_ref<const std::string&>();
92 }
93 if (request.protocol_version_override.has_value()) {
94 return *request.protocol_version_override;
95 }
96 for (const auto& [name, value] : context.headers) {
97 if (name.size() == 20) {
98 char lower[21];
99 for (std::size_t i = 0; i < name.size(); ++i) {
100 const char c = name[i];
101 lower[i] = static_cast<char>(c >= 'A' && c <= 'Z' ? c - 'A' + 'a' : c);
102 }
103 lower[name.size()] = '\0';
104 if (std::string_view(lower) == "mcp-protocol-version") {
105 return value;
106 }
107 }
108 }
109 return protocol::McpProtocolVersion;
110}
111
115 std::string_view version) noexcept {
116 return version == protocol::McpProtocolVersion2026_07_28 ||
117 version == "DRAFT-2026-v1";
118}
119
122inline bool method_requires_cache_hints(std::string_view method) noexcept {
123 return method == protocol::ServerDiscoverMethod ||
124 method == protocol::ToolsListMethod ||
125 method == protocol::PromptsListMethod ||
126 method == protocol::ResourcesListMethod ||
127 method == protocol::ResourcesTemplatesListMethod ||
128 method == protocol::ResourcesReadMethod;
129}
130
138using NotificationHandler = std::function<core::Result<core::Unit>(
140
155 public:
156 virtual ~Transport() = default;
157
159 std::weak_ptr<void> lifetime_token() const noexcept {
160 return lifetime_token_;
161 }
162
170 RequestHandler handler,
171 NotificationHandler notification_handler = {}) = 0;
172
180 const protocol::JsonRpcRequest& request) {
181 (void)request;
182 return mcp::core::unexpected(core::Error{
183 static_cast<int>(protocol::ErrorCode::MethodNotFound),
184 "transport does not support outbound requests",
185 {},
186 });
187 }
188
196 std::string_view session_id, const protocol::JsonRpcRequest& request) {
197 (void)session_id;
198 return send_request(request);
199 }
200
204 virtual std::optional<protocol::ClientCapabilities> client_capabilities()
205 const {
206 return std::nullopt;
207 }
208
214 virtual std::optional<protocol::ClientCapabilities>
215 client_capabilities_for_session(std::string_view session_id) const {
216 (void)session_id;
217 return client_capabilities();
218 }
219
225 const protocol::JsonRpcNotification& notification) = 0;
226
233 std::string_view session_id,
234 const protocol::JsonRpcNotification& notification) {
235 (void)session_id;
236 return send_notification(notification);
237 }
238
249 virtual void publish_subscription_notification(std::string_view method,
250 protocol::Json params) {
251 (void)method;
252 (void)params;
253 }
254
259 virtual void stop() noexcept = 0;
260
263 virtual std::string_view name() const noexcept = 0;
264
270 virtual void wait_until_ready() {}
271
272 private:
273 std::shared_ptr<void> lifetime_token_ = std::make_shared<int>(0);
274};
275
276} // namespace mcp::server
MCP client and server capability declarations.
Non-owning handle for the client associated with a server session.
Definition peer.hpp:46
Abstract server transport for receiving client JSON-RPC messages.
Definition transport.hpp:154
std::weak_ptr< void > lifetime_token() const noexcept
Weak lifetime token for SessionContext and ClientPeer guards.
Definition transport.hpp:159
virtual core::Result< core::Unit > send_notification_to_session(std::string_view session_id, const protocol::JsonRpcNotification &notification)
Send a JSON-RPC notification to a specific logical session.
Definition transport.hpp:232
virtual core::Result< protocol::JsonRpcResponse > send_request_to_session(std::string_view session_id, const protocol::JsonRpcRequest &request)
Send a JSON-RPC request to a specific logical session.
Definition transport.hpp:195
virtual core::Result< core::Unit > start(RequestHandler handler, NotificationHandler notification_handler={})=0
Start accepting inbound messages and dispatch them to handlers.
virtual void stop() noexcept=0
Request transport shutdown.
virtual core::Result< core::Unit > send_notification(const protocol::JsonRpcNotification &notification)=0
Send a JSON-RPC notification from the server to the client.
virtual void publish_subscription_notification(std::string_view method, protocol::Json params)
Publish a notification to open subscriptions/listen streams (SEP-2575).
Definition transport.hpp:249
virtual std::optional< protocol::ClientCapabilities > client_capabilities_for_session(std::string_view session_id) const
Return capabilities learned for a specific logical session.
Definition transport.hpp:215
virtual core::Result< protocol::JsonRpcResponse > send_request(const protocol::JsonRpcRequest &request)
Send a JSON-RPC request from the server to the connected client.
Definition transport.hpp:179
virtual std::optional< protocol::ClientCapabilities > client_capabilities() const
Return capabilities learned from the client's initialize request.
Definition transport.hpp:204
Shared JSON, JSON-RPC, error, cancellation, and progress model types.
nlohmann::json Json
JSON value type used by all protocol DTOs.
Definition types.hpp:28
Shared result and error primitives used by the public cxxmcp SDK.
tl::expected< T, Error > Result
Alias for the SDK result type.
Definition result.hpp:64
JSON-RPC method names and message construction/parsing helpers.
Authentication extension points for server transports.
std::function< core::Result< protocol::JsonRpcResponse >(const protocol::JsonRpcRequest &, const SessionContext &)> RequestHandler
Callback used by transports to dispatch inbound JSON-RPC requests.
Definition transport.hpp:75
bool wire_version_requires_result_type(std::string_view version) noexcept
True when the wire version requires resultType on result objects (SEP-2322 / MRTR,...
Definition transport.hpp:114
bool method_requires_cache_hints(std::string_view method) noexcept
True when a result for method must carry the SEP-2549 caching hints (ttlMs, cacheScope) on the statel...
Definition transport.hpp:122
std::function< core::Result< core::Unit >(const protocol::JsonRpcNotification &, const SessionContext &)> NotificationHandler
Callback used by transports to dispatch inbound JSON-RPC notifications.
Definition transport.hpp:139
Structured error returned by fallible SDK operations.
Definition result.hpp:35
JSON-RPC notification envelope for one-way MCP messages.
Definition types.hpp:137
JSON-RPC request envelope carrying an MCP method invocation.
Definition types.hpp:99
Per-message connection metadata supplied to server handlers.
Definition transport.hpp:43
std::optional< std::string > http_method
HTTP request method for HTTP-based transports.
Definition transport.hpp:51
std::optional< AuthIdentity > auth_identity
Authenticated principal produced by the configured AuthProvider.
Definition transport.hpp:55
std::optional< std::string > http_url
Absolute HTTP request URL for HTTP-based transports.
Definition transport.hpp:53
std::weak_ptr< void > transport_lifetime
Weak lifetime token supplied by the transport.
Definition transport.hpp:61
std::string remote_address
Best-effort remote endpoint description such as "stdio" or an address.
Definition transport.hpp:47
SessionClient client() const noexcept
Return a non-owning handle for the client on this session.
Definition peer.hpp:874
Transport * transport
Borrowed transport used to create a ClientPeer for outbound messages.
Definition transport.hpp:57
std::string session_id
Logical MCP session id when the transport has one.
Definition transport.hpp:45
std::unordered_map< std::string, std::string > headers
Transport-supplied request headers or metadata for this message.
Definition transport.hpp:49