What it does
This example demonstrates the@grantex/langchain integration by building a LangChain agent with Grantex-scoped tools:
- Register an agent and obtain a grant token via the sandbox flow
- Create scoped tools using
createGrantexTool— each tool is bound to a specific scope (calendar:read,email:send) - Attach a
GrantexAuditHandlerthat automatically logs every tool invocation to the Grantex audit trail - Invoke tools directly (or via an LLM if
OPENAI_API_KEYis set) - Demonstrate scope enforcement — attempting to create a tool with
account:delete(a scope not in the grant) throws immediately - 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:OPENAI_API_KEY:
Expected output
Environment variables
| Variable | Default | Description |
|---|---|---|
GRANTEX_URL | http://localhost:3001 | Base URL of the Grantex auth service |
GRANTEX_API_KEY | sandbox-api-key-local | API key. Use a sandbox key for auto-approval |
OPENAI_API_KEY | (none) | Optional. Set to use a real LLM for tool selection |
Source code
The full source is inexamples/langchain-agent/src/index.ts.