メインコンテンツへ移動

手動インストール

このガイドでは、スタンドアロンの Mastra サーバーを手動で段階的に構築します。ほとんどの場合、クイックスタートガイドに従う方が短時間で済みます。このガイドでは、create-mastra コマンドを使用して同じ結果を得られます。既存のプロジェクトでは、mastra init も使用できます。

手順
手順への直接リンク

自動 CLI Tool を使用しない場合は、以下のガイドに従ってプロジェクトを自分でセットアップできます。

  1. 新しいプロジェクトを作成し、そのディレクトリに移動します。

    mkdir my-first-agent && cd my-first-agent

    新しい package.json ファイルを生成します。

    npm init

    次の依存関係をインストールします。

    npm install -D typescript @types/node mastra@latest
    npm install @mastra/core@latest zod@^4

    package.json ファイルに dev スクリプトと build スクリプトを追加します。

    package.json
    {
    "scripts": {
    "dev": "mastra dev",
    "build": "mastra build"
    }
    }
  2. 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 を使用すると、解決エラーが発生します。

  3. .env ファイルを作成します。

    touch .env

    API キーを追加します。

    .env
    GOOGLE_API_KEY=<your-api-key>
    注記

    このガイドでは Google Gemini を使用しますが、OpenAI、Anthropic など、サポートされている任意のモデル Provider を使用できます。

  4. weather-tool.ts ファイルを作成します。

    mkdir -p src/mastra/tools && touch src/mastra/tools/weather-tool.ts

    次のコードを追加します。

    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 ({ location }) => {
    return {
    output: `The weather in ${location} is sunny`,
    }
    },
    })
    情報

    ここでは weatherTool の例を短く簡略化しています。完全な天気 Tool は、Agent に Tool を追加するで確認できます。

  5. weather-agent.ts ファイルを作成します。

    mkdir -p src/mastra/agents && touch src/mastra/agents/weather-agent.ts

    次のコードを追加します。

    src/mastra/agents/weather-agent.ts
    import { 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 informative

    Use the weatherTool to fetch current weather data.
    `,
    model: 'google/gemini-2.5-pro',
    tools: { weatherTool },
    })
  6. Mastra のエントリーポイントを作成し、Agent を登録します。

    touch src/mastra/index.ts

    次のコードを追加します。

    src/mastra/index.ts
    import { Mastra } from '@mastra/core'
    import { weatherAgent } from './agents/weather-agent.ts'

    export const mastra = new Mastra({
    agents: { weatherAgent },
    })
  7. これで Studio を起動し、Agent をテストできます。

    npm run dev

次のステップ
次のステップへの直接リンク