Add OpenLegion with a coding agent
Paste this into Claude Code, Cursor, Codex or another coding agent, in your Next.js app's folder. It does the Quickstart for you, then wires the agent to one real function of your app. It asks before anything it can't undo.
Add OpenLegion to this Next.js app, so each of our customers gets an AI assistant that works through our own functions.
Read these first, and follow them over anything you remember:
- https://platform.openlegion.ai/llms-full.txt (every page of the docs)
Then:
1. Run `npx openlegion init` here. It opens my browser to sign in: tell me when, and wait for me. Don't create or edit .env.local yourself; init writes it, unless git would commit it.
2. Read the files it wrote: openlegion/tools.ts, openlegion/template.ts, openlegion/session.ts, openlegion/client.ts and app/api/openlegion/[...route]/route.ts.
3. Replace currentSession() in openlegion/session.ts with this app's own auth: the signed-in user's id, and the id and name of the customer (account, team or organization) they belong to. Show me the change.
4. Find one function in this codebase that reads something about the signed-in customer (their account, orders, projects, or similar) and make it a tool in openlegion/tools.ts with defineTool: a description that says what it returns, a zod input, trust "tool_verified", effect "read", and a run that scopes every read by ctx.external_ref (our customer id). Add it to the template's tools in openlegion/template.ts. Don't make a tool that writes, sends or deletes without asking me first.
5. Start the app's dev server, then run `npx openlegion dev` in another terminal, and leave both running.
6. Tell me to open http://localhost:3000/agent (or the port the app uses) and ask a question the new tool answers.
Don't commit secrets, don't print keys, and don't change anything outside this app.
What it should take
About 10 minutes, the same as the quickstart by hand: most of it is step 3 and step 4, which depend on your codebase. The sign-in in step 1 needs you, once.
If it gets stuck
- "Almost there" on
/agent:npx openlegion devisn't running yet, so your template has no version. Start it, and reload. - The chat answers 401:
currentSession()returned null, so nobody is signed in as far as the route knows. Sign in to your app first, or check step 3. - The agent doesn't call the tool: check that its description says what it returns, because the model picks tools by their descriptions.
npx openlegion devprints every tool call it sees.