源工程在 d:\new-start\CadProject\src\cad_mcp_frame(vcpkg 独立工程), 本次按 code2026 的 preset/CMake 体系移植到 SourceCode/Code2026/cad_mcp_frame。 移植改动: - 源码 + .def + .rc + res/ + cad-mcp-frame.json 拷入 cad_mcp_frame/; 插件接口头 mcp_plugin_api.h 放到 cad_mcp_frame/inc/ - cadsdk.h -> KTCad.h(两者除尾部 4 个 include 与 using namespace MemOper 外完全一致) - nlohmann/json 单头放 SourceCode/dep_inc/nlohmann/json.hpp, ghc::filesystem 单头放 SourceCode/dep_inc/ghc/filesystem.hpp; target_link_objectarx 已把 DEP_INC_ROOT 加进 include 路径,故无需额外配置 - 新增 cad_mcp_frame/cmakelists.txt:按 PipeHangerCmd 的写法 (add_def_file_name / target_set_name_and_suffix .arx / target_link_objectarx / target_include_directories inc / set_debug_post_build) - 补 SUB_ARX 宏:原工程由 CadProject 的 cmake 注入,code2026 只注入 ARX; 在模块自己的 CMake 里补,不动共享 functions.cmake(中望给 0) - 顶层挂载 add_custom_module_2(cad_mcp_frame CadMcpFrame "SH/Other"), 并加条件:nlohmann/json 与 ghc::filesystem 都需要 C++11+, 而 R180/R190 是 C++98,故只在 (IS_ZWCAD OR CAD_SDK_VERSION >= 20) 时编译 - 中望适配两处:_zrx.def 要导出 zcrxEntryPoint/zcrxGetApiVersion; 代码里显式声明的 acrxGetApiVersion() 不会被 SDK 宏改名, 改为 #ifdef ZWCAD 取 zcrxGetApiVersion() 验证: - R230 Debug 编译通过,cad_mcp_frame.arx 818 KB,自动部署到 c:\ShangHuaSoft\Envi3D-2026-M\R230\ - Z2026 Debug 编译通过,cad_mcp_frame.zrx 818 KB,自动部署到 ...\Z2026\ - R190/R180 按条件跳过(C++98 无法编译 nlohmann/json) - 所有 .cpp/.h 均为 UTF-8 with BOM;cmakelists.txt 保持 ASCII 无 BOM 未做:没有在 CAD 里加载实测;配置文件仍是 cad-mcp-frame.json, 而代码里找的是 mcp_config.json,加载前需要统一(详见 cad_mcp_frame-review.md); 插件工程 cad_mcp_plugins 未移植(18 个工具里 16 个依赖它)。
163 lines
4.5 KiB
C++
163 lines
4.5 KiB
C++
#pragma once
|
|
#include <string>
|
|
#include <nlohmann/json.hpp>
|
|
|
|
class mcp_Tool_Utility
|
|
{
|
|
public:
|
|
|
|
static std::wstring utf8ToWString(const std::string &utf8Str)
|
|
{
|
|
if (utf8Str.empty()) return std::wstring();
|
|
|
|
// 第一次调用获取需要的宽字符缓冲区大小
|
|
int sizeNeeded = ::MultiByteToWideChar(CP_UTF8, 0, &utf8Str[0], (int)utf8Str.size(), NULL, 0);
|
|
std::wstring wstrTo(sizeNeeded, 0);
|
|
|
|
// 第二次调用真正进行转换
|
|
::MultiByteToWideChar(CP_UTF8, 0, &utf8Str[0], (int)utf8Str.size(), &wstrTo[0], sizeNeeded);
|
|
return wstrTo;
|
|
}
|
|
|
|
static std::string WStringToUtf8(const std::wstring &wstr)
|
|
{
|
|
if (wstr.empty()) return std::string();
|
|
|
|
// 第一次调用获取需要的 UTF-8 缓冲区大小
|
|
int sizeNeeded = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
|
|
std::string strTo(sizeNeeded, 0);
|
|
|
|
// 第二次调用真正进行转换
|
|
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], sizeNeeded, NULL, NULL);
|
|
return strTo;
|
|
}
|
|
|
|
// 1. 纯文本响应
|
|
static nlohmann::json make_text_result(const std::wstring &_text)
|
|
{
|
|
std::string text = WStringToUtf8(_text);
|
|
return {
|
|
{ "result", {
|
|
{ "content", {
|
|
{ { "type", "text" }, { "text", text } }
|
|
} }
|
|
} }
|
|
};
|
|
}
|
|
|
|
// 2. 图片响应 (传入 base64 编码的图片数据)
|
|
static nlohmann::json make_image_result(const std::string &base64Data
|
|
, const std::string &mimeType = "image/png")
|
|
{
|
|
return {
|
|
{ "result", {
|
|
{ "content", {
|
|
{ { "type", "image" }, { "data", base64Data }, { "mimeType", mimeType } }
|
|
} }
|
|
} }
|
|
};
|
|
}
|
|
|
|
// 3. 图文混合响应 (文字汇报结果 + 附带 CAD 视口截图)
|
|
static nlohmann::json make_mixed_result(const std::wstring &_text
|
|
, const std::string &base64Data
|
|
, const std::string &mimeType = "image/png")
|
|
{
|
|
std::string text = WStringToUtf8(_text);
|
|
return {
|
|
{ "result", {
|
|
{ "content", {
|
|
{ { "type", "text" }, { "text", text } },
|
|
{ { "type", "image" }, { "data", base64Data }, { "mimeType", mimeType } }
|
|
} }
|
|
} }
|
|
};
|
|
}
|
|
|
|
static nlohmann::json make_error(int code, const std::wstring &_message)
|
|
{
|
|
std::string message = WStringToUtf8(_message);
|
|
return {
|
|
{ "error",{
|
|
{ "code", code },
|
|
{ "message", message }
|
|
} }
|
|
};
|
|
}
|
|
};
|
|
|
|
class mcp_Tool
|
|
{
|
|
protected:
|
|
nlohmann::json m_config;
|
|
std::string m_abiBuffer; // 存在于对象实例自己的堆内存中,确保指针生命周期安全
|
|
public:
|
|
virtual ~mcp_Tool() = default;
|
|
|
|
void SetConfig(const nlohmann::json &config) { m_config = config; }
|
|
|
|
// 兜底三剑客:当 JSON 中不写这些字段时,才调用这里(均可选重载)
|
|
virtual std::string private_name() const { return "unnamed_tool"; }
|
|
|
|
virtual std::string private_description() const { return ""; }
|
|
|
|
virtual nlohmann::json private_input_schema() const { return nlohmann::json::object(); }
|
|
|
|
// 统一由框架接管 Name 的获取 (供路由字典 mcp_Tools::Register 调用)
|
|
virtual std::string GetName() const final { return m_config.value("name", private_name()); }
|
|
|
|
// 统一由框架接管 ToLLM 组装
|
|
virtual nlohmann::json ToLLM() const final
|
|
{
|
|
nlohmann::json tool;
|
|
tool["name"] = GetName();
|
|
std::string desc = m_config.value("description", "");
|
|
tool["description"] = desc.empty() ? private_description() : desc;
|
|
|
|
if (m_config.contains("inputSchema") && !m_config["inputSchema"].empty())
|
|
{
|
|
tool["inputSchema"] = m_config["inputSchema"];
|
|
}
|
|
else
|
|
{
|
|
tool["inputSchema"] = private_input_schema();
|
|
}
|
|
return tool;
|
|
}
|
|
|
|
// 整个类唯一一个纯虚函数:全心全意干活!
|
|
virtual nlohmann::json Execute(const nlohmann::json &args) = 0;
|
|
|
|
// 安全配置注入
|
|
virtual void SetConfigAbiSafe(const char* config_json_str)
|
|
{
|
|
// 在 DLL 内部的安全环境中完成字符串到 JSON 的反序列化
|
|
m_config = nlohmann::json::parse(config_json_str);
|
|
}
|
|
|
|
// 安全执行路由
|
|
virtual const char* ExecuteAbiSafe(const char* args_json_str)
|
|
{
|
|
// 1. 在 DLL 的上下文中,将传入的 C 字符串转为 JSON
|
|
nlohmann::json args = nlohmann::json::parse(args_json_str);
|
|
|
|
// 2. 调用真实的业务逻辑
|
|
nlohmann::json result = Execute(args);
|
|
|
|
// 3. 将结果序列化为字符串,存入属于该 DLL 的内存缓冲区
|
|
m_abiBuffer = result.dump();
|
|
|
|
// 4. 返回安全的 C 指针
|
|
return m_abiBuffer.c_str();
|
|
}
|
|
};
|
|
|
|
#define EXPORT_MCP_TOOL(ToolClass, FactoryName) \
|
|
extern "C" __declspec(dllexport) mcp_Tool* FactoryName() \
|
|
{ \
|
|
return new ToolClass(); \
|
|
} \
|
|
extern "C" __declspec(dllexport) void Destroy_##FactoryName(mcp_Tool* pTool) \
|
|
{ \
|
|
delete pTool; \
|
|
} |