Skip to Content
リファレンスコアMastra Class

The Mastra Class

Mastraクラスは、あらゆるMastraアプリケーションの中心的なオーケストレーターであり、エージェント、ワークフロー、ストレージ、ログ記録、テレメトリなどを管理します。通常、アプリケーションを調整するためにMastraの単一インスタンスを作成します。

Mastraをトップレベルのレジストリとして考えてください:

  • integrationsを登録することで、agentsworkflowstoolsすべてからアクセス可能になります。
  • toolsMastraに直接登録されませんが、エージェントに関連付けられ、自動的に発見されます。

インポート

import { Mastra } from "@mastra/core";

Constructor

指定された設定で新しいMastraインスタンスを作成します。

constructor(config?: Config);

初期化

Mastraクラスは通常、src/mastra/index.tsファイルで初期化されます:

src/mastra/index.ts
import { Mastra } from "@mastra/core"; import { LibSQLStore } from "@mastra/libsql"; import { weatherAgent } from "./agents/weather-agent"; export const mastra = new Mastra({ agents: { weatherAgent }, storage: new LibSQLStore({ url: ":memory:", }), });

設定オブジェクト

コンストラクタは、動作をカスタマイズし、さまざまなMastraコンポーネントを統合するためのオプションのConfigオブジェクトを受け取ります。Configオブジェクトのすべてのプロパティはオプションです。

プロパティ

agents?:

Agent[]
= []
登録するAgentインスタンスの配列

tools?:

Record<string, ToolApi>
= {}
登録するカスタムツール。キーと値のペアとして構造化され、キーがツール名、値がツール関数になります。

storage?:

MastraStorage
データを永続化するためのストレージエンジンインスタンス

vectors?:

Record<string, MastraVector>
ベクターストアインスタンス。セマンティック検索やベクターベースのツール(例:Pinecone、PgVector、Qdrant)に使用されます

logger?:

Logger
= INFOレベルのコンソールロガー
new PinoLogger()で作成されたLoggerインスタンス

workflows?:

Record<string, Workflow>
= {}
登録するワークフロー。キーと値のペアとして構造化され、キーがワークフロー名、値がワークフローインスタンスになります。

tts?:

Record<string, MastraTTS>
Text-To-Speechサービスを登録するためのオブジェクト。

telemetry?:

OtelConfig
OpenTelemetry統合の設定。

deployer?:

MastraDeployer
デプロイメントを管理するためのMastraDeployerのインスタンス。

server?:

ServerConfig
= { port: 4111, host: localhost, cors: { origin: '*', allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], allowHeaders: ['Content-Type', 'Authorization', 'x-mastra-client-type'], exposeHeaders: ['Content-Length', 'X-Requested-With'], credentials: false } }
ポート、ホスト、タイムアウト、APIルート、ミドルウェア、CORS設定、およびSwagger UI、APIリクエストログ、OpenAPIドキュメントのビルドオプションを含むサーバー設定。

mcpServers?:

Record<string, MCPServerBase>
キーが一意のサーバー識別子で、値がMCPServerのインスタンスまたはMCPServerBaseを拡張するクラスのインスタンスであるオブジェクト。これにより、MastraがこれらのMCPサーバーを認識し、潜在的に管理できるようになります。

bundler:

BundlerConfig
アセットバンドラーの設定。

使用方法

以下のいずれのメソッドもMastraクラスで使用できます。例えば:

example.ts
import { mastra } from "./mastra"; const agent = mastra.getAgent("weatherAgent"); const result = await agent.generate("What's the weather like in London?");

メソッド

getAgent(name):

Agent
IDによってエージェントインスタンスを返します。エージェントが見つからない場合は例外をスローします。

getAgents():

Record<string, Agent>
登録されているすべてのエージェントをキー・バリューオブジェクトとして返します。

getWorkflow(id, { serialized }):

Workflow
IDによってワークフローインスタンスを返します。serializedオプション(デフォルト:false)は、名前のみを含む簡略化された表現を返します。

getWorkflows({ serialized }):

Record<string, Workflow>
登録されているすべてのワークフローを返します。serializedオプション(デフォルト:false)は簡略化された表現を返します。

getVector(name):

MastraVector
名前によってベクターストアインスタンスを返します。見つからない場合は例外をスローします。

getVectors():

Record<string, MastraVector>
登録されているすべてのベクターストアをキー・バリューオブジェクトとして返します。

getDeployer():

MastraDeployer | undefined
設定されているデプロイヤーインスタンスがあれば返します。

getStorage():

MastraStorage | undefined
設定されているストレージインスタンスを返します。

getMemory():

MastraMemory | undefined
設定されているメモリインスタンスを返します。注意:これは非推奨です。メモリはエージェントに直接追加する必要があります。

getServer():

ServerConfig | undefined
ポート、タイムアウト、APIルート、ミドルウェア、CORS設定、ビルドオプションを含むサーバー設定を返します。

setStorage(storage):

void
Mastraインスタンスのストレージインスタンスを設定します。

setLogger({ logger }):

void
すべてのコンポーネント(エージェント、ワークフローなど)のロガーを設定します。

setTelemetry(telemetry):

void
すべてのコンポーネントのテレメトリ設定を設定します。

getLogger():

Logger
設定されているロガーインスタンスを取得します。

getTelemetry():

Telemetry | undefined
設定されているテレメトリインスタンスを取得します。

getLogsByRunId({ runId, transportId }):

Promise<any>
特定の実行IDとトランスポートIDのログを取得します。

getLogs(transportId):

Promise<any>
特定のトランスポートIDのすべてのログを取得します。

getMCPServers():

Record<string, MCPServerBase> | undefined
登録されているすべてのMCPサーバーインスタンスを取得します。

エラーハンドリング

Mastraクラスのメソッドは、キャッチ可能な型付きエラーをスローします:

example.ts
import { mastra } from "./mastra"; try { const agent = mastra.getAgent("weatherAgent"); const result = await agent.generate("What's the weather like in London?"); } catch (error) { if (error instanceof Error) { console.log(error.message); } }