关于 mastra-best-practices
mastra-best-practices 是 Mastra 项目的快速参考技能,面向编写 Agent 和工作流的开发者。它汇总 TypeScript 配置、项目结构及常见开发约定,并提供基础代码示例。
主要功能
- 01区分 Agent 与工作流的适用场景
- 02说明 ES2022 TypeScript 配置
- 03提供项目目录结构参考
- 04展示 Agent 与工作流示例
- 05介绍结构化输出与流式响应
适用场景
- 01搭建 Mastra 项目目录
- 02配置 TypeScript 模块选项
- 03选择 Agent 或工作流实现任务
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mastra-ai/ skills --skill mastra-best-practices安装后在 Claude Code 中输入/mastra-best-practices确认已加载。
详细介绍
Mastra 最佳实践
Mastra 约定速查。详情请参阅 mastra.ai/docs。
Agent 与 Workflow
| 用途 | 适用场景 |
|---|---|
| Agent | 开放式任务(支持、研究)——进行推理、选择工具并保留记忆 |
| Workflow | 定义好的流程(流水线、审批)——按顺序编排特定步骤 |
TypeScript 配置(必需)
{
"compilerOptions": {
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "bundler"
}
}
重要: CommonJS 会导致错误。必须使用 ES2022。
项目结构
src/mastra/
├── tools/ # Custom tools
├── agents/ # Agent configs
├── workflows/ # Workflows
└── index.ts # Mastra instance
环境变量
OPENAI_API_KEY=...
ANTHROPIC_API_KEY=...
GOOGLE_GENERATIVE_AI_API_KEY=...
速查
基本 Agent
new Agent({
id: 'my-agent',
name: 'My Agent',
instructions: 'You are helpful',
model: 'openai/gpt-4o',
tools: { myTool } // optional
})
基本 Workflow
createWorkflow({
id: 'my-workflow',
inputSchema: z.object({ input: z.string() }),
outputSchema: z.object({ output: z.string() })
})
.then(step1)
.commit()
结构化输出
const response = await agent.generate('Extract: John, 30', {
structuredOutput: {
schema: z.object({
name: z.string(),
age: z.number()
})
}
})
console.log(response.object) // Access via .object property
流式传输
const stream = await agent.stream('Tell a story')
for await (const chunk of stream.fullStream) {
console.log(chunk)
}
关键约定
- 使用
mastra.getAgent('name')/mastra.getWorkflow('name'),而不是直接导入 - 始终使用环境变量存储 API 密钥
- 必须使用 ES2022 模块(而不是 CommonJS)
- 使用 Studio 测试:
npm run dev→ http://localhost:4111
资源
评论
支持 Markdown