feat: 图片改图 MCP 工具 image_edit
- imagegen.edit:POST multipart /v1/images/edits,输入图 必须是工作区内 plain basename 且存在(上限 20MB),产出 必须是新文件、绝不覆盖输入;响应解析与 generate 共用 _save_image_response(url 或 b64_json、下载落盘、格式探测)。 - imagegen_mcp 新增 image_edit 工具;image-workflow 技能 改图分支从“无专用工具”改为用 image_edit。 - 测试:multipart 体含提示词与原图字节、输入缺失/同名/ 路径逃逸拒绝、MCP 列表含 image_edit;全量相关 16 项过。 实测依据(2026-10-30):中转站图片分组 key 调 gpt-image-2.5-flare edits 返回 200,URL 图为真实 PNG 1024x1024、909K,usage 含输入图 token(1659 image tokens)。
This commit is contained in:
1 parent
5b03e0a85d
commit
e13a95067c
4 files changed
+236
-30
No files matched your search
+48
-4
@@ -20,10 +20,10 @@ import os
|
||||
import sys
|
||||
|
||||
try:
|
||||
from cloud.imagegen import ImageGenError, generate
|
||||
from cloud.imagegen import ImageGenError, edit, generate
|
||||
except ImportError: # flat copy in a tenant codex-home
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
from imagegen import ImageGenError, generate # noqa: E402
|
||||
from imagegen import ImageGenError, edit, generate # noqa: E402
|
||||
|
||||
SERVER_NAME = "imagegen"
|
||||
SERVER_VERSION = "0.1.0"
|
||||
@@ -61,6 +61,42 @@ _TOOLS = [
|
||||
"required": ["prompt"],
|
||||
},
|
||||
},
|
||||
{
|
||||
"name": "image_edit",
|
||||
"description": (
|
||||
"Edit an existing image in the current working directory "
|
||||
"from a text prompt and save the result as a NEW file; "
|
||||
"the input file is never overwritten. Returns the "
|
||||
"absolute output path, format, dimensions when "
|
||||
"detectable, byte size and the relay usage block. "
|
||||
"Verify the returned path with shell/PIL before "
|
||||
"reporting success."
|
||||
),
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"prompt": {"type": "string",
|
||||
"description": "What to change, in detail"},
|
||||
"input_filename": {"type": "string",
|
||||
"description": "Plain basename "
|
||||
"of the existing image in the "
|
||||
"working directory"},
|
||||
"output_filename": {"type": "string",
|
||||
"description": "Plain basename "
|
||||
"to save the edited image as. "
|
||||
"Must differ from input_filename. "
|
||||
"Default: generated-<epoch>.png"},
|
||||
"size": {"type": "string", "default": "1024x1024",
|
||||
"description": "1024x1024, 1536x1024, "
|
||||
"1024x1536, or auto"},
|
||||
"model": {"type": "string",
|
||||
"description": "Image model override; "
|
||||
"default is the IMAGE_GEN_MODEL env "
|
||||
"or gpt-image-2.5-flare"},
|
||||
},
|
||||
"required": ["prompt", "input_filename"],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@@ -68,11 +104,12 @@ def _err(code, message):
|
||||
return {"code": code, "message": message}
|
||||
|
||||
|
||||
def handle_request(request, generate_fn=None):
|
||||
def handle_request(request, generate_fn=None, edit_fn=None):
|
||||
"""Handle one JSON-RPC request dict. Returns a response dict or
|
||||
None for notifications (no id). Pure function for testability:
|
||||
the stdio loop passes the real generate, tests pass fakes."""
|
||||
the stdio loop passes the real generate/edit, tests pass fakes."""
|
||||
generate_fn = generate_fn or generate
|
||||
edit_fn = edit_fn or edit
|
||||
if not isinstance(request, dict):
|
||||
return {"jsonrpc": "2.0", "id": None,
|
||||
"error": _err(-32700, "request is not an object")}
|
||||
@@ -114,6 +151,13 @@ def handle_request(request, generate_fn=None):
|
||||
output_filename=args.get("output_filename"),
|
||||
size=args.get("size", "1024x1024"),
|
||||
model=args.get("model"))
|
||||
elif name == "image_edit":
|
||||
result = edit_fn(
|
||||
args.get("prompt"),
|
||||
args.get("input_filename"),
|
||||
output_filename=args.get("output_filename"),
|
||||
size=args.get("size", "1024x1024"),
|
||||
model=args.get("model"))
|
||||
else:
|
||||
return {"jsonrpc": "2.0", "id": req_id,
|
||||
"error": _err(-32602, "unknown tool: %r" % name)}
|
||||
|
||||
Reference in new issue
Block a user