Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(mcp): resume scanning buffered lines after a handler throws

AdityaUmale41e71b856a9a
diff --git a/server/stdio-lines.test.ts b/server/stdio-lines.test.ts
index 26a95c3c31..11872d3019 100644
--- a/server/stdio-lines.test.ts
+++ b/server/stdio-lines.test.ts
@@ -51,8 +51,8 @@ describe("readStdioLines", () => {
lines.push(line);
if (line === "bad") throw new Error("handler failed");
});
- expect(() => input.emit("data", Buffer.from("one\nbad\nthree"))).toThrow("handler failed");
+ expect(() => input.emit("data", Buffer.from("one\nbad\nthree\nfour"))).toThrow("handler failed");
input.emit("data", Buffer.from("\n"));
- expect(lines).toEqual(["one", "bad", "three"]);
+ expect(lines).toEqual(["one", "bad", "three", "four"]);
});
});
diff --git a/server/stdio-lines.ts b/server/stdio-lines.ts
index bc7c8d5e7c..bf254e2815 100644
--- a/server/stdio-lines.ts
+++ b/server/stdio-lines.ts
@@ -7,16 +7,18 @@ import { StringDecoder } from "node:string_decoder";
export function readStdioLines(input: Readable, onLine: (line: string) => void, onClose?: () => void): { close(): void } {
const decoder = new StringDecoder("utf8");
let rest = "";
+ let scanned = 0;
let open = true;
const deliver = (line: string) => onLine(line.endsWith("\r") ? line.slice(0, -1) : line);
const take = (text: string) => {
- const from = rest.length;
rest += text;
- for (let newline = rest.indexOf("\n", from); open && newline !== -1; newline = rest.indexOf("\n")) {
+ for (let newline = rest.indexOf("\n", scanned); open && newline !== -1; newline = rest.indexOf("\n")) {
const line = rest.slice(0, newline);
rest = rest.slice(newline + 1);
+ scanned = 0;
deliver(line);
}
+ scanned = rest.length;
};
const onData = (chunk: Buffer | string) => take(typeof chunk === "string" ? chunk : decoder.write(chunk));
const onEnd = () => {