Docs Client library

Capabilities

packages/client/docs/CAPABILITIES.md on GitHub

Each call of a procedure operates with a capability. A capability has three parts: a clearance (the data that the call can see), the permitted side effects, and a deadline. A nested call gets the same capability or a narrower one. It does not get a wider one at any time.

The call policy controls which procedures a call can reach. A capability controls what those procedures can do. The design and its decisions are in CAPABILITIES-2026-10.md.

A host without a capability operates as before: the feature costs nothing until a host uses it.

Declare the effects of a procedure#

A procedure declares its effects in its metadata:

createProcedure()
  .path(["notes", "read"])
  .meta({ description: "Read a note", effects: { reads: ["fs"], writes: [] } })
  .input(z.object({ id: z.string() }))
  .output(z.string())
  .handler(async ({ id }) => readNote(id))
  .build();
FieldMeaningAbsent
readsThe resources whose state the output depends onNot known
writesThe resources that the procedure changes. []: it changes nothingNot known
destructiveIt can delete or overwrite data that it did not maketrue when it writes (as in MCP)
idempotentA second identical call changes nothing morefalse

The resource names are strings. Four names are standard:

NameThe resource
fsThe files of the host
networkSystems outside the host
processIt starts a program that its caller (or the code of the project) chooses: that program can do anything
registryThe procedure registry

An application adds its own names. The packages of this repository use these:

NameThe resource
docker, mongo, s3The service that a package wraps
jobsThe background processes that the host started (node.spawn, vitest.watch)
programsThe stored programs of @procedures/store
serverA server that a procedure starts or stops
collection:<name>One collection of createCollectionProcedures()

A procedure that starts one fixed program (git status starts git) does not declare process. It declares what that program reads and writes. Code of the project is not a fixed program: a test file, a build plugin, a package script and a git hook can do anything. So vitest.run, vite.build, pnpm.install and git.commit write process. Under a capability without process, the other git procedures start no program of the repository: no hook, no core.fsmonitor and no filter. The rules are in CAPABILITIES-2026-10.md.

withEffects(effects, procedures) gives one declaration to a group of procedures. PURE_EFFECTS (reads: [], writes: []) is the declaration of a procedure that only calculates.

A procedure without a declaration has unknown effects. A host with a restriction refuses it.

Declare the paths of a procedure#

A host can confine all its procedures to one folder: mark --fs-root, procedure-server --fs-root and MCP_FS_ROOT. A procedure that reaches files declares its path inputs in metadata.fsRoot:

.meta({
  effects: { reads: ["fs"], writes: ["fs"] },
  fsRoot: { cwd: "cwd", paths: ["dbPath"], required: ["dbPath"], values: [":memory:"] },
})
FieldMeaning
cwdThe input of the working folder. Without a value, the procedure gets the root.
pathsThe inputs that name a file or a folder. A relative path starts at the root. The handler gets the absolute path.
namesThe inputs that name a file relative to the working folder. The host examines them, and does not change them.
requiredThe inputs of paths whose default is outside the root. Under a root, a call must give them.
valuesThe values that are not paths, for example :memory:
refuseThe reason that a root cannot confine the procedure. Under a root, the host refuses it.
programTrue for a procedure that starts the program of its input (shell.run)

Under a root, the handler of a procedure with rules gets the root in ctx.fsRoot. A procedure that starts a program uses it to keep the program in the root. For example, git gets GIT_CEILING_DIRECTORIES. Without a root, ctx.fsRoot is absent.

A procedure without metadata.fsRoot gets a decision from its effects. The host refuses it when its effects are not known, name fs, or write process. The rules and the table of the procedures are in CAPABILITIES-2026-10.md.

Give a host a capability#

The host makes the capability from its authentication: a token, a session cookie, the user of a local CLI. The input, the metadata and an unauthenticated header do not give it.

// Only the procedures that change nothing
const policy = hostPolicy({ readOnly: true });

// A capability for each request, from what the transport authenticated
const policy = hostPolicy({
  capability: (request) => (sessions.get(request.metadata["token"]) === "owner" ? undefined : guest),
  clearanceOrder: ladder(["public", "shared", "private"]),
});

const server = new ProcedureServer(procedureServerOptions(policy, registry));

A Client takes a capability for the procedures that it starts in its own process: new Client({ transport, capability }). A page that starts the programs of its visitors gives a read-only one.

narrowCapability(current, request, order?) makes a capability: the meet of the clearances, the intersection of the permitted effects, and the earlier deadline.

