Integration

Memory for Mastra agents

This page shows how to add Harny memory to a Mastra agent so each signed-in person gets a private memory the agent can read.

Jump to the code

What the framework does not keep

  • Which value is current. Harny memory is dated: a changed value with a newer timestamp becomes the current value, and the earlier value stays answerable by date.
  • When a fact happened. Send a timestamp (ISO 8601 with zone) with each memory so Harny knows when it happened.
  • One person across sources. Harny keeps a private memory for each person, and connected sources are read for that person only.

The pattern: the Harny MCP server

This page uses the Harny MCP server. The key in the header belongs to one person (fsk_), so every call the agent makes is scoped to that person.

A user key (fsk_) names its own user and needs no X-Harny-User header.

import { Agent } from "@mastra/core/agent";
import { MCPClient } from "@mastra/mcp";

export async function accountAssistant(userId: string, userKey: string) {
  const mcp = new MCPClient({
    id: `harny-${userId}`,
    servers: {
      harny: {
        url: new URL("https://api.harny.ai/v1/mcp/mcp"),
        // the signed-in person's key, set by your application
        requestInit: { headers: { Authorization: `Bearer ${userKey}` } },
      },
    },
  });
  return new Agent({
    name: "account-assistant",
    instructions: "Call assemble_context before you answer a question about a customer.",
    model, // the model you already use in Mastra
    tools: await mcp.getTools(),
  });
}

What the tools return

With a person's key (starts with fsk_), the server offers the memory tools. Calls are scoped to the key's user.

  • assemble_context is the Recall Memory tool. It returns ranked context from the person's memory and their connected sources, scoped to that person.
  • ingest_text is the Ingest Memory tool. It writes text into that person's memory.
  • describe is available with any key.

When to use which

  • Use Mastra's own state for the agent's working data during a run, such as the steps of a workflow and the tools it has called.
  • Add Harny when the agent needs a person's memory across sessions, with dates on each fact and a way to recall what was true at a given time.

Frequently asked questions

Is a package needed?

The Harny MCP server runs over Streamable HTTP at https://api.harny.ai/v1/mcp/mcp. Connect to it with an MCP client and send your key as a Bearer token.

Where does the user id come from?

The user id comes from your app's sign-in. The key in the header belongs to one person (fsk_), so calls are scoped to that person.

Go on. Get Harny.

The first 500 monthly-active end users are free for good. Create an app key and make your first call in minutes.