手動インストール
このガイドでは、スタンドアロンの Mastra サーバーを手動で段階的に構築します。ほとんどの場合、クイックスタートガイドに従う方が短時間で済みます。このガイドでは、create-mastra コマンドを使用して同じ結果を得られます。既存のプロジェクトでは、mastra init も使用できます。
https://mastra.ai/guides/getting-started/manual-install.md を読んでから、Mastra プロジェクトの作成を手伝ってください。プロジェクト名を質問し、その名前をフォルダー名として使用してください。
手順手順への直接リンク
自動 CLI Tool を使用しない場合は、以下のガイドに従ってプロジェクトを自分でセットアップできます。
新しいプロジェクトを作成し、そのディレクトリに移動します。
mkdir my-first-agent && cd my-first-agent新しい
package.jsonファイルを生成します。- npm
- pnpm
- Yarn
- Bun
npm initpnpm inityarn initbun init次の依存関係をインストールします。
- npm
- pnpm
- Yarn
- Bun
npm install -D typescript @types/node mastra@latestnpm install @mastra/core@latest zod@^4pnpm add -D typescript @types/node mastra@latestpnpm add @mastra/core@latest zod@^4yarn add --dev typescript @types/node mastra@latestyarn add @mastra/core@latest zod@^4bun add --dev typescript @types/node mastra@latestbun add @mastra/core@latest zod@^4package.jsonファイルにdevスクリプトとbuildスクリプトを追加します。package.json{"scripts": {"dev": "mastra dev","build": "mastra build"}}tsconfig.jsonファイルを作成します。touch tsconfig.json次の設定を追加します。
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およびmoduleResolution設定が必要です。CommonJSまたはnodeを使用すると、解決エラーが発生します。.envファイルを作成します。touch .envAPI キーを追加します。
.envGOOGLE_API_KEY=<your-api-key>注記このガイドでは Google Gemini を使用しますが、OpenAI、Anthropic など、サポートされている任意のモデル Provider を使用できます。
weather-tool.tsファイルを作成します。mkdir -p src/mastra/tools && touch src/mastra/tools/weather-tool.ts次のコードを追加します。
src/mastra/tools/weather-tool.tsimport { 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 ({ location }) => {return {output: `The weather in ${location} is sunny`,}},})情報ここでは
weatherToolの例を短く簡略化しています。完全な天気 Tool は、Agent に Tool を追加するで確認できます。weather-agent.tsファイルを作成します。mkdir -p src/mastra/agents && touch src/mastra/agents/weather-agent.ts次のコードを追加します。
src/mastra/agents/weather-agent.tsimport { Agent } from '@mastra/core/agent'import { weatherTool } from '../tools/weather-tool.ts'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 informativeUse the weatherTool to fetch current weather data.`,model: 'google/gemini-2.5-pro',tools: { weatherTool },})Mastra のエントリーポイントを作成し、Agent を登録します。
touch src/mastra/index.ts次のコードを追加します。
src/mastra/index.tsimport { Mastra } from '@mastra/core'import { weatherAgent } from './agents/weather-agent.ts'export const mastra = new Mastra({agents: { weatherAgent },})これで Studio を起動し、Agent をテストできます。
- npm
- pnpm
- Yarn
- Bun
npm run devpnpm run devyarn devbun run dev
次のステップ次のステップへの直接リンク
- プロジェクト構造を確認する:
src/mastra/のファイルが Agent、Tool、Workflow、Storage、設定にどのように対応するかを理解します。 - Studio で Agent をテストする:ローカルの Studio UI を開き、天気 Agent を実行します。
- Agent で Tool を使用する:天気 Tool のサンプルを、API やサービスを呼び出す実際の Tool に置き換えます。
- Memory を追加する:会話履歴とユーザー固有のコンテキストを永続化します。
- Storage を設定する:Memory、Workflow、Observability、その他のランタイム状態に永続 Storage アダプターを追加します。
- ビルドしてデプロイする:Mastra サーバーをビルドし、ホスティングプラットフォームへデプロイします。