Feature: Let the client report structured failures for MCP tools

Four new client-to-server tokens carry a win32/LSTATUS code when a
command fails instead of returning empty data: TOKEN_SERVICE_ERROR
(170), TOKEN_FILE_LIST_ERROR (171), TOKEN_REG_ERROR (172) and
TOKEN_DOWNLOAD_ERROR (173). They sit on byte values that were unused,
so existing numbering is untouched.

The three data paths that used to collapse "failed" and "empty" into
the same packet now distinguish them: OpenSCManager failures, directory
enumeration failures and RegOpenKeyEx failures all report the code that
was actually returned, and UploadToRemoteV2 reports why it found no
file to transfer before the transfer sub-connection is ever created.

Every failure branch still sends the packet the old server expects
(TOKEN_FILE_LIST, the empty TOKEN_REG_PATH/KEY pair, the ClientMsg
popup), so a new client talking to an old server degrades to exactly
today's behaviour instead of timing out. Success paths are byte-for-
byte unchanged.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-25 17:59:17 +01:00
1 parent eaa10cc7d3
commit 315a0855dc
8 files changed
+89 -13

No files matched your search

+36 -6
View File
@@ -438,8 +438,7 @@ UINT CFileManager::SendFilesList(LPCTSTR lpszDirectory)
// 展开失败 / 目录 + "\*.*" + NUL 装不下:按空目录回(放在分配缓冲之前,免得多一条泄漏路径)
if (szExpandedDir[0] == '\0' || lstrlenA(szExpandedDir) + 5 > MAX_PATH) {
BYTE bToken = TOKEN_FILE_LIST;
return Send(&bToken, 1);
return SendListError(ERROR_INVALID_NAME);
}
UINT nRet = 0;
@@ -461,8 +460,8 @@ UINT CFileManager::SendFilesList(LPCTSTR lpszDirectory)
hFile = FindFirstFile(strPath, &FindFileData);
if (hFile == INVALID_HANDLE_VALUE) {
BYTE bToken = TOKEN_FILE_LIST;
return Send(&bToken, 1);
// 路径不存在/不可访问:回结构化失败(TOKEN_FILE_LIST_ERROR),不再和「空目录」混为一包。
return SendListError(GetLastError());
}
*lpList = TOKEN_FILE_LIST;
@@ -850,6 +849,32 @@ int CFileManager::SendToken(BYTE bToken)
return Send(&bToken, 1);
}
// 列目录失败的结构化回报:[TOKEN_FILE_LIST_ERROR:1][win32err:4 LE]。
// 服务端(MCP list_files)据此返回「路径不存在/权限不足」,而不是一个空列表。
// 随后再补一个老的 1 字节 TOKEN_FILE_LIST:老服务端不认识新 token,仍按「空目录」处理,
// 行为与改动前完全一致。
UINT CFileManager::SendListError(DWORD dwErr)
{
if (dwErr == 0) dwErr = ERROR_GEN_FAILURE;
BYTE pkt[1 + sizeof(DWORD)] = { 0 };
pkt[0] = TOKEN_FILE_LIST_ERROR;
memcpy(pkt + 1, &dwErr, sizeof(DWORD));
int n = Send(pkt, (UINT)sizeof(pkt));
SendToken(TOKEN_FILE_LIST);
return n > 0 ? (UINT)n : 0;
}
// 下载请求无可用文件的结构化回报:[TOKEN_DOWNLOAD_ERROR:1][win32err:4 LE]。
// 走文件管理器子连接(传输子连接此时尚未建立),服务端立即失败而不是等停滞看门狗。
void CFileManager::SendDownloadError(DWORD dwErr)
{
if (dwErr == 0) dwErr = ERROR_FILE_NOT_FOUND;
BYTE pkt[1 + sizeof(DWORD)] = { 0 };
pkt[0] = TOKEN_DOWNLOAD_ERROR;
memcpy(pkt + 1, &dwErr, sizeof(DWORD));
m_ClientObject->Send2Server((char*)pkt, (UINT)sizeof(pkt));
}
bool CFileManager::UploadToRemote(LPBYTE lpBuffer)
{
if (lpBuffer[lstrlen((char *)lpBuffer) - 1] == '\\') {
@@ -1144,19 +1169,23 @@ void CFileManager::UploadToRemoteV2(LPBYTE lpBuffer, UINT nSize)
if (remotePaths.empty()) {
Mprintf("[V2] 没有要传输的文件\n");
SendDownloadError(ERROR_INVALID_PARAMETER);
return;
}
// 收集所有文件(展开目录)
std::vector<std::string> allFiles;
std::vector<std::string> rootCandidates; // 用于计算公共根目录
DWORD dwFirstErr = 0; // 首个失败的 win32 码(全部不存在时回传)
for (const auto& remotePath : remotePaths) {
std::string localPath = ExpandPath(remotePath.c_str());
DWORD attrs = GetFileAttributesA(localPath.c_str());
if (attrs == INVALID_FILE_ATTRIBUTES)
if (attrs == INVALID_FILE_ATTRIBUTES) {
if (dwFirstErr == 0) dwFirstErr = GetLastError();
continue;
}
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
// 目录:递归收集
@@ -1175,7 +1204,8 @@ void CFileManager::UploadToRemoteV2(LPBYTE lpBuffer, UINT nSize)
}
if (allFiles.empty()) {
Mprintf("[V2] 没有找到可传输的文件\n");
Mprintf("[V2] 没有找到可传输的文件 (win32=%lu)\n", dwFirstErr);
SendDownloadError(dwFirstErr);
return;
}