Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.
Merged
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
8 changes: 6 additions & 2 deletions MCPClient/Sources/Process+extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 =
Expand All @@ -51,6 +54,7 @@ extension Process {
}
}
}
#endif

// MARK: - CommandError

Expand Down
10 changes: 9 additions & 1 deletion MCPClient/Sources/stdioTransport/DataChannel+StdioProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [],
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -278,7 +286,7 @@ extension Transport {

return String(data: stdoutData, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines)
}

#endif
}

// MARK: - Lifetime
Expand Down