| """ |
| 图片缩放插件 MCP工具定义 |
| """ |
|
|
| from app.mcp.decorators import mcp_tool |
| from pydantic import BaseModel, Field |
|
|
| from .core import do_resize |
|
|
|
|
| |
| |
| |
|
|
| class ResizeInput(BaseModel): |
| """图片缩放工具输入参数""" |
| file_base64: str = Field( |
| description="图片文件的Base64编码", |
| examples=["iVBORw0KGgoAAAANSUhEUgAA..."] |
| ) |
| width: int = Field( |
| ge=16, le=2048, |
| description="目标宽度(像素),范围16-2048" |
| ) |
| height: int = Field( |
| ge=16, le=2048, |
| description="目标高度(像素),范围16-2048" |
| ) |
| format: str = Field( |
| default="PNG", |
| description="输出格式:PNG、JPEG、WEBP" |
| ) |
|
|
|
|
| class ResizeOutput(BaseModel): |
| """图片缩放工具输出结果""" |
| success: bool = Field(description="操作是否成功") |
| output_base64: str | None = Field(description="缩放后图片的Base64编码") |
| output_url: str | None = Field(default=None, description="临时下载链接(可选)") |
| message: str = Field(description="结果说明") |
|
|
|
|
| |
| |
| |
|
|
| @mcp_tool( |
| name="image-resize", |
| title="图片缩放", |
| description="缩放图片到指定尺寸,支持PNG、JPEG、WEBP格式输出", |
| annotations={ |
| "readOnlyHint": False, |
| "destructiveHint": False, |
| } |
| ) |
| async def resize_image(params: ResizeInput) -> ResizeOutput: |
| """将图片缩放到指定的宽度和高度。 |
| |
| 支持PNG、JPEG、WEBP格式输出。 |
| 输入图片尺寸必须大于目标尺寸。 |
| |
| Args: |
| params: 包含Base64图片数据、目标宽高、输出格式 |
| |
| Returns: |
| ResizeOutput: 包含缩放后的图片数据或错误信息 |
| """ |
| result = await do_resize(params.file_base64, params.width, params.height, params.format) |
| return ResizeOutput(**result) |