Skip to main content
Glama

sd-api-mcp

一个将 Stable Diffusion REST API 暴露给 AI 智能体的 MCP(Model Context Protocol)服务器。支持 SD1.5、SDXL 和 Illustrious XL 流水线,用于文生图、修复(inpainting)、模型管理和模型合并。

它基于 SD API Backend 项目构建。

使用 MCP Python SDK 构建,并通过 uv 进行管理。

环境要求

  • Python >= 3.11

  • uv 包管理器

  • 一个正在运行的 Stable Diffusion API 实例(默认:http://localhost:8000)

Related MCP server: invokeai-mcp

安装

git clone <repo-url>
cd sd-api-mcp
uv sync

用法

stdio(默认)

适用于管理服务器进程的 MCP 客户端(例如 Claude Code、Claude Desktop):

uv run sd-api-mcp

SSE

uv run sd-api-mcp --transport sse --host 0.0.0.0 --port 8080

Streamable HTTP(推荐用于网络部署)

uv run sd-api-mcp --transport streamable-http --host 0.0.0.0 --port 8080

MCP 端点将位于 http://<host>:<port>/mcp。

Claude Desktop 配置

添加到你的 claude_desktop_config.json:

{
  "mcpServers": {
    "stable-diffusion": {
      "command": "uv",
      "args": ["run", "--project", "/path/to/sd-api-mcp", "sd-api-mcp"],
      "env": {
        "SD_API_URL": "http://localhost:8000"
      }
    }
  }
}

Claude Code 配置

claude mcp add stable-diffusion -- uv run --project /path/to/sd-api-mcp sd-api-mcp

OpenCode 配置

添加到你的 opencode.json:

{
  "mcp": {
    "stable-diffusion": {
      "type": "stdio",
      "command": "uv",
      "args": ["run", "--project", "/path/to/sd-api-mcp", "sd-api-mcp"],
      "env": {
        "SD_API_URL": "http://localhost:8000"
      }
    }
  }
}

对于 SSE 或 Streamable HTTP 传输方式,请单独启动服务器,并使用远程 URL:

{
  "mcp": {
    "stable-diffusion": {
      "type": "sse",
      "url": "http://localhost:8080/sse"
    }
  }
}

环境变量

变量

默认值

描述

SD_API_URL

http://localhost:8000

Stable Diffusion API 的基础 URL

SD_POLL_INTERVAL

2.0

作业状态轮询间隔(秒)

SD_POLL_TIMEOUT

600.0

等待作业完成的最大秒数

MCP_TRANSPORT

stdio

传输协议(stdio、sse、streamable-http)

MCP_HOST

127.0.0.1

SSE/HTTP 传输的绑定地址

MCP_PORT

8080

SSE/HTTP 传输的端口

可用工具

图像生成

工具

描述

generate_image

生成图像并等待结果

inpaint_image

修复遮罩区域并等待结果

submit_generate

提交生成作业,立即返回作业 ID

submit_inpaint

提交修复作业,立即返回作业 ID

batch_generate

一次提交最多 10 个生成请求

compare_models

使用相同提示词,用 2-6 个模型生成图像进行比较

所有生成工具都接受 pipeline 参数:"sd15"、"sdxl" 或 "illustrious"。

模型管理

工具

描述

list_models

列出可用的 checkpoint、LoRA 或 VAE

get_model_metadata

从模型的 safetensors 头部读取元数据

作业管理

工具

描述

list_jobs

列出所有作业及其状态和进度

get_job_status

获取指定作业的状态和结果

cancel_job

取消待处理或正在运行的作业

系统

工具

描述

health_check

检查 SD API 是否可访问

system_info

获取 GPU、缓存和队列统计信息

list_schedulers

列出可用的噪声调度器

get_app_settings

获取当前配置参数

模型合并

工具

描述

merge_models

合并两个 checkpoint(linear、slerp、additive、subtract)

batch_merge_models

将基础模型与多个目标模型合并

recipe_merge

执行多步骤合并配方

示例

生成图像(智能体视角)

AI 智能体会使用以下参数调用 generate_image 工具:

{
  "pipeline": "sdxl",
  "positive_prompt": "a cat sitting on a windowsill, golden hour lighting, photorealistic",
  "negative_prompt": "blurry, low quality",
  "model_checkpoint": "dreamshaperXL_v2.safetensors",
  "width": 1024,
  "height": 1024,
  "steps": 30,
  "cfg_scale": 7.0,
  "seed": -1,
  "scheduler": "DPM++ 2M"
}

该工具将作业提交到 SD API,轮询直至完成,并返回包含生成图像的完整结果。

列出可用模型

{
  "model_type": "sdxl",
  "resource_type": "checkpoints"
}

合并两个模型

{
  "model_type": "sd15",
  "base_model": "v1-5-pruned.safetensors",
  "target_model": "dreamshaper_8.safetensors",
  "output_name": "merged_model.safetensors",
  "method": "slerp",
  "alpha": 0.5
}

项目结构

src/sd_api_mcp/
    __init__.py   # CLI entry point with transport selection
    server.py     # MCPServer instance and tool definitions
    client.py     # Async HTTP client for the SD API

许可证

本项目采用 GNU General Public License v3.0 授权。详情请参阅 LICENSE。

Available Tools

18 tools
batch_generateA

Submit a batch of image generation requests (up to 10).

Args:
    pipeline: Pipeline to use — "sd15", "sdxl", or "illustrious"
    requests: List of generation request objects, each with at least "positive_prompt" and "model_checkpoint"

Inputs: pipeline (required), requests (required). Returns: result

batch_merge_modelsA

Merge a base model with multiple target models in batch. Waits for completion.

Args:
    model_type: Model architecture — "sd15" or "sdxl"
    base_model: Base model checkpoint filename
    target_models: List of target model checkpoint filenames
    method: Merge method — "linear", "slerp", "additive", or "subtract"
    alpha: Merge strength (0.0-1.0)
    output_subdir: Output subdirectory name for merged models
    preserve_metadata: Whether to preserve model metadata

Inputs: base_model (required), model_type (required), target_models (required), alpha, +3 more. Returns: result

cancel_jobA

Cancel a pending or running job.

Args:
    job_id: The job identifier to cancel

Inputs: job_id (required). Returns: result

compare_modelsA

Generate images from multiple models using the same prompt for comparison.

Args:
    pipeline: Pipeline to use — "sd15", "sdxl", or "illustrious"
    positive_prompt: Text prompt for all models
    models: List of model checkpoint filenames to compare (2-6)
    negative_prompt: Negative prompt (things to avoid)
    width: Image width in pixels (256-2048)
    height: Image height in pixels (256-2048)
    steps: Number of inference steps (1-150)
    cfg_scale: Classifier-free guidance scale (1-30)
    seed: Fixed seed for fair comparison (default 42)
    scheduler: Noise scheduler name

Inputs: models (required), pipeline (required), positive_prompt (required), seed, +6 more. Returns: result

generate_imageA

Generate an image using Stable Diffusion. Waits for completion and returns the result.

Args:
    pipeline: Pipeline to use — "sd15", "sdxl", or "illustrious"
    positive_prompt: Text prompt describing the desired image
    model_checkpoint: Model checkpoint filename to use
    negative_prompt: Negative prompt (things to avoid)
    width: Image width in pixels (256-2048)
    height: Image height in pixels (256-2048)
    steps: Number of inference steps (1-150)
    cfg_scale: Classifier-free guidance scale (1-30)
    seed: Random seed (-1 for random)
    scheduler: Noise scheduler name
    loras: Optional list of LoRA configs, each with "name" (str) and optional "strength" (float 0-1)
    custom_vae: Optional custom VAE checkpoint filename

Inputs: pipeline (required), positive_prompt (required), model_checkpoint (required), seed, +8 more. Returns: result

get_app_settingsA

Get the current application configuration parameters.

Inputs: no named parameters. Returns: result

get_job_statusB

Get the current status and result of a specific job.

Args:
    job_id: The job identifier returned when the job was submitted

Inputs: job_id (required). Returns: result

get_model_metadataA

Get metadata for a specific model from its safetensors header.

Args:
    model_type: Model architecture — "sd15" or "sdxl"
    resource_type: Resource category — "checkpoints", "loras", or "vaes"
    model_name: Model filename

Inputs: model_name (required), model_type (required), resource_type (required). Returns: result

health_checkA

Check if the Stable Diffusion API is healthy and reachable.

Inputs: no named parameters. Returns: result

inpaint_imageA

Inpaint an image using Stable Diffusion. Waits for completion and returns the result.

Args:
    pipeline: Pipeline to use — "sd15", "sdxl", or "illustrious"
    positive_prompt: Text prompt describing what to paint in the masked region
    model_checkpoint: Model checkpoint filename to use
    image_base64: Base64-encoded input image
    mask_base64: Base64-encoded mask image (white = inpaint region)
    negative_prompt: Negative prompt (things to avoid)
    steps: Number of inference steps (1-150)
    cfg_scale: Classifier-free guidance scale (1-30)
    seed: Random seed (-1 for random)
    scheduler: Noise scheduler name
    loras: Optional list of LoRA configs, each with "name" (str) and optional "strength" (float 0-1)
    custom_vae: Optional custom VAE checkpoint filename

Inputs: pipeline (required), mask_base64 (required), image_base64 (required), positive_prompt (required), +8 more. Returns: result

list_jobsA

List all jobs with their current status, progress, and timing information.

Inputs: no named parameters. Returns: result

list_modelsB

List available models.

Args:
    model_type: Model architecture — "sd15" or "sdxl"
    resource_type: Resource category — "checkpoints", "loras", or "vaes"

Inputs: model_type (required), resource_type (required). Returns: result

list_schedulersA

List all available noise schedulers that can be used for image generation.

Inputs: no named parameters. Returns: result

merge_modelsA

Merge two model checkpoints. Submits the job and waits for completion.

Args:
    model_type: Model architecture — "sd15" or "sdxl"
    base_model: Base model checkpoint filename
    target_model: Target model checkpoint filename
    output_name: Output checkpoint filename for the merged model
    method: Merge method — "linear", "slerp", "additive", or "subtract"
    alpha: Merge strength (0.0-1.0)
    preserve_metadata: Whether to preserve model metadata

Inputs: base_model (required), model_type (required), output_name (required), target_model (required), +3 more. Returns: result

recipe_mergeB

Execute a multi-step merge recipe. Each step merges the current result with a new target. Waits for completion.

Args:
    model_type: Model architecture — "sd15" or "sdxl"
    base_model: Base model checkpoint filename
    steps: List of merge steps, each with "target_model" (str), "method" (str), and "alpha" (float 0-1)
    output_name: Output checkpoint filename for the final merged model

Inputs: steps (required), base_model (required), model_type (required), output_name (required). Returns: result

submit_generateA

Submit an image generation job without waiting. Returns the job ID for manual polling.

Args:
    pipeline: Pipeline to use — "sd15", "sdxl", or "illustrious"
    positive_prompt: Text prompt describing the desired image
    model_checkpoint: Model checkpoint filename to use
    negative_prompt: Negative prompt (things to avoid)
    width: Image width in pixels (256-2048)
    height: Image height in pixels (256-2048)
    steps: Number of inference steps (1-150)
    cfg_scale: Classifier-free guidance scale (1-30)
    seed: Random seed (-1 for random)
    scheduler: Noise scheduler name
    loras: Optional list of LoRA configs, each with "name" (str) and optional "strength" (float 0-1)
    custom_vae: Optional custom VAE checkpoint filename

Inputs: pipeline (required), positive_prompt (required), model_checkpoint (required), seed, +8 more. Returns: result

submit_inpaintA

Submit an inpainting job without waiting. Returns the job ID for manual polling.

Args:
    pipeline: Pipeline to use — "sd15", "sdxl", or "illustrious"
    positive_prompt: Text prompt describing what to paint in the masked region
    model_checkpoint: Model checkpoint filename to use
    image_base64: Base64-encoded input image
    mask_base64: Base64-encoded mask image (white = inpaint region)
    negative_prompt: Negative prompt (things to avoid)
    steps: Number of inference steps (1-150)
    cfg_scale: Classifier-free guidance scale (1-30)
    seed: Random seed (-1 for random)
    scheduler: Noise scheduler name
    loras: Optional list of LoRA configs, each with "name" (str) and optional "strength" (float 0-1)
    custom_vae: Optional custom VAE checkpoint filename

Inputs: pipeline (required), mask_base64 (required), image_base64 (required), positive_prompt (required), +8 more. Returns: result

system_infoA

Get system information including GPU device, dtype, model cache stats, and job queue stats.

Inputs: no named parameters. Returns: result

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A3.7/5.0

    Scored across 18 tools

    Disambiguation4/5

    Most tools target distinct operations (sync generation, async submission, batch, compare, merge variants), and descriptions clarify the differences. A few near pairs such as generate_image/submit_generate and batch_merge_models/recipe_merge could cause misselection, but the descriptions separate them well.

    Naming Consistency3/5

    The dominant verb_noun pattern (list_models, get_job_status, merge_models) is readable, but there are notable deviations like health_check, system_info, recipe_merge, and submit_generate. The mix of verb-first and noun-first names is inconsistent enough to slow an agent down.

    Tool Count4/5

    Eighteen tools is slightly above the ideal 3-15 range, but the breadth is justified by covering generation, async jobs, model metadata, system health, and model merging. It feels like a complete API surface rather than an inflated one.

    Completeness4/5

    The surface covers generation, inpainting, async job lifecycle, model listing/metadata, schedulers, and multiple merge workflows. Obvious gaps are minor, such as the lack of a general image-to-image tool or batch async variants, but core workflows have no dead ends.

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      A
      quality
      B
      maintenance
      Enables AI coding agents to control a local InvokeAI creative engine, supporting text-to-image, image-to-image, masked inpaint, upscaling, and full queue, model, gallery, board, and workflow management.
      13
      2
      MIT
    • A
      license
      A
      quality
      B
      maintenance
      Enables AI agents to generate images on a local Stable Diffusion Forge Neo instance, automatically inferring prompt style and sampling parameters from the user's setup and past generations, and providing tools for LoRA search, model management, and module checks.
      10
      2
      MIT