GeezerVison is showing github.com — Merge pull request #2481 from developing-today/queue/3095-1870-mcpx-p… · developing-today/code@9252bb2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pkgs/mcpx/docs/configuration.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -442,6 +442,7 @@ and a switch to turn the report off would have kept it quiet. | 442 | 442 | | `paths.config` | paths | *(empty)* | client | no | `--paths-config` | `MCPX_PATHS_CONFIG` | where configuration files are looked for | | 443 | 443 | | `paths.configFile` | string | *(empty)* | client | no | `--config` | `MCPX_CONFIG` | read exactly this configuration file instead of searching | | 444 | 444 | | `paths.placeholders` | paths | *(empty)* | client | no | `--paths-placeholders` | `MCPX_PATHS_PLACEHOLDERS` | directories of files declaring launcher placeholders | | | 445 | +| `paths.plugins` | paths | *(empty)* | client | no | `--paths-plugins` | `MCPX_PATHS_PLUGINS` | directories of plugins: an mcpx-plugin.json manifest, or executables loaded as zero-manifest plugins | | 445 | 446 | | `paths.scripts` | paths | *(empty)* | client | no | `--paths-scripts` | `MCPX_PATHS_SCRIPTS` | where named scripts are looked for | | 446 | 447 | | `paths.state` | string | *(empty)* | client | no | `--paths-state` | `MCPX_PATHS_STATE`, `MCPX_STATE_DIR` | where the daemon socket, logs and index live | | 447 | 448 |
| |
Collapse file Copy file name to clipboardExpand all lines: pkgs/mcpx/docs/environment.md +2-2Lines changed: 2 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -89,13 +89,13 @@ and read by name for that reason. | 89 | 89 | | variable | set by | when | what it is | read by | | 90 | 90 | | --- | --- | --- | --- | --- | | 91 | 91 | | `MCPX_SESSION` | `mcpx exec` / `mcpx run`; the exec service (`/v1/exec`, `mcpx_exec`); an event consumer | always | the session key this run's tool calls are leased under: --session, else MCPX_SESSION_ID, else one minted for the run | the generated client, which sends it with every call | | 92 | | -| `MCPX_SESSION_ID` | `mcpx exec` / `mcpx run`; the exec service (`/v1/exec`, `mcpx_exec`); an event consumer; the opencode plugin | the CLI passes through the value it was given, which may be empty; the plugin sets it at every plugin.env level | the session the host assigned. Unlike MCPX_SESSION it survives from one command to the next, so it is what session-scoped servers key on | `mcpx` run inside the script or shell, as its session when --session is not given | | | 92 | +| `MCPX_SESSION_ID` | `mcpx exec` / `mcpx run`; the exec service (`/v1/exec`, `mcpx_exec`); an event consumer; the opencode plugin; the plugin loader, for a discovered plugin | the CLI passes through the value it was given, which may be empty; the plugin sets it at every plugin.env level | the session the host assigned. Unlike MCPX_SESSION it survives from one command to the next, so it is what session-scoped servers key on | `mcpx` run inside the script or shell, as its session when --session is not given | | 93 | 93 | | `MCPX_PARENT_SESSION_ID` | `mcpx exec` / `mcpx run`; the opencode plugin | the CLI passes through what it was given; the plugin at plugin.env=full, when there is a parent | the session that spawned this one, so a parent agent and its subagents can share a server | `mcpx` run inside the script or shell, and the generated client for parent-session scoping | | 94 | 94 | | `MCPX_EPHEMERAL` | `mcpx exec` / `mcpx run` | always | 1 when the session key was minted for this run alone, so its instances may be stopped as soon as it ends | the generated client | | 95 | 95 | | `MCPX_RUN` | `mcpx exec` / `mcpx run`; the exec service (`/v1/exec`, `mcpx_exec`) | always | this execution's run id, which groups the artifacts it produces | the generated client, when storing an artifact | | 96 | 96 | | `MCPX_INPUT` | `mcpx exec` / `mcpx run` | mcpx exec and mcpx run, locally and with --remote; not when mcpx serve collects the output | report: nothing in this run can answer a server's question mid-call, so a call that asks one exits 75 with the question instead of waiting for it to expire | the generated client, sent as X-Mcpx-Input | | 97 | 97 | | `MCPX_ENDPOINT` | `mcpx exec` / `mcpx run`; the exec service (`/v1/exec`, `mcpx_exec`); an event consumer | always | the daemon's HTTP endpoint | the generated client; the plugin as a fallback for MCPX_DAEMON_ENDPOINT | | 98 | | -| `MCPX_SOCKET` | `mcpx exec` / `mcpx run`; the exec service (`/v1/exec`, `mcpx_exec`) | empty when the daemon is remote, since its socket is not reachable from here | the daemon's unix socket, which is faster than the endpoint. Also where a daemon binds and a client dials, for anyone who sets it by hand | the generated client, `mcpx` run inside the script or shell, the plugin | | | 98 | +| `MCPX_SOCKET` | `mcpx exec` / `mcpx run`; the exec service (`/v1/exec`, `mcpx_exec`); the plugin loader, for a discovered plugin | empty when the daemon is remote, since its socket is not reachable from here | the daemon's unix socket, which is faster than the endpoint. Also where a daemon binds and a client dials, for anyone who sets it by hand | the generated client, `mcpx` run inside the script or shell, the plugin | | 99 | 99 | | `MCPX_ARTIFACTS_LOCAL` | `mcpx exec` / `mcpx run`; the exec service (`/v1/exec`, `mcpx_exec`) | always | 1 when the daemon shares this filesystem, so artifact({path}) may hand over a path instead of the bytes | the generated client | | 100 | 100 | | `MCPX_CWD` | `mcpx exec` / `mcpx run`; the exec service (`/v1/exec`, `mcpx_exec`) | the exec service only when the caller sent its working directory | the directory the caller ran mcpx from, which a remote run cannot discover | the generated client (paths.cwd, and cwd scoping) | | 101 | 101 | | `MCPX_PID` | `mcpx exec` / `mcpx run`; the exec service (`/v1/exec`, `mcpx_exec`) | always | the pid of the mcpx process that started the script | the generated client, for pid scoping | | |
Collapse file Copy file name to clipboardExpand all lines: pkgs/mcpx/docs/parity.md +2-1Lines changed: 2 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -118,7 +118,7 @@ carry instead is a description that says so and MCP annotations | 118 | 118 | | `tool_invoke` | `POST /v1/tools/{tool}` | `mcpx tool invoke` *generated* | `mcpx_tool_invoke` | `ops.toolInvoke()` | | | 119 | 119 | | `types` | `GET /v1/types` | `mcpx types` | `mcpx_types` | `ops.types()`, `types()` | | | 120 | 120 |
| 121 | | -## CLI commands (65) | | 121 | +## CLI commands (66) | 122 | 122 |
| 123 | 123 | Every command the binary dispatches, with the operations it reaches. A command | 124 | 124 | that reaches none says why: those are decisions, not gaps. | @@ -158,6 +158,7 @@ that reaches none says why: those are decisions, not gaps. | 158 | 158 | | `man` | – | Text about the binary, produced by the binary. | | 159 | 159 | | `metrics` | `metrics_tokens`, `metrics_tools` | Generated from the operation table. | | 160 | 160 | | `openapi` | `openapi` | | | | 161 | +| `plugin` | – | Plugin discovery is local: the CLI and daemon read paths.plugins and PATH on this machine, and the daemon serves each plugin as a server of its own. | | 161 | 162 | | `preflight` | `preflight` | Generated from the operation table. | | 162 | 163 | | `prompt` | `intent` | | | 163 | 164 | | `prompts` | `prompt_get`, `prompts` | | | |
Collapse file Copy file name to clipboardExpand all lines: pkgs/mcpx/internal/cli/connect.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -246,7 +246,7 @@ func (a *App) inlineDaemon(ctx context.Context) (*Client, error) { | 246 | 246 | Config: cfg, Paths: paths, Version: a.Version, | 247 | 247 | Logger: log.New(io.Discard, "", 0), | 248 | 248 | Settings: a.Settings(), | 249 | | - Augment: a.augmentAdapters, | | 249 | + Augment: a.augmentConfig, | 250 | 250 | }) | 251 | 251 | if err != nil { | 252 | 252 | _ = os.RemoveAll(dir) | |
Collapse file Copy file name to clipboardExpand all lines: pkgs/mcpx/internal/cli/dispatch.go +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,6 +55,7 @@ func (a *App) handWritten() map[string]Handler { | 55 | 55 | "serve": a.CmdServe, | 56 | 56 | "openapi": a.CmdOpenAPI, | 57 | 57 | "adapter": a.CmdAdapter, | | 58 | + "plugin": a.CmdPlugin, | 58 | 59 | "registry": a.CmdRegistry, | 59 | 60 | "api": a.CmdAPI, | 60 | 61 | "prompts": a.CmdPrompts, | |
Collapse file Copy file name to clipboardExpand all lines: pkgs/mcpx/internal/cli/manual.go +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -579,6 +579,21 @@ func handCommands() []Command { | 579 | 579 | `mcpx adapter call jq.run '{"filter":"."}'`, | 580 | 580 | }, | 581 | 581 | }, | | 582 | + { | | 583 | + Name: "plugin", Group: "configuration", | | 584 | + Local: "Plugin discovery is local: the CLI and daemon read paths.plugins and PATH on this machine, and the daemon serves each plugin as a server of its own.", | | 585 | + Usage: "[list]", | | 586 | + Summary: "plugins discovered under paths.plugins -- each one an MCP server", | | 587 | + Detail: "A plugin is an MCP server (#1870). list shows what discovery found: " + | | 588 | + "a directory under paths.plugins holding mcpx-plugin.json, a bare " + | | 589 | + "executable in one of those directories, or an mcpx-plugin-<name> " + | | 590 | + "executable on PATH. The daemon adds every discovered plugin as a " + | | 591 | + "server of its own, so it is pooled and appears in mcpx ls, types, " + | | 592 | + "search, catalog and the generated client like any configured server.", | | 593 | + Examples: []string{ | | 594 | + "mcpx plugin list", | | 595 | + }, | | 596 | + }, | 582 | 597 | { | 583 | 598 | Name: "registry", Group: "configuration", | 584 | 599 | Usage: "[search|show|add] <query|name> [--limit N] [--write]", | |
Collapse file Copy file name to clipboard +105Lines changed: 105 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package cli | | 2 | + | | 3 | +import ( | | 4 | + "context" | | 5 | + "fmt" | | 6 | + "os" | | 7 | + "strings" | | 8 | + | | 9 | + "github.com/dezren39/mcpx/internal/config" | | 10 | + "github.com/dezren39/mcpx/internal/plugin" | | 11 | +) | | 12 | + | | 13 | +// loadPlugins discovers plugins from the configured paths.plugins and | | 14 | +// the process PATH (zero-manifest mcpx-plugin-<name> executables). | | 15 | +func (a *App) loadPlugins() ([]plugin.Plugin, error) { | | 16 | + return plugin.Discover(splitPathList(a.Settings().String("paths.plugins")), os.Getenv("PATH")) | | 17 | +} | | 18 | + | | 19 | +// augmentPlugins adds every discovered plugin to cfg as a server of its | | 20 | +// own, under the plugin's name. | | 21 | +// | | 22 | +// That is the whole of making a plugin first-class (#1870, Approach C). | | 23 | +// Attached as mcpserver extras, a plugin's tools would reach mcpx's own | | 24 | +// tools/list and nothing else -- not the pool, so not `mcpx ls`, `types`, | | 25 | +// `search`, `catalog`, /v1 or the generated client a script calls. As a | | 26 | +// server it gets all of those by the same path a configured server does, | | 27 | +// which is also where live add/remove lives: a reload re-runs discovery, | | 28 | +// so a plugin directory added or removed under paths.plugins appears or | | 29 | +// disappears with the reload, exactly as a servers edit does. | | 30 | +// | | 31 | +// The plugin process's environment carries the back-channel: MCPX_SOCKET | | 32 | +// (this daemon's socket) and MCPX_SESSION_ID, so a plugin can call /v1 | | 33 | +// the same way plugin/opencode and an exec'd script do. The session id | | 34 | +// is the plugin's own: a pooled plugin is shared between callers, so the | | 35 | +// honest session identity for its callbacks is the plugin itself, not | | 36 | +// any one caller. | | 37 | +func (a *App) augmentPlugins(cfg *config.Config) error { | | 38 | + plugins, err := a.loadPlugins() | | 39 | + if err != nil || len(plugins) == 0 { | | 40 | + return err | | 41 | + } | | 42 | + if cfg.MCPServers == nil { | | 43 | + cfg.MCPServers = map[string]*config.Server{} | | 44 | + } | | 45 | + for _, p := range plugins { | | 46 | + if _, dup := cfg.MCPServers[p.Name]; dup { | | 47 | + return fmt.Errorf("plugin %q (from %s) has the same name as a configured server; rename one", p.Name, p.Source) | | 48 | + } | | 49 | + cfg.MCPServers[p.Name] = p.Server(a.Paths.Socket, "plugin-"+p.Name) | | 50 | + } | | 51 | + return nil | | 52 | +} | | 53 | + | | 54 | +// augmentConfig is the daemon's config augmentation: adapters and | | 55 | +// plugins, each added as first-class servers before pools are built. | | 56 | +// daemon.Options.Augment takes one function, so the two live here as one. | | 57 | +func (a *App) augmentConfig(cfg *config.Config) error { | | 58 | + if err := a.augmentAdapters(cfg); err != nil { | | 59 | + return err | | 60 | + } | | 61 | + return a.augmentPlugins(cfg) | | 62 | +} | | 63 | + | | 64 | +// CmdPlugin inspects discovered plugins. | | 65 | +func (a *App) CmdPlugin(ctx context.Context, args []string) error { | | 66 | + sub := "" | | 67 | + if len(args) > 0 && !strings.HasPrefix(args[0], "-") { | | 68 | + sub, args = args[0], args[1:] | | 69 | + } | | 70 | + fs := newFlagSet("plugin") | | 71 | + if err := parseFlags(a, fs, args); err != nil { | | 72 | + return err | | 73 | + } | | 74 | + switch sub { | | 75 | + case "", "list": | | 76 | + plugins, err := a.loadPlugins() | | 77 | + if err != nil { | | 78 | + return err | | 79 | + } | | 80 | + if a.JSON { | | 81 | + return a.out(plugins) | | 82 | + } | | 83 | + if len(plugins) == 0 { | | 84 | + fmt.Println("No plugins discovered. Point paths.plugins at a directory holding " + | | 85 | + "a mcpx-plugin.json, or put an mcpx-plugin-<name> executable on PATH.") | | 86 | + return nil | | 87 | + } | | 88 | + for _, p := range plugins { | | 89 | + kind := "manifest" | | 90 | + if p.ZeroManifest { | | 91 | + kind = "zero-manifest" | | 92 | + } | | 93 | + target := p.Command | | 94 | + if target == "" { | | 95 | + target = p.URL | | 96 | + } | | 97 | + fmt.Printf("%-16s %-13s %s\n", p.Name, kind, target) | | 98 | + if p.Description != "" { | | 99 | + fmt.Printf(" %s\n", firstLine(p.Description)) | | 100 | + } | | 101 | + } | | 102 | + return nil | | 103 | + } | | 104 | + return fmt.Errorf("no plugin subcommand %q; list", sub) | | 105 | +} |
Collapse file Copy file name to clipboard +103Lines changed: 103 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package cli | | 2 | + | | 3 | +import ( | | 4 | + "os" | | 5 | + "path/filepath" | | 6 | + "strings" | | 7 | + "testing" | | 8 | + | | 9 | + "github.com/dezren39/mcpx/internal/config" | | 10 | + "github.com/dezren39/mcpx/internal/daemon" | | 11 | + "github.com/dezren39/mcpx/internal/testsupport" | | 12 | +) | | 13 | + | | 14 | +// pluginApp builds an App whose paths.plugins names dir, with PATH | | 15 | +// emptied so zero-manifest discovery on the host's real PATH cannot leak | | 16 | +// into the test. | | 17 | +func pluginApp(t *testing.T, pluginDir string) *App { | | 18 | + t.Helper() | | 19 | + root := testsupport.TempDir(t) | | 20 | + t.Setenv("HOME", root) | | 21 | + t.Setenv("XDG_CONFIG_HOME", filepath.Join(root, "xdg")) | | 22 | + t.Setenv("PATH", filepath.Join(root, "emptybin")) | | 23 | + t.Setenv("MCPX_PATHS_PLUGINS", pluginDir) | | 24 | + cfg := filepath.Join(root, "mcpx.json") | | 25 | + if err := os.WriteFile(cfg, []byte(`{"mcpServers":{}}`), 0o600); err != nil { | | 26 | + t.Fatal(err) | | 27 | + } | | 28 | + return &App{ConfigPath: cfg, Paths: daemon.PathsAt(root, "")} | | 29 | +} | | 30 | + | | 31 | +func writePlugin(t *testing.T, dir string) { | | 32 | + t.Helper() | | 33 | + pd := filepath.Join(dir, "weather") | | 34 | + if err := os.MkdirAll(pd, 0o755); err != nil { | | 35 | + t.Fatal(err) | | 36 | + } | | 37 | + manifest := `{"name":"weather","description":"forecasts","command":"./weather-server"}` | | 38 | + if err := os.WriteFile(filepath.Join(pd, "mcpx-plugin.json"), []byte(manifest), 0o644); err != nil { | | 39 | + t.Fatal(err) | | 40 | + } | | 41 | + if err := os.WriteFile(filepath.Join(pd, "weather-server"), []byte("#!/bin/sh\n"), 0o755); err != nil { | | 42 | + t.Fatal(err) | | 43 | + } | | 44 | +} | | 45 | + | | 46 | +// A discovered plugin becomes a server in the config the daemon builds | | 47 | +// pools from -- the pool-routing requirement of #1870, not an extra. | | 48 | +func TestAugmentPluginsAddsServerAndPool(t *testing.T) { | | 49 | + dir := t.TempDir() | | 50 | + writePlugin(t, dir) | | 51 | + a := pluginApp(t, dir) | | 52 | + | | 53 | + cfg := &config.Config{MCPServers: map[string]*config.Server{}} | | 54 | + if err := a.augmentPlugins(cfg); err != nil { | | 55 | + t.Fatal(err) | | 56 | + } | | 57 | + s, ok := cfg.MCPServers["weather"] | | 58 | + if !ok { | | 59 | + t.Fatalf("plugin not added as a server: %v", cfg.MCPServers) | | 60 | + } | | 61 | + if !strings.HasSuffix(s.Command, filepath.Join("weather", "weather-server")) { | | 62 | + t.Fatalf("unexpected plugin command %q", s.Command) | | 63 | + } | | 64 | + if s.Env["MCPX_SOCKET"] != a.Paths.Socket { | | 65 | + t.Fatalf("plugin env lacks the daemon socket: %v (socket %q)", s.Env, a.Paths.Socket) | | 66 | + } | | 67 | + if s.Env["MCPX_SESSION_ID"] != "plugin-weather" { | | 68 | + t.Fatalf("plugin env lacks its session id: %v", s.Env) | | 69 | + } | | 70 | + | | 71 | + reg, err := daemon.NewRegistry(cfg, a.Paths, nil) | | 72 | + if err != nil { | | 73 | + t.Fatal(err) | | 74 | + } | | 75 | + if _, ok := reg.Pool("weather"); !ok { | | 76 | + t.Fatalf("plugin has no pool; registry names: %v", reg.Names()) | | 77 | + } | | 78 | +} | | 79 | + | | 80 | +func TestAugmentPluginsNameCollisionWithServerIsAnError(t *testing.T) { | | 81 | + dir := t.TempDir() | | 82 | + writePlugin(t, dir) | | 83 | + a := pluginApp(t, dir) | | 84 | + | | 85 | + cfg := &config.Config{MCPServers: map[string]*config.Server{ | | 86 | + "weather": {Name: "weather", Command: "/bin/true"}, | | 87 | + }} | | 88 | + err := a.augmentPlugins(cfg) | | 89 | + if err == nil || !strings.Contains(err.Error(), "same name as a configured server") { | | 90 | + t.Fatalf("want collision error, got %v", err) | | 91 | + } | | 92 | +} | | 93 | + | | 94 | +func TestAugmentPluginsNoneConfiguredIsQuiet(t *testing.T) { | | 95 | + a := pluginApp(t, "") | | 96 | + cfg := &config.Config{MCPServers: map[string]*config.Server{}} | | 97 | + if err := a.augmentPlugins(cfg); err != nil { | | 98 | + t.Fatal(err) | | 99 | + } | | 100 | + if len(cfg.MCPServers) != 0 { | | 101 | + t.Fatalf("no plugins should mean no servers, got %v", cfg.MCPServers) | | 102 | + } | | 103 | +} |
Collapse file Copy file name to clipboardExpand all lines: pkgs/mcpx/internal/cli/root.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -335,7 +335,7 @@ func (a *App) CmdDaemon(ctx context.Context, args []string) error { | 335 | 335 | Logger: logger, | 336 | 336 | IdleExit: a.Settings().Duration("daemon.idleExit"), | 337 | 337 | Settings: a.Settings(), | 338 | | - Augment: a.augmentAdapters, | | 338 | + Augment: a.augmentConfig, | 339 | 339 | }) | 340 | 340 | if err != nil { | 341 | 341 | return err | |
|