> For the complete documentation index, see [llms.txt](https://docs.talordata.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.talordata.com/cn-tw/serp-api/integration/mcp-integration/cloudflare-agents-sdk-integration.md).

# Cloudflare Agents SDK 集成

如何將 Cloudflare 的 Agents SDK 與 TalorData 的 Web MCP 伺服器集成，以便在 Cloudflare Workers 上構建 AI 代理。提供 60 多種 MCP 工具。

### 託管 MCP

{% stepper %}
{% step %}

#### 獲取**您的 API 令牌**

* 前往 [SERP API-API Token](https://dashboard.talordata.com/scraping/serp-api/api-token/) 創建API Token
* 複製您的API token（格式如下：`sk_7qBRe***************************`）

{% endstep %}

{% step %}

#### 設定您的 Cloudflare 環境

請確保您已安裝 Node.js 20 或更高版本，然後登入Cloudflare：

```
npx wrangler login
```

{% endstep %}

{% step %}

#### 安裝 Agents Starter 模版

```
npx create-cloudflare@latest mcp-demo --template cloudflare/agents-starter
cd mcp-example
npm install
```

{% endstep %}

{% step %}

#### 配寘環境**变量**

複製示例 <kbd>env</kbd> 文件並添加您的 OpenAI API 密鑰：

```
cp .dev.vars.example .dev.vars
```

編輯  `.dev.vars`：

```
OPENAI_API_KEY=sk-...
```

{% endstep %}

{% step %}

#### 在您的代理中連接 TalorData MCP

在`src/server.ts`中，找到默認的、被注釋掉的MCP連接 `onChatMessage`：

```
// const mcpConnection = await this.mcp.connect(
//   "https://path-to-mcp-server/mcp"
// );
```

將其替換為 TalorData MCP 連接：

```
// Connect to TalorData MCP server
const mcpConnection = await this.mcp.connect(
  "https://mcp.talordata.net/API_TOKEN/mcp"
);

// Merge MCP tools with local tools
const allTools = {
  ...tools,
  ...this.mcp.getAITools()
};
```

{% hint style="info" %}
請確保在調用 `this.mcp.getAITools()` 之前先調用 `mcp.connect()`，以避免出現 `jsonSchema` 未初始化的錯誤。
{% endhint %}

{% endstep %}

{% step %}

#### 測試一下是否有效

* 請替換 `<API_TOKEN>`為您的實際 **TalorData API 令牌**
* 啟動開發服務器：

  ```
  npm start
  ```
* 在瀏覽器中打開應用程序，然後向客服人員提問，例如：

```
Find the latest 3 news items about Cloudflare Agents and summarize them in bullets with links.
```

你應該能看到代理使用 TalorData 的網路爬蟲工具來獲取和匯總實时結果：

<div align="left"><figure><img src="/files/tFNji4AK6SvKsup0Arku" alt=""><figcaption></figcaption></figure></div>

{% endstep %}

{% step %}

#### 監控使用情况

在 TalorData [**SERP API-使用記錄**](https://dashboard.talordata.com/scraping/serp-api/history) 中查看您的 API 使用情况。
{% endstep %}
{% endstepper %}
