RolebookDocs

The rolebook command

@rolebook/cli: set up Rolebook, check a setup, explain decisions, read and change access on your own server, and compare policies.

@rolebook/cli installs the rolebook command. It works on your project and on the Rolebook server you run yourself: it never calls anything else, and it sends no telemetry.

Not on npm yet

@rolebook/cli is not on npm yet. The npm package named rolebook is an unrelated project, so do not install it.

The commands on this page use npx --no, which runs only a copy already installed in your project and never downloads one.

npx --no rolebook --help

It needs Node 24 or later, or Bun. rolebook and @rolebook/sharing are its peers.

CommandWhat it does
rolebook initWrites a starting policy and its wiring.
rolebook doctorChecks a setup and reports what is wrong.
rolebook explainAsks your server why someone can or cannot do something.
rolebook accessReads access, and changes it after a preview.
rolebook policy diffCompares two policies.
rolebook mcpRuns the MCP server for your agent.

Every command takes --help, and every command that prints a result takes --json. The exit status is 0 when the answer is the good one, 1 when the command ran and the answer is a no (a denial from access check, a difference, a failed check, a change that was not applied), and 2 when the command could not run. explain exits 0 whenever it got an explanation, whatever the decision.

Text output shows control and direction characters from the server or from arguments as visible \u{…} escapes, so a crafted id cannot rewrite a line in your terminal. --json keeps every value exact. Error messages are written by the command from the HTTP status and the error code; text the server sent is never repeated.

Set up a project

npx --no rolebook init

init reads package.json, detects Next.js, Hono, Express or Fastify (or plain Node), and writes into src/authz/, or lib/authz/ when there is no src/ directory:

  • policy.ts, a definePolicy() starting point to rename to your own resources and roles;
  • middleware.ts for Hono, Express and Fastify, built on rolebook/middleware/<framework>;
  • authorizer.ts for Next.js and plain Node, with createAuthorizer(policy).

Each wiring file has a getBinding hook that returns undefined until you read the signed-in user from your session, so every check is denied until you fill it in. init never overwrites a file: one that exists is reported and left as it is. It does not edit package.json or install anything; it says so when rolebook is missing. --framework chooses the framework, --dir the project, and --dry-run writes nothing.

init writes only into real directories of the project: a symbolic link on the way, or a file where a directory belongs, is refused. Each file is written to a temporary name, flushed, and then linked to its final name, so a failed write leaves nothing behind; on a file system without hard links it is created exclusively instead. The checks work by path, so they do not defend against another process that swaps a directory for a link while init runs.

Check a setup

npx --no rolebook doctor

