From cdf325f17846d8830f4331aa56e7b29a65923441 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 8 Oct 2026 23:09:25 +0800 Subject: [PATCH] plugins: a package that is only a command (an MCP server like magpie-x-search) isn't listed under Unreviewed, isn't added as a plugin, and one added before says what it is instead of a host stack (#1327, congee949) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A repository tagged magpie-plugin whose npm copy (from that repository) names no file to load (no main, no exports) was listed and installed from npm without the entry check the GitHub route has; it then failed every host start with "has no entry" and a stack. Now: - the Unreviewed list checks npm's copy the same way: one that names no file needs the index.js the repository's own route needs (askTagged); - `magpie plugin add` / the Plugins page refuse a package that names no file, has no index file and is neither pi's nor a middleware, take the package just installed for it out again, and say it isn't an OpenCode or magpie plugin, and, for one with a bin, to add an MCP server under Library → MCP servers (notPlugin); - the host says the same, without a stack, for one already on the list. Run: TestCommandOnlyPackageIsNoPlugin (magpie-x-search 1.2.0's real package.json) fails without the change (Add = ; with only host.js reverted, the stack message), TestTaggedRepos with the real GitHub package.json and npm latest of cyberElar/magpie-x-search fails without it (listed), both pass with it; TestNotPluginKeepsPlugins (index.mjs, main, exports, middleware, pi, a file stay plugins). go test -tags nogui ./internal/plugin under a temp HOME ok; go vet -tags nogui for darwin, linux, windows. The built binary in a sandbox HOME: `magpie plugin add magpie-x-search@latest` from real npm prints the new error, and plugins/package.json no longer lists it. Co-Authored-By: Claude --- internal/plugin/github.go | 15 +++- internal/plugin/github_test.go | 5 +- internal/plugin/host.js | 9 ++- internal/plugin/market.go | 5 +- internal/plugin/notplugin_test.go | 74 +++++++++++++++++++ internal/plugin/store.go | 56 ++++++++++++++ .../testdata/github/npm/magpie-x-search.json | 1 + .../magpie-x-search/main/package.json | 33 +++++++++ .../notplugin/magpie-x-search/launcher.cjs | 2 + .../notplugin/magpie-x-search/package.json | 33 +++++++++ 10 files changed, 227 insertions(+), 6 deletions(-) create mode 100644 internal/plugin/notplugin_test.go create mode 100644 internal/plugin/testdata/github/npm/magpie-x-search.json create mode 100644 internal/plugin/testdata/github/raw/cyberElar/magpie-x-search/main/package.json create mode 100644 internal/plugin/testdata/notplugin/magpie-x-search/launcher.cjs create mode 100644 internal/plugin/testdata/notplugin/magpie-x-search/package.json diff --git a/internal/plugin/github.go b/internal/plugin/github.go index 5b1cf1b50..d6fb5c7c1 100644 --- a/internal/plugin/github.go +++ b/internal/plugin/github.go @@ -223,20 +223,31 @@ func askTagged(ctx context.Context) ([]Tagged, error) { // built only to publish), so it is installed from npm b, err := fetchJSON(pc, npmRegistry+"/"+npmPath(pj.Name)+"/latest", 1<<20) known := err == nil || errors.Is(err, errNotFound) + // npmNames is whether npm's copy names what it loads (main, + // exports); one that names nothing loads an index file + npmNames := false if err == nil { var l npmLatest if json.Unmarshal(b, &l) == nil && l.Version != "" && strings.EqualFold(githubRepo(repoURL(l.Repository)), t.Repo) { t.Spec, t.Version = pj.Name, l.Version + npmNames = l.Main != "" || len(l.Exports) > 0 } } // installed from the repository, the file it loads must be in // it: one whose dist is built only to publish couldn't load // (GitHub not answering is no answer, and keeps it; so is a - // name Bun would add .js or /index.js to) - if IsGit(t.Spec) { + // name Bun would add .js or /index.js to). Installed from npm, + // a copy that names no file loads index.js, which the + // repository has too when it is a plugin at all: a package + // that is only a command (bin), an MCP server, has none and + // would never load (#1327) + if IsGit(t.Spec) || !npmNames { entry := pj.Magpie.Middleware if t.Kind != "middleware" { entry = pkgEntry(pj.Exports, pj.Main) + if !IsGit(t.Spec) { + entry = pkgEntry(nil, "") + } } if path.Ext(entry) != "" { _, err := fetchJSON(pc, githubRaw+"/"+t.Repo+"/"+url.PathEscape(branch)+"/"+strings.TrimPrefix(path.Clean("/"+entry), "/"), 1) diff --git a/internal/plugin/github_test.go b/internal/plugin/github_test.go index 81465d752..6249913c3 100644 --- a/internal/plugin/github_test.go +++ b/internal/plugin/github_test.go @@ -52,6 +52,7 @@ func fakeGitHub(t *testing.T, up *atomic.Bool, asked *atomic.Int32) { like("someone/word-guard", map[string]any{"description": "word guard", "stargazers_count": 3, "license": nil}), like("alfaoz/opencode-see-image", nil), like("someone/flaky-entry", map[string]any{"license": map[string]any{"key": "other", "spdx_id": "NOASSERTION"}}), + like("cyberElar/magpie-x-search", nil), like("forker/opencode-claude-auth", map[string]any{"fork": true}), like("old/opencode-old", map[string]any{"archived": true}), like("magpie-community/plugins", nil)) @@ -122,7 +123,9 @@ func TestTaggedRepos(t *testing.T) { } // iPolloWork and learn-opencode (a workspace and a course) have no // index.js to load, nor see-image its dist: installed from GitHub, - // none would load + // none would load. magpie-x-search (real, #1327) is on npm from its + // repository, but a command alone, an MCP server: npm's copy names no + // file to load, and it has no index.js want := "rynfar/meridian griffinmartin/opencode-claude-auth slkiser/opencode-quota someone/word-guard someone/flaky-entry" if strings.Join(repos, " ") != want { t.Fatalf("repos %v\nwant %s", repos, want) diff --git a/internal/plugin/host.js b/internal/plugin/host.js index ad1a0bd06..93c680eae 100644 --- a/internal/plugin/host.js +++ b/internal/plugin/host.js @@ -827,7 +827,12 @@ function entries(target) { const p = path.join(target, f) if (fs.existsSync(p)) out.push(p) } - if (out.length === 0) throw new Error(`plugin ${target} has no entry (package.json main, exports or index file)`) + if (out.length === 0) { + // a command alone (bin), an MCP server, is no plugin at all (#1327) + const cmd = pkg?.bin ? ", only a command (bin). If it is an MCP server, add it under Library → MCP servers instead" : "" + // said without a stack: there is no fault in the host to trace + throw Object.assign(new Error(`${pkg?.name || target} isn't an OpenCode or magpie plugin: its package names no file to load (no main or exports in package.json, no index file)${cmd}`), { plain: true }) + } return [...new Set(out)] } @@ -890,7 +895,7 @@ async function loadPlugins(list) { for (const fn of fns) hooks.push({ spec: p.spec, target: p.target, hooks: (await fn(input, p.options)) ?? {} }) loaded.push({ spec: p.spec }) } catch (e) { - loaded.push({ spec: p.spec, error: String(e?.stack ?? e) }) + loaded.push({ spec: p.spec, error: e?.plain ? e.message : String(e?.stack ?? e) }) } } config = { provider: {}, ...structuredClone(userConfig) } diff --git a/internal/plugin/market.go b/internal/plugin/market.go index acd039550..125dd6f86 100644 --- a/internal/plugin/market.go +++ b/internal/plugin/market.go @@ -322,7 +322,10 @@ type npmLatest struct { Homepage string `json:"homepage"` Repository any `json:"repository"` Author any `json:"author"` - NPMUser struct { + // Main and Exports are what importing it loads (pkgEntry) + Main string `json:"main"` + Exports json.RawMessage `json:"exports"` + NPMUser struct { Name string `json:"name"` } `json:"_npmUser"` } diff --git a/internal/plugin/notplugin_test.go b/internal/plugin/notplugin_test.go new file mode 100644 index 000000000..e20a3cac1 --- /dev/null +++ b/internal/plugin/notplugin_test.go @@ -0,0 +1,74 @@ +package plugin + +import ( + "context" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +// A package that is only a command — magpie-x-search 1.2.0's own +// package.json, an MCP stdio server with a bin and no main, exports or +// index file (#1327) — isn't added as a plugin: Add says what it is and +// where it goes instead. One on the list already (added before) says so +// when the host loads it, without a stack. +func TestCommandOnlyPackageIsNoPlugin(t *testing.T) { + sandbox(t) + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + abs, _ := filepath.Abs("testdata/notplugin/magpie-x-search") + _, err := Add(ctx, abs) + if err == nil || !strings.Contains(err.Error(), "magpie-x-search isn't an OpenCode or magpie plugin") || !strings.Contains(err.Error(), "Library → MCP servers") { + t.Fatalf("Add = %v", err) + } + if l := Load().Plugins; len(l) != 0 { + t.Fatalf("listed: %+v", l) + } + + // added by an older magpie: the host says the same, plainly + if err := save(List{Plugins: []Entry{{Spec: abs}}}); err != nil { + t.Fatal(err) + } + Restart() + ps, err := Plugins(ctx) + if err != nil { + t.Fatal(err) + } + if len(ps) != 1 || !strings.Contains(ps[0].Error, "magpie-x-search isn't an OpenCode or magpie plugin") || !strings.Contains(ps[0].Error, "MCP servers") || strings.Contains(ps[0].Error, " at ") { + t.Fatalf("loaded: %+v", ps) + } +} + +// What a package that names no file still loads stays a plugin: an index +// file, main or exports; so do pi's packages and middleware, and a file. +func TestNotPluginKeepsPlugins(t *testing.T) { + sandbox(t) + pkg := func(json string, files ...string) string { + d := t.TempDir() + if err := os.WriteFile(filepath.Join(d, "package.json"), []byte(json), 0o644); err != nil { + t.Fatal(err) + } + for _, f := range files { + if err := os.WriteFile(filepath.Join(d, f), []byte("export default {}\n"), 0o644); err != nil { + t.Fatal(err) + } + } + return d + } + for name, d := range map[string]string{ + "index.mjs": pkg(`{"name":"a","bin":{"a":"cli.js"}}`, "index.mjs"), + "main": pkg(`{"name":"b","main":"dist/x.js"}`), + "exports": pkg(`{"name":"c","exports":{"./server":"./s.js"}}`), + "middleware": pkg(`{"name":"d","magpie":{"middleware":"d.middleware.js"}}`), + "pi": pkg(`{"name":"e","pi":{"extensions":["./x.ts"]},"peerDependencies":{"@earendil-works/pi-coding-agent":"*"}}`), + } { + if err := notPlugin(d); err != nil { + t.Errorf("%s: %v", name, err) + } + } + if abs, _ := filepath.Abs("testdata/fake/index.js"); notPlugin(abs) != nil { + t.Error("a file") + } +} diff --git a/internal/plugin/store.go b/internal/plugin/store.go index ddf717625..1918381dd 100644 --- a/internal/plugin/store.go +++ b/internal/plugin/store.go @@ -22,6 +22,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "os" "os/exec" "path/filepath" @@ -371,6 +372,15 @@ func Add(ctx context.Context, spec string) (Entry, error) { return Entry{}, err } } + if err := notPlugin(Target(spec)); err != nil { + // installed just now for this, and nothing loads it: taken out again + if !IsPath(spec) && !slices.Contains(slices.Collect(maps.Values(was)), Name(spec)) { + if bun, berr := Bun(ctx); berr == nil { + _ = bunCommand(ctx, bun, Dir(), "remove", "--ignore-scripts", Name(spec)).Run() + } + } + return Entry{}, err + } if err := ensurePi(ctx, Target(spec)); err != nil { return Entry{}, err } @@ -398,6 +408,52 @@ func Add(ctx context.Context, spec string) (Entry, error) { return e, nil } +// indexFiles are the files a package that names none loads, as the host +// looks for them (host.js's INDEX_FILES). +var indexFiles = []string{"index.ts", "index.tsx", "index.js", "index.mjs", "index.cjs"} + +// notPlugin says why the plugin at target is nothing the host can load, +// or nil: a package that names no file to import (main, exports), has no +// index file, and is neither pi's nor a middleware — a command alone +// (bin), an MCP server like magpie-x-search, is installed fine and then +// never loads (#1327). +func notPlugin(target string) error { + st, err := os.Stat(target) + if err != nil || !st.IsDir() || IsPi(target) { + return nil + } + if f, _ := Middleware(target); f != "" { + return nil + } + var pkg struct { + Name string `json:"name"` + Main string `json:"main"` + Exports json.RawMessage `json:"exports"` + Bin json.RawMessage `json:"bin"` + } + b, err := os.ReadFile(filepath.Join(target, "package.json")) + if err != nil || json.Unmarshal(b, &pkg) != nil { + return nil + } + if strings.TrimSpace(pkg.Main) != "" || len(pkg.Exports) > 0 && string(pkg.Exports) != "null" { + return nil + } + for _, f := range indexFiles { + if _, err := os.Stat(filepath.Join(target, f)); err == nil { + return nil + } + } + name := pkg.Name + if name == "" { + name = filepath.Base(target) + } + why := fmt.Sprintf("%s isn't an OpenCode or magpie plugin: its package names no file to load (no main or exports in package.json, no index file)", name) + if len(pkg.Bin) > 0 && string(pkg.Bin) != "null" { + why += ", only a command (bin). If it is an MCP server, add it under Library → MCP servers instead" + } + return errors.New(why) +} + // Update installs the version of each npm plugin its spec says now // (latest, for the most part), and fetches each git one again. func Update(ctx context.Context) error { diff --git a/internal/plugin/testdata/github/npm/magpie-x-search.json b/internal/plugin/testdata/github/npm/magpie-x-search.json new file mode 100644 index 000000000..677ab9281 --- /dev/null +++ b/internal/plugin/testdata/github/npm/magpie-x-search.json @@ -0,0 +1 @@ +{"bin":{"magpie-x-search":"launcher.cjs"},"bugs":{"url":"https://github.com/cyberElar/magpie-x-search/issues"},"dist":{"shasum":"6304721a2a326fc52df32e7bcd29d74d36fa8869","tarball":"https://registry.npmjs.org/magpie-x-search/-/magpie-x-search-1.2.0.tgz","fileCount":7,"integrity":"sha512-bJkXHCYn7548cLwbIrNtAAhF2BBSlOZo8HqVHzRN7lmtu/e/CBcUxSGtE0ech60s1L2uX8nJvzUJLq8euyxXBg==","signatures":[{"sig":"MEUCIGpmfGrxr1Wzf8+Xy4+ZVOvsv89noq2oQKWQk3uRRbRaAiEAzOHm8Z8hWQBT2TAagk9A70BTenB4n8OoyeV63/gZXCw=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":50260},"name":"magpie-x-search","author":{"name":"cyberElar"},"engines":{"node":">=22"},"gitHead":"d653c413cd7317e6f0562b29484d394501a80818","license":"MIT","scripts":{"test":"node --test test_launcher.cjs test_package.cjs && node test_python.cjs","start":"node launcher.cjs","prepack":"node check_package.cjs","prepublishOnly":"npm test"},"_npmUser":{"name":"elarrr","email":"elaratlas@gmail.com","approver":{"name":"elarrr","email":"elaratlas@gmail.com"}},"homepage":"https://github.com/cyberElar/magpie-x-search#readme","keywords":["mcp","x-search","twitter","grok","magpie","oauth","stdio"],"repository":{"url":"git+https://github.com/cyberElar/magpie-x-search.git","type":"git"},"_npmVersion":"12.2.0","description":"MCP server for native Grok X search through local magpie OAuth, callable by any model.","directories":{},"maintainers":[{"name":"elarrr","email":"elaratlas@gmail.com"}],"_nodeVersion":"26.10.0","publishConfig":{"access":"public","registry":"https://registry.npmjs.org/"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/magpie-x-search_1.2.0_1791310171064_0.80994226516181","host":"s3://npm-registry-packages-npm-production"},"_id":"magpie-x-search@1.2.0","version":"1.2.0"} \ No newline at end of file diff --git a/internal/plugin/testdata/github/raw/cyberElar/magpie-x-search/main/package.json b/internal/plugin/testdata/github/raw/cyberElar/magpie-x-search/main/package.json new file mode 100644 index 000000000..946a0fc6c --- /dev/null +++ b/internal/plugin/testdata/github/raw/cyberElar/magpie-x-search/main/package.json @@ -0,0 +1,33 @@ +{ + "name": "magpie-x-search", + "version": "1.2.0", + "description": "MCP server for native Grok X search through local magpie OAuth, callable by any model.", + "license": "MIT", + "author": "cyberElar", + "homepage": "https://github.com/cyberElar/magpie-x-search#readme", + "repository": { + "type": "git", + "url": "git+https://github.com/cyberElar/magpie-x-search.git" + }, + "bugs": { + "url": "https://github.com/cyberElar/magpie-x-search/issues" + }, + "keywords": ["mcp", "x-search", "twitter", "grok", "magpie", "oauth", "stdio"], + "engines": { + "node": ">=22" + }, + "bin": { + "magpie-x-search": "launcher.cjs" + }, + "files": ["launcher.cjs", "x_search_mcp.py", "search_backend.py", "search_cache.py"], + "scripts": { + "start": "node launcher.cjs", + "test": "node --test test_launcher.cjs test_package.cjs && node test_python.cjs", + "prepack": "node check_package.cjs", + "prepublishOnly": "npm test" + }, + "publishConfig": { + "access": "public", + "registry": "https://registry.npmjs.org/" + } +} diff --git a/internal/plugin/testdata/notplugin/magpie-x-search/launcher.cjs b/internal/plugin/testdata/notplugin/magpie-x-search/launcher.cjs new file mode 100644 index 000000000..b489ae99d --- /dev/null +++ b/internal/plugin/testdata/notplugin/magpie-x-search/launcher.cjs @@ -0,0 +1,2 @@ +#!/usr/bin/env node +// stands for magpie-x-search 1.2.0's launcher: an MCP stdio server diff --git a/internal/plugin/testdata/notplugin/magpie-x-search/package.json b/internal/plugin/testdata/notplugin/magpie-x-search/package.json new file mode 100644 index 000000000..946a0fc6c --- /dev/null +++ b/internal/plugin/testdata/notplugin/magpie-x-search/package.json @@ -0,0 +1,33 @@ +{ + "name": "magpie-x-search", + "version": "1.2.0", + "description": "MCP server for native Grok X search through local magpie OAuth, callable by any model.", + "license": "MIT", + "author": "cyberElar", + "homepage": "https://github.com/cyberElar/magpie-x-search#readme", + "repository": { + "type": "git", + "url": "git+https://github.com/cyberElar/magpie-x-search.git" + }, + "bugs": { + "url": "https://github.com/cyberElar/magpie-x-search/issues" + }, + "keywords": ["mcp", "x-search", "twitter", "grok", "magpie", "oauth", "stdio"], + "engines": { + "node": ">=22" + }, + "bin": { + "magpie-x-search": "launcher.cjs" + }, + "files": ["launcher.cjs", "x_search_mcp.py", "search_backend.py", "search_cache.py"], + "scripts": { + "start": "node launcher.cjs", + "test": "node --test test_launcher.cjs test_package.cjs && node test_python.cjs", + "prepack": "node check_package.cjs", + "prepublishOnly": "npm test" + }, + "publishConfig": { + "access": "public", + "registry": "https://registry.npmjs.org/" + } +}