Files
skillshare/internal/server/handler_mcp_test.go
T
Willie 7ee08b913d feat(mcp): open an Agent's MCP config file from the dashboard (#552)
* feat(mcp): open an Agent's MCP config file from the dashboard

Users asked to jump from the MCP page straight into a client's config
file, such as opencode.json or .cursor/mcp.json, instead of copying the
path and opening it by hand (#538).

The open uses the same editor chain as a skill's Open in editor, so the
dashboard opens files one way. The endpoint takes a target, not a path,
and only opens a file the MCP page already lists. It answers only
requests from the machine running the dashboard, because the editor
starts on that machine. The list now reports which files exist, so the
button is hidden for a client detected before it has a file.

* fix(mcp): hide Open in editor where the server would refuse it

Codex review: the dashboard can be opened through a LAN IP, but the
open endpoint answers only loopback requests, so the button showed and
every click returned 403, while the docs promised it would open.

The list now reports `openable` instead of `existing`: the files that
exist, and only when the request comes from the dashboard's machine. A
viewer elsewhere does not see the button, and the docs say where it
appears.

* fix(mcp): don't treat a reverse proxy's viewers as local for Open in editor

Codex review: the documented Nginx and Caddy setups connect from
127.0.0.1, so the loopback socket check let any browser behind the proxy
see the button and launch editors on the server.

The browser now counts as local only when the socket and the Host it
used are both loopback and no proxy forwarding header is present. Both
documented proxies keep the browser's Host and add X-Real-IP or
X-Forwarded-For, so their viewers no longer qualify.
2026-10-13 10:40:27 +08:00

311 lines
12 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
syncpkg "skillshare/internal/sync"
)
func TestMCPPiImportSourceSelection(t *testing.T) {
s, sourceDir := newTestServerWithExtras(t, nil, "")
home, agent, root, account := t.TempDir(), t.TempDir(), t.TempDir(), t.TempDir()
t.Setenv("HOME", home)
t.Setenv("PI_CODING_AGENT_DIR", agent)
data := []byte("source: " + sourceDir + "\ntargets:\n pi-work: {agent: pi, config_dir: " + account + "}\nmcp:\n projects:\n " + root + ": {}\n")
if err := os.WriteFile(s.configPath(), data, 0600); err != nil {
t.Fatal(err)
}
list := httptest.NewRecorder()
s.handleMCPList(list, httptest.NewRequest(http.MethodGet, "/api/mcp", nil))
var view struct {
ImportSources map[string][]struct{ Target, Path, PiExtension string }
}
if list.Code != 200 || json.Unmarshal(list.Body.Bytes(), &view) != nil {
t.Fatal(list.Body.String())
}
for _, scope := range []string{"", root} {
for _, target := range []string{"pi", "pi-work"} {
base := agent
if target == "pi-work" {
base = account
}
if scope != "" {
base = filepath.Join(root, ".pi")
}
found := map[string]string{}
for _, source := range view.ImportSources[scope] {
if source.Target == target {
found[source.PiExtension] = source.Path
}
}
if scope != "" && target == "pi-work" {
if len(found) != 0 {
t.Fatal("project offered an account")
}
continue
}
// Pi's built-in file, and the adapter's, which is still read for import.
if len(found) != 2 || found["builtin"] != filepath.Join(base, "mcp.json") || found["pi-mcp-adapter"] != filepath.Join(base, "mcp-adapter.json") {
t.Fatalf("%s %s: %+v", scope, target, found)
}
if err := os.MkdirAll(base, 0755); err != nil {
t.Fatal(err)
}
for file, command := range map[string]string{"mcp.json": "native", "mcp-adapter.json": "adapter"} {
if err := os.WriteFile(filepath.Join(base, file), []byte(`{"mcpServers":{"docs":{"command":"`+command+`"}}}`), 0600); err != nil {
t.Fatal(err)
}
}
// A dashboard from before 0.23.0 may still send pi-mcp-extension; it reads mcp.json.
for mode, command := range map[string]string{"builtin": "native", "pi-mcp-adapter": "adapter", "pi-mcp-extension": "native"} {
body, _ := json.Marshal(map[string]string{"from": target, "root": scope, "piExtension": mode})
w := httptest.NewRecorder()
s.handleMCPImport(w, httptest.NewRequest(http.MethodPost, "/api/mcp/import", strings.NewReader(string(body))))
var result struct {
Candidates []struct {
From string
Warnings []string
Server struct{ Command string }
}
}
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &result) != nil || len(result.Candidates) != 1 || result.Candidates[0].From != target || result.Candidates[0].Server.Command != command {
t.Fatalf("%s %s %s: %s", scope, target, mode, w.Body)
}
}
}
}
// An exact source must neither fall back to the other file nor accept non-Pi modes.
if err := os.Remove(filepath.Join(agent, "mcp.json")); err != nil {
t.Fatal(err)
}
for _, body := range []string{`{"from":"pi","piExtension":"builtin"}`, `{"from":"pi","piExtension":"unknown"}`, `{"from":"claude","piExtension":"builtin"}`} {
w := httptest.NewRecorder()
s.handleMCPImport(w, httptest.NewRequest(http.MethodPost, "/api/mcp/import", strings.NewReader(body)))
if w.Code != 400 {
t.Fatalf("accepted %s: %s", body, w.Body)
}
}
}
func TestMCPAPIPreviewAndApply(t *testing.T) {
s, _ := newTestServerWithExtras(t, nil, "")
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("CLAUDE_CONFIG_DIR", "")
body := `{"mutation":{"name":"docs","server":{"url":"https://example.com/mcp","targets":["claude"]}}}`
preview := httptest.NewRecorder()
s.handleMCPPreview(preview, httptest.NewRequest(http.MethodPost, "/api/mcp/preview", strings.NewReader(body)))
if preview.Code != 200 {
t.Fatalf("preview: %s", preview.Body.String())
}
var p struct {
Revision string `json:"revision"`
}
if err := json.Unmarshal(preview.Body.Bytes(), &p); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join(home, ".claude.json")); !os.IsNotExist(err) {
t.Fatal("preview wrote native file")
}
apply := httptest.NewRecorder()
request := strings.TrimSuffix(body, "}") + `,"sync":true,"revision":"` + p.Revision + `"}`
s.handleMCPConfigure(apply, httptest.NewRequest(http.MethodPost, "/api/mcp", strings.NewReader(request)))
if apply.Code != 200 {
t.Fatalf("apply: %s", apply.Body.String())
}
if _, err := os.Stat(filepath.Join(home, ".claude.json")); err != nil {
t.Fatal(err)
}
}
func TestMCPListIgnoresUnresolvableUnusedTarget(t *testing.T) {
s, _ := newTestServerWithExtras(t, nil, "")
xdg := t.TempDir()
t.Setenv("HOME", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", xdg)
for _, name := range []string{"opencode.json", "opencode.jsonc"} {
path := filepath.Join(xdg, "opencode", name)
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte("{}"), 0644); err != nil {
t.Fatal(err)
}
}
w := httptest.NewRecorder()
s.handleMCPList(w, httptest.NewRequest(http.MethodGet, "/api/mcp", nil))
if w.Code != http.StatusOK {
t.Fatalf("status = %d body = %s", w.Code, w.Body.String())
}
}
func TestMCPRoutesRejectRebindingHost(t *testing.T) {
s, _ := newTestServerWithExtras(t, nil, "")
s.addr = "127.0.0.1:19420"
routes := []string{"GET /api/mcp", "GET /api/mcp/check", "POST /api/mcp", "POST /api/mcp/preview", "POST /api/mcp/import", "POST /api/mcp/restore"}
for _, route := range routes {
method, path, _ := strings.Cut(route, " ")
req := httptest.NewRequest(method, path, strings.NewReader(`{}`))
req.RemoteAddr = "127.0.0.1:5555"
req.Host = "attacker.example:19420"
w := httptest.NewRecorder()
s.mux.ServeHTTP(w, req)
if w.Code != http.StatusForbidden {
t.Fatalf("%s: status = %d, want 403", route, w.Code)
}
}
}
func TestMCPPiBuiltinProjectRenderAndPaths(t *testing.T) {
s, _ := newTestServerWithExtras(t, nil, "")
home, root := t.TempDir(), t.TempDir()
t.Setenv("HOME", home)
t.Setenv("PI_CODING_AGENT_DIR", "")
configPath := s.configPath()
data, err := os.ReadFile(configPath)
if err != nil {
t.Fatal(err)
}
data = append(data, []byte("\nmcp:\n targets: [pi]\n servers:\n docs:\n command: docs\n piExtension: builtin\n projects:\n "+root+":\n servers: {}\n")...)
if err := os.WriteFile(configPath, data, 0600); err != nil {
t.Fatal(err)
}
body, _ := json.Marshal(map[string]any{"mutation": map[string]any{"project": root, "name": "docs", "server": map[string]any{"command": "docs", "piExtension": "builtin", "targets": []string{"pi"}, "piOptions": map[string]any{"exposure": "deferred"}}}})
result := httptest.NewRecorder()
s.handleMCPRender(result, httptest.NewRequest(http.MethodPost, "/api/mcp/render", strings.NewReader(string(body))))
var rendered struct {
Rendered []struct {
Path string
Content string
Error string
}
}
if result.Code != 200 || json.Unmarshal(result.Body.Bytes(), &rendered) != nil || len(rendered.Rendered) != 1 || rendered.Rendered[0].Path != filepath.Join(root, ".pi", "mcp.json") || rendered.Rendered[0].Error != "" {
t.Fatalf("wrong project render: %s", result.Body)
}
if _, err := os.Stat(rendered.Rendered[0].Path); !os.IsNotExist(err) {
t.Fatal("render wrote native config")
}
list := httptest.NewRecorder()
s.handleMCPList(list, httptest.NewRequest(http.MethodGet, "/api/mcp", nil))
var view struct{ Paths map[string]string }
if list.Code != 200 || json.Unmarshal(list.Body.Bytes(), &view) != nil || view.Paths["pi"] != filepath.Join(home, ".pi", "agent", "mcp.json") {
t.Fatalf("wrong mode path label: %s", list.Body)
}
}
func TestMCPRenderRejectsProjectOverrideInProjectMode(t *testing.T) {
s, _ := newTestServerWithExtras(t, nil, "")
s.projectRoot = t.TempDir()
body := `{"mutation":{"project":"/other/project","name":"docs","server":{"command":"docs","targets":["pi"],"piExtension":"builtin"}}}`
w := httptest.NewRecorder()
s.handleMCPRender(w, httptest.NewRequest(http.MethodPost, "/api/mcp/render", strings.NewReader(body)))
if w.Code != 400 || !strings.Contains(w.Body.String(), "project mode") {
t.Fatalf("%d %s", w.Code, w.Body)
}
}
// The dashboard's sync saves the config without the settings 0.23.0 retired, keeping the
// previous version in the file history, so the list no longer shows their notices.
func TestMCPSyncSavesTheConfigWithoutRetiredSettings(t *testing.T) {
s, sourceDir := newTestServerWithExtras(t, nil, "")
t.Setenv("HOME", t.TempDir())
t.Setenv("PI_CODING_AGENT_DIR", t.TempDir())
data := "source: " + sourceDir + "\nmcp:\n targets: [pi]\n servers:\n docs:\n command: docs\n piExtension: pi-mcp-adapter\n"
if err := os.WriteFile(s.configPath(), []byte(data), 0600); err != nil {
t.Fatal(err)
}
plan, err := s.mcpService().Preview()
if err != nil || !plan.Migrates || len(plan.Notices) != 2 {
t.Fatalf("%+v %v", plan, err)
}
body, _ := json.Marshal(map[string]any{"mutation": map[string]any{}, "revision": plan.Revision, "sync": true})
w := httptest.NewRecorder()
s.handleMCPConfigure(w, httptest.NewRequest(http.MethodPost, "/api/mcp", strings.NewReader(string(body))))
if w.Code != 200 || !strings.Contains(w.Body.String(), `"migrated":[{"path":`) {
t.Fatalf("%d %s", w.Code, w.Body)
}
if saved, _ := os.ReadFile(s.configPath()); strings.Contains(string(saved), "piExtension") || !strings.Contains(string(saved), "command: docs") {
t.Fatalf("saved config:\n%s", saved)
}
versions, err := syncpkg.FileBackupVersions(s.configPath())
if err != nil || len(versions) != 1 || versions[0].Reason != syncpkg.BackupReasonMigrate {
t.Fatalf("backups: %+v %v", versions, err)
}
if kept, _, err := syncpkg.ReadFileBackupVersion(s.configPath(), versions[0].ID); err != nil || string(kept) != data {
t.Fatalf("backup: %s %v", kept, err)
}
list := httptest.NewRecorder()
s.handleMCPList(list, httptest.NewRequest(http.MethodGet, "/api/mcp", nil))
if list.Code != 200 || strings.Contains(list.Body.String(), `"notices"`) || strings.Contains(list.Body.String(), `"migrates"`) {
t.Fatalf("list: %s", list.Body)
}
}
func TestMCPOpenOnlyListedConfigFiles(t *testing.T) {
s, _ := newTestServerWithExtras(t, nil, "")
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("CLAUDE_CONFIG_DIR", "")
t.Setenv("VISUAL", "")
t.Setenv("EDITOR", "true")
// A browser on this machine, then ones elsewhere: on the LAN, and behind a reverse proxy on
// this machine that keeps the browser's Host or adds a forwarding header.
local := func(r *http.Request) { r.RemoteAddr, r.Host = "127.0.0.1:1", "localhost:19420" }
remote := map[string]func(*http.Request){
"lan": func(r *http.Request) { r.RemoteAddr, r.Host = "192.168.1.9:1", "192.168.1.5:19420" },
"proxy host": func(r *http.Request) { r.RemoteAddr, r.Host = "127.0.0.1:1", "192.168.1.5" },
"proxy header": func(r *http.Request) {
local(r)
r.Header.Set("X-Forwarded-For", "192.168.1.9")
},
}
list := func(from func(*http.Request)) string {
req := httptest.NewRequest(http.MethodGet, "/api/mcp", nil)
from(req)
rec := httptest.NewRecorder()
s.handleMCPList(rec, req)
return rec.Body.String()
}
open := func(from func(*http.Request), body string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodPost, "/api/mcp/open", strings.NewReader(body))
from(req)
rec := httptest.NewRecorder()
s.handleMCPOpen(rec, req)
return rec
}
if rec := open(local, `{"target":"../etc/passwd"}`); rec.Code != 400 {
t.Fatalf("unknown target: %d", rec.Code)
}
if rec := open(local, `{"target":"claude"}`); rec.Code != 404 {
t.Fatalf("missing file: %d", rec.Code)
}
path := filepath.Join(home, ".claude.json")
if err := os.WriteFile(path, []byte("{}"), 0600); err != nil {
t.Fatal(err)
}
for name, from := range remote {
if body := list(from); !strings.Contains(body, `"openable":[]`) {
t.Fatalf("%s list: %s", name, body)
}
if rec := open(from, `{"target":"claude"}`); rec.Code != 403 {
t.Fatalf("%s open: %d", name, rec.Code)
}
}
if body := list(local); !strings.Contains(body, `"openable":["claude"]`) {
t.Fatalf("local list: %s", body)
}
rec := open(local, `{"target":"claude"}`)
var resp openInEditorResponse
if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &resp) != nil || resp.Editor != "true" || resp.Path != path {
t.Fatalf("open: %d %s", rec.Code, rec.Body)
}
}