Feature: Add MCP cancellation, progress notifications and dry runs
P11 closes the plan's last batch, entirely on the server side: no client change is required for any of it. Long-running tools now expose a cancellation flag that every wait loop checks in 500 ms slices. A cancelled tool returns through the cleanup branch it already had (late first packets still get absorbed, sub connections still get closed, pending entries still get deleted), so there is no second cleanup path to keep in sync, and the host is released within a second. Three sources set the flag: notifications/cancelled (matched on the exact JSON-RPC id, with a fallback to the display form so a request_id copied out of the session list round-trips), mcp_cancel_session (by request_id, host id or session id), and a failed SSE write when the client has gone away. The dispatcher rewrites a cancel-induced failure to -32800 so callers do not mistake it for a host fault. tools/call requests carrying _meta.progressToken are answered as text/event-stream on the same POST: the tool runs on its own thread and both the notifications/progress events and the final JSON-RPC response go out through one chunked provider. Requests without a token are unchanged. download_file reports files done vs. expected, upload_file reports bytes sent vs. total. Two new local-only tools, mcp_list_sessions and mcp_cancel_session, list and cancel in-flight sessions (calls, pending one-shots, terminal, remote control, file transfer, chat). They are deliberately not gated by the feature switches or readonly mode, since that is the only way out of a stuck request. The -32003 busy message now names the occupier: "Device busy: ... (tool=download_file, elapsed=12s)". power_action gains wait/wait_ms to verify the result against the main connection's presence (went_offline, came_back, elapsed_ms); not seeing the transition is reported as ok, not as a failure. kill_process, operate_window and the four host_control_* tools accept dry_run, which validates the target against one fresh snapshot and returns what would be affected without sending anything. Validation rejections now use -32012; -32008 stays as documented historical alias. One implementation note worth recording: the per-thread call context uses FLS, not thread_local. Any static thread_local in this server crashes the process about a second after start, because some threads come from shellcode/MemoryModule images that never ran PE TLS initialisation. Verified against a real Debug server and a real Windows client over MCP: the smoke test now passes 104 checks with 0 failures, including cancellation releasing the host in under a second, the cancelled request returning -32800, notifications/cancelled returning 202, 63 progress events with exact byte counts for a 64 MB upload, and a cleanly terminated SSE stream. Not exercised in this pass: power_action's wait=true path (it would take the test host down) and the -32006 readonly/switch-off section (every switch was on during the run). Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
3d388f8e45
commit
1c832ff7f2
10 files changed
+1855
-107
No files matched your search
@@ -490,6 +490,10 @@ static std::string BuildChatWait(const Json::Value& id, const Json::Value& args,
|
||||
case CMcpServer::ChatWaitResult::Busy:
|
||||
return BuildError(id, kErrBusy,
|
||||
"Another chat_wait is already pending for this session");
|
||||
case CMcpServer::ChatWaitResult::Cancelled:
|
||||
// 会话本身仍然有效;回 -32800 让调用方知道是自己取消了等待。
|
||||
return BuildError(id, kErrCancelled,
|
||||
"Waiting cancelled by client; the chat session is still open");
|
||||
case CMcpServer::ChatWaitResult::Closed:
|
||||
case CMcpServer::ChatWaitResult::Timeout:
|
||||
case CMcpServer::ChatWaitResult::Ok:
|
||||
@@ -867,10 +871,14 @@ CMcpServer::ChatReadyResult CMcpServer::WaitChatReady(uint64_t device_id,
|
||||
if (s->closed) return ChatReadyResult::Closed;
|
||||
if (s->started && now >= s->readyAt) return ChatReadyResult::Ok;
|
||||
if (now >= deadline) return ChatReadyResult::Timeout;
|
||||
if (McpCallCancelled()) return ChatReadyResult::Timeout; // P11:取消按超时收尾
|
||||
|
||||
// 绑定通知到达后只需等到 readyAt,不要一直睡到整个 open 超时。
|
||||
steady_clock::time_point wake = deadline;
|
||||
if (s->started && s->readyAt < wake) wake = s->readyAt;
|
||||
// 500ms 上限:唤醒即可响应取消。
|
||||
const steady_clock::time_point slice = now + std::chrono::milliseconds(500);
|
||||
if (slice < wake) wake = slice;
|
||||
m_ChatCv.wait_until(lk, wake);
|
||||
}
|
||||
}
|
||||
@@ -923,7 +931,11 @@ CMcpServer::ChatWaitResult CMcpServer::WaitChatMessages(
|
||||
|
||||
while (s->inbox.empty() && !s->closed) {
|
||||
if (timeoutMs <= 0 || steady_clock::now() >= deadline) break;
|
||||
m_ChatCv.wait_until(lk, deadline);
|
||||
if (McpCallCancelled()) break; // P11:≤500ms 内响应取消
|
||||
// 500ms 一片:允许取消及时生效;无取消时与整段等待等价。
|
||||
auto until = steady_clock::now() + std::chrono::milliseconds(500);
|
||||
if (until > deadline) until = deadline;
|
||||
m_ChatCv.wait_until(lk, until);
|
||||
}
|
||||
|
||||
while (!s->inbox.empty() && out.size() < maxMessages) {
|
||||
@@ -939,6 +951,8 @@ CMcpServer::ChatWaitResult CMcpServer::WaitChatMessages(
|
||||
s->waitBusy = false;
|
||||
if (!s->closed) s->lastActiveAt = time(nullptr);
|
||||
|
||||
// 取消优先于「等到超时」:没有消息且是被取消唤醒的,如实回报(会话本身不受影响)。
|
||||
if (out.empty() && McpCallCancelled() && !closed) return ChatWaitResult::Cancelled;
|
||||
return timedOut ? ChatWaitResult::Timeout : ChatWaitResult::Ok;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user