Cloudflare Agents
Give an agent built with the Cloudflare Agents SDK live web search. It calls SpecStream's search_web and read_pages tools over MCP, or the Search API from an AI SDK tool, and gets an answer with the page each value came from.
SpecStream for Cloudflare AgentsAgent frameworks & buildersWhat you need
- A Worker project with the Agents SDK, the AI SDK and Wrangler (
npm i agents ai zod), and a model that supports tool calls. The example uses a Workers AI model throughcreateAI. - A SpecStream account. New accounts start with $10 of credit, no card. Create one.
- A SpecStream API key with a spending limit.
- 1
1. Create an API key
In your SpecStream workspace, open Agents & API, choose API keys, then Create API key, and set a spending limit. Store the key as a Worker secret, and in
.dev.varsforwrangler dev:Terminal - 2
2. Add the agent to wrangler.jsonc
An agent is a Durable Object, so it needs a binding and a SQLite migration. The
aibinding gives it a Workers AI model. Then runnpx wrangler typessoEnvknowsAI,SearchAgentandSPECSTREAM_API_KEY:wrangler.jsonc - 3
3. Connect SpecStream as an MCP server
addMcpServer()connects the agent tohttps://api.specstream.ai/mcpand keeps the server in the agent's SQL storage, so it reconnects after the agent wakes. The key goes in the transport headers, so there is no OAuth step. Theidoption names the serverspecstream, and the Agents SDK keys each tooltool_<serverId>_<toolName>, so the agent can pass the model onlytool_specstream_search_webandtool_specstream_read_pages:src/server.ts - 4
4. Run it and ask
Start the Worker with
npx wrangler dev.routeAgentRequestservesSearchAgentat/agents/search-agent/<name>, so post a question to it:Terminal
What comes back
search_web takes {"q": "<the question>"}. The reply holds answer, the value as the pages print it, with the page it was read from, and sources, the results read in rank order with their url, title, site and snippet. When no page prints a value that answers the question, answer is null and the sources are still there, so the model can say what it found instead of guessing.
read_pages takes {"urls": ["<url>", ...]}, up to 20 at once, and returns each page's text as Markdown.
Or call the Search API from an AI SDK tool
To skip the MCP connection, wrap the Search API in an AI SDK tool() and pass it in tools. A POST keeps the question out of URL logs, and a failed search returns a sentence the model can read instead of throwing:
Or keep the Agents SDK web_search tool
The SDK's own webSearchTool takes a source in place of the Workers AI binding. A source backed by SpecStream keeps the tool's name, description and trimmed results for the model, and the search runs on your SpecStream key. speed: "fast" asks for the fast tier, which costs $1 per 1,000 searches of up to 10 results:
Cost
Each search_web call is one Search API call at $5 per 1,000 searches of up to 10 results, from your balance, with no quote and no approval. A fast search is $1 per 1,000. A search that returns no results costs nothing. Each page read_pages reads is $0.80 per 1,000 pages, and a page with no text is not charged. The reply's price_usd says what each call cost. Search API
Limits on the key
The key's spending limit holds on every call the agent makes, so a loop cannot spend past it. Give each Worker its own key, and revoke it in Agents & API to stop it at once. Every search is kept in your account's history with its answer and sources. The other SpecStream tools, which build and price larger lists, stay out of reach until you add them to tools. The agent guide describes each one.
Remove
Call this.removeMcpServer("specstream") to close the connection and delete the stored server, take the tool out of tools, and revoke the key in Agents & API. To put SpecStream on a Cloudflare MCP server portal for your team instead, see Cloudflare. Questions go to [email protected].
More
SpecStream for Cloudflare Agents covers what it does in Cloudflare Agents on one page. Every other tool has its guide in Connect your tools. You see the most a request can cost before it starts, and the matches are paid from your balance. Billing has the details. For help, email [email protected].