mastra-ai/mastra · error

Resource ID is required for resource-scoped working memory u

Error message

Resource ID is required for resource-scoped working memory updates

What it means

In Mastra memory, working memory can be scoped either to a thread or to a resource. When the configured scope is 'resource' (the default), a resourceId must be supplied to identify which resource's working memory to read or update. This error is thrown by the memory implementation when a resource-scoped working memory operation is attempted without a resourceId.

Source

Thrown at packages/core/src/memory/mock.ts:266

        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,
              });
            }

            if (thread.resourceId && resourceId && thread.resourceId !== resourceId) {
              throw new Error(
                `Thread with id ${threadId} resourceId does not match the current resourceId ${resourceId}`,
              );
            }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Pass a resourceId in the call: memory.updateWorkingMemory({ resourceId, threadId?, workingMemory })
  2. Alternatively configure workingMemory scope to 'thread' in Memory options if per-thread memory is what you want: new Memory({ options: { workingMemory: { scope: 'thread' } } })
  3. Ensure resourceId is propagated from the agent run (memoryResource / resourceid in request context) into the working-memory update call
  4. Log/inspect mergedConfig.workingMemory?.scope and the resourceId argument before the call to confirm which side is missing

Example fix

// before
await memory.updateWorkingMemory({ threadId, workingMemory: 'User likes jazz' });
// after
await memory.updateWorkingMemory({ threadId, resourceId, workingMemory: 'User likes jazz' });
Defensive patterns

Strategy: validation

Validate before calling

function assertResourceScopeArgs(opts: { resourceId?: string; threadId?: string }, scope: 'thread' | 'resource' = 'resource') {
  if (scope === 'resource' && !opts.resourceId) {
    throw new Error('resourceId is required for resource-scoped working memory updates');
  }
  if (scope === 'thread' && !opts.threadId) {
    throw new Error('threadId is required for thread-scoped working memory updates');
  }
}

Type guard

const hasResourceId = (o: { resourceId?: string | null }): o is { resourceId: string } =>
  typeof o.resourceId === 'string' && o.resourceId.length > 0;

Try / catch

try {
  await memory.updateWorkingMemory({ resourceId, workingMemory });
} catch (err) {
  if (err instanceof Error && err.message.includes('Resource ID is required')) {
    resourceId ??= await resolveResourceIdFromSession();
    await memory.updateWorkingMemory({ resourceId, workingMemory });
  } else throw err;
}

Prevention

When it happens

Trigger: Calling memory.updateWorkingMemory (or the working-memory tool path invoked via listTools) with workingMemory scope configured as 'resource' (or left at default) while passing no resourceId — e.g. calling updateWorkingMemory({ threadId, workingMemory }) with only a threadId.

Common situations: Developers switching from thread-scoped to default (resource-scoped) working memory after upgrading Mastra, or assuming threadId alone is enough. Also happens when resourceId is lost somewhere between an agent invocation and a manual memory.updateWorkingMemory call, or when workingMemoryConfig is built dynamically and the resourceId is undefined at call time.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/98071cce47c6d118. Report an issue: GitHub.