# Mastra

MillionShort search for Mastra agents, through Mastra's MCP client or a tool of your own, in TypeScript.

MCP · TypeScript · Nothing of ours to install · Works with no key · Checked 2026-11-19

> **For agents.** Read the API key from the `MILLIONSHORT_API_KEY` environment variable. Never print it; where a client needs it in a settings file, use a file that is not committed.
> The MCP address is https://mcp.millionshort.com/mcp (streamable HTTP; send `Authorization: Bearer <key>`, or no header for the no-key mode).
> On `INSUFFICIENT_CREDITS` stop and ask a person; on `RATE_LIMITED` or `CAPACITY` wait the `Retry-After` seconds, then retry once.
> Set-up steps written for agents: https://millionshort.com/docs/auth.md. Every integration: https://millionshort.com/docs/integrations.md.

Mastra's `MCPClient` connects to the MillionShort MCP server over HTTP, and `listTools()` returns its tools ready for an agent: `millionshort_search`, `millionshort_get_domain_info`, `millionshort_list_sources` and the tools of the other endpoints, such as `millionshort_extract` and `millionshort_places` (Mastra puts the server's name in front of each tool's name; [the MCP page](https://millionshort.com/docs/mcp#tools) lists them all). With no MCP at all, a `createTool` tool that calls the API does the same job. Mastra is TypeScript only.

## 1. Install

```bash TypeScript
npm install @mastra/core @mastra/mcp zod
export MILLIONSHORT_API_KEY=ms-YOUR_API_KEY
```

The examples use the model id from Mastra's own examples, `openai/gpt-6.1-sol`, which reads your OpenAI key from OPENAI_API_KEY; any model id your Mastra setup supports works the same way. Get a MillionShort key in the [dashboard](https://millionshort.com/app/); an agent can ask for a [trial key](https://millionshort.com/docs/api-reference/agent-key) by itself.

## 2. Connect the server

Each server in `servers` gets a key of your choosing (here `millionshort`) and a `url`. The header goes in `requestInit`, the options Mastra passes to `fetch`.

```ts TypeScript
import { Agent } from '@mastra/core/agent';
import { MCPClient } from '@mastra/mcp';

const mcp = new MCPClient({
  id: 'millionshort-client',
  servers: {
    millionshort: {
      url: new URL('https://mcp.millionshort.com/mcp'),
      requestInit: { headers: { Authorization: `Bearer ${process.env.MILLIONSHORT_API_KEY}` } },
    },
  },
});

const agent = new Agent({
  id: 'researcher',
  name: 'Researcher',
  instructions: 'Use the millionshort search tool for anything recent or niche, and cite the URLs.',
  model: 'openai/gpt-6.1-sol',
  tools: await mcp.listTools(),
});

try {
  const result = await agent.generate('Find independent blogs about home espresso, without the 1,000 most popular sites.');
  console.log(result.text);
} finally {
  await mcp.disconnect();
}
```

Without a key, leave `requestInit` out: the agent gets `search` and `list_sources`.

## 3. Give the tools to an agent

`tools: await mcp.listTools()` fixes the tools, and the key, when the agent is created. In an app where each user brings their own key, create a client per request and pass its tools to that call only, with `listToolsets()`:

```ts TypeScript
import { Agent } from '@mastra/core/agent';
import { MCPClient } from '@mastra/mcp';

const agent = new Agent({
  id: 'researcher',
  name: 'Researcher',
  instructions: 'Use the millionshort search tool for anything recent or niche, and cite the URLs.',
  model: 'openai/gpt-6.1-sol',
});

export async function answer(question: string, userKey: string) {
  const mcp = new MCPClient({
    id: `millionshort-${crypto.randomUUID()}`,
    servers: { millionshort: { url: new URL('https://mcp.millionshort.com/mcp'), requestInit: { headers: { Authorization: `Bearer ${userKey}` } } } },
  });
  try {
    return (await agent.generate(question, { toolsets: await mcp.listToolsets() })).text;
  } finally {
    await mcp.disconnect();
  }
}
```

In a toolset, the tools keep the server's own names (`search`, not `millionshort_search`). A Mastra agent has no web search of its own: it has only the tools you pass.

## 4. Check it works

Run the example: the answer should cite pages, and `result.toolCalls` lists the call to `millionshort_search`. To see the tools without a model:

```ts TypeScript
import { MCPClient } from '@mastra/mcp';

const mcp = new MCPClient({
  servers: {
    millionshort: {
      url: new URL('https://mcp.millionshort.com/mcp'),
      requestInit: { headers: { Authorization: `Bearer ${process.env.MILLIONSHORT_API_KEY}` } },
    },
  },
});
console.log(Object.keys(await mcp.listTools())); // millionshort_search, millionshort_get_domain_info, millionshort_list_sources, ...
await mcp.disconnect();
```

If the model answers from memory, say when to search in its instructions:

> Use the MillionShort `search` tool for anything recent, niche, or that needs a source. To reach lesser-known sites, set `remove_top_sites` (for example 1000). Cite the URLs it returns.

## Without MCP: a function tool

`createTool` takes an id, a description, an input schema and `execute`, which receives the validated input and a context with an `abortSignal`:

```ts TypeScript
import { Agent } from '@mastra/core/agent';
import { createTool } from '@mastra/core/tools';
import { z } from 'zod';

const millionshortSearch = createTool({
  id: 'millionshort-search',
  description: 'Search the web with MillionShort. remove_top_sites leaves out the N most popular sites.',
  inputSchema: z.object({ query: z.string(), remove_top_sites: z.number().int().default(1000) }),
  execute: async ({ query, remove_top_sites }, { abortSignal }) => {
    const res = await fetch('https://api.millionshort.com/v1/search', {
      method: 'POST',
      headers: { Authorization: `Bearer ${process.env.MILLIONSHORT_API_KEY}`, 'Content-Type': 'application/json' },
      body: JSON.stringify({ query, remove_top_sites, num_results: 5 }),
      signal: abortSignal,
    });
    if (!res.ok) throw new Error(`search failed: ${res.status}`);
    const { data } = (await res.json()) as { data: { results: { title: string; url: string; snippet?: string }[] } };
    return data.results.map(({ title, url, snippet }) => ({ title, url, snippet }));
  },
});

const agent = new Agent({
  id: 'researcher',
  name: 'Researcher',
  instructions: 'Use millionshortSearch for anything recent or niche, and cite the URLs.',
  model: 'openai/gpt-6.1-sol',
  tools: { millionshortSearch },
});
console.log((await agent.generate('Find independent blogs about home espresso.')).text);
```

## MCP configuration

The same address for every client: `https://mcp.millionshort.com/mcp`, streamable HTTP.

| Mode | How | What the agent gets |
|---|---|---|
| API key | Send `Authorization: Bearer ms-YOUR_API_KEY` | Every tool, at your plan's limits; calls use your credits |
| No key | Send no `Authorization` header | `search`, `list_sources` and a few more; one source and up to 5 results a search, 10 free searches a day |

## Tools

| Tool | What it does | Without a key | Cost |
|---|---|---|---|
| `search` | Searches the web and the specialist sources, with the popularity, domain, page-type, topic, site and date filters | Yes: one source, up to 5 results | 1 credit per source searched, first 10 results |
| `get_domain_info` | Popularity and risk for up to 25 sites | No | 1 credit |
| `list_sources` | The sources, page types and topics `search` accepts | Yes: source names | Free |
| The tools of the other endpoints | `extract`, `map`, `crawl`, `places` and more: [the MCP page](https://millionshort.com/docs/mcp#tools) lists them | A few, such as `places` | What the API call behind each costs |

## Settings

| Variable | Meaning |
|---|---|
| `MILLIONSHORT_API_KEY` | Your key, read by the examples above. Leave `requestInit` out for the no-key mode |

## Costs and limits

A tool call costs what the API call behind it costs: 1 credit per source searched for the first 10 results, 1 credit for a domain lookup, nothing for the list of sources, the price of the endpoint for the other tools, and nothing at all in the no-key mode. An agent that loops can make many calls: give it a step limit, and ask for fewer results when a few will do. [Credits and limits](https://millionshort.com/docs/credits-and-limits) has the numbers.

## When it fails

| What you see | Why | Fix |
|---|---|---|
| The server does not connect, or no tools are listed | The address or the transport is not the one the server speaks | Use exactly `https://mcp.millionshort.com/mcp` and the streamable HTTP transport (often called `http`), not SSE |
| `401` or "unauthorized" | The key is wrong, or it was sent without `Bearer ` | Send `Authorization: Bearer ms-...`, or leave the header out to use the no-key mode |
| "10 free searches a day without a key" | The no-key mode's daily searches are used up | Add a key: it is free in the [dashboard](https://millionshort.com/app/) |
| `INSUFFICIENT_CREDITS` | The plan's credits are used up | Stop and add credits; retrying does not help |
| The agent answers from memory, or uses another search | Nothing told it when to use this one | Turn off its own search where it can be (this page says how), and add the line for its instructions |
| One user's searches use another user's credits | Tools from `listTools()` carry the key they were created with | Create a client per user and pass `listToolsets()` per call, as in step 3 |

## Related

- [Agent tool use](https://millionshort.com/docs/search/guides/agent-tool-use): when to give a model tools, and what to tell it.
- [Vercel AI SDK](https://millionshort.com/docs/integrations/vercel-ai-sdk) and [Strands Agents](https://millionshort.com/docs/integrations/strands): the same server in other TypeScript frameworks.
- [MCP server](https://millionshort.com/docs/mcp) and [Filters for agents](https://millionshort.com/docs/search/filters).
- Mastra's own documentation, read on 2026-11-19: [MCP](https://mastra.ai/docs/connections/mcp), [MCPClient reference](https://mastra.ai/reference/tools/mcp-client), [Tools](https://mastra.ai/docs/agents/tools).
