agentleFS
louisbrulenaudetlouisbrulenaudet/monorepo-template/.cursor/rules/backend/service-bindings.mdc

monorepo-template / backend

Cursor rule19 starsChanged 16 days agoApache-2.0View on GitHub

Worker-to-Worker RPC - bindings.worker() by name, WorkerEntrypoint callee, typed RPC deferred, requestId propagation.

  • Reads credentials

The file

We split this service-bindings.mdc into the rules it gives an agent

2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Call env.BINDING.method() from a route handler or service module, and await every call: RPC client methods are always async.

    line 13
  2. Very specific:

    Default-export the entrypoint class, or export a named class and set exportName on the caller.

    line 20

≈ “Common” is an estimate: at least 1 in 5 of the 6,668 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
service-bindings.mdc
---
description: "Worker-to-Worker RPC - bindings.worker() by name, WorkerEntrypoint callee, typed RPC deferred, requestId propagation."
alwaysApply: false
globs: **/cloudflare.config.ts,apps/worker-*/src/**/*.ts,apps/queue-*/src/**/*.ts,apps/webhook-*/src/**/*.ts,apps/mcp-*/src/**/*.ts
---

# Service Bindings (Worker-to-Worker RPC)

## Caller

- Bind by Worker-name string in the caller's `env`: `FOO: bindings.worker({ worker: "worker-foo-production" })`, the name taken from the `Deployment` table so each mode binds the callee's matching Worker. `exportName` selects a named `WorkerEntrypoint` export (default: the default export) and is required when binding a named class.
- A string reference types the binding as a plain `Fetcher`, without RPC method types. The cf docs' typed form passes the callee's `defineWorker` config, which imports another app's `cloudflare.config.ts` and fails `pnpm boundaries` ([boundaries.mdc](../core/boundaries.mdc)). Typed cross-Worker RPC is deferred to the first `worker-*`; decide it in that change. Never augment `Env` by hand to type a binding.
- Call `env.BINDING.method()` from a route handler or service module, and `await` every call: RPC client methods are always async. Never `env.BINDING.fetch()` when RPC methods are the goal; the exception is a cacheable hot read path ([workers-cache.mdc](workers-cache.mdc)).
- Payload schemas live in `@repo/dtos-common/rpc` ([contracts.mdc](../contracts/contracts.mdc)).
- Cloudflare propagates no custom id across a binding: pass `requestId` as an explicit RPC argument (or queue message field) and log it the same way; traces nest on their own.

## Callee

- Extend `WorkerEntrypoint` from `cloudflare:workers` and add public methods - those are what callers invoke. RPC methods cannot live on a plain `export default { fetch }` handler.
- Default-export the entrypoint class, or export a named class and set `exportName` on the caller.
- Bindings are `this.env` (a class property of `WorkerEntrypoint`: `this.env.GREETING`, `this.env.D1`); RPC methods take only their own arguments, no `request` / `env` parameters.
- Errors lose their class across a binding, so a thrown `HTTPException` reaches the gateway's `errorHandler` as a 500. Return expected outcomes as a result union from `@repo/dtos-common/rpc`; throw an `Error` with a serializable `name` / `code` for the unexpected, which the caller maps by those fields, never `instanceof`. `HTTPException` stays in the Hono gateway.

## After a binding change

1. Callee: exported `WorkerEntrypoint` class with public RPC methods.
2. Caller: `bindings.worker()` in `cloudflare.config.ts`, named per mode.
3. Order: the callee takes a lower `monorepo.deployOrder` than every caller, so it is live before a caller binds it. Remove an RPC method one release after the last caller stops calling it.
4. Shared payload schemas in `dtos-common/rpc` if needed.
5. `pnpm types`, then `pnpm check-types`.
6. The first `worker-*` binding also adds the `createTestHarness` Node suite (checklist in `packages/vitest-config/AGENTS.md`), keeping the pool suites.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.