Fix: Stop MCP tool timeouts from popping MFC dialogs

A sub-connection's first packet doubles as the readiness packet: the file,
service, registry, process, log and terminal tools all get answered that way.
Once a tool has given up (timeout or failure) its pending entry is gone, so if
the client only gets around to sending that first packet afterwards,
MessageHandle cannot tell it apart from a human click and opens the matching
modeless dialog - a File Manager window appeared while timeouts were being
exercised on this machine.

The wait paths now record, per device, which first-packet token the abandoned
tool would have sent (MarkLateFirstPacketForTool from WaitPending,
WaitPendingRegistry, WaitPendingWrite, WaitFileTransferDone and
WaitTerminalReady), and every dialog-opening case consumes that marker before
falling back to the MFC path. The marker is one-shot, expires after five
minutes and is never set on the success path, so normal use is unaffected; the
worst case is that a single manual open on a host that just had an MCP timeout
is swallowed.

ClientMsg packets are dropped with a log line while a tool is pending for that
host: new clients still send the legacy popup message alongside the structured
failure token, which would otherwise put a modal box on the server UI during an
MCP call.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-25 18:12:56 +01:00
1 parent 1456882f16
commit b4cb34fea9
4 files changed
+131 -2

No files matched your search

+50
View File
@@ -5946,11 +5946,16 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
// 只取首条全量,取走即 CancelIO 关子链接,停止后续增量推送(与 MFC 日志对话框
// 持续接收增量的路径区分开)。
uint64_t devId = ContextObject->GetClientID();
const bool mcpLate = McpServer().TakeToolFirstPacket(devId, (BYTE)cmd);
if (McpServer().IsPending(devId)) {
McpServer().TakeMainResponse(devId, szBuffer, len);
ContextObject->CancelIO();
break;
}
if (mcpLate) {
ContextObject->CancelIO(); // get_client_log 已放弃后的迟到日志子链接:不弹日志窗口
break;
}
std::string logText((char*)(szBuffer + 1), len > 1 ? len - 1 : 0);
if (!ContextObject->hDlg) {
// 对话框尚未打开:用 SendMessage 同步打开并显示初始全量日志
@@ -5968,10 +5973,12 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
// P2b:MCP 挂起时接管。历史活动为「主连接 RPC」(无子链接),响应回主 context,
// 不能 CancelIO(会断开主连接),仅同步拷贝后 break。
uint64_t devId = ContextObject->GetClientID();
const bool mcpLate = McpServer().TakeToolFirstPacket(devId, (BYTE)cmd);
if (McpServer().IsPending(devId)) {
McpServer().TakeMainResponse(devId, ContextObject->GetBuffer(0), ContextObject->GetBufferLength());
break;
}
if (mcpLate) break; // get_activity_history 已放弃后的迟到包:主连接,不能 CancelIO,直接丢弃
// 一次性快照:直接打开“历史活动”对话框展示,不占用 hDlg(避免与运行日志对话框冲突)
std::string text((char*)(szBuffer + 1), len > 1 ? len - 1 : 0);
CActivityDialog* dlg = new CActivityDialog(this, ContextObject->GetServer(), ContextObject);
@@ -5982,6 +5989,17 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
break;
}
case TOKEN_CLIENT_MSG: {
// MCP 工具在飞时不弹客户端的老式提示框:新客户端在失败分支同时发结构化 token(已能
// 让工具立即返回错误)和老 ClientMsg(只为兼容老服务端),后者会让 MCP 调用在服务端
// UI 上弹一个模态框。有挂起请求说明这次失败已经由工具结果解释,直接丢弃并记日志。
uint64_t devId = ContextObject->GetClientID();
if (McpServer().IsRunning() &&
(McpServer().IsPending(devId) || McpServer().IsFileTransferPending(devId))) {
ClientMsg *msg =(ClientMsg*)ContextObject->InDeCompressedBuffer.GetBuffer(0);
Mprintf("[MCP] suppress client popup dev=%llu title=%.31s text=%.512s\n",
(unsigned long long)devId, msg->title, msg->text);
break;
}
ClientMsg *msg =(ClientMsg*)ContextObject->InDeCompressedBuffer.GetBuffer(0);
PostMessageA(WM_SHOWERRORMSG, (WPARAM)new CString(_L(msg->text)), (LPARAM)new CString(_L(msg->title)));
break;
@@ -6848,6 +6866,9 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
// P2c:MCP 挂起时接管。列盘 = 取走数据后用完即关;列目录 = 转向下发
// COMMAND_LIST_FILES、等 TOKEN_FILE_LIST(此时不关子链接,交给 TOKEN_FILE_LIST 分支收尾)。
uint64_t devId = ContextObject->GetClientID();
// 先取「迟到首包」标记(成功路径同样消费,不留残留):工具超时后客户端才连上来的
// 文件管理器子链接与本包,绝不能当成人手点击去弹文件管理器窗口。
const bool mcpLate = McpServer().TakeToolFirstPacket(devId, (BYTE)cmd);
if (McpServer().IsPending(devId)) {
if (McpServer().OnDriveList(devId, ContextObject, szBuffer, len))
ContextObject->CancelIO(); // 只列盘 → 用完即关一次性子链接
@@ -6860,6 +6881,10 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
ContextObject->CancelIO();
break;
}
if (mcpLate) {
ContextObject->CancelIO(); // 工具已放弃后的迟到包:关子链接,不弹窗
break;
}
ContextObject->EnableZstdContext(6);
g_2015RemoteDlg->SendMessage(WM_OPENFILEMANAGERDIALOG, 0, (LPARAM)ContextObject);
break;
@@ -6927,10 +6952,15 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
break;
}
// MCP exec_command 触发的 shell 子上下文:交给 McpServer 接管(老 cmd 管道,GBK)。
const bool mcpLate = McpServer().TakeToolFirstPacket(devId, (BYTE)cmd);
if (McpServer().IsTermPending(devId)) {
McpServer().RegisterTerminalContext(devId, ContextObject, /*isPty*/false);
break;
}
if (mcpLate) {
ContextObject->CancelIO(); // 终端工具已放弃后的迟到 shell 子链接:不弹终端窗口
break;
}
g_2015RemoteDlg->SendMessage(WM_OPENSHELLDIALOG, 0, (LPARAM)ContextObject);
break;
}
@@ -6942,10 +6972,15 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
break;
}
// MCP exec_command 触发的 PTY 子上下文:交给 McpServer 接管(ConPTY,UTF-8)。
const bool mcpLate = McpServer().TakeToolFirstPacket(devId, (BYTE)cmd);
if (McpServer().IsTermPending(devId)) {
McpServer().RegisterTerminalContext(devId, ContextObject, /*isPty*/true);
break;
}
if (mcpLate) {
ContextObject->CancelIO(); // 终端工具已放弃后的迟到 PTY 子链接:不弹终端窗口
break;
}
// 三个前置条件,缺任何一个都回退到经典终端,并把原因贴到信息列表。
// SYSTEM 场景:WebView2 不支持 LocalSystem token,会出现"窗口能弹但页面空白",
@@ -6976,6 +7011,7 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
// OnSystemReady 命中就绪包时接管子连接并下发写命令(返回 true、不 CancelIO),
// 命中确认包或只读工具时返回 false,走 TakeMainResponse + CancelIO 收尾。
uint64_t devId = ContextObject->GetClientID();
const bool mcpLate = McpServer().TakeToolFirstPacket(devId, (BYTE)cmd);
if (McpServer().IsPending(devId)) {
if (McpServer().OnSystemReady(devId, ContextObject))
break;
@@ -6983,6 +7019,10 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
ContextObject->CancelIO(); // 用完即关,避免子链接泄漏(仿 WebService 终端关闭)
break;
}
if (mcpLate) {
ContextObject->CancelIO(); // 进程/窗口工具已放弃后的迟到包:不弹进程管理器
break;
}
g_2015RemoteDlg->SendMessage(WM_OPENSYSTEMDIALOG, 0, (LPARAM)ContextObject);
break;
}
@@ -6994,8 +7034,13 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
// P3:MCP 挂起时接管。TOKEN_REGEDIT 是客户端子链接构造即发(CRegisterManager 构造),
// 命中则下发 COMMAND_REG_FIND 并保持子链接(等 TOKEN_REG_PATH/KEY),不打开 MFC 对话框。
uint64_t devId = ContextObject->GetClientID();
const bool mcpLate = McpServer().TakeToolFirstPacket(devId, (BYTE)cmd);
if (McpServer().IsPending(devId) && McpServer().OnRegeditReady(devId, ContextObject))
break;
if (mcpLate) {
ContextObject->CancelIO(); // list_registry 已放弃后的迟到子链接:不弹注册表窗口
break;
}
g_2015RemoteDlg->SendMessage(WM_OPENREGISTERDIALOG, 0, (LPARAM)ContextObject);
break;
}
@@ -7033,6 +7078,7 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
// P7:control_service 的就绪/确认是同一 token,OnServiceReady 命中就绪包时接管并下发
// COMMAND_SERVICECONFIG(返回 true、不 CancelIO),确认包走 TakeMainResponse 收尾。
uint64_t devId = ContextObject->GetClientID();
const bool mcpLate = McpServer().TakeToolFirstPacket(devId, (BYTE)cmd);
if (McpServer().IsPending(devId)) {
if (McpServer().OnServiceReady(devId, ContextObject))
break;
@@ -7040,6 +7086,10 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
ContextObject->CancelIO();
break;
}
if (mcpLate) {
ContextObject->CancelIO(); // 服务工具已放弃后的迟到包:不弹服务管理器
break;
}
g_2015RemoteDlg->SendMessage(WM_OPENSERVICESDIALOG, 0, (LPARAM)ContextObject);
break;
}