Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.

Commit 9674388

Browse files
wangqiqiku
andauthored
Process+extensions.swift and DataChannel+StdioProcess.swift are changed to make the Process-dependent functionality only available on platforms where the API exists. (#28)
Co-authored-by: wangqi <[email protected]>
1 parent 0cd9520 commit 9674388

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

MCPClient/Sources/Process+extensions.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33

44
import Foundation
55

6+
// MARK: - Process Extensions (macOS only)
7+
8+
#if os(macOS)
69
extension Process {
710
/// Launches process.
811
///
912
/// - throws: CommandError.inAccessibleExecutable if command could not be executed.
10-
func launchThrowably() throws {
13+
public func launchThrowably() throws {
1114
#if !os(macOS)
1215
guard Files.isExecutableFile(atPath: executableURL!.path) else {
1316
throw CommandError.inAccessibleExecutable(path: executableURL!.lastPathComponent)
@@ -32,7 +35,7 @@ extension Process {
3235
///
3336
/// - throws: `CommandError.returnedErrorCode(command: String, errorcode: Int)`
3437
/// if the exit code is anything but 0.
35-
func finish() throws {
38+
public func finish() throws {
3639
/// The full path to the executable + all arguments, each one quoted if it contains a space.
3740
func commandAsString() -> String {
3841
let path: String =
@@ -51,6 +54,7 @@ extension Process {
5154
}
5255
}
5356
}
57+
#endif
5458

5559
// MARK: - CommandError
5660

MCPClient/Sources/stdioTransport/DataChannel+StdioProcess.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ extension JSONRPCSetupError: LocalizedError {
4444

4545
extension Transport {
4646

47+
#if os(macOS)
4748
/// Creates a new `Transport` by launching the given executable with the specified arguments and attaching to its standard IO.
49+
/// This functionality is only available on macOS.
4850
public static func stdioProcess(
4951
_ executable: String,
5052
args: [String] = [],
@@ -105,6 +107,7 @@ extension Transport {
105107
}
106108

107109
/// Creates a new `Transport` by launching the given process and attaching to its standard IO.
110+
/// This functionality is only available on macOS.
108111
public static func stdioProcess(
109112
unlaunchedProcess process: Process,
110113
verbose: Bool = false)
@@ -193,6 +196,7 @@ extension Transport {
193196
}
194197

195198
/// Finds the full path to the executable using the `which` command.
199+
/// This functionality is only available on macOS.
196200
private static func locate(executable: String, env: [String: String]? = nil) throws -> String {
197201
let process = Process()
198202
process.executableURL = URL(fileURLWithPath: "/usr/bin/which")
@@ -209,6 +213,8 @@ extension Transport {
209213
return executablePath
210214
}
211215

216+
/// Loads the zsh environment variables.
217+
/// This functionality is only available on macOS.
212218
private static func loadZshEnvironment(userEnv: [String: String]? = nil) throws -> [String: String] {
213219
// Load shell environment as base
214220
let shellProcess = Process()
@@ -246,6 +252,8 @@ extension Transport {
246252
}
247253
}
248254

255+
/// Gets the standard output from a process.
256+
/// This functionality is only available on macOS.
249257
private static func getProcessStdout(process: Process) throws -> String? {
250258
let stdout = Pipe()
251259
let stderr = Pipe()
@@ -278,7 +286,7 @@ extension Transport {
278286

279287
return String(data: stdoutData, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines)
280288
}
281-
289+
#endif
282290
}
283291

284292
// MARK: - Lifetime

0 commit comments

Comments
 (0)