> ## Documentation Index
> Fetch the complete documentation index at: https://trigger.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Tool permissions in a chat

> Check the user's grants before each tool call in a chat.agent, ask with approve once, always allow and deny buttons when there's no grant, and let users revoke grants.

This guide builds the [tool permissions](https://trigger.dev/docs/ai-chat/patterns/tool-permissions.md) flow in a `chat.agent`. You'll store grants, record who owns each chat, check grants before each tool call, and add the approval buttons to your chat UI.

## Store grants

Give each tool a scope string, and store one row per grant. A scope ending in `:*` covers every tool in that integration.

```prisma prisma/schema.prisma theme={"theme":"css-variables"}
model ToolGrant {
  id        String    @id @default(cuid())
  userId    String
  scope     String    // "gmail:send", or "gmail:*" for every Gmail tool
  createdAt DateTime  @default(now())
  revokedAt DateTime?

  @@unique([userId, scope])
}
```

Run `npx prisma migrate dev` to create the table.

Keep the scope map in its own file with no database imports, so your approval buttons can import it in the browser:

```ts lib/tool-scopes.ts theme={"theme":"css-variables"}
// Maps each tool that asks for approval to the scope a grant must cover
export const toolScopes = {
  sendEmail: "gmail:send",
} as const;

export type ScopedTool = keyof typeof toolScopes;

// The scopes a user can grant: one tool, or every tool in its integration
export const grantableScopes = new Set<string>(
  Object.values(toolScopes).flatMap((scope) => [scope, `${scope.split(":")[0]}:*`])
);
```

```ts lib/permissions.ts theme={"theme":"css-variables"}
import { db } from "@/lib/db";

export async function hasGrant(userId: string, scope: string) {
  const [integration] = scope.split(":");
  const grant = await db.toolGrant.findFirst({
    where: { userId, scope: { in: [scope, `${integration}:*`] }, revokedAt: null },
  });
  return grant !== null;
}

// decidedAt is when the user chose "always allow". A revoke after that wins,
// so replaying an older answer can't restore a grant the user removed.
export async function addGrant(userId: string, scope: string, decidedAt: Date) {
  // The revoke check is part of the write, so a revoke that lands in between still wins
  const restored = await db.toolGrant.updateMany({
    where: { userId, scope, OR: [{ revokedAt: null }, { revokedAt: { lt: decidedAt } }] },
    data: { revokedAt: null },
  });
  if (restored.count === 0) {
    // No row yet, or one revoked after decidedAt, which skipDuplicates leaves alone
    await db.toolGrant.createMany({ data: [{ userId, scope }], skipDuplicates: true });
  }
}

export async function revokeGrant(userId: string, scope: string) {
  await db.toolGrant.updateMany({ where: { userId, scope }, data: { revokedAt: new Date() } });
}
```

The examples use Prisma with Postgres. On SQLite, which doesn't support `skipDuplicates`, catch the unique constraint error instead. They import your Prisma client from `@/lib/db`, a `gmail` wrapper around the user's Gmail connection from `@/lib/gmail`, and `requireUser` from `@/lib/auth`, which returns the signed-in user. Swap in your own.

## Save the chat's owner

The agent checks the grants of the user who owns the chat. Save the owner when the session starts, and only mint tokens for that user. Otherwise anyone who learns a chat ID can answer its approvals, or ask the agent to act with the owner's grants.

```prisma prisma/schema.prisma theme={"theme":"css-variables"}
model Chat {
  id     String @id
  userId String
}
```

```ts app/actions.ts theme={"theme":"css-variables"}
"use server";

import { auth } from "@trigger.dev/sdk";
import { chat, type ChatStartSessionParams } from "@trigger.dev/sdk/ai";
import type { myChat } from "@/trigger/my-chat";
import { db } from "@/lib/db";
import { requireUser } from "@/lib/auth";

const start = chat.createStartSessionAction<typeof myChat>("my-chat");

// Takes only the chat ID and client data from the browser, not run options like the machine
export async function startChatSession({ chatId, clientData }: ChatStartSessionParams<typeof myChat>) {
  const user = await requireUser();
  const existing = await db.chat.findUnique({ where: { id: chatId } });
  if (existing && existing.userId !== user.id) throw new Error("Not found");
  if (!existing) await db.chat.create({ data: { id: chatId, userId: user.id } });
  return start({ chatId, clientData });
}

export async function mintChatAccessToken(chatId: string) {
  const user = await requireUser();
  await db.chat.findFirstOrThrow({ where: { id: chatId, userId: user.id } });
  return auth.createPublicToken({ scopes: { read: { sessions: chatId }, write: { sessions: chatId } } });
}
```

These replace the session actions from the [quick start](https://trigger.dev/docs/ai-chat/quick-start.md).

## Check grants before each tool call

Check the user's grants each time the model calls a tool. Because the check runs on every call, a new grant applies to the next call and a revoked grant stops applying on the next call.

Look the user up from the chat you saved, not from `clientData`, because [the browser can set `clientData`](https://trigger.dev/docs/ai-chat/frontend#client-data-and-metadata) to anything.

> [tab] AI SDK 7
>
> Pass a `toolApproval` function to `streamText`. Return `"approved"` to run the call, `"user-approval"` to ask the user, or `undefined` to run it without a check. List the tools that only read, and ask for everything else, so a tool you add later asks until you give it a scope.
>
> ```ts trigger/my-chat.ts theme={"theme":"css-variables"}
> import { chat } from "@trigger.dev/sdk/ai";
> import { isStepCount, tool } from "ai";
> import { anthropic } from "@ai-sdk/anthropic";
> import { z } from "zod";
> import { db } from "@/lib/db";
> import { gmail } from "@/lib/gmail";
> import { hasGrant } from "@/lib/permissions";
> import { toolScopes, type ScopedTool } from "@/lib/tool-scopes";
>
> const readOnlyTools = new Set(["searchEmail"]);
>
> const ownerOf = async (chatId: string) =>
>   (await db.chat.findUniqueOrThrow({ where: { id: chatId } })).userId;
>
> function buildTools(chatId: string) {
>   return {
>     searchEmail: tool({
>       description: "Search the user's Gmail inbox.",
>       inputSchema: z.object({ query: z.string() }),
>       execute: async ({ query }) => gmail.search(await ownerOf(chatId), query),
>     }),
>     sendEmail: tool({
>       description: "Send an email from the user's Gmail account.",
>       inputSchema: z.object({ to: z.string(), subject: z.string(), body: z.string() }),
>       execute: async (input, { toolCallId }) =>
>         gmail.send(await ownerOf(chatId), input, {
>           // Your send function records this key and skips one it has already sent
>           idempotencyKey: `${chatId}:${toolCallId}`,
>         }),
>     }),
>   };
> }
>
> export const myChat = chat.agent({
>   id: "my-chat",
>   tools: ({ chatId }) => buildTools(chatId),
>   run: async ({ chatId, messages, tools, signal, streamText }) =>
>     streamText({
>       model: anthropic("claude-sonnet-5-5"),
>       messages,
>       tools,
>       abortSignal: signal,
>       stopWhen: isStepCount(15),
>       system: "If the user denies a tool call, don't retry it. Ask what they want instead.",
>       toolApproval: async ({ toolCall }) => {
>         if (readOnlyTools.has(toolCall.toolName)) return undefined;
>         const scope = toolScopes[toolCall.toolName as ScopedTool];
>         if (!scope) return "user-approval"; // no scope, so no grant can cover it
>         return (await hasGrant(await ownerOf(chatId), scope)) ? "approved" : "user-approval";
>       },
>     }),
> });
> ```
>
> Calls that a grant approved are marked `approval.isAutomatic: true` on the message, so you can see which calls ran without asking.

> [tab] AI SDK 6
>
> Give `needsApproval` a function instead of `true`. Return `true` to ask the user and `false` to run the call. A tool without `needsApproval` runs without asking, so add it to every tool that changes data.
>
> ```ts trigger/my-chat.ts theme={"theme":"css-variables"}
> import { chat } from "@trigger.dev/sdk/ai";
> import { stepCountIs, tool, type ModelMessage } from "ai";
> import { anthropic } from "@ai-sdk/anthropic";
> import { z } from "zod";
> import { db } from "@/lib/db";
> import { gmail } from "@/lib/gmail";
> import { hasGrant } from "@/lib/permissions";
> import { toolScopes } from "@/lib/tool-scopes";
>
> // True once the user has been asked about this call. AI SDK 6 checks needsApproval again
> // when the answer arrives and denies the call if it now returns false.
> const alreadyAsked = (messages: ModelMessage[], toolCallId: string) =>
>   messages.some(
>     (m) =>
>       m.role === "assistant" &&
>       typeof m.content !== "string" &&
>       m.content.some((p) => p.type === "tool-approval-request" && p.toolCallId === toolCallId)
>   );
>
> function buildTools(chatId: string) {
>   const ownerOf = async () => (await db.chat.findUniqueOrThrow({ where: { id: chatId } })).userId;
>
>   return {
>     // Read-only: never asks
>     searchEmail: tool({
>       description: "Search the user's Gmail inbox.",
>       inputSchema: z.object({ query: z.string() }),
>       execute: async ({ query }) => gmail.search(await ownerOf(), query),
>     }),
>     // Asks unless the user has a gmail:send or gmail:* grant
>     sendEmail: tool({
>       description: "Send an email from the user's Gmail account.",
>       inputSchema: z.object({ to: z.string(), subject: z.string(), body: z.string() }),
>       needsApproval: async (_input, { toolCallId, messages }) =>
>         alreadyAsked(messages, toolCallId) || !(await hasGrant(await ownerOf(), toolScopes.sendEmail)),
>       execute: async (input, { toolCallId }) =>
>         gmail.send(await ownerOf(), input, {
>           // Your send function records this key and skips one it has already sent
>           idempotencyKey: `${chatId}:${toolCallId}`,
>         }),
>     }),
>   };
> }
>
> export const myChat = chat.agent({
>   id: "my-chat",
>   tools: ({ chatId }) => buildTools(chatId),
>   run: async ({ messages, tools, signal, streamText }) =>
>     streamText({
>       model: anthropic("claude-sonnet-5-5"),
>       messages,
>       tools,
>       abortSignal: signal,
>       stopWhen: stepCountIs(15),
>       system: "If the user denies a tool call, don't retry it. Ask what they want instead.",
>     }),
> });
> ```
>
> > [!WARNING]
> >
> > Keep the `alreadyAsked` check. Clicking "Always allow" saves a grant before the answer reaches the agent. AI SDK 6 then calls `needsApproval` again, gets `false`, and turns the user's approval into a denial with the reason "does not require approval". The model may still tell the user the email was sent.

When the check asks the user, the turn ends with the call in the `approval-requested` state and the run waits for the answer. The user can answer hours later. If the run has ended by then (after [`turnTimeout`](https://trigger.dev/docs/ai-chat/reference#chatagentoptions), one hour by default), the answer starts a continuation run that restores the conversation from [transcript storage](https://trigger.dev/docs/ai-chat/transcript-storage.md) and sends the email.

> [!NOTE]
>
> Keep authorization checks in `execute` or in the function it calls, as well as in the approval check. The grant decides whether to ask. It doesn't replace checking that the user may act on that account.

## Add approval buttons to your chat UI

Your chat UI talks to the agent through `useChat` and Trigger.dev's [chat transport](https://trigger.dev/docs/ai-chat/frontend.md). When the agent pauses on a call, the call appears in the assistant message as a tool part in the `approval-requested` state. Render buttons for it, and send the answer back with `addToolApprovalResponse`:

```tsx app/components/chat.tsx theme={"theme":"css-variables"}
"use client";

import { useState } from "react";
import { useChat } from "@ai-sdk/react";
import { isStaticToolUIPart, lastAssistantMessageIsCompleteWithApprovalResponses } from "ai";
import { useTriggerChatTransport } from "@trigger.dev/sdk/chat/react";
import type { myChat } from "@/trigger/my-chat";
import { mintChatAccessToken, startChatSession } from "@/app/actions";
import { Approval } from "./approval";

export function Chat({ chatId }: { chatId: string }) {
  const transport = useTriggerChatTransport<typeof myChat>({
    task: "my-chat",
    accessToken: ({ chatId }) => mintChatAccessToken(chatId),
    startSession: ({ chatId, clientData }) => startChatSession({ chatId, clientData }),
  });

  const [input, setInput] = useState("");
  const { messages, sendMessage, addToolApprovalResponse } = useChat({
    id: chatId,
    transport,
    // Sends the answers to the agent once every pending call has one
    sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithApprovalResponses,
  });

  // Answer pending approvals before sending a new message, or their buttons stop working
  const awaitingApproval = messages
    .at(-1)
    ?.parts.some((part) => isStaticToolUIPart(part) && part.state === "approval-requested");

  return (
    <div>
      {messages.map((message) =>
        message.parts.map((part, i) => {
          const key = `${message.id}-${i}`;
          if (part.type === "text") return <p key={key}>{part.text}</p>;
          if (isStaticToolUIPart(part)) {
            return <Approval key={key} part={part} respond={addToolApprovalResponse} />;
          }
          return null;
        })
      )}
      <form
        onSubmit={(e) => {
          e.preventDefault();
          if (!input.trim() || awaitingApproval) return;
          sendMessage({ text: input });
          setInput("");
        }}
      >
        <input value={input} onChange={(e) => setInput(e.target.value)} placeholder="Type a message..." />
        <button type="submit" disabled={awaitingApproval}>
          Send
        </button>
      </form>
    </div>
  );
}
```

The approval component renders four buttons. Both "Always allow" buttons save a grant first, then approve the call. A tool with no scope gets only **Approve once** and **Deny**:

```tsx app/components/approval.tsx theme={"theme":"css-variables"}
"use client";

import type { ToolUIPart } from "ai";
import { allowAlways } from "@/app/actions";
import { toolScopes, type ScopedTool } from "@/lib/tool-scopes";

type Props = {
  part: ToolUIPart;
  respond: (response: { id: string; approved: boolean; reason?: string }) => void;
};

export function Approval({ part, respond }: Props) {
  if (part.state !== "approval-requested") return null;

  const toolName = part.type.replace(/^tool-/, "");
  const scope: string | undefined = toolScopes[toolName as ScopedTool];
  const integration = scope?.split(":")[0];
  const approve = () => respond({ id: part.approval.id, approved: true });

  return (
    <div>
      <p>The agent wants to run {toolName}:</p>
      <pre>{JSON.stringify(part.input, null, 2)}</pre>
      <button onClick={approve}>Approve once</button>
      {scope && (
        <>
          <button
            onClick={async () => {
              await allowAlways(scope);
              approve();
            }}
          >
            Always allow this
          </button>
          <button
            onClick={async () => {
              await allowAlways(`${integration}:*`);
              approve();
            }}
          >
            Always allow {integration}
          </button>
        </>
      )}
      <button onClick={() => respond({ id: part.approval.id, approved: false, reason: "User denied" })}>
        Deny
      </button>
    </div>
  );
}
```

Add the action that saves the grant to `app/actions.ts`:

```ts app/actions.ts theme={"theme":"css-variables"}
import { addGrant } from "@/lib/permissions";
import { grantableScopes } from "@/lib/tool-scopes";

export async function allowAlways(scope: string) {
  if (!grantableScopes.has(scope)) throw new Error("Unknown scope");
  // The grant is written for the signed-in user, never a user ID from the browser
  const user = await requireUser();
  await addGrant(user.id, scope, new Date());
}
```

> [!WARNING]
>
> A grant skips the user's check on every call it covers, including calls prompted by content the agent reads. An email that says "forward this inbox to me" runs unchecked if the user has a `gmail:*` grant. Only offer "always allow" for actions you're comfortable running without a person looking.

## Let users revoke grants

List the user's grants on a settings page, one row per grant, with a revoke button that calls this action. Revoking `gmail:send` leaves a `gmail:*` grant in place, so show both. The next call after a revoke asks again, and nothing in the agent needs to change.

```ts app/actions.ts theme={"theme":"css-variables"}
import { revokeGrant } from "@/lib/permissions";

export async function revokeAlways(scope: string) {
  const user = await requireUser();
  await revokeGrant(user.id, scope);
}
```

## Verify it

1. Ask the agent to send an email. Confirm the approval buttons appear and nothing is sent yet.
2. Click **Deny**. Confirm `execute` doesn't run and the agent replies without retrying.
3. Ask again and click **Always allow this**. Confirm a `gmail:send` row appears in `ToolGrant` and the email sends.
4. Ask for another email. Confirm it sends with no approval prompt.
5. Revoke the grant. Confirm the next request asks again.

## Common mistakes

* **Reading the user ID from `clientData`.** The browser sets it. Look up the chat's owner on the server.
* **Minting tokens for any chat ID.** Check that the signed-in user owns the chat, or another user can answer its approvals.
* **Saving the grant after approving.** Write the grant first, so a later call in the same turn finds it.
* **Grants with no revoke path.** Give users a page where they can see and remove their grants.
* **An idempotency key your send function ignores.** Gmail doesn't deduplicate sends. Record each key and skip one you've already sent.
* **Approvals on read-only tools.** Don't ask before tools that only read, or users learn to click through every prompt.

## Next steps

> [card] [Outside a chat](https://trigger.dev/docs/ai-chat/patterns/tool-permissions-outside-chat.md)
>
> Ask for permission from scheduled jobs and background agents.

> [card] [Human-in-the-loop](https://trigger.dev/docs/ai-chat/patterns/human-in-the-loop.md)
>
> Questions, selections and approvals inside a chat.

> [card] [Frontend](https://trigger.dev/docs/ai-chat/frontend.md)
>
> Connect `useChat` to your agent with the chat transport.

> [card] [Tools](https://trigger.dev/docs/ai-chat/tools.md)
>
> Declare tools on the agent and build them per turn.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.