Skip to content

fix(McpServer): stdio server exits 130 instead of 0 when the client closes stdin #10474

Closed

Description

@coygeek

Summary

An MCP server built with the documented Layer.launch(McpServer.layerStdio(...)).pipe(NodeRuntime.runMain) pattern exits with status 130 when the client closes stdin at the end of a normal session, the same status the process gets when runMain handles SIGINT or SIGTERM. Closing stdin is the shutdown sequence the MCP specification asks stdio clients to use, so every clean shutdown is reported like a Ctrl-C. The same server shape on Effect 3.x exits 0. All responses are written before exit; only the exit status is affected.

Steps to reproduce

In an empty directory, using the baseline under Runtime or environment:

npm init -y
npm pkg set type=module
npm install [email protected] @effect/[email protected] --ignore-scripts

server.js, following the shape of the Getting Started example in packages/effect/MCP.md:

import { NodeRuntime, NodeStdio } from "@effect/platform-node"
import { Effect, Layer, Schema } from "effect"
import { McpProtocol, McpServer, Tool, Toolkit } from "effect/ai"

const Echo = Tool.make("echo", { parameters: Schema.Struct({ message: Schema.String }), success: Schema.String })
const Tools = Toolkit.make(Echo)

const ServerLayer = McpServer.layerStdio({
  name: "repro",
  version: "1.0.0",
  protocols: [McpProtocol.v2025_06_18],
  registrations: McpServer.toolkit(Tools).pipe(
    Layer.provide(Tools.toLayer({ echo: ({ message }) => Effect.succeed(message) }))
  )
}).pipe(Layer.provide(NodeStdio.layer))

Layer.launch(ServerLayer).pipe(NodeRuntime.runMain)

frames.jsonl:

{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"repro","version":"0"}}}
{"jsonrpc":"2.0","method":"notifications/initialized"}
{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"echo","arguments":{"message":"hi"}}}

Run:

printf '' | node server.js; echo "exit=$?"
(cat frames.jsonl; sleep 1) | node server.js; echo "exit=$?"

Expected behavior

Both runs exit with status 0. The client ended the session the way the MCP specification's stdio shutdown sequence describes (close the server's stdin, then wait for it to exit), and every request received a response, so the process completed its job rather than being interrupted. Exit status 130 conventionally means 128 + SIGINT, and it is also what NodeRuntime.runMain produces on SIGINT or SIGTERM, so a caller checking the status cannot tell a finished session from a killed one. The Effect 3.x control under Actual behavior exits 0 here. Signal shutdown should keep exiting 130.

Actual behavior

Both runs exit 130. The second run writes both responses first:

exit=130
{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2025-06-18","capabilities":{"logging":{},"tools":{"listChanged":true},"completions":{}},"serverInfo":{"name":"repro","version":"1.0.0"}}}
{"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"hi"}],"isError":false}}
exit=130

A custom teardown passed to runMain that delegates to Runtime.defaultTeardown observed Exit.isSuccess(exit) === false and Cause.hasInterruptsOnly(exit.cause) === true after a stdin EOF that followed a complete initialize, tools/list and tools/call exchange. Sending SIGINT or SIGTERM to the same server while stdin is still open also exits 130, so a caller cannot tell a clean EOF shutdown from a signal.

Controls run locally:

  • Effect 3.x control: the same server shape on [email protected], @effect/[email protected], @effect/[email protected] and @effect/[email protected] (McpServer.layerStdio({ name, version, stdin: NodeStream.stdin, stdout: NodeSink.stdout }), Layer.launch, NodeRuntime.runMain) exits 0 for both runs. Its defaultTeardown mapped interruption-only exits to 0.
  • Workaround: running the launch in a child fiber, treating an interruption-only exit as success and re-raising any other failure, exits 0 after stdin EOF, still exits 130 on SIGINT and SIGTERM, and still exits 1 when the server layer fails to build. Awaiting the child fiber without re-raising its failure would also exit 0 for a failing layer, so the check matters (Cause, Exit and Fiber imported from effect):
Effect.gen(function*() {
  const fiber = yield* Effect.forkChild(Layer.launch(ServerLayer))
  const exit = yield* Fiber.await(fiber)
  if (Exit.isFailure(exit) && !Cause.hasInterruptsOnly(exit.cause)) {
    return yield* Effect.failCause(exit.cause)
  }
}).pipe(NodeRuntime.runMain)

Evidence

Source breadcrumbs on the main build: RpcServer.makeProtocolStdio captures the fiber that builds the protocol layer (Fiber.getCurrent(), packages/effect/src/rpc/RpcServer.ts line 1587) and interrupts it once the stdout stream ends after stdin EOF (line 1615). Under Layer.launch, which has type Effect<never, E, RIn>, that fiber is the main fiber, so the program can only end as an interruption, and defaultTeardown maps that to 130 (packages/effect/src/Runtime.ts line 116). The makeProtocolStdio and defaultTeardown bodies are identical in the release package and on main; the line numbers above refer to main.

Restoration check

With the reproduction above, printf '' | node server.js and the frames.jsonl session both exit 0 instead of 130, and the session still writes both responses before exit. As discriminating controls, sending SIGINT or SIGTERM to the server while stdin is open still exits 130, and the response draining after EOF added by #8714 still holds (a request whose handler is still running at EOF gets its response before the process exits).

Runtime or environment

  • [email protected] and @effect/[email protected] from npm (current latest).
  • main at 7d3e4411c707dff36ee1856ca4cf530014c2f8d4, installed from the pkg.pr.new snapshot of effect, @effect/platform-node and @effect/platform-node-shared for that commit. Same results as the release.
  • Node.js v26.12.1, npm 12.2.1, macOS arm64.

Additional context

#8710 reported this exit status together with lost responses and expected the server to exit 0 after EOF. #8714 fixed the lost responses and closed #8710, and its description states that "the existing interrupt-based process exit status remains unchanged". The stdin-EOF interrupt dates from #5218 in Effect 3.x, where the default teardown mapped interruption to exit 0. Open #10412 and its PR #10415 add an opt-in policy for interrupting running handlers at EOF; they keep the interrupt-based shutdown and do not address the exit status. Because the interrupt lives in RpcServer.makeProtocolStdio, plain RPC servers on RpcServer.layerProtocolStdio launched the same way probably exit 130 on EOF too; I did not run that case.

Triage assessment

  • Impact level: P2
  • Assessment status: provisional
  • Impact basis: Every clean stdin-EOF shutdown of a server built with the documented pattern reports exit status 130 instead of 0, on both tested builds, which changes what exit-status checks and process supervisors observe; all responses are delivered and no data is lost. This investigation observed no specific client or supervisor that misbehaves on the status, so the practical consequence for real clients is unverified.
  • Workaround status: available
  • Workaround basis: The child-fiber workaround under Actual behavior was run locally on both tested builds: it exits 0 after stdin EOF, keeps exit 130 for SIGINT and SIGTERM, and keeps exit 1 for a server layer that fails to build. The simpler form that only awaits the child fiber is unsafe because it also exits 0 for a failing layer.

Activity

  1. self-assigned this
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions