メインコンテンツへ移動

AI 株価 Agent の構築

このガイドでは、指定したシンボルについて前日の株価の終値を取得する Agent を作成します。Tool を作成して Agent に追加し、その Agent を使って株価を取得します。

前提条件
前提条件への直接リンク

  • Node.js v22.13.0 以降がインストールされていること
  • サポートされている Model Provider の API キー
  • 既存の Mastra プロジェクト(新しいプロジェクトをセットアップするには、インストールガイドに従ってください)

Agent の作成
Agent の作成への直接リンク

Mastra で Agent を作成するには、Agent クラスで定義し、Mastra に登録します。

  1. 新しいファイル src/mastra/agents/stockAgent.ts を作成し、Agent を定義します。

    src/mastra/agents/stockAgent.ts
    import { 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',
    })
  2. src/mastra/index.ts ファイルで Agent を登録します。

    src/mastra/index.ts
    import { Mastra } from '@mastra/core'
    import { stockAgent } from './agents/stockAgent'

    export const mastra = new Mastra({
    agents: { stockAgent },
    })

株価 Tool の作成
株価 Tool の作成への直接リンク

Stock Agent はまだ現在の株価を把握できません。これを可能にするため、Tool を作成して Agent に追加します。

  1. 新しいファイル src/mastra/tools/stockPrices.ts を作成します。その中に、指定したシンボルについて前日の株価の終値を取得する stockPrices Tool を追加します。

    src/mastra/tools/stockPrices.ts
    import { 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),
    }
    },
    })
  2. src/mastra/agents/stockAgent.ts で、作成した stockPrices Tool を import して Agent に追加します。

    src/mastra/agents/stockAgent.ts
    import { 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 を操作する方法を説明します。

  1. mastra dev コマンドを使って、Agent をサービスとして実行できます。

    mastra dev

    登録済みの Agent とやり取りするためのエンドポイントを公開するサーバーが起動します。Studio では、UI を介して stockAgentstockPrices Tool をテストできます。

  2. デフォルトでは、mastra devhttp://localhost:4111 で実行されます。Stock Agent は次の URL で利用できます。

    POST http://localhost:4111/api/agents/stockAgent/generate
  3. コマンドラインから 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 がリクエストを正常に処理し、stockPrices Tool で株価を取得して結果を返したことを示します。