23namespace mcp::protocol {
25#define CXXMCP_PROTOCOL_STRING_CONSTANT(name, value) \
26 inline constexpr core::StringConstant name { value }
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");
39 "2024-11-05",
"2025-03-26",
"2025-06-18",
40 "2025-11-25",
"2026-07-28",
"DRAFT-2026-v1"};
45 if (version == std::string_view(supported)) {
54 std::string_view requested)
noexcept {
56 if (requested == std::string_view(supported)) {
57 return std::string_view(supported);
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");
164#undef CXXMCP_PROTOCOL_STRING_CONSTANT
172 std::optional<Json> data = std::nullopt);
180 std::optional<Json> data = std::nullopt);
201 Json params = Json::object());
208 Json params = Json::object());
215 Json params = Json::object());
257 const ErrorObject& error, std::optional<RequestId>
id = std::nullopt);
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 ¬ification)
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