All skills
cloudflare avatar

/agents-sdk

@41e0d19 official
by cloudflarecloudflare/skills3k stars
298

Build, debug, or review Cloudflare Agents SDK applications using the agents package.

Use this Skill: https://skilld.dev/gh/cloudflare/skills/agents-sdk

This session only. Nothing lands on disk.

referencesemail.md

≈889 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Email Handling

Fetch https://developers.cloudflare.com/agents/communication-channels/email/index.md for complete documentation.

Overview

Agents receive and reply to emails via Cloudflare Email Routing.

Wrangler Configuration

{
  "durable_objects": {
    "bindings": [{ "name": "EmailAgent", "class_name": "EmailAgent" }]
  },
  "migrations": [{ "tag": "v1", "new_sqlite_classes": ["EmailAgent"] }],
  "send_email": [
    { "name": "SEB", "destination_address": "reply@yourdomain.com" }
  ]
}

Basic Email Handler

import { Agent } from "agents";
import { type AgentEmail } from "agents/email";
import PostalMime from "postal-mime";

export class EmailAgent extends Agent<Env, State> {
  async onEmail(email: AgentEmail) {
    const raw = await email.getRaw();
    const parsed = await PostalMime.parse(raw);

    console.log("From:", email.from);
    console.log("Subject:", parsed.subject);

    await this.replyToEmail(email, {
      fromName: "My Agent",
      subject: `Re: ${parsed.subject}`,
      body: "Thanks for your email!"
    });
  }
}

Routing Emails

import { routeAgentRequest, routeAgentEmail } from "agents";
import { createAddressBasedEmailResolver } from "agents/email";

export default {
  async email(message, env) {
    await routeAgentEmail(message, env, {
      resolver: createAddressBasedEmailResolver("EmailAgent")
    });
  },

  async fetch(request, env) {
    return routeAgentRequest(request, env) ?? new Response("Not found", { status: 404 });
  }
};

Resolvers

Address-Based (Inbound Mail)

Routes based on recipient address:

import { createAddressBasedEmailResolver } from "agents/email";

const resolver = createAddressBasedEmailResolver("EmailAgent");
// support@example.com → EmailAgent, instance "support"
// NotificationAgent+user123@example.com → NotificationAgent, instance "user123"

Secure Reply (Reply Flows)

Verifies replies are authentic using HMAC-SHA256 signatures:

import { createSecureReplyEmailResolver } from "agents/email";

const resolver = createSecureReplyEmailResolver(env.EMAIL_SECRET, {
  maxAge: 7 * 24 * 60 * 60, // 7 days (default: 30 days)
  onInvalidSignature: (email, reason) => {
    console.warn(`Invalid signature from ${email.from}: ${reason}`);
  }
});

Sign outbound emails to enable secure reply routing:

await this.replyToEmail(email, {
  fromName: "My Agent",
  body: "Thanks!",
  secret: this.env.EMAIL_SECRET  // Signs headers for secure reply routing
});

Catch-All (Single Instance)

Routes all emails to one agent instance:

import { createCatchAllEmailResolver } from "agents/email";

const resolver = createCatchAllEmailResolver("EmailAgent", "default");

Combining Resolvers

async email(message, env) {
  const secureReply = createSecureReplyEmailResolver(env.EMAIL_SECRET);
  const addressBased = createAddressBasedEmailResolver("EmailAgent");

  await routeAgentEmail(message, env, {
    resolver: async (email, env) => {
      // Try secure reply first
      const result = await secureReply(email, env);
      if (result) return result;
      // Fall back to address-based
      return addressBased(email, env);
    }
  });
}

Utilities

import { isAutoReplyEmail } from "agents/email";

async onEmail(email: AgentEmail) {
  if (isAutoReplyEmail(email.headers)) {
    // Skip auto-replies (vacation, out-of-office, etc.)
    return;
  }
  // Process email...
}

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides reference documentation and configuration guidelines for building, debugging, and reviewing applications using the Cloudflare Agents SDK. It consists entirely of educational markdown files and contains no executable code or security risks.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    8 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 41e0d19. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 5 hours ago.

Activeupdated 7 hours ago
  • MCP
  • cloudflare
  • workers
  • agents
  • state-management
  • websocket
  • durable-objects
  • scheduling
  • workflows
  • observable

README badge

README badge for cloudflare/skills/agents-sdk

Builds stateful AI agents on Cloudflare Workers using Durable Objects, with APIs for persistent state, callable RPC methods, scheduling, workflows, durable execution, queues, and streaming chat. Covers the full lifecycle of agent development from configuration through observability, with experimental support for voice, browser automation, and MCP integration.

Generated from the current SKILL.md.

Does this work with existing Cloudflare Workers projects?
Yes. Use the 'Add to existing project' guide to install the SDK into an existing Workers app. You'll need to configure durable objects and migrations in wrangler.jsonc.
What state management does the SDK provide?
SQLite-backed persistent state that auto-syncs to clients via setState(). State changes trigger validateStateChange() and onStateUpdate() hooks, and the SDK includes built-in SQL query support.
Can I use this with chat applications?
Yes. The SDK includes AIChatAgent for streaming chat with tools, message persistence, and resumable streams. It requires @cloudflare/ai-chat and ai packages.
Does this support background workflows and scheduled tasks?
Yes. The SDK provides AgentWorkflow for durable multi-step tasks, schedule() / scheduleEvery() for one-time and recurring tasks, and runFiber() for work that survives durable object eviction.
Can I connect to external MCP servers or build MCP servers with this?
Yes. The SDK includes MCP client integration to connect to external MCP servers and McpAgent to build MCP servers with configurable transports (HTTP, SSE, RPC).

Generated from the current SKILL.md. These answers refresh after source changes.