Add Public Tools to Your Agent
AI Agents can't just interact with the outside world on their own, instead we need to provide them integrations. Our agent today uses Vercel AI SDK
in order to access stock api, we'll provide it basic tools to call.
What we're building in this step
Add tools
Your agent already has an empty tools configuration at app/api/chat/tools.ts. These are then imported into app/api/chat/route.ts. Lets start by building a very basic tool
In app/api/chat/ create a new file fetch-stock-price.ts.
import z from "zod";
import { tool } from "ai";
export const getStockPrice = tool({
description: 'Get current stock price by symbol for Demo Trade Pro Platform',
inputSchema: z.object({
symbol: z.string()
.min(1, 'Stock symbol is required')
.max(10, 'Stock symbol must be 10 characters or less')
.regex(/^[A-Z]+$/, 'Stock symbol must be uppercase letters only')
.describe('Stock symbol (e.g., WAYNE, STARK)')
}),
async execute({ symbol }) {
const response = await fetch("https://workshop-stock-api.auth101.dev/api/stocks/" + symbol);
if (response.ok) {
return await response.json();
}
}
})Then you can import this tool in the tools, allowing the ai-agent to use it, update the app/api/chat/tools.ts to now expose the getStockPrice tool.
import { getStockPrice } from "./fetch-stock-price"
export const agentTools = {
getStockPrice
}Test your agent: Now you should be able to fetch stock prices for WAYNE.
Leverage shared tools
Ideally the tool code should be packaged nicely and tested, since we are already using a monorepo we decided to make that easy for you! All of our tools are provided as @workspace/agent-utils. To start using @workspace/agent-utils you can import the tools by name from /tools directory.
We also provided a api-client this handles the nitty-gritty details of calling the DemoTradePro API Server, including retries, and error handling. Let's start by first importing the createAPIClient function to create our own client to call DemoTradePro API Server.
In your /apps/agent/app/api/chat/tools.ts import the createAPIClient function and then invoke it with process.env.API_BASE_URL this environment variable is configured to "https://workshop-stock-api.auth101.dev/api/ by the init script, in the setup.
import { createAPIClient } from '@workspace/agent-utils';
/**
* Create shared API client for public endpoints
* No token provider needed for public market data
*/
const apiClient = createAPIClient(process.env.API_BASE_URL!);Add the API_BASE_URL environment variable to your .env.local file:
API_BASE_URL=https://workshop-stock-api.auth101.dev/api/Now we can import a pre-built tool from our toolkit, such as GetStockPrice, and create an instance suitable for Vercel AI SDK.
import * as GetStockPrice from '@workspace/agent-utils/tools/get-stock-price';
const getStockPrice = GetStockPrice.createAISDKTool(apiClient);The above code replaces, the tool we wrote in the previous step, and the full file should can now be updated to
import { createAPIClient } from '@workspace/agent-utils';
/**
* Create shared API client for public endpoints
* No token provider needed for public market data
*/
const apiClient = createAPIClient(process.env.API_BASE_URL!);
const getStockPrice = GetStockPrice.createAISDKTool(apiClient);
export const agentTools = {
getStockPrice
}Test your agent: Now you you should still be able to fetch stock prices for WAYNE.
Now let's test the getStockPrice tool.
Test these prompts:
-
Stock price: "What's the current price of WAYNE stock?"
- Should call
getStockPricetool and show real price data!
- Should call
-
General trading: "What's your role?"
- Should work as before (trading advice)
Expected behavior:
- Agent will pause, call the appropriate tool
- Show real stock data from DemoTradePro API
🎉 Success! Public Data Works Great
Your agent now has public stock data access and it works perfectly!
What you've built:
- ✅ Working AI SDK v5 agent with tools
- ✅ Real-time stock price fetching
- ✅ Clean shared tools architecture
- ✅ No authentication needed - public data is safe!
Try these test prompts:
- "What's WAYNE trading at today?"
🤔 But What About Personal Data?
Now try asking: "What's in my portfolio?" or "What stocks do I own?"
Agent response: "I don't have access to your personal portfolio data..."
The problem: Your agent can fetch public data, but has no way to know who you are or access your personal trading data.
Coming up: We'll add authentication so your agent knows your identity and can access your personal portfolio! 🔐