Skip to Content
エージェントエージェントの呼び出し

エージェントの呼び出し

Mastra で作成したエージェントとやり取りする方法はいくつかあります。以下では、ワークフローのステップ、ツール、Mastra Client SDK、そしてローカルで手早くテストするためのコマンドラインを使ってエージェントを呼び出す例を紹介します。

このページでは、システムプロンプトの変更の例で説明している harryPotterAgent の呼び出し方を示します。

ワークフローのステップから

mastra インスタンスは、ワークフローのステップにある execute 関数へ引数として渡されます。getAgent() を使って登録済みのエージェントにアクセスできます。このメソッドでエージェントを取得し、続けてプロンプトを渡して generate() を呼び出してください。

src/mastra/workflows/test-workflow.ts
import { createWorkflow, createStep } from "@mastra/core/workflows"; import { z } from "zod"; const step1 = createStep({ // ... execute: async ({ mastra }) => { const agent = mastra.getAgent("harryPotterAgent"); const response = await agent.generate("What is your favorite room in Hogwarts?"); console.log(response.text); } }); export const testWorkflow = createWorkflow({ // ... }) .then(step1) .commit();

ツールから

mastra インスタンスはツールの execute 関数内で利用できます。getAgent() で登録済みのエージェントを取得し、プロンプトを渡して generate() を呼び出します。

src/mastra/tools/test-tool.ts
import { createTool } from "@mastra/core/tools"; import { z } from "zod"; export const testTool = createTool({ // ... execute: async ({ mastra }) => { const agent = mastra.getAgent("harryPotterAgent"); const response = await agent.generate("What is your favorite room in Hogwarts?"); console.log(response!.text); } });

Mastra Client から

mastraClient インスタンスは、登録済みのエージェントへアクセスできます。getAgent() でエージェントを取得し、role と content のペアを含む messages 配列を持つオブジェクトを渡して generate() を呼び出します。

import { mastraClient } from "../lib/mastra-client"; const agent = mastraClient.getAgent("harryPotterAgent"); const response = await agent.generate({ messages: [ { role: "user", content: "What is your favorite room in Hogwarts?" } ] }); console.log(response.text);

詳細は Mastra Client SDK をご覧ください。

コマンドラインから

ローカルでエージェントをテストするための簡単なスクリプトを作成できます。mastra インスタンスでは、getAgent() を使って登録済みのエージェントにアクセスできます。

モデルが環境変数(OPENAI_API_KEY など)にアクセスできるように、dotenv をインストールし、スクリプトの先頭でインポートしてください。

src/test-agent.ts
import "dotenv/config"; import { mastra } from "./mastra"; const agent = mastra.getAgent("harryPotterAgent"); const response = await agent.generate("What is your favorite room in Hogwarts?"); console.log(response.text);

このスクリプトは、次のコマンドでコマンドラインから実行できます:

npx tsx src/test-agent.ts

curl から

登録済みのエージェントとは、Mastra アプリケーションの /generate エンドポイントに POST リクエストを送信することで対話できます。messages 配列には role と content のペアを含めてください。

curl -X POST http://localhost:4111/api/agents/harryPotterAgent/generate \ -H "Content-Type: application/json" \ -d '{ "messages": [ { "role": "user", "content": "What is your favorite room in Hogwarts?" } ] }'| jq -r '.text'