Skip to content

Agent frameworks

Mastra

@surrealdb/mastra-ai is the SurrealDB integration for Mastra. It ships two things that can be used separately or together: a storage adapter backed by a SurrealDB instance you run, and an SurrealDB Agent Memory memory provider backed by the hosted agent memory service.

  • Bun 1+ or Node.js 22+

  • @mastra/core 1.31.0+

  • For the storage adapter: SurrealDB v3

  • For SurrealDB Agent Memory: an endpoint, context, and API key

bun add @surrealdb/mastra-ai

The SurrealDB Agent Memory client ships with zod; install it alongside when you use that subpath:

bun add zod

AgentMemory works standalone, with no database required. Verbatim message history is kept in-process while SurrealDB Agent Memory handles fact extraction, semantic recall, and the user profile. Every SurrealDB Agent Memory call is guarded, so a service outage degrades to verbatim-only behaviour rather than breaking the agent loop:

import { Agent } from "@mastra/core/agent";
import { anthropic } from "@ai-sdk/anthropic";
import { AgentMemory } from "@surrealdb/mastra-ai/agent-memory";

const agent = new Agent({
    name: "assistant",
    instructions: "You are a helpful assistant with long-term memory.",
    model: anthropic("claude-sonnet-4-5"),
    memory: new AgentMemory({
        endpoint: process.env.AGENT_MEMORY_ENDPOINT!,
        context: process.env.AGENT_MEMORY_CONTEXT!,
        apiKey: process.env.AGENT_MEMORY_API_KEY!,
    }),
});

Pass a Mastra storage to keep durable verbatim threads, messages, and working memory in SurrealDB, with SurrealDB Agent Memory layered on as the intelligence tier:

import { SurrealDBStore } from "@surrealdb/mastra-ai";

const store = new SurrealDBStore({ id: "agent-memory-demo", url: "ws://localhost:8000", username: "root", password: "secret" });
await store.init();

const memory = new AgentMemory({
    endpoint: process.env.AGENT_MEMORY_ENDPOINT!,
    context: process.env.AGENT_MEMORY_CONTEXT!,
    apiKey: process.env.AGENT_MEMORY_API_KEY!,
    storage: store, // durable verbatim history; omit to keep it in-process
});

Let an agent call SurrealDB Agent Memory explicitly to store, recall, forget, fetch context, and search documents:

import { AgentMemory, createAgentMemoryTools } from "@surrealdb/mastra-ai/agent-memory";

const client = new AgentMemory({
    endpoint: process.env.AGENT_MEMORY_ENDPOINT!,
    context: process.env.AGENT_MEMORY_CONTEXT!,
    apiKey: process.env.AGENT_MEMORY_API_KEY!,
});

const agent = new Agent({
    name: "assistant",
    instructions: "Use agent-memory-recall before answering questions about the user.",
    model: anthropic("claude-sonnet-4-5"),
    tools: createAgentMemoryTools(client),
});

The toolset is agent-memory-remember, agent-memory-recall, agent-memory-forget, agent-memory-context, and agent-memory-search-documents. Document helpers ingestDocument and searchDocuments cover RAG.

Note

Isolation is soft under a shared API key: resourceId maps to SurrealDB Agent Memory scopes and labels, not a hard tenant boundary. Use client.onBehalfOf(principal) for stronger isolation. One client is pinned to one SurrealDB Agent Memory context.

Used on its own, SurrealDBStore covers conversation memory, workflow suspend/resume snapshots, scoring, observability, and native HNSW vector search against a SurrealDB instance you run:

import { Mastra } from "@mastra/core/mastra";
import { SurrealDBStore } from "@surrealdb/mastra-ai";

const store = new SurrealDBStore({
    id: "my-store",
    url: "ws://localhost:8000",
    username: "root",
    password: "secret",
    namespace: "mastra",
    database: "my_app",
});

const mastra = new Mastra({ agents: { assistant }, storage: store });
await store.init();

It also accepts token auth or a pre-connected Surreal instance.

Was this page helpful?