├── .gitignore ├── .env.example ├── package.json ├── Dockerfile ├── test.js ├── index.js ├── smithery.yaml ├── generateImage.js ├── readme.md └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | # files 2 | .env 3 | .DS_Store 4 | 5 | # directories 6 | /node_modules 7 | -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | REPLICATE_API_TOKEN = "replicate api token" 2 | STORAGE_ENDPOINT = "" 3 | STORAGE_ACCESS_KEY = "" 4 | STORAGE_SECRET_KEY = "" 5 | STORAGE_BUCKET = "" 6 | STORAGE_DOMAIN = "" -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mcp-replicate-flux", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "dependencies": { 13 | "@aws-sdk/client-s3": "^3.758.0", 14 | "@modelcontextprotocol/sdk": "^1.4.1", 15 | "dotenv": "^16.4.7", 16 | "replicate": "^1.0.1", 17 | "zod": "^3.24.1" 18 | }, 19 | "devDependencies": { 20 | "standardx": "^7.0.0" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Generated by https://smithery.ai. See: https://smithery.ai/docs/config#dockerfile 2 | FROM node:18-slim 3 | 4 | WORKDIR /app 5 | 6 | # 复制package.json和package-lock.json 7 | COPY package*.json ./ 8 | 9 | # 安装依赖 10 | RUN npm install 11 | 12 | # 复制所有项目文件 13 | COPY . . 14 | 15 | # 暴露端口(如果需要使用SSE模式) 16 | EXPOSE 3000 17 | 18 | # 设置环境变量(这些将在运行时被覆盖) 19 | ENV REPLICATE_API_TOKEN="" 20 | ENV STORAGE_ENDPOINT="" 21 | ENV STORAGE_ACCESS_KEY="" 22 | ENV STORAGE_SECRET_KEY="" 23 | ENV STORAGE_BUCKET="" 24 | ENV STORAGE_DOMAIN="" 25 | 26 | # 启动MCP服务器 27 | CMD ["node", "index.js"] 28 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const generateImage = require('./generateImage') 2 | 3 | async function test() { 4 | try { 5 | const prompt = 'a beautiful girl' 6 | const filename = `test-${Date.now()}.jpg` 7 | 8 | console.log(`Generating image with prompt: "${prompt}"`) 9 | console.log(`Filename: ${filename}`) 10 | 11 | const imageUrl = await generateImage({ prompt, filename }) 12 | 13 | console.log('Image generated and uploaded successfully!') 14 | console.log(`Image URL: ${imageUrl}`) 15 | } catch (error) { 16 | console.error('Test failed:', error) 17 | } 18 | } 19 | 20 | test() -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const { z } = require('zod') 2 | const { McpServer } = require('@modelcontextprotocol/sdk/server/mcp.js') 3 | const { 4 | StdioServerTransport 5 | } = require('@modelcontextprotocol/sdk/server/stdio.js') 6 | 7 | const generateImage = require('./generateImage') 8 | 9 | const server = new McpServer({ 10 | version: '1.0.0', 11 | name: 'Replicate-Flux' 12 | }) 13 | 14 | server.tool( 15 | 'generate-image', 16 | { prompt: z.string(), filename: z.string() }, 17 | async ({ prompt, filename }) => { 18 | try { 19 | const imageUrl = await generateImage({ prompt, filename }) 20 | return { 21 | content: [ 22 | { type: 'text', text: `Image successfully generated and uploaded to Cloudflare R2: ${imageUrl}` } 23 | ] 24 | } 25 | } catch (error) { 26 | console.error('Image generation failed:', error) 27 | return { 28 | content: [ 29 | { type: 'text', text: `Image generation failed: ${error.message}` } 30 | ] 31 | } 32 | } 33 | } 34 | ) 35 | 36 | const run = async () => { 37 | const transport = new StdioServerTransport() 38 | await server.connect(transport) 39 | } 40 | 41 | run() 42 | -------------------------------------------------------------------------------- /smithery.yaml: -------------------------------------------------------------------------------- 1 | # Smithery configuration file: https://smithery.ai/docs/config#smitheryyaml 2 | 3 | startCommand: 4 | type: stdio 5 | configSchema: 6 | # JSON Schema defining the configuration options for the MCP. 7 | type: object 8 | required: 9 | - replicateApiToken 10 | - storageEndpoint 11 | - storageAccessKey 12 | - storageSecretKey 13 | - storageBucket 14 | - storageDomain 15 | properties: 16 | replicateApiToken: 17 | type: string 18 | description: Token to authenticate with Replicate API 19 | storageEndpoint: 20 | type: string 21 | description: Endpoint URL for the S3 compatible storage (e.g., Cloudflare R2) 22 | storageAccessKey: 23 | type: string 24 | description: Access key for S3 storage 25 | storageSecretKey: 26 | type: string 27 | description: Secret key for S3 storage 28 | storageBucket: 29 | type: string 30 | description: Bucket name for storing the generated images 31 | storageDomain: 32 | type: string 33 | description: Domain for accessing images from the storage 34 | commandFunction: 35 | # A JS function that produces the CLI command based on the given config to start the MCP on stdio. 36 | |- 37 | (config) => ({ 38 | command: 'node', 39 | args: ['index.js'], 40 | env: { 41 | REPLICATE_API_TOKEN: config.replicateApiToken, 42 | STORAGE_ENDPOINT: config.storageEndpoint, 43 | STORAGE_ACCESS_KEY: config.storageAccessKey, 44 | STORAGE_SECRET_KEY: config.storageSecretKey, 45 | STORAGE_BUCKET: config.storageBucket, 46 | STORAGE_DOMAIN: config.storageDomain 47 | } 48 | }) 49 | exampleConfig: 50 | replicateApiToken: dummy-replicate-token 51 | storageEndpoint: https://dummy-storage.endpoint 52 | storageAccessKey: dummy-access-key 53 | storageSecretKey: dummy-secret-key 54 | storageBucket: dummy-bucket 55 | storageDomain: dummy.storage.domain 56 | -------------------------------------------------------------------------------- /generateImage.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const Replicate = require('replicate') 3 | const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3') 4 | require('dotenv').config({ path: path.resolve(__dirname, '.env') }) 5 | 6 | const s3Client = new S3Client({ 7 | endpoint: process.env.STORAGE_ENDPOINT, 8 | region: 'auto', 9 | credentials: { 10 | accessKeyId: process.env.STORAGE_ACCESS_KEY, 11 | secretAccessKey: process.env.STORAGE_SECRET_KEY, 12 | }, 13 | }) 14 | 15 | module.exports = async ({ prompt, filename }) => { 16 | const replicate = new Replicate({ 17 | auth: process.env.REPLICATE_API_TOKEN 18 | }) 19 | 20 | try { 21 | // 生成唯一文件名,避免覆盖 22 | const timestamp = Date.now() 23 | const fileNameWithoutExt = path.basename(filename, path.extname(filename)) 24 | const fileExt = path.extname(filename) || '.jpg' 25 | const uniqueFilename = `${fileNameWithoutExt}-${timestamp}${fileExt}` 26 | 27 | // 创建基于日期的文件夹结构 28 | const date = new Date() 29 | const year = date.getFullYear() 30 | const month = String(date.getMonth() + 1).padStart(2, '0') // 月份从0开始,需要+1 31 | const day = String(date.getDate()).padStart(2, '0') 32 | const folderPath = `${year}/${month}/${day}` 33 | 34 | // 完整的文件路径,包含日期文件夹 35 | const fullFilePath = `${folderPath}/${uniqueFilename}` 36 | 37 | // 使用 Replicate 生成图片 38 | const output = await replicate.run('black-forest-labs/flux-schnell', { 39 | input: { 40 | prompt, 41 | go_fast: true, 42 | num_outputs: 1, 43 | megapixels: '1', 44 | output_quality: 80, 45 | aspect_ratio: '16:9', 46 | output_format: 'jpg', 47 | num_inference_steps: 4 48 | } 49 | }) 50 | 51 | if (!output || !output[0]) { 52 | throw new Error('No image generated') 53 | } 54 | 55 | const imageUrl = output[0] 56 | 57 | // 获取图片数据 58 | const response = await fetch(imageUrl) 59 | if (!response.ok) { 60 | throw new Error('Failed to fetch generated image') 61 | } 62 | const imageBuffer = await response.arrayBuffer() 63 | 64 | // 上传到 R2,使用包含日期文件夹的路径 65 | await s3Client.send(new PutObjectCommand({ 66 | Bucket: process.env.STORAGE_BUCKET, 67 | Key: fullFilePath, 68 | Body: Buffer.from(imageBuffer), 69 | ContentType: 'image/jpeg' 70 | })) 71 | 72 | // 返回可访问的图片链接 73 | const r2ImageUrl = `https://${process.env.STORAGE_DOMAIN}/${fullFilePath}` 74 | return r2ImageUrl 75 | } catch (error) { 76 | console.error('Error in generateImage:', error) 77 | throw error 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # MCP Replicate FLUX 2 | 3 | [](https://smithery.ai/server/@andylee20014/mcp-replicate-flux) 4 | 5 | A Model Context Protocol (MCP) server that generates images using Replicate's FLUX model and stores them in Cloudflare R2. 6 | 7 | 使用 Replicate 的 FLUX 模型生成图片并存储到 Cloudflare R2 的模型上下文协议(MCP)服务器。 8 | 9 | ## Features 功能特点 10 | 11 | - Generate images using Replicate's black-forest-labs/flux-schnell model 12 | - Store generated images in Cloudflare R2 13 | - Return accessible image URLs 14 | - Support for custom prompts and filenames 15 | 16 | - 使用 Replicate 的 black-forest-labs/flux-schnell 模型生成图片 17 | - 将生成的图片存储到 Cloudflare R2 18 | - 返回可访问的图片链接 19 | - 支持自定义提示词和文件名 20 | 21 | ## Prerequisites 前置要求 22 | 23 | - Node.js (v16 or higher) 24 | - Replicate API token 25 | - Cloudflare R2 bucket and credentials 26 | - npm or yarn 27 | 28 | - Node.js(v16 或更高版本) 29 | - Replicate API 令牌 30 | - Cloudflare R2 存储桶和凭证 31 | - npm 或 yarn 32 | 33 | ## Environment Variables 环境变量 34 | 35 | Required environment variables in `.env` file: 36 | 37 | `.env` 文件中需要的环境变量: 38 | 39 | ```env 40 | REPLICATE_API_TOKEN=your_replicate_token 41 | STORAGE_ENDPOINT=your_r2_endpoint 42 | STORAGE_ACCESS_KEY=your_r2_access_key 43 | STORAGE_SECRET_KEY=your_r2_secret_key 44 | STORAGE_BUCKET=your_bucket_name 45 | STORAGE_DOMAIN=your_domain 46 | ``` 47 | 48 | ## Local Installation 本地安装 49 | 50 | ### Installing via Smithery 51 | 52 | To install MCP Replicate FLUX for Claude Desktop automatically via [Smithery](https://smithery.ai/server/@andylee20014/mcp-replicate-flux): 53 | 54 | ```bash 55 | npx -y @smithery/cli install @andylee20014/mcp-replicate-flux --client claude 56 | ``` 57 | 58 | ### Installing manually 59 | 60 | Follow these steps to set up the project locally: 61 | 62 | 按照以下步骤在本地设置项目: 63 | 64 | ```bash 65 | # Clone the repository 66 | git clone https://github.com/andylee20014/mcp-replicate-flux.git 67 | cd mcp-replicate-flux 68 | 69 | # Install dependencies 70 | npm install 71 | 72 | # Create .env file and add your credentials 73 | cp .env.example .env 74 | # Edit .env with your actual credentials 75 | ``` 76 | 77 | Make sure your Cloudflare R2 bucket has appropriate CORS settings for public access to the uploaded images. 78 | 79 | 确保您的 Cloudflare R2 存储桶具有适当的 CORS 设置,以允许公共访问上传的图片。 80 | 81 | ## Running Locally 本地运行 82 | 83 | To start the MCP server locally: 84 | 85 | 在本地启动 MCP 服务器: 86 | 87 | ```bash 88 | node index.js 89 | ``` 90 | 91 | The server will start and listen for MCP protocol messages on standard input/output. 92 | 93 | 服务器将启动并通过标准输入/输出监听 MCP 协议消息。 94 | 95 | ## Usage 使用方法 96 | 97 | The server provides a `generate-image` tool that accepts two parameters: 98 | - `prompt`: The text prompt for image generation 99 | - `filename`: The desired filename for the generated image 100 | 101 | 服务器提供了一个 `generate-image` 工具,接受两个参数: 102 | - `prompt`:用于生成图片的文本提示词 103 | - `filename`:生成的图片所需的文件名 104 | 105 | ### Integration Example 集成示例 106 | 107 | Here's an example of how to integrate with this MCP server using the MCP client library: 108 | 109 | 以下是使用 MCP 客户端库与此 MCP 服务器集成的示例: 110 | 111 | ```javascript 112 | const { McpClient } = require('@modelcontextprotocol/sdk/client/mcp.js') 113 | 114 | async function generateImage(prompt, filename) { 115 | const client = new McpClient() 116 | // Connect to your running MCP server 117 | await client.connect(yourTransport) 118 | 119 | const result = await client.tools.call('generate-image', { 120 | prompt, 121 | filename 122 | }) 123 | 124 | return result 125 | } 126 | ``` 127 | 128 | Example response 示例响应: 129 | ```json 130 | { 131 | "content": [ 132 | { "type": "text", "text": "Image successfully generated and uploaded to Cloudflare R2" }, 133 | { "type": "image", "url": "https://your-domain.com/filename.jpg" } 134 | ] 135 | } 136 | ``` 137 | 138 | ## Testing 测试 139 | 140 | A test script is provided to verify the image generation and upload functionality. 141 | 142 | 提供了一个测试脚本,用于验证图片生成和上传功能。 143 | 144 | ### Running the test 运行测试 145 | 146 | ```bash 147 | node test.js 148 | ``` 149 | 150 | The test script will: 151 | 1. Generate an image with a sample prompt 152 | 2. Upload the image to Cloudflare R2 153 | 3. Return the accessible URL 154 | 155 | 测试脚本将: 156 | 1. 使用示例提示词生成图片 157 | 2. 将图片上传到 Cloudflare R2 158 | 3. 返回可访问的 URL 159 | 160 | Example test output 测试输出示例: 161 | ``` 162 | Generating image with prompt: "a beautiful girl" 163 | Filename: test-1234567890.jpg 164 | Image generated and uploaded successfully! 165 | Image URL: https://your-domain.com/test-1234567890.jpg 166 | ``` 167 | 168 | You can modify the prompt in `test.js` to test different image generation scenarios. 169 | 170 | 您可以在 `test.js` 中修改提示词来测试不同的图片生成场景。 171 | 172 | ## Cursor MCP Configuration Cursor MCP配置 173 | 174 | There are two ways to configure the MCP server in Cursor: 175 | 176 | 在Cursor中配置MCP服务器有两种方式: 177 | 178 | ### Method 1: Complete Configuration 完整配置方式 179 | 180 | Create a file at `~/.cursor/mcp.json` with all environment variables: 181 | 182 | 在 `~/.cursor/mcp.json` 创建完整配置文件,包含所有环境变量: 183 | 184 | ```json 185 | { 186 | "mcpServers": { 187 | "replicate-flux": { 188 | "command": "node", 189 | "args": ["C:\\Users\\YourUsername\\path\\to\\mcp-replicate-flux\\index.js"], 190 | "description": "使用Replicate的FLUX模型生成图片并存储到Cloudflare R2的MCP服务器", 191 | "env": { 192 | "REPLICATE_API_TOKEN": "your_replicate_token", 193 | "STORAGE_ENDPOINT": "your_r2_endpoint", 194 | "STORAGE_ACCESS_KEY": "your_r2_access_key", 195 | "STORAGE_SECRET_KEY": "your_r2_secret_key", 196 | "STORAGE_BUCKET": "your_bucket_name", 197 | "STORAGE_DOMAIN": "your_domain" 198 | } 199 | } 200 | } 201 | } 202 | ``` 203 | 204 | ### Method 2: Minimal Configuration 最小配置方式 205 | 206 | If you prefer to manage environment variables separately (using system environment variables or .env file), you can use a minimal configuration: 207 | 208 | 如果你想单独管理环境变量(使用系统环境变量或.env文件),可以使用最小配置: 209 | 210 | ```json 211 | { 212 | "mcpServers": { 213 | "replicate-flux": { 214 | "command": "node", 215 | "args": ["C:\\Users\\YourUsername\\path\\to\\mcp-replicate-flux\\index.js"] 216 | } 217 | } 218 | } 219 | ``` 220 | 221 | When using the minimal configuration, make sure your environment variables are properly set up either in: 222 | - System environment variables 223 | - `.env` file in the project directory 224 | - Cloud platform's environment variables (for deployed servers) 225 | 226 | 使用最小配置时,请确保你的环境变量正确设置在以下位置之一: 227 | - 系统环境变量 228 | - 项目目录中的 `.env` 文件 229 | - 云平台的环境变量(对于已部署的服务器) 230 | 231 | ### Notes 注意事项 232 | 233 | - The `description` field is optional in both methods 234 | - Environment variables in the configuration file take precedence over system environment variables 235 | - After changing the configuration, restart Cursor to apply the changes 236 | 237 | - `description` 描述字段在两种方式中都是可选的 238 | - 配置文件中的环境变量优先级高于系统环境变量 239 | - 更改配置后,需要重启Cursor以应用更改 240 | 241 | ## Project Structure 项目结构 242 | 243 | ``` 244 | mcp-replicate-flux/ 245 | ├── .env # Environment variables 246 | ├── index.js # MCP server entry point 247 | ├── generateImage.js # Image generation and R2 upload logic 248 | ├── test.js # Test script 249 | └── README.md # Project documentation 250 | ``` 251 | 252 | ## Troubleshooting 故障排除 253 | 254 | ### Common Issues 常见问题 255 | 256 | 1. **Image generation fails**: Check your Replicate API token and quota 257 | 2. **R2 upload fails**: Verify your R2 credentials and bucket permissions 258 | 3. **Cannot access generated images**: Ensure your R2 bucket has proper CORS configuration 259 | 260 | 1. **图片生成失败**:检查您的 Replicate API 令牌和配额 261 | 2. **R2 上传失败**:验证您的 R2 凭证和存储桶权限 262 | 3. **无法访问生成的图片**:确保您的 R2 存储桶具有正确的 CORS 配置 263 | 264 | ## MCP Protocol 协议说明 265 | 266 | The Model Context Protocol (MCP) is a standard for AI model interaction that enables interoperability between different AI systems. This server implements an MCP tool that can be used with any MCP-compatible client. 267 | 268 | 模型上下文协议(MCP)是一种 AI 模型交互标准,可实现不同 AI 系统之间的互操作性。此服务器实现了一个 MCP 工具,可与任何兼容 MCP 的客户端一起使用。 269 | 270 | For more information about MCP, visit: [https://mcp.freeaigen.com/](https://mcp.freeaigen.com/) 271 | 272 | ## References 参考链接 273 | 274 | - [Model Context Protocol Documentation](https://mcp.freeaigen.com/) 275 | - [Replicate API Documentation](https://replicate.com/docs) 276 | - [Cloudflare R2 Documentation](https://developers.cloudflare.com/r2/) 277 | - [Flux AI Model](https://replicate.com/black-forest-labs/flux-schnell) 278 | 279 | ## License 许可证 280 | 281 | MIT 282 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 |101 | 基于 Model Context Protocol (MCP) 的图像生成服务,使用 Replicate 的 FLUX 模型生成高质量图片并存储到 Cloudflare R2 102 |
103 |124 | 使用 Replicate 的 FLUX 模型生成高质量图片,支持多种风格和场景 125 |
126 |137 | 自动将生成的图片存储到 Cloudflare R2,确保高速访问和可靠存储 138 |
139 |150 | 通过 MCP 协议轻松集成到任何支持 MCP 的应用中 151 |
152 |git clone https://github.com/andylee20014/mcp-replicate-flux.git 174 | cd mcp-replicate-flux175 |
npm install180 |
创建 .env 文件并添加以下配置:
184 |REPLICATE_API_TOKEN=your_token 186 | STORAGE_ENDPOINT=your_endpoint 187 | STORAGE_ACCESS_KEY=your_key 188 | STORAGE_SECRET_KEY=your_secret 189 | STORAGE_BUCKET=your_bucket 190 | STORAGE_DOMAIN=your_domain191 |
node index.js196 |
const { McpClient } = require('@modelcontextprotocol/sdk/client/mcp.js')
234 |
235 | async function generateImage(prompt, filename) {
236 | const client = new McpClient()
237 | await client.connect(yourTransport)
238 |
239 | const result = await client.tools.call('generate-image', {
240 | prompt,
241 | filename
242 | })
243 |
244 | return result
245 | }
246 | 261 | 在Cursor中配置MCP服务器有两种方式 262 |
263 |在 ~/.cursor/mcp.json 创建完整配置文件,包含所有环境变量:
269 |{
271 | "mcpServers": {
272 | "replicate-flux": {
273 | "command": "node",
274 | "args": ["C:\\Users\\YourUsername\\path\\to\\mcp-replicate-flux\\index.js"],
275 | "description": "使用Replicate的FLUX模型生成图片并存储到Cloudflare R2的MCP服务器",
276 | "env": {
277 | "REPLICATE_API_TOKEN": "your_replicate_token",
278 | "STORAGE_ENDPOINT": "your_r2_endpoint",
279 | "STORAGE_ACCESS_KEY": "your_r2_access_key",
280 | "STORAGE_SECRET_KEY": "your_r2_secret_key",
281 | "STORAGE_BUCKET": "your_bucket_name",
282 | "STORAGE_DOMAIN": "your_domain"
283 | }
284 | }
285 | }
286 | }
287 | 如果你想单独管理环境变量(使用系统环境变量或.env文件),可以使用最小配置:
291 |{
293 | "mcpServers": {
294 | "replicate-flux": {
295 | "command": "node",
296 | "args": ["C:\\Users\\YourUsername\\path\\to\\mcp-replicate-flux\\index.js"]
297 | }
298 | }
299 | }
300 | 334 | 以下是一些使用 MCP Replicate FLUX 生成的图片示例 335 |
336 |
339 |
340 |
341 |