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
49 changes: 36 additions & 13 deletions MCPClient/Sources/stdioTransport/DataChannel+StdioProcess.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import Foundation
import JSONRPC
import MCPInterface
Expand Down Expand Up @@ -76,13 +75,15 @@ extension Transport {
let process = Process()
// In MacOS, zsh is the default since macOS Catalina 10.15.7. We can safely assume it is available.
process.launchPath = "/bin/zsh"

if let executable = path(for: executable, env: env) {
// If executable is found directly, use user-provided env or current process env
process.environment = env ?? ProcessInfo.processInfo.environment
let command = "\(executable) \(args.joined(separator: " "))"
process.arguments = ["-c"] + [command]
process.environment = env ?? ProcessInfo.processInfo.environment
} else {
// If we cannot locate the executable, try loading the default environment for zsh, as the current process might not have the correct PATH.
process.environment = try loadZshEnvironment()
// If we cannot locate the executable, try loading the default environment for zsh
process.environment = try loadZshEnvironment(userEnv: env)
let command = "\(executable) \(args.joined(separator: " "))"
process.arguments = ["-c"] + [command]
}
Expand Down Expand Up @@ -208,19 +209,41 @@ extension Transport {
return executablePath
}

private static func loadZshEnvironment() throws -> [String: String] {
let process = Process()
process.launchPath = "/bin/zsh"
// Those are loaded for interactive login shell by zsh:
// https://www.freecodecamp.org/news/how-do-zsh-configuration-files-work/
process.arguments = ["-c", "source ~/.zshenv; source ~/.zprofile; source ~/.zshrc; source ~/.zshrc; printenv"]
let env = try getProcessStdout(process: process)
private static func loadZshEnvironment(userEnv: [String: String]? = nil) throws -> [String: String] {
// Load shell environment as base
let shellProcess = Process()
shellProcess.executableURL = URL(fileURLWithPath: "/bin/zsh")

if let path = env?.split(separator: "\n").filter({ $0.starts(with: "PATH=") }).last {
return ["PATH": String(path.dropFirst("PATH=".count))]
// Set process environment - either use userEnv if it exists and isn't empty, or use system environment
if let env = userEnv, !env.isEmpty {
shellProcess.environment = env
} else {
shellProcess.environment = ProcessInfo.processInfo.environment
}

shellProcess.arguments = ["-ilc", "printenv"]

let outputPipe = Pipe()
shellProcess.standardOutput = outputPipe
shellProcess.standardError = Pipe()

try shellProcess.run()
shellProcess.waitUntilExit()

let data = outputPipe.fileHandleForReading.readDataToEndOfFile()
guard let outputString = String(data: data, encoding: .utf8) else {
logger.error("Failed to read environment from shell.")
return ProcessInfo.processInfo.environment
}

// Parse shell environment
return outputString
.split(separator: "\n")
.reduce(into: [String: String]()) { result, line in
let components = line.split(separator: "=", maxSplits: 1)
guard components.count == 2 else { return }
result[String(components[0])] = String(components[1])
}
}

private static func getProcessStdout(process: Process) throws -> String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
7B73E9F52D814ABE00BF0CD1 /* MCPClient in Frameworks */ = {isa = PBXBuildFile; productRef = 7B73E9F42D814ABE00BF0CD1 /* MCPClient */; };
7BDD62DB2D764B3D00E18088 /* SwiftAnthropic in Frameworks */ = {isa = PBXBuildFile; productRef = 7BDD62DA2D764B3D00E18088 /* SwiftAnthropic */; };
7BDD62ED2D764B7C00E18088 /* MCPClient in Frameworks */ = {isa = PBXBuildFile; productRef = 7BDD62EC2D764B7C00E18088 /* MCPClient */; };
7BDD644F2D7811BA00E18088 /* SwiftOpenAI in Frameworks */ = {isa = PBXBuildFile; productRef = 7BDD644E2D7811BA00E18088 /* SwiftOpenAI */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -58,9 +58,9 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
7BDD62ED2D764B7C00E18088 /* MCPClient in Frameworks */,
7BDD644F2D7811BA00E18088 /* SwiftOpenAI in Frameworks */,
7BDD62DB2D764B3D00E18088 /* SwiftAnthropic in Frameworks */,
7B73E9F52D814ABE00BF0CD1 /* MCPClient in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -130,8 +130,8 @@
name = MCPClientChat;
packageProductDependencies = (
7BDD62DA2D764B3D00E18088 /* SwiftAnthropic */,
7BDD62EC2D764B7C00E18088 /* MCPClient */,
7BDD644E2D7811BA00E18088 /* SwiftOpenAI */,
7B73E9F42D814ABE00BF0CD1 /* MCPClient */,
);
productName = MCPClientChat;
productReference = 7BDD62A92D764A4A00E18088 /* MCPClientChat.app */;
Expand Down Expand Up @@ -217,8 +217,8 @@
minimizedProjectReferenceProxies = 1;
packageReferences = (
7BDD62D72D764ADA00E18088 /* XCRemoteSwiftPackageReference "SwiftAnthropic" */,
7BDD62D82D764B1800E18088 /* XCLocalSwiftPackageReference "../../../mcp-swift-sdk" */,
7BDD644D2D7811B300E18088 /* XCRemoteSwiftPackageReference "SwiftOpenAI" */,
7B73E9F32D814ABE00BF0CD1 /* XCLocalSwiftPackageReference "../../../mcp-swift-sdk" */,
);
preferredProjectObjectVersion = 77;
productRefGroup = 7BDD62AA2D764A4A00E18088 /* Products */;
Expand Down Expand Up @@ -576,7 +576,7 @@
/* End XCConfigurationList section */

/* Begin XCLocalSwiftPackageReference section */
7BDD62D82D764B1800E18088 /* XCLocalSwiftPackageReference "../../../mcp-swift-sdk" */ = {
7B73E9F32D814ABE00BF0CD1 /* XCLocalSwiftPackageReference "../../../mcp-swift-sdk" */ = {
isa = XCLocalSwiftPackageReference;
relativePath = "../../../mcp-swift-sdk";
};
Expand All @@ -602,16 +602,15 @@
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
7B73E9F42D814ABE00BF0CD1 /* MCPClient */ = {
isa = XCSwiftPackageProductDependency;
productName = MCPClient;
};
7BDD62DA2D764B3D00E18088 /* SwiftAnthropic */ = {
isa = XCSwiftPackageProductDependency;
package = 7BDD62D72D764ADA00E18088 /* XCRemoteSwiftPackageReference "SwiftAnthropic" */;
productName = SwiftAnthropic;
};
7BDD62EC2D764B7C00E18088 /* MCPClient */ = {
isa = XCSwiftPackageProductDependency;
package = 7BDD62D82D764B1800E18088 /* XCLocalSwiftPackageReference "../../../mcp-swift-sdk" */;
productName = MCPClient;
};
7BDD644E2D7811BA00E18088 /* SwiftOpenAI */ = {
isa = XCSwiftPackageProductDependency;
package = 7BDD644D2D7811B300E18088 /* XCRemoteSwiftPackageReference "SwiftOpenAI" */;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading