AI 주식 Agent 구축
이 가이드에서는 기호에 대한 전날의 종가를 가져오는 Agent를 만듭니다. Tool을 만들어 Agent에 추가한 다음 Agent를 사용하여 주가를 가져옵니다.
전제조건전제조건에 대한 직접 링크
- Node.js
v22.13.0or later installed - 지원되는 API 키Model Provider
- 기존 Mastra 프로젝트(다음을 따르세요.installation guide to set up a new project)
Agent 만들기Agent 만들기에 대한 직접 링크
Mastra에서 Agent를 생성하려면Agent 클래스로 정의한 다음 Mastra에 등록합니다.
새 파일 만들기
src/mastra/agents/stockAgent.tsand define your 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.tsfile, register the 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. Inside, add astockPricesTool을 만들어 지정된 symbol의 전일 종가를 가져옵니다: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.tsimport your newly createdstockPricestool and add it to the 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와 상호 작용하는 방법을 알아보세요.
다음을 사용하여 Agent를 서비스로 실행할 수 있습니다.
mastra devcommand:mastra dev등록된 Agent와 상호 작용하기 위해 엔드포인트를 노출하는 서버가 시작됩니다. 이내에Studio you can test your
stockAgentandstockPricestool through a UI.기본적으로
mastra devruns onhttp://localhost:4111. Your Stock agent will be available at:POST http://localhost:4111/api/agents/stockAgent/generate다음을 사용하여 Agent와 상호작용할 수 있습니다.
curlfrom 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가 요청을 성공적으로 처리했으며
stockPricesTool을 사용해 주가를 가져오고 결과를 반환했습니다.