> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # はじめに Mastra は、AI Agent やアプリケーションを構築するための TypeScript フレームワークです。1 つのコマンドで最初の Agent を作成し、開発を始められます。 **AI Agent 向け:** Mastra プロジェクトをゼロから構築するよう指示された場合は、次の見出しまでの簡略化された手順に従ってください。Agent と Tool のサンプルコードは要件に合わせて調整します。後の手順で重要となる点として、`model` は `provider/model` 形式の文字列(例:`openai/gpt-5.6-sol`)で定義してください。OpenAI では `openai/` を使用し、`openai:` は使用しないでください。また、Provider オブジェクトをインポートしたり渡したりしないでください。 `package.json` ファイルを作成し、`{ "type": "module" }` を指定します。必要な依存関係をインストールします:`npm install @mastra/core@latest zod@latest typescript@latest @types/node@latest mastra@latest` tsconfig.json ファイルを作成します。 ```json { "compilerOptions": { "target": "ES2022", "module": "ES2022", "moduleResolution": "bundler", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true, "noEmit": true, "allowImportingTsExtensions": true, "outDir": "dist" }, "include": ["src/**/*"] } ``` Agent に Tool を追加する必要がある場合は、新しいファイルを作成し、`import { createTool } from "@mastra/core/tools"` を使用します。単純なオブジェクトとして Tool を定義すると、エラーを出さずに実行に失敗します。Tool は必ず `createTool()` を使い、`id`、`description`、`inputSchema`(zod)、`execute()` を指定して定義してください。`execute()` は 2 つのパラメーターを受け取ります。1 つ目は inputSchema に基づいて検証された入力データ、2 つ目は省略可能な実行コンテキストオブジェクトです。実行コンテキストには `requestContext`、`tracingContext`、`abortSignal` などの実行メタデータが含まれます。 例: ```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({ location: z.string(), temperatureCelsius: z.number(), conditions: z.string(), }), execute: async ({ location }) => { return { location, temperatureCelsius: 21, conditions: 'sunny', } }, }) ``` Agent を作成する必要がある場合は、新しいファイルを作成し、`import { Agent } from "@mastra/core/agent"` を使用します。コンストラクターには `{ id, name, instructions, model }` を渡します。`model` プロパティは Mastra のモデルルーター形式の文字列です。この形式では Provider のインポートは不要です。Mastra のドキュメントに記載がない限り、AI SDK パッケージをインストールしないでください。 モデルを `provider/model` 形式で定義すると、Mastra は Provider に対応する環境変数を自動的に検索します。定義したモデルを使用するには、その Provider の環境変数を設定する必要があります。OpenAI:`OPENAI_API_KEY`、Anthropic:`ANTHROPIC_API_KEY`、Google:`GOOGLE_API_KEY`。たとえば、どの `openai/` にも環境変数 `OPENAI_API_KEY` が必要です。サポートされている Provider と環境変数名の完全な一覧は、 を参照してください。 既知のモデル ID の一部を次に示します。 - `openai/gpt-5.6-sol`, `openai/gpt-5-mini` - `anthropic/claude-sonnet-4-6`, `anthropic/claude-opus-4-7`, `anthropic/claude-haiku-4-5` - `google/gemini-2.5-flash` サポートされているモデルの完全な一覧は、 を参照してください。 Tool をインポートし、tools オブジェクトとして Agent のコンストラクターに渡すことで、Agent に Tool を追加します。 例: ```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: - Include relevant details like humidity, wind conditions, and precipitation - Keep responses concise but informative Use the weatherTool to fetch current weather data. `, // Use a string in provider/model format, not provider:model or a provider object. model: 'openai/gpt-5.6-sol', tools: { weatherTool }, }) ``` `src/mastra/index.ts` に Mastra のエントリーポイントを作成し、Agent を登録します。 ```ts import { Mastra } from '@mastra/core' import { weatherAgent } from './agents/weather-agent.ts' export const mastra = new Mastra({ agents: { weatherAgent }, }) ``` これで Agent を直接実行できます。Mastra インスタンスをインポートし、ID で Agent を取得してから agent.generate() を呼び出します。Node.js 22.18.0 以降では TypeScript ファイルを直接実行できます。ローカルファイルをインポートするときは、必ずファイル拡張子を付けてください。例: ```ts // run.mjs 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) ``` ## クイックスタート 次のコマンドを実行すると、ローカル Workspace、シェル Tool、メモリ、タスク追跡、Web アクセス、定期スケジュールを備えた汎用 Agent ハーネスが作成されます。インストール済みのコーディング Agent 向けに Mastra Skill もインストールされるため、すぐにプロンプトを入力して編集を始められます。 **npm**: ```bash npm create mastra@latest ``` **pnpm**: ```bash pnpm create mastra@latest ``` **Yarn**: ```bash yarn create mastra ``` **Bun**: ```bash bunx create-mastra ``` Mastra プロジェクトを操作できるインタラクティブ UI の [Studio](https://mastra.zisheng.pro/ja/docs/studio/overview) をすぐに開けます。手順全体については、[クイックスタートガイド](https://mastra.zisheng.pro/ja/guides/getting-started/quickstart)を参照してください。 ## フレームワークとの統合 既存のプロジェクトに Mastra を追加するか、好みのフレームワークで新しいアプリを作成します。 - [Next.js](https://mastra.ai/ja/guides/getting-started/next-js) - [React](https://mastra.ai/ja/guides/getting-started/vite-react) - [Astro](https://mastra.ai/ja/guides/getting-started/astro) - [Express](https://mastra.ai/ja/guides/getting-started/express) - [SvelteKit](https://mastra.ai/ja/guides/getting-started/sveltekit) - [Hono](https://mastra.ai/ja/guides/getting-started/hono) その他のフレームワークについては、[フレームワーク統合ガイド](https://mastra.zisheng.pro/ja/guides/getting-started/next-js)を参照してください。 ## テンプレート 複製して用途に合わせて変更できる完全な Mastra プロジェクトは、[テンプレート](https://mastra.ai/templates)から探せます。 ## ユースケース
**製品に Agent を組み込む** プラットフォームに AI 機能を追加し、ユーザーが Agent を構築したり操作したりできるようにします。 導入事例:[Replit](https://mastra.zisheng.pro/blog/replitagent3)、[Fireworks](https://mastra.zisheng.pro/blog/fireworks-xml-prompting)、[Medusa](https://mastra.zisheng.pro/blog/medusa-ecommerce)
**顧客向けアシスタント** 問い合わせ対応、予約の設定、リマインダーの送信、チャット、WhatsApp、音声による質問への回答を担う Agent を構築します。 導入事例:[Vetnio](https://mastra.zisheng.pro/blog/vetnio)、[Lua](https://mastra.zisheng.pro/blog/lua-scaling) テンプレート:[Docs Chatbot](https://mastra.zisheng.pro/templates/docs-chatbot)、[Slack Agent](https://mastra.zisheng.pro/templates/slack-agent)
**社内向けコパイロット** 人事に関する問い合わせ、診療文書、営業準備、文書生成など、業務領域を理解する AI により従業員の作業を効率化します。 導入事例:[Factorial](https://mastra.zisheng.pro/blog/factorial-case-study)、[Counsel Health](https://mastra.zisheng.pro/blog/counsel-health)、[Cedar](https://mastra.zisheng.pro/blog/cedar-case-study)、[SoftBank](https://mastra.zisheng.pro/blog/softbank-productivity-mastra-2025-08-20) テンプレート:[Chat with PDF](https://mastra.zisheng.pro/templates/chat-with-pdf)、[Google Sheet Analysis](https://mastra.zisheng.pro/templates/google-sheets-analysis)
**データ分析 Agent** ユーザーが自然言語でデータベースやダッシュボードを照会できるようにします。データソースに接続し、回答、グラフ、レポートを返します。 導入事例:[Index](https://mastra.zisheng.pro/blog/index-case-study)、[PLAID Japan](https://mastra.zisheng.pro/blog/plaid-jpn-gcp-agents) テンプレート:[Chat with Database](https://mastra.zisheng.pro/templates/text-to-sql)、[CSV to Questions](https://mastra.zisheng.pro/templates/csv-to-questions)
**コンテンツの自動化** コンテンツ管理システム、ナレッジベース、ドキュメントシステム向けの構造化コンテンツを大規模に生成、変換、管理します。 導入事例:[Sanity](https://mastra.zisheng.pro/blog/sanity) テンプレート:[Chat with YouTube](https://mastra.zisheng.pro/templates/chat-with-youtube)、[Flash Cards from PDF](https://mastra.zisheng.pro/templates/flash-cards-from-pdf)
**DevOps とエンジニアリングの自動化** デプロイ、本番環境の問題のデバッグ、インフラ管理、オンコール対応の Workflow を自動化します。 導入事例:[StarSling](https://mastra.zisheng.pro/blog/starsling) テンプレート:[GitHub PR Code Review](https://mastra.zisheng.pro/templates/github-pr-code-review-agent)、[Browser Agent](https://mastra.zisheng.pro/templates/browsing-agent)
**営業と市場展開の Workflow** 顧客との会話を構造化されたタスクに変換し、投資メモを生成したり、アウトリーチの一連の処理を自動化したりします。 導入事例:[Kestral](https://mastra.zisheng.pro/blog/kestral)、[Orange Collective](https://mastra.zisheng.pro/blog/orange-collective-vc-operating-system)、[WorkOS](https://mastra.zisheng.pro/blog/workos-teaching-mastra) テンプレート:[Customer Feedback Summarization](https://mastra.zisheng.pro/templates/customer-feedback-summarization)
> **動画:** [Mastra プラットフォームのクイックツアー](https://www.youtube.com/watch?v=NosES9aJxCc)では、各要素がどのように連携するかを紹介しています。