markitup_remove_background
Remove background from images using AI, returning a transparent PNG. Accepts image URL or base64 input.
Instructions
Remove the background from an image using Photoroom's HD AI background-removal service. Returns a transparent PNG. Costs 1 credit (free for active Pro/Power subscribers). Provide the source image as URL or base64.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image_url | No | Public HTTPS URL of the source image. | |
| image_base64 | No | Base64-encoded source image (no data: prefix). Mutually exclusive with image_url. | |
| image_mime_type | No | image/png |
Implementation Reference
- src/tools/bgremove.ts:27-57 (handler)Main handler function `runBgRemove` that processes the background removal request. It validates mutual exclusivity of image_url/image_base64, fetches the image if a URL is provided, calls the API endpoint /bgremove, and returns the result as text + image content.
export async function runBgRemove( api: MarkItUpApiClient, args: Record<string, unknown> ): Promise<{ content: Array<TextContent | ImageContent>; structuredContent: BgRemoveBackendResponse; }> { const imageUrl = typeof args.image_url === "string" ? args.image_url : undefined; const imageBase64 = typeof args.image_base64 === "string" ? args.image_base64 : undefined; const mimeType = typeof args.image_mime_type === "string" ? args.image_mime_type : "image/png"; if (!!imageUrl === !!imageBase64) { throw new Error("Provide exactly one of image_url or image_base64"); } const imageDataUrl = imageUrl ? await fetchAsDataUrl(imageUrl) : `data:${mimeType};base64,${imageBase64}`; const data = await api.post<BgRemoveBackendResponse>("/bgremove", { imageDataUrl }); const content: Array<TextContent | ImageContent> = [ { type: "text", text: "Background removed." }, ]; const parsed = parseDataUrl(data.imageDataUrl); if (parsed) { content.push({ type: "image", data: parsed.data, mimeType: parsed.mimeType }); } return { content, structuredContent: data }; } - src/tools/bgremove.ts:3-18 (schema)Tool definition with name 'markitup_remove_background', description, and inputSchema defining image_url, image_base64, and image_mime_type parameters.
export const bgremoveTool = { name: "markitup_remove_background", description: "Remove the background from an image using Photoroom's HD AI background-removal service. " + "Returns a transparent PNG. Costs 1 credit (free for active Pro/Power subscribers). " + "Provide the source image as URL or base64.", inputSchema: { type: "object", properties: { image_url: { type: "string", description: "Public HTTPS URL of the source image." }, image_base64: { type: "string", description: "Base64-encoded source image (no data: prefix). Mutually exclusive with image_url." }, image_mime_type: { type: "string", default: "image/png" }, }, additionalProperties: false, }, } as const; - src/index.ts:41-47 (registration)Tool is registered in the tools array alongside other tools (bgremoveTool at line 46).
const tools: Tool[] = [ balanceTool as unknown as Tool, generateTool as unknown as Tool, regenTool as unknown as Tool, extendTool as unknown as Tool, bgremoveTool as unknown as Tool, ]; - src/index.ts:63-64 (registration)Tool handler dispatch: case 'markitup_remove_background' routes to runBgRemove(api, args).
case bgremoveTool.name: return await runBgRemove(api, args ?? {}); - src/tools/bgremove.ts:59-65 (helper)Helper function `fetchAsDataUrl` that downloads an image from a URL and converts it to a data URL.
async function fetchAsDataUrl(url: string): Promise<string> { const res = await fetch(url); if (!res.ok) throw new Error(`Failed to fetch image_url (${res.status}): ${url}`); const buf = Buffer.from(await res.arrayBuffer()); const contentType = res.headers.get("content-type") ?? "image/png"; return `data:${contentType};base64,${buf.toString("base64")}`; }