MCPcopy Create free account
hub / github.com/Luce-Org/lucebox-hub / convert_param_value

Function convert_param_value

server/src/server/tool_parser.cpp:71–113  ·  view source on GitHub ↗

Convert a string value to its JSON-schema-typed equivalent.

Source from the content-addressed store, hash-verified

69 if (fn.contains("parameters") && fn["parameters"].is_object()) {
70 const auto & params = fn["parameters"];
71 if (params.contains("properties") && params["properties"].is_object()) {
72 return params["properties"];
73 }
74 }
75 }
76 return json::object();
77}
78
79// Convert a string value to its JSON-schema-typed equivalent.
80static json convert_param_value(const std::string & val, const std::string & key,
81 const json & props) {
82 if (val == "null") return nullptr;
83 if (!props.contains(key)) return val;
84
85 const auto & cfg = props[key];
86 std::string ptype = "string";
87 if (cfg.is_object() && cfg.contains("type")) {
88 const auto & t = cfg["type"];
89 if (t.is_string()) {
90 ptype = t.get<std::string>();
91 } else if (t.is_array()) {
92 // JSON Schema allows "type": ["string","null"]; take the first
93 // non-null string entry instead of throwing.
94 for (const auto & e : t) {
95 if (e.is_string() && e.get<std::string>() != "null") {
96 ptype = e.get<std::string>();
97 break;
98 }
99 }
100 }
101 }
102
103 // string types
104 if (ptype == "string" || ptype == "str" || ptype == "enum") return val;
105
106 // integer types
107 if (ptype.substr(0, 3) == "int" || ptype == "integer") {
108 try { return std::stol(val); } catch (...) { return val; }
109 }
110
111 // number / float
112 if (ptype == "number" || ptype.substr(0, 5) == "float") {
113 try {
114 double f = std::stod(val);
115 if (f == (double)(long)f) return (long)f;
116 return f;

Callers 1

parse_xml_paramsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected