File size: 2,170 Bytes
bdcdaf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc826a1
bdcdaf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
图片缩放插件 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)