Skip to content

Commit d55140b

Browse files
authored
Import System package when available (#34)
* Import System package when available * Add isResourceTemporarilyUnavailable helper method to reduce number of imports of System / SystemPackage
1 parent 97b756c commit d55140b

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

Sources/MCP/Base/Error.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import Foundation
22

3+
#if canImport(System)
4+
import System
5+
#else
6+
@preconcurrency import SystemPackage
7+
#endif
8+
39
/// A model context protocol error.
410
public enum Error: Sendable {
511
// Standard JSON-RPC 2.0 errors (-32700 to -32603)
@@ -29,6 +35,20 @@ public enum Error: Sendable {
2935
case .transportError: return -32001
3036
}
3137
}
38+
39+
/// Check if an error represents a "resource temporarily unavailable" condition
40+
public static func isResourceTemporarilyUnavailable(_ error: Swift.Error) -> Bool {
41+
#if canImport(System)
42+
if let errno = error as? System.Errno, errno == .resourceTemporarilyUnavailable {
43+
return true
44+
}
45+
#else
46+
if let errno = error as? SystemPackage.Errno, errno == .resourceTemporarilyUnavailable {
47+
return true
48+
}
49+
#endif
50+
return false
51+
}
3252
}
3353

3454
// MARK: LocalizedError

Sources/MCP/Base/Transports.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import Darwin
22
import Logging
3-
import SystemPackage
43

54
import struct Foundation.Data
65

6+
#if canImport(System)
7+
import System
8+
#else
9+
@preconcurrency import SystemPackage
10+
#endif
11+
712
/// Protocol defining the transport layer for MCP communication
813
public protocol Transport: Actor {
914
var logger: Logger { get }
@@ -107,7 +112,7 @@ public actor StdioTransport: Transport {
107112
messageContinuation.yield(message)
108113
}
109114
}
110-
} catch let error as Errno where error == .resourceTemporarilyUnavailable {
115+
} catch let error where Error.isResourceTemporarilyUnavailable(error) {
111116
try? await Task.sleep(nanoseconds: 10_000_000) // 10ms backoff
112117
continue
113118
} catch {
@@ -147,7 +152,7 @@ public actor StdioTransport: Transport {
147152
if written > 0 {
148153
remaining = remaining.dropFirst(written)
149154
}
150-
} catch let error as Errno where error == .resourceTemporarilyUnavailable {
155+
} catch let error where Error.isResourceTemporarilyUnavailable(error) {
151156
try await Task.sleep(nanoseconds: 10_000_000) // 10ms backoff
152157
continue
153158
} catch {

Sources/MCP/Client/Client.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Logging
2-
import SystemPackage
32

43
import struct Foundation.Data
54
import struct Foundation.Date
@@ -180,7 +179,7 @@ public actor Client {
180179
"Unexpected message received by client", metadata: metadata)
181180
}
182181
}
183-
} catch let error as Errno where error == .resourceTemporarilyUnavailable {
182+
} catch let error where Error.isResourceTemporarilyUnavailable(error) {
184183
try? await Task.sleep(nanoseconds: 10_000_000) // 10ms
185184
continue
186185
} catch {

Sources/MCP/Server/Server.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Logging
2-
import SystemPackage
32

43
import struct Foundation.Data
54
import struct Foundation.Date
@@ -202,7 +201,7 @@ public actor Server {
202201
}
203202
throw Error.parseError("Invalid message format")
204203
}
205-
} catch let error as Errno where error == .resourceTemporarilyUnavailable {
204+
} catch let error where Error.isResourceTemporarilyUnavailable(error) {
206205
// Resource temporarily unavailable, retry after a short delay
207206
try? await Task.sleep(nanoseconds: 10_000_000) // 10ms
208207
continue

Tests/MCPTests/RoundtripTests.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import Foundation
22
import Logging
3-
import SystemPackage
43
import Testing
54

65
@testable import MCP
76

7+
#if canImport(System)
8+
import System
9+
#else
10+
@preconcurrency import SystemPackage
11+
#endif
12+
813
@Suite("Roundtrip Tests")
914
struct RoundtripTests {
1015
@Test(

Tests/MCPTests/TransportTests.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import Foundation
2-
import SystemPackage
32
import Testing
43

54
@testable import MCP
65

6+
#if canImport(System)
7+
import System
8+
#else
9+
@preconcurrency import SystemPackage
10+
#endif
11+
712
@Suite("Stdio Transport Tests")
813
struct StdioTransportTests {
914
@Test("Connection")

0 commit comments

Comments
 (0)