> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # 使用 Firecrawl 擷取網頁資料 Firecrawl 是一個網絡資料 API,可將網站轉換成整潔的 Markdown 或結構化 JSON。在本指南中,你會將 Firecrawl 接入 Mastra Tool,讓 Agent 及 Workflow 可以按需要搜尋及擷取即時網絡資料。 ## 先決條件 - 已安裝 Node.js `v22.13.0` 或更新版本 - Firecrawl API 金鑰(可於 取得) - 受支援的[模型 Provider](https://mastra.zisheng.pro/zh-HK/models) 所提供的 API 金鑰 - 現有的 Mastra 項目(按照[安裝指南](https://mastra.zisheng.pro/zh-HK/guides/getting-started/quickstart)設定新項目) ## 安裝 安裝 Firecrawl SDK: **npm**: ```bash npm install firecrawl ``` **pnpm**: ```bash pnpm add firecrawl ``` **Yarn**: ```bash yarn add firecrawl ``` **Bun**: ```bash bun add firecrawl ``` ## 設定環境變數 在項目根目錄建立 `.env` 檔案: ```bash FIRECRAWL_API_KEY=fc-your-api-key # Optional: FIRECRAWL_API_URL=http://localhost:3002 ``` ## 建立 Firecrawl Tool 建立一個 Tool 檔案,讓 Mastra 可以使用 Firecrawl 的搜尋及擷取功能。 1. 建立 `src/mastra/tools/firecrawl.ts` 並設定 Firecrawl: ```ts import { Firecrawl } from 'firecrawl' import { createTool } from '@mastra/core/tools' import { z } from 'zod' const firecrawl = new Firecrawl({ apiKey: process.env.FIRECRAWL_API_KEY! }) export const firecrawlSearch = createTool({ id: 'firecrawl-search', description: 'Search the web and return top results.', inputSchema: z.object({ query: z.string().min(1) }), outputSchema: z.object({ results: z.array( z.object({ title: z.string().nullable(), url: z.string(), }), ), }), execute: async ({ query }) => { const results = await firecrawl.search(query, { limit: 3 }) return { results: (results.web ?? []).map(item => ({ title: item.title ?? null, url: item.url, })), } }, }) export const firecrawlScrape = createTool({ id: 'firecrawl-scrape', description: 'Scrape a URL and return markdown content.', inputSchema: z.object({ url: z.string().url() }), outputSchema: z.object({ markdown: z.string() }), execute: async ({ url }) => { const result = await firecrawl.scrape(url, { formats: ['markdown'], onlyMainContent: true, }) return { markdown: result.markdown ?? '' } }, }) ``` 2. 在 `src/mastra/agents/web-agent.ts` 建立新 Agent: ```ts import { Agent } from '@mastra/core/agent' import { firecrawlSearch, firecrawlScrape } from '../tools/firecrawl' export const webAgent = new Agent({ id: 'web-agent', name: 'Web Agent', instructions: 'Use Firecrawl tools to search and scrape web pages, then summarize the results.', model: 'openai/gpt-5.6-sol', tools: { firecrawlSearch, firecrawlScrape }, }) ``` 3. 在 `src/mastra/index.ts` 的 Mastra 執行個體中註冊新建立的 Agent: ```ts import { Mastra } from '@mastra/core' import { webAgent } from './agents/web-agent' export const mastra = new Mastra({ agents: { webAgent }, }) ``` ## 在 Studio 中測試 執行開發伺服器並開啟 [Studio](https://mastra.zisheng.pro/zh-HK/docs/studio/overview): ```bash mastra dev ``` 在 Studio 中開啟 **Web Agent**,然後嘗試: - 「尋找最新的 Mastra 變更記錄,並總結上一個版本。」 - 「搜尋 Firecrawl 的定價,並抽取各個方案級別。」 ## 自行託管 Firecrawl 如果你在本機執行 Firecrawl,請設定 `FIRECRAWL_API_URL`,或在客戶端傳入 `apiUrl`: ```ts const firecrawl = new Firecrawl({ apiKey: process.env.FIRECRAWL_API_KEY!, apiUrl: process.env.FIRECRAWL_API_URL, }) ``` ## 相關內容 - [Firecrawl 文件](https://docs.firecrawl.dev)