Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(mcp): cancel requests that time out
xpeng5278-webcd6300c7b0bb
diff --git a/apps/desktop/electron/main/plugin-mcp.ts b/apps/desktop/electron/main/plugin-mcp.tsindex ebf23fbb8..dd60208d4 100644--- a/apps/desktop/electron/main/plugin-mcp.ts+++ b/apps/desktop/electron/main/plugin-mcp.ts@@ -799,6 +799,13 @@ export class McpServerClient {this.pending.delete(id);removeAbort();rejectPromise(mcpError("TIMEOUT", `mcp ${method} timed out after ${timeoutMs}ms`));+ // The spec asks a sender that stops waiting to cancel the request, so+ // the server does not keep running work whose result is discarded.+ // `initialize` must never be cancelled; a failed handshake closes the+ // transport instead.+ if (method !== "initialize") {+ void transport.send({ jsonrpc: "2.0", method: "notifications/cancelled", params: { requestId: id, reason: "timeout" } }).catch(() => undefined);+ }}, timeoutMs);this.pending.set(id, {resolve: (value) => { removeAbort(); resolvePromise(value); },diff --git a/apps/desktop/test/plugin-mcp.test.mjs b/apps/desktop/test/plugin-mcp.test.mjsindex 3c64c05c7..e51570ecf 100644--- a/apps/desktop/test/plugin-mcp.test.mjs+++ b/apps/desktop/test/plugin-mcp.test.mjs@@ -489,6 +489,53 @@ test("aborting one HTTP MCP call cancels its request", async (t) => {assert.equal(await canceled, callId);});+test("a timed-out MCP call tells the server to stop the request", async (t) => {+ // The MCP lifecycle spec asks the sender to issue notifications/cancelled+ // when it gives up on a request; otherwise the server keeps running a tool+ // whose result nobody will read, and a model retry runs it a second time.+ let cancellationReceived;+ const canceled = new Promise((resolve) => { cancellationReceived = resolve; });+ let callId;+ const fetchImpl = async (_url, options) => {+ const message = JSON.parse(options.body);+ if (message.method === "notifications/initialized") return new Response(null, { status: 202 });+ if (message.method === "notifications/cancelled") {+ cancellationReceived(message.params);+ return new Response(null, { status: 202 });+ }+ if (message.method === "tools/call") {+ callId = message.id;+ return new Promise((_, reject) => {+ options.signal.addEventListener("abort", () => reject(new DOMException("aborted", "AbortError")), { once: true });+ });+ }+ const result = message.method === "initialize"+ ? { protocolVersion: message.params.protocolVersion, capabilities: {} }+ : { tools: [{ name: "slow" }] };+ return new Response(JSON.stringify({ jsonrpc: "2.0", id: message.id, result }), {+ status: 200,+ headers: { "content-type": "application/json" },+ });+ };+ const client = new McpServerClient({+ rootPath: mkdtempSync(join(tmpdir(), "pi-mcp-timeout-")),+ server: { id: "remote", transport: "http", url: "https://mcp.example.com/mcp" },+ values: {},+ callTimeoutMs: 50,+ fetchImpl,+ });+ t.after(() => client.close());+ await client.connect();++ await assert.rejects(client.callTool("slow", {}), { code: "TIMEOUT" });+ const params = await Promise.race([+ canceled,+ new Promise((resolve) => setTimeout(() => resolve("no cancellation"), 500)),+ ]);+ assert.notEqual(params, "no cancellation");+ assert.equal(params.requestId, callId);+});+test("aborting a tool waiting for a shared handshake does not send tools/call", async (t) => {let finishInitialize;let initializeStarted;diff --git a/docs/spec/03-runtime/01-ipc-protocol.md b/docs/spec/03-runtime/01-ipc-protocol.mdindex 2b540c8b7..fd5d330c8 100644--- a/docs/spec/03-runtime/01-ipc-protocol.md+++ b/docs/spec/03-runtime/01-ipc-protocol.md@@ -1535,7 +1535,8 @@ Stopping a session aborts its in-flight user MCP tool calls. The client sends`notifications/cancelled` for each active request without closing a connectionused by other sessions; a completed or canceled tool call is never replayed.Cancellation stops the local wait, while a server may ignore the notification-and finish an already started side effect.+and finish an already started side effect. A request that times out is+cancelled the same way, with reason `timeout`; `initialize` is never cancelled.Desktop-only channels scan configuration written by other agent tools on thesame machine — Claude Desktop (`claude_desktop_config.json` on macOS, Windowsdiff --git a/docs/spec/07-plugins/04-plugin-security.md b/docs/spec/07-plugins/04-plugin-security.mdindex e7af8456e..814748351 100644--- a/docs/spec/07-plugins/04-plugin-security.md+++ b/docs/spec/07-plugins/04-plugin-security.md@@ -417,7 +417,8 @@ manifest did not name:disabled.Stopping the calling session cancels that session's in-flight MCP request andsends `notifications/cancelled` to the server. A shared server connection and- calls owned by other sessions remain active.+ calls owned by other sessions remain active. A request that times out is+ cancelled the same way; `initialize` is never cancelled.## 8.2 Desktop control and device accessdiff --git a/docs/zh-CN/spec/03-runtime/01-ipc-protocol.md b/docs/zh-CN/spec/03-runtime/01-ipc-protocol.mdindex f218e7c90..deb680bf1 100644--- a/docs/zh-CN/spec/03-runtime/01-ipc-protocol.md+++ b/docs/zh-CN/spec/03-runtime/01-ipc-protocol.md@@ -1224,7 +1224,7 @@ type PluginSummary = {遮蔽全局项。仅桌面的 `mcp/test` IPC 操作用于强制连接测试,并把状态返回MCP 编辑器。设置页的 `mcp.list` 在返回状态前会探测此前就绪的远程连接;服务器失联时,列表显示 `failed`,而不是保留过时的 `ready`。服务器恢复后,“测试连接”会重试。探测失败不会中断正在执行的工具调用;“测试连接”会先关闭旧客户端再重试。MCP 编辑器。-停止会话会中止该会话正在执行的用户 MCP 工具调用,并向服务器发送 `notifications/cancelled`。其他会话共用的连接保持可用;已取消的调用不会重放。取消会结束本地等待,但服务器可能忽略通知并完成已经开始的副作用。+停止会话会中止该会话正在执行的用户 MCP 工具调用,并向服务器发送 `notifications/cancelled`。其他会话共用的连接保持可用;已取消的调用不会重放。取消会结束本地等待,但服务器可能忽略通知并完成已经开始的副作用。请求超时后同样发送 `notifications/cancelled`(reason 为 `timeout`);`initialize` 请求从不取消。```tstype McpServerStatus = {diff --git a/docs/zh-CN/spec/07-plugins/04-plugin-security.md b/docs/zh-CN/spec/07-plugins/04-plugin-security.mdindex af5b60a81..947ed43e4 100644--- a/docs/zh-CN/spec/07-plugins/04-plugin-security.md+++ b/docs/zh-CN/spec/07-plugins/04-plugin-security.md@@ -293,7 +293,7 @@ MCP 服务器是 `net.fetch` 旁边的第二个出口路径,因此它是声明拒绝,而不是贡献其目录的一个前缀,因为 MCP 工具是以延迟加载的按需条目(`ToolSearch` 之后)而非常驻列表的形式到达模型的。服务器按需连接,并在插件卸载或禁用时关闭。- 停止调用方会话会取消该会话正在执行的 MCP 请求,并向服务器发送 `notifications/cancelled`;共享连接及其他会话的调用保持有效。+ 停止调用方会话会取消该会话正在执行的 MCP 请求,并向服务器发送 `notifications/cancelled`;共享连接及其他会话的调用保持有效。请求超时后同样会发送取消通知;`initialize` 请求从不取消。## 8.2 桌面控制与设备访问