Skip to main content

What it does

This example demonstrates the @grantex/langchain integration by building a LangChain agent with Grantex-scoped tools:
  1. Register an agent and obtain a grant token via the sandbox flow
  2. Create scoped tools using createGrantexTool — each tool is bound to a specific scope (calendar:read, email:send)
  3. Attach a GrantexAuditHandler that automatically logs every tool invocation to the Grantex audit trail
  4. Invoke tools directly (or via an LLM if OPENAI_API_KEY is set)
  5. Demonstrate scope enforcement — attempting to create a tool with account:delete (a scope not in the grant) throws immediately
  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 for tool selection instead of direct invocation, set OPENAI_API_KEY:

Expected output

Environment variables

Source code

The full source is in examples/langchain-agent/src/index.ts.
Last modified on February 28, 2026