cloudflare-image-mcp
Version:
Cloudflare Workers AI Image Generator MCP Server
171 lines (125 loc) • 5.8 kB
Markdown
A TypeScript MCP server that provides image generation capabilities using Cloudflare Workers AI text-to-image models.
- **6 Supported Models**: FLUX Schnell, SDXL Base, SDXL Lightning, DreamShaper LCM, Leonardo Phoenix, Leonardo Lucid Origin
- **Modular Architecture**: Easy to extend with new models by adding files to `src/models/`
- **Advanced Features**: negative prompts, size control, guidance optimization
- **Type-Safe**: Full TypeScript support with Zod validation
- **MCP Compliant**: Works with Model Context Protocol
- Node.js 18+
- Cloudflare API Token and Account ID
```bash
git clone https://github.com/tan-yong-sheng/cloudflare-image-mcp.git
npm install
```
Set environment variables:
```bash
export CLOUDFLARE_API_TOKEN="your_api_token_here"
export CLOUDFLARE_ACCOUNT_ID="your_account_id_here"
export DEFAULT_IMAGE_GENERATION_MODEL="@cf/black-forest-labs/flux-1-schnell"
```
```bash
npm run check
npm run lint
npm run build
npm run dev
```
```bash
npm run lint
npm run check
npm run build
npm publish
```
Generate an image using Cloudflare Workers AI. The model is configured via the `DEFAULT_IMAGE_GENERATION_MODEL` environment variable.
```typescript
{
"prompt": "A beautiful sunset over mountains",
"size": "1024x1024", // Optional
"negativePrompt": "blurry, low quality", // Optional
"steps": 4, // Optional (model-dependent)
"guidance": 7.5, // Optional (model-dependent)
"seed": 12345, // Optional
"imageB64": "base64_encoded_image", // Optional (for img2img)
"strength": 1.0 // Optional (for img2img)
}
```
List all available models with their capabilities and supported parameters.
| Model | Model ID | Size | Guidance | Negative |
|-------|------|------|----------|----------|
| FLUX Schnell | @cf/black-forest-labs/flux-1-schnell | ❌ | ❌ | ❌ |
| Stable Diffusion XL | @cf/stabilityai/stable-diffusion-xl-base-1.0 | ✅ | ✅ | ✅ |
| SDXL Lightning | @cf/bytedance/stable-diffusion-xl-lightning | ✅ | ✅ | ✅ |
| DreamShaper 8 LCM | @cf/lykon/dreamshaper-8-lcm | ✅ | ✅ | ✅ |
| Leonardo AI Phoenix 1.0 | @cf/leonardo/phoenix-1.0 | ✅ | ✅ | ✅ |
| Leonardo Lucid Origin | @cf/leonardo/lucid-origin | ✅ | ✅ | ❌ |
Read more [here](./docs/env_setup.md) to know how to get all environment variables for Cloudflare Workers AI and Cloudflare R2 storage required for this MCP setup.
- `CLOUDFLARE_API_TOKEN`: Your Cloudflare API token
- `CLOUDFLARE_ACCOUNT_ID`: Your Cloudflare account ID
- `DEFAULT_IMAGE_GENERATION_MODEL`: Default model to use (optional, defaults to lucid-origin)
### S3 Storage Configuration (Required)
- `S3_BUCKET`: S3 bucket name for storing generated images
- `S3_REGION`: S3 region (e.g., "auto")
- `S3_ACCESS_KEY`: S3 access key
- `S3_SECRET_KEY`: S3 secret key
- `S3_ENDPOINT`: Custom endpoint (e.g., Cloudflare R2 endpoint)
- `S3_CDN_URL`: Custom CDN URL for image links
### Performance & Rate Limiting Configuration (Optional)
- `IMAGE_GENERATION_CONCURRENCY`: Number of concurrent image generation requests (default: 2, range: 1-8)
- `IMAGE_GENERATION_BATCH_DELAY_MS`: Delay between batches of concurrent requests in milliseconds (default: 1000, range: 100-10000)
- `IMAGE_GENERATION_MAX_RETRIES`: Maximum retry attempts for rate-limited requests (default: 3, range: 0-10)
### Logging Configuration (Optional)
- `LOG_LEVEL`: Control logging verbosity (error, warn, info, debug) (default: info)
- `NODE_ENV=development`: Auto-enables debug mode with detailed logging
**Logging Features:**
- **Centralized logging system** with consistent formatting and timestamps
- **Service-specific prefixes**: `[Server]`, `[ImageService]`, `[S3 Storage]` for easy identification
- **Specialized loggers**: 🚦 rate limiting, 🌐 API, 💾 storage operations
- **Conditional logging**: Debug messages only show in debug mode
- **Performance timing**: Built-in timing helpers for performance monitoring
```bash
CLOUDFLARE_API_TOKEN="your_api_token_here"
CLOUDFLARE_ACCOUNT_ID="your_account_id_here"
DEFAULT_IMAGE_GENERATION_MODEL="@cf/black-forest-labs/flux-1-schnell"
S3_BUCKET="your-bucket-name"
S3_REGION="auto"
S3_ACCESS_KEY="your_access_key"
S3_SECRET_KEY="your_secret_key"
S3_ENDPOINT="https://your-account-id.r2.cloudflarestorage.com"
S3_CDN_URL="https://pub-....r2.dev"
IMAGE_GENERATION_CONCURRENCY=2
IMAGE_GENERATION_BATCH_DELAY_MS=1000
IMAGE_GENERATION_MAX_RETRIES=3
LOG_LEVEL=info
NODE_ENV=production
```
The server provides comprehensive error handling for:
- Missing configuration
- Invalid parameters
- API errors
- Timeout issues
- Unsupported model features