execute.
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.
Check the grant and wait for an answer
If the user has no grant, create a wait token tagged with their ID, and tell the user what the task wants to do by email, Slack or your app. The run suspends until the token completes or times out. If the user chose “always allow”, the task saves the grant from the answer it received.trigger/send-digest.ts
wait.forToken returns ok: false when the token times out, so a request nobody answers counts as not approved.
Complete the token from your backend
Your approval page posts the user’s answer to a route like this one. It checks that the token is waiting on the signed-in user, then completes it with the answer:app/api/approve/route.ts
Let users revoke grants
List the user’s grants on a settings page, one row per grant, with a revoke button that callsrevokeGrant for the signed-in user. Revoking gmail:send leaves a gmail:* grant in place, so show both. The next run after a revoke asks again.
Verify it
- Trigger
send-digestfor a user with no grant. Confirm the run waits and the user gets a request. - As that user, post
{ tokenId, approved: false }to/api/approve. Confirm the run returns{ sent: false }and nothing is sent. - Trigger it again and post
{ tokenId, approved: true, always: true }. Confirm agmail:sendrow appears inToolGrantand the email sends. - Trigger it once more. Confirm it sends without asking.
- Revoke the grant and trigger it again. Post the new token ID as a different user, and confirm the route returns 404 and the run keeps waiting. Then post it as the owner and confirm the email sends.
Common mistakes
- Completing any token ID you’re sent. Check that the token is tagged for the signed-in user and still waiting, or one user can approve another user’s action.
- Sending the token URL in a notification. Anyone with the URL can complete the token. Link to your own app instead.
- An idempotency key your send function ignores. A retried run can send the same email twice, and Gmail doesn’t deduplicate sends. Key it on the run ID, and skip a key you’ve already sent.
- Grants with no revoke path. Give users a page where they can see and remove their grants.
Next steps
In a chat
Ask for permission inside a chat with approval buttons.
Wait for token
Pause any task until your app or an external service completes a token.
Slack approvals
Approve and Deny buttons for agents running in Slack.
Tool permissions
How connections, grants and approvals fit together.

