Metadata-Version: 2.4
Name: lokra
Version: 0.8.6
Summary: Policy proxy between AI agents and databases: per-agent roles, masking, write approval, signed audit ledger.
License-Expression: Apache-2.0
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: mcp<3,>=2.3
Requires-Dist: psycopg[binary]>=3.2
Requires-Dist: pyyaml>=6
Requires-Dist: sqlglot>=30
Requires-Dist: truststore>=0.10
Requires-Dist: pyjwt[crypto]>=2.10
Provides-Extra: slack
Requires-Dist: slack_sdk>=3.33; extra == "slack"
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == "dev"
Requires-Dist: slack_sdk>=3.33; extra == "dev"
Requires-Dist: markdown>=3.5; extra == "dev"
Requires-Dist: pygments>=2.17; extra == "dev"
Dynamic: license-file

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/lokrahq/lokra/main/brand/lokra-logo-white.svg">
    <img src="https://raw.githubusercontent.com/lokrahq/lokra/main/brand/lokra-logo.svg" alt="Lokra" height="56">
  </picture>
</p>

<!-- mcp-name: io.github.lokrahq/lokra -->

<p align="center">
  <b>Lock rows for AI agents.</b><br>
  <a href="https://lokra.dev">lokra.dev</a> · <a href="https://lokra.dev/docs/">Docs</a> · <a href="https://cloud.lokra.dev">Lokra Cloud</a>
</p>

<p align="center">
  <a href="https://github.com/lokrahq/lokra/actions/workflows/ci.yml"><img src="https://github.com/lokrahq/lokra/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
  <a href="https://pypi.org/project/lokra/"><img src="https://img.shields.io/pypi/v/lokra" alt="PyPI"></a>
  <img src="https://img.shields.io/badge/license-Apache--2.0-blue" alt="License: Apache-2.0">
</p>

Lokra is an MCP server that sits between your AI agents and Postgres. You write one
`policies.yaml`, and Lokra turns it into a Postgres role per agent, with grants, row-level
security and masking. Postgres itself decides what each agent can read. Writes are dry-run
and wait for a person, and every request goes into a signed, hash-chained log.

It works with Claude Code, Claude Desktop, Cursor, VS Code, Windsurf and any other MCP client.

<p align="center"><img src="https://raw.githubusercontent.com/lokrahq/lokra/main/docs/demo.svg" alt="lokra demo in a terminal: two agents on a throwaway Postgres, with a row lock, masking, a refused prompt injection, an approved write, a rate limit and the signed ledger" width="820"></p>

## Try it

```bash
brew install lokrahq/tap/lokra    # or: pipx install lokra
lokra demo
```

The demo starts a throwaway Postgres in Docker, points two agents at it and cleans up when
it's done. No Docker? `lokra demo --dsn "postgresql://..."` runs in its own schema on any
Postgres where you can create roles.

## Use it on your database

```bash
lokra init "postgresql://user:pass@host:5432/app"                     # read-only, writes policies.yaml
lokra check --dsn "postgresql://user:pass@host:5432/app"              # typos, unmasked PII columns
lokra provision --admin-dsn "postgresql://admin:pass@host:5432/app"   # roles, grants, row-level security
lokra connect                                                         # adds the agent to Claude Code
```

`init` only reads. It marks columns that look like emails, phone numbers or ID numbers so
you can mask them. `provision` needs a role that can create roles, not a superuser, so the
RDS master user and Supabase's `postgres` user both work. For other clients, use
`lokra connect --client cursor` (or `claude-desktop`, `vscode`, `windsurf`).

A policy looks like this:

```yaml
database: {host: db.internal, port: 5432, dbname: app}

agents:
  support-bot:
    rows: {column: account_id, values: [42]}   # only account 42's rows, enforced by Postgres
    read:
      customers: [id, name, email]
      orders: "*"

  ops-bot:
    read: {orders: "*"}
    write: {orders: [update]}
    approval: {auto_max_rows: 1}               # one-row updates run, bigger ones wait for you
    rate_limit: 60                             # requests a minute

masking:
  columns: {email: redact, phone: last3}
```

