mastra-knowledge
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Give Mastra agents a shared, scoped knowledge graph of pages and notes with the Knowledge class from @mastra/core/knowledge: declare scope types and access rules, set each run's scopes from server code, seed content with upsertPage and addNote, turn on the knowledge_search, knowledge_read, knowledge_page, knowledge_note, and knowledge_pin tools, pin pages into context every turn, and browse it in Studio. Use this skill whenever the user wants agents to share policies, runbooks, docs, or learned conventions across threads, agents, teams, or tenants, or wants one agent to serve many customers with per-org content. Triggers on Mastra Knowledge, new Knowledge, knowledgeMemoryScopes, scopes, MASTRA_SCOPES_KEY, explainAccess, upsertPage, addNote, pinnedContext, knowledge_search, knowledge_read, KNOWLEDGE_ROUTES, scope declarations, viewer contributor editor owner roles. Also triggers when the user asks how Knowledge differs from RAG or memory.
In this repository46 other files it gives its agents.
- mastra-a2a-acpSkill
- mastra-agent-approval-hitlSkill
- mastra-agentic-uiSkill
- mastra-agent-skillsSkill
- mastra-agentsSkill
- mastra-authSkill
- mastra-browserSkill
- mastra-build-playbookSkill
- mastra-channelsSkill
- mastra-classifierSkill
- mastra-cli-apiSkill
- mastra-client-sdkSkill
- mastra-datasets-experimentsSkill
- mastra-deploymentSkill
- mastra-evalsSkill
- mastra-file-based-agentsSkill
- mastra-framework-integrationSkill
- mastra-guideSkill
- mastra-harnessSkill
- mastra-mcpSkill
- mastra-memorySkill
- mastra-migrationsSkill
- mastra-modelsSkill
- mastra-multi-agentSkill
- mastra-observabilitySkill
- mastra-observational-memorySkill
- mastra-platformSkill
- mastra-processors-guardrailsSkill
- mastra-project-setupSkill
- mastra-ragSkill
- mastra-schedulesSkill
- mastra-semantic-recallSkill
- mastra-serverSkill
- mastra-signalsSkill
- mastra-storageSkill
- mastra-streamingSkill
- mastra-structured-outputSkill
- mastra-studio-editorSkill
- mastra-tool-integrationsSkill
- mastra-toolsSkill
- mastra-troubleshootingSkill
- mastra-vector-storesSkill
- mastra-voiceSkill
- mastra-workflowsSkill
- mastra-workflow-suspend-resumeSkill
- mastra-workspace-sandboxSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 119
Use RAG for large static corpora.
- Very specific: line 125
Verify the API: If node_modules/@mastra/ exists, read node_modules/@mastra/core/dist/docs/ and the installed Knowledge types.
- Very specific: line 166
Every run scope matches a declared type, even one that only names a role ('role:$orgId:support': {}).
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Mastra Knowledge
Set up a Knowledge instance so agents share curated pages and notes across conversations, with scopes and roles that decide what each run can read and change.
When to use
- Agents must follow rules or policies you edit, without redeploying prompts
- One agent serves many customers or teams, each with its own content
- Agents should record decisions, conventions, or runbook notes for other agents and people
- Agents hand work to each other through shared pages
- You want a page (policy, conventions, a docs index) in the agent's context every turn
- Seeding product docs or policies into a store agents can browse and search
- Deciding between Knowledge, RAG, and memory
When NOT to use
- Semantic search over a large document corpus you chunk and embed yourself (use
mastra-rag) - Conversation history, threads, and working memory for one user (use
mastra-memory) - Recalling older messages by meaning (use
mastra-semantic-recall) - Compressing long threads (use
mastra-observational-memory) - Server middleware, auth providers, and request context plumbing in general (use
mastra-serverandmastra-auth) - The state-signal mechanics behind pinned pages (use
mastra-signals) - Studio features beyond the Knowledge page (use
mastra-studio-editor)
Required inputs
- The audiences: orgs, teams, users, tenants, sites, or repos that need separate content
- Who may read, add to, edit, or delete each kind of content (maps to roles)
- How the server knows the user (auth, org ID, team list) so it can set run scopes
- Whether the agent only reads (docs agent) or also writes (shared learning)
- Which pages must be in context every turn, and the token budget for them
- Storage adapter, and optionally a vector store plus embedder for search by meaning
Knowledge is alpha. Tell the user access checks are not yet a guaranteed isolation boundary. Do not store data that must never reach another customer.
The framework: pages, notes, scopes, roles
- Page: a topic with
name, shortdescription, and Markdownbody. Pages nest:org:acme#policies/refund. - Note: a short attributed fact on a page (at most 250 tokens). Notes link to pages with
[[scope#path]]. - Scope: a separate area of the graph, addressed
<type>:<value>(org:acme,team:acme:support). You declare the types. - Role:
viewerreads,contributoradds pages and notes,editoredits, moves within a scope, and pins,ownerdeletes, merges, and moves across scopes. Roles come from rules and are computed per tool call. - Run scopes: the scopes one agent call holds. Only server code sets them, never the model or the request body.
import { Mastra } from '@mastra/core'
import { Agent } from '@mastra/core/agent'
import { Knowledge, knowledgeMemoryScopes } from '@mastra/core/knowledge'
import { LibSQLStore } from '@mastra/libsql'
import { Memory } from '@mastra/memory'
import { KNOWLEDGE_ROUTES } from '@mastra/server/server-adapter'
const storage = new LibSQLStore({ id: 'storage', url: 'file:./mastra.db' })
export const knowledge = new Knowledge({
id: 'company',
storage,
scopes: {
...knowledgeMemoryScopes(), // org:$orgId, resource:$resourceId, thread:$threadId
'team:$orgId:$teamId': {
description: 'A team',
belongsTo: 'org:$orgId',
access: [
{ principal: 'self', role: 'editor' },
{ principal: 'org:$orgId', role: 'viewer' },
],
},
},
tools: true, // off by default
})
export const agent = new Agent({
id: 'assistant',
name: 'Assistant',
instructions: 'You help your team and keep its knowledge up to date.',
model: 'openai/gpt-6.1-sol',
memory: new Memory({ storage }),
knowledge,
})
export const mastra = new Mastra({
agents: { agent },
knowledge: { company: knowledge },
server: { apiRoutes: [...KNOWLEDGE_ROUTES] }, // Studio browsing
})
await agent.generate('Our refund window is 30 days. Save that for the team.', {
scopes: ['org:acme', 'resource:dana', 'thread:dana-1'],
})Knowledge vs RAG vs memory
mastra-knowledge | mastra-rag | mastra-memory | |
| Unit | Curated pages and attributed notes | Embedded chunks of your documents | Messages in one thread |
| Who writes | Agents, people, and host seeding | Your ingestion pipeline | The conversation |
| Shared across | Threads, agents, users in a scope | Whoever queries the index | One resource and thread |
| Access control | Declared scopes and roles per tool call | Your filters or tenant stores | Resource and thread ownership |
| Search | Keyword, plus meaning with vector + embedder | Vector similarity, filters, rerank | Recency, semantic recall |
| Always in context | Pinned pages via state signals | No | Working memory, recent messages |
Use RAG for large static corpora. Use Knowledge when content is curated, changes, is written by agents, or needs per-audience access.
Workflow
- Verify the API. If
node_modules/@mastra/exists, readnode_modules/@mastra/core/dist/docs/and the installedKnowledgetypes. Otherwise fetchhttps://mastra.ai/docs/knowledge/overview.md,https://mastra.ai/reference/knowledge/knowledge-class.md, andhttps://mastra.ai/reference/knowledge/scope-declarations.md. Knowledge is alpha. Trust these over memory. - Model the audiences as scope types. Spread
knowledgeMemoryScopes()(rename the tenant with{ org: 'tenant:$tenantId' }). Add one pattern per content area (policy:$orgId,docs:$orgId,team:$orgId:$teamId). Patterns of one type must not overlap. - Write access rules. For each type, list
{ principal, role }.selfis the scope being accessed. Bind principal params from the address prefix or throughbelongsTo. Roles never inherit or chain. - Choose tools.
tools: truefor read and write,tools: { read: true }for read-only agents. Raiseread.maxTokensfor large-context models. - Attach to agents with
knowledge, or wireknowledge.createTools(opts)plusknowledge.getInstructions(opts)with identical options. - Set run scopes in server code. In middleware, set
MASTRA_SCOPES_KEY(andMASTRA_RESOURCE_ID_KEY) onrequestContext. In direct calls, passscopes. Use an agent-levelscopesfunction to narrow access for one agent. - Seed content from trusted host code with
upsertPage(). Re-run seeds freely. UseaddNote()once per fact. - Pin what must always be visible. Set
pinnedContexton the instance (needs Memory on the agent), then list page paths or{ list, depth }in a scope declaration. - Add search by meaning with
vectorandembeddertogether, before writing content. - Test access with
explainAccess()in unit tests. Register on Mastra withKNOWLEDGE_ROUTESand check the Studio Knowledge page.
Deep dive: run scopes from the server
import { Mastra } from '@mastra/core/mastra'
import { MASTRA_RESOURCE_ID_KEY, MASTRA_SCOPES_KEY } from '@mastra/core/request-context'
export const mastra = new Mastra({
agents: { support },
server: {
middleware: [
async (c, next) => {
const user = await getUser(c.req.header('Authorization')) // your auth lookup
const requestContext = c.get('requestContext')
requestContext.set(MASTRA_SCOPES_KEY, [
`org:${user.orgId}`,
...user.teams.map(team => `team:${user.orgId}:${team}`),
`resource:${user.id}`,
])
requestContext.set(MASTRA_RESOURCE_ID_KEY, user.id)
await next()
},
],
},
})Rules a run must satisfy:
- Every run scope matches a declared type, even one that only names a role (
'role:$orgId:support': {}). - At most one
resource:and onethread:scope. Aresource:scope must equalmemory.resource. - Pass raw IDs. Knowledge encodes them.
- An agent
scopesfunction must return everyresource:andthread:scope it receives.
Failure patterns
Knowledge needs scopesat construction. Passscopes, at least{ ...knowledgeMemoryScopes() }or{}.Knowledge tools need run scopes. The run has no scopes and no Memory. Passscopeson the call or setMASTRA_SCOPES_KEYin middleware.run scope ... does not match a declared scope type. Declare every type the server passes.- Agent has Knowledge but no tools.
toolsdefaults tofalse. Settools: trueor{ read: true }. - Pinned pages never appear. The instance lacks
pinnedContext, the agent has no Memory, the page has no body yet, or the run cannot read the page. - Overlapping patterns (
kb:$orgIdandkb:$orgId:public) fail at startup. Use two types. - Expecting inheritance. A role on
org:acmegrants nothing on scopes under it unless a rule says so. - Seed run twice duplicates notes.
upsertPage()is idempotent.addNote()is not. UseupdateNote(). - Exposing host methods.
upsertPage,addNote,removePage, andexplainAccessskip access checks. Never wrap them in an agent tool or an unauthenticated route. - Renaming a scope key makes the instance refuse to run. Call
renameScopeDeclaration(from, to)right after deploying. - Switching embedders on existing content gives keyword-only or poor matches. Knowledge cannot re-embed yet.
- Studio shows everything to everyone. Without
server.authorstudio.auth, routes read as owner. SetMASTRA_KNOWLEDGE_PRINCIPALS_KEYinauthorize.
Output format
A finished Knowledge delivery contains:
src/mastra/knowledge.tsexporting theKnowledgeinstance with scope declarations and tool settings- Agents with
knowledgeattached (and Memory when pins are used) - Server middleware or call sites that set run scopes from authenticated identity
- A seed script using
upsertPage()for preloaded content - Registration on
new Mastra({ knowledge: { ... } })withKNOWLEDGE_ROUTES, plus auth mapping if deployed explainAccess()tests covering each access rule
Reference files
references/api.md- Constructor options, scope declaration fields, roles, host methods, tools and their arguments, limits.references/examples.md- Docs agent, multi-tenant support agent, pinned policy, access tests, Studio auth.references/checklist.md- Scope design, access, and deployment checklist.
Live docs
- https://mastra.ai/docs/knowledge/overview
- https://mastra.ai/docs/knowledge/scopes-and-access
- https://mastra.ai/docs/knowledge/agents-and-tools
- https://mastra.ai/docs/knowledge/pinned-context
- https://mastra.ai/docs/knowledge/search
- https://mastra.ai/docs/knowledge/load-your-content
- https://mastra.ai/docs/knowledge/patterns
- https://mastra.ai/docs/studio/knowledge
- https://mastra.ai/reference/knowledge/knowledge-class
- https://mastra.ai/reference/knowledge/scope-declarations
- https://mastra.ai/reference/knowledge/knowledge-tools
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

