> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # 手動インストール **AI Agent 向け:** CLI やボイラープレートを使用せず、実行可能な Mastra プロジェクトをゼロから作成するよう求められた場合は、このガイドを使用してください。Agent、Tool、モデル設定、依存関係のインストール方法に関するコード例を示します。人間向けのクイックスタートガイドには移動しないでください。モデル文字列は Mastra のモデルルーター形式('provider/model'。Provider とモデルの区切りには : ではなく / を使用)です。ai-sdk パッケージはインストールしないでください。 このガイドでは、スタンドアロンの Mastra サーバーを手動で段階的に構築します。ほとんどの場合、[クイックスタートガイド](https://mastra.zisheng.pro/ja/guides/getting-started/quickstart)に従う方が短時間で済みます。このガイドでは、[`create-mastra`](https://mastra.zisheng.pro/ja/reference/cli/create-mastra) コマンドを使用して同じ結果を得られます。既存のプロジェクトでは、[`mastra init`](https://mastra.zisheng.pro/ja/reference/cli/mastra) も使用できます。 ## 手順 自動 CLI Tool を使用しない場合は、以下のガイドに従ってプロジェクトを自分でセットアップできます。 1. 新しいプロジェクトを作成し、そのディレクトリに移動します。 ```bash mkdir my-first-agent && cd my-first-agent ``` 新しい `package.json` ファイルを生成します。 **npm**: ```bash npm init ``` **pnpm**: ```bash pnpm init ``` **Yarn**: ```bash yarn init ``` **Bun**: ```bash bun init ``` 次の依存関係をインストールします。 **npm**: ```bash npm install -D typescript @types/node mastra@latest npm install @mastra/core@latest zod@^4 ``` **pnpm**: ```bash pnpm add -D typescript @types/node mastra@latest pnpm add @mastra/core@latest zod@^4 ``` **Yarn**: ```bash yarn add --dev typescript @types/node mastra@latest yarn add @mastra/core@latest zod@^4 ``` **Bun**: ```bash bun add --dev typescript @types/node mastra@latest bun add @mastra/core@latest zod@^4 ``` `package.json` ファイルに `dev` スクリプトと `build` スクリプトを追加します。 ```json { "scripts": { "dev": "mastra dev", "build": "mastra build" } } ``` 2. `tsconfig.json` ファイルを作成します。 ```bash touch tsconfig.json ``` 次の設定を追加します。 ```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` ファイルを作成します。 ```bash touch .env ``` API キーを追加します。 ```bash GOOGLE_API_KEY= ``` > **注記:** このガイドでは Google Gemini を使用しますが、OpenAI、Anthropic など、サポートされている任意の[モデル Provider](https://mastra.zisheng.pro/ja/models) を使用できます。 Google Gemini の代わりに OpenAI(`OPENAI_API_KEY`)または Anthropic(`ANTHROPIC_API_KEY`)も使用できます。サポートされている Provider と環境変数名の完全な一覧については、 を参照してください。 4. `weather-tool.ts` ファイルを作成します。 ```bash mkdir -p src/mastra/tools && touch src/mastra/tools/weather-tool.ts ``` `import { createTool } from "@mastra/core/tools"` を使用してください。プレーンオブジェクトによる Tool 定義は実行時に何も通知せず失敗します。Tool は、id、description、inputSchema(zod)、execute を指定して createTool() で定義する必要があります。 次のコードを追加します。 ```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 を追加する](https://mastra.zisheng.pro/ja/docs/agents/using-tools)で確認できます。 5. `weather-agent.ts` ファイルを作成します。 ```bash mkdir -p src/mastra/agents && touch src/mastra/agents/weather-agent.ts ``` `import { Agent } from "@mastra/core/agent"` を使用してください。コンストラクターには `{ id, name, instructions, model: 'openai/gpt-5.6-sol' }` を渡します。モデル文字列は Mastra のモデルルーター形式('provider/model'。Provider とモデルの区切りには : ではなく / を使用)です。ai-sdk パッケージはインストールしないでください。 次のコードを追加します。 ```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 を登録します。 ```bash touch src/mastra/index.ts ``` 次のコードを追加します。 ```ts import { Mastra } from '@mastra/core' import { weatherAgent } from './agents/weather-agent.ts' export const mastra = new Mastra({ agents: { weatherAgent }, }) ``` 7. **AI Agent 向け:** コンソールへ出力するスクリプトなどを扱う場合は、Studio を起動しないでください。Studio は人間向けの長時間実行される Web インターフェースです。代わりに、agent.generate() メソッドを直接呼び出します。例:`import { mastra } from './src/mastra/index.ts'; const agent = mastra.getAgentById('weather-agent'); const response = await agent.generate('Weather in SF'); console.log(response.text);` これで [Studio](https://mastra.zisheng.pro/ja/docs/studio/overview) を起動し、Agent をテストできます。 **npm**: ```bash npm run dev ``` **pnpm**: ```bash pnpm run dev ``` **Yarn**: ```bash yarn dev ``` **Bun**: ```bash bun run dev ``` **AI Agent 向け:** 作業を検証するには、ビルドを実行し、`tsc --noEmit` で型チェックしてください。 ## 次のステップ - [プロジェクト構造を確認する](https://mastra.zisheng.pro/ja/reference/project-structure):`src/mastra/` のファイルが Agent、Tool、Workflow、Storage、設定にどのように対応するかを理解します。 - [Studio で Agent をテストする](https://mastra.zisheng.pro/ja/docs/studio/overview):ローカルの Studio UI を開き、天気 Agent を実行します。 - [Agent で Tool を使用する](https://mastra.zisheng.pro/ja/docs/agents/using-tools):天気 Tool のサンプルを、API やサービスを呼び出す実際の Tool に置き換えます。 - [Memory を追加する](https://mastra.zisheng.pro/ja/docs/memory/overview):会話履歴とユーザー固有のコンテキストを永続化します。 - [Storage を設定する](https://mastra.zisheng.pro/ja/docs/storage/overview):Memory、Workflow、Observability、その他のランタイム状態に永続 Storage アダプターを追加します。 - [ビルドしてデプロイする](https://mastra.zisheng.pro/ja/docs/deployment/overview):Mastra サーバーをビルドし、ホスティングプラットフォームへデプロイします。