AGENTS.md · cloudflare/moltworker
AGENTS.md from cloudflare/moltworker: 262 lines covering Project Overview, Project Structure, Key Patterns, Commands and more.
Template by cloudflare
Install
Works with Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode, Claude Code. Read the repository before you give any tool access to your data.
curl -fsSL https://raw.githubusercontent.com/cloudflare/moltworker/main/AGENTS.md -o AGENTS.md
Read it and adapt it to your project before use.
Get the weekly diff
The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.
About
Use cases: cloud-management, code-generation
Reviews
Write a reviewNo reviews yet. Installed it? Reviews come from verified installers only. How reviews work
What's in it
An AGENTS.md file: instructions coding agents (Codex, Cursor, Copilot, Gemini CLI and others) read before working in a repo.
- For
- Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode, Claude Code
- Length
- 262 lines
- File
AGENTS.md
Sections
- Project Overview
- Project Structure
- Key Patterns
- Commands
- Testing
- Code Style
- Documentation
- Architecture
- Local Development
- Docker Image Caching
- Gateway Configuration
- OpenClaw Config Schema
- Common Tasks
- R2 Storage Notes
First lines (shown under the repo's Apache-2.0 license)
# Agent Instructions
Guidelines for AI agents working on this codebase.
## Project Overview
This is a Cloudflare Worker that runs [OpenClaw](https://github.com/openclaw/openclaw) (formerly Moltbot/Clawdbot) in a Cloudflare Sandbox container. It provides:
- Proxying to the OpenClaw gateway (web UI + WebSocket)
- Admin UI at `/_admin/` for device management
- API endpoints at `/api/*` for device pairing
- Debug endpoints at `/debug/*` for troubleshooting
**Note:** The CLI tool and npm package are now named `openclaw`. Config files use `.openclaw/openclaw.json`. Legacy `.clawdbot` paths are supported for backward compatibility during transition.
## Project Structure
```
src/
├── index.ts # Main Hono app, route mounting
├── types.ts # TypeScript type definitions
├── config.ts # Constants (ports, timeouts, paths)
├── auth/ # Cloudflare Access authentication
│ ├── jwt.ts # JWT verification
│ ├── jwks.ts # JWKS fetching and caching
│ └── middleware.ts # Hono middleware for auth
├── gateway/ # OpenClaw gateway management
│ ├── process.ts # Process lifecycle (find, start)
│ ├── env.ts # Environment variable building
│ ├── r2.ts # R2 bucket mounting
│ ├── sync.ts # R2 backup sync logic
│ └── utils.ts # Shared utilities (waitForProcess)
├── routes/ # API route handlers
│ ├── api.ts # /api/* endpoints (devices, gateway)
│ ├── admin.ts # /_admin/* static file serving
│ └── debug.ts # /debug/* endpoints
└── client/ # React admin UI (Vite)
├── App.tsx
├── api.ts # API client
└── pages/
```
## Key Patterns
### Environment Variables
- `DEV_MODE` - Skips CF Access auth AND bypasses device pairing (maps to `OPENCLAW_DEV_MODE` for container)
- `DEBUG_ROUTES` - Enables `/debug/*` routes (disabled by default)
- See `src/types.ts` for full `MoltbotEnv` interface
### CLI Commands
When calling the OpenClaw CLI from the worker, always include `--url ws://localhost:18789`:
```typescript
sandbox.startProcess('openclaw devices list --json --url ws://localhost:18789')
```
CLI commands take 10-15 seconds due to WebSocket connection overhead. Use `waitForProcess()` helper in `src/routes/api.ts`.
### Success Detection
The CLI outputs "Approved" (capital A). Use case-insensitive checks:
```typescript
stdout.toLowerCase().includes('approved')
```
## Commands
```bash
npm test # Run tests (vitest)
npm run test:watch # Run tests in watch mode
npm run build # Build worker + client
npm run deploy # Build and deploy to Cloudflare
npm run dev # Vite dev server
npm run start # wrangler dev (local worker)
npm run typecheck # TypeScript check
```
## Testing
Tests use Vitest. Test files are colocated with source files (`*.test.ts`).
Current test coverage:
- `auth/jwt.test.ts` - JWT decoding and validation
- `auth/jwks.test.ts` - JWKS fetching and caching
- `auth/middleware.test.ts` - Auth middleware behavior
- `gateway/env.test.ts` - Environment variable building
- `gateway/process.test.ts` - Process finding logic
- `gateway/r2.test.ts` - R2 mounting logic
- `gateway/sync.test.ts` - R2 backup sync logic
When adding new functionality, add corresponding tests.
## Code Style
- Use TypeScript strict mode
- Prefer explicit types over inference for function signatures
- Keep route handlers thin - extract logic to separate modules
- Use Hono's context methods (`c.json()`, `c.html()`) for responses
## Documentation
- `README.md` - User-facing documentation (setup, configuration, usage)
- `AGENTS.md` - This file, for AI agents
Development documentation goes in AGENTS.md, not README.md.
---
## Architecture
```
Browser
│
▼
┌─────────────────────────────────────┐
│ Cloudflare Worker (index.ts) │
│ - Starts OpenClaw in sandbox │
│ - Proxies HTTP/WebSocket requests │
│ - Passes secrets as env vars │
└──────────────┬──────────────────────┘
│
▼
┌─────────────────────────────────────┐
│ Cloudflare Sandbox Container │
│ ┌───────────────────────────────┐ │
│ │ OpenClaw Gateway │ │
│ │ - Control UI on port 18789 │ │
│ │ - WebSocket RPC protocol │ │
│ │ - Agent runtime │ │
│ └───────────────────────────────┘ │
└─────────────────────────────────────┘
```
### Key Files
| File | Purpose |
|------|---------|
| `src/index.ts` | Worker that manages sandbox lifecycle and proxies requests |
| `Dockerfile` | Container image based on `cloudflare/sandbox` with Node 22 + OpenClaw |
| `start-openclaw.sh` | Startup script: R2 restore → onboard → config patch → launch gateway |
| `wrangler.jsonc` | Cloudflare Worker + Container configuration |
## Local Development
```bash
npm install
cp .dev.vars.example .dev.vars
# Edit .dev.vars with your ANTHROPIC_API_KEY
npm run start
```
### Environment Variables
For local development, create `.dev.vars`:
```bash
ANTHROPIC_API_KEY=sk-ant-...
DEV_MODE=true # Skips CF Access auth + device pairing
DEBUG_ROUTES=true # Enables /debug/* routes
```Signals
- Stars
- 9,944
- Forks
- 1,707
- Open issues
- 61
- Last commit (this folder)
- Feb 6, 2026 (8 months ago)
From GitHub (fetched Oct 24, 2026).
Details
- License
- Apache-2.0
- Repository
- github.com/cloudflare/moltworker
- First seen
- Oct 24, 2026
- Last checked
- Oct 24, 2026
- Source
- GitHubgithub-graphql
More from cloudflare
- 0.844score
basin
cloudflareOfficial
Build and troubleshoot Cloudflare Basin analytics workflows with Basin Pipelines, Basin Catalog, and Basin SQL.
SkillAFreshness: A★ 3.2KClaude Code +9 - 0.842score
workers-best-practices
cloudflareOfficial
Cloudflare Workers best practices for production applications.
SkillAFreshness: A★ 3.2KClaude Code +9 - 0.840score
sandbox-next
cloudflareOfficial
Build or maintain Cloudflare Sandbox apps on @cloudflare/sandbox@next (SDK 1.0 preview).
SkillAFreshness: A★ 3.2KClaude Code +9 - 0.840score
turnstile-spin
cloudflareOfficial
Set up, repair, or migrate to Cloudflare Turnstile bot verification in an existing frontend and backend, including server-side Siteverify.
SkillAFreshness: A★ 3.2KClaude Code +9 - 0.839score
cloudflare
cloudflareOfficial
Discover and choose Cloudflare products for apps, APIs, AI agents, storage, networking, and security.
SkillAFreshness: A★ 3.2KClaude Code +9 - 0.838score
cf-cli
cloudflareOfficial
Cloudflare's official cf CLI (beta) for the public Cloudflare API and Workers projects.
SkillAFreshness: A★ 3.2KClaude Code +9
Cite this page
AGENTS.md · cloudflare/moltworker. Agent Mart. https://agentmart.dev/templates/cloudflare-moltworker-agents-md (last updated 2026-10-24)