Skip to Content
Database

Agent

Per-user database tools for AI agents, safe by construction.

Usage

import { agentTools } from "@dapplelight/signet-db/agent"
import { createMcpServer } from "@dapplelight/signet-server/mcp"
import { notes, orders } from "./schema"
import { db } from "./db"
import { session } from "./session"

const tools = agentTools(db, {
  tables: [orders, notes],
  userId: session.userId,
  maxRows: 50,
})
createMcpServer({ name: "app", version: "1.0.0", tools }) // or call tool.run(input) yourself

Needs zod. Each table gets <table>_list, <table>_get and <table>_search. With allowWrites: true it also gets <table>_insert and <table>_update, which take dryRun: true.

API

ExportWhat it does
agentTools(db, options)Build the tool list for one user
jsonSchema(zodType)Zod to JSON Schema, dates as date-time strings
AgentTool{ name, description, annotations, input, inputSchema, run, handler }
AgentToolsOptionsOptions type

agentTools(db, options)

OptionTypeDefault
tablesTable[] | schema modulerequiredEach needs a userId column
userIdstringrequiredThe user the agent acts for
actorActor{ type: "agent", id: "agent" }Recorded in the audit log
allowWritesbooleanfalseAdd insert and update tools
maxRowsnumber100Most rows any call returns
includeEncryptedbooleanfalseReturn encrypted fields, decrypted

Guards

  • Scoped to userId; a table without a userId column is refused.
  • Soft-deleted rows are hidden; userId is never returned.
  • Inputs are strict Zod objects: unknown fields fail with BAD_INPUT.
  • Writes are audited as the agent actor.

Examples

Let the agent edit, after a dry run

import { agentTools } from "@dapplelight/signet-db/agent"
import { orders } from "./schema"
import { db } from "./db"
import { session } from "./session"

const orderId = "ord_1"

const tools = agentTools(db, {
  tables: [orders],
  userId: session.userId,
  allowWrites: true,
  actor: { type: "agent", id: "support-bot" },
})
const update = tools.find((t) => t.name === "orders_update")!
await update.run({ id: orderId, set: { item: "Idli" }, dryRun: true }) // { dryRun, before, after }