mastra-tools
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Define, attach, and control Mastra tools: createTool with inputSchema, outputSchema, and execute(inputData, context), schemas in Zod, Valibot, or ArkType, output validation, toModelOutput, transform redaction, client tools, tool hooks, lifecycle hooks, toolChoice and activeTools, built-in tools, code mode, and dynamic tool search. Use this skill whenever the user wants an agent to call an API, query a database, run a function, fetch a web page, search the web, ask the user a question, track tasks, or run many tools in one sandboxed call. Triggers on createTool, @mastra/core/tools, tool schema, beforeToolCall, afterToolCall, onInputAvailable, onOutput, toModelOutput, renderOnly, clientTools, toolChoice, activeTools, webSearchTool, webFetchTool, askUserTool, submitPlanTool, task tools, createCodeMode, QuickJsCodeModeTransport, IsolatedVmCodeModeTransport, ToolSearchProcessor. Also triggers when an agent ignores its tools, retries a tool forever, or has too many tools.
- Reads credentials
- Deletes or force-pushes
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-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-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
4 rules. About 0 are common, 0 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
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 139
Verify the API: If node_modules/@mastra/ exists, read node_modules/@mastra/core/dist/docs/ and the installed createTool types.
- Very specific: line 141
Write the schemas: Use any Standard JSON Schema library.
- Very specific: line 143
Decide what the model sees: Add toModelOutput when the raw result is large.
- Very specific: line 144
Mark side effects: Use outputValidation: 'warn' for non-idempotent tools.
≈ “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 Tools
Define tools with createTool(), attach them to agents, and control how the model sees, calls, and receives them.
When to use
- Writing a new tool or fixing a
createTool({...})definition - Choosing a schema library (Zod, Valibot, ArkType) for tool input and output
- Shaping what the model receives (
toModelOutput) versus what the UI or transcript receives (transform) - Tools the browser runs (client tools,
renderOnly) - Logging, auditing, or blocking tool calls with agent
hooksor tool lifecycle hooks - Forcing or limiting tools per call with
toolChoiceandactiveTools - Adding built-in tools: web search, web fetch, ask user, submit plan, task tracking
- Running multi-tool computations in one call with code mode
- Agents with large tool libraries that need on-demand tool loading
When NOT to use
- Agent definition, instructions, and
generate()/stream()basics (usemastra-agents) - Approval gates and
suspend()/resume flows in depth (usemastra-agent-approval-hitl) - Typed final answers from the agent (use
mastra-structured-output) - Tools from MCP servers, or exposing your tools over MCP (use
mastra-mcp) - Ready-made third-party tools and Mastra Connect (use
mastra-tool-integrations) - Subagents exposed as tools and delegation (use
mastra-multi-agent) - Retrieval tools such as vector query (use
mastra-rag) - Sandboxes and workspace tools (use
mastra-workspace-sandbox) - Tools inside workflow steps (use
mastra-workflows)
Required inputs
- What the tool does, in one sentence the model can act on
- Input fields and their types, and which are optional
- Output fields the app needs, and what the model actually needs to see
- Side effects (writes, payments, emails) and whether a retry is safe
- Secrets or per-user values the tool needs (pass them through
requestContext, not input) - Where it runs: server (
execute), client (noexecute), or display only (renderOnly)
The framework: define, attach, control
1. Define
// src/mastra/tools/weather-tool.ts
import { createTool } from '@mastra/core/tools'
import { z } from 'zod'
export const weatherTool = createTool({
id: 'weather-tool',
description: 'Fetches weather for a location',
inputSchema: z.object({
location: z.string(),
}),
outputSchema: z.object({
location: z.string(),
temperatureCelsius: z.number(),
conditions: z.string(),
}),
execute: async ({ location }, { abortSignal }) => {
const response = await fetch(`https://wttr.in/${location}?format=j1`, {
signal: abortSignal,
})
const data = await response.json()
return {
location,
temperatureCelsius: Number(data.current_condition[0].temp_C),
conditions: data.current_condition[0].weatherDesc[0].value,
}
},
})execute has exactly one signature: execute(inputData, context). The first argument is validated against inputSchema. The second carries requestContext, abortSignal, tracingContext, agent, workflow, mcp, and observe. Omit it when unused. Other shapes are outdated.
2. Attach
import { Agent } from '@mastra/core/agent'
import { weatherTool } from '../tools/weather-tool'
export const weatherAgent = new Agent({
id: 'weather-agent',
name: 'Weather Agent',
instructions: `
You are a helpful weather assistant.
Use the weatherTool to fetch current weather data.`,
model: 'openai/gpt-6.1-sol',
tools: { weatherTool },
})The object key is the toolName in streams and traces, not the tool id. tools: { weatherTool } yields weatherTool. tools: { [weatherTool.id]: weatherTool } yields weather-tool. Subagents become agent-<key>, workflows become workflow-<key>.
3. Control per call
await agent.generate('Check the forecast', {
toolChoice: 'required', // 'auto' | 'none' | 'required' | { type: 'tool', toolName }
activeTools: ['weatherTool'], // keys, not ids
})The tool surface at a glance
| Need | Use |
| Model sees a smaller or multimodal result | toModelOutput |
| UI stream or transcript hides secrets | transform.display / transform.transcript |
| Side effect already happened, schema mismatch | outputValidation: 'warn' |
| Exact schema-matching arguments | strict: true |
| Browser runs the tool | omit execute, or pass via clientTools |
| Browser only renders a card | renderOnly: true |
| Log, audit, or block every call | agent hooks.beforeToolCall / afterToolCall |
| React to streaming input or output of one tool | onInputStart, onInputDelta, onInputAvailable, onOutput |
| Human approval | requireApproval: true (see mastra-agent-approval-hitl) |
| Many tools, one sandboxed call | createCodeMode() |
| Hundreds of tools | ToolSearchProcessor |
Workflow
- Verify the API. If
node_modules/@mastra/exists, readnode_modules/@mastra/core/dist/docs/and the installedcreateTooltypes. Otherwise fetchhttps://mastra.ai/docs/agents/tools.mdandhttps://mastra.ai/reference/tools/create-tool.md. Trust these over memory. - Write the description. One short sentence on what the tool does and its main use case. The model picks tools from descriptions and schema field names.
- Write the schemas. Use any Standard JSON Schema library. Add
.describe()on non-obvious fields. Make optional fields explicit. - Implement
execute(inputData, context). Destructure input fields. Passcontext.abortSignaltofetch. Read secrets and tenant ids fromcontext.requestContext. Return only whatoutputSchemadeclares. - Decide what the model sees. Add
toModelOutputwhen the raw result is large. Addtransformwhen the UI or transcript must not see raw values. - Mark side effects. Use
outputValidation: 'warn'for non-idempotent tools. Gate destructive tools withrequireApproval: true. - Attach to the agent with a deliberate key, and name each tool in the instructions with the condition for calling it.
- Add guards. Agent
hooksfor audit and policy.toolChoice/activeToolsper call when the route already knows what is allowed. - Scale. Over roughly a dozen related tools: code mode for multi-tool computations, or
ToolSearchProcessorfor large catalogs (seereferences/built-in-and-advanced.md). - Test the tool alone with
npx mastra api tool execute weather-tool '{"location":"San Francisco"}'againstnpx mastra dev. Runnpx mastra api tool execute --schemabefore building other inputs. Then test through the agent in Studio.
Deep dive: client tools and hooks
A tool without execute is a client tool. The run stops with finishReason: 'tool-calls' until the client sends the result on a new request. Pick the pattern by what the model needs back:
- The model needs the client's result: omit
execute. The client runs it and sends a follow-up request. - The client only renders the call: set
renderOnly: true. Mastra returns{ acknowledged: true }and the agent continues. Do not add an incompatibleoutputSchema. - The tool needs a structured answer from the user: call
suspend()inexecute(seemastra-agent-approval-hitl).
Subagents cannot run client tools. Give client tools to the supervisor.
Agent-level hooks apply to every tool source (assigned, memory, toolsets, client, agent and workflow tools, workspace tools):
export const guardedAgent = new Agent({
id: 'guarded-agent',
name: 'guarded-agent',
instructions: 'Run shell commands for the user.',
model: 'openai/gpt-6.1-sol',
hooks: {
beforeToolCall: ({ toolName, input }) => {
const command = (input as { command?: string }).command ?? ''
if (toolName === 'execute_command' && command.includes('rm -rf')) {
return { proceed: false, output: 'Command blocked by policy.' }
}
},
afterToolCall: ({ toolName, output, error }) => {
console.log(`Finished ${toolName}`, { output, error })
},
},
})Per-call hooks on generate()/stream() merge per key with agent hooks. Tool lifecycle hook errors are logged and never fail the call.
Failure patterns
- Old
executeshapes such asexecute: async ({ context }) => .... Useexecute: async (inputData, context) => .... activeToolsortoolChoice.toolNameuses the toolid. They use the object key fromtools: {...}. Use matching keys.- The model never calls the tool. The instructions do not mention it, or the description is vague. Name the tool and when to call it.
- The model retries a payment or order. The result failed
outputSchemaand became a validation error. SetoutputValidation: 'warn'or fix the schema. - Huge tool results bloat context. Return the full object to the app and use
toModelOutputto send a short summary. - Secrets leak into the UI stream.
toModelOutputdoes not affect display. Addtransform.displayandtransform.transcript. - Client tool inside a subagent. The subagent stops and returns
subAgentPendingToolCalls. Move the tool to the supervisor. - Trusting
onOutputfor client results. The server does not validate client output againstoutputSchema. Treat it as untrusted and keep the hook idempotent. webSearchToolon an unsupported provider. The run fails withMastraError. Use OpenAI, Anthropic, Google Gemini, or xAI models.- Code mode without a boundary.
createCodeMode()needs asandbox, a workspace sandbox, or an isolating transport.LocalSandboxruns model code on the host. - Task tools without memory. They return an error. Add
memoryto the agent. - Too many tools on one agent. Selection degrades. Use
ToolSearchProcessor, code mode, or split into subagents.
Output format
A finished tool delivery contains:
src/mastra/tools/<tool-id>.tsexporting the tool, withid,description,inputSchema,outputSchema,execute- The tool attached to an agent under a deliberate key, and named in the agent instructions
- Any secrets read from
requestContextor env vars, listed in.env.example - Side-effect handling:
outputValidation, approval, or idempotency noted - Evidence it works: a
npx mastra api tool executecall, then an agent run in Studio
Reference files
references/api.md-createTool()options, execution context, lifecycle hooks, agent hooks, and per-call tool options.references/built-in-and-advanced.md- Built-in tools, code mode and transports,ToolSearchProcessor.references/examples.md- Complete tools: API tool, redacting tool, client and render-only tools, code mode agent, tool search agent.references/checklist.md- Pre-production checklist for tools.
Live docs
- https://mastra.ai/docs/agents/tools
- https://mastra.ai/reference/tools/create-tool
- https://mastra.ai/docs/agents/code-mode
- https://mastra.ai/reference/tools/create-code-mode
- https://mastra.ai/reference/tools/ask-user-tool
- https://mastra.ai/reference/tools/submit-plan-tool
- https://mastra.ai/reference/tools/task-tools
- https://mastra.ai/reference/processors/tool-search-processor
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.

