first commit

This commit is contained in:
lnkosadmin
2025-10-29 11:00:50 +08:00
committed by lnkosadmin
commit 533a37789a
5 changed files with 1979 additions and 0 deletions

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 lnkos
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

248
README.md Normal file
View File

@@ -0,0 +1,248 @@
# Local Stable Diffusion MCP
一个基于本地Stable Diffusion WebUI的MCPModel Context Protocol服务器提供AI图片生成功能支持透明背景图片生成。
## 🎨 功能特性
- **文本生成图片**:支持多种风格和参数设置
- **透明背景生成**:专门的透明背景图片生成功能
- **图生图功能**:基于现有图片生成新图片
- **多种采样器**Euler a、DPM++ 2M、DPM++ SDE等
- **预设风格模板**:动漫角色、写实肖像、幻想艺术、现代风格
- **智能提示词优化**:自动生成高质量提示词
## 🚀 快速开始
### 1. 环境要求
- Python 3.8+
- Stable Diffusion WebUI已安装并运行
- MCP客户端Claude Desktop、Cursor等
### 2. 安装依赖
```bash
pip install -r requirements.txt
```
### 3. 配置MCP服务器
#### 配置文件示例(`config.json`
```json
{
"sd_webui_url": "http://127.0.0.1:7860",
"models_path": "<YOUR_SD_MODELS_PATH>",
"default_model": "<YOUR_DEFAULT_MODEL>",
"output_path": "./output",
"max_retries": 3,
"timeout": 60
}
```
### 4. 启动MCP服务器
```bash
python server.py
```
## 🛠️ 在各种AI编辑器中配置MCP
### Trae&Trae CN
```
{
"mcpServers": {
"local_stable_diffusion_mcp": {
"command": "python",
"args": [
"<YOUR_PROJECT_PATH>\\server.py"
]
}
}
}
```
### Claude Desktop
1. 打开Claude Desktop设置
2. 找到MCP配置选项
3. 添加新的MCP服务器
```json
{
"mcpServers": {
"local-stable-diffusion": {
"command": "python",
"args": ["<YOUR_PROJECT_PATH>/server.py"],
"env": {
"PYTHONPATH": "<YOUR_PROJECT_PATH>"
}
}
}
}
```
### Cursor
1. 打开Cursor设置Settings
2. 搜索"MCP"或"Model Context Protocol"
3. 在MCP配置中添加
```json
{
"servers": {
"local-stable-diffusion": {
"command": "python",
"args": ["<YOUR_PROJECT_PATH>/server.py"],
"cwd": "<YOUR_PROJECT_PATH>"
}
}
}
```
### VS Code + Continue插件
1. 安装Continue插件
2. 打开`.continue/config.json`
3. 添加MCP服务器配置
```json
{
"models": [
{
"title": "Local Stable Diffusion MCP",
"provider": "mcp",
"server": {
"command": "python",
"args": ["server.py"],
"cwd": "<YOUR_PROJECT_PATH>"
}
}
]
}
```
### Windsurf
1. 打开Windsurf设置
2. 导航到MCP配置
3. 添加服务器配置:
```json
{
"mcpServers": {
"stable-diffusion": {
"command": "python",
"args": ["<YOUR_PROJECT_PATH>/server.py"],
"env": {
"MCP_CONFIG_PATH": "<YOUR_PROJECT_PATH>/config.json"
}
}
}
}
```
## 🎨 使用示例
### 生成透明背景图片
```python
# 生成透明背景的动漫角色
generate_transparent_image(
prompt="beautiful anime girl, long silver hair, detailed eyes, school uniform",
output_path="./transparent_character.png",
width=512,
height=768,
style="anime_character"
)
```
### 图生图功能
```python
# 基于现有图片生成新图片
generate_image_img2img(
input_image_path="./input.jpg",
prompt="beautiful anime style, detailed eyes, masterpiece",
output_path="./output.png",
denoising_strength=0.75
)
```
## ⚙️ 参数说明
### 基础参数
| 参数 | 说明 | 默认值 | 范围 |
|------|------|--------|------|
| `width` | 图片宽度 | 512 | 64-2048 |
| `height` | 图片高度 | 512 | 64-2048 |
| `steps` | 生成步数 | 20 | 1-150 |
| `cfg_scale` | CFG引导强度 | 7.5 | 1-30 |
| `sampler` | 采样器 | "Euler a" | 多种可选 |
### 风格模板
- `none`:无特定风格
- `anime_character`:动漫角色风格
- `realistic_portrait`:写实肖像风格
- `fantasy_art`:幻想艺术风格
- `modern_style`:现代风格
### 采样器推荐
- **快速生成**Euler a
- **平衡质量**DPM++ 2M
- **高质量**DPM++ SDE
## 💡 使用技巧
### 透明背景生成技巧
1. **使用专门的透明背景功能**:使用`generate_transparent_image`而不是普通生成功能
2. **选择合适的提示词**:包含"transparent background", "no background", "isolated object"
3. **避免背景相关词汇**:如"background", "scene", "landscape"
4. **使用适当的风格模板**:根据需求选择`anime_character``fantasy_art`
### 提示词优化
系统会自动添加以下优化:
- 质量增强:`best quality, amazing quality, very aesthetic, absurdres`
- 透明背景:`transparent background, alpha channel, no background, isolated object`
- 负面提示词:`background, white background, black background, colored background`
## 🔧 故障排除
### 常见问题
1. **连接失败**检查Stable Diffusion WebUI是否运行
2. **模型加载失败**:确认模型文件路径正确
3. **生成质量差**:调整`steps``cfg_scale`参数
4. **透明效果不佳**:使用专门的透明生成功能
### 调试模式
启动服务器时添加调试参数:
```bash
python server.py --debug
```
## 📄 文件结构
```
local_stable_diffusion_mcp/
├── config.json # 配置文件
├── requirements.txt # Python依赖
├── server.py # MCP服务器主文件
└── README.md # 本文件
```
## 🤝 贡献
此项目为测试使用项目不一定保证都能正常使用欢迎提交Issue和Pull Request来改进这个项目。
## 📄 许可证
MIT License

