Agentic Inbox

repository·main·Indexed 27 days ago

https://github.com/cloudflare/agentic-inbox

A self-hosted email client powered by Cloudflare Workers, using Durable Objects for mailbox isolation, R2 for attachments, and the Cloudflare Agents SDK. It features an AI-powered email assistant capable of reading, searching, and drafting replies, and includes an MCP server implementation for integration with AI coding tools.

Tokens
6.7K
Snippets
5
Records
87
Agent score
91%

What's inside agentic-inbox

  1. Deploy Agentic Inbox to Cloudflare

    main

    You can deploy Agentic Inbox using the 'Deploy to Cloudflare' button which automatically provisions R2, Durable Objects, and Workers AI. During the deployment flow, you will be prompted for DOMAINS, which is the domain (e.g., yourdomain.com) you want to receive emails for.

    Note: Deployment is only the first step. You must complete the post-deployment configuration (Cloudflare Access, Email Routing, and Email Service) for the application to function correctly.

    https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/agentic-inbox
  2. Configure Cloudflare Access for Agentic Inbox

    main

    To protect your inbox, you must enable one-click Cloudflare Access on your Worker.

    1. Navigate to Settings > Domains & Routes in your Worker dashboard.
    2. Enable Access. A modal will appear displaying your POLICY_AUD and TEAM_DOMAIN values.
    3. Crucial: You must set both POLICY_AUD and TEAM_DOMAIN as secrets in your Worker configuration.

    TEAM_DOMAIN can be your Access team URL or the full .../cdn-cgi/access/certs URL.

    Troubleshooting Access Errors:

    • Invalid or expired Access token: This indicates POLICY_AUD or TEAM_DOMAIN secrets are incorrect. To fix, turn Access off and back on to regenerate the modal, then update your Worker secrets with the new values.
    • Cloudflare Access must be configured in production: This error occurs because the app enforces Access to prevent public exposure. Follow the setup steps above to enable it.
  3. Set up Email Routing and Service

    main

    For the inbox to receive and send emails, configure the following in your Cloudflare dashboard:

    1. Email Routing (Receiving): Go to your domain > Email Routing and create a catch-all rule that forwards incoming emails to this Worker.
    2. Email Service (Sending): The worker requires a send_email binding to send outbound emails. Ensure the Email Service is enabled.
  4. Local Development Setup

    main

    To run Agentic Inbox locally, follow these steps:

    1. Install dependencies:
      npm install
    2. Run the development server:
      npm run dev
    3. Configuration Requirements:
      • Set your domain in wrangler.jsonc.
      • Create an R2 bucket named agentic-inbox using: wrangler r2 bucket create agentic-inbox.
    npm install
    npm run dev
  5. Configure Cloudflare Access for production

    main

    In production environments, the application requires Cloudflare Access for JWT validation. To enable this, you must configure the following environment variables:

    • POLICY_AUD: The audience for the Cloudflare Access JWT.
    • TEAM_DOMAIN: The domain of your Cloudflare Access team.

    Validation is performed using the cf-access-jwt-assertion header. In development (import.meta.env.DEV), this validation is skipped.

  6. Configure custom agent system prompts

    main

    The EmailAgent uses a system prompt to define its behavior, writing style, and constraints. By default, it uses a strict DEFAULT_SYSTEM_PROMPT that enforces plain-text replies and prohibits meta-commentary.

    Overriding the prompt: You can override the default behavior on a per-mailbox basis by storing a custom prompt in R2. The agent looks for a JSON file at mailboxes/{mailboxId}.json containing an agentSystemPrompt field.

  7. Configure React Router settings in react-router.config.ts

    main

    The react-router.config.ts file is used to configure the React Router application. It accepts a configuration object that satisfies the Config type from @react-router/dev/config.

    Key configuration options used in this project:

    • ssr: A boolean indicating whether Server-Side Rendering is enabled. Set to true to enable SSR.
    • future: An object used to enable experimental or upcoming features. In this project, v8_viteEnvironmentApi is set to true to opt into the Vite Environment API.
    import type { Config } from "@react-router/dev/config";
    
    export default {
      ssr: true,
      future: {
        v8_viteEnvironmentApi: true,
      },
    } satisfies Config;
  8. Draft a new email with toolDraftEmail

    main

    Creates a new draft email. It can be a standalone email or a reply-style draft if in_reply_to or thread_id is provided.

    Key Options:

    • isPlainText: If true, the body is treated as plain text and converted to HTML.
    • runVerifyDraft: If true, uses AI to verify/sanitize the body before saving.
  9. Handle incoming emails via the email handler

    main

    The application exports a default fetch handler and an email handler. The email handler is used by Cloudflare Email Routing to process incoming messages using the receiveEmail function.

    If receiveEmail fails, the error is re-thrown to ensure Cloudflare's email routing can retry delivery or bounce the message rather than silently dropping it.