Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302import assert from "node:assert/strict";import { test } from "node:test";import { mkdtemp, rm } from "node:fs/promises";import { tmpdir } from "node:os";import { join } from "node:path";import { build } from "esbuild";import { Miniflare, convertV4MiniflareOptions } from "miniflare";
test( "native MCP diagnostics describe accepted outcomes without payloads or healthy poll noise", { timeout: 30000 }, async (t) => { const bundle = await build({ entryPoints: ["tests/fixtures/mcp-runtime-worker.ts"], alias: { path: "node:path" }, bundle: true, write: false, format: "esm", platform: "neutral", conditions: ["workerd", "worker", "browser"], mainFields: ["module", "main"], external: ["cloudflare:*", "node:*"], }); const persistence = await mkdtemp( join(tmpdir(), "flarebot-mcp-diagnostics-"), ); let worker, release, entered, gate, failConnection = false, failDiscovery = false; let pings = 0; t.after( async () => { release?.(); await worker?.dispose(); await rm(persistence, { recursive: true, force: true }); }, { timeout: 10000 }, ); const hold = () => { const waiting = new Promise((resolve) => { entered = resolve; }); gate = new Promise((resolve) => { release = resolve; }); return waiting; }; const options = { ...convertV4MiniflareOptions({ modules: true, script: bundle.outputFiles[0].text, compatibilityDate: "2026-09-04", compatibilityFlags: ["nodejs_compat"], durableObjects: { MODEL: { className: "McpRuntimeFixture", useSQLite: true }, }, async outboundService(request) { if (request.method !== "POST") return new Response(null, { status: 405 }); const body = await request.json(); if (failConnection && body.method === "initialize") return new Response(null, { status: 503 }); if (body.method === "initialize" && gate) { entered(); await gate; } if (body.method === "ping") pings++; if (!("id" in body)) return new Response(null, { status: 202 }); if (failDiscovery && body.method === "tools/list") return Response.json({ jsonrpc: "2.0", id: body.id, error: { code: -32603, message: "SECRET_PROVIDER_PAYLOAD https://credentials.example?token=secret", }, }); const result = body.method === "initialize" ? { protocolVersion: "2025-06-18", capabilities: { tools: {} }, serverInfo: { name: "Provider raw name", version: "1" }, } : body.method === "tools/list" ? { tools: [ { name: "search", inputSchema: { type: "object" } }, ], } : {}; return Response.json({ jsonrpc: "2.0", id: body.id, result }); }, }), resourcePersistencePath: persistence, }; worker = new Miniflare(options); const unknown = await worker.dispatchFetch("http://fixture", { method: "POST", body: JSON.stringify({ action: "unknown" }), signal: t.signal, }); assert.equal(unknown.status, 400); assert.deepEqual(await unknown.json(), { ok: false, error: "unknown_action", }); const get = async (path) => { const response = await worker.dispatchFetch(`http://fixture${path}`, { signal: t.signal, }); assert.equal(response.status, 200); return response.json(); }; const call = async (action, args = {}) => { const response = await worker.dispatchFetch("http://fixture", { method: "POST", body: JSON.stringify({ action, ...args }), signal: t.signal, }); const result = await response.json(); assert.equal(result.ok, true, JSON.stringify(result)); return result.value; }; const until = async (read, predicate) => { for (let index = 0; index < 100; index++) { t.signal.throwIfAborted(); const value = await read(); if (predicate(value)) return value; await new Promise((resolve) => setTimeout(resolve, 20)); } assert.fail("Native diagnostic outcome did not settle"); }; const events = () => get("/__extensions"); const settled = (id) => until( async () => (await call("list")).connections.find((row) => row.id === id), (row) => row.state !== "connecting", ); const firstEntered = hold(); const started = Date.now(); let item = await call("add", { value: { name: "Private server label", endpoint: "https://mcp.example.com/private-endpoint", authMode: "none", }, }); await firstEntered; assert.deepEqual( await events(), [], "Acknowledgement is not a completed connection", ); await new Promise((resolve) => setTimeout(resolve, 60)); release(); gate = undefined; item = await settled(item.id); assert.equal(item.state, "ready"); let records = await events(); assert.equal(records.length, 1); assert.deepEqual(Object.keys(records[0]).sort(), [ "operation", "source", "startedAt", "status", ]); assert.equal(records[0].operation, "mcp-connect"); assert.equal(records[0].status, "completed"); assert.deepEqual(records[0].source, { kind: "mcp", id: item.id }); assert.ok( records[0].startedAt >= started && records[0].startedAt <= Date.now() - 50, );
await worker.dispatchFetch(`http://fixture/__age?id=${item.id}`); await call("maintain"); assert.equal(pings, 1); assert.deepEqual( await events(), records, "A successful maintenance ping adds no event", ); failDiscovery = true; item = await call("refresh", item); item = await settled(item.id); assert.equal(item.lastError, "discovery_failed"); records = await events(); assert.equal(records.at(-1).operation, "mcp-refresh"); assert.equal(records.at(-1).status, "error"); assert.equal(records.at(-1).failure, "discovery"); assert.doesNotMatch( JSON.stringify(records), /SECRET_PROVIDER|credentials\.example|private=query|Private server label|Provider raw name|search/, );
failDiscovery = false; const staleEntered = hold(); const beforeStale = records.length; item = await call("refresh", item); await staleEntered; item = await call("enable", { ...item, value: false }); release(); gate = undefined; await until( () => call("list"), (value) => value.native.length === 0, ); records = await until(events, (value) => value.length > beforeStale); assert.deepEqual( records .slice(beforeStale) .map(({ operation, status }) => ({ operation, status })), [{ operation: "mcp-disconnect", status: "completed" }], "A superseded native refresh must not claim a current outcome", );
await call("diagnostic-fault", { value: true }); item = await call("enable", { ...item, value: true }); item = await settled(item.id); assert.equal( item.state, "ready", "A throwing diagnostics sink cannot break native connection", ); assert.deepEqual(await events(), records); await worker.dispose(); worker = new Miniflare(options); item = await settled(item.id); assert.equal(item.state, "ready"); records = await events(); assert.equal(records.at(-1).operation, "mcp-connect"); assert.equal(records.at(-1).status, "completed"); failDiscovery = true; await worker.dispose(); worker = new Miniflare(options); item = await settled(item.id); assert.equal(item.state, "error"); assert.equal( item.lastError, "connection_failed", "Restoration retains its existing recovery classification", ); records = await events(); assert.equal(records.at(-1).operation, "mcp-connect"); assert.equal(records.at(-1).status, "error"); assert.equal( records.at(-1).failure, "discovery", "Restoration and explicit refresh classify the same native protocol failure alike", ); assert.ok( item.health.retryAt, "The diagnostic stage does not change recovery admission", ); failDiscovery = false; failConnection = true; await worker.dispose(); worker = new Miniflare(options); item = await settled(item.id); assert.equal(item.state, "error"); records = await events(); assert.equal( records.at(-1).failure, "connection", "Failure to restore a native transport remains a connection failure", ); failConnection = false; item = await call("refresh", item); item = await settled(item.id); assert.equal(item.state, "ready"); const auth = await call("add", { value: { name: "OAuth", endpoint: "https://oauth.example.com/mcp", authMode: "oauth", }, }); await settled(auth.id); records = await events(); assert.equal(records.at(-1).failure, "authentication"); assert.equal(records.at(-1).status, "error"); const authEntered = hold(); const authCurrent = await settled(auth.id); const beforeAuth = records.length; await call("authorize-start", authCurrent); await authEntered; await call("authorize-cancel"); records = await until(events, (value) => value.length > beforeAuth); assert.equal(records.at(-1).operation, "mcp-auth"); assert.equal(records.at(-1).status, "aborted"); assert.equal(records.at(-1).failure, "cancelled"); release(); gate = undefined; },);