{"record":{"id":"50d730e0c99d6c34","repo":"mastra-ai/mastra","slug":"knowledge-tools-require-an-active-resourceid","errorCode":null,"errorMessage":"Knowledge tools require an active resourceId.","messagePattern":"Knowledge tools require an active resourceId\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/memory/src/processors/observational-memory/subconscious/knowledge-tools.ts","lineNumber":38,"sourceCode":"  storage: {\n    getStore(name: 'knowledge'): Promise<KnowledgeStorage | undefined>;\n  };\n  getKnowledgeSemanticIndex(): Promise<KnowledgeSemanticIndexCoordinator>;\n};\n\ntype KnowledgeToolContext = {\n  agent?: { threadId?: string; resourceId?: string };\n  requestContext?: { get(key: string): unknown };\n};\n\nfunction resolveScope(context: KnowledgeToolContext | undefined): KnowledgeScope {\n  const organizationId = context?.requestContext?.get('organizationId');\n  const resourceId = resolveKnowledgeResourceId(context?.requestContext, context?.agent?.resourceId);\n  const threadId = context?.agent?.threadId;\n  if (typeof organizationId !== 'string' || !organizationId.trim()) {\n    throw new Error('Knowledge tools require requestContext.organizationId.');\n  }\n  if (!resourceId) throw new Error('Knowledge tools require an active resourceId.');\n  if (!threadId) throw new Error('Knowledge tools require an active threadId.');\n  return [`org:${organizationId}`, `resource:${resourceId}`, `thread:${threadId}`];\n}\n\nasync function getKnowledgeStore(memory: KnowledgeToolsMemory): Promise<KnowledgeStorage> {\n  const store = await memory.storage.getStore('knowledge');\n  if (!store) throw new Error('Knowledge tools require a configured knowledge storage domain.');\n  return store;\n}\n\nfunction normalizeLimit(limit: number | undefined): number {\n  return Math.min(Math.max(limit ?? DEFAULT_LIMIT, 1), MAX_LIMIT);\n}\n\nfunction serializeRecord(record: KnowledgeRecord) {\n  return {\n    id: record.id,\n    text: record.text,","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/memory/src/processors/observational-memory/subconscious/knowledge-tools.ts#L20-L56","documentation":"After validating organizationId, resolveScope needs a resourceId identifying whose memories/knowledge are accessed. It is resolved from requestContext (via resolveKnowledgeResourceId) falling back to context.agent.resourceId; if neither yields a truthy value, knowledge tools cannot scope access and throw.","triggerScenarios":"Invoking a knowledge tool where resolveKnowledgeResourceId returns undefined/empty and context.agent.resourceId is unset — e.g. agent run without a resourceId, or requestContext lacking the knowledge resource id key.","commonSituations":"Agent instantiated without resourceId in memory configuration; server calls that omit per-user resourceId; tests constructing an agent without memory resource setup; renamed config keys after an upgrade.","solutions":["Set resourceId on the agent's memory configuration or pass it in run options so context.agent.resourceId is populated.","Set the resource id on requestContext (the key read by resolveKnowledgeResourceId) before tool execution.","Inspect resolveKnowledgeResourceId's expected requestContext key and provide it in your middleware."],"exampleFix":"// before\nnew Agent({ memory: new Memory({ storage }) }); // no resourceId\n// after\nconst memory = new Memory({ storage, options: { resourceId: thread.userId } });\nawait agent.stream(prompt, { resourceId: thread.userId, threadId: thread.id, requestContext });","handlingStrategy":"validation","validationCode":"const resourceId = requestContext?.get('knowledgeResourceId') ?? agent.resourceId;\nif (!resourceId) throw new Error('knowledge tools invoked without resourceId');","typeGuard":"function hasKnowledgeResource(ctx?: { requestContext?: { get(k: string): unknown }; agent?: { resourceId?: string; threadId?: string } }): boolean {\n  return !!(ctx?.requestContext?.get('knowledgeResourceId') ?? ctx?.agent?.resourceId);\n}","tryCatchPattern":"try {\n  await knowledgeTool.invoke({ requestContext, agent });\n} catch (e) {\n  if (e instanceof Error && e.message === 'Knowledge tools require an active resourceId.') {\n    console.error('Agent run missing resourceId; check memory config / run options');\n  } else throw e;\n}","preventionTips":["Always pass resourceId (and threadId) in agent run options.","Set resourceId on Memory options at agent construction for long-lived agents.","Add an integration test that runs knowledge tools end-to-end with a real requestContext.","Re-check config keys after upgrading memory packages."],"tags":["configuration","request-context","knowledge-tools","memory"],"backgroundTag":"missing-request-context","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}