feat: Add initial MCP commands to query, open select and read data

This commit is contained in:
WerWolv
2025-12-17 16:03:57 +01:00
parent 2047a41498
commit d775b80a44
9 changed files with 265 additions and 2 deletions

View File

@@ -6,6 +6,40 @@
namespace hex::mcp {
struct TextContent {
std::string text;
operator nlohmann::json() const {
nlohmann::json result;
result["content"] = nlohmann::json::array({
nlohmann::json::object({
{ "type", "text" },
{ "text", text }
})
});
return result;
}
};
struct StructuredContent {
std::string text;
nlohmann::json data;
operator nlohmann::json() const {
nlohmann::json result;
result["content"] = nlohmann::json::array({
nlohmann::json::object({
{ "type", "text" },
{ "text", text }
})
});
result["structuredContent"] = data;
return result;
}
};
class Server {
public:
constexpr static auto McpInternalPort = 19743;

View File

@@ -56,7 +56,7 @@ namespace hex::mcp {
if (!m_id.has_value())
return std::nullopt;
return createResponseMessage(result);
return createResponseMessage(result.is_null() ? nlohmann::json::object() : result);
}
std::optional<nlohmann::json> handleBatchedMessages(const nlohmann::json &request, auto callback) {
@@ -154,7 +154,9 @@ namespace hex::mcp {
if (auto primitiveIt = m_primitives.find(primitive); primitiveIt != m_primitives.end()) {
auto name = params.value("name", "");
if (auto functionIt = primitiveIt->second.find(name); functionIt != primitiveIt->second.end()) {
return functionIt->second.function(params.value("arguments", nlohmann::json::object()));
auto result = functionIt->second.function(params.value("arguments", nlohmann::json::object()));
return result.is_null() ? nlohmann::json::object() : result;
}
}
}