Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions packages/eko-nodejs/src/mcp/stdio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class SimpleStdioMcpClient implements IMcpClient {
private options?: SpawnOptionsWithoutStdio;
private process: ChildProcessWithoutNullStreams | null = null;
private requestMap: Map<string, (messageData: any) => void>;
private stdoutBuffer: string = "";

constructor(
command: string,
Expand All @@ -38,18 +39,29 @@ export class SimpleStdioMcpClient implements IMcpClient {
this.process.kill();
} catch (e) {}
}
this.stdoutBuffer = "";
this.process = spawn(this.command, this.args, this.options);
this.process.stdout.on("data", (data) => {
const response = data.toString().trim();
Log.debug("MCP Client, onmessage", this.command, this.args, response);
if (!response.startsWith("{")) {
return;
}
const message = JSON.parse(response);
if (message.id) {
const callback = this.requestMap.get(message.id);
if (callback) {
callback(message);
this.stdoutBuffer += data.toString();
const lines = this.stdoutBuffer.split("\n");
// Keep the last (possibly incomplete) segment in the buffer
this.stdoutBuffer = lines.pop() || "";
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || !trimmed.startsWith("{")) {
continue;
}
Log.debug("MCP Client, onmessage", this.command, this.args, trimmed);
try {
const message = JSON.parse(trimmed);
if (message.id) {
const callback = this.requestMap.get(message.id);
if (callback) {
callback(message);
}
}
} catch (e) {
Log.warn("MCP Client, failed to parse JSON message:", trimmed, e);
}
}
});
Expand Down