> 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/scraping/serp-api/integration/mcp-integration/cloudflare-agents-sdk-integration.md).

# Cloudflare Agents SDK Integration

Connect Thordata MCP Server to Cloudflare Agents SDK to build Cloudflare Workers agents with real-time search, scraping, and structured data tools.

Integrate Thordata MCP Server with Cloudflare Agents SDK to give AI agents access to real-time SERP data and structured search results.

How to integrate Cloudflare's Agents SDK with ThorData's Web MCP server to build AI agents on Cloudflare Workers, featuring over 60 MCP tools.

#### Hosted MCP

{% stepper %}
{% step %}
**Get your API token**

* Go to [SERP API-API Token](https://dashboard.thordata.com/) to create an API token.
* Copy your API Token (format as: `sk_7qBRe***************************`）
  {% endstep %}

{% step %}
**Set up your Cloudflare environment**

Please ensure you have Node.js 20 or later installed, then log in to Cloudflare:

```
npx wrangler login
```

{% endstep %}

{% step %}
**Install the Agents Starter template.**

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

{% endstep %}

{% step %}
**Configure environment variables**

Copy the example <kbd>.env</kbd> file and add your OpenAI API key:

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

edit `.dev.vars`：

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

{% endstep %}

{% step %}
**Connect ThorData MCP in your agent.**

In `src/server.ts`，find the default, commented-out MCP connection `onChatMessage`:

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

Replace it with the ThorData MCP connection:

```
// 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" %}
Please ensure that `mcp.connect()` is called before `this.mcp.getAITools()` to avoid an error indicating that `jsonSchema` is uninitialized.
{% endhint %}
{% endstep %}

{% step %}
**Testing to see if it works.**

* Please replace <kbd>\<API\_TOKEN></kbd> with your actual **ThorData API token**.
* Start the development server:

  ```
  npm start
  ```
* Open the application in your browser and ask customer service a question, for example:

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

You should be able to see agents using ThorData's web scraping tools to retrieve and aggregate real-time results:

<div align="left"><figure><img src="https://1563576391-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8nDscYLNSmSnujUsW4Dv%2Fuploads%2FL2IHGi7phsRyQ5FmRftA%2Fclou.png?alt=media&#x26;token=9915fcd4-d2d0-43cf-8ecf-2e320aa60ac7" alt=""><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}
**Monitor usage**

View your API usage in the [ThorData SERP API usage logs](https://dashboard.thordata.com/).
{% endstep %}
{% endstepper %}