168
config.json Normal file
View File

@@ -0,0 +1,168 @@
{
"default_model": "sd1.5\\anything-v5.safetensors",
"default_vae": "None",
"base_url": "http://127.0.0.1:7860",
"endpoint": "/sdapi/v1/txt2img",
"img2img_endpoint": "/sdapi/v1/img2img",
"transparent_endpoint": "/sdapi/v1/img2img",
"timeout": 120,
"default_params": {
"steps": 20,
"cfg_scale": 7,
"sampler_index": "Euler a",
"width": 512,
"height": 768,
"n_iter": 1,
"batch_size": 1,
"denoising_strength": 0.75,
"resize_mode": 1,
"mask_blur": 4,
"inpainting_fill": 1,
"inpaint_full_res": true,
"inpaint_full_res_padding": 32
},
"prompt_suggestions": {
"available_samplers": [
"Euler a",
"Euler",
"LMS",
"Heun",
"DPM2",
"DPM2 a",
"DPM++ 2S a",
"DPM++ 2M",
"DPM++ SDE",
"DPM++ 2M Karras",
"DPM++ SDE Karras",
"DPM fast",
"DPM adaptive",
"DDIM",
"PLMS",
"UniPC",
"LCM"
],
"character_prompts": {
"anime_girl": "1girl, solo, school uniform, pink hair, long hair, smile, looking at viewer",
"anime_boy": "1boy, solo, school uniform, black hair, short hair, confident expression",
"fantasy_character": "fantasy, elf, pointed ears, long hair, elegant clothing, magical aura",
"modern_character": "modern, casual wear, natural lighting, detailed face, realistic proportions",
"beautiful_girl": "1girl, beautiful, detailed eyes, long hair, elegant, graceful pose, masterpiece",
"handsome_man": "1boy, handsome, sharp facial features, confident expression, detailed face, masterpiece",
"cute_loli": "1girl, loli, small body, cute, innocent expression, frilly dress, detailed hair",
"mature_woman": "1woman, mature, elegant, long hair, sophisticated clothing, detailed face, masterpiece",
"warrior": "warrior, armor, weapon, battle stance, detailed armor, epic lighting, fantasy",
"magical_girl": "magical girl, transformation outfit, wand, sparkles, cute pose, anime style, colorful",
"misaka_mikoto": "misaka mikoto, toaru majutsu no index, anime style, detailed eyes, masterpiece",
"kafuu_chino": "kafuu chino, gochuumon wa usagi desu ka, anime style, detailed eyes, masterpiece",
"anya": "anya forger, spy x family, anime style, detailed eyes, masterpiece",
"rem": "rem, re:zero, anime style, blue hair, detailed eyes, masterpiece",
"megumin": "megumin, kono subarashii sekai ni shukufuku wo, anime style, crimson demon, detailed eyes, masterpiece",
"hakurei_reimu": "hakurei reimu, touhou, shrine maiden, red white dress, detailed eyes, masterpiece"
},
"quality_enhancers": {
"masterpiece": "masterpiece, best quality, amazing quality, very aesthetic, absurdres",
"detailed": "extremely detailed, intricate details, high resolution",
"professional": "professional artwork, studio lighting, perfect composition",
"artistic": "artistic, beautiful, stunning visuals, creative composition",
"sharp_focus": "sharp focus, clear, crisp image, perfect focus",
"realistic_lighting": "realistic, reverse light, golden light, best quality, amazing quality, very aesthetic, absurdres",
"high_quality": "best quality, amazing quality, very aesthetic, absurdres, high resolution",
"ultra_detailed": "ultra detailed, extremely detailed, intricate details, masterpiece",
"professional_art": "professional artwork, masterpiece, best quality, studio lighting",
"premium_quality": "premium quality, top tier, masterpiece, best quality, amazing quality",
"lighting": "beautiful lighting, dynamic lighting, atmospheric lighting, rim lighting",
"composition": "perfect composition, dynamic angle, rule of thirds, depth of field",
"vibrant": "vibrant colors, vivid colors, rich colors, saturated colors, colorful"
},
"style_modifiers": {
"anime_style": "anime style, manga style, cel shading, vibrant colors",
"realistic_style": "photorealistic, hyperrealistic, lifelike, detailed skin texture",
"artistic_style": "digital art, concept art, illustration, painterly style",
"manga_style": "manga style, black and white, ink drawing, japanese comic",
"watercolor_style": "watercolor painting, soft colors, artistic, traditional media, flowing brushstrokes",
"oil_painting_style": "oil painting, classical art, rich textures, traditional art, fine art",
"sketch_style": "sketch, pencil drawing, line art, black and white, artistic drawing",
"pixel_art_style": "pixel art, retro style, 8-bit, 16-bit, pixelated, nostalgic",
"3d_render": "3d render, cgi, computer graphics, 3d modeling, realistic lighting",
"vintage_style": "vintage style, retro style, classic, old-fashioned, nostalgic, antique"
},
"negative_prompts": {
"general": "lowres, bad anatomy, worst quality, low quality, extra limbs, missing fingers, extra digits, fused fingers, too many fingers, long neck, mutation, deformed, ugly, blurry, bad proportions",
"anime": "bad anatomy, bad hands, missing fingers, extra digits, fewer digits, blurry, bad face, poorly drawn face",
"realistic": "cartoon, anime, illustration, painting, drawing, art, sketch, unrealistic proportions",
"transparent": "background, white background, black background, colored background",
"low_quality": "low quality, worst quality, jpeg artifacts, compression artifacts, pixelated, blurry, out of focus",
"distorted": "distorted, deformed, twisted, warped, malformed, disfigured, grotesque",
"anatomical_errors": "bad anatomy, bad hands, bad feet, bad proportions, wrong anatomy, anatomical errors",
"text_and_watermarks": "text, watermark, signature, username, logo, copyright, subtitles, ui, interface",
"heavy_negative": "nsfw, lowres, {bad}, error, fewer, extra, missing, worst quality, jpeg artifacts, bad quality, watermark, unfinished, displeasing, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract]",
"light_negative": "nsfw, lowres, jpeg artifacts, worst quality, watermark, blurry, very displeasing",
"character_priority": "nsfw, lowres, {bad}, error, fewer, extra, missing, worst quality, jpeg artifacts, bad quality, watermark, unfinished, displeasing, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract], bad anatomy, bad hands, @_@, mismatched pupils, heart-shaped pupils, glowing eyes"
},
"transparent_background": {
"layerdiffuse_script": "LayerDiffuse",
"layerdiffuse_method": "Foreground/Background",
"layerdiffuse_fidelity": 0.5
},
"scene_backgrounds": {
"nature": "beautiful sky, clouds, natural lighting, outdoor, scenic, landscape",
"city": "urban environment, city background, modern architecture, street view, cityscape",
"indoor": "indoor setting, room interior, cozy atmosphere, home environment, indoor lighting",
"fantasy": "magical background, fantasy landscape, ethereal atmosphere, mystical environment",
"abstract": "abstract background, artistic background, minimalist, simple background, gradient background",
"japanese_garden": "japanese garden, cherry blossoms, traditional architecture, zen garden",
"chinese_temple": "chinese temple, traditional architecture, red pillars, incense",
"european_castle": "european castle, medieval architecture, stone walls, towers",
"modern_city": "modern city, skyscrapers, urban landscape, contemporary architecture",
"nature_forest": "nature forest, trees, foliage, woodland, natural environment",
"underwater": "underwater, ocean floor, coral reef, aquatic life, submarine",
"mountain_landscape": "mountain landscape, peaks, valleys, alpine scenery, rocky terrain",
"desert_oasis": "desert oasis, sand dunes, palm trees, water source, arid landscape",
"arctic_ice": "arctic ice, snow, icebergs, frozen landscape, polar environment",
"tropical_beach": "tropical beach, palm trees, white sand, crystal water, paradise"
},
"poses_expressions": {
"standing_pose": "standing, full body, confident pose, dynamic pose, elegant stance",
"sitting_pose": "sitting, relaxed pose, comfortable position, casual sitting, elegant sitting",
"action_pose": "action pose, dynamic pose, movement, energetic pose, fighting stance",
"cute_expression": "cute expression, happy smile, cheerful, adorable, kawaii",
"serious_expression": "serious expression, determined look, confident, intense gaze, focused",
"gentle_expression": "gentle smile, soft expression, kind eyes, peaceful, calm"
},
"clothing_accessories": {
"school_uniform": "school uniform, sailor uniform, pleated skirt, student outfit, academic dress",
"casual_wear": "casual clothing, everyday outfit, comfortable clothes, modern fashion, street wear",
"formal_wear": "formal attire, elegant dress, suit and tie, sophisticated clothing, dress clothes",
"fantasy_costume": "fantasy costume, magical outfit, medieval clothing, fantasy armor, mystical garments",
"accessories": "jewelry, necklace, earrings, bracelet, accessories, detailed accessories, fashionable accessories",
"traditional_chinese": "traditional chinese clothing, hanfu, qipao, chinese dress, oriental style",
"traditional_japanese": "traditional japanese clothing, kimono, yukata, japanese dress, oriental style",
"modern_fashion": "modern fashion, trendy clothing, contemporary style, fashionable outfit, chic",
"sportswear": "sportswear, athletic clothing, gym outfit, activewear, sports uniform",
"fantasy_clothing": "fantasy clothing, magical outfit, enchanted dress, mystical attire, ethereal clothing",
"military_uniform": "military uniform, army outfit, military attire, combat uniform, soldier clothing",
"gothic_lolita": "gothic lolita, lolita fashion, gothic dress, victorian style, elegant gothic",
"swimwear": "swimwear, swimsuit, beach outfit, bikini, summer clothing",
"winter_clothing": "winter clothing, coat, jacket, warm outfit, cold weather attire",
"cosplay_costume": "cosplay costume, character outfit, anime cosplay, game costume, themed clothing"
},
"environment_tags": {
"natural_environments": "forest, mountains, ocean, sky, clouds, sunset, sunrise, landscape, nature",
"urban_environments": "city, street, buildings, modern architecture, urban landscape, downtown, cityscape",
"fantasy_environments": "magical realm, floating islands, crystal caves, enchanted forest, fantasy landscape",
"indoor_environments": "room, interior, furniture, cozy atmosphere, indoor lighting, bedroom, kitchen, living room",
"architectural_styles": "chinese architecture, japanese architecture, european architecture, gothic architecture, modern architecture",
"gardens_parks": "japanese garden, chinese garden, park, greenhouse, botanical garden, zen garden, cherry blossoms",
"transportation": "street, road, highway, bridge, railway, train station, airport, harbor, port",
"buildings_structures": "castle, temple, shrine, palace, tower, skyscraper, house, mansion, church, cathedral",
"weather_atmosphere": "rain, snow, sunny, cloudy, fog, mist, storm, night, day, evening, morning",
"special_locations": "school, classroom, office, hospital, library, museum, restaurant, hotel, amusement park"
},
"technical_parameters": {
"sampling_methods": "Euler a, Euler, DPM++ 2M, DPM++ SDE, DPM++ 2S a, DDIM, DPM2, Heun, LMS, PLMS, UniPC, DPM fast, DPM adaptive",
"noise_schedules": "native, karras, exponential, polyexponential, sgm uniform",
"special_features": "smea, dyn, variety, decrisp, hires fix, face restoration, 4k, 8k, ultra highres",
"image_settings": "portrait, landscape, square, high resolution, detailed, masterpiece, best quality, absurdres"
}
}
}

4
requirements.txt Normal file
View File

@@ -0,0 +1,4 @@
aiohttp>=3.8.0
pillow>=9.0.0
numpy>=1.21.0
mcp>=0.1.0

1538
server.py Normal file

File diff suppressed because it is too large Load Diff