mcpField
M
mastra-best-practices

mastra-best-practices

github.com/mastra-ai開発9987 回インストール1 日前に更新
リポジトリ

mastra-best-practices について

mastra-best-practices は Mastra で開発する人向けのクイックリファレンススキルです。TypeScript の設定、プロジェクト構成、エージェントやワークフローの基本的なコード例をまとめています。

主な機能

  1. 01エージェントとワークフローの使い分け
  2. 02必須の ES2022 TypeScript 設定
  3. 03プロジェクトディレクトリ構成の例
  4. 04エージェントとワークフローのコード例
  5. 05構造化出力とストリーミングの説明

活用シーン

  1. 01Mastra プロジェクトの構成
  2. 02TypeScript のモジュール設定
  3. 03タスクに応じたエージェントまたはワークフローの選択

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/mastra-ai/skills --skill mastra-best-practices

インストール後、Claude Code で/mastra-best-practicesを入力して読み込まれたか確認します。

詳細

Mastra のベストプラクティス

Mastra の規約をすばやく確認できます。詳細は mastra.ai/docs をご覧ください。


エージェントとワークフロー

用途使用する場面
エージェント自由度の高いタスク(サポート、調査)。推論し、ツールを選択し、メモリを保持します
ワークフロー定義済みの手順(パイプライン、承認)。特定のステップを順番に実行します

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=...

クイックリファレンス

基本的なエージェント
new Agent({
  id: 'my-agent',
  name: 'My Agent',
  instructions: 'You are helpful',
  model: 'openai/gpt-4o',
  tools: { myTool }  // optional
})
基本的なワークフロー
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)
}

主な規約

  1. 直接インポートするのではなく、mastra.getAgent('name') / mastra.getWorkflow('name') を使用します
  2. API キーには必ず環境変数を使用します
  3. ES2022 モジュールが必須です(CommonJS は使用しません)
  4. Studio でテストします:npm run dev → http://localhost:4111

リソース

コメント