Use this file to discover all available pages before exploring further.
An Agent that acts as an MCP client - dynamically connects to remote MCP servers, handles OAuth authentication, and aggregates tools, prompts, and resources from all connected servers.
The Agent manages MCP server connections via the built-in mcp property. Each connection maintains state about available tools, prompts, and resources.
2
OAuth configuration
configureOAuthCallback sets up the OAuth flow. When an MCP server requires authentication, a popup window opens. After auth completes, the custom handler closes the popup automatically.
3
Real-time updates
When servers are added, removed, or change state, the Agent broadcasts updates via WebSocket. The client’s onMcpUpdate callback receives the new state.
4
Call server methods
The client uses agent.call() to invoke callable methods on the Agent, like adding or removing servers.
// Server: configure OAuth callbackthis.mcp.configureOAuthCallback({ customHandler: (result) => { if (result.authSuccess) { // Close the popup window return new Response("<script>window.close();</script>", { headers: { "content-type": "text/html" } }); } // Show error in popup return new Response(`Auth failed: ${result.authError}`, { status: 400 }); }});// Client: add server that requires authawait agent.call("addServer", [ "GitHub", "https://mcp-server.example.com/mcp"]);// If auth is required, a popup automatically opens// After user authorizes, popup closes and connection completes
import { AIChatAgent } from "@cloudflare/ai-chat";import { streamText } from "ai";export class ChatAgent extends AIChatAgent { waitForMcpConnections = true; async onChatMessage() { // Get tools from all connected MCP servers const mcpTools = this.mcp.getAITools(); const result = streamText({ model, tools: { // MCP tools are now available to the LLM ...mcpTools, // Plus any local tools myLocalTool: tool({ ... }) }, messages: this.messages }); return result.toUIMessageStreamResponse(); }}