cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
types_reflect.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
10
11#include <cmath>
12
15
16namespace mcp::protocol {
17
18// ---------------------------------------------------------------------------
19// Finite-number validators for progress/total fields
20// ---------------------------------------------------------------------------
21
22inline core::Result<core::Unit> validate_finite_double(const double& value) {
23 if (!std::isfinite(value)) {
25 core::Error{static_cast<int>(ErrorCode::InvalidRequest),
26 "number must be finite",
27 {}});
28 }
29 return core::Unit{};
30}
31
32inline core::Result<core::Unit> validate_optional_finite_double(
33 const std::optional<double>& value) {
34 if (value.has_value() && !std::isfinite(*value)) {
36 core::Error{static_cast<int>(ErrorCode::InvalidRequest),
37 "number must be finite",
38 {}});
39 }
40 return core::Unit{};
41}
42
43// ---------------------------------------------------------------------------
44// Reflect<Icon>
45// ---------------------------------------------------------------------------
46
47template <>
48struct Reflect<Icon> {
49 static constexpr bool defined = true;
50 static auto fields() {
51 return std::make_tuple(
52 field("src", &Icon::src), defaulted_field("mimeType", &Icon::mime_type),
53 defaulted_field("sizes", &Icon::sizes), field("theme", &Icon::theme));
54 }
55 static std::vector<std::string> known_keys() {
56 return {"src", "mimeType", "sizes", "theme"};
57 }
58};
59
61
62// ---------------------------------------------------------------------------
63// Reflect<CancelledNotificationParams>
64// ---------------------------------------------------------------------------
65
66template <>
68 static constexpr bool defined = true;
69 static auto fields() {
70 return std::make_tuple(
73 }
74 static std::vector<std::string> known_keys() {
75 return {"requestId", "reason"};
76 }
77};
78
80
81// ---------------------------------------------------------------------------
82// Reflect<ProgressNotificationParams>
83// ---------------------------------------------------------------------------
84
85template <>
87 static constexpr bool defined = true;
88 static auto fields() {
89 return std::make_tuple(
92 validate_finite_double),
94 validate_optional_finite_double),
96 }
97 static std::vector<std::string> known_keys() {
98 return {"progressToken", "progress", "total", "message"};
99 }
100};
101
103
104// ---------------------------------------------------------------------------
105// Serialization wrappers (delegates to reflection)
106// ---------------------------------------------------------------------------
107
109inline Json icon_to_json(const Icon& icon) { return reflect_to_json(icon); }
110
114inline std::optional<Icon> icon_from_json(const Json& json) {
115 auto result = reflect_from_json<Icon>(json);
116 if (!result) {
117 return std::nullopt;
118 }
119 return std::move(*result);
120}
121
124 const CancelledNotificationParams& params) {
125 return reflect_to_json(params);
126}
127
129inline std::optional<CancelledNotificationParams>
131 auto result = reflect_from_json<CancelledNotificationParams>(json);
132 if (!result) {
133 return std::nullopt;
134 }
135 return std::move(*result);
136}
137
140 const ProgressNotificationParams& params) {
141 return reflect_to_json(params);
142}
143
145inline std::optional<ProgressNotificationParams>
147 auto result = reflect_from_json<ProgressNotificationParams>(json);
148 if (!result) {
149 return std::nullopt;
150 }
151 return std::move(*result);
152}
153
154} // namespace mcp::protocol
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
C++17 tuple-reflection infrastructure for zero-boilerplate DTO serialization.
#define CXXMCP_REFLECT_CHECK(Struct, expected_count)
Validates that a Reflect<> specialization covers the expected number of fields.
Definition reflect.hpp:1008
constexpr FieldDescriptor< Struct, Field > field(const char *wire_name, Field Struct::*pointer)
Creates a FieldDescriptor with wire name and pointer-to-member.
Definition reflect.hpp:75
constexpr FieldDescriptor< Struct, Field > defaulted_field(const char *wire_name, Field Struct::*pointer)
Creates a FieldDescriptor whose missing wire value keeps the C++ default value.
Definition reflect.hpp:145
Json reflect_to_json(const T &obj)
Serializes a DTO to JSON using its Reflect<T> trait.
Definition reflect.hpp:701
constexpr FieldDescriptor< Struct, Field > validated_field(const char *wire_name, Field Struct::*pointer, core::Result< core::Unit >(*validator)(const Field &))
Creates a FieldDescriptor with a post-deserialization validator.
Definition reflect.hpp:82
constexpr auto unexpected(E &&value)
Creates an unexpected result value for the active expected backend.
Definition result.hpp:24
Parameters for notifications/cancelled.
Definition types.hpp:218
RequestId request_id
Id of the JSON-RPC request being cancelled.
Definition types.hpp:220
std::optional< std::string > reason
Optional human-readable cancellation reason.
Definition types.hpp:222
Icon descriptor used by tools, resources, resource templates, and prompts.
Definition types.hpp:160
std::string src
Required icon source URI or data URI.
Definition types.hpp:162
std::string mime_type
Optional MIME type such as image/png or image/svg+xml.
Definition types.hpp:164
std::vector< std::string > sizes
Optional size hints such as 16x16, 32x32, or any.
Definition types.hpp:166
std::optional< IconTheme > theme
Optional theme specialization.
Definition types.hpp:168
Parameters for notifications/progress.
Definition types.hpp:226
double progress
Current progress value.
Definition types.hpp:230
std::optional< std::string > message
Optional human-readable status text.
Definition types.hpp:234
ProgressToken progress_token
Token supplied by the original request metadata.
Definition types.hpp:228
std::optional< double > total
Optional total value using the same unit as progress.
Definition types.hpp:232
Primary template.
Definition reflect.hpp:199
Json cancelled_notification_params_to_json(const CancelledNotificationParams &params)
Serializes cancellation notification parameters.
Definition types_reflect.hpp:123
Json progress_notification_params_to_json(const ProgressNotificationParams &params)
Serializes progress notification parameters.
Definition types_reflect.hpp:139
Json icon_to_json(const Icon &icon)
Serializes a shared icon descriptor.
Definition types_reflect.hpp:109
std::optional< Icon > icon_from_json(const Json &json)
Parses a shared icon descriptor.
Definition types_reflect.hpp:114
std::optional< CancelledNotificationParams > cancelled_notification_params_from_json(const Json &json)
Parses cancellation notification parameters.
Definition types_reflect.hpp:130
std::optional< ProgressNotificationParams > progress_notification_params_from_json(const Json &json)
Parses progress notification parameters.
Definition types_reflect.hpp:146