askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
mcp.test.ts · 38.5 KB · 621 lines
| 1 | import { createHash } from "node:crypto"; |
| 2 | import { connect as tcpConnect } from "node:net"; |
| 3 | import { request } from "node:http"; |
| 4 | import { describe, expect, it } from "vitest"; |
| 5 | import { McpAuthError, McpClient, parseEventStream } from "../src/mcp-client"; |
| 6 | import { OAuthError, REDIRECT_PORTS, authorizeUrl, discover, exchangeCode, listenForCode, newPkce, refreshTokens, register, revoke, type Fetch, type ServerMeta } from "../src/mcp-oauth"; |
| 7 | import { DEFAULT_SERVERS, McpService, createStore, dropKeys, loadServerConfigs, parseServerConfigs, writeSummary, type McpServerConfig, type Store } from "../src/mcp-service"; |
| 8 | |
| 9 | type Handler = (url: URL, init: { method?: string; headers?: Record<string, string>; body?: string }) => { status?: number; json?: unknown; text?: string; headers?: Record<string, string> }; |
| 10 | |
| 11 | /** A fetch that answers from a routing function, and remembers what it was asked. */ |
| 12 | function fakeFetch(handler: Handler): Fetch & { calls: Array<{ url: string; method: string; headers: Record<string, string>; body: string }> } { |
| 13 | const calls: Array<{ url: string; method: string; headers: Record<string, string>; body: string }> = []; |
| 14 | const f = (async (url: string, init = {}) => { |
| 15 | calls.push({ url, method: init.method ?? "GET", headers: init.headers ?? {}, body: init.body ?? "" }); |
| 16 | const r = handler(new URL(url), init); |
| 17 | const status = r.status ?? 200; |
| 18 | const text = r.text ?? JSON.stringify(r.json ?? {}); |
| 19 | const headers = new Map(Object.entries({ "content-type": r.text !== undefined ? "text/plain" : "application/json", ...(r.headers ?? {}) }).map(([k, v]) => [k.toLowerCase(), v])); |
| 20 | return { ok: status >= 200 && status < 300, status, headers: { get: (n: string) => headers.get(n.toLowerCase()) ?? null }, text: async () => text }; |
| 21 | }) as Fetch & { calls: typeof calls }; |
| 22 | f.calls = calls; |
| 23 | return f; |
| 24 | } |
| 25 | |
| 26 | const BASE = "https://mcp.example.com"; |
| 27 | const META_DOCS: Handler = (u) => { |
| 28 | if (u.pathname === "/.well-known/oauth-protected-resource") return { json: { resource: `${BASE}/api/mcp/`, authorization_servers: [BASE], scopes_supported: ["mcp"] } }; |
| 29 | if (u.pathname === "/.well-known/oauth-authorization-server") |
| 30 | return { json: { issuer: BASE, authorization_endpoint: `${BASE}/o/authorize/`, token_endpoint: `${BASE}/o/token/`, registration_endpoint: `${BASE}/o/register/`, revocation_endpoint: `${BASE}/o/revoke_token/`, code_challenge_methods_supported: ["S256"] } }; |
| 31 | return { status: 404 }; |
| 32 | }; |
| 33 | const META: ServerMeta = { resource: `${BASE}/api/mcp/`, issuer: BASE, authorizationEndpoint: `${BASE}/o/authorize/`, tokenEndpoint: `${BASE}/o/token/`, registrationEndpoint: `${BASE}/o/register/`, revocationEndpoint: `${BASE}/o/revoke_token/`, scopes: ["mcp"] }; |
| 34 | |
| 35 | describe("finding out how to sign in", () => { |
| 36 | it("follows the protected-resource document to the authorization server and its endpoints", async () => { |
| 37 | const m = await discover(`${BASE}/api/mcp/`, fakeFetch(META_DOCS)); |
| 38 | expect(m).toMatchObject({ resource: `${BASE}/api/mcp/`, issuer: BASE, tokenEndpoint: `${BASE}/o/token/`, registrationEndpoint: `${BASE}/o/register/`, scopes: ["mcp"] }); |
| 39 | }); |
| 40 | |
| 41 | it("refuses a server that cannot do PKCE with S256, or that publishes nothing", async () => { |
| 42 | const noS256: Handler = (u, i) => (u.pathname.includes("authorization-server") ? { json: { authorization_endpoint: `${BASE}/a`, token_endpoint: `${BASE}/t`, code_challenge_methods_supported: ["plain"] } } : META_DOCS(u, i)); |
| 43 | await expect(discover(`${BASE}/api/mcp/`, fakeFetch(noS256))).rejects.toThrow(/S256/); |
| 44 | await expect(discover(`${BASE}/api/mcp/`, fakeFetch(() => ({ status: 404 })))).rejects.toMatchObject({ code: "discovery" }); |
| 45 | }); |
| 46 | |
| 47 | it("will not talk to plain http (except to this machine) or to an authorization server it was sent somewhere unsafe", async () => { |
| 48 | await expect(discover("http://mcp.example.com/api/mcp/", fakeFetch(META_DOCS))).rejects.toMatchObject({ code: "unsafe" }); |
| 49 | const evil: Handler = (u, i) => (u.pathname.endsWith("protected-resource") ? { json: { resource: `${BASE}/api/mcp/`, authorization_servers: ["http://evil.example.com"] } } : META_DOCS(u, i)); |
| 50 | await expect(discover(`${BASE}/api/mcp/`, fakeFetch(evil))).rejects.toMatchObject({ code: "unsafe" }); |
| 51 | const sneaky: Handler = (u, i) => (u.pathname.includes("authorization-server") ? { json: { authorization_endpoint: "javascript:alert(1)", token_endpoint: `${BASE}/t` } } : META_DOCS(u, i)); |
| 52 | await expect(discover(`${BASE}/api/mcp/`, fakeFetch(sneaky))).rejects.toMatchObject({ code: "unsafe" }); |
| 53 | }); |
| 54 | |
| 55 | it("registers a public client with every loopback redirect, and no secret", async () => { |
| 56 | const f = fakeFetch(() => ({ status: 201, json: { client_id: "cid-1" } })); |
| 57 | expect(await register(META, f, "dbbasic-browser")).toBe("cid-1"); |
| 58 | const sent = JSON.parse(f.calls[0]!.body); |
| 59 | expect(sent).toMatchObject({ client_name: "dbbasic-browser", token_endpoint_auth_method: "none", grant_types: ["authorization_code", "refresh_token"] }); |
| 60 | expect(sent.redirect_uris).toEqual(REDIRECT_PORTS.map((p) => `http://127.0.0.1:${p}/callback`)); |
| 61 | await expect(register({ ...META, registrationEndpoint: undefined }, f, "x")).rejects.toMatchObject({ code: "registration" }); |
| 62 | await expect(register(META, fakeFetch(() => ({ status: 429, json: {} })), "x")).rejects.toThrow(/refused \(429\)/); |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | describe("the authorization request", () => { |
| 67 | it("makes a verifier and a matching S256 challenge, and puts everything in the address", () => { |
| 68 | const { verifier, challenge } = newPkce(); |
| 69 | expect(verifier.length).toBeGreaterThanOrEqual(43); |
| 70 | expect(challenge).toBe(createHash("sha256").update(verifier).digest("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "")); |
| 71 | const u = new URL(authorizeUrl(META, "cid", "http://127.0.0.1:53682/callback", challenge, "st")); |
| 72 | expect(Object.fromEntries(u.searchParams)).toMatchObject({ response_type: "code", client_id: "cid", redirect_uri: "http://127.0.0.1:53682/callback", code_challenge: challenge, code_challenge_method: "S256", state: "st", scope: "mcp" }); |
| 73 | expect(newPkce().verifier).not.toBe(verifier); |
| 74 | }); |
| 75 | }); |
| 76 | |
| 77 | function get(port: number, path: string, host?: string): Promise<number> { |
| 78 | return new Promise((resolve, reject) => { |
| 79 | const r = request({ host: "127.0.0.1", port, path, headers: host ? { host } : {} }, (res) => (res.resume(), resolve(res.statusCode ?? 0))); |
| 80 | r.on("error", reject); |
| 81 | r.end(); |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | describe("the redirect listener", () => { |
| 86 | it("takes the code only for the right state, and ignores stray requests", async () => { |
| 87 | const cb = await listenForCode("right", { ports: [0] }); |
| 88 | expect(cb.redirectUri).toBe(`http://127.0.0.1:${cb.port}/callback`); |
| 89 | expect(await get(cb.port, "/other")).toBe(404); |
| 90 | expect(await get(cb.port, "/callback?code=x&state=wrong")).toBe(400); |
| 91 | expect(await get(cb.port, "/callback?code=x&state=right", "evil.example.com")).toBe(404); // a rebinding page cannot finish it |
| 92 | expect(await get(cb.port, "/callback?code=THE-CODE&state=right")).toBe(200); |
| 93 | await expect(cb.code).resolves.toBe("THE-CODE"); |
| 94 | }); |
| 95 | |
| 96 | it("reports a denial without reflecting anything into the page, and a cancel", async () => { |
| 97 | const cb = await listenForCode("s", { ports: [0] }); |
| 98 | expect(await get(cb.port, "/callback?error=access_denied&state=s")).toBe(400); |
| 99 | await expect(cb.code).rejects.toMatchObject({ code: "denied", message: "sign-in was declined" }); |
| 100 | const c2 = await listenForCode("s", { ports: [0] }); |
| 101 | c2.cancel(); |
| 102 | await expect(c2.code).rejects.toMatchObject({ code: "denied" }); |
| 103 | }); |
| 104 | |
| 105 | it("times out, and moves to the next port when one is busy", async () => { |
| 106 | const t = await listenForCode("s", { ports: [0], timeoutMs: 40 }); |
| 107 | await expect(t.code).rejects.toMatchObject({ code: "timeout" }); |
| 108 | const first = await listenForCode("s", { ports: [0] }); |
| 109 | const second = await listenForCode("s", { ports: [first.port, 0] }); |
| 110 | expect(second.port).not.toBe(first.port); |
| 111 | first.cancel(); |
| 112 | second.cancel(); |
| 113 | }); |
| 114 | }); |
| 115 | |
| 116 | describe("tokens", () => { |
| 117 | it("trades a code for tokens, sending the verifier and no secret", async () => { |
| 118 | const f = fakeFetch(() => ({ json: { access_token: "AT", refresh_token: "RT", expires_in: 3600, scope: "mcp" } })); |
| 119 | const t = await exchangeCode(META, f, { clientId: "cid", code: "c", verifier: "v", redirect: "http://127.0.0.1:53682/callback" }, 1_000); |
| 120 | expect(t).toEqual({ access: "AT", refresh: "RT", expiresAt: 1_000 + 3_600_000, scope: "mcp" }); |
| 121 | const body = new URLSearchParams(f.calls[0]!.body); |
| 122 | expect(Object.fromEntries(body)).toMatchObject({ grant_type: "authorization_code", code: "c", code_verifier: "v", client_id: "cid", redirect_uri: "http://127.0.0.1:53682/callback" }); |
| 123 | expect(body.has("client_secret")).toBe(false); |
| 124 | }); |
| 125 | |
| 126 | it("explains a refusal without echoing the server's text, and needs an access token", async () => { |
| 127 | await expect(exchangeCode(META, fakeFetch(() => ({ status: 400, json: { error: "invalid_grant" } })), { clientId: "c", code: "c", verifier: "v", redirect: "r" })).rejects.toThrow(/refused \(400: invalid_grant\)/); |
| 128 | await expect(exchangeCode(META, fakeFetch(() => ({ json: {} })), { clientId: "c", code: "c", verifier: "v", redirect: "r" })).rejects.toThrow(/no access token/); |
| 129 | }); |
| 130 | |
| 131 | it("keeps the old refresh token when a refresh does not send a new one", async () => { |
| 132 | const f = fakeFetch(() => ({ json: { access_token: "AT2", expires_in: 60 } })); |
| 133 | expect(await refreshTokens(META, f, { clientId: "c", refresh: "RT" }, 0)).toEqual({ access: "AT2", refresh: "RT", expiresAt: 60_000 }); |
| 134 | const g = fakeFetch(() => ({ json: { access_token: "AT3", refresh_token: "RT2" } })); |
| 135 | expect((await refreshTokens(META, g, { clientId: "c", refresh: "RT" }, 0)).refresh).toBe("RT2"); |
| 136 | }); |
| 137 | |
| 138 | it("revokes best-effort and never throws", async () => { |
| 139 | expect(await revoke(META, fakeFetch(() => ({ status: 200 })), "c", "t")).toBe(true); |
| 140 | expect(await revoke(META, fakeFetch(() => ({ status: 500 })), "c", "t")).toBe(false); |
| 141 | expect(await revoke({ ...META, revocationEndpoint: undefined }, fakeFetch(() => ({})), "c", "t")).toBe(false); |
| 142 | }); |
| 143 | }); |
| 144 | |
| 145 | describe("the MCP client", () => { |
| 146 | const rpc = (result: unknown, id = 1) => ({ jsonrpc: "2.0", id, result }); |
| 147 | |
| 148 | it("starts a session, sends the token, and lists tools across pages", async () => { |
| 149 | let n = 0; |
| 150 | const f = fakeFetch((_u, init) => { |
| 151 | const b = JSON.parse(init.body ?? "{}"); |
| 152 | if (b.method === "initialize") return { json: rpc({}, b.id), headers: { "mcp-session-id": "S1" } }; |
| 153 | if (b.method === "notifications/initialized") return { status: 202, text: "" }; |
| 154 | if (b.method === "tools/list") return { json: rpc(n++ === 0 ? { tools: [{ name: "a" }], nextCursor: "p2" } : { tools: [{ name: "b", description: "B", inputSchema: { type: "object" } }] }, b.id) }; |
| 155 | return { status: 400 }; |
| 156 | }); |
| 157 | const c = new McpClient("https://mcp.example.com/api/mcp/", async () => "TOKEN", f); |
| 158 | expect((await c.listTools()).map((t) => t.name)).toEqual(["a", "b"]); |
| 159 | expect(f.calls.every((x) => x.headers.authorization === "Bearer TOKEN")).toBe(true); |
| 160 | expect(f.calls.filter((x) => x.headers["mcp-session-id"] === "S1").length).toBeGreaterThanOrEqual(3); // later calls carry the session |
| 161 | expect(f.calls.filter((x) => JSON.parse(x.body).method === "initialize").length).toBe(1); |
| 162 | }); |
| 163 | |
| 164 | it("reads an event-stream reply and joins the text of a tool result", async () => { |
| 165 | expect(parseEventStream('event: message\ndata: {"jsonrpc":"2.0","id":3,"result":{"x":1}}\n\n', 3)).toMatchObject({ result: { x: 1 } }); |
| 166 | expect(parseEventStream('data: {"id":9}\n\n', 3)).toBeNull(); |
| 167 | const f = fakeFetch((_u, init) => { |
| 168 | const b = JSON.parse(init.body ?? "{}"); |
| 169 | if (b.method === "tools/call") return { text: `data: ${JSON.stringify(rpc({ content: [{ type: "text", text: "one" }, { type: "image" }, { type: "text", text: "two" }], isError: false }, b.id))}\n\n`, headers: { "content-type": "text/event-stream" } }; |
| 170 | return { json: rpc({}, b.id) }; |
| 171 | }); |
| 172 | const r = await new McpClient("https://x.example/mcp", async () => "T", f).callTool("t", { a: 1 }); |
| 173 | expect(r).toEqual({ text: "one\n[image not shown]\ntwo", isError: false }); |
| 174 | }); |
| 175 | |
| 176 | it("a 401, or the server's own 'authentication required', is an auth error; other errors are plain errors", async () => { |
| 177 | await expect(new McpClient("https://x.example/mcp", async () => "T", fakeFetch(() => ({ status: 401, json: {} }))).listTools()).rejects.toBeInstanceOf(McpAuthError); |
| 178 | const f = fakeFetch((_u, init) => ({ json: { jsonrpc: "2.0", id: JSON.parse(init.body ?? "{}").id, error: { code: -32000, message: "Authentication required" } } })); |
| 179 | await expect(new McpClient("https://x.example/mcp", async () => "T", f).listTools()).rejects.toBeInstanceOf(McpAuthError); |
| 180 | const g = fakeFetch((_u, init) => ({ json: { jsonrpc: "2.0", id: JSON.parse(init.body ?? "{}").id, error: { code: -32602, message: "bad arguments" } } })); |
| 181 | await expect(new McpClient("https://x.example/mcp", async () => "T", g).listTools()).rejects.toThrow("bad arguments"); |
| 182 | }); |
| 183 | }); |
| 184 | |
| 185 | // ---- the service, against one fake server that does OAuth and MCP --------------------------------------------- |
| 186 | |
| 187 | const SERVER: McpServerConfig = { |
| 188 | id: "demo", name: "Demo", url: `${BASE}/api/mcp/`, |
| 189 | tools: ["read_thing", "write_thing", "get_person"], |
| 190 | readOnly: ["read_thing", "get_person"], |
| 191 | fixedArgs: { write_thing: { project_id: "P-1" } }, |
| 192 | dropFields: { get_person: ["notes"] }, |
| 193 | }; |
| 194 | |
| 195 | function fakeServer() { |
| 196 | const st = { clock: 1_000_000, issued: 0, refreshed: 0, revoked: 0, registered: 0, lastArgs: null as Record<string, unknown> | null, badRefresh: false, expireAccess: false }; |
| 197 | const f = fakeFetch((u, init) => { |
| 198 | const meta = META_DOCS(u, init); |
| 199 | if (meta.status !== 404) return meta; |
| 200 | if (u.pathname === "/o/register/") return (st.registered++, { status: 201, json: { client_id: "cid-" + st.registered } }); |
| 201 | if (u.pathname === "/o/token/") { |
| 202 | const b = new URLSearchParams(init.body); |
| 203 | if (b.get("grant_type") === "refresh_token") { |
| 204 | if (st.badRefresh) return { status: 400, json: { error: "invalid_grant" } }; |
| 205 | st.refreshed++; |
| 206 | return { json: { access_token: `AT-r${st.refreshed}`, expires_in: 3600 } }; |
| 207 | } |
| 208 | st.issued++; |
| 209 | return { json: { access_token: `AT-${st.issued}`, refresh_token: "RT-1", expires_in: 3600 } }; |
| 210 | } |
| 211 | if (u.pathname === "/o/revoke_token/") return (st.revoked++, { status: 200, text: "" }); |
| 212 | if (u.pathname === "/api/mcp/") { |
| 213 | if (st.expireAccess || !/^Bearer AT-/.test(init.headers?.authorization ?? "")) return { status: 401, json: {} }; |
| 214 | const b = JSON.parse(init.body ?? "{}"); |
| 215 | if (b.method === "initialize") return { json: { jsonrpc: "2.0", id: b.id, result: {} } }; |
| 216 | if (b.method === "notifications/initialized") return { status: 202, text: "" }; |
| 217 | if (b.method === "tools/list") |
| 218 | return { json: { jsonrpc: "2.0", id: b.id, result: { tools: [ |
| 219 | { name: "read_thing", description: "Read a thing", inputSchema: { type: "object", properties: { q: { type: "string" } } } }, |
| 220 | { name: "write_thing", description: "Write a thing", inputSchema: { type: "object", properties: { title: { type: "string" }, project_id: { type: "string" } }, required: ["title", "project_id"] } }, |
| 221 | { name: "get_person", description: "Get a person", inputSchema: { type: "object" } }, |
| 222 | { name: "delete_everything", description: "Not allowed", inputSchema: { type: "object" } }, |
| 223 | ] } } }; |
| 224 | if (b.method === "tools/call") { |
| 225 | st.lastArgs = b.params.arguments; |
| 226 | const text = b.params.name === "get_person" ? JSON.stringify({ name: "Ada", notes: "secret notes", nested: [{ notes: "more", ok: 1 }] }) : "done"; |
| 227 | return { json: { jsonrpc: "2.0", id: b.id, result: { content: [{ type: "text", text }] } } }; |
| 228 | } |
| 229 | } |
| 230 | return { status: 404 }; |
| 231 | }); |
| 232 | return { f, st }; |
| 233 | } |
| 234 | |
| 235 | function memStore(): Store & { text: string | null } { |
| 236 | const s = { persistent: true, text: null as string | null, load: () => s.text, save: (t: string | null) => void (s.text = t) }; |
| 237 | return s; |
| 238 | } |
| 239 | |
| 240 | async function signIn(svc: McpService, id = "demo") { |
| 241 | const r = await svc.connect(id, { open: false }); |
| 242 | const u = new URL(r.authorizeUrl); |
| 243 | const redirect = u.searchParams.get("redirect_uri") as string; |
| 244 | const res = await fetch(`${redirect}?code=CODE&state=${u.searchParams.get("state")}`); |
| 245 | expect(res.status).toBe(200); |
| 246 | await r.done; |
| 247 | return u; |
| 248 | } |
| 249 | |
| 250 | function setup(over: { caller?: () => boolean; now?: () => number } = {}) { |
| 251 | const { f, st } = fakeServer(); |
| 252 | const store = memStore(); |
| 253 | const announced: Array<Record<string, unknown>> = []; |
| 254 | const svc = new McpService({ fetch: f, store, servers: [SERVER], openExternal: () => {}, callerNetworkOn: over.caller ?? (() => false), announce: (e) => announced.push(e), now: over.now ?? (() => st.clock) }); |
| 255 | return { svc, f, st, store, announced }; |
| 256 | } |
| 257 | |
| 258 | describe("signing in and using a server's tools", () => { |
| 259 | it("signs in with PKCE through a loopback redirect and keeps the tokens out of state()", async () => { |
| 260 | const { svc, st, store, announced } = setup(); |
| 261 | expect(svc.state()[0]).toMatchObject({ id: "demo", signedIn: false, connecting: false }); |
| 262 | const u = await signIn(svc); |
| 263 | expect(u.searchParams.get("code_challenge_method")).toBe("S256"); |
| 264 | expect(svc.state()[0]).toMatchObject({ signedIn: true, connecting: false }); |
| 265 | expect(JSON.stringify(svc.state())).not.toMatch(/AT-|RT-/); |
| 266 | expect(st.registered).toBe(1); |
| 267 | expect(store.text).toMatch(/AT-1/); // saved (here unencrypted, because the test store is) |
| 268 | expect(announced.some((e) => e.kind === "mcp-signed-in")).toBe(true); |
| 269 | await signIn(svc); // signing in again reuses the registration |
| 270 | expect(st.registered).toBe(1); |
| 271 | }); |
| 272 | |
| 273 | it("remembers a sign-in across a restart", async () => { |
| 274 | const a = setup(); |
| 275 | await signIn(a.svc); |
| 276 | const b = new McpService({ fetch: a.f, store: a.store, servers: [SERVER], openExternal: () => {}, callerNetworkOn: () => false, announce: () => {}, now: () => a.st.clock }); |
| 277 | expect(b.state()[0]?.signedIn).toBe(true); |
| 278 | expect((await b.toolDefs()).length).toBe(3); |
| 279 | }); |
| 280 | |
| 281 | it("gives the assistant only the allowed tools, hides fixed arguments, and asks about every tool that writes", async () => { |
| 282 | const { svc } = setup(); |
| 283 | await signIn(svc); |
| 284 | const tools = await svc.toolDefs(); |
| 285 | expect(tools.map((t) => t.name)).toEqual(["demo__read_thing", "demo__write_thing", "demo__get_person"]); |
| 286 | const w = tools.find((t) => t.name === "demo__write_thing")!; |
| 287 | expect(w.risk).toBe("act"); |
| 288 | expect(Object.keys((w.input_schema as { properties: object }).properties)).toEqual(["title"]); |
| 289 | expect((w.input_schema as { required: string[] }).required).toEqual(["title"]); |
| 290 | const plan = await w.plan!({ title: "x" }); |
| 291 | expect(plan.danger).toBe(true); // asked about even in Auto mode |
| 292 | expect(plan.summary).toBe("Ask Demo to write_thing:\ntitle: x\nproject_id: P-1"); // every argument, the forced one too, one per line |
| 293 | expect(tools.find((t) => t.name === "demo__read_thing")!.risk).toBe("read"); |
| 294 | }); |
| 295 | |
| 296 | it("forces the fixed arguments whatever the model sends, and drops the named fields from results", async () => { |
| 297 | const { svc, st } = setup(); |
| 298 | await signIn(svc); |
| 299 | const tools = await svc.toolDefs(); |
| 300 | await tools.find((t) => t.name === "demo__write_thing")!.run({ title: "t", project_id: "SOMEONE-ELSES" }); |
| 301 | expect(st.lastArgs).toEqual({ title: "t", project_id: "P-1" }); |
| 302 | const person = (await tools.find((t) => t.name === "demo__get_person")!.run({})) as string; |
| 303 | expect(JSON.parse(person)).toEqual({ name: "Ada", nested: [{ ok: 1 }] }); |
| 304 | expect(dropKeys("not json", ["notes"])).toBe("not json"); |
| 305 | }); |
| 306 | |
| 307 | it("refreshes the access token shortly before it expires, once, and keeps working", async () => { |
| 308 | const { svc, st } = setup(); |
| 309 | await signIn(svc); |
| 310 | st.clock += 3_600_000 - 30_000; // 30 s left: inside the 60 s margin |
| 311 | const [a, b] = await Promise.all([svc.token("demo"), svc.token("demo")]); |
| 312 | expect(a).toBe("AT-r1"); |
| 313 | expect(b).toBe("AT-r1"); |
| 314 | expect(st.refreshed).toBe(1); |
| 315 | expect(await svc.token("demo")).toBe("AT-r1"); |
| 316 | }); |
| 317 | |
| 318 | it("refreshNow refreshes at once and says whether the server rotated the refresh token, without showing any token", async () => { |
| 319 | const { svc, st } = setup(); |
| 320 | await signIn(svc); |
| 321 | const r = await svc.refreshNow("demo"); |
| 322 | expect(r).toEqual({ ok: true, rotated: false, expiresInSeconds: 3600 }); // this server keeps the old refresh token |
| 323 | expect(st.refreshed).toBe(1); |
| 324 | expect(JSON.stringify(r)).not.toMatch(/AT-|RT-/); |
| 325 | st.badRefresh = true; |
| 326 | expect(await svc.refreshNow("demo")).toMatchObject({ ok: false, message: expect.stringMatching(/refused \(400/) }); |
| 327 | expect(await setup().svc.refreshNow("demo")).toMatchObject({ ok: false, message: expect.stringMatching(/not signed in/) }); |
| 328 | }); |
| 329 | |
| 330 | it("a refresh the server refuses signs you out, and says so", async () => { |
| 331 | const { svc, st } = setup(); |
| 332 | await signIn(svc); |
| 333 | st.clock += 4_000_000; |
| 334 | st.badRefresh = true; |
| 335 | await expect(svc.token("demo")).rejects.toBeInstanceOf(McpAuthError); |
| 336 | expect(svc.state()[0]).toMatchObject({ signedIn: false, error: expect.stringMatching(/signed out/) }); |
| 337 | }); |
| 338 | |
| 339 | it("a server that stops accepting the token tells the model to ask the person to connect again", async () => { |
| 340 | const { svc, st } = setup(); |
| 341 | await signIn(svc); |
| 342 | const tools = await svc.toolDefs(); |
| 343 | st.expireAccess = true; |
| 344 | await expect(tools[0]!.run({})).rejects.toThrow(/fresh sign-in/); |
| 345 | }); |
| 346 | |
| 347 | it("disconnecting forgets the tokens and asks the server to revoke them", async () => { |
| 348 | const { svc, st, store } = setup(); |
| 349 | await signIn(svc); |
| 350 | expect(await svc.disconnect("demo")).toEqual({ revoked: true }); |
| 351 | expect(st.revoked).toBe(1); |
| 352 | expect(svc.state()[0]?.signedIn).toBe(false); |
| 353 | expect(store.text).not.toMatch(/AT-1/); |
| 354 | expect(await svc.toolDefs()).toEqual([]); |
| 355 | }); |
| 356 | |
| 357 | it("is off while the caller network is on", async () => { |
| 358 | let on = false; |
| 359 | const { svc } = setup({ caller: () => on }); |
| 360 | await signIn(svc); |
| 361 | on = true; |
| 362 | expect(await svc.toolDefs()).toEqual([]); |
| 363 | await expect(svc.connect("demo", { open: false })).rejects.toThrow(/caller network/); |
| 364 | }); |
| 365 | |
| 366 | it("cancelling a sign-in ends it, with the reason kept for the panel", async () => { |
| 367 | const { svc } = setup(); |
| 368 | const r = await svc.connect("demo", { open: false }); |
| 369 | expect(svc.state()[0]?.connecting).toBe(true); |
| 370 | svc.cancelConnect("demo"); |
| 371 | await expect(r.done).rejects.toBeInstanceOf(OAuthError); |
| 372 | expect(svc.state()[0]).toMatchObject({ connecting: false, signedIn: false, error: "sign-in was cancelled" }); |
| 373 | }); |
| 374 | }); |
| 375 | |
| 376 | describe("signing in inside the browser's own tab", () => { |
| 377 | /** A browser that, given the consent address, "approves" and lets the redirect arrive at the service, as the browser's request layer does. */ |
| 378 | function inTab(over: { approve?: boolean; error?: string } = {}) { |
| 379 | const { f, st } = fakeServer(); |
| 380 | const opened: string[] = []; |
| 381 | const closed: number[] = []; |
| 382 | let svc!: McpService; |
| 383 | svc = new McpService({ |
| 384 | fetch: f, store: memStore(), servers: [SERVER], callerNetworkOn: () => false, announce: () => {}, now: () => st.clock, |
| 385 | openExternal: () => { throw new Error("the system browser must not be used"); }, |
| 386 | openTab: async (url) => { |
| 387 | opened.push(url); |
| 388 | if (over.approve === false) return 7; |
| 389 | const u = new URL(url); |
| 390 | const back = new URL(u.searchParams.get("redirect_uri") as string); |
| 391 | if (over.error) back.searchParams.set("error", over.error); |
| 392 | else back.searchParams.set("code", "CODE"); |
| 393 | back.searchParams.set("state", u.searchParams.get("state") as string); |
| 394 | queueMicrotask(() => { caught = svc.captureRedirect(back.href); }); |
| 395 | return 7; |
| 396 | }, |
| 397 | closeTab: async (id) => void closed.push(id), |
| 398 | }); |
| 399 | let caught: ReturnType<McpService["captureRedirect"]> = null; |
| 400 | return { svc, st, opened, closed, caught: () => caught }; |
| 401 | } |
| 402 | const portOpen = (port: number) => new Promise<boolean>((resolve) => { const s = tcpConnect({ host: "127.0.0.1", port }, () => (s.destroy(), resolve(true))); s.on("error", () => resolve(false)); }); |
| 403 | |
| 404 | it("opens the consent page in a tab, catches the redirect itself, closes the tab, and never listens on a port", async () => { |
| 405 | const t = inTab(); |
| 406 | const r = await t.svc.connect("demo"); |
| 407 | expect(t.opened).toHaveLength(1); |
| 408 | expect(new URL(t.opened[0] as string).searchParams.get("redirect_uri")).toBe("http://127.0.0.1:53682/callback"); |
| 409 | expect(await portOpen(53682)).toBe(false); // nothing is listening: the redirect is recognised, not received |
| 410 | await r.done; |
| 411 | expect(t.svc.state()[0]).toMatchObject({ signedIn: true, connecting: false }); |
| 412 | expect(t.closed).toEqual([7]); |
| 413 | expect(t.caught()).toMatchObject({ status: 200 }); |
| 414 | expect(t.caught()?.html).toMatch(/signed in/i); |
| 415 | expect(await t.svc.token("demo")).toMatch(/^AT-/); |
| 416 | }); |
| 417 | |
| 418 | it("only the exact redirect with the right secret state is taken; anything else is an ordinary request", async () => { |
| 419 | const t = inTab({ approve: false }); |
| 420 | const r = await t.svc.connect("demo"); |
| 421 | const u = new URL(t.opened[0] as string); |
| 422 | const state = u.searchParams.get("state") as string; |
| 423 | expect(t.svc.captureRedirect(`http://127.0.0.1:53682/callback?code=x&state=wrong`)).toBeNull(); |
| 424 | expect(t.svc.captureRedirect(`http://127.0.0.1:53682/other?code=x&state=${state}`)).toBeNull(); |
| 425 | expect(t.svc.captureRedirect(`http://evil.example:53682/callback?code=x&state=${state}`)).toBeNull(); |
| 426 | expect(t.svc.captureRedirect(`http://127.0.0.1:53683/callback?code=x&state=${state}`)).toBeNull(); |
| 427 | expect(t.svc.state()[0]?.connecting).toBe(true); |
| 428 | expect(t.svc.captureRedirect(`http://127.0.0.1:53682/callback?code=ok&state=${state}`)).toMatchObject({ status: 200 }); |
| 429 | await r.done; |
| 430 | expect(t.svc.captureRedirect(`http://127.0.0.1:53682/callback?code=again&state=${state}`)).toBeNull(); // single use |
| 431 | }); |
| 432 | |
| 433 | it("a declined approval, a cancel and a timeout all end the sign-in, close the tab, and say why", async () => { |
| 434 | const declined = inTab({ error: "access_denied" }); |
| 435 | const r = await declined.svc.connect("demo"); |
| 436 | await expect(r.done).rejects.toMatchObject({ code: "denied", message: "sign-in was declined" }); |
| 437 | expect(declined.closed).toEqual([7]); |
| 438 | expect(declined.caught()).toMatchObject({ status: 400 }); |
| 439 | |
| 440 | const cancelled = inTab({ approve: false }); |
| 441 | const c = await cancelled.svc.connect("demo"); |
| 442 | cancelled.svc.cancelConnect("demo"); |
| 443 | await expect(c.done).rejects.toMatchObject({ message: "sign-in was cancelled" }); |
| 444 | expect(cancelled.closed).toEqual([7]); |
| 445 | |
| 446 | const { f, st } = fakeServer(); |
| 447 | const slow = new McpService({ fetch: f, store: memStore(), servers: [SERVER], callerNetworkOn: () => false, announce: () => {}, now: () => st.clock, openExternal: () => {}, openTab: async () => 9, closeTab: async () => {}, timeoutMs: 30 }); |
| 448 | const t = await slow.connect("demo"); |
| 449 | await expect(t.done).rejects.toMatchObject({ code: "timeout" }); |
| 450 | expect(slow.captureRedirect("http://127.0.0.1:53682/callback?code=late&state=x")).toBeNull(); |
| 451 | }); |
| 452 | |
| 453 | it("asks for extra permissions the server does not publish; an older server that refuses them is asked again without", async () => { |
| 454 | for (const knows of [true, false]) { |
| 455 | const opened: string[] = []; |
| 456 | const regs: string[] = []; |
| 457 | let granted = ""; |
| 458 | let svc!: McpService; |
| 459 | const f = fakeFetch((u, init) => { |
| 460 | const meta = META_DOCS(u, init); |
| 461 | if (meta.status !== 404) return meta; |
| 462 | if (u.pathname === "/o/register/") return (regs.push(JSON.parse(init.body ?? "{}").scope), { status: 201, json: { client_id: "cid-" + regs.length } }); |
| 463 | if (u.pathname === "/o/token/") return { json: { access_token: "AT-x", refresh_token: "RT", expires_in: 3600, scope: granted } }; |
| 464 | return { status: 404, json: {} }; |
| 465 | }); |
| 466 | svc = new McpService({ |
| 467 | fetch: f, store: memStore(), servers: [{ ...SERVER, extraScopes: ["files:upload"] }], callerNetworkOn: () => false, announce: () => {}, now: () => 1, |
| 468 | openExternal: () => { throw new Error("no"); }, |
| 469 | openTab: async (url) => { |
| 470 | opened.push(url); |
| 471 | const a = new URL(url); |
| 472 | const want = (a.searchParams.get("scope") ?? "").split(" "); |
| 473 | const back = new URL(a.searchParams.get("redirect_uri") as string); |
| 474 | if (!knows && want.includes("files:upload")) back.searchParams.set("error", "invalid_scope"); |
| 475 | else (back.searchParams.set("code", "C"), (granted = want.join(" "))); |
| 476 | back.searchParams.set("state", a.searchParams.get("state") as string); |
| 477 | queueMicrotask(() => svc.captureRedirect(back.href)); |
| 478 | return opened.length; |
| 479 | }, |
| 480 | closeTab: async () => {}, |
| 481 | }); |
| 482 | await (await svc.connect("demo")).done; |
| 483 | expect(new URL(opened[0] as string).searchParams.get("scope")).toBe("mcp files:upload"); |
| 484 | expect(svc.state()[0]).toMatchObject({ signedIn: true }); |
| 485 | if (knows) { |
| 486 | expect(opened).toHaveLength(1); |
| 487 | expect(regs).toEqual(["mcp files:upload"]); |
| 488 | expect(svc.missingScopes("demo")).toEqual([]); |
| 489 | expect(svc.state()[0]?.missingScopes).toBeUndefined(); |
| 490 | } else { |
| 491 | expect(opened).toHaveLength(2); // asked once more, without the extra |
| 492 | expect(new URL(opened[1] as string).searchParams.get("scope")).toBe("mcp"); |
| 493 | expect(svc.state()[0]?.error).toBeUndefined(); |
| 494 | expect(svc.missingScopes("demo")).toEqual(["files:upload"]); |
| 495 | expect(svc.state()[0]?.missingScopes).toEqual(["files:upload"]); |
| 496 | } |
| 497 | } |
| 498 | }); |
| 499 | |
| 500 | it("a sign-in from before an extra permission was asked for registers again, and keeps working until the new one is done", async () => { |
| 501 | const { f, st } = fakeServer(); |
| 502 | const store = memStore(); |
| 503 | const old = new McpService({ fetch: f, store, servers: [SERVER], callerNetworkOn: () => false, announce: () => {}, now: () => st.clock, openExternal: () => {} }); |
| 504 | await signIn(old); |
| 505 | expect(st.registered).toBe(1); |
| 506 | let svc!: McpService; |
| 507 | svc = new McpService({ |
| 508 | fetch: f, store, servers: [{ ...SERVER, extraScopes: ["files:upload"] }], callerNetworkOn: () => false, announce: () => {}, now: () => st.clock, openExternal: () => {}, |
| 509 | openTab: async () => 3, closeTab: async () => {}, |
| 510 | }); |
| 511 | const r = await svc.connect("demo"); |
| 512 | expect(st.registered).toBe(2); // the permissions changed |
| 513 | expect(await svc.token("demo")).toMatch(/^AT-/); // the old sign-in still works while the new one waits |
| 514 | svc.cancelConnect("demo"); |
| 515 | await expect(r.done).rejects.toBeInstanceOf(OAuthError); |
| 516 | expect(svc.state()[0]?.signedIn).toBe(true); |
| 517 | expect(JSON.parse(store.text as string).servers.demo.clientId).toBe("cid-1"); // and the new client was not kept |
| 518 | }); |
| 519 | |
| 520 | it("nothing is expected when no sign-in is waiting, and scripts can still ask for the address only", async () => { |
| 521 | const t = inTab(); |
| 522 | expect(t.svc.captureRedirect("http://127.0.0.1:53682/callback?code=a&state=b")).toBeNull(); |
| 523 | const r = await t.svc.connect("demo", { open: false }); // "none": the listener, not a tab |
| 524 | expect(t.opened).toHaveLength(0); |
| 525 | expect(new URL(r.authorizeUrl).searchParams.get("redirect_uri")).toMatch(/^http:\/\/127\.0\.0\.1:\d+\/callback$/); |
| 526 | t.svc.cancelConnect("demo"); |
| 527 | await expect(r.done).rejects.toBeInstanceOf(OAuthError); |
| 528 | }); |
| 529 | }); |
| 530 | |
| 531 | describe("where tokens are kept, and which servers there are", () => { |
| 532 | it("writes tokens to disk only when the system can encrypt them, and never as plain text", () => { |
| 533 | const disk = new Map<string, Buffer>(); |
| 534 | const files = { read: (p: string) => disk.get(p) ?? null, write: (p: string, b: Buffer | null) => void (b ? disk.set(p, b) : disk.delete(p)) }; |
| 535 | const enc = { isEncryptionAvailable: () => true, encryptString: (s: string) => Buffer.from(s.split("").reverse().join("")), decryptString: (b: Buffer) => b.toString().split("").reverse().join("") }; |
| 536 | const s = createStore("/x", enc, files); |
| 537 | s.save("secret-token"); |
| 538 | expect(disk.get("/x")!.toString()).not.toContain("secret-token"); |
| 539 | expect(s.load()).toBe("secret-token"); |
| 540 | s.save(null); |
| 541 | expect(disk.size).toBe(0); |
| 542 | const mem = createStore("/y", { ...enc, isEncryptionAvailable: () => false }, files); |
| 543 | mem.save("kept in memory"); |
| 544 | expect(mem.persistent).toBe(false); |
| 545 | expect(mem.load()).toBe("kept in memory"); |
| 546 | expect(disk.size).toBe(0); |
| 547 | }); |
| 548 | |
| 549 | it("never touches the keychain until it must: no file means nothing is decrypted, and the service reads nothing at start-up", () => { |
| 550 | const calls: string[] = []; |
| 551 | const enc = { isEncryptionAvailable: () => (calls.push("available"), true), encryptString: (s: string) => (calls.push("encrypt"), Buffer.from(s)), decryptString: (b: Buffer) => (calls.push("decrypt"), b.toString()) }; |
| 552 | const disk = new Map<string, Buffer>(); |
| 553 | const files = { read: (p: string) => disk.get(p) ?? null, write: (p: string, b: Buffer | null) => void (b ? disk.set(p, b) : disk.delete(p)) }; |
| 554 | const store = createStore("/x", enc, files); |
| 555 | expect(store.load()).toBeNull(); // no file yet |
| 556 | expect(calls).toEqual([]); |
| 557 | const { f } = fakeServer(); |
| 558 | const svc = new McpService({ fetch: f, store, servers: [SERVER], openExternal: () => {}, callerNetworkOn: () => false, announce: () => {} }); |
| 559 | expect(calls).toEqual([]); // constructing the service reads nothing |
| 560 | expect(svc.state()[0]).toMatchObject({ signedIn: false, persistent: true }); |
| 561 | expect(calls).toEqual([]); // and neither does showing the state when nobody is signed in |
| 562 | store.save("secret"); // a first sign-in is where it reaches the keychain |
| 563 | expect(calls).toEqual(["available", "encrypt"]); |
| 564 | expect(store.load()).toBe("secret"); |
| 565 | expect(calls).toEqual(["available", "encrypt", "decrypt"]); |
| 566 | }); |
| 567 | |
| 568 | it("reads extra servers from a file, skips bad entries and says why, and lets an entry replace the built-in one", () => { |
| 569 | const good = { id: "notes", name: "Notes", url: "https://notes.example/mcp", tools: ["list", "add"], readOnly: ["list"] }; |
| 570 | const r = parseServerConfigs(JSON.stringify([good, { id: "Bad Id", name: "x", url: "https://a.example" }, { ...good, id: "plain", url: "http://notes.example/mcp" }, { ...good, id: "ro", readOnly: ["nope"] }, { ...good, id: "empty", tools: [] }])); |
| 571 | expect(r.servers.map((s) => s.id)).toEqual(["notes"]); |
| 572 | expect(r.problems.length).toBe(4); |
| 573 | expect(parseServerConfigs("not json").problems[0]).toMatch(/not valid JSON/); |
| 574 | expect(parseServerConfigs(JSON.stringify([{ ...good, url: "http://127.0.0.1:9/mcp" }])).servers.length).toBe(1); |
| 575 | expect(parseServerConfigs(JSON.stringify([{ ...good, extraScopes: ["files:upload"] }])).servers[0]?.extraScopes).toEqual(["files:upload"]); |
| 576 | expect(parseServerConfigs(JSON.stringify([{ ...good, extraScopes: ["two words"] }])).servers).toEqual([]); |
| 577 | const merged = loadServerConfigs("/f", () => JSON.stringify([{ ...good, id: "askrobots", name: "Mine" }])).servers; |
| 578 | expect(merged.find((s) => s.id === "askrobots")?.name).toBe("Mine"); |
| 579 | expect(merged.find((s) => s.id === "askrobots")?.extraScopes).toEqual(["files:upload", "history"]); // a built-in permission is kept |
| 580 | expect(loadServerConfigs("/f", () => JSON.stringify([{ ...good, id: "askrobots", extraScopes: [] }])).servers.find((s) => s.id === "askrobots")?.extraScopes).toEqual([]); // unless the entry says otherwise |
| 581 | expect(loadServerConfigs(undefined).servers).toEqual(DEFAULT_SERVERS); |
| 582 | expect(loadServerConfigs("/missing", () => { throw new Error("ENOENT"); }).servers).toEqual(DEFAULT_SERVERS); |
| 583 | }); |
| 584 | |
| 585 | it("says what a write sends, in words, one argument per line", () => { |
| 586 | expect(writeSummary("askrobots.com", "create_task", { title: "Visit the moon terrain", instructions: "Around Nov 2027", urgency: "low" })).toBe( |
| 587 | "Add a task on askrobots.com:\ntitle: Visit the moon terrain\ninstructions: Around Nov 2027\nurgency: low", |
| 588 | ); |
| 589 | expect(writeSummary("askrobots.com", "create_note", { content: "x", is_public: false })).toBe("Add a note on askrobots.com:\ncontent: x\nis_public: no"); |
| 590 | expect(writeSummary("askrobots.com", "stop_timer", {})).toBe("Stop the timer on askrobots.com"); |
| 591 | expect(writeSummary("X", "frob", { a: { b: 1 }, skip: "", n: null })).toBe('Ask X to frob:\na: {"b":1}'); |
| 592 | expect(writeSummary("X", "frob", { long: "y".repeat(5000) }).length).toBeLessThan(1300); |
| 593 | }); |
| 594 | |
| 595 | it("writes that only add are asked about but not marked risky; a personal entry keeps that list", async () => { |
| 596 | const a = DEFAULT_SERVERS.find((s) => s.id === "askrobots")!; |
| 597 | expect(a.addsOnly).not.toContain("update_task"); // changes what exists: risky |
| 598 | expect(a.addsOnly!.every((t) => a.tools.includes(t) && !a.readOnly.includes(t))).toBe(true); |
| 599 | const mine = loadServerConfigs("/f", () => JSON.stringify([{ id: "askrobots", name: "mine", url: "https://askrobots.com/api/mcp/", tools: ["list_tasks", "create_task", "update_task"], readOnly: ["list_tasks"] }])).servers[0]!; |
| 600 | expect(mine.addsOnly).toEqual(["create_task"]); // only the ones this entry allows |
| 601 | }); |
| 602 | |
| 603 | it("the built-in askrobots entry reads, saves links as private, names no one's project, and drops contact notes", () => { |
| 604 | const a = DEFAULT_SERVERS.find((s) => s.id === "askrobots")!; |
| 605 | expect(a.url).toBe("https://askrobots.com/api/mcp/"); |
| 606 | expect(a.readOnly.every((t) => a.tools.includes(t))).toBe(true); |
| 607 | // the only writes the assistant gets, each asked about every time |
| 608 | expect(a.tools.filter((t) => !a.readOnly.includes(t))).toEqual(["create_link", "create_task", "add_task_comment", "update_task", "create_note", "create_article", "create_contact", "start_timer", "stop_timer", "search_history"]); |
| 609 | for (const never of ["send_email", "search_messages", "update_article", "complete_task", "cancel_task", "submit_task", "post_to_feed", "generate_image", "run_function", "execute_task"]) expect(a.tools).not.toContain(never); |
| 610 | expect(a.tools.some((t) => /^delete_|finance|^ads_|invoice|journal/.test(t))).toBe(false); |
| 611 | expect(a.fixedArgs?.create_note).toEqual({ is_public: false }); |
| 612 | expect(a.fixedArgs?.create_article).toEqual({ is_public: false }); |
| 613 | expect(a.fixedArgs?.upload_file).toEqual({ is_public: false, category: "browser/kept" }); |
| 614 | expect(a.fixedArgs?.create_link).toEqual({ is_public: false }); |
| 615 | expect(JSON.stringify(DEFAULT_SERVERS)).not.toMatch(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/); // no personal ids in the shipped defaults |
| 616 | expect(a.dropFields?.get_contact).toEqual(["notes", "interactions"]); |
| 617 | expect(a.extraScopes).toEqual(["files:upload", "history"]); // saving kept pages in Files, and history, are their own permissions |
| 618 | expect(a.browserOnly).toEqual(["upload_file", "add_history", "list_history", "forget_history", "history_settings"]); // the assistant never writes or forgets history |
| 619 | expect(a.readOnly).not.toContain("search_history"); // searching history asks the person every time |
| 620 | }); |
| 621 | }); |