Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/agents-sdk/configuration.md
1# Configuration23## Wrangler Setup45```jsonc6{7"name": "my-agents-app",8"durable_objects": {9"bindings": [10{"name": "MyAgent", "class_name": "MyAgent"}11]12},13"migrations": [14{"tag": "v1", "new_sqlite_classes": ["MyAgent"]}15],16"ai": {17"binding": "AI"18}19}20```2122## Environment Bindings2324**Type-safe pattern:**2526```typescript27interface Env {28AI?: Ai; // Workers AI29MyAgent?: DurableObjectNamespace<MyAgent>;30ChatAgent?: DurableObjectNamespace<ChatAgent>;31DB?: D1Database; // D1 database32KV?: KVNamespace; // KV storage33R2?: R2Bucket; // R2 bucket34OPENAI_API_KEY?: string; // Secrets35GITHUB_CLIENT_ID?: string; // MCP OAuth credentials36GITHUB_CLIENT_SECRET?: string;37QUEUE?: Queue; // Queues38}39```4041**Best practice:** Define all DO bindings in Env interface for type safety.4243## Deployment4445```bash46# Local dev47npx wrangler dev4849# Deploy production50npx wrangler deploy5152# Set secrets53npx wrangler secret put OPENAI_API_KEY54```5556## Agent Routing5758**Recommended: Use route helpers**5960```typescript61import { routeAgentRequest } from "agents";6263export default {64fetch(request: Request, env: Env) {65return routeAgentRequest(request, env);66}67}68```6970Helper routes requests to agents automatically based on URL patterns.7172**Manual routing (advanced):**7374```typescript75export default {76async fetch(request: Request, env: Env) {77const url = new URL(request.url);7879// Named ID (deterministic)80const id = env.MyAgent.idFromName("user-123");8182// Random ID (from URL param)83// const id = env.MyAgent.idFromString(url.searchParams.get("id"));8485const stub = env.MyAgent.get(id);86return stub.fetch(request);87}88}89```9091**Multi-agent setup:**9293```typescript94import { routeAgentRequest } from "agents";9596export default {97fetch(request: Request, env: Env) {98const url = new URL(request.url);99100// Route by path101if (url.pathname.startsWith("/chat")) {102return routeAgentRequest(request, env, "ChatAgent");103}104if (url.pathname.startsWith("/task")) {105return routeAgentRequest(request, env, "TaskAgent");106}107108return new Response("Not found", { status: 404 });109}110}111```112113## Email Routing114115**Code setup:**116117```typescript118import { routeAgentEmail } from "agents";119120export default {121fetch: (req: Request, env: Env) => routeAgentRequest(req, env),122email: (message: ForwardableEmailMessage, env: Env) => {123return routeAgentEmail(message, env);124}125}126```127128**Dashboard setup:**129130Configure email routing in Cloudflare dashboard:131132```133Destination: Workers with Durable Objects134Worker: my-agents-app135```136137Then handle in agent:138139```typescript140export class EmailAgent extends Agent<Env> {141async onEmail(email: AgentEmail) {142const text = await email.text();143// Process email144}145}146```147148## AI Gateway (Optional)149150```typescript151// Enable caching/routing through AI Gateway152const response = await this.env.AI.run(153"@cf/meta/llama-3.1-8b-instruct",154{ prompt },155{156gateway: {157id: "my-gateway-id",158skipCache: false,159cacheTtl: 3600160}161}162);163```164165## MCP Configuration (Optional)166167For exposing tools via Model Context Protocol:168169```typescript170// wrangler.jsonc - Add MCP OAuth secrets171{172"vars": {173"MCP_SERVER_URL": "https://mcp.example.com"174}175}176177// Set secrets via CLI178// npx wrangler secret put GITHUB_CLIENT_ID179// npx wrangler secret put GITHUB_CLIENT_SECRET180```181182Then register in agent code (see api.md MCP section).183