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/schema.prisma
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:
lib/tool-scopes.ts
lib/permissions.ts
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/schema.prisma
app/actions.ts
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 fromclientData, because the browser can set clientData to anything.
- AI SDK 7
- AI SDK 6
Pass a Calls that a grant approved are marked
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.trigger/my-chat.ts
approval.isAutomatic: true on the message, so you can see which calls ran without asking.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, one hour by default), the answer starts a continuation run that restores the conversation from transcript storage and sends the email.
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 throughuseChat and Trigger.dev’s chat transport. 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:
app/components/chat.tsx
app/components/approval.tsx
app/actions.ts:
app/actions.ts
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. Revokinggmail: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.
app/actions.ts
Verify it
- Ask the agent to send an email. Confirm the approval buttons appear and nothing is sent yet.
- Click Deny. Confirm
executedoesn’t run and the agent replies without retrying. - Ask again and click Always allow this. Confirm a
gmail:sendrow appears inToolGrantand the email sends. - Ask for another email. Confirm it sends with no approval prompt.
- 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
Outside a chat
Ask for permission from scheduled jobs and background agents.
Human-in-the-loop
Questions, selections and approvals inside a chat.
Frontend
Connect
useChat to your agent with the chat transport.Tools
Declare tools on the agent and build them per turn.

