pyodide_read-image
Extract and read image files from a specified mounted directory using Python on the mcp-pyodide server. Input mount name and image path to process visual data efficiently.
Instructions
Read an image from a mounted directory
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| imagePath | Yes | Path of the image file | |
| mountName | Yes | Name of the mount point |
Implementation Reference
- Core implementation of pyodide_read-image: calls readResource to get base64 image data and formats it as MCP image content.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); } }
- Helper method that reads image from mounted directory's host path, determines MIME type using MIME_TYPES, and base64 encodes the file.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) }; } }
- src/tools/index.ts:64-81 (schema)Tool definition including name, description, and input schema for pyodide_read-image.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"], }, };
- src/handlers/index.ts:118-126 (handler)MCP CallToolRequest handler: validates input using isReadImageArgs and delegates to PyodideManager.readImage.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; }
- src/handlers/index.ts:32-38 (registration)Registers READ_IMAGE_TOOL (pyodide_read-image) in the server's list of available tools, served via ListToolsRequest.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, ];