What it does
This example demonstrates the @grantex/anthropic integration by building scope-enforced tools for Claude:
- Register an agent and obtain a grant token via the sandbox flow
- Create scoped tools using
createGrantexTool with JSON Schema (calendar:read, email:send)
- Use
GrantexToolRegistry to manage tools and dispatch tool_use blocks
- Invoke tools via
client.messages.create() (if ANTHROPIC_API_KEY is set) or directly
- Demonstrate
GrantexScopeError — attempting to execute a tool with storage:delete (a scope not in the grant) throws a GrantexScopeError with requiredScope and grantedScopes properties
- Inspect scopes offline using
getGrantScopes() to decode the JWT without a network call
- 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 Claude for real tool use instead of direct invocation, set ANTHROPIC_API_KEY:
Expected output
Without ANTHROPIC_API_KEY (direct invocation):
Environment variables
Source code
The full source is in examples/anthropic-tool-use/src/index.ts.Last modified on March 30, 2026