- Create a new project:
npm create cloudflare@latest -- --template cloudflare/agents-starter- Install dependencies:
npm install- Set up your environment:
Create a .dev.vars file:
# Cloudflare API Configuration
CLOUDFLARE_API_TOKEN="your_cloudflare_api_token_here"
CLOUDFLARE_ZONE_ID="your_cloudflare_zone_id_here"
CLOUDFLARE_RULESET_ID="your_cloudflare_ruleset_id_here"
# Google Chat Webhook Configuration
GOOGLE_CHAT_WEBHOOK_URL="your_google_chat_webhook_url_here"- Deploy secrets to Cloudflare:
wrangler secret bulk .dev.vars- Run locally:
npm start- Deploy:
npm run deployThe following tools are available in this chat agent:
- getWeatherInformation - Get current weather information for a specified city (requires confirmation)
- getLocalTime - Get the local time for a specified location
- generateImage - Generate an image from a text description using Cloudflare Workers AI
- searchPokemon - Search for Pokémon details by name or ID using the PokeAPI
- sendWebhook - Send a message to a configured webhook URL
- callDoWorker - Call the do-worker Cloudflare Worker and return its response
- callgraphqlWorker - Call the graphql worker to get total user agent information
- addCloudflareCustomRule - Create and add custom rules to Cloudflare using the API
Add 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
