From d7827e0c512f536d4a37594358af93dc5dd86920 Mon Sep 17 00:00:00 2001 From: Nayan Bhut Date: Mon, 23 Nov 2026 17:10:33 +0000 Subject: [PATCH] Fix StdioTransport trap when stdio is a socket Convert st_dev and st_ino to UInt64 by bit pattern (truncatingIfNeeded) instead of value when saving descriptor state in setNonBlocking and comparing it in restoreFlags. Node's child_process.spawn connects a server's stdio to socketpairs, and on Darwin fstat() on a socket reports st_dev == -1. dev_t is a signed Int32 there, so UInt64(status.st_dev) traps with "Negative value is not representable" during connect(), killing every Node-based MCP client (Claude Desktop, Claude Code report "Connection closed" right after initialize). Shell pipes report st_dev == 0, which is why terminal testing never caught it. The device and inode values are only compared for identity between save and restore, so converting by bit pattern is safe on every platform: a no-op where dev_t is unsigned (Linux), and trap-free where it is signed (Darwin). Adds a regression test that connects a StdioTransport over a socketpair and exchanges a message, mirroring how Node clients spawn servers. Fixes #327 Assisted-by: AI coding agent (Darwin). The approach follows the analysis in the issue; all code was reviewed and tested by the author. --- .../MCP/Base/Transports/StdioTransport.swift | 11 ++++-- Tests/MCPTests/StdioTransportTests.swift | 34 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/Sources/MCP/Base/Transports/StdioTransport.swift b/Sources/MCP/Base/Transports/StdioTransport.swift index fbada31e..0b91ad33 100644 --- a/Sources/MCP/Base/Transports/StdioTransport.swift +++ b/Sources/MCP/Base/Transports/StdioTransport.swift @@ -143,8 +143,13 @@ import struct Foundation.Data guard result >= 0 else { throw MCPError.transportError(Errno(rawValue: CInt(errno))) } + // Convert by bit pattern: `st_dev` is signed on Darwin, where sockets + // report -1, and `UInt64(status.st_dev)` would trap. The values are + // only compared for identity, so the exact conversion doesn't matter. return DescriptorState( - 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)) #else // For platforms where non-blocking operations aren't supported throw MCPError.internalError( @@ -156,8 +161,8 @@ import struct Foundation.Data guard state.flags & O_NONBLOCK == 0 else { return } var status = stat() guard fstat(fileDescriptor.rawValue, &status) == 0, - UInt64(status.st_dev) == state.device, - UInt64(status.st_ino) == state.inode + UInt64(truncatingIfNeeded: status.st_dev) == state.device, + UInt64(truncatingIfNeeded: status.st_ino) == state.inode else { return } let currentFlags = fcntl(fileDescriptor.rawValue, F_GETFL) guard currentFlags >= 0 else { return } diff --git a/Tests/MCPTests/StdioTransportTests.swift b/Tests/MCPTests/StdioTransportTests.swift index 08331411..a2a2f39e 100644 --- a/Tests/MCPTests/StdioTransportTests.swift +++ b/Tests/MCPTests/StdioTransportTests.swift @@ -28,6 +28,40 @@ struct StdioTransportTests { await transport.disconnect() } + @Test("Connect and exchange messages over a socketpair, as used by Node clients") + func testStdioTransportOverSocketPair() async throws { + // Node's child_process.spawn uses socketpairs for stdio pipes. On Darwin, + // fstat() on a socket reports st_dev == -1, which used to trap when + // converted with UInt64(st_dev) during connect(). + var fds: [Int32] = [0, 0] + // Glibc imports SOCK_STREAM as a C enum; Darwin and Musl expose it as Int32. + #if canImport(Glibc) + let streamSocketType = Int32(SOCK_STREAM.rawValue) + #else + let streamSocketType = SOCK_STREAM + #endif + #expect(socketpair(AF_UNIX, streamSocketType, 0, &fds) == 0) + let input = FileDescriptor(rawValue: fds[0]) + let output = FileDescriptor(rawValue: fds[1]) + defer { + try? input.close() + try? output.close() + } + + let transport = StdioTransport(input: input, output: output, logger: nil) + try await transport.connect() + + let message = #"{"key":"value"}"# + try await transport.send(message.data(using: .utf8)!) + + let stream: AsyncThrowingStream = await transport.receive() + var iterator = stream.makeAsyncIterator() + let received = try await iterator.next() + #expect(received == message.data(using: .utf8)!) + + await transport.disconnect() + } + @Test("Disconnect restores file descriptor blocking mode") func testDisconnectRestoresBlockingMode() async throws { let (input, writer) = try FileDescriptor.pipe()