Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Sources/MCP/Base/Transports/StdioTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 }
Expand Down
34 changes: 34 additions & 0 deletions Tests/MCPTests/StdioTransportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Data, Swift.Error> = 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()
Expand Down
Loading