Skip to content

The backend stack

stacks/backend runs Directus per environment, at https://directus.<domain>, on the environment’s Cloud SQL database directus. It came from isthmia’s crm, clubspot-sync, and gsuite-sync projects with their resource names unchanged, and their states moved over with pulumi state move (cyc-seattle/workplace#6). It holds:

  • Directus, as a WebService, keeping uploaded files in the environment’s Cloud Storage bucket, with the query guard and the group-policies hook mounted as hook extensions, and the cents-as-dollars display
  • the schema apply, from every schemas/<domain>.yaml merged into one snapshot
  • each collection’s default view in the Data Studio
  • the roles, policies, and permission rules (access.md), including the Community and Team policies and the query guard
  • the group-policies hook, which gives each user the policies their groups give
  • the website-publish hook, which publishes the public website when its content changes (website.md)
  • the admins’ accounts, and the clubspot-sync, gsuite-sync, and website build machine users with their tokens
  • Directus’s OIDC client at Authentik
  • the MCP settings
  • the clubspot-sync and gsuite-sync CronJobs, their images, and the Secrets that hold their tokens

infra makes prod’s files bucket, <project>-directus-files-prod, and lets Directus’s pod use it through Workload Identity, since neither deploy identity may. The bucket is private and versioned, and keeps a deleted or replaced file for 30 days.

A push to main deploys it to prod, after infra.

Outputs

Output Read by
directusOidcClientId isthmia’s authentik, to register Directus
directusOidcClientSecret isthmia’s authentik, to register Directus
directusAdmins authentik, to make them superusers

backend owns the OIDC client and Authentik registers it, so platform never reads isthmia’s outputs. Its client is the one Authentik generated before cyc-seattle/workplace#6: client ID directus, and the secret directus-oidc-client-secret, whose state moved into backend. While Authentik and Directus hold different clients, signing in to Directus through Authentik fails. The built-in admin still signs in.

What staff see in the Data Studio

  • Each collection’s table opens with the sort and columns in stacks/backend/default-views.ts. DirectusPreset writes them as global presets: no user, no role, and no bookmark. A staffer who saved their own view keeps it until they reset it.
  • A default sort never crosses a link the query guard refuses, or the table fails to open for Community, Team, or Crew. Where a staff view’s sort would, Community and Team get a view of their own (familyViews), a preset for their role. test/default-views.test.ts checks every view against the guard.
  • Money is stored in cents. A field that holds cents uses the cents-as-dollars display (stacks/backend/cents-display.mjs), so a table shows 12500 as $125.00. Directus’s own displays can’t divide. A row’s own page still edits the number in cents, and its note says so.
  • A field a sync writes is read-only in the Data Studio, since the next sync would overwrite an edit. The syncs write through the API, which ignores that setting.

Never destroy it

Directus’s license activation is bound to its database’s project_id and its PUBLIC_URL, and the key allows 5 activations (cyc-seattle/isthmia#172). Never pulumi destroy backend, and never drop, recreate, or rename its database or change its URL. Move its resources with pulumi state move instead. Ask the maintainer before any change that could activate a new Directus.

  • Directus activates on startup only when directus_settings.license_key is empty, and otherwise refreshes. A restart, redeploy, or reset that keeps the database and the URL spends nothing.
  • A throwaway Directus, such as the CI stack’s (testing.md), may have the key: a new activation stays pending for 24 hours and never counts if the instance is gone by then (pending activations). Never keep one running for 24 hours.
  • Before an instance leaves its project_id or PUBLIC_URL, release its activation with @directus/license’s deactivateKey, which is meant for server migrations.
  • A refresh that fails with BINDING_MISMATCH, as after Directus support resets the activation count, keeps the stored license for its 30-day grace period. To bind again, clear license_key (not the token) and restart, which spends one activation.

Migrations are one way

Directus runs stacks/backend/migrations/ once each, at start, and none of them undo themselves. A rollback to an image from before a migration runs the old code on the migrated database:

  • Before cyc0004, a Financial Plan was marked active in version_set_activations, which it drops. The old hook’s triggers don’t know the active status, so an active Financial Plan, and its plans, could be changed until the newer image is back. Roll forward instead.

MCP

Directus 12.3.1 serves MCP at /mcp. It needs both:

  • the environment: MCP_ENABLED (on by default) and, for OAuth sign-in, MCP_OAUTH_ENABLED
  • the directus_settings row, which the Data Studio shows under Settings → AI: mcp_enabled, mcp_oauth_enabled, mcp_oauth_cimd_enabled, mcp_oauth_dcr_enabled, and mcp_allow_deletes

backend sets both. The DirectusSettings resource writes the settings row through the admin API. backend:mcp turns MCP on, and it’s off when unset. Prod sets it. Deletes are allowed, and dynamic client registration is always off.

A client registers by its metadata document (CIMD). Both the document and the redirects must be on claude.ai or claude.com. Loopback redirects, which desktop clients use, are always allowed. Custom-scheme redirects, which Directus allows for Cursor and Raycast by default, are not.

What MCP can reach

MCP adds no access. Its tools act as the signed-in user:

  • The items tool reads, writes and deletes through Directus’s ItemsService, with the user’s own permissions.
  • Tools that change the schema, flows, or operations are hidden from anyone who isn’t an admin.

Directus 12 has no per-policy or per-role switch for MCP. Every signed-in user can use it:

  • through OAuth, since the consent page needs only a Directus session, which every role gets when it signs in through Authentik
  • with a session cookie or a static token, whatever their role

So MCP can’t keep Community users out. It’s on in prod for every signed-in user anyway (cyc-seattle/workplace#6 decision 16): it adds no access, and the query guard checks its reads.

The query guard

The guard (cyc-seattle/isthmia#235) is a filter on items.query. The items tool’s reads call ItemsService.readByQuery, which emits items.query for every read, including the reads after a create or an update. So the guard checks an MCP tool call’s filter, sort, group, aggregate, and deep exactly as it checks a REST query.

The syncs

clubspot-sync and gsuite-sync live in src/syncs/, and syncs.md describes them. The root Dockerfile bundles each into one file with esbuild, so its image has no node_modules. An image’s tag is a hash of what it’s built from (stacks/backend/sync-image.ts), and each tag is pushed once. A deploy runs the tag from the registry if it’s there, and builds and pushes it if it isn’t. A preview never builds one. A commit that doesn’t change the syncs changes neither image, and its prod deploy is skipped (cyc-seattle/platform#72). Each CronJob reads its machine user’s Directus token from its own Secret, which backend writes from the token it generates.

In prod they run hourly, clubspot-sync on the hour and gsuite-sync at half past. kubectl create job --from=cronjob/<name> runs one by hand. The features run each sync in the test’s own process, against the fakes, on just their own camp or program (testing.md).

Their config keeps the clubspot-sync: and gsuite-sync: keys it had in isthmia’s projects.