|
| 1 | +#include "mcp_response_shaper.hpp" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <crow/json.h> |
| 5 | +#include <set> |
| 6 | + |
| 7 | +namespace flapi { |
| 8 | + |
| 9 | +namespace { |
| 10 | + |
| 11 | +bool isRedacted(const std::set<std::string>& names, const std::string& key) { |
| 12 | + return names.find(key) != names.end(); |
| 13 | +} |
| 14 | + |
| 15 | +crow::json::wvalue cloneRvalue(const crow::json::rvalue& source) { |
| 16 | + return crow::json::wvalue(source); |
| 17 | +} |
| 18 | + |
| 19 | +crow::json::wvalue redactRow(const crow::json::rvalue& row, |
| 20 | + const std::set<std::string>& redact_set) { |
| 21 | + if (row.t() != crow::json::type::Object) { |
| 22 | + return cloneRvalue(row); |
| 23 | + } |
| 24 | + crow::json::wvalue out; |
| 25 | + for (const auto& key : row.keys()) { |
| 26 | + if (isRedacted(redact_set, key)) { |
| 27 | + out[key] = MCPResponseShaper::kRedactedSentinel; |
| 28 | + } else { |
| 29 | + out[key] = row[key]; |
| 30 | + } |
| 31 | + } |
| 32 | + return out; |
| 33 | +} |
| 34 | + |
| 35 | +std::vector<std::string> collectColumnNames(const crow::json::rvalue& array) { |
| 36 | + std::vector<std::string> names; |
| 37 | + if (array.t() != crow::json::type::List || array.size() == 0) { |
| 38 | + return names; |
| 39 | + } |
| 40 | + const auto& first = array[0]; |
| 41 | + if (first.t() != crow::json::type::Object) { |
| 42 | + return names; |
| 43 | + } |
| 44 | + for (const auto& key : first.keys()) { |
| 45 | + names.push_back(key); |
| 46 | + } |
| 47 | + return names; |
| 48 | +} |
| 49 | + |
| 50 | +std::string buildSamplePayload(const crow::json::rvalue& array) { |
| 51 | + crow::json::wvalue out; |
| 52 | + out["sampled"] = true; |
| 53 | + out["row_count"] = static_cast<int64_t>(array.size()); |
| 54 | + crow::json::wvalue columns = crow::json::wvalue::list(); |
| 55 | + auto names = collectColumnNames(array); |
| 56 | + for (size_t i = 0; i < names.size(); ++i) { |
| 57 | + columns[i] = names[i]; |
| 58 | + } |
| 59 | + out["columns"] = std::move(columns); |
| 60 | + return out.dump(); |
| 61 | +} |
| 62 | + |
| 63 | +} // namespace |
| 64 | + |
| 65 | +std::string MCPResponseShaper::shape(const std::string& json_payload, |
| 66 | + const ResponseShape& config) const { |
| 67 | + if (config.isNoOp()) { |
| 68 | + return json_payload; |
| 69 | + } |
| 70 | + |
| 71 | + auto parsed = crow::json::load(json_payload); |
| 72 | + if (!parsed || parsed.t() != crow::json::type::List) { |
| 73 | + // Non-array payloads (objects, primitives, malformed JSON) pass |
| 74 | + // through unchanged — we don't impose row-shape semantics on them. |
| 75 | + return json_payload; |
| 76 | + } |
| 77 | + |
| 78 | + if (config.sample) { |
| 79 | + return buildSamplePayload(parsed); |
| 80 | + } |
| 81 | + |
| 82 | + const std::set<std::string> redact_set( |
| 83 | + config.redact_columns.begin(), config.redact_columns.end()); |
| 84 | + |
| 85 | + const size_t cap = config.max_rows.has_value() ? *config.max_rows : parsed.size(); |
| 86 | + const size_t emit_count = std::min<size_t>(cap, parsed.size()); |
| 87 | + |
| 88 | + crow::json::wvalue out = crow::json::wvalue::list(); |
| 89 | + for (size_t i = 0; i < emit_count; ++i) { |
| 90 | + if (redact_set.empty()) { |
| 91 | + out[i] = parsed[i]; |
| 92 | + } else { |
| 93 | + out[i] = redactRow(parsed[i], redact_set); |
| 94 | + } |
| 95 | + } |
| 96 | + return out.dump(); |
| 97 | +} |
| 98 | + |
| 99 | +} // namespace flapi |
0 commit comments