get_image_size
Retrieve the dimensions (width and height) of an image directly from its URL for accurate sizing and processing in applications.
Instructions
Get the size of an image from URL
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| options | Yes | Options for retrieving image size |
Implementation Reference
- src/server.ts:179-197 (handler)MCP tool handler that extracts the image URL from input options, calls the helper function getImageSizeFromUrl, formats the result as JSON text content, and handles errors.async ({ options = {} }) => { try { const { imageUrl } = options as { imageUrl: string }; // Call tool function implementation const result = await getImageSizeFromUrl(imageUrl); return { content: [ { type: "text" as const, text: JSON.stringify(result, null, 2), }, ], }; } catch (error) { throw new Error( `Failed to get image size: ${(error as Error).message}`, ); } },
- src/tools/imageSize.ts:5-14 (helper)Core helper function that uses probe-image-size to fetch and return width, height, type, and mime of an image from a given URL.export async function getImageSizeFromUrl(imageUrl: string) { // 获取图片尺寸信息 const imageInfo = await probe(imageUrl); return { width: imageInfo.width, height: imageInfo.height, type: imageInfo.type, mime: imageInfo.mime, }; }
- src/server.ts:173-177 (schema)Zod input schema defining the required 'imageUrl' string parameter for the tool.options: z .object({ imageUrl: z.string().describe("Url of the image to retrieve"), }) .describe("Options for retrieving image size"),
- src/server.ts:169-198 (registration)Full registration of the 'get_image_size' tool on the MCP server, including name, description, schema, and handler implementation.server.tool( "get_image_size", "Get the size of an image from URL", { options: z .object({ imageUrl: z.string().describe("Url of the image to retrieve"), }) .describe("Options for retrieving image size"), }, async ({ options = {} }) => { try { const { imageUrl } = options as { imageUrl: string }; // Call tool function implementation const result = await getImageSizeFromUrl(imageUrl); return { content: [ { type: "text" as const, text: JSON.stringify(result, null, 2), }, ], }; } catch (error) { throw new Error( `Failed to get image size: ${(error as Error).message}`, ); } }, );