A starter template for building AI-powered chat agents using Cloudflare's Agent platform, powered by agents. This project provides a foundation for creating interactive chat experiences with AI, complete with a modern UI and tool integration capabilities.
- π¬ Interactive chat interface with AI
- π οΈ Built-in tool system with human-in-the-loop confirmation
- οΏ½ Model Context Protocol (MCP) integration for external tool servers
- π€ Dynamic AI agent system with thread-specific specialization
- οΏ½π Advanced task scheduling (one-time, delayed, and recurring via cron)
- π Dark/Light theme support
- β‘οΈ Real-time streaming responses
- π State management and chat history
- π¨ Modern, responsive UI
- Cloudflare account
- OpenAI API key
- Create a new project:
npx create-cloudflare@latest --template cloudflare/agents-starter- Install dependencies:
npm install- Set up your environment:
Create a .dev.vars file:
OPENAI_API_KEY=your_openai_api_key- Run locally:
npm start- Deploy:
npm run deployβββ src/
β βββ app.tsx # Chat UI implementation
β βββ server.ts # Chat agent logic with MCP integration
β βββ tools.ts # Tool definitions (built-in + MCP)
β βββ lib/
β β βββ mcp-connection.ts # MCP server connection manager
β βββ api/
β β βββ agents.ts # Agent and MCP server management API
β βββ types/
β β βββ mcp.ts # MCP and agent type definitions
β βββ components/ # UI components for agent management
β βββ utils.ts # Helper functions
β βββ styles.css # UI styling
This starter includes a full Model Context Protocol (MCP) integration system that allows you to connect external tool servers:
// MCP servers are configured via database and connect automatically
// The system supports:
// - WebSocket and SSE transports
// - Authentication (API key, Basic Auth, OAuth2)
// - Automatic tool discovery
// - Reliability with retry logic
// - Thread-specific tool loading based on active agents
// Tools from MCP servers are automatically available alongside built-in tools
// No additional configuration needed - just configure servers in the UIAdd new tools in tools.ts using the tool builder:
// Example of a tool that requires confirmation
const searchDatabase = tool({
description: "Search the database for user records",
parameters: z.object({
query: z.string(),
limit: z.number().optional(),
}),
// No execute function = requires confirmation
});
// Example of an auto-executing tool
const getCurrentTime = tool({
description: "Get current server time",
parameters: z.object({}),
execute: async () => new Date().toISOString(),
});
// Scheduling tool implementation
const scheduleTask = tool({
description:
"schedule a task to be executed at a later time. 'when' can be a date, a delay in seconds, or a cron pattern.",
parameters: z.object({
type: z.enum(["scheduled", "delayed", "cron"]),
when: z.union([z.number(), z.string()]),
payload: z.string(),
}),
execute: async ({ type, when, payload }) => {
// ... see the implementation in tools.ts
},
});To handle tool confirmations, add execution functions to the executions object:
export const executions = {
searchDatabase: async ({
query,
limit,
}: {
query: string;
limit?: number;
}) => {
// Implementation for when the tool is confirmed
const results = await db.search(query, limit);
return results;
},
// Add more execution handlers for other tools that require confirmation
};Tools can be configured in two ways:
- With an
executefunction for automatic execution - Without an
executefunction, requiring confirmation and using theexecutionsobject to handle the confirmed action
The starting server.ts implementation uses the ai-sdk and the OpenAI provider, but you can use any AI model provider by:
- Installing an alternative AI provider for the
ai-sdk, such as theworkers-ai-provideroranthropicprovider: - Replacing the AI SDK with the OpenAI SDK
- Using the Cloudflare Workers AI + AI Gateway binding API directly
For example, to use the workers-ai-provider, install the package:
npm install workers-ai-providerAdd an ai binding to wrangler.jsonc:
Replace the @ai-sdk/openai import and usage with the workers-ai-provider:
// server.ts
// Change the imports
- import { openai } from "@ai-sdk/openai";
+ import { createWorkersAI } from 'workers-ai-provider';
// Create a Workers AI instance
+ const workersai = createWorkersAI({ binding: env.AI });
// Use it when calling the streamText method (or other methods)
// from the ai-sdk
- const model = openai("gpt-4o-2024-11-20");
+ const model = workersai("@cf/deepseek-ai/deepseek-r1-distill-qwen-32b")Commit your changes and then run the agents-starter as per the rest of this README.
The chat interface is built with React and can be customized in app.tsx:
- Modify the theme colors in
styles.css - Add new UI components in the chat container
- Customize message rendering and tool confirmation dialogs
- Add new controls to the header
-
Customer Support Agent with MCP Integration
- Add built-in tools for:
- Ticket creation/lookup
- Order status checking
- Product recommendations
- FAQ database search
- Connect MCP servers for:
- CRM system integration
- Live chat transcription
- Knowledge base search
- Add built-in tools for:
-
Development Assistant with GitHub MCP
- Integrate tools for:
- Code linting
- Git operations
- Documentation search
- Dependency checking
- Connect GitHub MCP server for:
- Repository management
- Pull request automation
- Issue tracking
- GitHub Actions workflow management
- Integrate tools for:
-
Data Analysis Assistant
- Build tools for:
- Database querying
- Data visualization
- Statistical analysis
- Report generation
- Connect database MCP servers for:
- Live data access
- Query optimization
- Real-time analytics
- Build tools for:
-
Personal Productivity Assistant
- Implement tools for:
- Task scheduling with flexible timing options
- One-time, delayed, and recurring task management
- Task tracking with reminders
- Email drafting
- Note taking
- Implement tools for:
-
Scheduling Assistant with MCP Math Server
- Build tools for:
- One-time event scheduling using specific dates
- Delayed task execution (e.g., "remind me in 30 minutes")
- Recurring tasks using cron patterns
- Task payload management
- Connect MCP servers for:
- Advanced mathematical calculations
- Calendar system integration
- Time zone conversions
- Complex scheduling algorithms
- Build tools for:
Each use case can be implemented by:
- Adding relevant built-in tools in
tools.ts - Configuring MCP servers via the agent management UI
- Creating specialized agents with specific tool combinations
- Customizing the UI for specific interactions
- Extending the agent's capabilities in
server.ts - Adding any necessary external API integrations
The system is designed to work with any MCP-compliant server:
- Math Server: Advanced calculations and mathematical operations
- GitHub Server: Repository management, Actions, and code analysis
- Database Server: SQL queries and data analysis
- Calendar Server: Scheduling and time management
- Custom Servers: Build your own MCP servers for specific needs
MIT
