|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { cloudflareMcpActionHandlers, credentialValidators } from "./executors.ts"; |
| 3 | + |
| 4 | +describe("Cloudflare MCP executors", () => { |
| 5 | + it("sends bearer auth and maps search to the official MCP tool", async () => { |
| 6 | + const calls: Array<Record<string, unknown>> = []; |
| 7 | + const fetcher = createMcpFetch(calls, "cf-token"); |
| 8 | + |
| 9 | + const output = await cloudflareMcpActionHandlers.search( |
| 10 | + { code: "async () => ['workers']" }, |
| 11 | + { accessToken: "cf-token", fetcher }, |
| 12 | + ); |
| 13 | + |
| 14 | + expect(output).toEqual(["workers"]); |
| 15 | + expect(calls.find((call) => call.method === "tools/call")?.params).toEqual({ |
| 16 | + name: "search", |
| 17 | + arguments: { code: "async () => ['workers']" }, |
| 18 | + }); |
| 19 | + expect(fetcher).toHaveBeenCalled(); |
| 20 | + }); |
| 21 | + |
| 22 | + it("validates both API-token and OAuth bearer credentials with tools/list", async () => { |
| 23 | + const apiKeyResult = await credentialValidators.apiKey!( |
| 24 | + { apiKey: "api-token", values: {} }, |
| 25 | + { fetcher: createMcpFetch([], "api-token") }, |
| 26 | + ); |
| 27 | + const oauthResult = await credentialValidators.oauth2!( |
| 28 | + { |
| 29 | + authType: "oauth2", |
| 30 | + accessToken: "oauth-token", |
| 31 | + tokenType: "Bearer", |
| 32 | + profile: { accountId: "pending", displayName: "Pending", grantedScopes: [] }, |
| 33 | + metadata: {}, |
| 34 | + }, |
| 35 | + { fetcher: createMcpFetch([], "oauth-token") }, |
| 36 | + ); |
| 37 | + |
| 38 | + expect(apiKeyResult?.metadata?.mcpTools).toEqual(["docs", "execute", "search"]); |
| 39 | + expect(oauthResult?.metadata?.mcpTools).toEqual(["docs", "execute", "search"]); |
| 40 | + }); |
| 41 | + |
| 42 | + it("does not start MCP traffic after the execution is cancelled", async () => { |
| 43 | + const controller = new AbortController(); |
| 44 | + controller.abort(); |
| 45 | + const fetcher = createMcpFetch([], "cf-token"); |
| 46 | + |
| 47 | + await expect( |
| 48 | + cloudflareMcpActionHandlers.search( |
| 49 | + { code: "async () => ['workers']" }, |
| 50 | + { accessToken: "cf-token", fetcher, signal: controller.signal }, |
| 51 | + ), |
| 52 | + ).rejects.toThrow(); |
| 53 | + expect(fetcher).not.toHaveBeenCalled(); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +function createMcpFetch(calls: Array<Record<string, unknown>>, expectedToken: string): typeof fetch { |
| 58 | + return vi.fn(async (_input: RequestInfo | URL, init?: RequestInit): Promise<Response> => { |
| 59 | + const headers = new Headers(init?.headers); |
| 60 | + const rawBody = typeof init?.body === "string" ? init.body : undefined; |
| 61 | + const request = rawBody ? (JSON.parse(rawBody) as Record<string, unknown>) : {}; |
| 62 | + calls.push(request); |
| 63 | + |
| 64 | + expect(headers.get("authorization")).toBe(`Bearer ${expectedToken}`); |
| 65 | + if (init?.method === "DELETE") return new Response(null, { status: 200 }); |
| 66 | + if (!("id" in request)) return new Response(null, { status: 202 }); |
| 67 | + |
| 68 | + const method = request.method; |
| 69 | + const result = |
| 70 | + method === "initialize" |
| 71 | + ? { |
| 72 | + protocolVersion: "2025-03-26", |
| 73 | + capabilities: {}, |
| 74 | + serverInfo: { name: "cloudflare-api", version: "1.0.0" }, |
| 75 | + } |
| 76 | + : method === "tools/list" |
| 77 | + ? { |
| 78 | + tools: ["docs", "execute", "search"].map((name) => ({ |
| 79 | + name, |
| 80 | + inputSchema: { type: "object" }, |
| 81 | + })), |
| 82 | + } |
| 83 | + : { content: [{ type: "text", text: '["workers"]' }] }; |
| 84 | + |
| 85 | + return new Response(JSON.stringify({ jsonrpc: "2.0", id: request.id, result }), { |
| 86 | + headers: { |
| 87 | + "content-type": "application/json", |
| 88 | + "mcp-session-id": "test-session", |
| 89 | + }, |
| 90 | + }); |
| 91 | + }) as typeof fetch; |
| 92 | +} |
0 commit comments