Skip to main content
Glama
duke0317

Image Processing MCP Server

by duke0317

convert_format

Convert images between formats like PNG, JPEG, WEBP, BMP, TIFF, and GIF. Specify image source and target format to process images for compatibility or optimization needs.

Instructions

转换图片格式

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
image_sourceYes图片源,可以是文件路径或base64编码的图片数据
target_formatYes目标格式:PNG、JPEG、WEBP、BMP、TIFF、GIF 等
qualityNo图片质量,范围 1-100,仅对 JPEG 格式有效

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • Core handler function implementing the image format conversion logic using ImageProcessor, including validation, processing, and JSON response.
    async def convert_format(image_source: str, target_format: str, quality: int = 95) -> list[TextContent]:
        """
        转换图片格式
        
        Args:
            image_source: 图片源(文件路径或base64编码数据)
            target_format: 目标格式
            quality: 图片质量
            
        Returns:
            转换后的图片文件引用
        """
        try:
            # 验证参数
            if not image_source or not target_format:
                raise ValidationError("图片源和目标格式不能为空")
            
            if not validate_image_format(target_format):
                raise ValidationError(f"不支持的目标格式: {target_format}")
            
            if not (1 <= quality <= 100):
                raise ValidationError("图片质量必须在1-100之间")
            
            # 加载图片
            image = processor.load_image(image_source)
            
            # 获取原始格式
            original_format = image.format or 'PNG'
            
            # 输出转换后的图片
            output_info = processor.output_image(image, f"convert_to_{target_format.lower()}", target_format, quality)
            
            result = {
                "success": True,
                "message": f"图片格式转换成功: {original_format} -> {target_format}",
                "data": {
                    **output_info,
                    "original_format": original_format,
                    "target_format": target_format,
                    "quality": quality
                }
            }
            
            return [TextContent(type="text", text=json.dumps(result, ensure_ascii=False))]
            
        except ValidationError as e:
            error_result = {
                "success": False,
                "error": f"参数验证失败: {str(e)}"
            }
            return [TextContent(type="text", text=json.dumps(error_result, ensure_ascii=False))]
            
        except Exception as e:
            error_result = {
                "success": False,
                "error": f"格式转换失败: {str(e)}"
            }
            return [TextContent(type="text", text=json.dumps(error_result, ensure_ascii=False))]
  • main.py:152-167 (registration)
    MCP tool registration using @mcp.tool() decorator, defining input parameters with descriptions and constraints, and delegating to the basic handler.
    @mcp.tool()
    def convert_format(
        image_source: Annotated[str, Field(description="图片源,可以是文件路径或base64编码的图片数据")],
        target_format: Annotated[str, Field(description="目标格式:PNG、JPEG、WEBP、BMP、TIFF、GIF 等")],
        quality: Annotated[int, Field(description="图片质量,范围 1-100,仅对 JPEG 格式有效", ge=1, le=100, default=95)]
    ) -> str:
        """转换图片格式"""
        try:
            result = safe_run_async(basic_convert_format(image_source, target_format, quality))
            return result[0].text
        except Exception as e:
            return json.dumps({
                "success": False,
                "error": f"转换图片格式失败: {str(e)}"
            }, ensure_ascii=False, indent=2)
  • Input schema definition for the convert_format tool within the Tool object returned by get_basic_tools(). Note: actual registration uses Annotated fields in main.py.
    Tool(
        name="convert_format",
        description="转换图片格式",
        inputSchema={
            "type": "object",
            "properties": {
                "image_data": {
                    "type": "string",
                    "description": "图片数据(base64编码)"
                },
                "target_format": {
                    "type": "string",
                    "description": "目标格式(PNG, JPEG, BMP, TIFF, WEBP)"
                },
                "quality": {
                    "type": "integer",
                    "description": "图片质量(1-100,仅对JPEG有效)",
                    "minimum": 1,
                    "maximum": 100,
                    "default": 95
                }
            },
            "required": ["image_data", "target_format"]
        }
    )
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries full burden. '转换图片格式' implies a mutation operation that changes image format, but it doesn't disclose behavioral traits like whether it preserves metadata, handles errors, or has performance implications. This is a significant gap for a tool with no annotation coverage.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single phrase '转换图片格式' (convert image format), which is extremely concise and front-loaded with the core purpose. There's zero wasted text, making it efficient for quick understanding.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given that an output schema exists, the description doesn't need to explain return values. However, with no annotations and a mutation tool (format conversion), the description is too minimal—it doesn't cover behavioral aspects like side effects or error handling, leaving gaps despite the good schema coverage.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all parameters (image_source, target_format, quality). The description adds no additional meaning beyond what's in the schema, such as explaining parameter interactions or constraints. Baseline 3 is appropriate when the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description '转换图片格式' (convert image format) clearly states the verb (convert) and resource (image format), making the purpose immediately understandable. However, it doesn't differentiate from sibling tools like 'convert_to_grayscale' or 'create_gif' which also involve format transformations, so it lacks sibling differentiation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. With many sibling tools for image processing (e.g., 'convert_to_grayscale', 'create_gif'), there's no indication of when format conversion is appropriate versus other transformations or creations.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/duke0317/ps-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server