Skip to content

Commit d2befa6

Browse files
MCP tool calls timed out after 20 seconds, the limit meant for the handshake. A slow but working tool (a browser navigation, a query, a build) returned mcp timeout while the server kept running it, and the agent would retry it. tools/call now waits up to 10 minutes and says so when it gives up (stub tool taking 21 s: timeout before, result after). The per-call timer is also cleared when the reply arrives, so a finished headless run no longer waits for it.
1 parent 6d8b576 commit d2befa6

2 files changed

Lines changed: 4 additions & 3 deletions

File tree

‎darknode.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,7 +2048,7 @@ function mcpConnect(name, spec, cwd) {
20482048
let buf = "", idc = 0; const pending = Object.create(null); // null-proto: a server reply with id "__proto__"/"toString" can't resolve an inherited member (crash / silent mis-route)
20492049
cp.stdout.setEncoding("utf8"); cp.stdout.on("data", (d) => { buf += d; let nl; while ((nl = buf.indexOf("\n")) >= 0) { const line = buf.slice(0, nl); buf = buf.slice(nl + 1); if (!line.trim()) continue; let m; try { m = JSON.parse(line); } catch (_) { continue; } if (m && m.method) { if (m.id != null) { try { cp.stdin.write(JSON.stringify(m.method === "ping" ? { jsonrpc: "2.0", id: m.id, result: {} } : { jsonrpc: "2.0", id: m.id, error: { code: -32601, message: "method not found: " + m.method } }) + "\n"); } catch (_) {} } continue; } /* a request FROM the server (ping, roots/list) shares the id space: never route it to our pending call */ if (m.id != null) { const cb = pending[m.id]; if (typeof cb === "function") { delete pending[m.id]; cb(m); } } } });
20502050
cp.on("error", () => {});
2051-
const call = (method, params) => new Promise((res, rej) => { const id = ++idc; pending[id] = (m) => m.error ? rej(new Error(m.error.message || "mcp error")) : res(m.result); try { cp.stdin.write(JSON.stringify({ jsonrpc: "2.0", id, method, params: params || {} }) + "\n"); } catch (e) { return rej(e); } setTimeout(() => { if (pending[id]) { delete pending[id]; rej(new Error("mcp timeout")); } }, 20000); });
2051+
const call = (method, params, ms) => new Promise((res, rej) => { const id = ++idc; let tm = null; pending[id] = (m) => (clearTimeout(tm), m.error) ? rej(new Error(m.error.message || "mcp error")) : res(m.result); try { cp.stdin.write(JSON.stringify({ jsonrpc: "2.0", id, method, params: params || {} }) + "\n"); } catch (e) { return rej(e); } tm = setTimeout(() => { if (pending[id]) { delete pending[id]; rej(new Error(ms ? "MCP tool did not answer within " + Math.round(ms / 60000) + " min" : "mcp timeout")); } }, ms || 20000); });
20522052
const notify = (method, params) => { try { cp.stdin.write(JSON.stringify({ jsonrpc: "2.0", method, params: params || {} }) + "\n"); } catch (_) {} };
20532053
(async () => {
20542054
try {
@@ -4468,7 +4468,7 @@ function nexusTui(engine, cwd, nexusMd, autoResume, launch) {
44684468
}
44694469
else if (name === "stop_background") { const _k = bgJobs.key(a.id || a.job); const _j = _k && bgJobs.get(_k); if (!_j) result = { error: (a.id || a.job ? "no such job " + (a.id || a.job) : "stop_background needs a job id") + bgJobs.known() }; else { const _was = _j.status; bgJobs.stop(_k); result = _was === "running" ? { ok: true, stopped: _k, note: "killed with its child processes" } : { ok: true, stopped: _k, note: "it had already " + (_was === "done" ? "exited (code " + _j.code + ")" : _was) }; } }
44704470
else if (name === "spawn_agents") { const tasks = (a.tasks || []).map(String).filter(Boolean).slice(0, 8); const actls = tasks.map(() => makeCtl()); const regs = tasks.map((tk, i) => addAgent(tk, { native: engine !== "claude", engine, ctl: actls[i] })); render(); const outs = await runSubagents(engine, tasks, cwd, mdl, (i, phase, out) => { if (phase === "done" && regs[i].status === "run") finishAgent(regs[i], /^error:/.test(String(out || "")) ? "fail" : "ok", out); render(); }, ctl, undefined, actls); regs.forEach((rg, i) => { if (rg.status === "run") finishAgent(rg, "ok", outs[i]); }); result = { agents: outs.map((o2, i) => ({ task: tasks[i], result: (o2 || "").slice(0, 1500) })) }; }
4471-
else if (String(name).startsWith("mcp__")) { const srv = mcpServers.find((s) => !s.error && String(name).slice(5).startsWith(s.name + "__")); if (!srv) result = { error: "MCP tool not connected: " + name }; else { const tool = String(name).slice(5 + srv.name.length + 2); const r = await srv.call("tools/call", { name: tool, arguments: a }); result = { content: resultText((r && r.content) || r) }; } }
4471+
else if (String(name).startsWith("mcp__")) { const srv = mcpServers.find((s) => !s.error && String(name).slice(5).startsWith(s.name + "__")); if (!srv) result = { error: "MCP tool not connected: " + name }; else { const tool = String(name).slice(5 + srv.name.length + 2); const r = await srv.call("tools/call", { name: tool, arguments: a }, 600000); /* 20 s is the handshake limit; a real tool (browser, query, build) can take minutes */ result = { content: resultText((r && r.content) || r) }; } }
44724472
else if (name === "discover") { result = { tools: discoverTools(a.query || a.q || "", TOOL_CATALOG.concat(mcpToolList().map((m) => [m.full, m.desc]))) }; }
44734473
else if (name === "repo_map" || name === "repomap" || name === "map") { const rm = getRepoMap({ fresh: !!a.fresh }); result = rm && rm.map ? { map: rm.text, files: rm.map.fileCount, symbols: rm.map.symbolCount } : { error: "could not build a repo map here" }; }
44744474
else if (name === "find_symbol" || name === "symbol" || name === "goto") { const rm = getRepoMap(); const q = a.name || a.symbol || a.query || a.q || ""; const hits = rm && rm.map ? findSymbol(rm.map, q, { limit: 20 }) : []; result = hits.length ? { symbol: q, definitions: hits } : { symbol: q, definitions: [], note: "no definition found in the repo map — try search{pattern} for usages" }; }

‎test/run.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3306,7 +3306,8 @@ ok("shouldCheck: first launch runs; throttled within 4h; runs after", shouldChec
33063306
{ const { spawnSync } = require("child_process"); const r = spawnSync(process.execPath, [path.join(__dirname, "..", "lib", "nexus", "ask-mcp.js")], { input: '{"jsonrpc":"2.0","id":7,"method":"ping"}\n', encoding: "utf8", timeout: 5000 });
33073307
ok("ask-mcp answers an MCP ping with an empty result", /"id":7,"result":\{\}/.test(r.stdout || "")); }
33083308
{ const src = fs.readFileSync(path.join(__dirname, "..", "darknode.js"), "utf8"), i = src.indexOf("function mcpConnect("), body = src.slice(i, src.indexOf("\n// ---------- Hooks", i));
3309-
ok("the MCP client answers server requests (ping) instead of routing them to its own pending call with the same id", /if \(m && m\.method\) \{ if \(m\.id != null\)/.test(body) && body.indexOf("if (m && m.method)") < body.indexOf("const cb = pending[m.id]")); }
3309+
ok("the MCP client answers server requests (ping) instead of routing them to its own pending call with the same id", /if \(m && m\.method\) \{ if \(m\.id != null\)/.test(body) && body.indexOf("if (m && m.method)") < body.indexOf("const cb = pending[m.id]"));
3310+
ok("an MCP tool call gets minutes, not the 20 s handshake limit, and its timer is cleared on reply", /srv\.call\("tools\/call", \{ name: tool, arguments: a \}, 600000\)/.test(src) && /pending\[id\] = \(m\) => \(clearTimeout\(tm\), m\.error\)/.test(body) && /tm = setTimeout\(/.test(body)); }
33103311
const { createAskServer } = require("../lib/nexus/ask-bridge");
33113312
const cp = require("child_process");
33123313

0 commit comments

Comments
 (0)