Skip to main content

What it does

This example demonstrates the @grantex/vercel-ai integration by building scope-enforced tools with the Vercel AI SDK:
  1. Register an agent and obtain a grant token via the sandbox flow
  2. Create scoped tools using createGrantexTool with Zod schemas for typed parameters (calendar:read, email:send)
  3. Wrap tools with withAuditLogging to automatically log every execution to the Grantex audit trail
  4. Invoke tools via generateText (if OPENAI_API_KEY is set) or directly
  5. Demonstrate GrantexScopeError — attempting to create a tool with storage:delete (a scope not in the grant) throws a GrantexScopeError with requiredScope and grantedScopes properties
  6. Inspect the audit trail to see the logged actions

Prerequisites

  • Node.js 18+
  • Docker (Docker Desktop or Docker Engine with Compose)

Run

Start the local Grantex stack from the repository root:
In a separate terminal, run the example:
To use a real LLM with generateText instead of direct tool invocation, set OPENAI_API_KEY:

Expected output

Without OPENAI_API_KEY (direct invocation):

Environment variables

Source code

The full source is in examples/vercel-ai-chatbot/src/index.ts.
Last modified on February 28, 2026