> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # Mastra.getAgentById() `.getAgentById()` メソッドは、登録済みの Agent を `id` で取得します。最初に `agent.id` で登録済み Agent を検索します。一致する Agent がなければ [`Mastra.getAgent()`](https://mastra.zisheng.pro/ja/reference/core/getAgent) にフォールバックし、指定された値を Agent の登録キーとして扱います。 コードで定義した Agent を取得するには `mastra.getAgentById(id)` を使用します。バージョン管理された Agent を取得するには `await mastra.getAgentById(id, version)` を使用します。 ## 使用例 次の例では Agent を登録し、ID で取得して、返された Agent を使用します。 ```typescript import { Agent } from '@mastra/core/agent' import { Mastra } from '@mastra/core/mastra' const supportAgent = new Agent({ id: 'support-agent-id', name: 'Support Agent', instructions: 'Answer support questions clearly and concisely.', model: 'openai/gpt-5.6-sol', }) export const mastra = new Mastra({ agents: { supportAgent, }, }) const agent = mastra.getAgentById('support-agent-id') const response = await agent.generate('How can I reset my password?') ``` ## パラメータ **id** (`string`): 取得する Agent の ID。Mastra は最初に agent.id で登録済み Agent を検索します。一致するものがなければ、この値を Agent の登録キーとして getAgent() を呼び出します。 **version** (`{ versionId: string } | { status?: 'draft' | 'published' }`): バージョン管理された Agent を取得するための任意のバージョンセレクター。指定すると、このメソッドは Promise を返します。 **version.versionId** (`string`): 取得する特定の Agent バージョンの ID。 **version.status** (`'draft' | 'published'`): 指定した公開ステータスの最新 Agent バージョンを選択します。 ## 戻り値 **agent** (`TAgents[TAgentName]`): version を省略した場合、指定した ID の Agent インスタンス。 **agent** (`Promise`): version を指定した場合、バージョン管理された Agent インスタンスに解決される Promise。 ID または登録キーで Agent が見つからない場合は `MastraError` をスローします。 ## 関連項目 - [Mastra.getAgent()](https://mastra.zisheng.pro/ja/reference/core/getAgent) - [Agent の概要](https://mastra.zisheng.pro/ja/docs/agents/overview)