DocsGuidesTools: Stock Agent

Stock Agent

We’re going to create a simple agent that fetches the last day’s closing stock price for a given symbol. This example will show you how to create a tool, add it to an agent, and use the agent to fetch stock prices.

Project Structure

stock-price-agent/
├── src/
│   ├── agents/
│   │   └── stockAgent.ts
│   ├── tools/
│   │   └── stockPrices.ts
│   └── index.ts
├── package.json
└── .env

Initialize the Project and Install Dependencies

First, create a new directory for your project and navigate into it:

mkdir stock-price-agent
cd stock-price-agent

Initialize a new Node.js project and install the required dependencies:

npm init -y
npm install @mastra/core zod

Set Up Environment Variables

Create a .env file at the root of your project to store your OpenAI API key.

.env
OPENAI_API_KEY=your_openai_api_key

Create the necessary directories and files:

mkdir -p src/agents src/tools
touch src/agents/stockAgent.ts src/tools/stockPrices.ts src/index.ts

Create the Stock Price Tool

Next, we’ll create a tool that fetches the last day’s closing stock price for a given symbol.

src/tools/stockPrices.ts
import { createTool } from "@mastra/core";
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 ({ context: { symbol } }) => {
    console.log("Using tool to fetch stock price for", symbol);
    return {
      symbol,
      currentPrice: await getStockPrice(symbol),
    };
  },
});

Add the Tool to an Agent

We’ll create an agent and add the stockPrices tool to it.

src/agents/stockAgent.ts
import { Agent } from "@mastra/core";
 
import * as tools from "../tools/stockPrices";
 
export const stockAgent = new Agent<typeof tools>({
  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: {
    provider: "OPEN_AI",
    name: "gpt-4o",
  },
  tools: {
    stockPrices: tools.stockPrices,
  },
});

Set Up the Mastra Instance

We need to initialize the Mastra instance with our agent and tool.

src/index.ts
import { Mastra } from "@mastra/core";
 
import { stockAgent } from "./agents/stockAgent";
 
export const mastra = new Mastra({
  agents: { stockAgent },
});

Serve the Application

Instead of running the application directly, we’ll use the mastra dev command to start the server. This will expose your agent via REST API endpoints, allowing you to interact with it over HTTP.

In your terminal, start the Mastra server by running:

mastra dev --dir src

This command will start the server and make your agent available at:

http://localhost:4111/api/agents/stockAgent/generate

Make sure that your environment variables are set, especially your OpenAI API key. If you haven’t set it yet, you can provide it inline:

OPENAI_API_KEY=your_openai_api_key mastra dev

Test the Agent with cURL

Now that your server is running, you can test your agent’s endpoint using curl:

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)?" }
    ]
  }'

Expected Response:

You should receive a JSON response similar to:

{
  "text": "The current price of Apple (AAPL) is $174.55.",
  "agent": "Stock Agent"
}

This indicates that your agent successfully processed the request, used the stockPrices tool to fetch the stock price, and returned the result.


MIT 2025 © Nextra.