From 95303abe9bcf16071a5c73a247996d04eeec92d6 Mon Sep 17 00:00:00 2001 From: wangqi Date: Mon, 28 Apr 2025 14:56:24 -0400 Subject: [PATCH] Process+extensions.swift and DataChannel+StdioProcess.swift are changed to make the Process-dependent functionality only available on platforms where the API exists. --- MCPClient/Sources/Process+extensions.swift | 8 ++++++-- .../stdioTransport/DataChannel+StdioProcess.swift | 10 +++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/MCPClient/Sources/Process+extensions.swift b/MCPClient/Sources/Process+extensions.swift index bcb6a73..d1caf16 100644 --- a/MCPClient/Sources/Process+extensions.swift +++ b/MCPClient/Sources/Process+extensions.swift @@ -3,11 +3,14 @@ import Foundation +// MARK: - Process Extensions (macOS only) + +#if os(macOS) extension Process { /// Launches process. /// /// - throws: CommandError.inAccessibleExecutable if command could not be executed. - func launchThrowably() throws { + public func launchThrowably() throws { #if !os(macOS) guard Files.isExecutableFile(atPath: executableURL!.path) else { throw CommandError.inAccessibleExecutable(path: executableURL!.lastPathComponent) @@ -32,7 +35,7 @@ extension Process { /// /// - throws: `CommandError.returnedErrorCode(command: String, errorcode: Int)` /// if the exit code is anything but 0. - func finish() throws { + public func finish() throws { /// The full path to the executable + all arguments, each one quoted if it contains a space. func commandAsString() -> String { let path: String = @@ -51,6 +54,7 @@ extension Process { } } } +#endif // MARK: - CommandError diff --git a/MCPClient/Sources/stdioTransport/DataChannel+StdioProcess.swift b/MCPClient/Sources/stdioTransport/DataChannel+StdioProcess.swift index 8ed97fd..8f1df10 100644 --- a/MCPClient/Sources/stdioTransport/DataChannel+StdioProcess.swift +++ b/MCPClient/Sources/stdioTransport/DataChannel+StdioProcess.swift @@ -44,7 +44,9 @@ extension JSONRPCSetupError: LocalizedError { extension Transport { +#if os(macOS) /// Creates a new `Transport` by launching the given executable with the specified arguments and attaching to its standard IO. + /// This functionality is only available on macOS. public static func stdioProcess( _ executable: String, args: [String] = [], @@ -105,6 +107,7 @@ extension Transport { } /// Creates a new `Transport` by launching the given process and attaching to its standard IO. + /// This functionality is only available on macOS. public static func stdioProcess( unlaunchedProcess process: Process, verbose: Bool = false) @@ -193,6 +196,7 @@ extension Transport { } /// Finds the full path to the executable using the `which` command. + /// This functionality is only available on macOS. private static func locate(executable: String, env: [String: String]? = nil) throws -> String { let process = Process() process.executableURL = URL(fileURLWithPath: "/usr/bin/which") @@ -209,6 +213,8 @@ extension Transport { return executablePath } + /// Loads the zsh environment variables. + /// This functionality is only available on macOS. private static func loadZshEnvironment(userEnv: [String: String]? = nil) throws -> [String: String] { // Load shell environment as base let shellProcess = Process() @@ -246,6 +252,8 @@ extension Transport { } } + /// Gets the standard output from a process. + /// This functionality is only available on macOS. private static func getProcessStdout(process: Process) throws -> String? { let stdout = Pipe() let stderr = Pipe() @@ -278,7 +286,7 @@ extension Transport { return String(data: stdoutData, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) } - +#endif } // MARK: - Lifetime