AI coding agents (MCP)
pgkiln mcp is a Model Context Protocol server. With it an AI coding agent (Claude Code, Cursor, VS Code Copilot, Codex, …) can find an application, read and understand its pages and shared components and the tables they run on, and change the application: it exports the application as files, edits them with its own file tools, shows the difference, and imports them again. Laravel developers know the idea from Laravel Boost.
This is separate from the AI features inside applications and the builder (AI assistant regions, AI agents with tools in Shared Components, Generate with AI): those call a model from pgkiln; here an agent on the developer's machine calls pgkiln. An application's AI agents and tools are part of its files (app.json: ai_agents, ai_tools), so a coding agent can read and change them like any other component.
Setting it up
Claude Code, in a pgkiln checkout: nothing to do. The repository's .mcp.json starts the server; Claude Code asks once whether to trust it. Then ask, for example:
Which applications are there? Explain page 3 of hr: what happens when I press Save?
In hr, add a "Hire date" column to the employee report on page 2 and show me the diff before importing.
Claude Code, in your own project (where you keep your application files and migrations):
claude mcp add pgkiln -- /path/to/pgkiln/bin/pgkiln.js mcp
# another database than the checkout's .env:
claude mcp add pgkiln -e DATABASE_URL=postgres://owner@host/db -- /path/to/pgkiln/bin/pgkiln.js mcpWithout a checkout: the npm package has the same command, so npx runs it (Node.js 20 or later). Give it the owner connection of the database pgkiln runs on:
claude mcp add pgkiln -e DATABASE_URL=postgres://pgkiln:secret@localhost:5432/pgkiln -- npx -y pgkiln mcpPin a version (npx -y pgkiln@0.59.0 mcp) to match the server you run.
With the Docker installation (chapter 1) the container already has the connection; the agent starts the server inside it, from the directory with compose.yaml:
claude mcp add pgkiln -- docker compose -f /path/to/pgkiln/compose.yaml exec -T pgkiln node bin/pgkiln.js mcp(-T: no terminal, the protocol runs over standard input and output.) The database port stays closed to the host.
Other agents take the same command in their MCP configuration, e.g. for Cursor (.cursor/mcp.json) or VS Code (.vscode/mcp.json, key servers):
{ "mcpServers": { "pgkiln": { "command": "node", "args": ["/path/to/pgkiln/bin/pgkiln.js", "mcp"] } } }The server connects with DATABASE_URL (the owner role) from the environment or the checkout's .env, like the command line. Relative paths in the tools are relative to the directory the agent started the server in (the project).
Remote, with a builder token
(sprint 194, NX-F4) The setups above run on the server, with the owner connection: fine on your own machine against your own database, not for a shared or production pgkiln. There, give the agent a builder token instead: the MCP server runs on your machine and acts as you, through the builder's API, with the token's scopes, in one workspace, and no database connection at all.
- In the builder: your account menu → Security → Builder tokens: a name (Claude Code on my laptop), the workspace, the scopes (
readto look,writeto import,sqlforrun_query) and an expiry (30 days by default, at most a year). It is shown once: copy it. Making one needs a sign-in in the last 10 minutes with your second factor or single sign-on (chapter 3). - Give it to the agent in an environment variable and point the server at the builder:
claude mcp add pgkiln -e PGKILN_TOKEN=pgkb_… -- npx -y pgkiln mcp --url https://pgkiln.example.com
# another variable: --token-env MY_PGKILN_TOKEN--url must be https (plain http only to localhost); the token is never a command-line argument, so it stays out of ps and the shell history; keep it out of files in git. The tools are the same, with the same answers:
export_app,diff_app,import_app(andadvise_appwith apath) read and write files on your machine; they get the export from the server and send the import to it.search_docssearches the guide that came with your copy.- every other tool runs on the server: only the token's workspace's applications exist for it (another workspace's is no application);
describe_schema,generate_ddl,compare_schemasandrun_queryrun as the workspace's developer login (the SQL Workshop's identity), never the owner connection, also for an administrator's token;lock_appandworking_copyact as you, whateverdevelopersays. - a tool the token's scopes don't cover answers
Error: 403 …: this token has no write scope; an expired or revoked tokenError: 401 …, at once: revoking a token on the Security page stops the agent on its next call. import_apptakesonly(["pages/12"], withreplace): only that page or file changes, aspgkiln import --replace --only(chapter 18). An import keeps the builder's rules: another developer's lock on the application or a page it changes refuses it.
Every call is in the activity log under your name with the token's number and name, the tool, the application and the outcome (builder_api; refusals forbidden), so an agent's work can be told from yours: Security → Builder tokens → Calls lists them per token, and administrators see every developer's. A token also stays tied to the sign-in it was made after: when a workspace of yours starts to require single sign-on or you now need a second factor, a token made without it answers 403 until you make a new one (chapter 3).
Tools
| Tool | What it does | Changes anything |
|---|---|---|
pgkiln_info | version, database, last migration, where the guide is | no |
list_apps | the applications: id, alias, name, pages | no |
app_overview | an application's settings, its pages (mode, authorization, number of regions, items, processes) and the paths of all its component files | no |
get_page | every component of one page as YAML, SQL inline ((229) page 0: the Global Page) | no |
read_app_files | component files by path or prefix (shared/lovs/, navigation.yaml, …) | no |
describe_schema | schemas with tables, views and functions; or one table with columns, constraints, indexes, RLS policies and triggers | no |
generate_ddl | the DDL of a schema's objects, in an order that runs: a start for a migration script | no |
compare_schemas | the differences between two schemas and a script that makes the target like the source (not run) | no |
run_query | one SQL statement, read-only, rows as JSON; with alias (and user) as that application's role and user | no |
search_docs | searches this user guide, the parity matrix and SECURITY.md | no |
recent_errors | the latest errors and refused requests from the activity log | no |
export_app | writes an application to a directory (default apps/<alias>, format text) | files on disk |
diff_app | what an import of a directory would change (A/D/M with unified diffs); also reports files it can't read | no |
advise_app | (0.77) the Advisor on an application or an export (imported and rolled back): SQL planned as the application's role, references, security checks, each finding with its file; offline: true with a path checks the files without the database (0.79) | no |
working_copy | (0.77) list, create, compare, merge, refresh or delete working copies; conflicts need prefer (copy or main) or take (component ids); a merge is refused while another developer has a lock on it | the database |
lock_app | (0.77) locks an application or a page (unlock: true removes it; another developer's lock only with force: true, logged) | the database |
import_app | imports a directory, zip or JSON file; replace: true updates the application in place; (sprint 242) a key a file leaves out takes the column default, listed per file; (sprint 251) a refused import names the file, the component, the key and the allowed values. It checks the application first (the Advisor, in the import's transaction): errors refuse the import and are listed; force: true imports anyway, dry_run: true only checks | the database |
The files are the directory format of the command line, in the text style: one YAML file per component, with SQL, PL/pgSQL and templates inline, so a region and its query are one file an agent reads top to bottom (a sub region names its container with parent: <static id>). An import with replace is the same as the command line's: one transaction, users, sessions and saved reports kept, supporting objects and plug-in install SQL never run.
(sprint 242) A new or hand-written file may leave keys out: an import gives a key that is absent the column's default (debug: false, a page's protection: checksum and requires_auth: true, an item's seq: 10; see keys you leave out), and import_app lists the keys it filled per file. A key written as null stays null. Only keys without a default (a region's type, a list of values' query) must be there, and so must the keys whose default would widen access (a REST module's roles and enabled, a web credential's valid_for, the application's mfa, mfa_roles and local_login); when one is missing the import names the file, the component and the key, and changes nothing. diff_app compares a left-out key as its default, so a file that leaves out default values shows no change. Agents that change an exported file should still keep its keys: an existing application's value is only kept when the key is in the file.
The workflow an agent follows
list_apps,app_overview, thenget_pageorread_app_filesfor the parts the task is about;describe_schemafor the tables;search_docsbefore guessing a property or region type.export_apptoapps/<alias>, then edit the YAML files there.diff_app, so you (and it) see exactly what will change.import_appwithreplace: true. When it names errors (a missing column, a typo in SQL), the agent fixes the files and imports again; nothing was changed. Open the page in the browser to check.
(sprint 251) A refused import says where. A value of the wrong type (target_page: "&P11_BACK."), two items of one name on a page, a value a check doesn't allow (a process with a computation's point before_header) or a condition its type can't use comes back as the file, the component, the key, its value and the allowed values, every problem at once (when an import is refused); advise_app with offline: true finds the same without a database. A region's or button's SQL condition is condition, the other components' condition_expr: the import takes either key on any of them.
Tables, views and functions are ordinary PostgreSQL objects: an agent changes them in your migration scripts, not in the application files (run_query can't change them anyway).
Safety
- Remote: a builder token. With
--urlthe server on your machine has no database connection and can't do more than the token's scopes and your rights in its workspace, checked by the builder at every call (see above). The points below are about the local server. - Read-only queries.
run_queryruns exactly one statement (the extended query protocol, socommit; delete …is refused) in aREAD ONLYtransaction that has already taken a snapshot (soset transaction read writeis refused), with a 10-second timeout, and rolls it back. Values of columns whose names contain password, secret, token, hash or api_key are shown as(hidden). That keeps credentials out of the agent's context by accident; it is not an access control: the server runs as the owner role, like the command line, and is meant for a developer's own machine. - As an application's user. (sprint 119)
run_querywithaliasruns the statement as that application's database role, withmeta.app_id(),meta.app_user()(user, defaultnobody) andmeta.has_role()(the user's roles in the application) set, so grants, column privileges, row level security and masking views apply as on that user's pages (chapter 8). Use it to check what a user sees, or let an agent look at data the way the application does. It is no sandbox either: the connection is still the owner's. Don't point it at production; deploy withpgkiln import --replacefrom git instead. - Writes are only
export_app(files; it refuses a non-empty directory that isn't an export, so it can't overwrite your project) andimport_app(the database, in one transaction). Both are marked as such in the tool list (the others arereadOnlyHint), which clients use to decide what to confirm with you; Claude Code asks before every MCP tool it hasn't been allowed.import_appwithoutreplacenever touches an existing application. - Output is capped at 200,000 characters per call; ask for one page or a prefix instead of a whole large application.
- Diagnostics go to standard error; standard output carries only protocol messages.
Instructions for agents in your own project
Agents read CLAUDE.md (Claude Code) or AGENTS.md (most others). A few lines are enough:
## pgkiln applications
- The applications live in pgkiln (the `pgkiln` MCP server); their files are in `apps/<alias>/`.
- Read before changing: `app_overview`, `get_page`, `describe_schema`, `search_docs`.
- Change an application by editing `apps/<alias>/` (export_app first if it is missing or stale),
then `diff_app`, then `import_app` with replace=true. Never edit the meta tables with SQL.
- Database objects (tables, views, functions, RLS) change in `db/` migrations, not in the app files.For pgkiln developers
(sprint 194) Each tool's run(args, ctx) gets a ToolContext: LOCAL (the owner connection of DATABASE_URL, every application), the builder API's serverContext (src/builder/api-v1.ts: the owner pool for the token's workspace's applications only, the workspace's developer login for the database's objects), or, for the file tools under --url, RemoteClient.context() (exports and imports over the API). A new tool uses ctx.meta / ctx.sql and ctx.app(db, alias) instead of a connection of its own, and gets a scope in toolScope (api-v1.ts).
src/cli/mcp.ts holds the tools and the JSON-RPC loop (no SDK: newline-delimited JSON-RPC 2.0 on stdio, protocol versions 2025-06-18, 2025-03-26 and 2024-11-05). The tools reuse src/cli/apps.ts (export, read, import) and src/appfiles.ts, so they always match the command line. A new tool needs a JSON schema, readOnlyHint (or destructiveHint), and a test in test/mcp.test.ts; anything that takes SQL or paths also needs one in test/security.test.ts.