Skip to content

Capability Scopes and Access

Dory Azar edited this page Nov 15, 2026 · 2 revisions

Three options of #[Capability(...)] say who may call a capability: public, scope and level. They are checked once, in Capabilities::call(), for every channel: a page, a form, REST, the JS client, an agent over MCP, the command line and the jobs all get the same answer. Since 2.1.0.

Since 2.26.0 an empty scope on a write capability (not readOnly, not public) means a token needs the scope named after the capability (create_book) or *; resource:* does not cover it. Signed-in people and the command line are unchanged. php caligrafer.php doctor lists such capabilities; TOKEN_DEFAULT_SCOPES=false (deprecated) restores the old rule.


With Claude

Say the scope and who may call it:

/caligrafy-quill:new-capability list_books books:read read-only public: the catalog, 20 per page
/caligrafy-quill:new-capability delete_book books:write destructive: only administrators (level 1)

Check before you say OK:

  • php caligrafer.php capabilities shows public, the scope, or signed in (no scope) in the second column;
  • php caligrafer.php call <name> --guest answers unauthenticated for a capability that is not public;
  • a token without the scope gets 403 over REST and does not see the tool in MCP tools/list.

By hand

#[Capability(name: 'list_books', description: 'Lists the books of the catalog, 20 per page.', readOnly: true, public: true)]
#[Capability(name: 'create_book', description: 'Adds a book to the catalog and returns it.', scope: 'books:write')]
#[Capability(name: 'delete_book', description: 'Deletes a book for good. Returns its id.', scope: 'books:write', level: 1, destructive: true)]

(one attribute per class). Then try each caller:

php caligrafer.php call create_book title=Dawn published_year=1987 author_id=2 --guest          # unauthenticated
php caligrafer.php token create "Reader" --scopes books:read
php caligrafer.php call create_book title=Dawn published_year=1987 author_id=2 --token cq_...   # forbidden

The second call answers {"error":{"code":"forbidden","message":"This needs the scope books:write"}}; with a level the message ends with and permission level 1.


The checks, in order

Capabilities::call():

  1. Find it: no such name, or a webOnly capability reached by anything but Route::action, is not_found.
  2. public: true: no check at all; anyone may call it, visitors included.
  3. Someone must act: a guest (nobody signed in, no token) is unauthenticated (401).
  4. $context->allows($scope, $level), else forbidden (403).
  5. Then the inputs are validated and handle() runs. A capability can still refuse more itself, with ServiceError::forbidden() (Refusing a call).

scope: what a token may do

A scope is <resource>:<action>: lowercase letters, digits, _, . and - on each side (books:read, books:write). The scopes a token was given cover a required scope when one of them is:

  • the same scope (books:write);
  • <resource>:* (books:* covers books:read and books:write);
  • * (everything).

scope: '' is met by any token. Scopes apply to tokens only: a signed-in person has every scope, and is checked by the level.


level: how much a person may do

level is the minimum value of the users' permissions column (Context::$levelAttribute, permissions by default). 0, the default, means anyone signed in. $context->level() is the user's value, 0 without a user.

Caller What allows($scope, $level) checks
the command line, scripts and jobs (Context::cli(), Context::system()), the legacy API key nothing: always allowed
a signed-in person permissions >= level
a token with an owner (token create ... --user 7) the scope, and the owner's permissions >= level
a token without an owner the scope only: the level does not apply
a token whose owner was deleted (with authentication on) refused
a guest refused (only public capabilities)

public: anyone

public: true skips the checks: a visitor's page, a REST call without a token and any MCP token reach it. Use it for what anyone may read (the catalog), or for what a visitor must do before having an account. A public capability may still look at $context and refuse: the framework's register_user is public but refuses every channel but web pages and the command line (G81).


What each caller sees

Capabilities::available($context) keeps the capabilities this caller may call (public, or allowed), web-only ones left out. MCP tools/list and GET /capabilities show only those, so a token never sees a tool it may not call.

Reference: Context::allows(), Tokens::allows(), Capabilities::available().


Common mistakes

  • service without --scope (G76): scope: '' lets any token call it.
  • Trusting the level with an application token (G53): a token without --user passes any level. Create agent tokens with --user <id> when the level matters.
  • The legacy API key and tokens without an owner never manage users (G85): the user capabilities need a real administrator, so give an agent that manages users a token of an administrator.

Related pages

Caligrafy: Describe it. Approve it. Ship it. Your idea turned into a complete app: every feature built once, for web pages, Vue apps, a REST API and AI agents, and online.

Start here

Why Caligrafy: the benefits, the measurements, and what it is not yet

  1. Quickstart with Claude: describe it, approve it, ship it

    or the Quickstart: the same three steps by hand, in 5 minutes

  2. How Caligrafy works: the ideas in 5 minutes
  3. Your first capability: build a feature, page included

Capabilities

Experiences

Data and people

  1. Models
  2. Relationships
  3. Queries: filters, paging, related rows
  4. Sign-in, users and tokens

Built-in features

Put it online

Build with AI

Reference

Clone this wiki locally