Skip to content

fix(secureenv): inherit user's interactive PATH so stdio servers find uvx/npx (closes #439) - #440

Merged
Dumbris merged 2 commits into
mainfrom
fix/issue-439-stdio-path-launchd
Apr 30, 2026
Merged

Dumbris merged 2 commits into
mainfrom
fix/issue-439-stdio-path-launchd

Conversation

@Dumbris

@Dumbris Dumbris commented Apr 30, 2026

Copy link
Copy Markdown
Member

Summary

Closes #439. When MCPProxy launches from a macOS .app bundle (Finder, Dock, Login Item), launchd hands the process PATH=/usr/bin:/bin:/usr/sbin:/sbin (4 entries). Stdio MCP servers then can't resolve uvx, npx, node, brew, etc., because none of those live on the system-bin set.

The existing PATH-enhancement gate len(pathParts) <= 2 (in internal/secureenv/manager.go) blocked enhancement for that 4-entry launchd PATH, so enhancement never triggered for the headline scenario in the issue. PR #420 was a related fix but only covered the PKG postinstall launch path.

What this PR changes

  • Drop the len(pathParts) <= 2 gate. EnhancePath is already an explicit opt-in (set only for stdio upstreams in internal/upstream/core/client.go:155); the additional path-length condition was redundant and turned out to be the bug.
  • Merge in the user's interactive login-shell PATH via shellwrap.LoginShellPATH so non-standard installs (mise/asdf/Colima/custom shims) get picked up — not just /usr/local/bin and /opt/homebrew/bin. The login-shell capture is already cached process-wide via sync.Once.
  • Harden shellwrap.LoginShellPATH against rc-file banner contamination. Bracket the captured $PATH with unique markers so welcome messages / oh-my-zsh updates / motd lines on stdout don't get prepended onto the value. Previously, banner output mixed with PATH made exec.LookPath fail to resolve anything.

The new compose order in buildEnhancedPath: login-shell PATH → static-discovery → existing PATH. Static discovery acts as a deterministic floor when login-shell capture is unavailable / contaminated / shell-less. PATHs already containing /usr/local/bin or /opt/homebrew/bin are returned untouched (terminal launches stay clean).

Test plan

  • go test ./internal/secureenv/... ./internal/shellwrap/... -count=1 -race — all green, including new tests
  • go test ./internal/upstream/... — green
  • go build ./cmd/mcpproxy (personal) and go build -tags server ./cmd/mcpproxy (server) — both clean
  • ./scripts/run-linter.sh — 0 issues
  • Local repro on macOS: env -i PATH=/usr/bin:/bin:/usr/sbin:/sbin HOME=$HOME ./mcpproxy serve … with a npx-based stdio server connects + initializes. A focused demo program shows BuildSecureEnvironment() returns 14 PATH entries (resolving uvx/npx/node/brew) under launchd-minimal input; on main the same scenario yields the unchanged 4-entry PATH and all four tools fail to resolve.

New tests

  • internal/secureenv/launchd_path_test.go — launchd 4-entry PATH must be enhanced; login-shell entries rank ahead of launchd-minimal ones; static discovery still works when login-shell capture is empty; comprehensive PATH is left untouched.
  • internal/shellwrap/shellwrap_test.go (TestLoginShellPATH_StripsBannerContamination) — banner stdout from rc files is rejected by the marker parser; only the bracketed $PATH value is returned.

🤖 Generated with Claude Code

… uvx/npx when launched from Finder/LoginItem (#439)

When MCPProxy launches from a macOS .app bundle (Finder, Dock, Login Item),
launchd hands the process PATH=/usr/bin:/bin:/usr/sbin:/sbin (4 entries).
Stdio MCP servers then can't resolve uvx, npx, node, brew, etc., because
none of those live on the system-bin set.

The existing PATH-enhancement gate `len(pathParts) <= 2` blocked enhancement
for the launchd 4-entry case. Drop that gate (EnhancePath is already an
explicit opt-in for stdio in core/client.go) and merge in the user's
interactive login-shell PATH via shellwrap.LoginShellPATH so non-standard
installs (mise/asdf/Colima/custom shims) are also picked up.

Also harden shellwrap.LoginShellPATH against rc-file banner contamination:
bracket the captured $PATH with unique markers so welcome messages /
oh-my-zsh updates / motd lines on stdout no longer get prepended onto the
PATH value (they previously made exec.LookPath fail to resolve anything).

Tests:
- internal/secureenv/launchd_path_test.go — covers launchd-style 4-entry
  PATH, login-shell merge priority, fallback when login shell is broken,
  and the "already-comprehensive PATH" no-op path.
- internal/shellwrap/shellwrap_test.go — TestLoginShellPATH_StripsBannerContamination
  verifies banner stdout is rejected by the marker parser.

Verified locally: with env -i PATH=/usr/bin:/bin:/usr/sbin:/sbin on macOS,
secureenv.BuildSecureEnvironment now produces a 14-entry PATH that resolves
uvx, npx, node, python3, brew. Without the fix, the same env yields the
unchanged 4-entry launchd PATH and the four GUI tools all fail to resolve.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Apr 30, 2026 •

Copy link
Copy Markdown

…PathEnhancement

Post-#439 the buildEnhancedPath compose order is `login-shell PATH →
discovered → existing`. On hosts where `bash -l -c '…'` does not enrich
the ambient PATH (e.g. Linux CI runners — non-interactive bash does not
read /etc/environment), the captured login-shell PATH equals the test's
ambient `/usr/bin:/bin` and `/usr/local/bin` is added by static discovery
*after* `/usr/bin`. The user-visible guarantee (`/usr/local/bin` is
findable on PATH) is unchanged; only the implementation detail of strict
position relative to `/usr/bin` differs.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: fix/issue-439-stdio-path-launchd

Available Artifacts

  • archive-darwin-amd64 (26 MB)
  • archive-darwin-arm64 (23 MB)
  • archive-linux-amd64 (15 MB)
  • archive-linux-arm64 (13 MB)
  • archive-windows-amd64 (26 MB)
  • archive-windows-arm64 (23 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (19 MB)
  • installer-dmg-darwin-arm64 (18 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 25151523103 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

@Dumbris
Dumbris merged commit 2e47b9c into main Apr 30, 2026
23 checks passed
@Dumbris
Dumbris deleted the fix/issue-439-stdio-path-launchd branch April 30, 2026 07:00
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stdio servers can't find binaries (uvx, npx) when launched from macOS .app

3 participants