mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 13:37:42 -05:00
feat: Add initial MCP commands to query, open select and read data
This commit is contained in:
93
plugins/builtin/source/content/mcp_tools.cpp
Normal file
93
plugins/builtin/source/content/mcp_tools.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
#include <content/providers/file_provider.hpp>
|
||||
#include <hex/api/content_registry/communication_interface.hpp>
|
||||
#include <hex/api/imhex_api/provider.hpp>
|
||||
#include <hex/helpers/crypto.hpp>
|
||||
#include <romfs/romfs.hpp>
|
||||
#include <wolv/literals.hpp>
|
||||
|
||||
namespace hex::plugin::builtin {
|
||||
|
||||
using namespace wolv::literals;
|
||||
|
||||
void registerMCPTools() {
|
||||
ContentRegistry::MCP::registerTool(romfs::get("mcp/tools/open_file.json").string(), [](const nlohmann::json &data) -> nlohmann::json {
|
||||
auto filePath = data.at("file_path").get<std::string>();
|
||||
|
||||
auto provider = ImHexApi::Provider::createProvider("hex.builtin.provider.file", true);
|
||||
if (auto *fileProvider = dynamic_cast<FileProvider*>(provider.get()); fileProvider != nullptr) {
|
||||
fileProvider->setPath(filePath);
|
||||
|
||||
ImHexApi::Provider::openProvider(provider);
|
||||
}
|
||||
|
||||
return mcp::TextContent {
|
||||
.text = "File opened"
|
||||
};
|
||||
});
|
||||
|
||||
ContentRegistry::MCP::registerTool(romfs::get("mcp/tools/list_open_data_sources.json").string(), [](const nlohmann::json &) -> nlohmann::json {
|
||||
const auto &providers = ImHexApi::Provider::getProviders();
|
||||
nlohmann::json array = nlohmann::json::array();
|
||||
for (const auto &provider : providers) {
|
||||
nlohmann::json providerInfo;
|
||||
providerInfo["name"] = provider->getName();
|
||||
providerInfo["type"] = provider->getTypeName().get();
|
||||
providerInfo["size"] = provider->getSize();
|
||||
providerInfo["is_writable"] = provider->isWritable();
|
||||
providerInfo["handle"] = provider->getID();
|
||||
|
||||
array.push_back(providerInfo);
|
||||
}
|
||||
|
||||
nlohmann::json result;
|
||||
result["data_sources"] = array;
|
||||
|
||||
return mcp::StructuredContent {
|
||||
.text = result.dump(),
|
||||
.data = result
|
||||
};
|
||||
});
|
||||
|
||||
ContentRegistry::MCP::registerTool(romfs::get("mcp/tools/select_data_source.json").string(), [](const nlohmann::json &data) -> nlohmann::json {
|
||||
const auto &providers = ImHexApi::Provider::getProviders();
|
||||
auto handle = data.at("handle").get<u64>();
|
||||
|
||||
for (size_t i = 0; i < providers.size(); i++) {
|
||||
if (providers[i]->getID() == handle) {
|
||||
ImHexApi::Provider::setCurrentProvider(static_cast<i64>(i));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
nlohmann::json result = { "selected_handle", ImHexApi::Provider::get()->getID() };
|
||||
return mcp::StructuredContent {
|
||||
.text = result.dump(),
|
||||
.data = result
|
||||
};
|
||||
});
|
||||
|
||||
ContentRegistry::MCP::registerTool(romfs::get("mcp/tools/read_data.json").string(), [](const nlohmann::json &data) -> nlohmann::json {
|
||||
auto address = data.at("address").get<u64>();
|
||||
auto size = data.at("size").get<u64>();
|
||||
|
||||
size = std::min<size_t>(size, 16_MiB);
|
||||
|
||||
auto provider = ImHexApi::Provider::get();
|
||||
std::vector<u8> buffer(std::min(size, provider->getActualSize() - address));
|
||||
provider->read(address, buffer.data(), buffer.size());
|
||||
|
||||
auto base64 = crypt::encode64(buffer);
|
||||
|
||||
nlohmann::json result = {
|
||||
"handle", provider->getID(),
|
||||
"data", std::string(base64.begin(), base64.end()),
|
||||
"data_size", buffer.size()
|
||||
};
|
||||
return mcp::StructuredContent {
|
||||
.text = result.dump(),
|
||||
.data = result
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -40,6 +40,7 @@ namespace hex::plugin::builtin {
|
||||
void registerThemes();
|
||||
void registerBackgroundServices();
|
||||
void registerNetworkEndpoints();
|
||||
void registerMCPTools();
|
||||
void registerFileHandlers();
|
||||
void registerProjectHandlers();
|
||||
void registerAchievements();
|
||||
@@ -140,6 +141,7 @@ IMHEX_PLUGIN_SETUP_BUILTIN("Built-in", "WerWolv", "Default ImHex functionality")
|
||||
registerStyleHandlers();
|
||||
registerBackgroundServices();
|
||||
registerNetworkEndpoints();
|
||||
registerMCPTools();
|
||||
registerFileHandlers();
|
||||
registerProjectHandlers();
|
||||
registerCommandForwarders();
|
||||
|
||||
Reference in New Issue
Block a user