INTEGRITY Cloudflare Docs

MCP

Agents can use Model Context Protocol (MCP) as clients. Connect an agent to external MCP servers, discover the tools those servers expose, and pass those tools into model calls.

Use MCP when you want an agent to:

To build an MCP server instead, refer to Model Context Protocol (MCP).

Basic pattern

Call addMcpServer() to connect to a remote MCP server, then pass this.mcp.getAITools() to the AI SDK.

import { Agent } from "agents";
import { generateText } from "ai";
import { createWorkersAI } from "workers-ai-provider";

export class ToolAgent extends Agent {
	async onStart() {
		await this.addMcpServer("github", "https://mcp.github.com/mcp");
	}

	async onRequest(request) {
		const workersai = createWorkersAI({ binding: this.env.AI });

		const response = await generateText({
			model: workersai("@cf/zai-org/glm-4.7-flash"),
			prompt: "Use available tools to summarize the latest issue activity.",
			tools: this.mcp.getAITools(),
		});

		return new Response(response.text);
	}
}
import { Agent } from "agents";
import { generateText } from "ai";
import { createWorkersAI } from "workers-ai-provider";

export class ToolAgent extends Agent<Env> {
	async onStart() {
		await this.addMcpServer("github", "https://mcp.github.com/mcp");
	}

	async onRequest(request: Request) {
		const workersai = createWorkersAI({ binding: this.env.AI });

		const response = await generateText({
			model: workersai("@cf/zai-org/glm-4.7-flash"),
			prompt: "Use available tools to summarize the latest issue activity.",
			tools: this.mcp.getAITools(),
		});

		return new Response(response.text);
	}
}

If the server requires OAuth, addMcpServer() returns an authentication state and authorization URL. The connection is persisted in the agent's SQL storage.

Configuration

For public MCP servers, no binding configuration is required. Store server URLs, API tokens, or OAuth settings as environment variables or secrets.

For MCP servers that require bearer tokens or Cloudflare Access headers, pass custom transport headers when connecting.

await this.addMcpServer("internal", this.env.MCP_SERVER_URL, {
	transport: {
		headers: {
			Authorization: `Bearer ${this.env.MCP_TOKEN}`,
			"CF-Access-Client-Id": this.env.CF_ACCESS_CLIENT_ID,
			"CF-Access-Client-Secret": this.env.CF_ACCESS_CLIENT_SECRET,
		},
	},
});
await this.addMcpServer("internal", this.env.MCP_SERVER_URL, {
	transport: {
		headers: {
			Authorization: `Bearer ${this.env.MCP_TOKEN}`,
			"CF-Access-Client-Id": this.env.CF_ACCESS_CLIENT_ID,
			"CF-Access-Client-Secret": this.env.CF_ACCESS_CLIENT_SECRET,
		},
	},
});

McpClient API

Connect Agents to external MCP servers and use their tools, resources, and prompts.

Connect to an MCP server

Create an Agent that connects to an external MCP server and uses its tools.

Use MCP tools with Code Mode

Use progressive discovery, code-based composition, and durable approvals with MCP tools.

Model Context Protocol specification

Learn about the open protocol for connecting AI applications to external tools and data.