Put `lokra check --strict` in CI and the build fails when someone adds a column that nothing masks.

## What you get

- Each agent logs in as its own Postgres role and sees only the tables and columns in its
  policy. It gets a short-lived signed token, never a password.
- Rows can be locked to fixed values, or per request: one support agent serves every customer
  and only sees the signed-in customer's rows, taken from your app's token or your login
  provider's. See [one agent for every customer](https://lokra.dev/docs/customer-agents).
- Emails, phone numbers, cards, IBANs, SSNs and Australian Medicare and IHI numbers are masked
  in columns, free text and JSON. A masked column can't be pulled out through a function, an
  alias or `row_to_json`.
- Every INSERT, UPDATE and DELETE is dry-run and shown with the rows it would change, before
  and after. Approve it in the terminal, the local dashboard, Slack or Lokra Cloud. Small writes
  can run on their own, deletes can need two people, and `lokra undo` puts the rows back for
  7 days. An agent that has moved on can have Lokra run its write the moment it's approved.
- `lokra pause support-bot` refuses the agent's requests, blocks its database login and ends
  its sessions. `lokra revoke-token` kills one leaked token.
- `lokra exposure` lists whose data an agent read or changed, `lokra report` flags probing and
  runaway loops, and `lokra evidence` writes an audit pack mapped to ISO 27001 and the
  Essential Eight.

## Why Postgres enforces it

Several "read-only" database MCP servers were bypassed in 2025 and 2026 because they decided
what was safe by parsing SQL. Lokra checks SQL too, but that isn't the boundary. The limits
live in Postgres, so a query that gets past the check still meets them:

| Layer | Stops | Enforced by |
|---|---|---|
| Signed short-lived token | Unknown, forged or expired agents | Lokra (HMAC) |
| SQL check | Writes in the read tool, stacked statements, DDL, `SET ROLE` | Lokra (sqlglot), fails closed |
| Per-agent login role | Tables and columns outside the policy | Postgres grants |
| Row lock | Rows outside the agent's scope, for reads and writes | Postgres row-level security |
| Read-only transaction | Writes hidden in CTEs or functions | Postgres |
| Prepared statements only | `COMMIT; DROP ...` style stacking | Postgres protocol |
| Statement timeout | Runaway queries | Postgres role setting |
| Masking | Identifiers in results, and masked columns leaving under another name | Lokra |
| Ledger | Silent edits or deletions of history | SHA-256 chain and HMAC |

The tests in `tests/test_integration.py` turn the SQL check off and run the attacks anyway, to
show the database still blocks them. More in the [security model](https://lokra.dev/docs/security-model).

## Running it for real

- Agents on other machines connect over HTTP: `lokra serve --http` behind TLS, with a token per
  agent from `lokra mint-token`. See [production](https://lokra.dev/docs/production).
- Every release is also an image for amd64 and arm64:
  `docker run -p 8787:8787 --user "$(id -u):$(id -g)" -v "$PWD:/work" ghcr.io/lokrahq/lokra:0.8`
- `lokra slack` posts each pending write to a channel with Approve and Deny buttons. It connects
  out over Socket Mode, so Lokra needs no public URL. See [Slack](https://lokra.dev/docs/slack).
- [Lokra Cloud](https://cloud.lokra.dev) is one inbox for all your gates, with phone approvals and
  team roles. `LOKRA_CLOUD_TOKEN=lkg_... lokra cloud` sends it masked SQL, counts and the ledger,
  never query results. It's free to start.

## Docs

[Quick start](https://lokra.dev/docs/quickstart) ·
[Policies](https://lokra.dev/docs/policies) ·
[Row locks](https://lokra.dev/docs/row-locks) ·
[Approvals](https://lokra.dev/docs/approvals) ·
[Masking](https://lokra.dev/docs/masking) ·
[Policy reference](https://lokra.dev/docs/policy-reference) ·
[Troubleshooting](https://lokra.dev/docs/troubleshooting)

## Contributing

Issues and pull requests are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) to set up the lab
database and run the tests. Report security issues privately, as described in [SECURITY.md](SECURITY.md).

## License

Apache-2.0. See [LICENSE](LICENSE).
