resize_image
Adjust image dimensions by specifying width, height, and fit method. Save the resized image to a designated path for optimized file management.
Instructions
Resize an image and save to a new file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fit | No | Fit method: cover, contain, fill, inside, outside | |
| height | No | Target height in pixels (optional) | |
| input | Yes | Path to the input image file | |
| output | Yes | Path to save the resized image | |
| width | Yes | Target width in pixels |
Implementation Reference
- src/index.ts:385-436 (handler)Executes the resize_image tool: validates input, uses Sharp to resize the image to specified dimensions with fit option, saves to output path, retrieves metadata, and returns success message or error.case "resize_image": { const { input, output, width, height, fit = "cover" } = request.params.arguments as { input: string, output: string, width: number, height?: number, fit?: string }; if (!input || !output || !width) { throw new McpError(ErrorCode.InvalidParams, "Input path, output path, and width are required"); } if (!fs.existsSync(input)) { throw new McpError(ErrorCode.InvalidRequest, `Input image not found: ${input}`); } try { // Create output directory if it doesn't exist await fsExtra.ensureDir(path.dirname(output)); // Resize the image await sharp(input) .resize({ width, height, fit: fit as keyof sharp.FitEnum }) .toFile(output); // Get metadata of the resized image const metadata = await getImageMetadata(output); return { content: [ { type: "text", text: `Image resized successfully:\n` + `- Original: ${input}\n` + `- Resized: ${output}\n` + `- New dimensions: ${metadata.width}x${metadata.height}\n` + `- Size: ${(metadata.size / 1024).toFixed(2)} KB` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error resizing image: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } }
- src/index.ts:254-280 (schema)Input schema defining parameters for resize_image: input/output paths, width (required), optional height and fit method.inputSchema: { type: "object", properties: { input: { type: "string", description: "Path to the input image file" }, output: { type: "string", description: "Path to save the resized image" }, width: { type: "number", description: "Target width in pixels" }, height: { type: "number", description: "Target height in pixels (optional)" }, fit: { type: "string", description: "Fit method: cover, contain, fill, inside, outside", enum: ["cover", "contain", "fill", "inside", "outside"] } }, required: ["input", "output", "width"] }
- src/index.ts:251-281 (registration)Tool registration in the server.setTools array, including name, description, and input schema.{ name: "resize_image", description: "Resize an image and save to a new file", inputSchema: { type: "object", properties: { input: { type: "string", description: "Path to the input image file" }, output: { type: "string", description: "Path to save the resized image" }, width: { type: "number", description: "Target width in pixels" }, height: { type: "number", description: "Target height in pixels (optional)" }, fit: { type: "string", description: "Fit method: cover, contain, fill, inside, outside", enum: ["cover", "contain", "fill", "inside", "outside"] } }, required: ["input", "output", "width"] } },