From 1fdcdefef15d4a369a2d907b94ca14df677971b1 Mon Sep 17 00:00:00 2001 From: LittleGuo Date: Mon, 28 Sep 2026 22:13:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmakelists.txt | 2 +- src/AutoAgent/mcp.cpp | 18 +++++++++++++++--- test_autoagent.py | 20 ++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 test_autoagent.py diff --git a/cmakelists.txt b/cmakelists.txt index a9b21ba..1a618fe 100644 --- a/cmakelists.txt +++ b/cmakelists.txt @@ -86,7 +86,7 @@ endforeach() if(MSVC) # 🌟 开启 MSVC 多核并发编译!巨型工程提速的终极神器 add_compile_options(/MP) - + # 🌟 统一设置全局 Unicode 宏 (ARX 和 MFC 都强烈需要) add_compile_definitions( UNICODE diff --git a/src/AutoAgent/mcp.cpp b/src/AutoAgent/mcp.cpp index 6952232..4612b6f 100644 --- a/src/AutoAgent/mcp.cpp +++ b/src/AutoAgent/mcp.cpp @@ -131,10 +131,21 @@ namespace mcp bool init_mcp() { g_bRunning = true; - g_serverThread = std::thread(TcpServerLoop); - acutPrintf(_T("\n[MCP Server] 后台 TCP 监听线程已启动 (端口: 8080)...\n")); + try + { + g_serverThread = std::thread(TcpServerLoop); + acutPrintf(_T("\n[MCP Server] 后台 TCP 监听线程已启动 (端口: 8080)...\n")); + } + catch (const std::system_error &e) + { + // 线程创建失败,重置状态标志并输出错误 + g_bRunning = false; + // e.what() 返回的是 const char*,所以在 acutPrintf 中要用 %hs 而不是 %s 或 %ls + acutPrintf(_T("\n[MCP Server] 致命错误:无法创建后台监听线程!原因: %hs\n"), e.what()); + } + return g_bRunning; } - + bool close_mcp() { g_bRunning = false; @@ -143,5 +154,6 @@ namespace mcp g_serverThread.join(); } acutPrintf(_T("\n[MCP Server] 插件已卸载,线程已关闭。\n")); + return true; } } diff --git a/test_autoagent.py b/test_autoagent.py new file mode 100644 index 0000000..9b4b265 --- /dev/null +++ b/test_autoagent.py @@ -0,0 +1,20 @@ +import socket +import json + +# 模拟大模型生成的 JSON 指令 +command = { + "cmd": "draw_circle", + "x": 500.0, + "y": 500.0, + "r": 250.0 +} + +try: + client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + client.connect(("127.0.0.1", 8080)) + # 发送序列化后的 JSON 字符串 + client.sendall(json.dumps(command).encode('utf-8')) + client.close() + print("指令发送成功,请查看 AutoCAD 屏幕。") +except Exception as e: + print(f"连接失败: {e}") \ No newline at end of file