doctor checks the Node version, that every rolebook and @rolebook/* dependency is installed and that its Rolebook peers are installed at a matching version (a range other than ^x.y.z is reported as unchecked), that the policy passes definePolicy, and, when ROLEBOOK_URL is set, that your server answers and accepts the configured credentials. It changes nothing in the project. It does not inspect database tables; the storage adapters’ own migrations and tests cover those.

On its own, doctor reads only a data policy: rolebook.policy.json, src/authz/policy.json or lib/authz/policy.json. A policy module such as the src/authz/policy.ts that init writes is code, so doctor points at it and runs it only when you pass it explicitly:

npx --no rolebook doctor --policy src/authz/policy.ts

A module runs in a child process. The command passes that process only PATH as its environment, so the credentials are not passed on; it starts in a new empty working directory, and under Bun with .env loading and auto-install turned off. Inside that process, definePolicy builds the policy from the module’s original export, and only that result comes back; the module’s own output and error text are discarded, and a policy error is reported by its code alone. The policy checked is the one the module exports under those conditions: a module that depends on an environment variable or on the working directory may select a different policy there than in your application, or fail to load, in which case the check fails. To check what a particular deployment loads, write that deployment’s policy to a JSON file and compare or check that file. The process and its process group are ended after 10 seconds, or when the command is interrupted (on Windows, only the process itself). This is not a sandbox: the module runs with your own file-system and network access, and on some systems a process can read its parent’s environment from the process table. Run only your own files.

Connect to your server

explain, access and mcp call the Sharing HTTP API that you mount with @rolebook/server. The credentials come from the environment, never from a flag that would land in your shell history; only the URL can also be given with --url:

VariableMeaning
ROLEBOOK_URLThe mounted API, for example https://app.example.com/api/sharing. --url overrides it.
ROLEBOOK_TOKENSent as Authorization: Bearer <token>.
ROLEBOOK_COOKIESent as the Cookie header.
ROLEBOOK_HEADERSA JSON object of further headers, for example a team header your getContext reads.
ROLEBOOK_TIMEOUT_MSMilliseconds per request, 1 to 120000. Default 10000.

Your server’s getContext turns these credentials into the tenant and the actor, exactly as for a request from your app. The command sends no tenant and no actor of its own, and no flag can name one. It fails closed:

  • without ROLEBOOK_URL, or without any of the three credential variables, it stops before sending anything;
  • the URL must be https, except http for localhost, 127.0.0.1 and [::1], and may not carry a user name, a password, a query or a fragment;
  • a credential with a line break or another character a header cannot hold is refused, and ROLEBOOK_HEADERS may not set host, origin, content-type or the other headers the request owns;
  • an unreachable server, a timeout, a 401 or a response that is not the Sharing API is an error (exit 2), never an empty answer. No message repeats a credential.

Ask why

npx --no rolebook explain --resource project:q3-report --action delete --subject user:ada

explain calls sharing.explain and prints the decision, the rule that decided it, and the rules that were considered. --subject defaults to the configured identity, and --field explains one field. Your server must enable inspection, and the configured identity must be allowed to inspect the item; otherwise the answer is NOT_FOUND_OR_DENIED and the exit status is 1.

Read access

npx --no rolebook access check --resource project:q3-report --action delete
npx --no rolebook access permissions --resource project:q3-report
npx --no rolebook access describe --resource project:q3-report
npx --no rolebook access list --type project --action read --limit 20
npx --no rolebook access history --resource project:q3-report

check and permissions answer for the configured identity. describe lists the owner, the members with their roles and expiry, and pending invitations. list and history page with --cursor, which they print when there is more.

Change access

npx --no rolebook access grant --resource project:brand-assets --subject team:design --role editor --until 2027-03-31
npx --no rolebook access change --resource project:brand-assets --subject team:design --role viewer --permanent
npx --no rolebook access revoke --resource project:brand-assets --subject team:design

A change takes two runs. The first only reads the item’s current access and prints what would change, and a confirmation; nothing that changes anything is sent. To apply exactly what you read, run the same command again with --confirm and the confirmation that preview printed, in place of <confirmation>:

npx --no rolebook access revoke --resource project:brand-assets --subject team:design --confirm <confirmation>

The confirmation is a digest of the change (kind, item, subject, role and expiry) followed by the item’s sharing revision. The second run refuses a confirmation made for any other change, and stops without changing anything when the revision is no longer the one you confirmed. The confirmation is not tied to the tenant or to --url, and it is an integrity check, not proof that a preview was shown: anyone who knows the change and the item’s revision can compute it. A server revision with anything but letters, digits and . _ ~ : - is refused rather than printed, so the line you are asked to run can never carry shell syntax from the server. Otherwise it sends one sharing.executeIfUnchanged command carrying the confirmed revision, and the server decides whether the configured identity may make the change. --until takes a date (meaning 00:00 UTC on that day) or an RFC 3339 time with real calendar values; without it a grant is permanent, and a role change keeps the current expiry unless you pass --until or --permanent.

What the revision check does and does not guarantee:

  • It catches any change that moves the item’s sharing revision between your preview and the write. The server checks the confirmed revision in the same commit as the change, so a change that lands at any moment before it, however close, is refused as STALE_PREVIEW and nothing is applied. A membership reaching its expiry does not move the revision.
  • A server that predates sharing.executeIfUnchanged, or a newer one whose sharing runtime does not declare conditionalExecution, answers 404, and the command stops with PRECONDITION_UNSUPPORTED without changing anything; it never falls back to an unconditional sharing.execute.
  • The revision belongs to the item in the tenant your credentials select. If your server picks the tenant from session state that can change (an “active team” cookie, for example), keep it fixed while you change access.
  • A valid sharing.executeIfUnchanged result is definitive: applied, already applied, or refused with a reason. A 4xx refusal in the Sharing API’s error format is definitive too. Any other exchange (no answer, a 5xx, a 2xx or 3xx carrying an error, or a body that is not the API’s) leaves the outcome unknown: the command says so and exits 2. Read the access with access describe before trying again.

Invitations and ownership transfers are not in this version.

Compare policies

npx --no rolebook policy diff policies/staging.json policies/production.json

policy diff loads two policies, checks each with definePolicy, and lists the differences that diffPolicies finds: resource types and actions, parents, roles, scopes, inheritance, and direct and effective rules. It exits 0 when the two grant the same access and 1 when they differ.

Each side is a .json file, or a module (.js, .mjs, and .ts or .mts where your runtime strips types) whose policy or default export is the policy; --export names another export. A module runs in a child process as for doctor: the credentials are not passed to it, but it is not a sandbox, so compare only your own files. JSON is read strictly, so a policy that repeats a key is refused rather than read as its last value.

Not in this version

  • rolebook login and rolebook keys belong to Rolebook Console, which is not available yet.
  • rolebook migrate: apply your storage adapter’s migrations with your own migration tool; see Migrations.
  • doctor does not inspect database tables, columns or fences.
  • A way for the server to report the tenant a preview was read in.

API reference

The package’s entry point runs the same command in-process, for a script or a test.

runCli

function runCli(argv: readonly string[], io: CliIo, streams?: McpStreams): Promise<number>;

Runs one invocation and resolves to its exit status. It never calls process.exit. streams is needed only for mcp.

While a policy module runs (doctor --policy, policy diff, the MCP policy tools), SIGINT, SIGTERM and SIGHUP end that module’s process. Inside your own program, runCli leaves the signal itself to you: it never ends your process, whatever handlers you have, so handle those signals yourself. The rolebook command, which owns its process, ends after ending the module.

CliIo

interface CliIo {
  readonly stdout: (text: string) => void;
  readonly stderr: (text: string) => void;
  readonly env: Readonly<Record<string, string | undefined>>;
  readonly cwd: string;
  readonly fetch?: (input: string, init: RequestInit) => Promise<Response>;
}

fetch defaults to the global fetch; it is only ever called with the configured server’s URL.

McpStreams

interface McpStreams {
  readonly input: AsyncIterable<Uint8Array | string>;
  readonly write: (line: string) => void;
}

EXIT

const EXIT: Readonly<{
  ok: 0;
  negative: 1;
  error: 2;
}>;

VERSION

const VERSION: string;

The version in the package’s own package.json.