> For the complete documentation index, see [llms.txt](https://doc.thordata.com/doc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.thordata.com/doc/zh/pa-qu/serp-api/ji-cheng/mcp-ji-cheng/cloudflare-agents-sdk-ji-cheng.md).

# Cloudflare Agents SDK 集成

将 Thordata MCP Server 连接到 Cloudflare Agents SDK，在 Cloudflare Workers 上构建具备实时搜索、抓取和结构化数据工具的 Agent。

如何将 Cloudflare 的 Agents SDK 与 ThorData 的 Web MCP 伺服器集成，以便在 Cloudflare Workers 上构建 AI 代理。提供 60 多种 MCP 工具。

**托管 MCP**

{% stepper %}
{% step %}
**获取您的 API 令牌**

* 前往 [SERP API-API Token](https://dashboard.thordata.com/) 创建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 %}
**在您的代理中连接 ThorData MCP**

在`src/server.ts`中，找到默认的、被注释掉的MCP连接 `onChatMessage`：

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

将其替换为 ThorData MCP 连接：

```
// Connect to ThorData MCP server
const mcpConnection = await this.mcp.connect(
"https://mcp.thordata.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>`为您的实际 **ThorData API 令牌**
* 启动开发服务器：

```
npm start
```

* 在浏览器中打开应用程序，然后向客服人员提问，例如：

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

你应该能看到代理使用 ThorData 的网路爬虫工具来获取和汇总实时结果：

<div align="left"><figure><img src="https://1043327331-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FiR9tknC6bcTWE6xEmOb0%2Fuploads%2FTszoiZJjgpTNgMBDZY6C%2Fclou.png?alt=media&#x26;token=91c25718-b434-4ee9-b568-9feb1e29486c" alt=""><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}
**监控使用情况**

在 ThorData [**SERP API-使用记录**](https://dashboard.thordata.com/) 中查看您的 API 使用情况。
{% endstep %}
{% endstepper %}
