Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220import 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 tool choices are atomic, durable and filter current model definitions", { timeout: 60000 }, async () => { 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-tools-")); let changed = false; 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 (!("id" in body)) return new Response(null, { status: 202 }); const results = { initialize: { protocolVersion: "2025-06-18", capabilities: { tools: {} }, serverInfo: { name: "Health", version: "1" }, }, "tools/list": { tools: [ { name: "search", inputSchema: { type: "object", properties: { query: { type: changed ? "number" : "string" }, }, }, }, { name: "write", annotations: { readOnlyHint: false }, inputSchema: { type: "object" }, }, ...(changed ? [{ name: "new_write", inputSchema: { type: "object" } }] : []), ], }, ping: {}, }; return Response.json({ jsonrpc: "2.0", id: body.id, ...(results[body.method] ? { result: results[body.method] } : { error: { code: -32601, message: "Method not found" } }), }); }, }), resourcePersistencePath: persistence, }; let worker = new Miniflare(options); const raw = async (action, args = {}) => ( await worker.dispatchFetch("https://fixture.example", { method: "POST", body: JSON.stringify({ action, ...args }), }) ).json(); const call = async (action, args) => { const result = await raw(action, args); assert.equal(result.ok, true, JSON.stringify(result)); return result.value; }; const ready = async () => { for (let i = 0; i < 300; i++) { const item = (await call("list")).connections[0]; if (item?.state === "ready") return item; await new Promise((resolve) => setTimeout(resolve, 20)); } assert.fail("MCP did not become ready"); }; const selection = (item, enabled, tools = item.capabilities) => ({ id: item.id, value: { settingsRevision: item.settingsRevision, enabled, tools: tools.map(({ id, fingerprint }) => ({ id, fingerprint })), }, }); const names = async () => (await call("definitions")).map((tool) => tool.name).sort(); try { await call("add", { value: { name: "Tool choices", endpoint: "https://tools.example.com/mcp", authMode: "none", }, }); let item = await ready(); assert.deepEqual(await names(), []); const revision = item.revision; const before = item; item = await call("tools", selection(item, true)); assert.equal( item.revision, revision, "choices do not invalidate native transport credentials", ); assert.deepEqual(await names(), ["search", "write"]); assert.equal( (await raw("tools", selection(before, false))).error, "mcp_tools_conflict", ); item = await call( "tools", selection( item, false, item.capabilities.filter((tool) => tool.name === "write"), ), ); assert.deepEqual(await names(), ["search"]); item = await call("connect", item); assert.deepEqual( await names(), [], "connecting state does not expose cached definitions", ); item = await ready(); assert.deepEqual(await names(), ["search"]); const oldDefinition = selection(item, true); changed = true; await call("connect", item); item = await ready(); assert.deepEqual( await names(), [], "changed and newly discovered definitions require a new choice", ); assert.equal( (await raw("tools", oldDefinition)).error, "mcp_tools_conflict", ); item = await call("tools", selection(item, true)); assert.deepEqual(await names(), ["new_write", "search", "write"]); const writeTool = item.capabilities.find((tool) => tool.name === "write"); item = await call("tools", { id: item.id, value: { settingsRevision: item.settingsRevision, tools: [{ id: writeTool.id, fingerprint: writeTool.fingerprint }], approval: "never", }, }); assert.equal( item.capabilities.find((tool) => tool.name === "write").settings .enabled, true, ); assert.deepEqual(await names(), ["new_write", "search"]); item = await call("tools", { id: item.id, value: { settingsRevision: item.settingsRevision, tools: [{ id: writeTool.id, fingerprint: writeTool.fingerprint }], approval: null, }, }); assert.deepEqual(await names(), ["new_write", "search", "write"]); await worker.dispose(); worker = new Miniflare(options); item = await ready(); assert.deepEqual( await names(), ["new_write", "search", "write"], "unchanged choices survive full native restoration", ); item = await call("enable", { ...item, value: false }); assert.deepEqual(await names(), []); item = await call("tools", selection(item, false)); await call("enable", { ...item, value: true }); item = await ready(); assert.deepEqual( await names(), [], "bulk disable persists while server is disabled", ); const forged = selection(item, true); forged.value.tools[0].id = "0".repeat(64); assert.equal((await raw("tools", forged)).error, "mcp_tools_conflict"); assert.deepEqual(await names(), []); await call("remove", item); assert.deepEqual(await names(), []); } finally { await worker.dispose(); await rm(persistence, { recursive: true, force: true }); } },);