跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Bright Data

The @mastra/brightdata package wraps the Bright Data SDK as Mastra-compatible tools. It exposes factory functions for web search and web fetch. Each function returns a tool created with createTool() that includes full Zod input/output schemas.

The search tool is backed by Bright Data's SERP API and the fetch tool by Web Unlocker. Both bypass bot detection and CAPTCHAs.

Installation
Installation的直接链接

npm install @mastra/brightdata zod

Quick start
Quick start的直接链接

Use createBrightDataTools() to get both tools with shared configuration:

src/mastra/tools/index.ts
import { createBrightDataTools } from '@mastra/brightdata'

const { webSearch, webFetch } = createBrightDataTools()
// Or pass an explicit API key:
// const { webSearch, webFetch } = createBrightDataTools({ apiKey: 'brd-...' })

Each tool can also be created individually:

src/mastra/tools/index.ts
import { createBrightDataSearchTool, createBrightDataFetchTool } from '@mastra/brightdata'

const searchTool = createBrightDataSearchTool()
const fetchTool = createBrightDataFetchTool({ apiKey: 'brd-...' })

By default, all tools read BRIGHTDATA_API_TOKEN from the environment. You can pass { apiKey } explicitly to override.

Configuration
Configuration的直接链接

All factory functions accept BrightDataClientOptions from @brightdata/sdk:

apiKey?:

string
Bright Data API token. Falls back to the BRIGHTDATA_API_TOKEN environment variable.

Additional fields supported by the bdclient constructor (such as timeout, webUnlockerZone, serpZone, and rateLimit) can be passed through the same options object.

createBrightDataTools()
createbrightdatatools的直接链接

Returns an object containing both tools with a shared configuration.

import { createBrightDataTools } from '@mastra/brightdata'

const tools = createBrightDataTools({ apiKey: 'brd-...' })
// tools.webSearch, tools.webFetch

Returns: { webSearch, webFetch }

createBrightDataSearchTool()
createbrightdatasearchtool的直接链接

Creates a tool that searches Google through Bright Data's SERP API. Returns parsed organic results.

Tool ID: brightdata-search

import { createBrightDataSearchTool } from '@mastra/brightdata'

const searchTool = createBrightDataSearchTool()

Input
Input的直接链接

query:

string
The search query.

country?:

string
Two-letter country code for geo-targeted results (for example, us or gb).

start?:

number
Result offset for pagination. For example, 10 returns the second page of 10 results.

Output
Output的直接链接

query:

string
The original search query.

results:

SearchResult[]
Organic search results. Entries missing a link or title are filtered out.
SearchResult

link:

string
Result URL.

title:

string
Result title.

description:

string
Result snippet.

currentPage:

number
Page number returned by the SERP API. Defaults to 1 when the upstream response omits or returns a non-positive value.

createBrightDataFetchTool()
createbrightdatafetchtool的直接链接

Creates a tool that fetches a webpage through Bright Data's Web Unlocker and returns the page content as Markdown.

Tool ID: brightdata-fetch

import { createBrightDataFetchTool } from '@mastra/brightdata'

const fetchTool = createBrightDataFetchTool()

Input
Input的直接链接

url:

string
The URL to fetch. Must be a valid HTTP or HTTPS URL.

Output
Output的直接链接

url:

string
The input URL.

content:

string
Page content as Markdown.

Agent example
Agent example的直接链接

The following example demonstrates a research agent that combines search and fetch:

src/mastra/agents/index.ts
import { Agent } from '@mastra/core/agent'
import { createBrightDataTools } from '@mastra/brightdata'

const { webSearch, webFetch } = createBrightDataTools()

const agent = new Agent({
id: 'research-agent',
name: 'Research Agent',
model: 'anthropic/claude-sonnet-4-6',
instructions:
'You are a research assistant. Use the search tool to find relevant pages, then use the fetch tool to read full Markdown content from the best results.',
tools: {
webSearch,
webFetch,
},
})

Environment variables
Environment variables的直接链接

VariableDescription
BRIGHTDATA_API_TOKENYour Bright Data API token. Used as the default when apiKey isn't passed to a factory function.