Conditionalize availability of StdioTransport (#65)

* Conditionalize availability of StdioTransport

* Update README with information about platform support
This commit is contained in:
Mattt 2025-04-10 09:15:13 -07:00 • committed by GitHub
commit 960c1dda0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 141 additions and 131 deletions

View file

@ -6,7 +6,7 @@ Swift implementation of the [Model Context Protocol][mcp] (MCP).
- Swift 6.0+ (Xcode 16+) - Swift 6.0+ (Xcode 16+)
### Supported Platforms ## Platform Support
| Platform | Minimum Version | | Platform | Minimum Version |
|----------|----------------| |----------|----------------|
@ -15,9 +15,19 @@ Swift implementation of the [Model Context Protocol][mcp] (MCP).
| watchOS | 9.0+ | | watchOS | 9.0+ |
| tvOS | 16.0+ | | tvOS | 16.0+ |
| visionOS | 1.0+ | | visionOS | 1.0+ |
| Linux | ✓ [^1] | | Linux | ✓ |
| Windows | ✓ |
[^1]: Linux support requires glibc-based distributions such as Ubuntu, Debian, Fedora, CentOS, or RHEL. Alpine Linux and other musl-based distributions are not supported. > [!IMPORTANT]
> MCP's transport layer handles communication between clients and servers.
> The Swift SDK supports multiple transport mechanisms,
> with different platform availability:
>
> * `StdioTransport` is available on Apple platforms
> and Linux distributions with glibc, such as
> Ubuntu, Debian, Fedora, CentOS, or RHEL.
>
> * `NetworkTransport` is available only on Apple platforms.
## Installation ## Installation

View file

@ -9,12 +9,13 @@ import struct Foundation.Data
#endif #endif
// Import for specific low-level operations not yet in Swift System // Import for specific low-level operations not yet in Swift System
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) #if canImport(Darwin)
import Darwin.POSIX import Darwin.POSIX
#elseif os(Linux) #elseif canImport(Glibc)
import Glibc import Glibc
#endif #endif
#if canImport(Darwin) || canImport(Glibc)
/// Standard input/output transport implementation /// Standard input/output transport implementation
public actor StdioTransport: Transport { public actor StdioTransport: Transport {
private let input: FileDescriptor private let input: FileDescriptor
@ -61,7 +62,7 @@ public actor StdioTransport: Transport {
} }
private func setNonBlocking(fileDescriptor: FileDescriptor) throws { private func setNonBlocking(fileDescriptor: FileDescriptor) throws {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(Linux) #if canImport(Darwin) || canImport(Glibc)
// Get current flags // Get current flags
let flags = fcntl(fileDescriptor.rawValue, F_GETFL) let flags = fcntl(fileDescriptor.rawValue, F_GETFL)
guard flags >= 0 else { guard flags >= 0 else {
@ -75,7 +76,8 @@ public actor StdioTransport: Transport {
} }
#else #else
// For platforms where non-blocking operations aren't supported // For platforms where non-blocking operations aren't supported
throw MCPError.internalError("Setting non-blocking mode not supported on this platform") throw MCPError.internalError(
"Setting non-blocking mode not supported on this platform")
#endif #endif
} }
@ -103,7 +105,8 @@ public actor StdioTransport: Transport {
pendingData = pendingData[(newlineIndex + 1)...] pendingData = pendingData[(newlineIndex + 1)...]
if !messageData.isEmpty { if !messageData.isEmpty {
logger.debug("Message received", metadata: ["size": "\(messageData.count)"]) logger.debug(
"Message received", metadata: ["size": "\(messageData.count)"])
messageContinuation.yield(Data(messageData)) messageContinuation.yield(Data(messageData))
} }
} }
@ -130,11 +133,7 @@ public actor StdioTransport: Transport {
public func send(_ message: Data) async throws { public func send(_ message: Data) async throws {
guard isConnected else { guard isConnected else {
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(Linux)
throw MCPError.transportError(Errno(rawValue: ENOTCONN)) throw MCPError.transportError(Errno(rawValue: ENOTCONN))
#else
throw MCPError.internalError("Transport not connected")
#endif
} }
// Add newline as delimiter // Add newline as delimiter
@ -170,3 +169,4 @@ public actor StdioTransport: Transport {
} }
} }
} }
#endif