Files
magpie/internal/gui/tests/search-mcp.test.cjs
T
yetone 552c2244dd gateway, gui: magpie's web search is an MCP server too (Kayphoon on Discord)
What: /mcp/magpie/web-search serves the gateway's existing web search
(webSearch: the searcher's model, a Kimi Code plan's service, the search
APIs, in the order Settings sets) as an MCP server with one web_search
tool, so Cursor, Claude Desktop (through mcp-remote) or any agent can
mount it. Streamable HTTP only: POSTed JSON-RPC messages and batches are
answered with JSON (initialize with version negotiation, ping, tools/list,
tools/call), notifications get 202, GET/DELETE 405. Legacy SSE isn't
served. Two segments under /mcp/, so it can't hide a signed-in server's
/mcp/{name}.

Guards: lanGuard as on every route (a gateway key from another machine,
only while shared; rebound browser hosts refused), plus a local/shared
backstop in the handler. Any web page Origin (localhost too) is refused
unless listed in Settings, as the MCP spec asks of a server. keyLimited
passes /mcp/ by, so each tools/call reserves the calling key's limit
(budget.Reserve) and a spent key gets a tool error with no search. The
search is marked CallFor{MCP: true}, and Routing says the agent called
magpie's web_search MCP tool. With nothing that can search, the tool error
says where to add a search API.

