Feature: Turn client failure reports into branchable MCP errors

Wire the four new failure tokens into MessageHandle and translate them
in the tool builders, so the three previously silent failures now return
a code an agent can branch on: list_services on a non-elevated client is
-32010 in well under a second instead of a 20 s timeout, list_files and
list_registry distinguish a missing path or key (-32009) from an empty
one, and download_file no longer waits for the stall watchdog when the
remote path does not exist. Access denied gets its own code, -32010, and
the win32 code is kept in the message for every case.

TakeRegError fills both registry wait flags in one packet because a
failing client sends no subkey or value packet at all. OnDownloadError
sets the session error without cancelling the file-manager sub-connection,
which stays owned by ClearFileTransfer. ListRemoteTopLevelNames treats
"not found" as "directory not created yet" so uploading into a fresh
directory keeps working now that the client reports it.

Also converge the error codes that were overloaded: a multi-monitor host
is a capability limit, not a parameter mistake, so remote_open reports
-32005 instead of -32008. The tool wait timeout moves from a compile-time
constant to McpToolTimeoutMs in settings.ini (default 20000, clamped to
1-600 s), which makes it possible to tune without a rebuild.

The smoke test gains the three reproducible negatives.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-25 17:59:21 +01:00
1 parent 315a0855dc
commit 3b1626f415
4 files changed
+211 -25

No files matched your search

+42
View File
@@ -2432,6 +2432,8 @@ BOOL CMy2015RemoteDlg::OnInitDialog()
McpServer().SetChatEnabled(THIS_CFG.GetInt("settings", "McpChat", 0) != 0);
// N3 键盘记录(get_keylog):独立开关默认关,与只读开关无关。
McpServer().SetKeylogEnabled(THIS_CFG.GetInt("settings", "McpKeylog", 0) != 0);
// 工具默认等待超时(ini-only 键,无设置界面):默认 20000ms,clamp 在 SetToolTimeoutMs 内。
McpServer().SetToolTimeoutMs(THIS_CFG.GetInt("settings", "McpToolTimeoutMs", 20000));
if (!McpServer().Start(mcpBind, mcpPort)) {
Mprintf("McpServer start failed on %s:%d\n", mcpBind.c_str(), mcpPort);
} else {
@@ -6873,6 +6875,26 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
ContextObject->CancelIO(); // 无论是否命中,用完即关(无 MFC 对话框接管此子链接)
break;
}
case TOKEN_FILE_LIST_ERROR: { // 列目录失败(新客户端)【MCP 结构化失败】
// [token][win32err:4]。与 TOKEN_FILE_LIST 同构:命中则当作响应投递(由 BuildListFiles
// 转译成 -32009/-32010),否则是迟到的孤儿包;两种情况都关子链接。
uint64_t devId = ContextObject->GetClientID();
if (McpServer().IsPending(devId)) {
McpServer().TakeMainResponse(devId, szBuffer, len);
}
ContextObject->CancelIO();
break;
}
case TOKEN_DOWNLOAD_ERROR: { // download_file 无可用文件(新客户端)【MCP 结构化失败】
// 客户端在「传输子链接还没建立」时就在文件管理器子连接上回报,故此处不 CancelIO:
// 该子链接由 ClearFileTransfer 统一收尾(避免与 tool 线程的 CancelIO 竞争)。
uint64_t devId = ContextObject->GetClientID();
if (McpServer().IsFileTransferPending(devId)) {
McpServer().OnDownloadError(devId, ContextObject, szBuffer, len);
}
// 未命中(工具线程已放弃 / MFC 手动下载不会发此 token)→ 忽略,等 ClearFileTransfer。
break;
}
case TOKEN_SEARCH_FILE_LIST: { // 搜索结果【B2:仅 MCP 无头搜索会到达】
// 有挂起的 search_file 且是本连接发的 → 收下这一包;否则是已被放弃的旧搜索
// (工具线程已超时返回部分结果)的尾包,原地丢弃即可。
@@ -6996,6 +7018,15 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
ContextObject->CancelIO(); // 值包为最后包,无论命中与否用完即关
break;
}
case TOKEN_REG_ERROR: { // 注册表打开失败(新客户端)【MCP 结构化失败】
// 失败时客户端只发这一包(不会有 TOKEN_REG_PATH/KEY),TakeRegError 一次置满三个标志。
uint64_t devId = ContextObject->GetClientID();
if (McpServer().IsPending(devId)) {
McpServer().TakeRegError(devId, szBuffer, len);
}
ContextObject->CancelIO(); // 这一包即最后包
break;
}
case TOKEN_SERVERLIST: { // 服务管理【x】
// P3:MCP 挂起时接管。服务列表为一次性子链接回传(CServicesManager 构造即发),
// 取走即 CancelIO 关子链接(无 MFC 对话框续用该子链接)。
@@ -7012,6 +7043,17 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
g_2015RemoteDlg->SendMessage(WM_OPENSERVICESDIALOG, 0, (LPARAM)ContextObject);
break;
}
case TOKEN_SERVICE_ERROR: { // 服务枚举失败(新客户端)【MCP 结构化失败】
// 典型:客户端未提权,OpenSCManager 失败(win32=5)。命中则当作响应投递(由
// BuildListServices 立即返回 -32010),不再等 20s 超时。MFC 服务管理对话框有自己的
// OnReceive,不会走到这里;到达此处的孤儿包直接丢弃并关子链接。
uint64_t devId = ContextObject->GetClientID();
if (McpServer().IsPending(devId)) {
McpServer().TakeMainResponse(devId, szBuffer, len);
}
ContextObject->CancelIO();
break;
}
case TOKEN_CONNECTION_LIST: { // 连接管理【x】
g_2015RemoteDlg->SendMessage(WM_OPENCONNECTIONDIALOG, 0, (LPARAM)ContextObject);
break;