Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(mcp): retry failed connections on later tool calls

Linuxdazhao78725e19153e
diff --git a/apps/desktop/electron/main/user-mcp.ts b/apps/desktop/electron/main/user-mcp.ts
index 31cf52f4b..bfbb4cedf 100644
--- a/apps/desktop/electron/main/user-mcp.ts
+++ b/apps/desktop/electron/main/user-mcp.ts
@@ -489,19 +489,24 @@ export class UserMcpRuntime {
existing.lastUsedAt = Date.now();
return { tools: await existing.connecting, entry: existing };
}
+ // Session assembly must not repeatedly pay a broken server's connect
+ // timeout. A later explicit tool call is a fresh request, however, and
+ // gets a fresh handshake instead of staying unavailable forever.
+ if (existing?.status.state === "failed") {
+ // A settings probe can mark a transport failed while an older call is
+ // still in flight. Let that call finish rather than closing its client.
+ if (!pin || existing.activeCalls > 0) {
+ if (pin) existing.activeCalls += 1;
+ existing.lastUsedAt = Date.now();
+ return { tools: [], entry: existing };
+ }
+ existing.client.close();
+ }
if (existing?.client.isConnected()) {
if (pin) existing.activeCalls += 1;
existing.lastUsedAt = Date.now();
return { tools: existing.client.getTools(), entry: existing };
}
- // A server that already failed its handshake this run stays failed until the
- // user edits it or asks for a test, so every session assembly does not pay
- // the connect timeout again.
- if (existing?.status.state === "failed") {
- if (pin) existing.activeCalls += 1;
- existing.lastUsedAt = Date.now();
- return { tools: [], entry: existing };
- }
const entry = existing ?? this.createEntry(record, oauthToken, key, projectPath);
if (pin) entry.activeCalls += 1;
diff --git a/apps/desktop/test/user-mcp.test.mjs b/apps/desktop/test/user-mcp.test.mjs
index e8df10a3b..6e369e27a 100644
--- a/apps/desktop/test/user-mcp.test.mjs
+++ b/apps/desktop/test/user-mcp.test.mjs
@@ -230,6 +230,7 @@ test("the stdio connection cache stays bounded and evicted workspaces reconnect"
test("refreshing HTTP MCP status detects a server that went offline", async (t) => {
let online = true;
let pings = 0;
+ let handshakes = 0;
let connected = false;
let finishCall;
let failCall;
@@ -238,7 +239,11 @@ test("refreshing HTTP MCP status detects a server that went offline", async (t)
const client = {
isConnected: () => connected,
getTools: () => [{ name: "lookup" }],
- connect: async () => { connected = true; return [{ name: "lookup" }]; },
+ connect: async () => {
+ handshakes += 1;
+ connected = true;
+ return [{ name: "lookup" }];
+ },
callTool: () => new Promise((resolve, reject) => {
finishCall = resolve;
failCall = reject;
@@ -270,6 +275,12 @@ test("refreshing HTTP MCP status detects a server that went offline", async (t)
finishCall({ content: [{ type: "text", text: "completed" }] });
assert.deepEqual(await pendingCall, { content: [{ type: "text", text: "completed" }] });
assert.equal(pings, 2);
+
+ online = true;
+ client.callTool = async () => "recovered";
+ assert.equal(await rt.callTool("mcp_stub_lookup", {}, "/repo"), "recovered");
+ assert.equal(handshakes, 2, "a failed live transport should be re-handshaken");
+ assert.equal(rt.statusFor("stub").state, "ready");
});
test("stopping one session cancels only its active MCP call", async (t) => {
const started = new Map();
@@ -568,16 +579,25 @@ test("reconnection revalidates the advertised tool list before dispatch", async
assert.deepEqual(state.calls, []);
});
-test("a failed reconnect reports unavailability without repeated handshakes", async (t) => {
+test("a failed reconnect is retried and can recover on a later tool call", async (t) => {
const { rt, client, state } = reconnectingRuntime(t);
await rt.toolsForProject("/repo");
client.close();
state.fail = true;
- for (let i = 0; i < 2; i += 1) {
- await assert.rejects(rt.callTool("mcp_stub_lookup", {}, "/repo"), { errorCode: "UNAVAILABLE" });
- }
+ await assert.rejects(rt.callTool("mcp_stub_lookup", {}, "/repo"), { errorCode: "UNAVAILABLE" });
assert.equal(state.handshakes, 2);
+
+ assert.deepEqual(await rt.toolsForProject("/repo"), []);
+ assert.equal(state.handshakes, 2, "session assembly should not repeatedly dial a failed server");
+ await assert.rejects(rt.callTool("mcp_stub_lookup", {}, "/repo"), { errorCode: "UNAVAILABLE" });
+ assert.equal(state.handshakes, 3, "an explicit tool call should retry the failed handshake");
assert.deepEqual(state.calls, []);
+
+ state.fail = false;
+ assert.equal(await rt.callTool("mcp_stub_lookup", {}, "/repo"), "lookup");
+ assert.equal(state.handshakes, 4);
+ assert.deepEqual(state.calls, ["lookup"]);
+ assert.equal(rt.statusFor("stub").state, "ready");
});
test("scope and enablement are checked before reconnecting a remembered tool", async (t) => {
diff --git a/docs/spec/06-delivery/e2e/plugins-and-extensions.md b/docs/spec/06-delivery/e2e/plugins-and-extensions.md
index f525cbb4d..a6bf12c92 100644
--- a/docs/spec/06-delivery/e2e/plugins-and-extensions.md
+++ b/docs/spec/06-delivery/e2e/plugins-and-extensions.md
@@ -835,7 +835,8 @@ window; opening a normal panel afterward must still work.
the same tool in the existing session without searching again.
11. Repeat with the restarted stub omitting that tool, and with the server
disabled or scoped away before recovery. Also try concurrent calls after
- a disconnect and a server that fails its recovery handshake.
+ a disconnect and a server that fails its recovery handshake. Restore that
+ server and call the same tool again without an edit or Test connection.
12. Connect the HTTP server, stop it, and refresh the MCP settings page.
Test connection again after restarting the server.
- **Expected**:
@@ -843,8 +844,9 @@ window; opening a normal panel afterward must still work.
concurrent calls share one handshake. The fresh server list must still
advertise the tool. Removed tools and inactive servers are refused without
executing a call; inactive servers are not reconnected. Recovery failure
- returns `UNAVAILABLE` and does not trigger repeated handshake attempts until
- an edit or Test connection. A failed tool execution is never replayed.
+ returns `UNAVAILABLE` and is not redialled by session assembly. A later
+ explicit tool call retries the failed handshake and succeeds after the
+ server recovers. A failed tool execution is never replayed.
- Two servers import; the third is listed as skipped with "a stdio server
requires command". The LAN HTTP entry lands as `http` with its url intact,
and the editor shows the unencrypted-connection warning.
diff --git a/docs/spec/07-plugins/01-plugin-system.md b/docs/spec/07-plugins/01-plugin-system.md
index 4ab326822..27c80dfc8 100644
--- a/docs/spec/07-plugins/01-plugin-system.md
+++ b/docs/spec/07-plugins/01-plugin-system.md
@@ -534,10 +534,11 @@ Rules the control encodes:
- Enablement and project scope are checked before and after recovery. Removing
a server or disposing the runtime discards its remembered names; an obsolete
in-flight handshake cannot restore them. Concurrent calls share a handshake.
-- A failed recovery reports `UNAVAILABLE` and retains the existing failed-server
- policy (edit or Test connection to retry), rather than repeatedly connecting
- on each call. Removed tools return `TOOL_NOT_FOUND`. Recovery never replays a
- failed `tools/call`, which may already have performed a mutation.
+- A failed recovery reports `UNAVAILABLE`. Session assembly does not repeatedly
+ dial the failed server, but a later explicit tool call retries the handshake
+ and can recover without an edit or Test connection. Removed tools return
+ `TOOL_NOT_FOUND`. Recovery never replays a failed `tools/call`, which may
+ already have performed a mutation.
- Test connection uses the requested settings project, or the currently open
project when no project is supplied. It retries that workspace and clears the
same server's other idle failed workspace connections, so a successful test