spawnCommand pipes stdin/stdout when used inside MCP server, breaking JSON-RPC transport
When node-llama-cpp is used as a dependency in an MCP server running over stdio transport (JSON-RPC over stdin/stdout), the spawnCommand function in dist/utils/spawnCommand.js causes two critical issues during lazy model initialization.
Problem
Lines 57-60 in spawnCommand.js:
if (progressLogs) {
child.stdout?.pipe(process.stdout); // (1) cmake output → JSON-RPC stdout
child.stderr?.pipe(process.stderr);
process.stdin.pipe(child.stdin); // (2) JSON-RPC stdin → cmake child
}
Issue 1: stdout pollution
child.stdout?.pipe(process.stdout) sends cmake/Vulkan build output directly into the MCP JSON-RPC response stream. The MCP client receives non-JSON lines and throws ValidationError, poisoning the session.
Issue 2: stdin hijack (more severe)
process.stdin.pipe(child.stdin) connects the MCP JSON-RPC request stream to the cmake child process. When cmake exits (e.g., exit 1 on Vulkan not found), the pipe closes or drains the parent's stdin. The parent node process can no longer read JSON-RPC requests and exits cleanly with code 0. The MCP client sees ClosedResourceError on the next request.
Reproduction
- Install an npm package that uses
node-llama-cpp(e.g.,@tobilu/qmd) - Run it as an MCP server over stdio:
qmd mcp - Send a JSON-RPC tool call that triggers lazy model loading (e.g., a vector/semantic search)
- Observe: cmake build output appears in stdout between JSON-RPC messages
- After cmake exits, the MCP server exits — no further requests can be processed
Environment
- node-llama-cpp: bundled via
@tobilu/qmd - Platform: Linux x86_64 (WSL2 Ubuntu 24.04)
- Node.js: 22.x
- GPU: Vulkan not available (falls back to CPU — triggers compilation attempt)
Workaround
Comment out the two pipe lines in spawnCommand.js:
if (progressLogs) {
// child.stdout?.pipe(process.stdout);
child.stderr?.pipe(process.stderr);
// process.stdin.pipe(child.stdin);
}
Suggested Fix
When running inside an MCP server (or any stdio-based IPC), progressLogs should not pipe to process.stdout/process.stdin. Options:
- Check if stdout is a TTY before piping (
process.stdout.isTTY) - Add an option to disable stdin/stdout piping (e.g.,
progressLogs: "stderr-only") - Never pipe
process.stdinto child processes — cmake doesn't need interactive input
The diagnosis here is clear and nicely localized — thanks for pinpointing it. Confirmed it's still present on main (src/utils/spawnCommand.ts:69-75):
if (progressLogs) {
child.stdout?.pipe(process.stdout);
child.stderr?.pipe(process.stderr);
process.stdin.pipe(child.stdin!);
} else {
child.stderr?.pipe(process.stderr);
}
This bites specifically when node-llama-cpp is a dependency inside a stdio MCP server: child.stdout.pipe(process.stdout) injects cmake/build output into the JSON-RPC response stream, and process.stdin.pipe(child.stdin) hands the server's JSON-RPC request stream to the build child — which drains/closes it on exit, so the parent stops reading requests and exits 0. Several of us run node-llama-cpp under stdio MCP servers, so this is a real footgun.
Note the child's output is already captured via the child.stdout/stderr.on("data") handlers, so the progressLogs block is purely for live echo. Proposed fix shape (happy to adjust): drop process.stdin.pipe(child.stdin!) entirely (build commands don't need parent stdin, and that pipe is the stdin-hijack / early-exit cause), and route the live progress echo to stderr instead of stdout so stdout stays reserved for JSON-RPC.
The one open choice is where progressLogs output should go — stderr-only, or through the library's existing logger? Happy to send a PR with the fix + a regression test once you point me at the direction you'd prefer.
Opened #616 for this. It drops the process.stdin.pipe(child.stdin!) (the stdin-hijack / early-exit cause) and routes the progressLogs echo to stderr so stdout stays clean for the JSON-RPC channel — the child's stdout is still captured for the resolved result, so nothing is lost. Added a regression test under test/standalone/utils/.
I went with stderr as the minimal, transport-safe default; happy to reroute the progress echo through the library logger instead if you'd prefer that direction.
:tada: This issue has been resolved in version 3.19.0 :tada:
The release is available on:
v3.19.0- npm package (@latest dist-tag)
- GitHub release
v3.19.0
Your semantic-release bot :package::rocket: