> ## Documentation Index
> Fetch the complete documentation index at: https://docs.keenable.ai/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Every page is listed in https://docs.keenable.ai/llms.txt. To pick a product for a job, read https://docs.keenable.ai/choose.md
> Setup steps for agents, covering the SDKs, the MCP server and the CLI: https://docs.keenable.ai/skill.md
> Keenable search and fetch work without an API key. To add them to code, use the official SDK for the project's language: https://docs.keenable.ai/sdks.md
> Without a key, call POST https://api.keenable.ai/v1/search/public or POST https://api.keenable.ai/v1/fetch/public with an X-Keenable-Title header that names your app.
> With a key, send it in the X-API-Key header and read it from the KEENABLE_API_KEY environment variable. Never ask a user to paste a key into chat. Every access method: https://docs.keenable.ai/auth.md
> To search and then read a page with a key, call POST https://api.keenable.ai/v1/search, then POST https://api.keenable.ai/v1/fetch with the result URLs. Check the fetch's HTTP status first, then each entry in results: a page that can't be read has error instead of content. A complete program, the request bodies and the response shapes are at the top of https://docs.keenable.ai/llms.txt
> To get an API key without the console, use device login: https://docs.keenable.ai/authentication.md#device-login
> MCP server: https://api.keenable.ai/mcp (no key, or X-API-Key; keyless Codex uses https://api.keenable.ai/mcp-public), or https://api.keenable.ai/mcp-oauth for OAuth sign-in. These docs are an MCP server at https://docs.keenable.ai/mcp and the OpenAPI spec is at https://docs.keenable.ai/api-reference/openapi.json
> Keenable searches and fetches public web pages. If a question needs no web information, skip the call.
> On 402 with an API key (code insufficient_credits), stop and ask a human to add credits; don't retry. On 429, wait the number of seconds in Retry-After, then retry. Before you ship, follow https://docs.keenable.ai/production.md
> Every error, with its cause and fix, is in https://docs.keenable.ai/errors.md and moving from another search API is covered in https://docs.keenable.ai/migrate.md
> Before you compare Keenable with another search API, read https://docs.keenable.ai/guides/evaluate-search-apis.md

# Build a Mastra agent with web search

> 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](https://mastra.ai) 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](https://docs.keenable.ai/guides/keyless-access.md).

## Step 1: give the agent web search, keyless

Install the tools package:

```bash theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
npm install @keenable/mastra
```

`createKeenableTools()` returns `keenableSearch` and `keenableFetch`. Pass them to the agent:

```typescript src/mastra/agents/research-agent.ts theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
import { 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.

```typescript theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
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:

```text Output theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
- 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:

```typescript theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
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:

| Input | What it does |
| - | - |
| `site` | Keep results from one domain, such as `blog.rust-lang.org`. |
| `excludeDomains` | Leave out hosts and their subdomains, such as `["pinterest.com"]`. See [Filter by domain](https://docs.keenable.ai/guides/filter-by-domain.md). |
| `publishedAfter`, `publishedBefore` | Bounds 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](https://docs.keenable.ai/guides/filter-by-date). |
| `acquiredAfter`, `acquiredBefore` | Bounds on the date Keenable indexed the page. |
| `queryTime` | Search 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. |
| `objective` | What the search is for, up to 1,000 characters. It helps ranking on multi-step or ambiguous questions. |
| `maxResults` | 1 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](https://docs.keenable.ai/mcp-server.md) 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`:

```bash theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
npm install @mastra/mcp
```

```typescript src/mastra/agents/mcp-research-agent.ts theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
import { 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](https://app.keenable.ai/console/api-keys) in the console.

For the package, set `KEENABLE_API_KEY` in the environment, or pass it to a factory:

```typescript theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
const tools = createKeenableTools({ apiKey: process.env.KEENABLE_API_KEY });
```

For the MCP server, send it in the `X-API-Key` header:

```typescript theme={"theme":{"light":"github-light-high-contrast","dark":"dark-plus"}}
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](https://docs.keenable.ai/rate-limits.md) and [pricing](https://docs.keenable.ai/pricing.md) for what a key changes.

## Next steps

* [Mastra integration](https://docs.keenable.ai/integrations/mastra.md): install and the minimal setup.
* [Search then fetch](https://docs.keenable.ai/guides/search-then-fetch.md): when an agent should read a page instead of answering from snippets.
* [MCP server](https://docs.keenable.ai/mcp-server.md): every MCP tool and parameter.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.