mehmetozer.dev
open to senior rolesGet in touch
my-app.workers.dev · castra

Castra

A backend on Cloudflare that is closed by default.

One Worker and one D1 database in your account serve a REST API for your tables. Every route is protected by policies written in TypeScript and compiled to SQL, and an MCP server lets AI agents see exactly what production enforces.

Free & open source · MIT · Your Cloudflare account, no hosted service

What's inside.

01 / features

Closed by default

A table without a policy has no routes. Unknown and closed tables answer the same 404.

Policies are SQL

Rules compile to WHERE clauses, so pagination and counts stay correct.

Checked writes

Every create and update is verified on the stored row, atomically. Server-set columns never come from the client.

Auth included

Better Auth (email, GitHub), roles and API keys, checked against D1 on every request.

Agent-safe MCP

Read and write tools on the same engine and write path as REST, with scoped tokens and an audit log.

Your account

One Worker and one D1 database in your Cloudflare account. No hosted service in between.

Castra — Create a backend, migrate local D1, check policies, and start the API
Create a backend, migrate local D1, check policies, and start the API
Castra — Policies enforced live: anonymous visitors see only public rows, and writes need a signed-in user
Policies enforced live: anonymous visitors see only public rows, and writes need a signed-in user

Get started.

02 / quickstart

Create a new project:

npm create @castra-kit@latest my-app

A policy. Any table not listed is never exposed:

import { authenticated, defineConfig, isPublic, owner, role } from "@castra-kit/core";

export default defineConfig({
  database: "d1",
  roles: ["admin", "editor"],
  tables: {
    posts: {
      read: isPublic().where((q) => q.eb("status", "=", "published")).or(owner("author_id")),
      create: authenticated(),
      update: owner("author_id").or(role("editor", "admin")),
      delete: role("admin"),
      fields: { author_id: { setBy: "server" } },
    },
  },
});
// Any table not listed here is never exposed.

Read before you rely on it.

03 / status

Castra is early. It is deployed and tested, but only writes are rate limited and D1 has a single writer. See status and limits and the security model.

Free, open source, yours.

MIT licensed, published on npm as @castra-kit. Issues and contributions welcome.