AI 株価 Agent の構築
このガイドでは、指定したシンボルについて前日の株価の終値を取得する Agent を作成します。Tool を作成して Agent に追加し、その Agent を使って株価を取得します。
前提条件前提条件への直接リンク
- Node.js
v22.13.0以降がインストールされていること - サポートされている Model Provider の API キー
- 既存の Mastra プロジェクト(新しいプロジェクトをセットアップするには、インストールガイドに従ってください)
Agent の作成Agent の作成への直接リンク
Mastra で Agent を作成するには、Agent クラスで定義し、Mastra に登録します。
新しいファイル
src/mastra/agents/stockAgent.tsを作成し、Agent を定義します。src/mastra/agents/stockAgent.tsimport { Agent } from '@mastra/core/agent'export const stockAgent = new Agent({id: 'stock-agent',name: 'Stock Agent',instructions:'You are a helpful assistant that provides current stock prices. When asked about a stock, use the stock price tool to fetch the stock price.',model: 'openai/gpt-5.6-sol',})src/mastra/index.tsファイルで Agent を登録します。src/mastra/index.tsimport { Mastra } from '@mastra/core'import { stockAgent } from './agents/stockAgent'export const mastra = new Mastra({agents: { stockAgent },})
株価 Tool の作成株価 Tool の作成への直接リンク
Stock Agent はまだ現在の株価を把握できません。これを可能にするため、Tool を作成して Agent に追加します。
新しいファイル
src/mastra/tools/stockPrices.tsを作成します。その中に、指定したシンボルについて前日の株価の終値を取得するstockPricesTool を追加します。src/mastra/tools/stockPrices.tsimport { createTool } from '@mastra/core/tools'import { z } from 'zod'const getStockPrice = async (symbol: string) => {const data = await fetch(`https://mastra-stock-data.vercel.app/api/stock-data?symbol=${symbol}`,).then(r => r.json())return data.prices['4. close']}export const stockPrices = createTool({id: 'Get Stock Price',inputSchema: z.object({symbol: z.string(),}),description: `Fetches the last day's closing stock price for a given symbol`,execute: async inputData => {console.log('Using tool to fetch stock price for', inputData.symbol)return {symbol: inputData.symbol,currentPrice: await getStockPrice(inputData.symbol),}},})src/mastra/agents/stockAgent.tsで、作成したstockPricesTool を import して Agent に追加します。src/mastra/agents/stockAgent.tsimport { Agent } from '@mastra/core/agent'import { stockPrices } from '../tools/stockPrices'export const stockAgent = new Agent({id: 'stock-agent',name: 'Stock Agent',instructions:'You are a helpful assistant that provides current stock prices. When asked about a stock, use the stock price tool to fetch the stock price.',model: 'openai/gpt-5.6-sol',tools: {stockPrices,},})
Agent サーバーの実行Agent サーバーの実行への直接リンク
Mastra の API を介して Agent を操作する方法を説明します。
mastra devコマンドを使って、Agent をサービスとして実行できます。mastra dev登録済みの Agent とやり取りするためのエンドポイントを公開するサーバーが起動します。Studio では、UI を介して
stockAgentとstockPricesTool をテストできます。デフォルトでは、
mastra devはhttp://localhost:4111で実行されます。Stock Agent は次の URL で利用できます。POST http://localhost:4111/api/agents/stockAgent/generateコマンドラインから
curlを使って Agent とやり取りできます。curl -X POST http://localhost:4111/api/agents/stockAgent/generate \-H "Content-Type: application/json" \-d '{"messages": [{ "role": "user", "content": "What is the current stock price of Apple (AAPL)?" }]}'次のような JSON 応答を受け取ります。
{"text": "The current price of Apple (AAPL) is $174.55.","agent": "Stock Agent"}これは、Agent がリクエストを正常に処理し、
stockPricesTool で株価を取得して結果を返したことを示します。