|
| 1 | +// Cloud-only (billing): every code execution run through the MCP session |
| 2 | +// Durable Object is metered to Autumn, exactly like the HTTP executor plane. |
| 3 | +// The MCP server is the PRIMARY execution surface (Claude/Cursor run code here, |
| 4 | +// not over /api), so if the DO doesn't bill, the bulk of real usage silently |
| 5 | +// never reaches the meter — the regression this pins. |
| 6 | +// |
| 7 | +// Black-box and end-to-end: drive a real @modelcontextprotocol/sdk client over |
| 8 | +// StreamableHTTP (the exact transport an MCP client uses) against the production |
| 9 | +// workerd + McpSessionDO topology, then read the usage the server ACTUALLY |
| 10 | +// tracked from the Autumn ledger. The execution's own response can't prove it |
| 11 | +// was billed — metering is fire-and-forget, decoupled from the user-facing |
| 12 | +// result — so only the meter is the source of truth. |
| 13 | +import { expect } from "@effect/vitest"; |
| 14 | +import { Effect } from "effect"; |
| 15 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 16 | +import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; |
| 17 | + |
| 18 | +import { scenario } from "../src/scenario"; |
| 19 | +import { Autumn, Billing, Mcp, Target } from "../src/services"; |
| 20 | +import type { Identity } from "../src/target"; |
| 21 | + |
| 22 | +const emailOf = (identity: Identity): string => identity.credentials?.email ?? identity.label; |
| 23 | + |
| 24 | +/** The org the bearer is scoped to — the Autumn customer id `trackExecution` |
| 25 | + * meters against — read from the JWT's public claims. */ |
| 26 | +const orgIdOf = (bearer: string): string => { |
| 27 | + const claims = JSON.parse(Buffer.from(bearer.split(".")[1] ?? "", "base64url").toString()) as { |
| 28 | + readonly org_id?: string; |
| 29 | + }; |
| 30 | + if (!claims.org_id) throw new Error("orgIdOf: bearer carries no org_id claim"); |
| 31 | + return claims.org_id; |
| 32 | +}; |
| 33 | + |
| 34 | +const textOf = (result: { content?: unknown; toolResult?: unknown }): string => |
| 35 | + ((result.content ?? []) as Array<{ type: string; text?: string }>) |
| 36 | + .filter((part) => part.type === "text") |
| 37 | + .map((part) => part.text) |
| 38 | + .join("\n"); |
| 39 | + |
| 40 | +const RUNS = 3; |
| 41 | + |
| 42 | +scenario( |
| 43 | + "Billing · every MCP execution is metered to Autumn, one unit per run", |
| 44 | + { timeout: 180_000 }, |
| 45 | + Effect.gen(function* () { |
| 46 | + // Gates: billing is enforced here AND the Autumn ledger is observable |
| 47 | + // (the suite booted the emulator). Yield before any work so a target |
| 48 | + // missing either capability skips cleanly. |
| 49 | + yield* Billing; |
| 50 | + const autumn = yield* Autumn; |
| 51 | + const target = yield* Target; |
| 52 | + const mcp = yield* Mcp; |
| 53 | + |
| 54 | + const identity = yield* target.newIdentity(); |
| 55 | + const bearer = yield* mcp.mintBearer(emailOf(identity)); |
| 56 | + const customerId = orgIdOf(bearer); |
| 57 | + |
| 58 | + // A fresh org has metered nothing yet — the baseline the run is measured |
| 59 | + // against, and a guard that we're reading this customer's ledger in |
| 60 | + // isolation from every other scenario's executions. |
| 61 | + const before = yield* autumn.usageEvents({ customerId, featureId: "executions" }); |
| 62 | + expect(before.length, "a brand-new org starts with zero metered executions").toBe(0); |
| 63 | + |
| 64 | + // A real MCP client over StreamableHTTP — the production code path. |
| 65 | + const client = new Client( |
| 66 | + { name: "executor-e2e-metering", version: "0.0.1" }, |
| 67 | + { capabilities: {} }, |
| 68 | + ); |
| 69 | + const transport = new StreamableHTTPClientTransport(new URL(target.mcpUrl), { |
| 70 | + requestInit: { headers: { authorization: `Bearer ${bearer}` } }, |
| 71 | + }); |
| 72 | + const connected = yield* Effect.promise(() => client.connect(transport).then(() => client)); |
| 73 | + |
| 74 | + yield* Effect.gen(function* () { |
| 75 | + // Run a handful of executions; each is one billable unit. |
| 76 | + for (let i = 1; i <= RUNS; i++) { |
| 77 | + const result = yield* Effect.promise(() => |
| 78 | + connected.callTool({ name: "execute", arguments: { code: `return ${i} * 2;` } }), |
| 79 | + ); |
| 80 | + expect(result.isError, `execution ${i} succeeds`).not.toBe(true); |
| 81 | + expect(textOf(result), `execution ${i} returns its value`).toContain(String(i * 2)); |
| 82 | + } |
| 83 | + |
| 84 | + // The meter is the source of truth. Tracking is fire-and-forget, so poll |
| 85 | + // the ledger until all runs have landed. |
| 86 | + const after = yield* autumn.expectUsage({ |
| 87 | + customerId, |
| 88 | + featureId: "executions", |
| 89 | + count: RUNS, |
| 90 | + }); |
| 91 | + |
| 92 | + expect( |
| 93 | + after.length, |
| 94 | + "exactly one 'executions' usage event per run — no over- or under-counting", |
| 95 | + ).toBe(RUNS); |
| 96 | + expect( |
| 97 | + after.every((event) => event.value === 1), |
| 98 | + "each run meters a single unit", |
| 99 | + ).toBe(true); |
| 100 | + }).pipe(Effect.ensuring(Effect.promise(() => connected.close().catch(() => undefined)))); |
| 101 | + }), |
| 102 | +); |
0 commit comments