StdioTransport traps ("Negative value is not representable") when stdio is a socket, e.g. spawned by Node clients

Open Beginner friendly
#327 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@branben is already working on this.

Since Nov 17, 2026.

  • #1 by @branben — closed without merging
  • #2 by @branben — closed without merging
  • #3 by @branben — closed without merging
  • #4 by @branben — closed without merging
  • #5 by @branben — closed without merging
  • #6 by @branben — open
  • #334 by @NayanBhut — closed without merging

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
73/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
swift

Research direction

Read Sources/MCP/Base/Transports/StdioTransport.swift around the reported lines in setNonBlocking(fileDescriptor:) and restoreFlags. The issue provides the proposed conversion and a reproducing Node socketpair scenario; add a socketpair regression test to verify the transport connects without trapping and run the relevant Swift tests. Done when socket-backed stdin/stdout no longer cause the negative-value trap.

Written by the indexing model from the issue text.

Description

Summary

Since #297 (merged 2026-10-31, on main, not in 0.12.1), StdioTransport.connect() traps when stdin/stdout is a socket:

Fatal error: Negative value is not representable

Node's child_process.spawn with stdio: 'pipe' uses socketpairs on macOS, and fstat on a socket reports st_dev == -1. dev_t is signed (Int32 on Darwin), so UInt64(status.st_dev) traps. Every Node-based MCP client hits this: Claude Desktop and Claude Code both report "Connection closed" right after initialize.

Affected lines: StdioTransport.swift L147, L159–160. Symbolicated crash: StdioTransport.setNonBlocking(fileDescriptor:) (StdioTransport.swift:147) ← StdioTransport.connect() ← Server.start(transport:).

Repro (macOS)

Build any server using StdioTransport(), then:

// node repro.js /path/to/server
const p = require("child_process").spawn(process.argv[2], [], { stdio: ["pipe", "pipe", "inherit"] });
p.on("exit", (c, s) => console.log("exit", c, s)); // → exit null SIGTRAP
p.stdin.write(JSON.stringify({ jsonrpc: "2.0", id: 0, method: "initialize",
  params: { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "n", version: "1" } } }) + "\n");

Shell pipes don't reproduce it (st_dev == 0), which is why terminal testing passes:

$ python3 -c "import os,socket; a,_=socket.socketpair(); r,_=os.pipe(); print(os.fstat(a.fileno()).st_dev, os.fstat(r).st_dev)"
-1 0
Fix

Convert by bit pattern instead of value (the values are only compared for identity):

-flags: flags, device: UInt64(status.st_dev), inode: UInt64(status.st_ino))
+flags: flags, device: UInt64(truncatingIfNeeded: status.st_dev), inode: UInt64(truncatingIfNeeded: status.st_ino))

(and the same in restoreFlags). Patch with this change: https://github.com/noisyneighborstudio/swift-sdk/commit/b7a26b8d887fda9389a81f0c5feaa34e3d7e8ff7. Happy to open a PR with it plus a socketpair regression test.

Dominant language
Swift
Stars
1.6k
Forks
278
Avg merge
11h 15m
Merged PRs (30d)
46

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from modelcontextprotocol/swift-sdk

All issues in modelcontextprotocol/swift-sdk

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.