Add live web search to a Vercel AI SDK agent
Vercel AI SDK's tool() helper and MCP support make it straightforward to give any TypeScript agent live web search — either via an autonomous x402 wallet or a free API key.
Two paths
| Path | Best for | What you need |
|---|---|---|
| MCP client | All five tools, fully autonomous — agent pays per call | A funded Base wallet + npx |
Typed tool() helpers | Explicit tool definitions, controlled spend | A free API key from /pricing |
Path 1: MCP client (recommended for autonomous agents)
npm install ai @ai-sdk/openai zod
import { experimental_createMCPClient, generateText } from 'ai'
import { Experimental_StdioMCPTransport } from 'ai/mcp-stdio'
import { openai } from '@ai-sdk/openai'
async function main() {
const mcpClient = await experimental_createMCPClient({
transport: new Experimental_StdioMCPTransport({
command: 'npx',
args: ['-y', 'superhighway-mcp'],
env: {
AGENT_PRIVATE_KEY: process.env.AGENT_PRIVATE_KEY!,
X402_NETWORK: 'base',
},
}),
})
const tools = await mcpClient.tools()
const { text } = await generateText({
model: openai('gpt-4o'),
tools,
prompt: 'What are the latest developments in AI agent frameworks?',
maxSteps: 5,
})
console.log(text)
await mcpClient.close()
}
main()
The MCP server exposes all five tools — web_search, news_search, image_search, scrape, research — and each call settles a $0.001–$0.005 USDC payment automatically via x402. See wallet setup or the x402 flow.
Path 2: Typed tool() helpers with an API key
Get a free API key (1,000 calls/month) and wire up typed tools with Zod schemas:
import { generateText, tool } from 'ai'
import { openai } from '@ai-sdk/openai'
import { z } from 'zod'
const BASE = 'https://superhighway.walls.sh'
const KEY = process.env.SUPERHIGHWAY_API_KEY!
const { text } = await generateText({
model: openai('gpt-4o'),
tools: {
webSearch: tool({
description: 'Search the live web for current information, facts, and events.',
parameters: z.object({
query: z.string(),
limit: z.number().optional(),
}),
execute: async ({ query, limit = 5 }) => {
const res = await fetch(
`${BASE}/search?q=${encodeURIComponent(query)}&limit=${limit}`,
{ headers: { Authorization: `Bearer ${KEY}` } },
)
const data = await res.json()
return data.results
.map((r: any) => `${r.title}\n${r.url}\n${r.description}`)
.join('\n\n')
},
}),
newsSearch: tool({
description: 'Search recent news articles with published dates.',
parameters: z.object({ query: z.string() }),
execute: async ({ query }) => {
const res = await fetch(
`${BASE}/news?q=${encodeURIComponent(query)}`,
{ headers: { Authorization: `Bearer ${KEY}` } },
)
const data = await res.json()
return data.results
.map((r: any) => `${r.title} (${r.publishedDate ?? ''})\n${r.url}\n${r.description}`)
.join('\n\n')
},
}),
research: tool({
description: 'Search + read the top matching pages in one call. Use for deep synthesis.',
parameters: z.object({
query: z.string(),
pages: z.number().optional(),
}),
execute: async ({ query, pages = 2 }) => {
const res = await fetch(
`${BASE}/research?q=${encodeURIComponent(query)}&pages=${pages}`,
{ headers: { Authorization: `Bearer ${KEY}` } },
)
const data = await res.json()
return data.pages
.filter((p: any) => !p.error)
.map((p: any) => `## ${p.title}\n${p.url}\n\n${p.markdown.slice(0, 3000)}`)
.join('\n\n---\n\n')
},
}),
},
prompt: 'Summarize the top AI news from the last 48 hours.',
maxSteps: 5,
})
console.log(text)
Each tool() is typed end-to-end with Zod: the SDK validates inputs before calling execute and passes the return value back to the model as a tool result. Add only what the agent needs — webSearch + newsSearch for real-time Q&A, research for multi-page synthesis.
Which tools are available
| Tool / endpoint | What it returns | Price |
|---|---|---|
web_search / /search | Ranked web results — title, URL, description | $0.001 |
news_search / /news | Recent news with published dates | $0.001 |
image_search / /images | Image URLs, thumbnails, source pages | $0.001 |
scrape / /scrape | Any URL → clean markdown text | $0.002 |
research / /research | Search + read top pages in one call | $0.005 |
Full API reference: /openapi.json. Get a free API key or learn the x402 wallet flow.