A MCP server that provides text-to-image generation capabilities using Stable Diffusion WebUI API (ForgeUI/AUTOMATIC-1111). It also supports optional image uploading to a Cloudflare ImgBed instance.
- Node.js (v18 or later recommended)
- npm
- Access to a Stable Diffusion WebUI instance with API enabled
- The WebUI must have
--api
flag enabled when starting - (Optional) Access to a Cloudflare ImgBed instance for image hosting.
-
Clone the repository:
git clone https://github.com/slot181/sd-image-gen-mcp.git cd sd-image-gen-mcp
-
Install dependencies:
npm install
This will install necessary packages including
axios
,sharp
, andform-data
. -
Build the server:
npm run build
-
Configure the server:
You can configure the server using command-line arguments (
-e KEY VALUE
) or environment variables. Command-line arguments take precedence over environment variables, which in turn take precedence over the default values defined in the code.Example using command-line arguments (Recommended for MCP integration):
Add the server configuration to your MCP client's settings (e.g., in VS Code settings.json):
{ "mcpServers": { "sd-image-gen": { // Choose a name for your server instance "command": "node", "args": [ "/path/to/sd-image-gen-mcp/build/index.js" ], "env": { "SD_WEBUI_URL": "http://your-sd-webui-url:7860", // Required: URL of your Stable Diffusion WebUI instance "SD_OUTPUT_DIR": "/path/to/output/directory", // Optional: if not provided, images will be saved to the server's running directory "SD_AUTH_USER": "your-username", // Optional: if authentication is enabled "SD_AUTH_PASS": "your-password", // Optional: if authentication is enabled "SD_RESIZE_MODE": "0", // Optional: upscaling mode (0=multiplier, 1=dimensions) "SD_UPSCALE_MULTIPLIER": "2", // Optional: default upscale multiplier "SD_UPSCALE_WIDTH": "1024", // Optional: default upscale width "SD_UPSCALE_HEIGHT": "1024", // Optional: default upscale height "SD_UPSCALER_1": "R-ESRGAN 4x+", // Optional: default primary upscaler "SD_UPSCALER_2": "None", // Optional: default secondary upscaler "CF_IMGBED_UPLOAD_URL": "https://your-imgbed-url/upload", // Optional: if Cloudflare ImgBed is enabled "CF_IMGBED_API_KEY": "your-imgbed-api-key", // Optional: if Cloudflare ImgBed is enabled "REQUEST_TIMEOUT": "300000" // Optional: timeout for requests to the SD WebUI API } } } }
Configuration Parameters:
-
SD_WEBUI_URL
(Required): URL of your Stable Diffusion WebUI instance (e.g.,http://127.0.0.1:7860
). -
SD_OUTPUT_DIR
(Optional): Directory where generated images will be saved locally. Defaults to./output
relative to the server's running directory. -
SD_AUTH_USER
(Optional): Username for WebUI basic authentication, if enabled. -
SD_AUTH_PASS
(Optional): Password for WebUI basic authentication, if enabled. -
REQUEST_TIMEOUT
(Optional): Timeout in milliseconds for requests to the SD WebUI API. Defaults to300000
(5 minutes). -
CF_IMGBED_UPLOAD_URL
(Optional): The upload endpoint URL for your Cloudflare ImgBed instance. If set along withCF_IMGBED_API_KEY
, generated images will be uploaded. -
CF_IMGBED_API_KEY
(Optional): The API key (authCode) for your Cloudflare ImgBed instance. -
SD_RESIZE_MODE
(Optional): Default upscaling mode (0=multiplier, 1=dimensions). Defaults to0
. -
SD_UPSCALE_MULTIPLIER
(Optional): Default upscale multiplier whenSD_RESIZE_MODE
is 0. Defaults to2
. -
SD_UPSCALE_WIDTH
(Optional): Default target width whenSD_RESIZE_MODE
is 1. Defaults to1024
. -
SD_UPSCALE_HEIGHT
(Optional): Default target height whenSD_RESIZE_MODE
is 1. Defaults to1024
. -
SD_UPSCALER_1
(Optional): Default primary upscaler model name. Defaults to"R-ESRGAN 4x+"
. -
SD_UPSCALER_2
(Optional): Default secondary upscaler model name. Defaults to"None"
.
-
-
generate_image
: Generate images using Stable Diffusion.-
Parameters:
-
prompt
(required): Text description of the desired image. -
negative_prompt
: Things to exclude from the image. (Defaults to a general list of negative terms if not provided). -
steps
: Number of sampling steps (default: 20, range: 1-150). -
width
: Image width (default: 1024, range: 512-2048). -
height
: Image height (default: 1024, range: 512-2048). -
cfg_scale
: CFG scale (default: 3.5, range: 1-30). -
sampler_name
: Sampling algorithm (default: "Euler a"). -
scheduler_name
: Scheduler algorithm (default: "Automatic"). -
seed
: Random seed (-1 for random). -
batch_size
: Number of images to generate (default: 1, max: 4). -
restore_faces
: Enable face restoration (boolean). -
tiling
: Generate tileable images (boolean). -
output_path
: Custom local output directory for the generated image(s), overridesSD_OUTPUT_DIR
.
-
-
Returns: A JSON string representing an array of generated image results. Each result object contains:
-
path
: The local file path where the image was saved. -
parameters
: The generation parameters string obtained from the image metadata. -
url
: The public URL of the image if uploaded to Cloudflare ImgBed (requiresCF_IMGBED_UPLOAD_URL
andCF_IMGBED_API_KEY
to be configured), otherwisenull
.
-
-
Parameters:
-
get_sd_models
: Get list of available Stable Diffusion checkpoint models.- No parameters required.
- Returns a JSON string array of model names (titles).
-
set_sd_model
: Set the active Stable Diffusion checkpoint model.-
Parameters:
-
model_name
(required): Name of the model to set as active.
-
-
Parameters:
-
get_sd_upscalers
: Get list of available upscaler models.- No parameters required.
- Returns a JSON string array of upscaler names.
-
upscale_images
: Upscale one or more images using Stable Diffusion's "Extras" feature.-
Parameters:
-
images
(required): Array of local image file paths to upscale. -
resize_mode
: Upscaling mode (0 for multiplier, 1 for dimensions). Defaults toSD_RESIZE_MODE
. -
upscaling_resize
: Upscale multiplier whenresize_mode=0
. Defaults toSD_UPSCALE_MULTIPLIER
. -
upscaling_resize_w
: Target width in pixels whenresize_mode=1
. Defaults toSD_UPSCALE_WIDTH
. -
upscaling_resize_h
: Target height in pixels whenresize_mode=1
. Defaults toSD_UPSCALE_HEIGHT
. -
upscaler_1
: Primary upscaler model name. Defaults toSD_UPSCALER_1
. -
upscaler_2
: Secondary upscaler model name. Defaults toSD_UPSCALER_2
. -
output_path
: Custom local output directory for upscaled images, overridesSD_OUTPUT_DIR
.
-
-
Returns: A JSON string representing an array of result objects, each containing the
path
to the upscaled image file.
-
Parameters:
For development with auto-rebuild on file changes:
npm run watch
This will compile TypeScript files and restart the server when changes are detected in the src
directory.
Common issues and solutions:
-
Connection Errors:
- Ensure your Stable Diffusion WebUI is running and accessible from where the MCP server is running.
- Verify the WebUI was started with the
--api
flag. - Check if the
SD_WEBUI_URL
is correct. - If using authentication (
SD_AUTH_USER
,SD_AUTH_PASS
), ensure credentials are correct.
-
File System Errors:
- Verify the output directory (
SD_OUTPUT_DIR
oroutput_path
parameter) exists and the server process has write permissions. - When upscaling, ensure the input image files specified in the
images
array exist and are readable by the server process.
- Verify the output directory (
-
Timeout Errors:
- Image generation can take time. If you encounter timeouts, consider increasing the
REQUEST_TIMEOUT
value (in milliseconds).
- Image generation can take time. If you encounter timeouts, consider increasing the
-
ImgBed Upload Errors:
- Ensure
CF_IMGBED_UPLOAD_URL
andCF_IMGBED_API_KEY
are correctly set if you expect uploads to work. - Check the ImgBed server logs for potential issues on its end.
- Check the
sd-image-gen-mcp
server logs for detailed error messages related to the upload attempt.
- Ensure
This MCP server is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository.