Root-caused two real issues Rune hit with Obsidian/Homepage external MCP servers: 1. Toggling a server's enable switch silently wiped transportKind/env/ url/bearerToken/headers back to stdio defaults (only id/name/command/ args/isEnabled/timeout/createdAt were preserved) — almost certainly how Obsidian's config got corrupted into an empty-command stdio entry despite never being edited directly. Fixed via ExternalMCPServer.withEnabledToggled(), which flips only isEnabled. 2. npx (installed via Homebrew) was invisible to Confab because GUI apps only inherit launchd's minimal PATH, not the Terminal PATH. Tried spawning the user's login shell to ask for its real PATH — this caused two real hangs in one session (first an -ilc pipe deadlock, then a waitUntilExit()/CFRunLoop reentrancy issue even after fixing that) and was abandoned entirely in favor of LoginShellEnvironment: deterministic, subprocess-free directory probing (Homebrew, MacPorts, Volta, nvm's alias file) that can't hang by construction. Also added: - Edit capability for existing External MCP servers (previously only Add/Toggle/Delete) — the second thing Rune explicitly asked for, and the way to fix a corrupted entry like Obsidian's without deleting it. - MCPClientError.commandNotFound: a stdio server's command is checked against PATH up front in StdioMCPTransport.prepare() and fails immediately with a clear reason instead of cycling through 3 rounds of crash/restart backoff (5s/15s/30s) for a permanently-missing binary. - A "Get Node.js" button appears when this happens, opening a sheet with a copyable `brew install node`, a one-click install (via NodeInstallHelper, using the terminationHandler/readabilityHandler pattern already proven safe elsewhere in this file — deliberately not waitUntilExit()), or a nodejs.org link if Homebrew isn't present. - ExternalMCPManager.retryClient(id:) to manually retry after fixing the underlying cause. - Help book: new "Servers That Use npx" section, updated Server Status section, updated Settings blurb. 37 new/changed tests covering the toggle fix, PATH probing, the commandNotFound fast-fail path, and missing-command detection — full suite (374 tests) passes clean.
80 lines
3.3 KiB
Swift
80 lines
3.3 KiB
Swift
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
|
|
// Copyright (C) 2026 Rune Olsen
|
|
|
|
import Foundation
|
|
|
|
/// Runs `brew install node` on the user's explicit request (from `NodeInstallHelpSheet`), when a
|
|
/// stdio External MCP server's command couldn't be found and Homebrew is present.
|
|
///
|
|
/// Deliberately does NOT use `Process.waitUntilExit()` — see `LoginShellEnvironment`'s doc comment
|
|
/// in `MCPTransport.swift` for the full story: that call hung for real, twice, in this app's test
|
|
/// process, even with careful draining and multiple timeout layers. `terminationHandler` +
|
|
/// `readabilityHandler` (both callback-driven, no blocking wait) is the pattern this codebase
|
|
/// already uses safely elsewhere (`StdioMCPTransport.prepare()`), so this reuses it instead of
|
|
/// introducing a second instance of the primitive that just caused two real hangs.
|
|
enum NodeInstallHelper {
|
|
struct InstallResult: Sendable {
|
|
let success: Bool
|
|
let output: String
|
|
}
|
|
|
|
static func installNode(homebrewPrefix: String, timeout: TimeInterval = 180) async -> InstallResult {
|
|
await withCheckedContinuation { continuation in
|
|
let proc = Process()
|
|
proc.executableURL = URL(fileURLWithPath: "\(homebrewPrefix)/bin/brew")
|
|
proc.arguments = ["install", "node"]
|
|
let outPipe = Pipe()
|
|
let errPipe = Pipe()
|
|
proc.standardOutput = outPipe
|
|
proc.standardError = errPipe
|
|
|
|
let ioQueue = DispatchQueue(label: "confab.nodeinstall.io")
|
|
var collected = Data()
|
|
let resumeLock = NSLock()
|
|
var resumed = false
|
|
|
|
func finish(_ result: InstallResult) {
|
|
resumeLock.lock()
|
|
defer { resumeLock.unlock() }
|
|
guard !resumed else { return }
|
|
resumed = true
|
|
outPipe.fileHandleForReading.readabilityHandler = nil
|
|
errPipe.fileHandleForReading.readabilityHandler = nil
|
|
continuation.resume(returning: result)
|
|
}
|
|
|
|
outPipe.fileHandleForReading.readabilityHandler = { handle in
|
|
let chunk = handle.availableData
|
|
ioQueue.sync { collected.append(chunk) }
|
|
}
|
|
errPipe.fileHandleForReading.readabilityHandler = { handle in
|
|
let chunk = handle.availableData
|
|
ioQueue.sync { collected.append(chunk) }
|
|
}
|
|
|
|
proc.terminationHandler = { p in
|
|
ioQueue.sync {
|
|
let text = String(data: collected, encoding: .utf8) ?? ""
|
|
finish(InstallResult(success: p.terminationStatus == 0, output: text))
|
|
}
|
|
}
|
|
|
|
do {
|
|
try proc.run()
|
|
} catch {
|
|
finish(InstallResult(success: false, output: "Failed to launch brew: \(error.localizedDescription)"))
|
|
return
|
|
}
|
|
|
|
DispatchQueue.global().asyncAfter(deadline: .now() + timeout) {
|
|
guard proc.isRunning else { return }
|
|
proc.terminate()
|
|
ioQueue.sync {
|
|
let text = String(data: collected, encoding: .utf8) ?? ""
|
|
finish(InstallResult(success: false, output: text + "\n[Timed out after \(Int(timeout))s]"))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|