Skip to main content
Glama
liuyazui

Base64 MCP Server

base64_decode_image

Decode Base64-encoded strings into image files by specifying output path and MIME type to convert encoded data into usable images.

Instructions

将Base64编码解码为图片

Args:
    encoded: Base64编码的字符串
    output_path: 输出图片的路径
    mime_type: 图片的MIME类型 (默认为image/png)

Returns:
    解码结果

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
encodedYes
output_pathYes
mime_typeNoimage/png

Implementation Reference

  • The handler function for the 'base64_decode_image' tool. It decodes Base64-encoded image data (supporting data: URLs), creates output directory if needed, and saves the image file.
    @mcp.tool()
    def base64_decode_image(
        encoded: str, output_path: str, mime_type: str = "image/png"
    ) -> str:
        """将Base64编码解码为图片
    
        Args:
            encoded: Base64编码的字符串
            output_path: 输出图片的路径
            mime_type: 图片的MIME类型 (默认为image/png)
    
        Returns:
            解码结果
        """
        try:
            # 清理输入,移除可能的前缀和空白
            encoded = encoded.strip()
    
            # 如果输入是Data URL格式,提取实际的Base64部分
            if encoded.startswith("data:"):
                # 分离MIME类型和Base64内容
                header, encoded = encoded.split(",", 1)
                if ";base64" not in header:
                    return "错误: 输入不是有效的Base64编码的Data URL"
                # 提取MIME类型但不使用,因为我们使用参数中的mime_type
    
            # 解码Base64
            image_data = base64.b64decode(encoded)
    
            # 确保输出目录存在
            output_dir = os.path.dirname(output_path)
            if output_dir and not os.path.exists(output_dir):
                os.makedirs(output_dir)
    
            # 保存图片
            with open(output_path, "wb") as image_file:
                image_file.write(image_data)
    
            return f"图片已成功解码并保存到 {output_path}"
        except Exception as e:
            return f"图片解码失败: {str(e)}"

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/liuyazui/base64_server'

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