cxxmcp 1.3.0
C++ MCP SDK
Loading...
Searching...
No Matches
serialization.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
12
13#include <array>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <variant>
18
22
23namespace mcp::protocol {
24
25#define CXXMCP_PROTOCOL_STRING_CONSTANT(name, value) \
26 inline constexpr core::StringConstant name { value }
27
29CXXMCP_PROTOCOL_STRING_CONSTANT(JsonRpcVersion, "2.0");
30CXXMCP_PROTOCOL_STRING_CONSTANT(McpProtocolVersion2026_07_28, "2026-07-28");
31CXXMCP_PROTOCOL_STRING_CONSTANT(McpProtocolVersion2025_11_25, "2025-11-25");
32CXXMCP_PROTOCOL_STRING_CONSTANT(McpProtocolVersion2025_06_18, "2025-06-18");
33CXXMCP_PROTOCOL_STRING_CONSTANT(McpProtocolVersion2025_03_26, "2025-03-26");
34CXXMCP_PROTOCOL_STRING_CONSTANT(McpProtocolVersion2024_11_05, "2024-11-05");
36inline constexpr core::StringConstant McpProtocolVersion{"2025-11-25"};
38inline constexpr std::array<const char*, 6> McpSupportedProtocolVersions{
39 "2024-11-05", "2025-03-26", "2025-06-18",
40 "2025-11-25", "2026-07-28", "DRAFT-2026-v1"};
41
43inline bool is_supported_protocol_version(std::string_view version) noexcept {
44 for (const auto* supported : McpSupportedProtocolVersions) {
45 if (version == std::string_view(supported)) {
46 return true;
47 }
48 }
49 return false;
50}
51
53inline std::optional<std::string_view> negotiate_protocol_version(
54 std::string_view requested) noexcept {
55 for (const auto* supported : McpSupportedProtocolVersions) {
56 if (requested == std::string_view(supported)) {
57 return std::string_view(supported);
58 }
59 }
60 return std::nullopt;
61}
62
64CXXMCP_PROTOCOL_STRING_CONSTANT(InitializeMethod, "initialize");
66CXXMCP_PROTOCOL_STRING_CONSTANT(InitializedMethod, "notifications/initialized");
68CXXMCP_PROTOCOL_STRING_CONSTANT(PingMethod, "ping");
70CXXMCP_PROTOCOL_STRING_CONSTANT(ServerDiscoverMethod, "server/discover");
72CXXMCP_PROTOCOL_STRING_CONSTANT(PromptsListMethod, "prompts/list");
74CXXMCP_PROTOCOL_STRING_CONSTANT(PromptsGetMethod, "prompts/get");
76CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesListMethod, "resources/list");
78CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesReadMethod, "resources/read");
80CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesTemplatesListMethod,
81 "resources/templates/list");
83CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesSubscribeMethod,
84 "resources/subscribe");
86CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesUnsubscribeMethod,
87 "resources/unsubscribe");
89CXXMCP_PROTOCOL_STRING_CONSTANT(ToolsListMethod, "tools/list");
91CXXMCP_PROTOCOL_STRING_CONSTANT(ToolsGetMethod, "tools/get");
93CXXMCP_PROTOCOL_STRING_CONSTANT(ToolsCallMethod, "tools/call");
95CXXMCP_PROTOCOL_STRING_CONSTANT(CompletionCompleteMethod,
96 "completion/complete");
98CXXMCP_PROTOCOL_STRING_CONSTANT(LoggingSetLevelMethod, "logging/setLevel");
100CXXMCP_PROTOCOL_STRING_CONSTANT(SamplingCreateMessageMethod,
101 "sampling/createMessage");
103CXXMCP_PROTOCOL_STRING_CONSTANT(ElicitationCreateMethod, "elicitation/create");
105CXXMCP_PROTOCOL_STRING_CONSTANT(ElicitationCompleteNotificationMethod,
106 "notifications/elicitation/complete");
108CXXMCP_PROTOCOL_STRING_CONSTANT(TasksListMethod, "tasks/list");
110CXXMCP_PROTOCOL_STRING_CONSTANT(TasksGetMethod, "tasks/get");
112CXXMCP_PROTOCOL_STRING_CONSTANT(TasksCancelMethod, "tasks/cancel");
114CXXMCP_PROTOCOL_STRING_CONSTANT(TasksResultMethod, "tasks/result");
116CXXMCP_PROTOCOL_STRING_CONSTANT(TasksUpdateMethod, "tasks/update");
118CXXMCP_PROTOCOL_STRING_CONSTANT(TasksCreateMethod, "tasks/create");
120CXXMCP_PROTOCOL_STRING_CONSTANT(SubscriptionsListenMethod,
121 "subscriptions/listen");
124CXXMCP_PROTOCOL_STRING_CONSTANT(TasksExtensionId,
125 "io.modelcontextprotocol/tasks");
128CXXMCP_PROTOCOL_STRING_CONSTANT(SkillsExtensionId,
129 "io.modelcontextprotocol/skills");
131CXXMCP_PROTOCOL_STRING_CONSTANT(TasksStatusNotificationMethod,
132 "notifications/tasks/status");
135CXXMCP_PROTOCOL_STRING_CONSTANT(SubscriptionsAcknowledgedNotificationMethod,
136 "notifications/subscriptions/acknowledged");
138CXXMCP_PROTOCOL_STRING_CONSTANT(RootsListMethod, "roots/list");
140CXXMCP_PROTOCOL_STRING_CONSTANT(CancelledNotificationMethod,
141 "notifications/cancelled");
143CXXMCP_PROTOCOL_STRING_CONSTANT(ProgressNotificationMethod,
144 "notifications/progress");
146CXXMCP_PROTOCOL_STRING_CONSTANT(RootsListChangedNotificationMethod,
147 "notifications/roots/list_changed");
149CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesListChangedNotificationMethod,
150 "notifications/resources/list_changed");
152CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesUpdatedNotificationMethod,
153 "notifications/resources/updated");
155CXXMCP_PROTOCOL_STRING_CONSTANT(ToolsListChangedNotificationMethod,
156 "notifications/tools/list_changed");
158CXXMCP_PROTOCOL_STRING_CONSTANT(PromptsListChangedNotificationMethod,
159 "notifications/prompts/list_changed");
161CXXMCP_PROTOCOL_STRING_CONSTANT(LoggingMessageNotificationMethod,
162 "notifications/message");
163
164#undef CXXMCP_PROTOCOL_STRING_CONSTANT
165
171ErrorObject make_error(int code, std::string message,
172 std::optional<Json> data = std::nullopt);
173
179ErrorObject make_error(ErrorCode code, std::string message,
180 std::optional<Json> data = std::nullopt);
181
187
192JsonRpcResponse make_error_response(std::optional<RequestId> id,
193 ErrorObject error);
194
200JsonRpcRequest make_request(std::string method, RequestId id,
201 Json params = Json::object());
202
208 Json params = Json::object());
209
215 Json params = Json::object());
216
221
226JsonRpcRequest make_ping_request(RequestId id, Json params = Json::object());
227
233
238
251 const JsonRpcNotification& notification);
257 const ErrorObject& error, std::optional<RequestId> id = std::nullopt);
258
259} // namespace mcp::protocol
String-like literal wrapper safe for inline public constants.
Definition string_constant.hpp:23
Shared JSON, JSON-RPC, error, cancellation, and progress model types.
ErrorCode
JSON-RPC and MCP-specific error codes used in ErrorObject.
Definition types.hpp:65
nlohmann::json Json
JSON value type used by all protocol DTOs.
Definition types.hpp:28
std::variant< JsonRpcRequest, JsonRpcResponse, JsonRpcNotification > JsonRpcMessage
Variant over the JSON-RPC message shapes accepted by MCP transports.
Definition types.hpp:148
std::variant< std::int64_t, std::string > RequestId
JSON-RPC request or response identifier.
Definition types.hpp:56
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
core::Result< std::string > serialize_notification(const JsonRpcNotification &notification)
Serializes a JSON-RPC notification envelope to text.
core::Result< JsonRpcNotification > parse_notification(std::string_view text)
Parses a JSON-RPC notification envelope from text.
core::Result< std::string > serialize_response(const JsonRpcResponse &response)
Serializes a JSON-RPC response envelope to text.
constexpr core::StringConstant McpProtocolVersion
Latest MCP protocol version advertised during initialization.
Definition serialization.hpp:36
JsonRpcRequest make_ping_request(RequestId id, Json params=Json::object())
Builds a ping liveness request.
JsonRpcNotification make_notification(std::string method, Json params=Json::object())
Builds a generic JSON-RPC notification envelope.
JsonRpcResponse make_error_response(std::optional< RequestId > id, ErrorObject error)
Builds an error JSON-RPC response.
ErrorObject make_error(int code, std::string message, std::optional< Json > data=std::nullopt)
Builds a JSON-RPC error object.
bool is_supported_protocol_version(std::string_view version) noexcept
Returns true when a peer protocol version is supported.
Definition serialization.hpp:43
core::Result< JsonRpcRequest > parse_request(std::string_view text)
Parses a JSON-RPC request envelope from text.
core::Result< std::string > serialize_request(const JsonRpcRequest &request)
Serializes a JSON-RPC request envelope to text.
JsonRpcRequest make_initialize_request(RequestId id, Json params=Json::object())
Builds an initialize lifecycle request.
core::Result< JsonRpcResponse > parse_response(std::string_view text)
Parses a JSON-RPC response envelope from text.
core::Result< JsonRpcMessage > parse_message(std::string_view text)
Parses any JSON-RPC message shape from text.
core::Result< std::string > serialize_error(const ErrorObject &error, std::optional< RequestId > id=std::nullopt)
Serializes a JSON-RPC error response to text.
constexpr std::array< const char *, 6 > McpSupportedProtocolVersions
Protocol versions accepted by this SDK during initialization.
Definition serialization.hpp:38
std::optional< std::string_view > negotiate_protocol_version(std::string_view requested) noexcept
Returns the peer-requested version when known.
Definition serialization.hpp:53
JsonRpcResponse make_response(RequestId id, Json result)
Builds a successful JSON-RPC response.
core::Result< std::string > serialize_message(const JsonRpcMessage &message)
Serializes any JSON-RPC message shape to JSON text.
JsonRpcRequest make_request(std::string method, RequestId id, Json params=Json::object())
Builds a generic JSON-RPC request envelope.
JsonRpcNotification make_initialized_notification(Json params=Json::object())
Builds an initialized lifecycle notification.
Zero-dynamic-initialization string constants for public headers.
JSON-RPC error object.
Definition types.hpp:89
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
JSON-RPC response envelope for either success or failure.
Definition types.hpp:115