Install KavachOS, create your first AgentIdentity, and run an authorization check. Scaffold a full Next.js SaaS app with create-kavachos-app in one command.
The fastest path is the scaffolder. One command, three prompts, a running Next.js SaaS with KavachOS wired up.
pnpm
npm
yarn
bun
terminal
pnpm create kavachos-app
terminal
npm create kavachos-app
terminal
yarn create kavachos-app
terminal
bun create kavachos-app
The CLI asks for a directory, a template, a package manager, and a database driver. Only the Next.js SaaS template is shipping today; the Hono MCP and Expo templates print a coming-soon note and exit. Placeholders in the template (__APP_NAME__, __DB_DRIVER__, __DB_URL__) are replaced on copy.Next steps are printed at the end: cd, install, db:push, dev.Prefer to wire up an existing app? Keep reading.
The token is shown exactly once, at creation time. Store it immediately in your secrets manager or pass it directly to the agent. It cannot be recovered after this point, only rotated.
There are three agent types:
Type
When to use
autonomous
Runs without human involvement. Default for most agents.
delegated
Receives permissions from another agent via a delegation chain.
Call kavach.authorize before any sensitive operation. It returns { allowed, reason?, auditId }.
const result = await kavach.authorize(agent.id, { action: 'read', resource: 'mcp:github:repos',});if (!result.allowed) { throw new Error(`Denied: ${result.reason}`);}// result.auditId links this decision to its audit log entry
If you only have the raw bearer token (from an incoming HTTP request, for example), use authorizeByToken instead:
const result = await kavach.authorizeByToken(bearerToken, { action: 'read', resource: 'mcp:github:repos',});
An orchestrator agent can delegate a subset of its permissions to a sub-agent. The delegation has its own expiry and a maxDepth to prevent unbounded chains.
const sub = await kavach.agent.create({ ownerId: 'user-123', name: 'sub-reader', type: 'delegated', permissions: [], // starts empty; receives permissions via delegation});await kavach.delegate({ fromAgent: agent.id, toAgent: sub.id, permissions: [{ resource: 'mcp:github:issues', actions: ['read'] }], expiresAt: new Date(Date.now() + 3_600_000), // 1 hour maxDepth: 2,});// Resolves the full effective permission set, including delegated onesconst perms = await kavach.delegation.getEffectivePermissions(sub.id);
An agent cannot delegate permissions it does not hold itself. Attempts to escalate are rejected at the point of delegation, not at authorization time.
Run npx wrangler d1 execute kavach --file=./kavach-schema.sql to apply the schema, or set skipMigrations: false to let KavachOS run migrations on first boot.
createKavach is async when using D1. Workers and Deno both support top-level await, so you can also initialize outside the handler if you use a module worker.
The React hooks store sessions in localStorage. Make sure your app is wrapped in <KavachProvider>. If using SSR (Next.js), wrap the provider in a "use client" component.