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>
45 static Json schema() {
return JsonSchema::object(); }
50 static Json schema() {
return JsonSchema::any(); }
55 static Json schema() {
return JsonSchema::string(); }
60 static Json schema() {
return JsonSchema::boolean(); }
65 T, std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>>> {
66 static Json schema() {
return JsonSchema::integer(); }
70struct SchemaTraits<T, std::enable_if_t<std::is_floating_point_v<T>>> {
71 static Json schema() {
return JsonSchema::number(); }
80 static Json schema() {
return JsonSchema::array(schema_for<T>()); }
85 static Json schema() {
return schema_for<T>(); }
91template <
typename Struct,
typename Field>
92inline void add_reflected_field_schema(
95 if constexpr (is_optional_v<Field>) {
96 properties[fd.wire_name] = schema_for<typename Field::value_type>();
98 properties[fd.wire_name] = schema_for<Field>();
99 required.push_back(fd.wire_name);
103template <
typename Struct>
104inline void add_reflected_field_schema(Json&, Json&,
105 const ExtensionsField<Struct>&) {}
107template <
typename Struct,
typename Field>
108inline void add_reflected_field_schema(
109 Json&, Json&,
const DeserializeOnlyField<Struct, Field>&) {}
112inline Json reflect_schema() {
113 Json properties = Json::object();
114 Json required = Json::array();
117 [&](
const auto&... fds) {
118 (add_reflected_field_schema(properties, required, fds), ...);
120 Reflect<T>::fields());
122 Json result = {{
"type",
"object"}, {
"properties", std::move(properties)}};
123 if (!required.empty()) {
124 result[
"required"] = std::move(required);
126 result[
"additionalProperties"] =
false;
138 using Decayed = std::decay_t<T>;
139 if constexpr (has_reflect_v<Decayed>) {
140 return detail::reflect_schema<Decayed>();
150 schema_[
"type"] =
"object";
151 schema_[
"properties"] = Json::object();
155 schema_[
"title"] = std::move(value);
160 schema_[
"description"] = std::move(value);
165 schema_[
"properties"][std::move(name)] = std::move(schema);
170 return property(std::move(name), std::move(schema));
174 schema_[
"required"].push_back(std::move(name));
179 const auto required_name = name;
180 property(std::move(name), std::move(schema));
181 return required(required_name);
185 schema_[
"additionalProperties"] = value;
189 Json build()
const& {
return schema_; }
191 Json build() && {
return std::move(schema_); }
194 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:147
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 schema_for()
Returns the JSON Schema advertised for a C++ type.
Definition schema.hpp:137
ObjectSchemaBuilder object_schema()
Creates a fluent object-schema builder.
Definition schema.hpp:198
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