Cyrus on Discord: once a Library MCP server's token was written as
${NAME}, the Library listed no tools for it. magpie expanded the
reference from its own environment only, and a magpie opened from the
Finder or the Dock has none of the profile's variables, so a token
exported in .zshrc was "not set" for the tool-list check, and the
router (dialHub) and Codex's run through the gateway (specOf) didn't
start the server either.
expandAll, the one place all three expand a reference, now takes a
variable magpie's environment hasn't from proc.LoginVars: what the
login shell (and fish or nushell, as shellEnv asks them) exports, read
through /bin/sh as the existing PATH probe is, each answer kept a
minute. On Windows it reads the registry's user then machine variables
(one set with setx after magpie started). The value is only in memory
at call time: never set in magpie's environment, never written to
library.json or an agent's file, never logged. A variable set nowhere
is still novar, naming it ("NAME not set" on the page, unchanged). The
library only documents ${NAME} (no ${NAME:-default}), so nothing else
is parsed.
Run at this head (90d554db1 rebased on 46a13f837), on mote:
- go test -tags nogui ./internal/proc ./internal/library: ok
- GOOS=linux and GOOS=windows go vet ./internal/proc ./internal/library: ok
- gofmt -l: clean
- wsl-run (magpie-desktop): TestLoginVars, TestEnvRefs*, TestCheckStdio,
TestCheckHTTP, TestShellEnv*, TestLoginEnv*: ok
- ./internal/cli before the rebase: only TestEveryRouteHasACommandOrIsNamed
red, POST /api/usage/checkin-account, already filed as #4826.
- By hand on mote: LoginVars with the real /bin/zsh (.zshrc) and
/bin/bash (.bash_profile) in a temp HOME read the exported token and
left an unset name unset.
TestEnvRefsFromLoginShell: a fake login shell exports CYRUS_TOKEN,
magpie's environment hasn't it; a fake stdio server that exits without
the right token and an http one that answers 401 without it are each
checked (ok, with tools) and dialled by the router, Codex's run spec has
the value, a variable set nowhere is novar, and after SaveServer, a
check and a Sync no file in the sandbox HOME holds the token.
TestLoginVars: exported, exported-empty, unexported and unset names, bad
names never asked, the minute's cache and a new SHELL asked again.
Broken by hand: loginVars returning nothing made TestEnvRefsFromLoginShell
red (command and url both novar CYRUS_TOKEN, the reporter's case);
parseNamed ignoring the set flag made TestLoginVars red.
Not tried: the Windows registry read (compiled by vet only, no run on
xiaomi-gpu); a real Finder-launched magpie.app with a real token; the
GUI suite (no GUI file changed).
143 lines
4.5 KiB
Go
143 lines
4.5 KiB
Go
package proc
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"golang.org/x/sys/windows/registry"
|
|
|
|
"github.com/yetone/magpie/internal/appdir"
|
|
)
|
|
|
|
// UserPath gives magpie, and what it starts, the Node a terminal has from
|
|
// fnm (#2685). An app started from the Start menu gets the user's PATH from
|
|
// the registry, as a terminal does, but fnm puts its Node on PATH from the
|
|
// shell's profile (`fnm env | Invoke-Expression`) and never in the
|
|
// registry: an MCP server run with npx, even by npx.cmd's full path, died
|
|
// with "'node' is not recognized" once the package it fetched ran node.
|
|
// The folder fnm's default alias names is added after the ones PATH has.
|
|
func UserPath() {
|
|
home, _ := os.UserHomeDir()
|
|
addPath(fnmWinPath(home, appdir.Getenv("APPDATA"), os.Getenv("FNM_DIR")))
|
|
}
|
|
|
|
// UserBinDirs are the folders a user's command-line tools are installed in
|
|
// that exist here — npm's global one, bun's, volta's, pnpm's, mise's
|
|
// shims, the standalone installers', each Node version of fnm's, its
|
|
// default first — for finding one PATH doesn't reach.
|
|
func UserBinDirs() []string {
|
|
home, _ := os.UserHomeDir()
|
|
// mise's data folder: MISE_DATA_DIR, else XDG_DATA_HOME's mise (which
|
|
// mise follows on Windows too), else LOCALAPPDATA's
|
|
mise := filepath.Join(appdir.Getenv("LOCALAPPDATA"), "mise")
|
|
if d := os.Getenv("XDG_DATA_HOME"); filepath.IsAbs(d) {
|
|
mise = filepath.Join(d, "mise")
|
|
}
|
|
if d := os.Getenv("MISE_DATA_DIR"); filepath.IsAbs(d) {
|
|
mise = d
|
|
}
|
|
known := []string{
|
|
filepath.Join(appdir.Getenv("APPDATA"), "npm"),
|
|
filepath.Join(home, ".local", "bin"),
|
|
filepath.Join(home, ".bun", "bin"),
|
|
filepath.Join(appdir.Getenv("LOCALAPPDATA"), "Volta", "bin"),
|
|
filepath.Join(appdir.Getenv("LOCALAPPDATA"), "pnpm"),
|
|
filepath.Join(mise, "shims"),
|
|
}
|
|
for _, r := range fnmWinRoots(home, appdir.Getenv("APPDATA"), os.Getenv("FNM_DIR")) {
|
|
known = append(known, fnmWinBins(r)...)
|
|
}
|
|
var have []string
|
|
for _, d := range known {
|
|
if st, err := os.Stat(d); filepath.IsAbs(d) && err == nil && st.IsDir() {
|
|
have = append(have, d)
|
|
}
|
|
}
|
|
return have
|
|
}
|
|
|
|
// LoginPath is the PATH the registry has now, the machine's then the
|
|
// user's: what a terminal opened now gets, and what a magpie started
|
|
// before a CLI was installed doesn't.
|
|
func LoginPath() []string {
|
|
var dirs []string
|
|
for _, k := range []struct {
|
|
root registry.Key
|
|
path string
|
|
}{
|
|
{registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Control\Session Manager\Environment`},
|
|
{registry.CURRENT_USER, `Environment`},
|
|
} {
|
|
key, err := registry.OpenKey(k.root, k.path, registry.QUERY_VALUE)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if v, _, err := key.GetStringValue("Path"); err == nil {
|
|
// %SystemRoot%\system32 as Windows expands it: os.ExpandEnv of
|
|
// $SystemRoot$\system32 made C:\Windows$\system32
|
|
if x, err := registry.ExpandString(v); err == nil {
|
|
v = x
|
|
}
|
|
for _, d := range strings.Split(v, ";") {
|
|
if d = strings.TrimSpace(d); d != "" {
|
|
dirs = append(dirs, d)
|
|
}
|
|
}
|
|
}
|
|
key.Close()
|
|
}
|
|
return dirs
|
|
}
|
|
|
|
// LoginEnv is "" on Windows: an app started from the Start menu gets the
|
|
// user's variables from the registry, as a terminal does, so magpie's own
|
|
// environment has them.
|
|
func LoginEnv(string) string { return "" }
|
|
|
|
// LoginEnvKnown is "" on Windows too (LoginEnv).
|
|
func LoginEnvKnown(string) string { return "" }
|
|
|
|
// ShellPath is LoginPath on Windows: PowerShell and cmd both take the
|
|
// registry's PATH.
|
|
func ShellPath(string) []string { return LoginPath() }
|
|
|
|
// LoginVars are the values the registry has now for names, the user's
|
|
// over the machine's: what a terminal opened now gets, and what a magpie
|
|
// started before the variable was set (setx, the Environment Variables
|
|
// dialog) doesn't. A ${NAME} in an MCP server's headers or environment is
|
|
// read so where magpie's own environment hasn't it. The values are only
|
|
// returned, never set in magpie's environment or logged.
|
|
func LoginVars(names []string) map[string]string {
|
|
out := map[string]string{}
|
|
for _, k := range []struct {
|
|
root registry.Key
|
|
path string
|
|
}{
|
|
{registry.CURRENT_USER, `Environment`},
|
|
{registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Control\Session Manager\Environment`},
|
|
} {
|
|
key, err := registry.OpenKey(k.root, k.path, registry.QUERY_VALUE)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, n := range names {
|
|
if _, have := out[n]; have || n == "" {
|
|
continue
|
|
}
|
|
v, typ, err := key.GetStringValue(n)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if typ == registry.EXPAND_SZ {
|
|
if x, err := registry.ExpandString(v); err == nil {
|
|
v = x
|
|
}
|
|
}
|
|
out[n] = v
|
|
}
|
|
key.Close()
|
|
}
|
|
return out
|
|
}
|