// MAA quickstart: the official Anthropic SDK pointed at MAA. // Run: MAA_BASE_URL=http://localhost:8787 MAA_API_KEY=... npx tsx docs/guide/examples/quickstart.ts import Anthropic from "@anthropic-ai/sdk"; const client = new Anthropic({ apiKey: process.env.MAA_API_KEY, baseURL: process.env.MAA_BASE_URL ?? "http://localhost:8787", }); // 1. Create the agent once and keep its id. const agent = await client.beta.agents.create({ name: "Helper", model: "claude-opus-5-5", system: "You are a careful assistant. Be brief.", tools: [{ type: "agent_toolset_20260401" }], } as any); // 2. Create an environment: the sandbox template sessions run in. const environment = await client.beta.environments.create({ name: "quickstart", config: { type: "cloud", networking: { type: "limited", allowed_hosts: ["example.com"] } }, } as any); // 3. Start a session. One session is one run. const session = await client.beta.sessions.create({ agent: agent.id, environment_id: environment.id, title: "Quickstart", } as any); console.log("session", session.id, (session as any).residency?.inference); // 4. Send a message, then wait for the agent to finish its turn. await client.beta.sessions.events.send(session.id, { events: [{ type: "user.message", content: [{ type: "text", text: "Write a hello file please." }] }], } as any); for (let i = 0; i < 300; i++) { const s: any = await client.beta.sessions.retrieve(session.id); if (s.stop_reason?.type === "end_turn") break; await new Promise((r) => setTimeout(r, 500)); } // 5. Read what happened. for await (const e of client.beta.sessions.events.list(session.id)) { const ev: any = e; if (ev.type === "agent.message") console.log("agent:", ev.content?.map((b: any) => b.text ?? "").join("")); if (ev.type === "agent.tool_use") console.log("tool:", ev.name); } // 6. Delete the session. MAA returns a signed certificate of deletion. const deleted: any = await (client as any).delete(`/v1/sessions/${session.id}`); console.log("certificate", deleted.certificate?.id); const base = process.env.MAA_BASE_URL ?? "http://localhost:8787"; const check = await (await fetch(`${base}/v1/certificates/${deleted.certificate.id}/verify`)).json(); console.log("certificate valid:", check.valid);