mastra-auth
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Secure a Mastra server and Studio: server.auth with SimpleAuth, MastraJwtAuth, CompositeAuth, a custom MastraAuthProvider, or a provider package (Auth0, Better Auth, Clerk, Firebase, Google, Okta, Supabase, WorkOS, Neon), plus per-user memory isolation with mapUserToResourceId, public routes, RBAC, fine-grained authorization (FGA), and worker tokens. Use this skill whenever the user wants to require login or API keys for Mastra endpoints, pass tokens from a frontend, keep users from reading each other's threads, combine auth methods, map roles to permissions, or authenticate workers. Triggers on server.auth, authenticateToken, mapUserToResourceId, requiresAuth, SimpleAuth, MastraJwtAuth, @mastra/auth, CompositeAuth, MastraAuthProvider, @mastra/auth-clerk, @mastra/auth-auth0, @mastra/auth-supabase, @mastra/auth-firebase, @mastra/auth-workos, @mastra/auth-better-auth, @mastra/auth-okta, @mastra/auth-google, @mastra/auth-neon, server.rbac, server.fga, MASTRA_WORKER_AUTH_TOKEN, 401, 403.
- Reads credentials
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-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-knowledgeSkill
- 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
6 rules. About 0 are common, 2 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 94
Verify the API: If node_modules/@mastra/ exists, read node_modules/@mastra/core/dist/docs/ and the provider package docs and types.
- Very specific: line 99
Check the API prefix: If server.apiPrefix changed, update protected and public paths on the provider.
- Very specific: line 101
Enforce ownership in tools: Read requestContext.get('user') in execute, never an ID from tool input.
- Distinctive: line 102
Add RBAC or FGA: if roles or per-resource permissions are needed.
- Very specific: line 130
Put the most common method first.
- Distinctive: line 137
Mapper returns an empty or non-string value: Every authenticated request fails with 500.
≈ “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 Auth
Authenticate callers to the Mastra server and Studio, carry their identity into agents and tools, and authorize what they can touch.
When to use
- Turning on authentication for the built-in
/apiroutes, custom routes, and Studio - Choosing between SimpleAuth, JWT, an identity provider package, or a custom provider
- Supporting several auth methods at once (API keys plus user sessions)
- Isolating memory and threads per user or tenant
- Making a webhook or health route public
- Mapping identity-provider groups or roles to Mastra permissions (RBAC)
- Resource-level permissions for multi-tenant apps (FGA)
- Authenticating separate worker processes
When NOT to use
- Middleware, CORS, and route plumbing in general (use
mastra-server) - Sending the token from a frontend or service (covered briefly here, details in
mastra-client-sdk) - MCP server OAuth and MCP tool auth context (use
mastra-mcp) - Studio deployment and its sign-in screens (use
mastra-studio-editor) - Worker process topology (use
mastra-deployment) - Thread and resource design for memory itself (use
mastra-memory)
Required inputs
- The identity system in use (none yet, Clerk, Auth0, Supabase, Firebase, WorkOS, Okta, Google Workspace, Better Auth, Neon Auth, or in-house)
- Who calls the server: browsers with sessions, services with API keys, workers, or several of these
- The ownership boundary for memory: per user, per organization, or per workspace and user
- Routes that must stay public (webhooks, health checks)
- Whether roles or per-resource permissions are needed, and whether an Enterprise Edition license applies in production (RBAC and FGA)
The framework: authenticate, propagate, authorize
| Stage | Question | Mastra mechanism |
| Authenticate | Who is calling? | server.auth |
| Propagate | How does identity reach code? | RequestContext (requestContext.get('user'), MASTRA_RESOURCE_ID_KEY) |
| Authorize | What may they do? | authorizeUser, server.rbac, server.fga, checks in tools and routes |
| Outbound | Which credential calls another service? | Trusted tool code, never model input |
Once server.auth is set, built-in routes and custom routes require a valid user. Without it, every route and Studio is public.
// src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { MastraJwtAuth } from '@mastra/auth'
export const mastra = new Mastra({
server: {
auth: new MastraJwtAuth({
secret: process.env.MASTRA_JWT_SECRET,
mapUserToResourceId: user => user.id,
}),
},
})mapUserToResourceId sets a server-owned resource ID after authentication. The server then filters thread lists, rejects cross-resource access with 403, and ignores a client-supplied memory.resource. Without it, any authenticated caller can read any resource's threads, and Mastra logs a startup warning.
Picking a provider
| Need | Use |
| Local dev, static API keys, worker tokens | SimpleAuth from @mastra/core/server |
| Shared-secret JWTs | MastraJwtAuth from @mastra/auth |
| Hosted identity provider | MastraAuth<Provider> from @mastra/auth-<provider> (see references/providers.md) |
| Several methods at once | CompositeAuth([a, b]) from @mastra/core/server |
| In-house tokens or SSO | Extend MastraAuthProvider, or pass { authenticateToken, mapUserToResourceId } |
Studio login works with Simple Auth, JWT, WorkOS, Better Auth, and Google. Set studio.auth to give Studio its own provider.
Workflow
- Verify the API. If
node_modules/@mastra/exists, readnode_modules/@mastra/core/dist/docs/and the provider package docs and types. Otherwise fetchhttps://mastra.ai/docs/auth/overview.mdand the provider page, for examplehttps://mastra.ai/reference/auth/clerk.md. Trust these over memory. - Pick the provider with the table above. Install its package.
- Set env vars the provider reads (see
references/providers.md). Many providers work with an empty constructor once env vars are set. - Configure
server.authinsrc/mastra/index.ts. AddmapUserToResourceIdwhenever more than one user shares the deployment. - Mark public routes. Set
requiresAuth: falseon webhook routes and verify their signatures in the handler. - Check the API prefix. If
server.apiPrefixchanged, updateprotectedandpublicpaths on the provider. - Send tokens from clients.
MastraClientheaders: { Authorization: 'Bearer <token>' }, orcredentials: 'include'for cookie sessions. - Enforce ownership in tools. Read
requestContext.get('user')inexecute, never an ID from tool input. - Add RBAC or FGA if roles or per-resource permissions are needed.
- Test that anonymous calls get 401, wrong-user thread access gets 403, and allowed calls succeed.
Deep dive: combining and customizing
CompositeAuth tries providers in order and returns the first user. Errors thrown in authenticateToken() are swallowed so the next provider gets a chance. authorizeUser() errors are not swallowed and fail the request with 500, so return false instead of throwing.
import { CompositeAuth, SimpleAuth } from '@mastra/core/server'
import { MastraAuthClerk } from '@mastra/auth-clerk'
const apiKeyAuth = new SimpleAuth({
tokens: { [process.env.CI_API_KEY!]: { id: 'ci', name: 'CI Pipeline' } },
})
const clerkAuth = new MastraAuthClerk({
publishableKey: process.env.CLERK_PUBLISHABLE_KEY,
secretKey: process.env.CLERK_SECRET_KEY,
jwksUri: process.env.CLERK_JWKS_URI,
})
export const mastra = new Mastra({
server: { auth: new CompositeAuth([clerkAuth, apiKeyAuth]) },
})Put the most common method first. For a custom provider, extend MastraAuthProvider, call super({ name }), then this.registerOptions(options), and implement authenticateToken(token, request) and authorizeUser(user, request). verifyJwks, verifyHmac, and decodeToken from @mastra/auth cover common JWT checks. See references/examples.md.
Failure patterns
- No
mapUserToResourceIdin a multi-user app. Users can list and read each other's threads. Add the mapper or setMASTRA_RESOURCE_ID_KEYin middleware. - Mapper returns an empty or non-string value. Every authenticated request fails with 500. Return a stable non-empty string.
- Custom
apiPrefixwith default auth paths. Built-in routes fall outside/api/*and become public. Updateprotectedandpublic. - Webhook rejected with 401. Custom routes are protected by default. Set
requiresAuth: falseand verify the signature yourself. - Middleware meant to block a public route. User middleware does not run on
requiresAuth: falseroutes. Check inside the handler. 401from the browser after sign-in. The token or cookie is not sent. Add theAuthorizationheader orcredentials: 'include'with a credentialed CORS origin.- Missing
Bearerprefix with providers that expect it. SendAuthorization: Bearer <token>. SimpleAuth accepts the token with or without the prefix, but providers such as Clerk require it. - SimpleAuth in production. Tokens live in memory with no expiry or signature. Use JWT or an identity provider.
MastraJwtAuththrows at startup. NeithersecretnorJWT_AUTH_SECRETis set, orsecretis an empty string.- Tool trusts a
userIdargument. The model can choose any value. Read the user from request context. - Supabase or Firebase denies valid users. The default
authorizeUserchecksusers.isAdmin(Supabase) or auser_accessdocument (Firebase). OverrideauthorizeUser. - Workers split from the API with no auth. Step execution and event endpoints are open. Configure
server.authand setMASTRA_WORKER_AUTH_TOKENon workers.
Output format
A finished auth setup contains:
server.auth(andstudio.auth,server.rbac,server.fgawhen needed) insrc/mastra/index.tsmapUserToResourceIdor equivalent middleware for multi-user deployments- Public routes listed with the reason and signature verification
- Provider env vars in
.env.example(never.env) - Client code that sends the token or cookie
- Tests or
curlchecks for 401, 403, and success paths
Reference files
references/providers.md- Every provider: package, class, constructor shape, env vars, default authorization, Studio and RBAC support.references/api.md-server.authconfig, SimpleAuth, JWT, CompositeAuth,MastraAuthProvider, helpers, RBAC, FGA, worker auth.references/examples.md- JWT with per-user memory, custom JWKS provider, tenant-safe tools, Okta RBAC, worker tokens.references/checklist.md- Pre-production auth checklist.
Live docs
- https://mastra.ai/docs/auth/overview
- https://mastra.ai/docs/auth/simple-auth
- https://mastra.ai/docs/auth/jwt
- https://mastra.ai/docs/auth/composite-auth
- https://mastra.ai/docs/auth/custom-auth-provider
- https://mastra.ai/docs/auth/fga
- https://mastra.ai/docs/auth/workers
- https://mastra.ai/docs/guides/authentication-identity
- https://mastra.ai/reference/auth/jwt
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.

