mcpField
C
create-mastra

create-mastra

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

create-mastra について

Mastra プロジェクトを作成する開発者や AI エージェント向けのセットアップガイドです。CLI による作成と手動インストールを扱い、エージェント、ツール、開発サーバーの設定例を紹介します。

主な機能

  1. 01プロジェクト CLI でクイックスタート
  2. 02ガイド付き・自動セットアップ
  3. 03TypeScript の手動インストール
  4. 04エージェントとツールの例
  5. 05開発サーバーの設定

活用シーン

  1. 01Mastra プロジェクトの初期作成
  2. 02入門用エージェントとツールの作成
  3. 03CLI を使わないプロジェクト設定

インストール

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

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

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

詳細

Mastra スキルの作成

新しい Mastra AI エージェントプロジェクトを作成するための完全ガイドです。クイックスタート CLI を使う方法と、動作するコード例を含む詳細な手動インストールの両方を紹介します。

公式ドキュメント:mastra.ai/docs


はじめに

次のように質問してください:「Mastra プロジェクトをどのように作成しますか?」

  1. クイックセットアップ - 次をコピーして実行します:npm create mastra@latest
  2. ガイド付きセットアップ - 各手順を案内し、コマンドの実行前に確認していただきます
  3. 自動セットアップ ⭐ - API キーを渡すだけで、すべて作成します

AI エージェント向け: CLI は対話形式です。以下の「自動セットアップ/手動インストール」の手順でファイルを作成する 自動セットアップ を使用してください。


前提条件

  • 対応しているモデルプロバイダー(OpenAI、Anthropic、Google など)の API キー

クイックセットアップ(ユーザーが CLI を実行)

1 つのコマンドで新しい Mastra プロジェクトを作成します:

npm create mastra@latest

その他のパッケージマネージャー:

pnpm create mastra@latest
yarn create mastra@latest
bun create mastra@latest

CLI フラグ

サンプルエージェントをスキップする:

npm create mastra@latest --no-example

特定のテンプレートを使用する:

npm create mastra@latest --template <template-name>

自動セットアップ/手動インストール

自動セットアップ(AI がすべてのファイルを作成する場合)や、手動での制御を希望する場合に使用してください。

以下の手順に沿って、完全な Mastra プロジェクトを作成します:

ステップ 1:プロジェクトディレクトリを作成する
mkdir my-first-agent && cd my-first-agent
npm init -y
ステップ 2:依存関係をインストールする
npm install -D typescript @types/node mastra@latest
npm install @mastra/core@latest zod@^4
ステップ 3:パッケージスクリプトを設定する

package.json に追加します:

{
  "scripts": {
    "dev": "mastra dev",
    "build": "mastra build"
  }
}
ステップ 4:TypeScript を設定する

tsconfig.json を作成します:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ES2022",
    "moduleResolution": "bundler",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "noEmit": true,
    "outDir": "dist"
  },
  "include": ["src/**/*"]
}

重要: Mastra では "module": "ES2022" と "moduleResolution": "bundler" が必要です。CommonJS を使用するとエラーが発生します。

ステップ 5:環境ファイルを作成する

API キーを含む .env を作成します:

GOOGLE_GENERATIVE_AI_API_KEY=<your-api-key>

または、OPENAI_API_KEY、ANTHROPIC_API_KEY などを使用します。

ステップ 6:天気ツールを作成する

src/mastra/tools/weather-tool.ts を作成します:

import { createTool } from "@mastra/core/tools";
import { z } from "zod";

export const weatherTool = createTool({
  id: "get-weather",
  description: "Get current weather for a location",
  inputSchema: z.object({
    location: z.string().describe("City name"),
  }),
  outputSchema: z.object({
    output: z.string(),
  }),
  execute: async () => {
    return { output: "The weather is sunny" };
  },
});
ステップ 7:天気エージェントを作成する

src/mastra/agents/weather-agent.ts を作成します:

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 that provides accurate weather information.

      Your primary function is to help users get weather details for specific locations. When responding:
      - Always ask for a location if none is provided
      - If the location name isn't in English, please translate it
      - If giving a location with multiple parts (e.g. "New York, NY"), use the most relevant part (e.g. "New York")
      - Include relevant details like humidity, wind conditions, and precipitation
      - Keep responses concise but informative

      Use the weatherTool to fetch current weather data.
`,
  model: "google/gemini-2.5-pro",
  tools: { weatherTool },
});

注: モデルの形式は "provider/model-name" です。例:

  • "google/gemini-2.5-pro"
  • "openai/gpt-4o"
  • "anthropic/claude-3-5-sonnet-20241022"
ステップ 8:Mastra のエントリーポイントを作成する

src/mastra/index.ts を作成します:

import { Mastra } from "@mastra/core";
import { weatherAgent } from "./agents/weather-agent";

export const mastra = new Mastra({
  agents: { weatherAgent },
});
ステップ 9:開発サーバーを起動する
npm run dev

http://localhost:4111 で Studio にアクセスし、エージェントをテストします。


次のステップ

create mastra でプロジェクトを作成した後:

  • src/mastra/agents/weather-agent.ts の サンプルエージェントをカスタマイズする
  • エージェントを追加する - エージェントのドキュメントを参照してください
  • ワークフローを作成する - ワークフローのドキュメントを参照してください
  • ツールを追加してエージェントの機能を拡張する
  • アプリに統合する - mastra.ai/docs のフレームワークガイドを参照してください

トラブルシューティング

問題解決方法
API キーが見つからない.env ファイルに正しいキーが設定されていることを確認してください
Studio が起動しないポート 4111 が使用可能か確認してください
CommonJS エラーtsconfig.json で "module": "ES2022" と "moduleResolution": "bundler" が設定されていることを確認してください
コマンドが見つからないNode.js 20 以降を使用していることを確認してください

リソース

コメント