cxxmcp 1.1.6
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(McpProtocolVersion2025_11_25, "2025-11-25");
31CXXMCP_PROTOCOL_STRING_CONSTANT(McpProtocolVersion2025_06_18, "2025-06-18");
32CXXMCP_PROTOCOL_STRING_CONSTANT(McpProtocolVersion2025_03_26, "2025-03-26");
33CXXMCP_PROTOCOL_STRING_CONSTANT(McpProtocolVersion2024_11_05, "2024-11-05");
35inline constexpr core::StringConstant McpProtocolVersion{"2025-11-25"};
37inline constexpr std::array<const char*, 5> McpSupportedProtocolVersions{
38 "2024-11-05", "2025-03-26", "2025-06-18", "2025-11-25", "DRAFT-2026-v1"};
39
41inline bool is_supported_protocol_version(std::string_view version) noexcept {
42 for (const auto* supported : McpSupportedProtocolVersions) {
43 if (version == std::string_view(supported)) {
44 return true;
45 }
46 }
47 return false;
48}
49
51inline std::optional<std::string_view> negotiate_protocol_version(
52 std::string_view requested) noexcept {
53 for (const auto* supported : McpSupportedProtocolVersions) {
54 if (requested == std::string_view(supported)) {
55 return std::string_view(supported);
56 }
57 }
58 return std::nullopt;
59}
60
62CXXMCP_PROTOCOL_STRING_CONSTANT(InitializeMethod, "initialize");
64CXXMCP_PROTOCOL_STRING_CONSTANT(InitializedMethod, "notifications/initialized");
66CXXMCP_PROTOCOL_STRING_CONSTANT(PingMethod, "ping");
68CXXMCP_PROTOCOL_STRING_CONSTANT(ServerDiscoverMethod, "server/discover");
70CXXMCP_PROTOCOL_STRING_CONSTANT(PromptsListMethod, "prompts/list");
72CXXMCP_PROTOCOL_STRING_CONSTANT(PromptsGetMethod, "prompts/get");
74CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesListMethod, "resources/list");
76CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesReadMethod, "resources/read");
78CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesTemplatesListMethod,
79 "resources/templates/list");
81CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesSubscribeMethod,
82 "resources/subscribe");
84CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesUnsubscribeMethod,
85 "resources/unsubscribe");
87CXXMCP_PROTOCOL_STRING_CONSTANT(ToolsListMethod, "tools/list");
89CXXMCP_PROTOCOL_STRING_CONSTANT(ToolsGetMethod, "tools/get");
91CXXMCP_PROTOCOL_STRING_CONSTANT(ToolsCallMethod, "tools/call");
93CXXMCP_PROTOCOL_STRING_CONSTANT(CompletionCompleteMethod,
94 "completion/complete");
96CXXMCP_PROTOCOL_STRING_CONSTANT(LoggingSetLevelMethod, "logging/setLevel");
98CXXMCP_PROTOCOL_STRING_CONSTANT(SamplingCreateMessageMethod,
99 "sampling/createMessage");
101CXXMCP_PROTOCOL_STRING_CONSTANT(ElicitationCreateMethod, "elicitation/create");
103CXXMCP_PROTOCOL_STRING_CONSTANT(ElicitationCompleteNotificationMethod,
104 "notifications/elicitation/complete");
106CXXMCP_PROTOCOL_STRING_CONSTANT(TasksListMethod, "tasks/list");
108CXXMCP_PROTOCOL_STRING_CONSTANT(TasksGetMethod, "tasks/get");
110CXXMCP_PROTOCOL_STRING_CONSTANT(TasksCancelMethod, "tasks/cancel");
112CXXMCP_PROTOCOL_STRING_CONSTANT(TasksResultMethod, "tasks/result");
114CXXMCP_PROTOCOL_STRING_CONSTANT(TasksCreateMethod, "tasks/create");
116CXXMCP_PROTOCOL_STRING_CONSTANT(TasksStatusNotificationMethod,
117 "notifications/tasks/status");
119CXXMCP_PROTOCOL_STRING_CONSTANT(RootsListMethod, "roots/list");
121CXXMCP_PROTOCOL_STRING_CONSTANT(CancelledNotificationMethod,
122 "notifications/cancelled");
124CXXMCP_PROTOCOL_STRING_CONSTANT(ProgressNotificationMethod,
125 "notifications/progress");
127CXXMCP_PROTOCOL_STRING_CONSTANT(RootsListChangedNotificationMethod,
128 "notifications/roots/list_changed");
130CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesListChangedNotificationMethod,
131 "notifications/resources/list_changed");
133CXXMCP_PROTOCOL_STRING_CONSTANT(ResourcesUpdatedNotificationMethod,
134 "notifications/resources/updated");
136CXXMCP_PROTOCOL_STRING_CONSTANT(ToolsListChangedNotificationMethod,
137 "notifications/tools/list_changed");
139CXXMCP_PROTOCOL_STRING_CONSTANT(PromptsListChangedNotificationMethod,
140 "notifications/prompts/list_changed");
142CXXMCP_PROTOCOL_STRING_CONSTANT(LoggingMessageNotificationMethod,
143 "notifications/message");
144
145#undef CXXMCP_PROTOCOL_STRING_CONSTANT
146
152ErrorObject make_error(int code, std::string message,
153 std::optional<Json> data = std::nullopt);
154
160ErrorObject make_error(ErrorCode code, std::string message,
161 std::optional<Json> data = std::nullopt);
162
168
173JsonRpcResponse make_error_response(std::optional<RequestId> id,
174 ErrorObject error);
175
181JsonRpcRequest make_request(std::string method, RequestId id,
182 Json params = Json::object());
183
189 Json params = Json::object());
190
196 Json params = Json::object());
197
202
207JsonRpcRequest make_ping_request(RequestId id, Json params = Json::object());
208
214
219
232 const JsonRpcNotification& notification);
238 const ErrorObject& error, std::optional<RequestId> id = std::nullopt);
239
240} // 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:35
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:41
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.
std::optional< std::string_view > negotiate_protocol_version(std::string_view requested) noexcept
Returns the peer-requested version when known.
Definition serialization.hpp:51
constexpr std::array< const char *, 5 > McpSupportedProtocolVersions
Protocol versions accepted by this SDK during initialization.
Definition serialization.hpp:37
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