GUI: Settings > Models > Web search has an "MCP server" row with the
address (from the Settings page's gateway URL) and Copy, in en, zh, zh-TW,
ja, de. docs/subsystems/gateway-routing.md has a section on it.

Run:
- go test -tags nogui -timeout 40m ./... (all ok, at the first rebase),
  then gateway/gui package tests again after rebasing onto b1396d23.
- TestSearchMCP* (protocol, guards, key limit, Routing mark, no
  searcher). Break checks: dropping Reserve, the Origin check or MCP:true
  each turns its test red; dropping the handler's local/shared check does
  not, because lanGuard already refuses (it is a backstop).
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ./...
- GUI, Chromium + WebKit: search-mcp (new, 5 languages, 520px), i18n-keys,
  gui-ja, gui-de, gui-zh-tw, search-first, search-api, searcher-pick,
  routing-side-calls. search-mcp caught the address falling back to 3425.
  search-mcp first held its row across Settings' redraws ("not attached
  to the DOM" 3 times in ~60 WebKit runs under load ~150); it now reads
  the row in one evaluate, and 3 full runs in both engines passed 60/60.
- Sandbox magpie (temp HOME, MAGPIE_ADDR 127.0.0.1:3512) with a fake
  SearXNG on 3513: the official MCP TypeScript SDK client 1.32.1 did
  initialize (2025-06-18), tools/list, tools/call and got the hit, as did
  mcp-remote 0.14.3 over stdio; curl: GET 405, localhost/foreign Origin
  403, rebound host 403, notification 202.
2026-10-11 01:55:34 +08:00

116 lines
7.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Run with Node's test runner and Playwright on the module path; see README.md.
// magpie's web search as an MCP server (Kayphoon on Discord): Settings ›
// Models › Web search has an "MCP server" row with the gateway's address of
// it, /mcp/magpie/web-search, to copy into Cursor, Claude Desktop or any
// other agent, and says a gateway key goes with it from another computer.
// A search an agent asked of it reads in Routing as that agent's MCP call,
// not as a search magpie ran for one of its models. In every language.
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const path = require("node:path");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const assets = path.resolve(__dirname, "../assets");
const address = "http://127.0.0.1:3999/mcp/magpie/web-search";
const words = {
en: { name: "MCP server", sub: /Cursor, Claude Desktop .*web_search.*Authorization: Bearer <key>/, story: /Claude Code called magpie's web_search MCP tool: codex\/gpt-6\.1-sol searched for it/ },
zh: { name: "MCP 服务器", sub: /Cursor、Claude Desktop .*web_search.*Authorization: Bearer <key>/, story: /Claude Code 调用了 magpie 的 web_search MCP 工具:由 codex\/gpt-6\.1-sol 代为搜索/ },
"zh-TW": { name: "MCP 伺服器", sub: /web_search.*Authorization: Bearer <key>/, story: /Claude Code 呼叫了 magpie 的 web_search MCP 工具:由 codex\/gpt-6\.1-sol 代為搜尋/ },
ja: { name: "MCP サーバー", sub: /web_search.*Authorization: Bearer <key>/, story: /Claude Code が magpie の web_search MCP ツールを呼び出しました。codex\/gpt-6\.1-sol が代わりに検索しました/ },
de: { name: "MCP-Server", sub: /web_search.*Authorization: Bearer <key>/, story: /Claude Code hat das web_search-MCP-Tool von magpie aufgerufen: codex\/gpt-6\.1-sol hat dafür gesucht/ },
};
const now = new Date();
const day = [now.getFullYear(), now.getMonth() + 1, now.getDate()].map((n) => String(n).padStart(2, "0")).join("-");
const routes = [{
id: 7, seq: 7, time: new Date(now.getTime() - 60e3).toISOString(), agent: "magpie", model: "codex/gpt-6.1-sol", provider: "codex",
kind: "web_search", for: { agent: "claude-code", mcp: true },
order: [{ id: "codex", provider: "codex", name: "Codex", who: "a@b.c", kind: "account", model: "gpt-6.1-sol" }],
tries: [{ id: "codex", model: "gpt-6.1-sol", start: new Date(now.getTime() - 60e3).toISOString(), done: true, status: 200, ms: 900 }],
done: true, status: 200, ms: 900, tokens: 1200,
}];
function serve(lang) {
const settings = { lang, theme: "light", searcher: "", gateway: "http://127.0.0.1:3999", port: 3999,
searchVendors: [{ id: "tavily", name: "Tavily", keysURL: "https://app.tavily.com/home" }],
searchAPIs: [{ vendor: "tavily", name: "Tavily", key: "tvly…abcd", ready: true }], searchChoices: [] };
const state = {
agents: [], profiles: [], settings,
clients: [{ id: "claude-code", name: "Claude Code", icon: "claude-color" }, { id: "magpie", name: "magpie", icon: "magpie" }],
};
return async (r) => {
const url = new URL(r.request().url());
const json = (data) => r.fulfill({ json: data });
if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(state);
if (url.pathname === "/api/settings") return json(settings);
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/gateway/trace") {
if (url.searchParams.get("wait")) await new Promise((res) => setTimeout(res, 20e3));
return json({ mine: true, now: now.toISOString(), seq: 1, totals: { requests: 0, rerouted: 0, errors: 0 }, routes: [] });
}
if (url.pathname === "/api/gateway/history") {
return json({ cut: false, days: [{ day, requests: routes.length }], routes: url.searchParams.get("day") ? routes : [] });
}
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
if (url.pathname.startsWith("/api/")) return json({});
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
try { await r.fulfill({ body: await fs.readFile(file), contentType }); } catch { await r.fulfill({ status: 404, body: "" }); }
};
}
async function open(t, engine, lang, view, width = 1100) {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await (await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang));
await page.goto("http://magpie.test/?view=" + view);
return { page, errors };
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(words)) {
const w = words[lang];
test(`${engine} ${lang}: Web search gives its MCP server's address`, async (t) => {
for (const width of [1100, 520]) {
const { page, errors } = await open(t, engine, lang, "settings&tab=models", width);
// Settings is drawn again as its answers come in, each time with a
// new row: it is read in one go, never held across a redraw
await page.waitForFunction((a) => document.querySelector("#searchList #searchMcpRow code")?.textContent === a, address);
const row = await page.evaluate(() => {
const r = document.querySelector("#searchList #searchMcpRow");
r.scrollIntoView({ block: "nearest" });
return { name: r.querySelector(".name").textContent, sub: r.querySelector(".sub").textContent,
code: r.querySelector("code").textContent, buttons: r.querySelectorAll("button").length };
});
assert.equal(row.name, w.name);
assert.match(row.sub, w.sub);
assert.equal(row.code, address);
assert.equal(row.buttons, 1, "a copy button");
// nothing spills out of the window at a narrow width
assert(await page.evaluate(() => document.scrollingElement.scrollWidth <= innerWidth + 1), `no sideways scroll at ${width}px`);
assert.deepEqual(errors, []);
}
});
test(`${engine} ${lang}: an agent's search through the MCP server reads as its own call`, async (t) => {
const { page, errors } = await open(t, engine, lang, "routing");
await page.locator(".rt-days .rt-day").nth(1).click();
const req = page.locator(".rt-req").first();
await req.waitFor();
await req.click();
const story = page.locator(".rt-steps li.kind");
await story.waitFor();
assert.match(await story.textContent(), w.story);
assert.deepEqual(errors, []);
});
}
}