Kavachos: Agent and Human Authentication OS

0.4.2 · active · verified Wed Apr 22

Kavachos is a comprehensive authentication and authorization library designed for both human users and, uniquely, AI agents. It provides identity management, fine-grained permissions, delegation capabilities, and an immutable audit trail tailored for the 'agentic era'. The current stable version is 0.4.2, with rapid iterative releases addressing features and fixes, as seen by the frequent minor and patch updates between 0.3.0 and 0.4.2. A key differentiator is its dual focus on AI agent identity (cryptographic bearer tokens, wildcard permissions, delegation chains) alongside robust human authentication (14 methods, 27+ OAuth providers, passkeys, SSO). It also functions as a spec-compliant OAuth 2.1 authorization server for the Model Context Protocol (MCP) and is designed to be edge-compatible, running on platforms like Cloudflare Workers, Deno, Bun, and Node.js with a minimal runtime dependency footprint.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates initializing Kavachos with an SQLite database and email/password plugin, creating an AI agent with specific permissions, and then performing an authorization check for that agent.

import { createKavach } from "kavachos";
import { emailPassword } from "kavachos/auth";

async function runKavachExample() {
  const kavach = createKavach({
    database: { provider: "sqlite", url: "kavach.db" },
    plugins: [emailPassword()],
  });

  // Ensure the database is initialized (implementation detail not in quickstart, but necessary for a runnable example)
  // In a real app, you'd likely have migrations or a setup script.
  // For this example, we'll assume the 'kavach.db' file exists or is created by the library.

  // Create an AI agent with scoped permissions
  const agent = await kavach.agent.create({
    ownerId: "user-123", // This would typically be a human user's ID
    name: "github-reader",
    type: "autonomous",
    permissions: [
      { resource: "mcp:github:*", actions: ["read"] },
      { resource: "mcp:deploy:production", actions: ["execute"],
        constraints: { requireApproval: true } }
    ]
  });

  console.log(`Created agent: ${agent.name} with ID ${agent.id}`);

  // Authorize and audit (< 1ms)
  const result = await kavach.authorize(agent.id, {
    action: "read",
    resource: "mcp:github:repos"
  });

  console.log(`Authorization result for 'read mcp:github:repos':`, result);
  // Expected output: { allowed: true, auditId: "aud_..." } if permissions are correctly configured

  const unauthorizedResult = await kavach.authorize(agent.id, {
    action: "write",
    resource: "mcp:github:repos"
  });
  console.log(`Authorization result for 'write mcp:github:repos':`, unauthorizedResult);
  // Expected output: { allowed: false, auditId: "aud_..." }
}

runKavachExample().catch(console.error);

view raw JSON →