Give Mastra agents long-term memory with CortexDB tools.
Mastra Integration
Mastra is a TypeScript framework for building agents with tools, workflows, and observability. Use the cortexdbai SDK to give Mastra agents persistent memory across runs.
Install
npm install cortexdbai @mastra/core
Recall + capture as Mastra tools
import { Agent, createTool } from "@mastra/core";
import { V1Client } from "cortexdbai/v1";
import { z } from "zod";
const cortex = new V1Client({
apiUrl: "https://api-v1.cortexdb.ai",
actor: "user:alice",
bearer: process.env.CORTEX_TOKEN!,
});
const SCOPE = "org:acme/user:alice";
const recall = createTool({
id: "recall_long_term",
description: "Recall prior context about the user from long-term memory.",
inputSchema: z.object({ query: z.string() }),
execute: async ({ context }) => {
const pack = await cortex.recall(SCOPE, {
view: "holistic",
query: context.query,
include: ["beliefs", "facts", "episodes"],
budgets: { max_tokens: 3000 },
});
return pack.context_block || "(no relevant context)";
},
});
const capture = createTool({
id: "capture_experience",
description: "Capture a memory of the current conversation turn.",
inputSchema: z.object({ text: z.string(), role: z.enum(["user","assistant"]) }),
execute: async ({ context }) => {
await cortex.experience(SCOPE, {
modality: "conversation",
content: { kind: "message", role: context.role, text: context.text },
context: { observed_at: new Date().toISOString() },
idempotency_key: `mastra-${context.role}-${crypto.randomUUID()}`,
});
return { ok: true };
},
});
export const memoryAgent = new Agent({
name: "memory-agent",
instructions: "Recall prior context before answering and capture the conversation after.",
tools: { recall, capture },
});