Files
magpie/internal/library/mcp_local.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

104 lines
3.2 KiB
Go

package library
import (
"fmt"
"slices"
)
// A server can be kept to this computer (Server.Local; huhuy72219405 on
// X: "有些mcp属于本地的,同步之后其他设备不应该读取到"). It is for a server
// that runs a program, or reads a folder, only this computer has.
//
// - Collect leaves it out, so a backup or a sync carries it to no other
// computer. Turning it on takes the server out of what is carried, so
// the other computers let go of theirs at their next sync, as of any
// server taken out; the flag itself is never carried.
// - Put keeps it, whatever comes in: a bundle without it doesn't take it
// away, and another computer's server by its name doesn't take its
// place. That one is kept in Away, given to no agent here, and carried
// on as it came, so a sync from here doesn't take it from the computers
// that have it.
// - Once no server here by that name is kept to this computer (it was
// removed, renamed or shared again), settleAway lets go of Away's: the
// server here shared again goes in its place, and with none here the
// other computers' comes in, as a sync would bring it.
// carriedServers are the servers a carried library has: the library's,
// but those kept to this computer, and the other computers' by their
// names.
func (l *Library) carriedServers() []*Server {
out := []*Server{}
for _, s := range l.MCP {
if !s.Local {
out = append(out, s)
}
}
return append(out, l.Away...)
}
// putServers makes the library's servers those a carried library has,
// but for the ones kept to this computer, which stay as they are.
func (l *Library) putServers(in []*Server) {
own := map[string]bool{}
servers := []*Server{}
here := map[string]*Server{}
for _, s := range l.MCP {
here[s.Name] = s
if s.Local {
own[s.Name] = true
servers = append(servers, s)
}
}
l.Away = nil
for _, s := range in {
c := *s
c.Local = false // only this computer says what is its own
c.Bridge = "" // and which of its providers a server is
c.Agents = slices.Sorted(slices.Values(orNone(s.Agents)))
// this computer's own start stays, what came in its synced one
// (mcp_here.go)
keepStart(&c, here[c.Name])
if own[c.Name] {
l.Away = append(l.Away, &c)
} else {
servers = append(servers, &c)
}
}
l.MCP = servers
}
// settleAway lets go of each server in Away whose name no server kept to
// this computer has any more. One shared here by that name is the one
// carried from now on; with none, the other computers' comes in.
func (l *Library) settleAway() {
away := l.Away[:0:0]
for _, a := range l.Away {
switch s := l.server(a.Name); {
case s != nil && s.Local:
away = append(away, a)
case s == nil:
l.MCP = append(l.MCP, a)
}
}
l.Away = away
if len(l.Away) == 0 {
l.Away = nil
}
}
// ServerLocal keeps a server to this computer, or lets a sync carry it
// again.
func ServerLocal(name string, on bool) (*Result, error) {
return change(func(l *Library) error {
s := l.server(name)
if s == nil {
return fmt.Errorf("no server called %s", name)
}
s.Local = on
if on {
s.Shared = nil // its command is this computer's alone now
}
return nil
})
}