From b99181a154cb153e783cfb5a6de66550084a4807 Mon Sep 17 00:00:00 2001 From: myifeng Date: Thu, 13 Nov 2025 10:49:46 +0800 Subject: [PATCH 1/2] feat(mcp_server): add description support for Property with backward compatibility --- main/mcp_server.h | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/main/mcp_server.h b/main/mcp_server.h index dacdd55153..324310bdc4 100644 --- a/main/mcp_server.h +++ b/main/mcp_server.h @@ -63,28 +63,58 @@ class Property { bool has_default_value_; std::optional min_value_; // 新增:整数最小值 std::optional max_value_; // 新增:整数最大值 + // 新增:字段描述(向后兼容,默认空) + std::string description_; public: // Required field constructor Property(const std::string& name, PropertyType type) - : name_(name), type_(type), has_default_value_(false) {} + : name_(name), type_(type), has_default_value_(false), description_("") {} // Optional field constructor with default value template Property(const std::string& name, PropertyType type, const T& default_value) - : name_(name), type_(type), has_default_value_(true) { + : name_(name), type_(type), has_default_value_(true), description_("") { + value_ = default_value; + } + + // --- 新增:带 description 的构造重载(向后兼容) + Property(const std::string& name, PropertyType type, const std::string& description) + : name_(name), type_(type), has_default_value_(false), description_(description) {} + + template + Property(const std::string& name, PropertyType type, const std::string& description, const T& default_value) + : name_(name), type_(type), has_default_value_(true), description_(description) { value_ = default_value; } Property(const std::string& name, PropertyType type, int min_value, int max_value) - : name_(name), type_(type), has_default_value_(false), min_value_(min_value), max_value_(max_value) { + : name_(name), type_(type), has_default_value_(false), min_value_(min_value), max_value_(max_value), description_("") { + if (type != kPropertyTypeInteger) { + throw std::invalid_argument("Range limits only apply to integer properties"); + } + } + + Property(const std::string& name, PropertyType type, int min_value, int max_value, const std::string& description) + : name_(name), type_(type), has_default_value_(false), min_value_(min_value), max_value_(max_value), description_(description) { if (type != kPropertyTypeInteger) { throw std::invalid_argument("Range limits only apply to integer properties"); } } Property(const std::string& name, PropertyType type, int default_value, int min_value, int max_value) - : name_(name), type_(type), has_default_value_(true), min_value_(min_value), max_value_(max_value) { + : name_(name), type_(type), has_default_value_(true), min_value_(min_value), max_value_(max_value), description_("") { + if (type != kPropertyTypeInteger) { + throw std::invalid_argument("Range limits only apply to integer properties"); + } + if (default_value < min_value || default_value > max_value) { + throw std::invalid_argument("Default value must be within the specified range"); + } + value_ = default_value; + } + + Property(const std::string& name, PropertyType type, int default_value, int min_value, int max_value, const std::string& description) + : name_(name), type_(type), has_default_value_(true), min_value_(min_value), max_value_(max_value), description_(description) { if (type != kPropertyTypeInteger) { throw std::invalid_argument("Range limits only apply to integer properties"); } @@ -95,6 +125,7 @@ class Property { } inline const std::string& name() const { return name_; } + inline const std::string& description() const { return description_; } inline PropertyType type() const { return type_; } inline bool has_default_value() const { return has_default_value_; } inline bool has_range() const { return min_value_.has_value() && max_value_.has_value(); } @@ -146,6 +177,11 @@ class Property { } } + // 如果有描述,则输出 description 字段(向后兼容:老工具 description_ 为空) + if (!description_.empty()) { + cJSON_AddStringToObject(json, "description", description_.c_str()); + } + char *json_str = cJSON_PrintUnformatted(json); std::string result(json_str); cJSON_free(json_str); From 631a017ae469f76ec450ee55229e8ce2de12f644 Mon Sep 17 00:00:00 2001 From: myifeng Date: Mon, 17 Nov 2025 10:01:11 +0800 Subject: [PATCH 2/2] Support property enum --- main/mcp_server.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/main/mcp_server.h b/main/mcp_server.h index 324310bdc4..eb7b2379b6 100644 --- a/main/mcp_server.h +++ b/main/mcp_server.h @@ -66,6 +66,9 @@ class Property { // 新增:字段描述(向后兼容,默认空) std::string description_; + std::vector enum_strings_; + std::vector enum_ints_; + public: // Required field constructor Property(const std::string& name, PropertyType type) @@ -124,6 +127,28 @@ class Property { value_ = default_value; } + // ================= 统一的 Enum 支持 ================= + + // 为字符串类型设置 enum 列表 + Property& SetEnum(const std::vector& enums) { + if (type_ != kPropertyTypeString) { + throw std::invalid_argument("SetEnum can only be applied to string properties"); + } + enum_strings_ = enums; + return *this; + } + + // 为整数类型设置 enum 列表 + Property& SetEnum(const std::vector& enums) { + if (type_ != kPropertyTypeInteger) { + throw std::invalid_argument("SetEnum can only be applied to integer properties"); + } + enum_ints_ = enums; + return *this; + } + + // ========================================== + inline const std::string& name() const { return name_; } inline const std::string& description() const { return description_; } inline PropertyType type() const { return type_; } @@ -170,11 +195,29 @@ class Property { if (max_value_.has_value()) { cJSON_AddNumberToObject(json, "maximum", max_value_.value()); } + + // 如果设置了 integer enum,则输出 enum(整型) + if (!enum_ints_.empty()) { + cJSON* arr = cJSON_CreateArray(); + for (int v : enum_ints_) { + cJSON_AddItemToArray(arr, cJSON_CreateNumber(v)); + } + cJSON_AddItemToObject(json, "enum", arr); + } } else if (type_ == kPropertyTypeString) { cJSON_AddStringToObject(json, "type", "string"); if (has_default_value_) { cJSON_AddStringToObject(json, "default", value().c_str()); } + + // 如果设置了 string enum,则输出 enum(字符串) + if (!enum_strings_.empty()) { + cJSON* arr = cJSON_CreateArray(); + for (const auto& s : enum_strings_) { + cJSON_AddItemToArray(arr, cJSON_CreateString(s.c_str())); + } + cJSON_AddItemToObject(json, "enum", arr); + } } // 如果有描述,则输出 description 字段(向后兼容:老工具 description_ 为空)