arcjet/arcjet-js/integrate-arcjet-guard-mastra
Claude Code and agent skill by arcjet/arcjet-js
- community
Checking that the SKILL.md file is still in the repository.
Integrate Arcjet security into a Mastra agent using @arcjet/guard — wrap createTool execute, screen input/output with a Processor tripwire, and gate unwrapped MCP/workspace tools with hooks. Use when asked to add Arcjet to a Mastra agent, rate limit its tools, screen inbound messages, or block prompt injection / PII.
Install
npx vskill@latest install arcjet/arcjet-js/integrate-arcjet-guard-mastraSKILL.md
Integrate Arcjet Guard into a Mastra agent
@arcjet/guard's Mastra v1 namespace wraps the agent's existing Arcjet client. It never talks to the Arcjet API itself. Four surfaces, one decision rule:
- An authored tool (
createTool({ execute })) →guardTool(). DENY is a
structured tool result. Do not throw.
- Inbound / outbound text (
inputProcessors/outputProcessors) →
guardProcessor(). processInput + abort() on DENY raises a tripwire. processInputStep screens later agentic steps (tool continuations). Channels already hit processInput, so there is no guardInbound.
- MCP / workspace / toolsets you did not wrap →
guardHooks().
beforeToolCall can return { proceed: false, output }.
- Correlation →
mastraAgentContext()readsMASTRA_THREAD_ID_KEY, then
resource, then run. It never mints a new id.
Mastra requireApproval is human HITL, not policy — same trap as Google ADK requireConfirmation, and Cloudflare Think needsApproval. There is no guardApproval. Do not also wrap these tools with @arcjet/guard/vercel-ai/v7 or @arcjet/guard/claude-managed-agents/v0.
Questions to ask the human first
Ask only what you cannot infer from the code; suggest defaults.
- Which tools are risky (external side effects, irreversible, spends
money, sends messages)? Those get guardTool. Purely informational tools can be left unguarded or gated with no rules.
- What limits? (e.g. "10 lookups/min per order" →
tokenBucket.) - Who is the user for metadata — an opaque user/tenant ID (never PII)?
Default: Mastra's resource id (MASTRA_RESOURCE_ID_KEY).
- Is an Arcjet outage unacceptable? Every helper defaults to
onGuardError: "deny". Ask explicitly about the inbound processor: failing closed there means the agent stops answering for the duration of the outage, so "allow" is a routine and legitimate choice at that one call site.
The six things readers get wrong
- There is no `guardInbound`. Mastra channels already run through
processInput. Screen prompt injection on guardProcessor in inputProcessors.
- There is no `guardApproval`. Mastra
requireApprovalis a human
in-the-loop pause, not a policy gate. Use guardTool or guardHooks.
- The import path is versioned and there is no alias.
@arcjet/guard/mastra/v1. @arcjet/guard/mastra does not resolve.
- Correlation is read, never minted. Do not call
createAgentContext
inside a Mastra callback — that generates a second id and splits the Sequence. mastraAgentContext reads thread / resource / run and omits correlationId when none of those is a valid id.
- Do not double-wrap with `@arcjet/guard/vercel-ai/v7`. Mastra tools
are createTool, not AI SDK tool(). guardTool throws if the tool already carries the Arcjet protection brand.
- A denial from `guardTool` is a structured result, not a throw. Prefer
omitting outputSchema on guarded tools, or verify the schema accepts ArcjetDenialResult. If onDeny throws, the tool still does not run and the model still receives the default denial object.
Step 1: Install and find the guard client
Install @arcjet/guard (required), plus @mastra/core (optional peer, needed for @arcjet/guard/mastra/v1). Always use the versioned path: @arcjet/guard/mastra/v1 resolves; @arcjet/guard/mastra throws ERR_PACKAGE_PATH_NOT_EXPORTED.
If the agent has no guard client yet, launch one once at module scope:
Step 2: Gate authored tools
- Omit
rulesto submit none. The guard call still happens. - Optional
actorandinputs(static, or a resolver over this adapter's native call — parsed input plus trusted runtime/context) are forwarded on the
guard call so a remote policy that declares those names can evaluate. Build each input with policyInput. A resolver that throws or returns a value Guard cannot use is left out of the guard call, and onGuardError decides.
- On DENY the tool's
executenever runs. The model receives
{ arcjetDenied: true, reason, message, retryable }.
- Default
onGuardError: "deny"blocks the tool if Arcjet is unreachable,
or if a rules or metadata callback throws or returns a value Guard cannot use. A failed callback never skips the guard call: Guard is called without its value, so remote policy still evaluates the call and a DENY stands.
Step 3: Screen inbound (and optional outbound) text
- On DENY,
processInput/processInputStepcallabort()and Mastra
raises a tripwire. If abort() were to return, the processor still throws so the turn cannot fail open.
- The same processor implements
processOutputResultso it can sit on
outputProcessors as well. Use a separate action name for outbound.
- Default
onGuardError: "deny"— if the guard cannot be evaluated, or a
rules or metadata callback throws or returns a value Guard cannot use, the turn is aborted. Guard is still called without the failed value, so a DENY stands. Use "allow" when the human cost of rejecting a legitimate message exceeds the security cost of an outage.
Step 4: Gate tools you did not wrap
Pass hooks to the Agent constructor (or to generate / stream). beforeToolCall returns { proceed: false, output } on DENY so MCP / workspace / toolset calls never execute. afterToolCall is observe-only. It records the call even when an action or metadata callback fails, under the default label and its own metadata.
Use this for tools you did not pass through guardTool. Applying both to the same authored tool double-calls the guard.
Step 5: Correlation
Set Mastra's reserved keys on RequestContext before generate / stream. mastraAgentContext reads them; it never calls createAgentContext.
Preference order: thread id, then resource id, then workflow.runId. If none is a valid 1–256 printable-ASCII string, the call is uncorrelated rather than joined to a generated id nobody has.
Verify the integration
npm run typecheckpasses.- Exercise inbound PI, a tool deny, PII on args, a rate limit, and
fail-closed (an unreachable guard).
- Confirm in the Arcjet dashboard that decisions share the thread id as
their correlation id.
- Manual E2E with a real
ARCJET_KEYis still-to-verify until you run it.
A full working demo lives in `arcjet/examples` `mastra-agent` (lands with arcjet/examples#193). Do not add an example under examples/ in the JS SDK repo.
Note: capture events are fire-and-forget and batched, so events can lag the decisions they accompany by a few seconds. A dropped event is diagnosed, never thrown.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingarcjet-guard/skills/integrate-arcjet-guard-mastra/SKILL.md
Popularity
- Repository stars
- 696
- Repository forks
- 35
- 7-day trend score
- 33
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/arcjet/arcjet-js/integrate-arcjet-guard-mastra)