← All cases

NPMBot maintainer · Recorded

MCP Connection closed: read stderr before changing the config

Two startup failures returned the same SDK error. A local four-case experiment separates an early exit, a wrong entry path, stdout noise and a clean connection.

Two runs · Node 22.23.4 / darwin arm64中文正文 ↗

The short answer

Connection closed tells you the connection ended. It does not identify the reason. In this experiment, a process that exited for missing configuration and a missing server file produced the same caller error. Their stderr output identified different next steps.

Two runs · Node 22.23.4 · SDK 1.32.2
Controlled caseInitializetools/listDiagnostic captured
Missing configuration, exit 1failednot-requestedLAB: missing required configuration
Wrong server file pathfailednot-requestedMODULE_NOT_FOUND
Debug text on stdoutokokUnexpected token 'L', "LAB: debug"... is not valid JSON
Debug text on stderrokokLAB: debug log on stderr

Two failures with the same message

The first fixture writes LAB: missing required configuration to stderr and exits with status 1 before initialize. The second asks Node to open a file that does not exist. Both calls to client.connect() reject with MCP error -32000: Connection closed, and neither reaches tools/list.

The corrective action depends on that extra evidence. For the first fixture, remove the deliberate early-exit branch and use the working fixture. For the second, point the client at the file the script actually created. The healthy stderr-log control completes initialization and tools/list in both runs. This verifies the small local fixtures, not the configuration of a third-party server.

Capture the evidence before retrying

With an SDK client, request a separate stderr pipe and attach a listener before connecting. Also keep the SDK client error callback: it captures protocol parsing errors which may not become the error rejected by an awaited call.

For an editor integration, find that client's MCP process log and note the actual command, absolute entry path, Node version and working directory. A shell where the command runs successfully may provide a different PATH or directory. These are things to compare; the generic Connection closed text alone does not prove any one of them is wrong.

const transport = new StdioClientTransport({
  command: process.execPath,
  args: [absoluteServerPath],
  stderr: 'pipe'
});
transport.stderr.on('data', chunk => {
  process.stderr.write(chunk);
});
const client = new Client({ name: 'diagnose', version: '0.0.0' });
client.onerror = error => console.error('[protocol]', error.message);
await client.connect(transport);
console.error(await client.listTools());

An extra stdout line did not close this connection

The third fixture first completes initialize. On tools/list it writes a plain debug line to stdout, followed by a valid JSON-RPC response. SDK 1.32.2 reports a JSON parsing error through client.onerror, but tools/list still succeeds. A guide that calls this a guaranteed disconnection would misdescribe the result.

Move diagnostic text to stderr. The stderr-log control returns the same empty tools list without a protocol parsing error. It makes the channel boundary explicit: protocol messages on stdout, diagnostics on stderr. Other clients or different malformed output may behave differently; this test does not establish recovery for all hosts.

Run the cases and compare the output

Download package.json, package-lock.json and run.mjs into a new directory. npm ci downloads the pinned SDK dependencies. The script then runs only its own local child-process fixtures, twice; it does not contact a remote MCP server or execute npmbot-mcp.

npm ci --ignore-scripts --no-audit --no-fund --registry=https://registry.npmjs.org
node run.mjs
node run.mjs --record my-results.json
SHA-256

run.mjs: 2a3bac9a63a3f809f029094670bfd0cf7432f61d3982a7de75719229c8e9c6a6
package-lock.json: c2937e6750a0732bfba5a56ce619b25d63630216dceea92ff1ef918d87d9b71b

What this experiment leaves open

Recorded with the official TypeScript SDK 1.32.2 on the environment below. It does not cover a real editor host, Windows, Linux, HTTP transport, authentication, network failures or model behavior. No independent reader reproduction has been recorded. An SDK issue is included as background; the table above comes from this site's own run, not from that issue.

darwin arm64 / OS 27.0.0 / Node 22.23.4 / SDK 1.32.2

Source references