Build a Free MCP Flux Schnell Server on Cloudflare in 5 Minutes for Unlimited Text-to-Image Generation
This guide walks you through installing prerequisites, initializing a Node.js project, implementing the Model Context Protocol server with ListTools and CallTool handlers, configuring Cloudflare Flux API credentials, debugging, and integrating the server into Cursor to enable on‑demand text‑to‑image generation.
Overview of MCP and Project Goal
The Model Context Protocol (MCP) is an open standard that connects LLM applications with external data sources and tools. This article shows how to create an MCP Flux Schnell server that can be used by desktop clients such as Claude Desktop or Cursor to provide text‑to‑image services.
Prerequisites
Node.js installed locally.
A Cloudflare account.
Project Initialization
Run the following command to scaffold the server project:
npx @modelcontextprotocol/create-server mcp-flux-schnellFollow the prompts to complete initialization, then open the project in VSCode or Cursor and install dependencies: npm install or
pnpm installUnderstanding MCP Server Message Flow
The MCP specification defines a three‑party message flow among the LLM, MCP client, and MCP server. The diagram below illustrates this flow.
Implementing Core Server Logic
1. Create the MCP Server
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
const server = new Server(
{ name: "mcp-flux-schnell", version: "0.1.0" },
{ capabilities: { tools: {} } }
);2. Register ListTools Handler
The handler returns the list of tools the server supports. In this case, a single tool generate_image is defined.
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [{
name: "generate_image",
description: "Generate an image from a text prompt using Flux Schnell model",
inputSchema: {
type: "object",
properties: {
prompt: {
type: "string",
minLength: 1,
maxLength: 2048,
description: "A text description of the image you want to generate."
}
},
required: ["prompt"]
}
}]
};
});3. Register CallTool Handler
The handler validates the request using zod, calls the Cloudflare Flux API, saves the returned image, and returns a text response.
server.setRequestHandler(CallToolRequestSchema, async (request) => {
switch (request.params.name) {
case "generate_image": {
const validationResult = generateImageSchema.safeParse(request.params.arguments);
const { prompt } = validationResult.data;
const timestamp = new Date().getTime();
const filename = `flux-${timestamp}.png`;
const filepath = join(WORKING_DIR, filename);
try {
const response = await fetch(FLUX_API_URL, {
method: "POST",
headers: {
Authorization: `Bearer ${FLUX_API_TOKEN}`,
"Content-Type": "application/json"
},
body: JSON.stringify({ prompt })
});
if (!response.ok) {
const errorText = await response.text();
return { content: [{ type: "text", text: `API Error: Flux API returned status ${response.status}: ${errorText}` }] };
}
const result = await response.json();
const base64Data = result.image.replace(/^data:image\/png;base64,/, "");
await writeFile(filepath, Buffer.from(base64Data, "base64"));
return { content: [{ type: "text", text: `Image saved successfully:
Filename: ${filename}
Path: ${filepath}` }] };
} catch (error) {
// error handling omitted for brevity
}
break;
}
default:
return { content: [{ type: "text", text: "Tool Error: Unknown tool requested" }] };
}
});The server can return both text and image response types. The image format is:
{
"type": "image",
"data": "base64-encoded-data",
"mimeType": "image/png"
}Note: In Cursor version 0.47.5, the image type currently exceeds the length limit, so the client shows a warning.
Debugging the MCP Server
Run the inspector to start the server and view logs:
npm run inspector
# or
pnpm inspectorSuccessful start prints:
Stdio transport: command=build/index.js, args=dist/index.js
Spawned stdio transport
Connected MCP client to backing server transport
Created web app transport
Set up MCP proxy
🔍 MCP Inspector is up and running at http://localhost:5173 🚀Open the URL in a browser to begin debugging.
Configuring the Server in Cursor
Project‑Level Configuration
Create .cursor/mcp.json in the project root with the required environment variables:
{
"mcpServers": {
"mcp-flux-schnell": {
"command": "node",
"args": ["/path/to/mcp-flux-schnell/build/index.js"],
"env": {
"FLUX_API_URL": "your flux api url",
"FLUX_API_TOKEN": "your flux api token",
"WORKING_DIR": "your working directory"
}
}
}
}Add mcp.json to .gitignore to avoid leaking credentials.
Global Configuration (macOS example)
Place the same JSON file at ~/.cursor/mcp.json to apply the configuration system‑wide.
{
"mcpServers": {
"mcp-flux-schnell": {
"command": "node",
"args": ["/path/to/mcp-flux-schnell/build/index.js"],
"env": {
"FLUX_API_URL": "your flux api url",
"FLUX_API_TOKEN": "your flux api token",
"WORKING_DIR": "your working directory"
}
}
}
}Using the Server in Cursor
After adding the configuration, the server status appears in Cursor’s settings page.
Set the chat mode to Agent and send a prompt such as:
生成图片:masterpiece, best quality, highly detailed, 1girl, beautiful, anime style, eating breakfast, dining table, morning light, delicious food, long flowing hair, kawaiiThe generated image is returned and displayed as shown below.
Repository
https://github.com/bytefer/mcp-flux-schnell
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Full-Stack Cultivation Path
Focused on sharing practical tech content about TypeScript, Vue 3, front-end architecture, and source code analysis.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
