-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmcp_dry_run.cpp
More file actions
65 lines (57 loc) · 1.87 KB
/
Copy pathmcp_dry_run.cpp
File metadata and controls
65 lines (57 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "mcp_dry_run.hpp"
namespace flapi {
bool MCPDryRun::extractFlag(crow::json::wvalue& arguments) {
if (arguments.t() != crow::json::type::Object) {
return false;
}
auto keys = arguments.keys();
bool present = false;
for (const auto& k : keys) {
if (k == kFlagKey) {
present = true;
break;
}
}
if (!present) {
return false;
}
// We have to round-trip via the rvalue type to read the value back, since
// crow::json::wvalue does not expose getters for individual children.
auto dumped = arguments.dump();
auto parsed = crow::json::load(dumped);
bool flag_value = false;
if (parsed && parsed.has(kFlagKey)) {
const auto& node = parsed[kFlagKey];
if (node.t() == crow::json::type::True) {
flag_value = true;
}
}
// Rebuild the wvalue without the reserved key so downstream validators
// never observe `_dryRun` as an unknown parameter.
crow::json::wvalue rebuilt;
if (parsed) {
for (const auto& key : parsed.keys()) {
if (key == kFlagKey) {
continue;
}
rebuilt[key] = parsed[key];
}
}
arguments = std::move(rebuilt);
return flag_value;
}
std::string MCPDryRun::formatResult(const std::string& tool_name,
const std::string& rendered_sql,
const std::map<std::string, std::string>& parameters) {
crow::json::wvalue payload;
payload["dry_run"] = true;
payload["tool_name"] = tool_name;
payload["rendered_sql"] = rendered_sql;
crow::json::wvalue params_obj = crow::json::wvalue::object();
for (const auto& [k, v] : parameters) {
params_obj[k] = v;
}
payload["parameters"] = std::move(params_obj);
return payload.dump();
}
} // namespace flapi