Skip to main content

knowledgeMemoryScopes()

knowledgeMemoryScopes() returns scope declarations for the resource: and thread: scopes every Memory run holds, plus a tenant scope. Spread it into the Knowledge scopes option so agents with Memory can use Knowledge. See Scope declarations for how declarations work.

alpha

Knowledge is in alpha. Its API may change between releases.

Usage example
Direct link to Usage example

src/mastra/knowledge.ts
import { Knowledge, knowledgeMemoryScopes } from '@mastra/core/knowledge'

export const knowledge = new Knowledge({
tools: true,
scopes: {
...knowledgeMemoryScopes(),
'team:$orgId:$teamId': { access: [{ principal: 'self', role: 'editor' }] },
},
})

Parameters
Direct link to Parameters

org?:

string | false
= 'org:$orgId'
The tenant scope pattern. Rename it, for example workspace:$workspaceId, or pass false to leave the tenant out. With false, resource: has no belongsTo.

orgRole?:

'editor' | 'contributor'
= 'editor'
The role a run holding the tenant scope gets on it. contributor makes the tenant append-only: runs add pages and notes but can't edit them.

Returns
Direct link to Returns

KnowledgeScopeDeclarations with three entries, or two when org is false:

KeyAccessbelongsTopinnedContextPriority
thread:$threadIdself ownerresource:$resourceId2
resource:$resourceIdself ownerorg:$orgId1
org:$orgIdself editornone0

The tenant row uses the org and orgRole options. Stored pins aimed at a scope with a higher pinnedContextPriority come first. A run gets its thread pins, then its resource pins, then tenant pins.

knowledgeMemoryScopes({ org: 'workspace:$workspaceId', orgRole: 'contributor' })
// {
// 'thread:$threadId': { access: [{ principal: 'self', role: 'owner' }], belongsTo: 'resource:$resourceId', pinnedContextPriority: 2 },
// 'resource:$resourceId': { access: [{ principal: 'self', role: 'owner' }], belongsTo: 'workspace:$workspaceId', pinnedContextPriority: 1 },
// 'workspace:$workspaceId': { access: [{ principal: 'self', role: 'contributor' }], pinnedContextPriority: 0 },
// }

knowledgeScopeAddress()
Direct link to knowledgescopeaddress

knowledgeScopeAddress(pattern: string, params: Record<string, string>): string

Builds a canonical scope address from a pattern and raw parameter values. Use it in host code for agent scopes, Knowledge APIs, pinnedContext entries and page references.

Inside every value, %, /, # and : are encoded as %25, %2F, %23 and %3A. Each value fills exactly one segment. It throws when a parameter is missing or empty.

import { knowledgeScopeAddress } from '@mastra/core/knowledge'

knowledgeScopeAddress('resource:$resourceId', { resourceId: 'acme/dana' })
// 'resource:acme%2Fdana'

knowledgeScopeAddress('team:$orgId:$teamId', { orgId: 'a:b', teamId: 'c:d' })
// 'team:a%3Ab:c%3Ad'

encodeKnowledgeScopeId()
Direct link to encodeknowledgescopeid

encodeKnowledgeScopeId(id: string): string

Encodes one ID, such as a tenant or user ID, for one segment of a scope address. It applies the same encoding as knowledgeScopeAddress(). Use it when you build an address with a template string:

import { encodeKnowledgeScopeId } from '@mastra/core/knowledge'

const scope = `org:${encodeKnowledgeScopeId('auth0|a:b')}`
// 'org:auth0|a%3Ab'

decodeKnowledgeScopeId()
Direct link to decodeknowledgescopeid

decodeKnowledgeScopeId(segment: string): string | null

Returns the ID in one segment of a scope address. It returns null when the segment isn't canonically encoded, for example when it holds a raw : or a lowercase escape such as %3a.

import { decodeKnowledgeScopeId } from '@mastra/core/knowledge'

decodeKnowledgeScopeId('slack%3AU123') // 'slack:U123'
decodeKnowledgeScopeId('a:b') // null

Agent scopes
Direct link to Agent scopes

Agent scopes are canonical addresses. Encode any ID that contains :, /, # or %, with encodeKnowledgeScopeId() or knowledgeScopeAddress(). A raw ID, such as org:a:b for org a:b, fails at run start:

Knowledge run scope "org:a:b" does not match a declared scope type. Declare it in Knowledge `scopes` (for Memory's resource:/thread: scopes, spread `...knowledgeMemoryScopes()`), and encode ids containing ":", "/", "#" or "%" with encodeKnowledgeScopeId.

resource: and thread: agent scopes are the exception. They set the run's Memory resource and thread, so they take Memory's raw IDs, and Knowledge encodes them. A run with Memory resource slack:U123 holds resource:slack%3AU123. See set a run's scopes.

Scopes stored by earlier alpha releases with a raw : in their last segment aren't moved to the encoded address. See Patterns and params.