Files
magpie/internal/library/mcp_copy.go
T
yetone 61aa681e60 library: an MCP server, or the servers picked, are copied as an agent's project file has them, to paste into a project magpie doesn't keep (#1478, xiaozhu1337)
A project's MCP servers could only be given by adding the project to
magpie first. For a one-off (add them, then let the project keep them),
that is a detour. Now the servers page's Select bar, and a server's
editor, have Copy config…: a menu of the agents that read a project's
servers from a file (Claude Code .mcp.json, Codex .codex/config.toml,
Gemini, Cursor, OpenCode, ZCode, Pi), each with its file. A pick puts
that file's text on the clipboard and the status says where to paste it.

What changed:
- library.ConfigOfServers(agent, names): the servers written by the same
  code a project of magpie's is written with (mcpFile.put, through() for
  one magpie is signed in to, the agent's own variable syntax) into a
  fresh file in a temp folder, read back and removed. Nothing in the
  user's files is written. A server the agent can't take there (SSE for
  Codex or Pi, a ${VAR} it can't read) is left out and named, as a
  project's sync leaves it out.
- POST /api/library/mcp/config answers {file, text, skipped}.
- library.js: copyConfigButton in the pick bar and the editor's bar;
  strings in zh, zh-TW, ja, de.
- docs/subsystems/library.md says so.

Run:
- TestConfigOfServersIsTheProjectFile: for every agent in
  projectMCPFiles, the copied text equals byte for byte the file magpie
  writes into a fresh project with the same servers, the servers left out
  are those supports() refuses, a signed-in server is the gateway's
  /mcp/<name>; with through() taken out it fails.
- library-mcp-copy-config.test.cjs in Chromium and WebKit, en/zh/ja/de at
  440px: the menu's agents and files, the post, the clipboard text, the
  left-out status, nothing copied when nothing fits, the editor's button;
  no sideways scroll. The library and project GUI tests that draw the
  pick bar or the editor (18 files) pass in both engines.
- go test -tags nogui ./internal/library/ ./internal/gui/ under a temp
  HOME; gofmt -l; GOOS=linux and GOOS=windows go vet -tags nogui.
- Not clicked in a running magpie window; the page against a faked API
  and the library code under a sandbox HOME only.
2026-10-10 18:35:17 +08:00

76 lines
2.1 KiB
Go

package library
import (
"errors"
"fmt"
"os"
"path/filepath"
"slices"
"github.com/yetone/magpie/internal/gateway"
)
// ServersConfig is what an agent's project file would hold with the
// library's servers called names in it, as magpie writes them into a
// project it has (syncProjectMCP): for the user to paste into a project
// magpie doesn't keep (#1478, xiaozhu1337: 不建项目、一次性添加项目级
// MCP). File is the file in the project, Text the file whole, and Skipped
// the servers the agent can't reach that way (SSE for Codex or Pi).
type ServersConfig struct {
File string `json:"file"`
Text string `json:"text"`
Skipped []string `json:"skipped,omitempty"`
}
// ConfigOfServers writes the servers into a fresh file in agent's project
// format, in a folder of its own, and reads it back: the same encoder, the
// same references to variables, a server magpie is signed in to through
// the gateway as a project of magpie's is given it.
func ConfigOfServers(agent string, names []string) (*ServersConfig, error) {
pf, ok := projectMCPFiles[agent]
if !ok {
return nil, fmt.Errorf("magpie knows of no file %s reads a project's MCP servers from", agent)
}
if len(names) == 0 {
return nil, errors.New("no servers named")
}
mu.Lock()
l, err := load()
mu.Unlock()
if err != nil {
return nil, err
}
tmp, err := os.MkdirTemp("", "magpie-mcp-config-")
if err != nil {
return nil, err
}
defer os.RemoveAll(tmp)
f := &mcpFile{Path: filepath.Join(tmp, filepath.FromSlash(pf.rel)), Format: pf.format}
out := &ServersConfig{File: pf.rel}
put := 0
// in the order a project's file has them
for _, name := range slices.Compact(slices.Sorted(slices.Values(names))) {
s := l.server(name)
if s == nil {
return nil, fmt.Errorf("no server called %s", name)
}
if f.supports(s) != nil {
out.Skipped = append(out.Skipped, name)
continue
}
if err := f.put(through(s, gateway.URL()), nil); err != nil {
return nil, err
}
put++
}
if put == 0 {
return out, nil
}
b, err := os.ReadFile(f.Path)
if err != nil {
return nil, err
}
out.Text = string(b)
return out, nil
}