mastra-ai/mastra · error
Memory instance is required for working memory updates
Error message
Memory instance is required for working memory updates
What it means
The working-memory update tool (registered by listTools on the mock/standalone memory path) needs a Memory instance to write working memory. It reads it from the tool execution context; if `context.memory` is absent, a plain Error is thrown. This happens when the agent running the tool is neither part of a Mastra instance nor constructed with memory wired into the context.
Source
Thrown at packages/core/src/memory/mock.ts:258
const description = usesMergeSemantics
? `Update the working memory with new information. Data is merged with existing memory - only include fields you want to add or update.`
: `Update the working memory with new information. Any data not included will be overwritten.`;
return {
updateWorkingMemory: createTool({
id: 'update-working-memory',
description,
inputSchema: z.object({ memory: z.string() }),
execute: async (inputData, context) => {
const threadId = context?.agent?.threadId;
const resourceId = context?.agent?.resourceId;
// Memory can be accessed via context.memory (when agent is part of Mastra instance)
// or context.memory (when agent is standalone with memory passed directly)
const memory = (context as any)?.memory;
if (!memory) {
throw new Error('Memory instance is required for working memory updates');
}
const scope = mergedConfig.workingMemory?.scope || 'resource';
if (scope === 'thread' && !threadId) {
throw new Error('Thread ID is required for thread-scoped working memory updates');
}
if (scope === 'resource' && !resourceId) {
throw new Error('Resource ID is required for resource-scoped working memory updates');
}
if (threadId) {
let thread = await memory.getThreadById({ threadId });
if (!thread) {
thread = await memory.createThread({
threadId,
resourceId,
memoryConfig: _config,View on GitHub (pinned to 75dd419e61)
Solutions
- Attach the Agent (with its Memory) to a Mastra instance so `context.memory` is populated.
- Pass the memory instance in the run/request context when invoking the agent or tool directly.
- Verify the agent is constructed with `memory` and that workingMemory is enabled in its config.
Example fix
// before
const agent = new Agent({ name: 'a', model, memory }); // never registered anywhere
const res = await agent.generate('...'); // tool cannot find context.memory
// after
const mastra = new Mastra({ agents: { a: agent }, storage });
const res = await mastra.getAgent('a').generate('...'); // context.memory now available Defensive patterns
Strategy: validation
Validate before calling
const memory = (context as any)?.memory;
if (!memory) throw new Error('Working-memory tool requires context.memory; attach agent to Mastra or pass memory in context'); Type guard
function hasMemoryInContext(ctx: unknown): ctx is { memory: MastraMemory } {
return Boolean((ctx as any)?.memory);
} Try / catch
try {
await updateWorkingMemoryTool.execute({ context, ... });
} catch (e) {
if (e instanceof Error && e.message.includes('Memory instance is required')) {
// re-run inside an agent run with memory in context
} else throw e;
} Prevention
- Invoke working-memory tools through agent runs, not directly, unless you populate context.memory.
- Register agents with a Mastra instance so memory is injected automatically.
- In tests, seed the context with a real Memory instance.
When it happens
Trigger: Executing the update-working-memory tool (via agent generate/stream with tools enabled) where the request context has no `memory` property — e.g. standalone agent whose memory was not registered, or tool invoked outside an agent run without memory in context.
Common situations: Instantiating an Agent with a Memory instance but not attaching it to Mastra and not passing memory through the run context; calling the tool directly in tests without a memory in context; custom tool runners dropping the context.
Related errors
- The workingMemory.use option has been removed. Working memor
- WORKING_MEMORY_MISSING_STORAGE_ADAPTER
- Thread ID is required for thread-scoped working memory updat
- Resource ID is required for resource-scoped working memory u
- Cannot update working memory: ${scope} ID is required
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/955cfbf6f79063a3.
Report an issue: GitHub.