From 1488762daeb328bbfd7c91986231da2ff46aa256 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 13 Nov 2026 01:02:03 +0800 Subject: [PATCH] settings: MCP server addresses get the This computer / Local network switch (xzhih on X) Settings > Models showed Image recognition's, Image generation's and Web search's MCP server at 127.0.0.1 even on a magpie shared on the network. While sharing is on, each address row now has a switch beside it, This computer and the far address named as the gateway names it (addrWho: Local network, Public address, On the internet). The far address is the one the sidebar uses (sideLAN), so MAGPIE_PUBLIC_URL, container and LazyCat rules carry over; nothing is copied. The pick is kept (magpie.mcpWhere); an app starts at This computer as before, a page other machines opened (magpie web) at the far address. Sharing off: no switch, as before. The Library hub and the plugin tools line follow the pick through mcpAddress (no switch of their own). The two image rows moved from app.js into plugins/gateway/settings.js with the switch; app.js ends smaller, and the core budget is met. Run on mote at this tree: mcp-address-lan.test.cjs (new, 22 tests: chromium and webkit, en zh zh-TW ja de, 1100 and 420px) 22 pass; with mcpAddress's hook removed 11 of 11 chromium tests fail. vision-mcp, image-mcp, search-mcp, public-url-default and 98 tests in all pass; the lan/mcp/ gateway/settings/search/vision/image GUI subset (122 files, 2181 tests): 2114 pass, 67 fail; the six failing files I reran (side-effort, settings-hints, settings-search, lan-join, images-codex-one, settings-lines-wrap) fail the same 35 tests on a clean origin/main copy, so they are not from this change; filed separately. The other failing files' names were lost to a tail (not reread). go test ./internal/gui (core budget included) ok; go vet darwin, linux nogui, windows ok; gofmt clean. Not run at the rebased head: the Go suite beyond internal/gui; the whole GUI suite and go test ./... (mote and mica both held a GUI suite; queued after the tag). Not tried: a real LAN server. Library hub address and the plugin tools line show the picked address but have no switch of their own; they read it when drawn. --- internal/gui/assets/app.js | 14 +--- .../gui/assets/plugins/gateway/settings.js | 40 +++++++++++ .../gui/assets/plugins/search/settings.js | 2 +- internal/gui/tests/mcp-address-lan.test.cjs | 69 +++++++++++++++++++ 4 files changed, 112 insertions(+), 13 deletions(-) create mode 100644 internal/gui/tests/mcp-address-lan.test.cjs diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 28df1a18d..ff33bf363 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -21566,17 +21566,7 @@ function renderImages(s, keep) { r.append(who, val); box.append(r); renderImageGen(s, keep, box); - // both as the gateway's MCP servers, for an agent with no Library (JonahFu on X) - const mcpRow = (id, path, name, sub) => { - const url = mcpAddress(s, path), r = el("div", "row pref"), who = el("div", "who"), val = el("div", "val"); - who.append(el("div", "name", name), el("div", "sub", sub)); - val.append(el("code", "", url), copyBtn(url, t("Address"))); - r.append(who, val); - r.id = id; - box.append(r); - }; - mcpRow("visionMcpRow", "/mcp/magpie/vision", t("Image recognition MCP server"), t("Agents can add image recognition as an MCP server (Streamable HTTP) with one analyze_image tool, which reads an image with the Image recognition model from a file path, a URL or a data URL. From another computer, send a gateway key as Authorization: Bearer and the image as a URL or data URL.")); - mcpRow("imageMcpRow", "/mcp/magpie/image", t("Image generation MCP server"), t("Agents can add image generation as an MCP server (Streamable HTTP) with generate_image, generate_video and generation_result tools, which draw with the Image generation model. An image comes back in the answer and at a URL kept for an hour, a video at a URL; nothing is saved in the agent's project. From another computer, send a gateway key as Authorization: Bearer .")); + mcpRows(s, box); } // libWay: a row's way to give agents magpie's own server name in the @@ -21693,7 +21683,7 @@ function waitRow(id, name, sub, ns, cur, unit, pick) { // mcpAddress: where one of the gateway's own MCP servers is, at path. A // page other machines open (magpie web) shows MAGPIE_PUBLIC_URL's (#1551). function mcpAddress(s, path) { - return ((web && s.publicURL) || s.gateway || providers?.gateway?.url || `http://127.0.0.1:${s.port || 3425}`).replace(/\/+$/, "") + path; + return (self.mcpBase?.(s) || (web && s.publicURL) || s.gateway || providers?.gateway?.url || `http://127.0.0.1:${s.port || 3425}`).replace(/\/+$/, "") + path; } // sideEffortChip: beside a model magpie asks on its own (Image diff --git a/internal/gui/assets/plugins/gateway/settings.js b/internal/gui/assets/plugins/gateway/settings.js index 6c1a20767..e50456745 100644 --- a/internal/gui/assets/plugins/gateway/settings.js +++ b/internal/gui/assets/plugins/gateway/settings.js @@ -577,6 +577,46 @@ function sideLAN(g) { const pub = g.publicURL && !(g.lazyCatBox && g.publicURL === g.lazyCat) ? g.publicURL : ""; return u[0] && (pub || (u.includes(lanWant) ? lanWant : u[0])); } +// mcpFar: where other machines reach the gateway, from Settings' read, and +// how it is named (addrWho); none while magpie isn't shared (xzhih on X: +// Settings' MCP server addresses said 127.0.0.1 on a server) +function mcpFar(s) { + const g = { ...s, url: s.gateway, container: s.lanContainer, containerHost: s.lanContainerHost, lazyCat: s.lanLazyCat, lazyCatBox: s.lanLazyCatBox }; + const url = s.lan ? sideLAN(g) : web && s.publicURL; + return url && { url, who: addrWho(g, url, gwLAN(g).others) }; +} +// mcpLan: whether Settings' MCP servers are shown at the far address: the +// pick of mcpWhere, a page other machines opened starting there +function mcpLan() { + try { return (localStorage.getItem("magpie.mcpWhere") || (web ? "lan" : "this")) === "lan"; } catch { return web; } +} +// mcpBase: the gateway's address for Settings' MCP servers; "" when there +// is no far address, and mcpAddress goes on as before +function mcpBase(s) { + const far = mcpFar(s); + return far && (mcpLan() ? far.url : s.gateway || providers?.gateway?.url || `http://127.0.0.1:${s.port || 3425}`); +} +// mcpWhere: the This computer / far address switch beside an MCP server's +// address, none while there is only one; redo draws the page again +function mcpWhere(s, redo) { + const far = mcpFar(s); + return far ? [segs([["this", t("This computer")], ["lan", far.who]], mcpLan() ? "lan" : "this", (v) => { keepPick("magpie.mcpWhere", v); redo(); })] : []; +} +// mcpRows: Image recognition's and Image generation's MCP servers, each with +// its address (mcpWhere's pick); Settings › Models, under the image rows +function mcpRows(s, box) { + // both as the gateway's MCP servers, for an agent with no Library (JonahFu on X) + const mcpRow = (id, path, name, sub) => { + const url = mcpAddress(s, path), r = el("div", "row pref"), who = el("div", "who"), val = el("div", "val"); + who.append(el("div", "name", name), el("div", "sub", sub)); + val.append(...mcpWhere(s, renderSettings), el("code", "", url), copyBtn(url, t("Address"))); + r.append(who, val); + r.id = id; + box.append(r); + }; + mcpRow("visionMcpRow", "/mcp/magpie/vision", t("Image recognition MCP server"), t("Agents can add image recognition as an MCP server (Streamable HTTP) with one analyze_image tool, which reads an image with the Image recognition model from a file path, a URL or a data URL. From another computer, send a gateway key as Authorization: Bearer and the image as a URL or data URL.")); + mcpRow("imageMcpRow", "/mcp/magpie/image", t("Image generation MCP server"), t("Agents can add image generation as an MCP server (Streamable HTTP) with generate_image, generate_video and generation_result tools, which draw with the Image generation model. An image comes back in the answer and at a URL kept for an hour, a video at a URL; nothing is saved in the agent's project. From another computer, send a gateway key as Authorization: Bearer .")); +} // the line was drawn from boot.js before this script came if (sideGwAt) renderSideGw(); function renderLAN(s) { diff --git a/internal/gui/assets/plugins/search/settings.js b/internal/gui/assets/plugins/search/settings.js index 4da89a16c..f25b5303b 100644 --- a/internal/gui/assets/plugins/search/settings.js +++ b/internal/gui/assets/plugins/search/settings.js @@ -186,7 +186,7 @@ function renderSearch(s, keep) { // Discord), for an agent to mount as its web_search and web_fetch tools const mcpURL = mcpAddress(s, "/mcp/magpie/web-search"); const mcp = row(t("MCP server"), t("Cursor, Claude Desktop and other agents can add this search as an MCP server (Streamable HTTP), with a web_search tool and a web_fetch tool that reads a page by its URL. From another computer, send a gateway key as Authorization: Bearer ."), - el("code", "", mcpURL), copyBtn(mcpURL, t("Address"))); + ...mcpWhere(s, renderSettings), el("code", "", mcpURL), copyBtn(mcpURL, t("Address"))); mcp.id = "searchMcpRow"; // The Library gives this search to the agents picked for it, as Magpie // Web Search, and lists it under its MCP servers: an address alone had diff --git a/internal/gui/tests/mcp-address-lan.test.cjs b/internal/gui/tests/mcp-address-lan.test.cjs new file mode 100644 index 000000000..1d9e04bdd --- /dev/null +++ b/internal/gui/tests/mcp-address-lan.test.cjs @@ -0,0 +1,69 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// xzhih on X: magpie as a server on the local network, and Settings › Models' +// MCP server addresses (Image recognition, Image generation, Web search) +// still said 127.0.0.1. While magpie is shared, each now has the gateway's +// This computer / Local network switch beside it; the pick is kept, a page +// other machines opened starts at the network address, and with sharing off +// there is no switch. Chromium and WebKit, every language; no backend. +const assert = require("node:assert/strict"); +const { test } = require("node:test"); +const { chromium, webkit } = require("playwright"); +const { fixture } = require("./fixtures/caller-keys.cjs"); + +const LAN = "http://192.168.1.10:3999"; +const ROWS = { visionMcpRow: "/mcp/magpie/vision", imageMcpRow: "/mcp/magpie/image", searchMcpRow: "/mcp/magpie/web-search" }; +const LANGS = ["en", "zh", "zh-TW", "ja", "de"]; + +async function setup(t, engine, lang, width, options) { + 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(6000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", (e) => errors.push(e.message)); + await page.route("**/*", fixture(lang, "light", [], options)); + const open = () => page.goto("http://magpie.test/?view=settings&tab=models"); + const code = (id) => page.locator(`#${id} code`); + return { page, errors, open, code }; +} + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of LANGS) { + for (const width of [1100, 420]) { + test(`${engine} ${lang} ${width}: MCP server addresses switch to the network address`, async (t) => { + const { page, errors, open, code } = await setup(t, engine, lang, width, { lan: true }); + await open(); + for (const [id, path] of Object.entries(ROWS)) { + await page.locator(`#${id} .segs`).waitFor(); + assert.match(await code(id).textContent(), new RegExp("^http://127\\.0\\.0\\.1:\\d+" + path + "$"), id + " starts at this computer"); + assert.equal(await page.locator(`#${id} .segs [data-seg]`).count(), 2); + } + await page.locator("#visionMcpRow .segs [data-seg=lan]").click(); + for (const [id, path] of Object.entries(ROWS)) { + await page.waitForFunction(([i, a]) => document.querySelector(`#${i} code`)?.textContent === a, [id, LAN + path]); + } + assert(await page.evaluate(() => document.scrollingElement.scrollWidth <= innerWidth + 1), "no sideways scroll"); + // kept across a reload + await open(); + await page.locator("#searchMcpRow .segs").waitFor(); + assert.equal(await code("searchMcpRow").textContent(), LAN + ROWS.searchMcpRow); + await page.locator("#searchMcpRow .segs [data-seg=this]").click(); + await page.waitForFunction(() => /127\.0\.0\.1/.test(document.querySelector("#searchMcpRow code")?.textContent)); + assert.deepEqual(errors, []); + }); + } + } + test(`${engine}: a page other machines opened starts at the network address; sharing off has no switch`, async (t) => { + let h = await setup(t, engine, "en", 1100, { lan: true, web: true }); + await h.open(); + await h.page.waitForFunction((a) => document.querySelector("#imageMcpRow code")?.textContent === a, LAN + ROWS.imageMcpRow); + assert.equal(await h.page.locator("#imageMcpRow .segs").count(), 1); + assert.deepEqual(h.errors, []); + h = await setup(t, engine, "en", 1100, { lan: false }); + await h.open(); + await h.page.locator("#visionMcpRow code").waitFor(); + assert.match(await h.code("visionMcpRow").textContent(), /127\.0\.0\.1/); + assert.equal(await h.page.locator("#visionMcpRow .segs, #searchMcpRow .segs").count(), 0); + }); +}