翻篇收录说明
翻篇网页快照

Build a Mastra agent with web search - Keenable

https://docs.keenable.ai/guides/build-a-mastra-agent

最近抓取:2026-12-03 06:13:01(北京时间)

最近内容变化:2026-12-03 06:13:01(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Build a Mastra agent that searches the web and reads pages: keyless first, then typed tools with filters, then Keenable's remote MCP server.

正文摘录

Build a TypeScript research agent with Mastra that searches the web, reads the pages it cites, and answers with links. You start without an API key, add typed tools and filters, and then connect the same agent to Keenable’s remote MCP server instead of a package. Before you start You need Node 20 or later, a Mastra project, and a key for the model the agent runs on. The examples use openai/gpt-5 through Mastra’s model router, which reads OPENAI_API_KEY. Any tool-calling model works. Keenable needs no key. Without one, calls share the keyless limit for your IP address. See Keyless access. Step 1: give the agent web search, keyless Install the tools package: npm install @keenable/mastra createKeenableTools() returns keenableSearch and keenableFetch. Pass them to the agent: src/mastra/agents/research-agent.tsimport { Agent } from "@mastra/core/agent"; import { createKeenableTools } from "@keenable/mastra"; export const researchAgent = new Agent({ id: "research-agent", name: "Research Agent", instructions: "Answer with sources. Search the web with keenable-search, read the pages you cite with keenable-fetch, and give the link for each claim.", model: "openai/gpt-5", tools: createKeenableTools(), }); Ask it something that needs the web. Give it room for several tool calls with maxSteps: with Mastra’s default, an agent that searches and then reads a few pages can run out of steps before it answers, and result.text comes back empty. const result = await researchAgent.generate( "Which Rust releases were announced on blog.rust-lang.org since October 1, 2026? Give each version with its date and link.", { maxSteps: 10 }, ); console.log(result.text); The agent sets the filters itself. Its first search sent site: "blog.rust-lang.org" and publishedAfter: "2026-10-01"; it then fetched each release post and answered: Output- Rust 1.99.0 — 2026-10-01 — https://blog.rust-lang.org/2026/10/01/Rust-1.99.0/ - Rust 1.99.1 — 2026-10-15 — https://blog.rust-lang.org/2026/10/15/Rust-1.99.1/ - Rust 1.100.0 — 2026-11-12 — https://blog.rust-lang.org/2026/11/12/Rust-1.100.0/ - Rust 1.100.1 — 2026-11-27 — https://blog.rust-lang.org/2026/11/27/Rust-1.100.1/ Step 2: use the typed tools and filters Each tool is a Mastra tool with Zod schemas, so you can also call it from your own code, and Mastra checks the input and the output against the schemas. Create one tool at a time when you want different settings per tool: import { createKeenableSearchTool } from "@keenable/mastra"; const search = createKeenableSearchTool({ maxResults: 5 }); const { results } = await search.execute!( { query: "mastra agent framework release", excludeDomains: ["github.com"], publishedAfter: "60d", maxResults: 3, }, {} as any, ); for (const r of results) console.log(r.publishedAt, r.title, r.url); The search tool takes these filters. The agent sees the same names and descriptions, and picks them per call: InputWhat it doessiteKeep results from one domain, such as blog.rust-lang.org.excludeDomainsLeave out hosts and their subdomains, such as ["pinterest.com"]. See Filter by domain.publishedAfter, publishedBeforeBounds on the publish date: a date, an ISO 8601 timestamp or an offset such as 7d. publishedAfter is how to get recent results. See Filter by date.acquiredAfter, acquiredBeforeBounds on the date Keenable indexed the page.queryTimeSearch the index as it stood at a moment in the past: pages indexed after it are left out. It’s an upper bound, not a recency filter.objectiveWhat the search is for, up to 1,000 characters. It helps ranking on multi-step or ambiguous questions.maxResults1 to 50 results, 10 by default. Each result has title, url, description (page text, cut to 500 characters by default), publishedAt and acquiredAt. The fetch tool takes a url and returns the page as markdown in content, with title, description, author, publishedAt, image and favicon when the page has them. queryTime suits evaluations and backtests: an agent that answers “what was known on 1 June” gets only pages Keenable had indexed by then. Step 3: connect Keenable’s remote MCP server Instead of the package, an agent can take its tools from Keenable’s hosted MCP server through @mastra/mcp. Use this when you already manage tools through MCP, or want the MCP tool set, which adds a fetch prompt and max_chars: npm install @mastra/mcp src/mastra/agents/mcp-research-agent.tsimport { Agent } from "@mastra/core/agent"; import { MCPClient } from "@mastra/mcp"; const mcp = new MCPClient({ servers: { keenable: { url: new URL("https://api.keenable.ai/mcp") }, }, }); export const mcpResearchAgent = new Agent({ id: "mcp-research-agent", name: "MCP Research Agent", instructions: "Answer with sources. Search the web, then read the pages you cite.", model: "openai/gpt-5", tools: await mcp.listTools(), }); listTools() prefixes each tool with the server name: keenable_search_web_pages, keenable_fetch_page_content and keenable_get_billing. Asked for the latest stable Rust release, this agent searched with published_after, read five pages from the Rust blog in one fetch call, and answered with Rust 1.100.1, 27 November 2026, and the link. Add an API key A key lifts the keyless hourly cap. Create one under API keys in the console. For the package, set KEENABLE_API_KEY in the environment, or pass it to a factory: const tools = createKeenableTools({ apiKey: process.env.KEENABLE_API_KEY }); For the MCP server, send it in the X-API-Key header: const mcp = new MCPClient({ servers: { keenable: { url: new URL("https://api.keenable.ai/mcp"), requestInit: { headers: { "X-API-Key": process.env.KEENABLE_API_KEY! } }, }, }, }); See rate limits and pricing for what a key changes. Next steps Mastra integration: install and the minimal setup. Search then fetch: when an agent should read a page instead of answering from snippets. MCP server: every MCP tool and parameter.