#pragma once #include #include class mcp_Tool_Utility { public: // 1. 纯文本响应 static nlohmann::json make_text_result(const std::string &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::string &text , const std::string &base64Data , const std::string &mimeType = "image/png") { return { { "result", { { "content", { { { "type", "text" }, { "text", text } }, { { "type", "image" }, { "data", base64Data }, { "mimeType", mimeType } } } } } } }; } static nlohmann::json make_error(int code, const std::string &message) { return { { "error",{ { "code", code }, { "message", message } } } }; } }; class mcp_Tool { protected: nlohmann::json m_config; 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; }; #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; \ }