Quickstart
An agent in your Next.js app, chatting in your UI and calling one of your own functions, in test mode. You need Node 22 or later, and a Next.js 16 app with the App Router and TypeScript (or an empty folder: you get a new app).
Set up with your coding agent
Run this in your app's folder:
npx openlegion setup
It connects your coding agent to OpenLegion and tells it what to do. Your agent does the rest, and asks before it changes anything.
To add OpenLegion's MCP server yourself:
Claude Code:
claude mcp add --scope project openlegion -- npx -y openlegion mcp
Cursor, in .cursor/mcp.json:
{
"mcpServers": {
"openlegion": {
"type": "stdio",
"command": "npx",
"args": [
"-y",
"openlegion",
"mcp"
]
}
}
}
Codex:
codex mcp add openlegion -- npx -y openlegion mcp
VS Code:
code --add-mcp '{"name":"openlegion","type":"stdio","command":"npx","args":["-y","openlegion","mcp"]}'
Then ask your coding agent:
Set up OpenLegion here: call the openlegion guide tool with "setup" and follow it.
Or follow the steps below yourself.
1. Add the kit
npx openlegion init
Run it in your app's folder, or as npx openlegion init my-app for a new app. It signs you in (your browser opens: approve the code your terminal shows), puts a test-mode key for your app in .env.local, installs @openlegion/platform, and writes:
app/api/openlegion/[...route]/route.ts: one route that is the chat stream, your webhook receiver and your tool server;openlegion/tools.ts: your functions, as the agent's tools;openlegion/template.ts: the agent, as code;openlegion/session.tsandopenlegion/client.ts: who is signed in, and which workspace is theirs;app/agent/page.tsx: a page with<AgentChat>.
It never overwrites a file without --force, and running it again changes nothing. npx runs the CLI without installing it, or from the project that installed it.
2. Run it
In one terminal, your app (npm run dev, or whatever init printed). In another:
npx openlegion dev
npx openlegion dev pushes your template to test mode and connects OpenLegion to your tools through this terminal: no tunnel, nothing listening on your machine. Then it prints each run and tool call.
The first npx openlegion dev writes your connector's secret into .env.local. next dev loads the new value by itself, and npx openlegion dev waits for it, for up to 30 seconds. An app that doesn't reload that file (one started with next start) is the exception: npx openlegion dev then tells you to restart your app; do that and run npx openlegion dev again.
If your app has auth middleware (middleware.ts or proxy.ts) over /api, exclude /api/openlegion from it. OpenLegion's calls to that route carry no session of yours: the route checks their signature itself, and your resolve decides who a browser caller is.
Open http://localhost:3000/agent and ask "What's my account?". The agent calls get_account, from openlegion/tools.ts:
export const getAccount = defineTool({
description: "Look up the signed-in customer's account in my-app: who they are.",
input: z.object({}),
trust: "tool_verified",
effect: "read",
// ctx.external_ref is the customer OpenLegion signed this call for: scope every read by it.
run: (_input, ctx) => ({ customer: ctx.external_ref, app: "my-app" }),
});
3. Make it yours
Replace the example with a call into your app (db here stands for your own data access), scoped to the customer the call is for, and save:
import { defineTool, refuse } from "@openlegion/platform";
import { z } from "zod";
export const getAccount = defineTool({
description: "Look up the signed-in customer's account: their plan, and when it renews.",
input: z.object({}),
trust: "tool_verified",
effect: "read",
run: async (_input, ctx) => {
const account = await db.accounts.find({ customer: ctx.external_ref });
return account ? { plan: account.plan, renews: account.renewsAt } : refuse("There is no account for this customer.");
},
});
Your app serves the tool, so the next message runs the new code. When a tool's description or input changes, npx openlegion dev re-pins your tools on save. A new tool goes in the template's tools (openlegion/template.ts) beside get_account, and npx openlegion dev pushes the new template.
An agent's schedules run in its workspace's time zone, which is UTC unless the workspace was created with one. openlegion/client.ts passes the customer's timeZone (the stand-in customer has this machine's): give your real customers theirs, so "every Monday at 9" is 9:00 where they are.
Next: How it works, then Give your customers an assistant that works in your product.