修改了目录和部分源文件名称
This commit is contained in:
gjm committed 2026-10-02 21:02:40 +08:00
1 parent 1435b864f8
commit cf87cfe93d
21 files changed
+8 -7

No files matched your search

+154
View File
@@ -0,0 +1,154 @@
#include "mcp_ConfigParser.h"
#include "cadsdk.h"
#include <windows.h>
#include "mcp_tool.h"
#include "mcp_utils.h"
#include "ghc/filesystem.hpp"
#pragma region mcp_ConfigParser
bool mcp_ConfigParser::LoadAndRegister(const std::wstring &jsonFilePath, mcp_Tools &registry)
{
std::ifstream ifs(jsonFilePath);
if (!ifs.is_open()) return false;
nlohmann::json config;
try { ifs >> config; }
catch (...) { return false; } // 防御非法 JSON
fs::path pluginPath
= mcp_utils::relative2absolute(config["global_settings"].value("plugin_directory", "./mcp_plugins"));
fs::path lispPath
= mcp_utils::relative2absolute(config["global_settings"].value("lisp_directory", "./mcp_lisp"));
if (!fs::exists(lispPath))
AfxMessageBox(CString(_T("[MCP Server]Warning: The LISP script directory does not exist; the associated tools will not load!\n")) + pluginPath.c_str());
if (!fs::exists(pluginPath))
AfxMessageBox(CString(_T("[MCP Server]Warning: The plugin DLL directory does not exist; the associated tools will not load!\n")) + pluginPath.c_str());
for (const auto &toolConfig : config["tools"])
{
std::string type = toolConfig["backend"].value("type", "");
if (type == "lisp_inline")
{
auto pTool = std::make_shared<mcp_Tool_LispInline>();
pTool->SetConfig(toolConfig);
registry.Register(pTool);
}
else if (type == "lisp_file" && fs::exists(lispPath))
{
auto pTool = std::make_shared<mcp_Tool_LispFile>(lispPath);
pTool->SetConfig(toolConfig);
registry.Register(pTool);
}
else if (type == "c++_dll" && fs::exists(pluginPath))
{
LoadDllTool(toolConfig, pluginPath, registry);
}
}
return true;
}
void mcp_ConfigParser::LoadDllTool(const nlohmann::json &toolConfig, const std::wstring &pluginDir, mcp_Tools &registry)
{
fs::path dllPath
= mcp_utils::relative2absolute(toolConfig["backend"].value("dll_name", "")
, fs::path(pluginDir));
std::string factoryName = toolConfig["backend"].value("factory_function", "");
if (!fs::exists(dllPath))
{
AfxMessageBox(CString(_T("[MCP Server]Warning: The DLL file does not exist; the associated tools will not load!\n")) + dllPath.c_str());
return;
}
HMODULE hMod = LoadLibrary(dllPath.c_str());
if (!hMod) return;
typedef mcp_Tool *(*CreateFunc)();
typedef void (*DestroyFunc)(mcp_Tool *);
std::string destroyName = "Destroy_" + factoryName;
CreateFunc createFn = (CreateFunc)GetProcAddress(hMod, factoryName.c_str());
DestroyFunc destroyFn = (DestroyFunc)GetProcAddress(hMod, destroyName.c_str());
if (createFn && destroyFn)
{
mcp_Tool *pRealTool = createFn();
if (pRealTool)
{
// 给真正的 DLL 实例也注入配置,以防第三方需要在内部读取
pRealTool->SetConfig(toolConfig);
// 实例化代理类,接管生命周期
auto pProxy = std::make_shared<mcp_Tool_DllProxy>(hMod, pRealTool, destroyFn);
// 代理类继承自基类,也需要配置来处理 GetName 和 ToLLM
pProxy->SetConfig(toolConfig);
registry.Register(pProxy);
}
else
{
FreeLibrary(hMod);
}
}
else
{
FreeLibrary(hMod); // 导出函数找不到,直接卸载
}
}
#pragma endregion mcp_ConfigParser
#pragma region mcp_Tools
// 注册工具
void mcp_Tools::Register(std::shared_ptr<mcp_Tool> pTool)
{
if (pTool != nullptr)
{
m_tools[pTool->GetName()] = pTool;
}
}
// 组装所有工具的描述,返回 JSON 数组 (用于 tools/list)
nlohmann::json mcp_Tools::ToLLM() const
{
nlohmann::json arr = nlohmann::json::array();
for (const auto &pair : m_tools)
{
arr.push_back(pair.second->ToLLM());
}
return arr;
}
// 路由分发执行指令 (用于 tools/call)
nlohmann::json mcp_Tools::Call(const std::string &name, const nlohmann::json &args)
{
auto it = m_tools.find(name);
if (it != m_tools.end())
{
return it->second->Execute(args);
}
// 找不到工具时,返回标准的 JSON-RPC 错误对象
return mcp_Tool_Utility::make_error(-32601, "Specified tool not found: " + name);
}
void mcp_Tools::print()
{
for (const auto &pair : m_tools)
{
acutPrintf(_T("Tool: %s Already registered.\n"), mcp_utils::utf8ToWString(pair.first.c_str()).c_str());
}
}
void mcp_Tools::clear()
{
m_tools.clear();
}
#pragma endregion mcp_Tools