Feature: Add copy_files_c2c to transfer files between clients directly

Adds the 54th MCP tool, copy_files_c2c, which asks the source client to
push a set of files straight to another online client. Previously the only
way to move files between two clients was to download them to the server
machine and upload them again, so the data crossed the wire twice and
needed free disk on the server. With C2C the server only relays packets
and never parses or stores the payload.

Protocol. Two new ids in common/commands.h: COMMAND_C2C_SEND_FILES (174),
carrying the destination directory followed by a NUL-separated path list,
and TOKEN_C2C_SEND_RESULT (175), a fixed 45-byte result packet sent back
over the same sub-connection the transfer used. Clients advertise support
with the LOGIN_INFOR capability bit CLIENT_CAP_C2C_PATHS (0x0008), exposed
server side as Context::SupportsC2CPaths(). File transfer V2 already knew
how to push to dstClientID, so the destination side needed no change at
all; existing V2 receive paths keep working, and older clients simply do
not advertise the bit and are rejected with a clear error instead of
hanging.

Client. OnC2CSendFiles expands environment variables, collects directories
recursively, opens its own sub-connection (with sub-connection auth), and
runs FileBatchTransferWorkerV2 in a detached thread, then reports the
result on that same connection. Every early exit now reports a structured
failure rather than staying silent. The worker needs the process-level
InitFileUpload call that the receive path performs lazily, and a source
client may never have opened a file manager or received an upload, so the
initialization is factored into EnsureV2TransferInited() and called from
both paths.

Server. copy_files_c2c mirrors the existing upload tool: gates, id parsing,
capability checks, path validation, then a session keyed on the source host
that WaitC2CDone polls until the result packet arrives. The C2C relay
branch used to attach a CDlgFileSend progress dialog to the source's
sub-connection; NotifyProc then routed every later packet on that
connection to the dialog, which parsed the result packet as a V1 file
chunk and dropped it, so the tool always timed out even though the files
had arrived. IsC2CTransferPending() now suppresses dialog creation for
MCP-driven transfers only, leaving manual Ctrl+V transfers untouched.
Errors map to -32001 for a source-side transfer failure, -32010 for access
denied, -32009 for a missing path and -32002 for an offline destination.
Both hosts must be Windows: the destination is restricted too, because the
source embeds dst_dir in each V2 chunk name as an absolute Windows path and
only the Windows receiver applies it literally. A Linux or macOS receiver
never gets a target directory here (the clipboard flow sets one with
COMMAND_C2C_PREPARE, which this path does not send), so it would strip the
drive letter and write under / instead. Supporting that needs a prepare
packet carrying an explicit path plus an upgrade of those clients, so for
now the tool rejects them with -32005 rather than writing somewhere wrong.

Verified end to end on the Debug build: three files including a non-ASCII
name, an empty directory and a 3 MB binary arrived with matching SHA-256
in 3.2 s, a non-existent destination directory was created, and the
negative cases returned the documented codes. mcp_smoke.ps1 passes 41/41
with two new assertions and 27/27 in -SkipHost mode. Design notes, error
table and limitations are in docs/Mcp_FileTransfer_Design.md section 14
and docs/Mcp_Design.md section 15.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-10-26 15:38:07 +01:00
1 parent 27d40f5678
commit c271911fcb
12 files changed
+729 -11

No files matched your search

+15
View File
@@ -6302,6 +6302,13 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
if (dstCtx) {
dstCtx->Send2Client(szBuffer, len);
// MCP copy_files_c2c 不给源端子连接挂进度对话框:一旦挂了 hDlg,
// NotifyProc 会把该子连接之后的包都交给对话框的 OnReceiveComplete,
// 源端最后的 TOKEN_C2C_SEND_RESULT 就被当作 V1 文件块解析并丢弃,
// MCP 工具只能等到超时。MFC 手动 C2C(Ctrl+V / 文件管理器)行为不变。
if (McpServer().IsC2CTransferPending(ContextObject->GetClientID(), pkt->dstClientID))
break;
// 获取或创建进度对话框(和客户端->服务端相同方式)
if (ContextObject->hDlg == NULL) {
CDlgFileSend* dlg = new CDlgFileSend(this, ContextObject->GetServer(), ContextObject, FALSE);
@@ -6933,6 +6940,14 @@ VOID CMy2015RemoteDlg::MessageHandle(CONTEXT_OBJECT* ContextObject)
// 未命中(工具线程已放弃 / MFC 手动下载不会发此 token)→ 忽略,等 ClearFileTransfer。
break;
}
case TOKEN_C2C_SEND_RESULT: { // copy_files_c2c 源端发送结果
// 到达的是源客户端自开的传输子连接(TOKEN_CONN_AUTH 已把 clientID 钉成主连接的值)。
// 无对应会话(工具已超时/放弃、MFC 手动 C2C 不会发此 token)→ OnC2CResult 直接忽略。
Mprintf("[C2C] 收到源端结果包: dev=%llu len=%u\n",
(unsigned long long)ContextObject->GetClientID(), len);
McpServer().OnC2CResult(ContextObject->GetClientID(), szBuffer, len);
break;
}
case TOKEN_SEARCH_FILE_LIST: { // 搜索结果【B2:仅 MCP 无头搜索会到达】
// 有挂起的 search_file 且是本连接发的 → 收下这一包;否则是已被放弃的旧搜索
// (工具线程已超时返回部分结果)的尾包,原地丢弃即可。