Clearance and resources#

An application gives its types to the framework in one place:

declare module "@procedures/client" {
  interface Register {
    clearance: "public" | "shared" | "private";
    resources: { notes: NotesStore };
  }
}

Then ctx.capability.clearance and ctx.resources have these types in every handler. The host gives the resources for the capability of each invocation:

hostPolicy({
  capability: capabilityOfRequest,
  clearanceOrder: ladder(["public", "shared", "private"]),
  // A view of the store with only the notes of the clearance
  resources: (invocation) => ({ notes: notes.visibleTo(invocation.capability.clearance) }),
  // A second net: the output that leaves the host
  admitItem: (item, invocation) => levelOf(item) <= invocation.capability.clearance,
});

The output filter drops a refused stream item. A refused single value gives FORBIDDEN with the reason clearance. The filter can give a promise, so it can look up the clearance of a record. The filter examines only the output that leaves the host, not the results of nested calls.

Nested calls#

A nested call (ctx.client.call) gets the capability of its caller. A caller can ask for less:

await ctx.client.call(["notes", "list"], {}, { narrow: { clearance: "public" } });

An interceptor can narrow too: next({ capability: { effects: { writes: [] } } }). The framework calculates the result, so a request for more has no effect.

The admission examines each call of a host, each nested call of a data-driven procedure (client.chain and the other procedures that start refs, because their caller chose the refs), and each nested call with a narrowed capability. A procedure of code that declares its effects answers for a declared edge only. An entry of its metadata.calls names the callee, and the effects of the callee are in its declaration. The admission examines each other nested call, also each nested call of a caller without calls.

checkEffects(procedures) examines the same edges over the registry: a declaration must include the reads and the writes of each procedure in its calls. Two things are the claim of the caller. The first is what a program that it starts does (the process of a callee). The second is the flags destructive and idempotent, because they depend on the input that the caller gives.

Deadlines#

timeoutInterceptor({ ms }) (or hostPolicy({ timeoutMs })) sets the deadline of the capability. The nested calls see it in ctx.capability.deadline, and a call after it gives TIMEOUT. A remote call sends the remaining time in milliseconds (the metadata key procedure-timeout). The server can only make its own deadline earlier with it, so a difference between two clocks has no effect.

Retry and idempotency#

A failed try has one of three outcomes. The retry middleware uses the outcome and the effects to decide if the call goes again.

OutcomeExampleThe call goes again
The request did not leaveThe connection was refusedYes
The server answered with a retryable errorUNAVAILABLEYes
Indeterminate: the request left, and no answer cameA reset, a closed WebSocket, the timeout of one tryOnly an idempotent call
  • A call is idempotent when its procedure declares idempotent: true or writes: [].
  • createRetryMiddleware({ idempotent: idempotentIn(registry) }) reads the effects of the procedures that the client knows.
  • A call can also set retry: { idempotent: true } in its context.
  • Each try of one call sends the same key in the metadata (idempotency-key). ProcedureServer starts a keyed call to a procedure that is not idempotent one time.
  • A second try with the same key gets the result of the first try. If the first try still runs, the second try waits for it.
  • A keyed call continues when its caller goes away, so that the next try can get its result. Only its deadline and the stop of the server end it.
  • new ProcedureServer({ idempotency: false }) turns this off. The default keeps 1000 results for 10 minutes.

Errors and explanations#

CodeReasonHTTP
FORBIDDENeffects-undeclared: the procedure declares no effects, and the capability has a restriction403
FORBIDDENeffects-exceeded: the declared effects are more than the permitted effects403
FORBIDDENclearance: the output filter refused a single value403
FORBIDDENoutside-root: a path of the input is outside the root of the host, or the root cannot confine the procedure (confineToRoot() of @procedures/fs)403
FORBIDDENhost-not-allowed: the HTTP procedures refuse the host of a URL or of a redirect (createHttpModule({ allow }) of @procedures/http)403
TIMEOUTthe deadline passed504

The response metadata of a server has the reason. explainInvocation(path, capability, registry) gives the decision and its reason without a call.

MCP#

The MCP server gives each tool the hints of its effects: readOnlyHint, destructiveHint, idempotentHint and openWorldHint (open world: it reads or writes network, or writes process). A data-driven tool gets no hints, because its operands decide what it does. mark mcp --read-only (or MCP_READ_ONLY=1) serves only the tools that change nothing, and each call operates with that capability.