Mastraをインストール
Mastraを始めるには、大規模言語モデル(LLM)へのアクセスが必要です。デフォルトでは、MastraはOpenAI と連携するように設定されているため、開始するにはAPIキーが必要です。
Mastraは他のLLMプロバイダーもサポートしています。サポートされているモデルの完全なリストとセットアップ手順については、モデルプロバイダーをご覧ください。
前提条件
- Node.js
v20.0
以上 - サポートされているModel ProviderからのAPIキー
create-mastra
CLIを使用してインストール
私たちのCLIはMastraを始める最も速い方法です。以下のコマンドを実行してインタラクティブセットアップを開始してください:
npx create-mastra@latest
CLIフラグを使用してインストール
必要なフラグをすべて渡すことで、Mastra CLIを非インタラクティブモードで実行することもできます。例えば:
npx create-mastra@latest --project-name hello-mastra --example --components tools,agents,workflows --llm openai
利用可能なCLIオプションの完全なリストについては、create-mastraのドキュメントを参照してください。
APIキーを追加
.env
ファイルにAPIキーを追加してください:
OPENAI_API_KEY=<your-api-key>
この例ではOpenAIを使用しています。各LLMプロバイダーは固有の名前を使用します。詳細についてはModel Capabilitiesを参照してください。
これでMastra Development Serverを起動し、Mastra Playgroundを使用してエージェントをテストできます。
手動でインストール
以下の手順では、Mastraを手動でインストールする方法を説明します。
新しいプロジェクトを作成する
新しいプロジェクトを作成してディレクトリを変更します:
mkdir hello-mastra && cd hello-mastra
@mastra/core
パッケージを含むTypeScriptプロジェクトを初期化します:
npm init -y
npm install typescript tsx @types/node mastra@latest --save-dev
npm install @mastra/core@latest zod @ai-sdk/openai
package.json
にdev
とbuild
スクリプトを追加します:
{
"scripts": {
// ...
"dev": "mastra dev",
"build": "mastra build"
}
}
TypeScriptを初期化する
tsconfig.json
ファイルを作成します:
touch tsconfig.json
以下の設定を追加します:
{
"compilerOptions": {
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "bundler",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"noEmit": true,
"outDir": "dist"
},
"include": [
"src/**/*"
]
}
このTypeScript設定はMastraプロジェクト用に最適化されており、モダンなモジュール解決と厳密な型チェックを使用しています。
APIキーを設定する
.env
ファイルを作成します:
touch .env
APIキーを追加します:
OPENAI_API_KEY=<your-api-key>
この例ではOpenAIを使用しています。各LLMプロバイダーは固有の名前を使用します。詳細についてはModel Capabilitiesを参照してください。
ツールを作成する
weather-tool.ts
ファイルを作成します:
mkdir -p src/mastra/tools && touch 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"
};
}
});
完全なweatherToolの例については、Giving an Agent a Toolを参照してください。
エージェントを作成する
weather-agent.ts
ファイルを作成します:
mkdir -p src/mastra/agents && touch src/mastra/agents/weather-agent.ts
以下のコードを追加します:
import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent";
import { weatherTool } from "../tools/weather-tool";
export const weatherAgent = new 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: openai('gpt-4o-mini'),
tools: { weatherTool }
});
エージェントを登録する
Mastraのエントリーポイントを作成し、エージェントを登録します:
touch src/mastra/index.ts
以下のコードを追加します:
import { Mastra } from "@mastra/core/mastra";
import { weatherAgent } from "./agents/weather-agent";
export const mastra = new Mastra({
agents: { weatherAgent }
});
これでMastra Development Serverを起動し、Mastra Playgroundを使用してエージェントをテストできます。
既存のプロジェクトに追加
Mastraは幅広いプロジェクトにインストールして統合することができます。以下は、開始に役立つ統合ガイドへのリンクです:
mastra init
既存のプロジェクトにMastraをインストールするには、mastra init
コマンドを使用します。
詳細についてはmastra initを参照してください。