Skip to main content
Glama
liuyazui

Base64 MCP Server

base64_encode_image

Convert image files to Base64 encoded strings for embedding in web applications, documents, or data transmission.

Instructions

将图片转换为Base64编码

Args:
    image_path: 图片文件路径

Returns:
    Base64编码结果

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
image_pathYes

Implementation Reference

  • The @mcp.tool() decorated function implementing the base64_encode_image tool. It reads the image file from the given path, encodes its binary content to base64, determines the MIME type using PIL, constructs a data URL, and returns previews of the encoded string and data URL.
    @mcp.tool()
    def base64_encode_image(image_path: str) -> str:
        """将图片转换为Base64编码
    
        Args:
            image_path: 图片文件路径
    
        Returns:
            Base64编码结果
        """
        try:
            if not os.path.exists(image_path):
                return f"错误: 文件 '{image_path}' 不存在"
    
            with open(image_path, "rb") as image_file:
                encoded = base64.b64encode(image_file.read()).decode("utf-8")
    
            # 获取MIME类型
            img = PILImage.open(image_path)
            mime_type = f"image/{img.format.lower()}"
    
            # 返回可在HTML中使用的Data URL格式
            data_url = f"data:{mime_type};base64,{encoded}"
            encoded_preview = f"图片Base64编码结果 (前100字符): {encoded[:100]}..."
            data_url_preview = f"完整Data URL (前100字符): {data_url[:100]}..."
            return f"{encoded_preview}\n\n{data_url_preview}"
        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