16namespace mcp::protocol {
21 static Json any() {
return Json::object(); }
23 static Json object() {
return Json{{
"type",
"object"}}; }
25 static Json string() {
return Json{{
"type",
"string"}}; }
27 static Json integer() {
return Json{{
"type",
"integer"}}; }
29 static Json number() {
return Json{{
"type",
"number"}}; }
31 static Json boolean() {
return Json{{
"type",
"boolean"}}; }
34 return Json{{
"type",
"array"}, {
"items", std::move(items)}};
37 static Json string_enum(std::vector<std::string> values) {
38 return Json{{
"type",
"string"}, {
"enum", std::move(values)}};
43template <
class T,
class Enable =
void>
48 static Json schema() {
return JsonSchema::any(); }
53 static Json schema() {
return JsonSchema::string(); }
58 static Json schema() {
return JsonSchema::boolean(); }
63 T, std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>>> {
64 static Json schema() {
return JsonSchema::integer(); }
68struct SchemaTraits<T, std::enable_if_t<std::is_floating_point_v<T>>> {
69 static Json schema() {
return JsonSchema::number(); }
74 static Json schema() {
return JsonSchema::integer(); }
83 static Json schema() {
return JsonSchema::array(schema_for<T>()); }
88 static Json schema() {
return schema_for<T>(); }
94template <
typename Struct,
typename Field>
95inline void add_reflected_field_schema(
98 if constexpr (is_optional_v<Field>) {
99 properties[fd.wire_name] = schema_for<typename Field::value_type>();
101 properties[fd.wire_name] = schema_for<Field>();
102 required.push_back(fd.wire_name);
106template <
typename Struct>
107inline void add_reflected_field_schema(Json&, Json&,
108 const ExtensionsField<Struct>&) {}
110template <
typename Struct,
typename Field>
111inline void add_reflected_field_schema(
112 Json&, Json&,
const DeserializeOnlyField<Struct, Field>&) {}
115inline Json reflect_schema() {
116 Json properties = Json::object();
117 Json required = Json::array();
120 [&](
const auto&... fds) {
121 (add_reflected_field_schema(properties, required, fds), ...);
123 reflect_fields<T>());
125 Json result = {{
"type",
"object"}, {
"properties", std::move(properties)}};
126 if (!required.empty()) {
127 result[
"required"] = std::move(required);
129 result[
"additionalProperties"] =
false;
141 using Decayed = std::decay_t<T>;
142 if constexpr (has_reflect_v<Decayed>) {
143 return detail::reflect_schema<Decayed>();
149template <
class T,
class =
void>
158 : std::bool_constant<has_reflect_v<std::decay_t<T>> ||
159 has_schema_traits<std::decay_t<T>>::value> {};
178 using Decayed = std::decay_t<T>;
180 is_tool_schema_type_v<Decayed>,
181 "cxxmcp typed tool argument/result type is a class or struct but is "
182 "not reflectable. Add CXXMCP_REFLECT_SELF(Type, ...) inside the type, "
183 "CXXMCP_REFLECT(Type, ...) after it, or provide nlohmann JSON conversion "
184 "plus an explicit mcp::protocol::SchemaTraits<T> specialization.");
185 auto schema = schema_for<Decayed>();
186 if (schema.is_object() && schema.value(
"type", std::string{}) ==
"object") {
189 if constexpr (std::is_same_v<Decayed, Json>) {
190 return JsonSchema::object();
192 return Json{{
"type",
"object"},
193 {
"properties",
Json{{
"value", std::move(schema)}}},
194 {
"required", Json::array({
"value"})},
195 {
"additionalProperties",
false}};
202 return tool_input_schema_for<T>();
209 schema_[
"type"] =
"object";
210 schema_[
"properties"] = Json::object();
214 schema_[
"title"] = std::move(value);
219 schema_[
"description"] = std::move(value);
224 schema_[
"properties"][std::move(name)] = std::move(schema);
229 return property(std::move(name), std::move(schema));
233 schema_[
"required"].push_back(std::move(name));
238 const auto required_name = name;
239 property(std::move(name), std::move(schema));
240 return required(required_name);
244 schema_[
"additionalProperties"] = value;
248 Json build()
const& {
return schema_; }
250 Json build() && {
return std::move(schema_); }
253 Json schema_ = Json::object();
Primitive JSON Schema helpers used by public SDK builders.
Definition schema.hpp:19
Fluent object-schema builder for tool input and output schemas.
Definition schema.hpp:206
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.
Json tool_input_schema_for()
Returns the root object schema required by MCP tool arguments.
Definition schema.hpp:177
Json schema_for()
Returns the JSON Schema advertised for a C++ type.
Definition schema.hpp:140
Json tool_output_schema_for()
Returns the root object schema required by MCP structuredContent.
Definition schema.hpp:201
ObjectSchemaBuilder object_schema()
Creates a fluent object-schema builder.
Definition schema.hpp:257
Describes a single DTO field: its JSON wire name, pointer-to-member, and optional post-deserializatio...
Definition reflect.hpp:41
Type-to-schema customization point for typed SDK helpers.
Definition schema.hpp:44
Definition schema.hpp:150