# Capabilities

> Give an AI agent a fixed, typed database operation and switch its free-form writes off: what it may do is decided in advance.

Source: https://osevel.com/docs/guides/capabilities/

A capability is a named operation you install for your agents, for example
`stok_dus(urun_id, adet)` or `siparis_gonder(siparis_id)`. An agent calls it as an MCP tool with
typed arguments; it never writes the SQL. Combined with an agent that has no raw SQL writes, the
agent can change data only in the ways you wrote down.

## Write one

A capability is one SQL function in a file:

```sql
CREATE FUNCTION stok_dus(urun_id int, adet int) RETURNS int
LANGUAGE sql AS $$
  UPDATE public.products SET stock = stock - adet
   WHERE id = urun_id AND adet > 0 AND stock >= adet
  RETURNING stock
$$
```

- `LANGUAGE sql` or `plpgsql`; every parameter named, of type `int`, `bigint`, `numeric`, `text`,
  `boolean`, `uuid`, `date`, `timestamptz` or `jsonb`; no defaults, `OUT` or `VARIADIC`.
- Leave out `SECURITY` and `SET`: osevel makes every capability `SECURITY INVOKER` with
  `search_path` = your managed schemas, `pg_catalog`, `pg_temp`.
- Dynamic SQL (`EXECUTE` in plpgsql) is allowed but flagged in the review: check that no argument
  reaches the query text unquoted.
- A capability marked `STABLE` or `IMMUTABLE` only reads: its calls run in a read-only
  transaction, are not journaled and need no approval, and the tool is marked read-only for the
  client. The label is read from the database, not from the agent. A mislabelled function that
  tries to write fails; the database refuses writes in a read-only transaction. Leave the label
  out (`VOLATILE`, the default) for anything that writes.
- The return value reaches the agent: a call's result carries it as `returned` (inside the
  untrusted-data block), next to the change id and the rows affected. Return what the agent needs
  to know (the new stock, whether the order was cancelled); `void` returns nothing.

## Install and grant

```bash
osevel capability add stok_dus.sql --description "Reduces a product's stock; does nothing if there is not enough."
osevel capability grant claude-cap stok_dus
osevel capability list
osevel capability revoke claude-cap stok_dus
osevel capability remove stok_dus
```

`add` and `remove` need the admin connection; `add`, `remove` and `grant` need the confirmation
code. The review shows the whole source: an agent granted the capability can run it with any
arguments.

## Proposals from agents

An agent with `schema` access has a `propose_capability` tool. It sends one `CREATE FUNCTION` and a
description; the source is validated at once (the agent learns what is wrong), and nothing is
installed:

```bash
osevel capability proposals          # pending proposals (--all for decided ones)
osevel capability approve 3          # shows the full source, asks for the code, installs it
osevel capability deny 3
```

Approving installs exactly the stored source with the admin connection, like `capability add`;
grant it afterwards. Journal version 5 (`005_capability_proposals.sql`) keeps the rules in the
database: only the MCP server's journal role records proposals, only for active agents with
`schema` access, always pending; only the approver role decides a proposal, once (`AR006`).

The dashboard shows proposals with their source and the commands above (see "In the dashboard"
below), but cannot approve one: installing needs the admin connection, which the dashboard bridge
never holds.

An agent with `read` or `write` access does not see the tool at all; raise it with
`osevel agent access <name> schema` (asks for the code) and reconnect its MCP server.

## Agents without raw writes

`agent add` now registers agents without raw SQL writes unless you pass `--raw-sql`. Such an agent
has `query` (reads), its capabilities, and no `execute` or `migrate`. Agents registered before
journal version 4 keep raw writes.

## Profiles

| Profile    | Raw SQL reads (`query`, `get_schema`) | Raw writes            | Proposals          | Capabilities |
| ---------- | ------------------------------------- | --------------------- | ------------------ | ------------ |
| `standard` | yes                                   | only with `--raw-sql` | with schema access | granted ones |
| `sealed`   | no                                    | never                 | no                 | granted ones |

```bash
osevel agent add scanner --ide other --profile sealed
osevel agent profile scanner standard     # unsealing needs the confirmation code
```

A sealed agent is for agents that should do exactly a few named things and nothing else (the
security scanners and auditors of Phase 3). The database refuses raw writes and proposals for it
(journal v6: a sealed agent can never have `raw_write`); the MCP server leaves out the raw read
tools, which the database cannot tell apart from other reads. The dashboard role may seal an agent,
never unseal it.

## What the database enforces

Journal version 4 (`004_capabilities.sql`, applied by `osevel upgrade`):

- Capabilities live in the `agent_capabilities` schema. Only the agent role may execute them, and
  they run with its own privileges, so a capability can never do more than the agent role could.
- A call is a data write on the usual path: impact measured, policy and approvals apply, every row
  is journaled and can be undone. `changes.capability` records which capability it came from.
- `changes_guard` refuses a capability change from an agent the capability is not granted to, and a
  raw data write or schema change from an agent without raw writes. A revoked grant takes effect at
  the next call.
- The agent never supplies SQL: the MCP tool writes `SELECT agent_capabilities.<name>(<constants>)`
  from validated arguments, and the classifier accepts exactly that shape as a capability call.

## What it does not protect against

- **The capability's own code.** If it is wrong, or builds dynamic SQL from an argument, the agent
  can use that. Review it before you confirm.
- **Grants per agent are checked by the MCP server and the journal guard**, not by a database role
  per agent: all agents share the agent role. In daemon mode the daemon is trusted to attribute.
- **A separate "caller" role** that cannot write tables at all (the design's stronger option) is not
  built yet; today the agent connection itself could write if a bug bypassed the journal path.

## Tested with a real IDE

On 2026-11-18 a Claude Code agent without raw writes, granted `stok_dus` and `siparis_gonder`,
processed three orders on a local Postgres 17 project: six capability calls, each journaled under
the agent; asked to change a price, it reported that none of its tools could.

## In the dashboard

Agents & sessions shows, read only:

- a **Writes** column in the agents table: `raw SQL`, `capabilities only` or `sealed`, with the
  number of capabilities granted;
- the **Capabilities** table: arguments, description, the agents each one is granted to, and its
  source;
- **Proposals from agents**, with the agent's description marked as the agent's own claim, the
  source, and the `capability approve` / `capability deny` commands to run at the terminal.

The dashboard cannot install, grant or approve a capability: installing needs the admin
connection, granting and approving need the terminal.
