From cba15a62829a0fe57a03c8031f981e8f790e2efc Mon Sep 17 00:00:00 2001 From: tanaka takashi Date: Sat, 12 Sep 2026 18:04:56 +0900 Subject: feat: Stable Diffusion 1.x MCPサーバを実装 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 仕様書・設計書に基づき、ローカルCUDA GPU上のStable Diffusion 1.x互換モデルをMCP経由で利用するサーバを新規実装。 実装内容: - generate_image、generate_guided_image、inpaint_imageの3ツールをMCP SDK v2で公開 - TOMLからcheckpoint、VAE、複数LoRAと重み、ControlNet、生成設定を読み込み - モデル種別ごとの固定ディレクトリ解決と起動時の設定・ファイル存在検証を追加 - Depth/MiDaS、OpenPose body/full、ControlNet Inpaintの前処理とパイプライン切替を実装 - FP16/CUDA、1枚固定生成、ControlNetオンデマンドロード、単一GPU直列実行を実装 - Base64 PNG/JPEG入力の形式・サイズ検証と任意のdata URI入力に対応 - seed、サイズ、モード、時刻、処理時間をPNG metadataへ記録 - 任意のローカルPNG保存、生成エラー分類、CUDA OOM後のキャッシュ回復を追加 - --configと--versionを備えたstdio CLIエントリポイントを追加 開発・運用: - Python 3.11およびuvによる依存・lock管理を構成 - config.example.tomlとセットアップ・運用手順をREADMEへ追加 - Ruff、Pyright、pytestの設定を追加 - 設定、パス解決、schema、画像検証、サービスルーティング、排他制御、MCP画像返却をテスト 検証結果: - pytest: 30 passed - Ruff lint/format: passed - Pyright: 0 errors, 0 warnings - CLI versionおよびMCPクライアント経由のPNG返却を確認 - 実GPU smoke testはCUDA GPUとモデルファイルがないため対象外 --- src/mcp_sd15/preprocessors/depth.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/mcp_sd15/preprocessors/depth.py (limited to 'src/mcp_sd15/preprocessors/depth.py') diff --git a/src/mcp_sd15/preprocessors/depth.py b/src/mcp_sd15/preprocessors/depth.py new file mode 100644 index 0000000..861b889 --- /dev/null +++ b/src/mcp_sd15/preprocessors/depth.py @@ -0,0 +1,24 @@ +"""MiDaS depth-map preprocessing.""" + +from __future__ import annotations + +from typing import cast + +from PIL import Image + + +class DepthPreprocessor: + """Lazily loads the fixed MiDaS annotator from the local cache.""" + + def __init__(self) -> None: + self._detector: object | None = None + + def __call__(self, image: Image.Image) -> Image.Image: + if self._detector is None: + from controlnet_aux import MidasDetector + + self._detector = MidasDetector.from_pretrained( + "lllyasviel/Annotators", local_files_only=True + ) + result = cast(Image.Image, self._detector(image)) # type: ignore[operator] + return result.convert("RGB") -- cgit v1.2.3