remove_background
Removes the background from an input image, returning a raster image with the background removed. Accepts a URL or local file path.
Instructions
Remove background in the input image using Recraft. This operation takes an input image and returns the same image with detected background removed. Raster image will be always returned. Local path or URL to resulting image and its preview will be returned in the response.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| imageURI | Yes | Image to use as an input. This can be a URL (starting with http:// or https://) or an absolute file path (starting with file://). |
Implementation Reference
- src/tools/RemoveBackground.ts:22-48 (handler)The main handler function for the remove_background tool. It parses the imageURI argument, downloads the image, calls the API's removeBackground method, and returns the result via transformSingleImageOperationToCallToolResult.
export const removeBackgroundHandler = async (server: RecraftServer, args: Record<string, unknown>): Promise<CallToolResult> => { try { const { imageURI } = z.object({ imageURI: z.string(), }).parse(args) const imageData = await downloadImage(imageURI) const result = await server.api.imageApi.removeBackground({ image: await imageDataToBlob(imageData), responseFormat: 'url', expire: server.isLocalResultsStorage, }) return await server.transformSingleImageOperationToCallToolResult(result.image, 'Removed background.') } catch (error) { return { content: [ { type: 'text', text: `Remove Background error: ${error}` } ], isError: true } } } - src/tools/RemoveBackground.ts:8-20 (schema)The tool registration object defining name 'remove_background', description, and inputSchema (requiring imageURI). Also references the PARAMETERS.imageURI definition.
export const removeBackgroundTool = { name: "remove_background", description: "Remove background in the input image using Recraft.\n" + "This operation takes an input image and returns the same image with detected background removed. Raster image will be always returned.\n" + "Local path or URL to resulting image and its preview will be returned in the response.", inputSchema: { type: "object", properties: { imageURI: PARAMETERS.imageURI, }, required: ["imageURI"] } } - src/index.ts:68-82 (registration)Registration of the removeBackgroundTool in the ListToolsRequestSchema handler so it appears in the tool list.
server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ generateImageTool, createStyleTool, vectorizeImageTool, imageToImageTool, removeBackgroundTool, replaceBackgroundTool, crispUpscaleTool, creativeUpscaleTool, getUserTool, ], } }) - src/index.ts:110-120 (registration)Routing for the remove_background tool call: dispatches to removeBackgroundHandler when tool name matches.
case removeBackgroundTool.name: return await removeBackgroundHandler(recraftServer, args ?? {}) case replaceBackgroundTool.name: return await replaceBackgroundHandler(recraftServer, args ?? {}) case crispUpscaleTool.name: return await crispUpscaleHandler(recraftServer, args ?? {}) case creativeUpscaleTool.name: return await creativeUpscaleHandler(recraftServer, args ?? {}) case getUserTool.name: return await getUserHandler(recraftServer, args ?? {}) default: - src/api/apis/ImageApi.ts:1027-1099 (helper)The API client method removeBackground that sends a POST request to /v1/images/removeBackground with the image as form data. The RemoveBackgroundRequest interface (lines 150-155) defines the schema for image, expire, imageFormat, and responseFormat.
/** * Remove background */ async removeBackgroundRaw(requestParameters: RemoveBackgroundRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<runtime.ApiResponse<ProcessImageResponse>> { if (requestParameters['image'] == null) { throw new runtime.RequiredError( 'image', 'Required parameter "image" was null or undefined when calling removeBackground().' ); } const queryParameters: any = {}; const headerParameters: runtime.HTTPHeaders = {}; if (this.configuration && this.configuration.accessToken) { const token = this.configuration.accessToken; const tokenString = await token("auth0", []); if (tokenString) { headerParameters["Authorization"] = `Bearer ${tokenString}`; } } const consumes: runtime.Consume[] = [ { contentType: 'multipart/form-data' }, ]; // @ts-ignore: canConsumeForm may be unused const canConsumeForm = runtime.canConsumeForm(consumes); let formParams: { append(param: string, value: any): any }; let useForm = false; // use FormData to transmit files using content-type "multipart/form-data" useForm = canConsumeForm; if (useForm) { formParams = new FormData(); } else { formParams = new URLSearchParams(); } if (requestParameters['expire'] != null) { formParams.append('expire', requestParameters['expire'] as any); } if (requestParameters['image'] != null) { formParams.append('image', requestParameters['image'] as any); } if (requestParameters['imageFormat'] != null) { formParams.append('image_format', requestParameters['imageFormat'] as any); } if (requestParameters['responseFormat'] != null) { formParams.append('response_format', requestParameters['responseFormat'] as any); } const response = await this.request({ path: `/v1/images/removeBackground`, method: 'POST', headers: headerParameters, query: queryParameters, body: formParams, }, initOverrides); return new runtime.JSONApiResponse(response, (jsonValue) => ProcessImageResponseFromJSON(jsonValue)); } /** * Remove background */ async removeBackground(requestParameters: RemoveBackgroundRequest, initOverrides?: RequestInit | runtime.InitOverrideFunction): Promise<ProcessImageResponse> { const response = await this.removeBackgroundRaw(requestParameters, initOverrides); return await response.value(); }