Files
magpie/internal/library/mcp_here.go
T
yetone 741d502d2a library: a synced MCP server can run its own command on this computer (jinpwng on X, #6426)
jinpwng runs one MCP server on a Mac and in WSL. Each needs its own
program path, and the only ways around it were two servers with two
names or keeping the server to one computer, which loses the sync of its
agents and note (#3836).

A command server now has an optional `shared` start. When it is set,
command, args and env are this computer's, and every reader (the agents'
files, projects, the router, the check) runs them with no change of its
own. Collect carries the shared start in their place and never the field,
so the other computers get exactly what they had. Put keeps this
computer's start, and what comes in for the server becomes the shared
start, so a note, agents or the other computer's own command change still
comes in. A server that comes in as a URL, one kept to this computer,
and the editor sending no shared start each end it.

- GUI: a saved synced command server's editor has "Use another command
  on this computer" under Command. With it on, the hint names what the
  other computers run, and "Use the synced command" goes back. Nothing
  else on the page moves; a new server and a local one have no button.
- CLI: `magpie library mcp here <name> <command args…>` and
  `… here <name> off`; the listing names a server that has one.
- TUI: an edit keeps the shared start.
- Docs: docs/subsystems/library.md, site docs (en, zh, ja).

Run (mote, at the tree pushed but before the rebase):
- go test ./internal/library/... ./internal/cli/... ./internal/tui/...
  ./internal/backup/... ./internal/gui/: ok
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: clean
- GUI, chromium and webkit: mcp-server-here (new: en, zh, zh-TW, ja, de at
  1100 and 390, the payload, no button on a local server, no sideways
  scroll), library-mcp-json, library-mcp-spaced-path, library-mcp-win-line,
  market-address, mcp-home-health, mcp-startup-timeout, market-have,
  mcp-editor-cancel, mcp-local-server, i18n-en-no-cjk, i18n-keys and the
  other library MCP files: 235 of 238. The reds:
  - i18n-keys "a plugin's words are its own": the same at origin/main
    3f6b1b0c9 (#6422, #6423, memory's "No folder").
  - market-have chromium "a server added by hand is marked added": a flake,
    red 1 of 3 at origin/main 3f6b1b0c9 and 1 of 3 here (#4620).

At the rebased head (on 04776ec6c, which touched library/client.js):
go test ./internal/gui/ ./internal/cli/ ok; GUI mcp-server-here,
mcp-local-server, library-group-steady, i18n-keys: 69 of 70, the red is
i18n-keys' #6422 above.

Broken by hand:
- keepStart doing nothing: TestServerHereThroughSync red (a sync took
  this computer's own command away).
- Collect carrying the server as is: red (carried /opt and "shared").

Not tried: the CLI's here command by hand (vet only; it calls the
tested ServerHere); the real app on a Mac with a real WSL computer syncing over
WebDAV; the test does the Mac-to-WSL round trip through Collect and Put.
A backup restored into an empty library gets the synced command only,
since a backup carries what sync carries.
2026-11-13 13:42:48 +08:00

93 lines
3.1 KiB
Go

package library
import (
"fmt"
"maps"
"slices"
)
// A synced command server can be started its own way on this computer
// (#3836, #6426; jinpwng on X: the same server on a Mac and in WSL, each
// with its own program path). Its name, agents, note and tools stay the
// one server every computer has; only how this computer starts it is its
// own.
//
// - Command, Args and Env are this computer's, so every reader (the
// agents' files, projects, the router, the check) runs what this
// computer runs with no change of its own.
// - Shared holds the synced start. Collect carries Shared in their place
// and never the field itself, so another computer gets what it had.
// - Put keeps this computer's start: what comes in for the server goes
// into Shared. A server that comes in as a URL takes the server back to
// what came in, since there is no command to start.
// - Turning it off puts the synced start back (ServerHere with none).
// Start is how a command server is started: its program, the program's
// arguments and its environment.
type Start struct {
Command string `json:"command"`
Args []string `json:"args,omitempty"`
Env map[string]string `json:"env,omitempty"`
}
// startOf is a server's own start, copied.
func startOf(s *Server) *Start {
return &Start{Command: s.Command, Args: slices.Clone(s.Args), Env: maps.Clone(s.Env)}
}
// carriedStart is the server as a carried library has it: the synced
// start in place of this computer's own.
func carriedStart(s *Server) *Server {
c := *s
if s.Shared != nil {
c.Command, c.Args, c.Env = s.Shared.Command, slices.Clone(s.Shared.Args), maps.Clone(s.Shared.Env)
}
c.Shared = nil
return &c
}
// keepStart gives a server that came in this computer's own start, when
// this computer had one for the server by its name: what came in becomes
// the synced start.
func keepStart(c, here *Server) {
c.Shared = nil
if here == nil || here.Shared == nil || c.Transport != "stdio" {
return
}
c.Shared = startOf(c)
c.Command, c.Args, c.Env = here.Command, slices.Clone(here.Args), maps.Clone(here.Env)
}
// ServerHere starts a synced command server with cmd (a program and its
// arguments) on this computer alone, the other computers keeping the
// synced one; with no cmd it goes back to the synced one.
func ServerHere(name string, cmd []string) (*Result, error) {
return change(func(l *Library) error {
s := l.server(name)
switch {
case s == nil:
return fmt.Errorf("no server called %s", name)
case len(cmd) == 0:
if s.Shared != nil {
s.Command, s.Args, s.Env = s.Shared.Command, s.Shared.Args, s.Shared.Env
s.Shared = nil
}
return nil
case s.Transport != "stdio":
return fmt.Errorf("%s is reached by URL: only a command server is started its own way here", name)
case s.Local:
return fmt.Errorf("%s is kept to this computer already: its command is this computer's alone", name)
}
n := *s
if n.Shared == nil {
n.Shared = startOf(s)
}
n.Command, n.Args = cmd[0], slices.Clone(cmd[1:])
if err := n.check(); err != nil {
return err
}
*s = n
return nil
})
}