Adding a contract
A contract is a promise every provider keeps and every caller relies on, so add one only when none fits. New to the idea? Read How contracts work first.
Before you add one
In this order of preference:
- Use an existing contract. Search the catalogue. If a contract already promises what your extension needs, require it or work with it. If one already promises what your extension offers, provide that one: your extension then stands in for the current provider.
- Extend an existing contract. These leave its promise unchanged: provide it alongside others, for contracts that
take a chain (
mail.fallback/v1); decorate whatever provides it (Contract.Wrap); add or replacepocketonecommands throughcli.commands/v1; give it a dashboard screen (Contract.UI). If the promise itself has to change, addvN+1besidevN, reviewed with the contract's owners. A publishedvNnever changes what it promises. - Add a new contract only when none fits. Keep it small and name it for what it does (
restore/v1), never for your extension. A new optional ability next to an existing contract is a small companion contract (mail.suppression/v1besidebulkmail/v1), not a second copy of the contract with changes. In the commit message, name the contracts you checked and say why none fits.
Whoever first creates a contract owns it; an extension author who adds one owns both the contract and the extension that provides it. Ids are unique across PocketOne. Contracts are reviewed like PocketOne's core.
The folder
A contract is a folder ext/contracts/<id path>/vN/ (mail.fallback/v1 → mail/fallback/v1/) with:
-
the Go package: the interface, its
//contract:directives and the generatedzz_contract.go; -
a
README.md, its page on pocketone.dev, with front matter and four sections in this order:--- owners: ["@your-github-handle"] summary: "One line: what the contract promises." --- # mail.fallback/v1 ## Use how to call it: HTTP, Go, pb_hooks, MCP, CLI ## Provide how to implement it ## Extensions (generated: who provides, requires and works with it) ## License MIT
The catalogue table and each README's Extensions section are generated from the READMEs and every extension's
Contracts(): run go run ./.github/extensions contracts (CI fails when they're stale, or when an extension names a
contract that doesn't exist).
Pick its kind
- Service: a Go interface, looked up in the same process (
fallbackv1.Contract.Of(app)). - Wire: a service with an HTTP API under
/api/_c/<id>/, generated from its interface: each method is an operation with its own HTTP method and path (//op: POST restart), plus who may call it (see Access and scopes); inputs and results are JSON, uploads (extkit.RawIn), downloads (extkit.RawOut) or streams (extkit.Stream, sent as server-sent events: a GET or a POST, pushed as they happen withRunor polled withPoll). It gets action hooks (remotev1.OnRestart,onRemoteRestartinpb_hooks), an MCP tool per JSON operation and a client. Every caller (HTTP, MCP, Go) passes the same scope check and hooks. - Routed: a contract whose provider is an
http.Handlerserving everything under/api/_c/<id>/as it is, for an API a protocol shapes (OAuth) or an existing API moved under a contract (deploy/v1).
A multisite host can provide a wire or routed contract for its instances, so it works for instances that run stock PocketBase.
Write the interface
A contract is an interface with //contract: directives; contractgen writes the rest
(zz_contract.go) with go generate ./.... A test fails when a generated file is stale.
//go:generate go run pocketone.dev/ext/contracts/cmd/contractgen
// Deployer sends a project's code to an app.
//
//contract:id deploy/v1
//contract:kind wire
//contract:host
//contract:js hooks
type Deployer interface {
// Upload writes changed files to the app.
Upload(ctx context.Context, a extkit.Auth, in UploadInput) (UploadResult, error)
}
Methods take ctx, then (wire) extkit.Auth, then at most one input; they return error or one value and error.
The doc comment of a wire method is its MCP tool's description.
A contract pb_hooks can touch (//contract:js hooks or open) also gets zz_contract.d.ts, its TypeScript for
types.d.ts: each action hook typed with its operation's input (onExampleUpload(handler: (e: contracts.CallEvent<contracts.example.v1.UploadInput>) => void)), and for open the interface e.provide checks
(contracts.Providers["mail.fallback/v1"]). Hooks see the Go value, so fields carry Go names with the first letter
lowered (maxTokens); a JS provider gets JSON, so fields carry their json names, omitempty ones optional.
Access and scopes
A wire operation says what its API allows, nothing about MCP, OAuth or consent screens. Its //op: line is
METHOD [path] [access=...] [perm=...] [read]:
access=is who may call it at all:superuser(the default),signed-in(any auth record or token; the operation checks whose data it touches) oranyone(no credential, no scope: webhooks, unsubscribe links, checkout).- Its token scope is the contract's namespace: the id's name with dots and dashes as
_(mail.suppression/v1→mail_suppression).perm=<group>gives it a group of its own (perm=refunds→billing:refunds).//contract:perm <namespace>shares one namespace on purpose (thellm.*contracts sharellm). - A GET only reads;
readmarks a POST that only reads. A read operation also passes with the scope's:readhalf (mail_suppression:read), a write operation only with the scope itself, which covers its reads too.
Token services register the scopes from these declarations (<scope>, and <scope>:read when the group has reads);
MCP names the tools (<namespace>_<op>) and decides who sees them. Neither is declared in the contract.
After changing a contract interface, run go generate ./... in ext/contracts, then
go run ./.github/extensions contracts and go run ./.github/extensions check.
A dashboard screen
A contract can bring its own dashboard screen (Contract.UI(name, fsys)): it shows on every app that provides the
contract, whichever extension does, and calls only the contract's API. restore/v1's "Restore to a moment" works this
way.
Next
- Writing an extension: declare, provide and use your contract.
internal/example/v1: a test-only wire contract that exercises the generated code.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.