lc on Discord ("agent未识别到zed"): the MCP server editor's agent chips
had no Zed. magpie already edits Zed's settings.json for its model, and
Zed reads its MCP servers from context_servers in that same file, so Zed
is now an MCP target like the other agents.
What Zed takes, from its own source (zed-industries/zed 2c99f547):
- crates/settings_content/src/project.rs ContextServerSettingsContent is
untagged: a command as command/args/env (enabled, remote, timeout
beside it), a url as url/headers (enabled, timeout, oauth), or an
extension's entry as settings. docs/src/ai/mcp.md writes the same.
- crates/context_server/src/transport/http.rs only POSTs (and DELETEs):
streamable HTTP, no SSE. So an SSE server is refused for Zed and its
chip is greyed with the existing reason (AgentView.NoSSE).
- stdio_transport.rs hands command.envs over as written and http.rs sends
headers as written: no ${NAME}. Zed is refNone, so a server naming a
variable is refused and its chip greyed (AgentView.NoEnvRefs).
Changes:
- targets.go: "zed" gets mcpFile{agent.zed's settings.json, fmtZed}; the
path is agent.zed's for every OS (~/.config/zed, $XDG_CONFIG_HOME/zed,
%APPDATA%\Zed, $MAGPIE_ZED_CONFIG_DIR).
- mcp.go: fmtZed under "context_servers", encoded as Cursor/Desktop are;
magpie owns url/headers/command/args/env only, so enabled, remote,
timeout and oauth the user sets on magpie's entry stay. The file is
JSONC and edited in place (edit.SetJSON/DelJSON), so comments, other
settings and the user's own servers are kept.
- mcp.go, sync.go: an entry Zed's extensions configure (settings, no
command or url) is never written over: a library server by that name
is a Problem for Zed (errZedExtension), asking for another name.
- envref.go: a comment citing why Zed has no syntax.
- docs/subsystems/library.md: Zed's file, format, no SSE, no ${NAME},
the extension guard, and the new tests.
Tests:
- internal/library/zed_mcp_test.go TestZedMCP, on a Zed-style JSONC
settings.json with comments, trailing commas, a user stdio server
(enabled:false, timeout), a user remote server and an extension's
entry, at the default path and under MAGPIE_ZED_CONFIG_DIR: found
servers, the page's chips, written entries, SSE/${NAME}/extension
refusals, user keys kept through an edit, uncheck and remove leaving
only the user's, comments kept, a Sync rewriting nothing.
- internal/gui/tests/library-zed-mcp.test.cjs: Zed's chip on every row
and in the editor, a click gives it the server, greyed with the reason
for SSE and ${NAME}; Chromium and WebKit, en/zh/zh-TW/ja/de, 1100 and
560px, no sideways scroll. No new strings.
Broken by hand, each went red and was restored: no zed target, no
extension guard, SSE allowed, enabled owned by magpie, wrong key (Go);
Zed without noSSE (GUI).
Run (temp HOME, Go caches pinned): gofmt -l internal/ (clean); go vet
-tags nogui ./... on darwin, GOOS=linux and GOOS=windows; go vet
./internal/library ./internal/agent; go build ./..., GOOS=linux go build
-tags nogui ./..., GOOS=windows go build ./...; go test -tags nogui ./...
(all ok); node --test library-zed-mcp.test.cjs (22/22, both engines).
Not verified: a real Zed reading the written file (the live
~/.config/zed was never touched); Linux and Windows beyond vet/build;
Zed found in WSL (not in ownFolder, so no MCP target there); a
project's .zed/settings.json (not added).
208 lines
7.3 KiB
Go
208 lines
7.3 KiB
Go
package library
|
|
|
|
import (
|
|
"encoding/json"
|
|
"path/filepath"
|
|
"runtime"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tidwall/jsonc"
|
|
)
|
|
|
|
// lc on Discord: Zed wasn't among the agents an MCP server could be given
|
|
// to. Zed reads them from context_servers in its settings.json (JSONC),
|
|
// the file magpie sets its model in: a command as command/args/env, a url
|
|
// as url/headers, which it speaks streamable HTTP to (zed-industries/zed
|
|
// 2c99f547, crates/settings_content/src/project.rs
|
|
// ContextServerSettingsContent). The user's comments, other settings, own
|
|
// servers and an extension's server are kept; unchecking Zed takes out
|
|
// only magpie's entries.
|
|
func TestZedMCP(t *testing.T) {
|
|
user := `// Zed settings
|
|
//
|
|
// For information on how to configure Zed, see the Zed
|
|
// documentation: https://zed.dev/docs/configuring-zed
|
|
{
|
|
"theme": "One Dark", // my theme
|
|
/* the servers I added in Zed */
|
|
"context_servers": {
|
|
"mine": {
|
|
"enabled": false,
|
|
"command": "uvx",
|
|
"args": ["mine"],
|
|
"env": {},
|
|
"timeout": 120,
|
|
},
|
|
"remote-mine": {
|
|
"url": "https://mine.example.com/mcp",
|
|
"headers": { "Authorization": "Bearer <token>" }
|
|
},
|
|
// installed from Zed's extensions
|
|
"mcp-server-github": {
|
|
"settings": { "github_personal_access_token": "ghp_x" }
|
|
},
|
|
},
|
|
"buffer_font_size": 15,
|
|
}
|
|
`
|
|
for _, c := range []struct {
|
|
name string
|
|
dir func(h string) string // Zed's settings folder; nil: the default
|
|
}{
|
|
{name: "default"},
|
|
{name: "MAGPIE_ZED_CONFIG_DIR", dir: func(h string) string { return filepath.Join(h, "zedg") }},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
h := sandbox(t)
|
|
dir := filepath.Join(h, ".config", "zed")
|
|
if runtime.GOOS == "windows" {
|
|
dir = filepath.Join(h, "AppData", "Roaming", "Zed")
|
|
}
|
|
if c.dir != nil {
|
|
dir = c.dir(h)
|
|
t.Setenv("MAGPIE_ZED_CONFIG_DIR", dir)
|
|
}
|
|
p := filepath.Join(dir, "settings.json")
|
|
write(t, p, user)
|
|
|
|
tg := targetByID("zed")
|
|
if tg == nil || tg.MCP == nil || tg.MCP.Path != p {
|
|
t.Fatalf("Zed has no MCP target at %s: %+v", p, tg)
|
|
}
|
|
if id, err := Takes("zed", "mcp"); id != "zed" || err != nil {
|
|
t.Errorf("Takes: %q %v", id, err)
|
|
}
|
|
parse := func() map[string]map[string]any {
|
|
t.Helper()
|
|
var doc map[string]any
|
|
if err := json.Unmarshal(jsonc.ToJSON([]byte(read(t, p))), &doc); err != nil {
|
|
t.Fatalf("%v:\n%s", err, read(t, p))
|
|
}
|
|
out := map[string]map[string]any{}
|
|
all, _ := doc["context_servers"].(map[string]any)
|
|
for k, v := range all {
|
|
out[k], _ = v.(map[string]any)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// the user's own are there to bring in; the extension's isn't one
|
|
l, err := load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
found := map[string]*Server{}
|
|
for _, f := range foundServers(l) {
|
|
if slices.Contains(f.Server.Agents, "zed") {
|
|
found[f.Server.Name] = f.Server
|
|
}
|
|
}
|
|
if s := found["mine"]; s == nil || s.Transport != "stdio" || s.Command != "uvx" {
|
|
t.Errorf("mine: %+v", s)
|
|
}
|
|
if s := found["remote-mine"]; s == nil || s.Transport != "http" || s.URL != "https://mine.example.com/mcp" {
|
|
t.Errorf("remote-mine: %+v", s)
|
|
}
|
|
if found["mcp-server-github"] != nil {
|
|
t.Errorf("an extension's server was found as one to bring in")
|
|
}
|
|
|
|
// the page: Zed's chip, greyed for SSE and for ${NAME}
|
|
v, err := Read(nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
i := slices.IndexFunc(v.Agents, func(a AgentView) bool { return a.ID == "zed" })
|
|
if i < 0 || v.Agents[i].MCP != p || !v.Agents[i].NoSSE || !v.Agents[i].NoEnvRefs || v.Agents[i].NoRemote {
|
|
t.Errorf("Zed on the page: %+v", v.Agents)
|
|
}
|
|
|
|
agents := []string{"zed"}
|
|
fs := Server{Name: "fs", Transport: "stdio", Command: "npx", Args: []string{"-y", "@mcp/fs"}, Env: map[string]string{"K": "V"}, Agents: agents}
|
|
web := Server{Name: "web", Transport: "http", URL: "https://example.com/mcp", Headers: map[string]string{"Authorization": "Bearer x"}, Agents: agents}
|
|
ok(t)(SaveServer("", fs))
|
|
ok(t)(SaveServer("", web))
|
|
for _, refused := range []struct {
|
|
s Server
|
|
why string
|
|
}{
|
|
{Server{Name: "old", Transport: "sse", URL: "https://example.com/sse", Agents: agents}, errNoSSE.Error()},
|
|
{Server{Name: "gh", Transport: "http", URL: "https://example.com/gh", Headers: map[string]string{"Authorization": "Bearer ${GH_TOKEN}"}, Agents: agents}, errNoEnvRef.Error()},
|
|
{Server{Name: "mcp-server-github", Transport: "stdio", Command: "github-mcp", Agents: agents}, errZedExtension.Error()},
|
|
} {
|
|
res, err := SaveServer("", refused.s)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !slices.ContainsFunc(res.Problems, func(p Problem) bool { return p.What == "mcp:"+refused.s.Name && p.Error == refused.why }) {
|
|
t.Errorf("%s: %+v", refused.s.Name, res.Problems)
|
|
}
|
|
}
|
|
|
|
got := parse()
|
|
for name, want := range map[string]map[string]any{
|
|
"fs": {"command": "npx", "args": []any{"-y", "@mcp/fs"}, "env": map[string]any{"K": "V"}},
|
|
"web": {"url": "https://example.com/mcp", "headers": map[string]any{"Authorization": "Bearer x"}},
|
|
// the user's, as they were
|
|
"mine": {"enabled": false, "command": "uvx", "args": []any{"mine"}, "env": map[string]any{}, "timeout": float64(120)},
|
|
"remote-mine": {"url": "https://mine.example.com/mcp", "headers": map[string]any{"Authorization": "Bearer <token>"}},
|
|
"mcp-server-github": {"settings": map[string]any{"github_personal_access_token": "ghp_x"}},
|
|
} {
|
|
g, _ := json.Marshal(got[name])
|
|
w, _ := json.Marshal(want)
|
|
if string(g) != string(w) {
|
|
t.Errorf("%s:\n got %s\nwant %s", name, g, w)
|
|
}
|
|
}
|
|
for _, name := range []string{"old", "gh"} {
|
|
if got[name] != nil {
|
|
t.Errorf("%s was written: %v", name, got[name])
|
|
}
|
|
}
|
|
back, err := tg.MCP.read()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, want := range []Server{fs, web} {
|
|
if s := back[want.Name]; s == nil || !s.same(&want) {
|
|
t.Errorf("%s read back as %+v", want.Name, s)
|
|
}
|
|
}
|
|
// what Zed can't take is no problem once it isn't given to Zed
|
|
ok(t)(RemoveServers([]string{"old", "gh", "mcp-server-github"}))
|
|
if parse()["mcp-server-github"]["settings"] == nil {
|
|
t.Errorf("the extension's server: %v", parse()["mcp-server-github"])
|
|
}
|
|
before := read(t, p)
|
|
ok(t)(Sync())
|
|
if read(t, p) != before {
|
|
t.Errorf("a sync rewrote servers that are as the library has them:\n%s", read(t, p))
|
|
}
|
|
|
|
// a key the user gives magpie's entry in Zed stays
|
|
text := strings.Replace(read(t, p), `"command": "npx"`, `"enabled": false, "command": "npx"`, 1)
|
|
write(t, p, text)
|
|
fs.Args = []string{"-y", "@mcp/fs", "/tmp"}
|
|
ok(t)(SaveServer("fs", fs))
|
|
if g := parse()["fs"]; g["enabled"] != false || len(g["args"].([]any)) != 3 {
|
|
t.Errorf("fs after an edit: %v", g)
|
|
}
|
|
|
|
// unchecking Zed takes out only magpie's
|
|
ok(t)(ServerAgents("fs", nil))
|
|
ok(t)(RemoveServer("web"))
|
|
got = parse()
|
|
if len(got) != 3 || got["mine"] == nil || got["remote-mine"] == nil || got["mcp-server-github"]["settings"] == nil {
|
|
t.Errorf("after taking magpie's out:\n%s", read(t, p))
|
|
}
|
|
for _, keep := range []string{"// Zed settings", "// my theme", "/* the servers I added in Zed */", "// installed from Zed's extensions", `"theme": "One Dark"`, `"buffer_font_size": 15`, `"timeout": 120`} {
|
|
if !strings.Contains(read(t, p), keep) {
|
|
t.Errorf("lost %s:\n%s", keep, read(t, p))
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|