Cloudflare Agents SDK 集成
如何将 Cloudflare 的 Agents SDK 与 ThorData 的 Web MCP 伺服器集成,以便在 Cloudflare Workers 上构建 AI 代理。提供 60 多种 MCP 工具。
托管 MCP
1
获取您的 API 令牌
前往 SERP API-API Token 创建API Token
复制您的API token(格式如下:
sk_7qBRe***************************)
2
设定您的 Cloudflare 环境
请确保您已安装 Node.js 20 或更高版本,然后登入Cloudflare:
npx wrangler login3
安装 Agents Starter 模版
npx create-cloudflare@latest mcp-demo --template cloudflare/agents-starter
cd mcp-example
npm install4
配置环境变量
复制示例 env 文件并添加您的 OpenAI API 密钥:
cp .dev.vars.example .dev.vars编辑 .dev.vars:
OPENAI_API_KEY=sk-...5
在您的代理中连接 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()
};6
测试一下是否有效
请替换
<API_TOKEN>为您的实际 ThorData API 令牌启动开发服务器:
npm start在浏览器中打开应用程序,然后向客服人员提问,例如:
Find the latest 3 news items about Cloudflare Agents and summarize them in bullets with links.你应该能看到代理使用 ThorData 的网路爬虫工具来获取和汇总实时结果:

7
监控使用情况
在 ThorData SERP API-使用记录 中查看您的 API 使用情况。
Last updated
Was this helpful?