Skip to main content
Glama
yonaka15
by yonaka15

pyodide_read-image

Read image files from mounted directories in Pyodide environments for Python-based image processing tasks.

Instructions

Read an image from a mounted directory

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
mountNameYesName of the mount point
imagePathYesPath of the image file

Implementation Reference

  • Handler for the 'pyodide_read-image' tool: validates input using isReadImageArgs and calls PyodideManager.readImage(mountName, imagePath).
    case "pyodide_read-image": {
      const readImageArgs = isReadImageArgs(args);
      if (readImageArgs instanceof type.errors) {
        throw readImageArgs;
      }
      const { mountName, imagePath } = readImageArgs;
      const results = await pyodideManager.readImage(mountName, imagePath);
      return results;
    }
  • Tool schema definition for 'pyodide_read-image', specifying input schema with mountName and imagePath as required string properties.
    export const READ_IMAGE_TOOL: Tool = {
      name: "pyodide_read-image",
      description: "Read an image from a mounted directory",
      inputSchema: {
        type: "object",
        properties: {
          mountName: {
            type: "string",
            description: "Name of the mount point",
          },
          imagePath: {
            type: "string",
            description: "Path of the image file",
          },
        },
        required: ["mountName", "imagePath"],
      },
    };
  • Registration of the pyodide_read-image tool by including READ_IMAGE_TOOL in the TOOLS array returned by ListToolsRequestHandler.
    const TOOLS: Tool[] = [
      tools.EXECUTE_PYTHON_TOOL,
      tools.INSTALL_PYTHON_PACKAGES_TOOL,
      tools.GET_MOUNT_POINTS_TOOL,
      tools.LIST_MOUNTED_DIRECTORY_TOOL,
      tools.READ_IMAGE_TOOL,
    ];
  • PyodideManager.readImage: core logic that retrieves image resource via readResource, formats it using contentFormatters.formatImage, and returns formatted success response.
    async readImage(mountName: string, imagePath: string) {
      if (!this.pyodide) {
        return formatCallToolError("Pyodide not initialized");
      }
      try {
        const resource = await this.readResource(mountName, imagePath);
        if ("error" in resource) {
          return formatCallToolError(resource.error);
        }
        const content = contentFormatters.formatImage(
          resource.blob,
          resource.mimeType
        );
        return formatCallToolSuccess(content);
      } catch (error) {
        return formatCallToolError(error);
      }
    }
  • PyodideManager.readResource: reads image file from host filesystem path, determines MIME type from extension, base64-encodes the content, returns blob and mimeType or error.
    async readResource(
      mountName: string,
      resourcePath: string
    ): Promise<
      | {
          blob: string;
          mimeType: string;
        }
      | { error: string }
    > {
      if (!this.pyodide) {
        return { error: "Pyodide not initialized" };
      }
    
      const mountConfig = this.mountPoints.get(mountName);
      if (!mountConfig) {
        return { error: `Mount point not found: ${mountName}` };
      }
    
      try {
        // Get full path to the image
        const fullPath = path.join(mountConfig.hostPath, resourcePath);
        if (!fs.existsSync(fullPath)) {
          return { error: `Image file not found: ${fullPath}` };
        }
    
        // Get MIME type from file extension
        const ext = path.extname(fullPath).toLowerCase();
        const mimeType = MIME_TYPES[ext];
        if (!mimeType) {
          return { error: `Unsupported image format: ${ext}` };
        }
    
        // Read and encode image
        const imageBuffer = await fs.promises.readFile(fullPath);
        const base64Data = imageBuffer.toString("base64");
    
        return { blob: base64Data, mimeType };
      } catch (error) {
        return { error: String(error) };
      }
    }

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/yonaka15/mcp-pyodide'

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