본문으로 건너뛰기

AI 주식 Agent 구축

이 가이드에서는 기호에 대한 전날의 종가를 가져오는 Agent를 만듭니다. Tool을 만들어 Agent에 추가한 다음 Agent를 사용하여 주가를 가져옵니다.

전제조건
전제조건에 대한 직접 링크

  • Node.jsv22.13.0 or later installed
  • 지원되는 API 키Model Provider
  • 기존 Mastra 프로젝트(다음을 따르세요.installation guide to set up a new project)

Agent 만들기
Agent 만들기에 대한 직접 링크

Mastra에서 Agent를 생성하려면Agent 클래스로 정의한 다음 Mastra에 등록합니다.

  1. 새 파일 만들기src/mastra/agents/stockAgent.ts and define your 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 file, register the 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. Inside, add a stockPrices Tool을 만들어 지정된 symbol의 전일 종가를 가져옵니다:

    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 import your newly created stockPrices tool and add it to the 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. 다음을 사용하여 Agent를 서비스로 실행할 수 있습니다.mastra dev command:

    mastra dev

    등록된 Agent와 상호 작용하기 위해 엔드포인트를 노출하는 서버가 시작됩니다. 이내에Studio you can test your stockAgent and stockPrices tool through a UI.

  2. 기본적으로mastra dev runs on http://localhost:4111. Your Stock agent will be available at:

    POST http://localhost:4111/api/agents/stockAgent/generate
  3. 다음을 사용하여 Agent와 상호작용할 수 있습니다.curl from the command line:

    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을 사용해 주가를 가져오고 결과를 반환했습니다.