import { expect } from "@effect/vitest"; import { Clock, Effect, Redacted, Schema } from "effect"; import { HttpClient, HttpClientRequest } from "effect/http"; import { randomBytes, randomUUID } from "node:crypto"; import { Api, body, RequestFailed } from "./api.ts"; import { Actors } from "./actors.ts"; import { connectionsPath, requirementPath } from "./app-accounts.ts"; import { App, Resource } from "./contracts.ts"; import { Target, withCause } from "./platform.ts"; import { CallFailed, parseJson, readResponse } from "./request-failure.ts"; import { removeAppWhenStopped, settleWebhookSubscriptions } from "./webhook-sources.ts"; import { appsManifest } from "./apps-release.ts"; import { heldValue, reveal } from "./failure-text.ts"; const whsec = () => `whsec_${randomBytes(32).toString("base64")}`; export const secrets = [whsec(), whsec()] as const; export const receiverFiles = [ { path: "index.ts", content: `import { defineApp, defineProvider, secrets, object, string, number, query, mutation, router, type MutationContext, type QueryContext, type Webhook, type WebhookContext } from "apps"; const service = defineProvider({ name: "Event receiver", auth: { key: secrets({ label: "Key", fields: object({ token: string() }) }) } }); const requirements = { user: { accounts: { service } } }; const empty = object({}); const SECRETS = ${JSON.stringify(secrets)}; const decode = (text) => Uint8Array.from(atob(text), (c) => c.charCodeAt(0)); const encode = (bytes) => btoa(String.fromCharCode(...new Uint8Array(bytes))); /** Standard Webhooks: v1,..")>. */ const expected = async (secret, id, timestamp, text) => { const key = await crypto.subtle.importKey("raw", decode(secret.slice(6)), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]); return "v1," + encode(await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(id + "." + timestamp + "." + text))); }; const inbox = { account: { user: "service" }, config: empty, state: empty, async register() { return {}; }, async unregister() {}, async handle(ctx, { request }) { const id = request.headers.get("webhook-id") ?? ""; const timestamp = request.headers.get("webhook-timestamp") ?? ""; const presented = (request.headers.get("webhook-signature") ?? "").split(" "); const text = await request.text(); const signedBy = []; for (const [index, secret] of SECRETS.entries()) if (presented.includes(await expected(secret, id, timestamp, text))) signedBy.push(index); const variant = new URL(request.url).searchParams.get("variant") ?? ""; const message = JSON.parse(text); const kind = message.type === "verification" ? "verification" : "event"; let status = 200; if (kind === "event") { const next = ctx.sql.exec("SELECT seq, status FROM responses ORDER BY seq LIMIT 1").toArray()[0]; if (next !== undefined) { status = next.status; ctx.sql.exec("DELETE FROM responses WHERE seq = ?", next.seq); } } ctx.sql.exec( "INSERT INTO deliveries (kind, variant, webhook_id, timestamp, subscription, signed_by, body, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", kind, variant, id, timestamp, request.headers.get("x-mcp-subscription-id") ?? "", signedBy.join(","), text, status, ); if (kind === "verification") return Response.json({ challenge: variant === "refuse" ? "not the challenge" : message.challenge }); return new Response(null, { status }); }, } satisfies Webhook, typeof empty, typeof empty>; /** Answer the next event deliveries with these statuses, in order. */ const respond = mutation({ input: object({ statuses: object({ list: string() }) }) }, async (ctx: MutationContext, { statuses }) => { for (const status of statuses.list.split(",").filter(Boolean)) ctx.sql.exec("INSERT INTO responses (status) VALUES (?)", Number(status)); return "ok"; }); const deliveries = query({ input: empty }, async (ctx: QueryContext) => ctx.sql.exec("SELECT kind, variant, webhook_id AS id, timestamp, subscription, signed_by AS signedBy, body, status FROM deliveries ORDER BY seq").toArray()); export default defineApp(requirements, { tools: router({ respond, deliveries }), webhooks: { inbox } }); `, }, { path: "migrations/0001_deliveries.sql", content: `CREATE TABLE deliveries (seq INTEGER PRIMARY KEY AUTOINCREMENT, kind TEXT NOT NULL, variant TEXT NOT NULL, webhook_id TEXT NOT NULL, timestamp TEXT NOT NULL, subscription TEXT NOT NULL, signed_by TEXT NOT NULL, body TEXT NOT NULL, status INTEGER NOT NULL); CREATE TABLE responses (seq INTEGER PRIMARY KEY AUTOINCREMENT, status INTEGER NOT NULL); `, }, appsManifest, ]; export const emitterFiles = [ { path: "index.ts", content: `import { defineApp, event, object, string, number, mutation, router, type MutationContext } from "apps"; const issueOpened = event({ description: "An issue was opened in a repository.", filters: { repo: string({ minLength: 3 }) }, payload: object({ title: string(), number: number() }), }); const requirements = { events: { "issue.opened": issueOpened } }; const open = mutation( { input: object({ repo: string(), title: string(), number: number(), id: string() }) }, async (ctx: MutationContext, input) => { ctx.events.emit("issue.opened", { title: input.title, number: input.number }, { filters: { repo: input.repo }, id: input.id, }); return "emitted"; }, ); /** An event that is not declared fails the call, and nothing it emitted before is kept. */ const broken = mutation({ input: object({ id: string() }) }, async (ctx: MutationContext, { id }) => { ctx.events.emit("issue.opened", { title: "partial", number: 0 }, { filters: { repo: "acme/widgets" }, id }); (ctx.events.emit as (name: string, data: unknown) => void)("issue.closed", {}); return "unreachable"; }); /** Events emitted in a transaction that rolls back are discarded with its writes. */ const rolledBack = mutation({ input: object({ id: string() }) }, async (ctx: MutationContext, { id }) => { try { ctx.sql.transaction((tx) => { tx.exec("INSERT INTO opened (id) VALUES (?)", id); ctx.events.emit("issue.opened", { title: "rolled back", number: 0 }, { filters: { repo: "acme/widgets" }, id }); throw new Error("Roll back"); }); } catch {} return ctx.sql.exec("SELECT count(*) AS count FROM opened WHERE id = ?", id).toArray()[0]; }); export default defineApp(requirements, { tools: router({ open, broken, rolledBack }) }); `, }, { path: "migrations/0001_opened.sql", content: "CREATE TABLE opened (id TEXT PRIMARY KEY NOT NULL);\n", }, appsManifest, ]; export const Delivery = Schema.Struct({ kind: Schema.Literals(["verification", "event"]), variant: Schema.String, id: Schema.String, timestamp: Schema.String, subscription: Schema.String, signedBy: Schema.String, body: Schema.String, status: Schema.Number, }); export const Occurrence = Schema.fromJsonString( Schema.Struct({ eventId: Schema.String, name: Schema.String, timestamp: Schema.String, data: Schema.Struct({ title: Schema.String, number: Schema.Number }), cursor: Schema.Null, }), ); export const appEvent = "executor.app_event"; export const Selected = Schema.fromJsonString( Schema.Struct({ eventId: Schema.String, name: Schema.Literal(appEvent), timestamp: Schema.String, data: Schema.Struct({ app: Schema.String, event: Schema.String, data: Schema.Struct({ title: Schema.String, number: Schema.Number }), }), cursor: Schema.Null, }), ); export const RpcResult = Schema.Struct({ result: Schema.Record(Schema.String, Schema.Unknown) }); export const RpcError = Schema.Struct({ error: Schema.Struct({ code: Schema.Number, message: Schema.String, data: Schema.optional(Schema.Unknown), }), }); export const EventList = Schema.Struct({ events: Schema.Array( Schema.Struct({ name: Schema.String, description: Schema.String, delivery: Schema.Array(Schema.String), inputSchema: Schema.Record(Schema.String, Schema.Unknown), payloadSchema: Schema.Record(Schema.String, Schema.Unknown), }), ), }); export const Granted = Schema.Struct({ id: Schema.String, refreshBefore: Schema.String, cursor: Schema.Null, }); export const Discovered = Schema.Struct({ capabilities: Schema.Struct({ events: Schema.optional(Schema.Struct({})) }), }); export const revision = "2026-07-28"; export const meta = { "io.modelcontextprotocol/protocolVersion": revision, "io.modelcontextprotocol/clientCapabilities": {}, }; export const eventFixtures = Effect.gen(function* () { const api = yield* Api, actors = yield* Actors, target = yield* Target, http = yield* HttpClient.HttpClient; const prefix = `/api/organizations/${actors.organization.id}`; const suffix = randomUUID().slice(0, 8); const created: { apps: string[]; account?: string; key?: string | undefined; } = { apps: [], }; yield* Effect.addFinalizer(() => Effect.gen(function* () { if (created.key) yield* api.request(actors.owner, "POST", "/api/auth/api-key/delete", { keyId: created.key, }); if (created.account && created.apps[0]) yield* api.request( actors.owner, "DELETE", `${requirementPath(`${prefix}/apps/${created.apps[0]}`, "user", "service")}/${created.account}`, ); for (const app of created.apps) yield* removeAppWhenStopped(actors.owner, `${prefix}/apps/${app}`); if (created.account) yield* api.request(actors.owner, "DELETE", `${prefix}/accounts/${created.account}`); }).pipe(Effect.orDie), ); const deploy = (name: string, files: typeof receiverFiles) => Effect.gen(function* () { const deployed = yield* api.request(actors.owner, "POST", `${prefix}/apps/deploy`, { name, files, }); expect(deployed.status, JSON.stringify(deployed.body)).toBe(200); const app = yield* body(App, deployed); created.apps.push(app.id); return app; }); const receiver = yield* deploy(`Event receiver ${suffix}`, receiverFiles); const receiverPath = `${prefix}/apps/${receiver.id}`; const connection = yield* api.request( actors.owner, "POST", connectionsPath(receiverPath, "user", "service"), {}, ); expect(connection.status).toBe(200); const saved = yield* api.request( actors.owner, "POST", `${prefix}/connections/${(yield* body(Resource, connection)).id}/submit`, { method: "key", label: `Receiver ${suffix}`, fields: { token: "synthetic-events" } }, ); expect(saved.status).toBe(200); const account = (yield* body(Resource, saved)).id; created.account = account; const [hook] = yield* settleWebhookSubscriptions(actors.owner, receiverPath, (hooks) => hooks.some((hook) => hook.sourceAccount === account && hook.status === "active"), ).pipe( Effect.map((hooks) => hooks.filter((hook) => hook.sourceAccount === account && hook.status === "active"), ), ); expect(hook?.status).toBe("active"); const callbackUrl = hook!.callbackUrl; const callReceiver = ( tool: "deliveries" | "respond", kind: "query" | "mutation", input: unknown, ) => api.request(actors.owner, "POST", `${receiverPath}/tools/call`, { tool, kind, input, }); const deliveries = callReceiver("deliveries", "query", {}).pipe( Effect.flatMap((response) => body(Schema.Array(Delivery), response)), ); const awaitDeliveries = ( ready: (all: ReadonlyArray) => boolean, seconds = 30, ) => Effect.gen(function* () { const deadline = (yield* Clock.currentTimeMillis) + seconds * 1000; for (;;) { const all = yield* deliveries; if (ready(all)) return all; expect(yield* Clock.currentTimeMillis, JSON.stringify(all)).toBeLessThan(deadline); yield* Effect.sleep("300 millis"); } }); const events = (all: ReadonlyArray) => all.filter((delivery) => delivery.kind === "event"); const emitter = yield* deploy(`Issue events ${suffix}`, emitterFiles); const emitFrom = ( app: { readonly id: string }, repo: string, number: number, id = randomUUID(), ) => api .request(actors.owner, "POST", `${prefix}/apps/${app.id}/tools/call`, { tool: "open", kind: "mutation", input: { repo, title: `Issue ${number}`, number, id }, }) .pipe( Effect.tap((response) => Effect.sync(() => expect(response.status, JSON.stringify(response.body)).toBe(200)), ), Effect.as(id), ); const emit = (repo: string, number: number, id = randomUUID()) => emitFrom(emitter, repo, number, id); const rpcAs = ( credential: Redacted.Redacted, method: string, params: Readonly> = {}, ) => Effect.gen(function* () { const request = HttpClientRequest.post(`${target.metadata.origin}/mcp`).pipe( HttpClientRequest.bearerToken(reveal(credential)), HttpClientRequest.setHeaders({ accept: "application/json, text/event-stream", "content-type": "application/json", "mcp-protocol-version": revision, "mcp-method": method, ...(method === "tools/call" && typeof params.name === "string" ? { "mcp-name": params.name } : {}), "x-executor-organization": actors.organization.id, }), ); const { response, text, started } = yield* readResponse( http.execute( yield* HttpClientRequest.bodyJson(request, { jsonrpc: "2.0", id: 1, method, params: { ...params, _meta: meta }, }), ), ); return yield* parseJson(text, response.status, started, response.headers); }).pipe( Effect.scoped, Effect.mapError((error) => error instanceof CallFailed ? withCause( new RequestFailed({ method: "POST", path: "/mcp", reason: "request", failure: error.failure, cause: error.cause, }), ) : error, ), ); const ok = (schema: Schema.Decoder, value: unknown) => Schema.decodeUnknownEffect(RpcResult)(value).pipe( Effect.flatMap(({ result }) => Schema.decodeUnknownEffect(schema)(result)), Effect.tapError(() => Effect.sync(() => expect.fail(JSON.stringify(value)))), ); const refused = (value: unknown) => Schema.decodeUnknownEffect(RpcError)(value).pipe( Effect.map(({ error }) => error), Effect.tapError(() => Effect.sync(() => expect.fail(JSON.stringify(value)))), ); const createKey = (name: string) => Effect.gen(function* () { const key = yield* body( Schema.Struct({ id: Schema.String, key: heldValue(Schema.RedactedFromValue(Schema.String)), }), yield* api.request(actors.owner, "POST", "/api/auth/api-key/create", { name }), ); yield* Effect.addFinalizer(() => api .request(actors.owner, "POST", "/api/auth/api-key/delete", { keyId: key.id }) .pipe(Effect.ignore), ); return key; }); return { prefix, suffix, receiver, emitter, callbackUrl, callReceiver, deliveries, awaitDeliveries, events, emit, emitFrom, deploy, rpcAs, ok, refused, createKey, name: `${emitter.slug}.issue.opened`, }; });