count-desktop-images
Counts the number of image files stored on your desktop, enabling users to manage and organize their desktop image collections efficiently with the Desktop Image Manager MCP Server.
Instructions
统计桌面上的图片文件数量
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {},
"type": "object"
}
Implementation Reference
- server.ts:53-71 (handler)The asynchronous handler function that executes the tool logic: fetches desktop image files and returns the count in a text response, with error handling.async () => { try { const imageFiles = await getDesktopImageFiles(); return { content: [{ type: "text", text: `桌面上共有 ${imageFiles.length} 个图片文件。` }] }; } catch (error) { return { content: [{ type: "text", text: `获取图片数量时出错: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } }
- server.ts:49-72 (registration)Registration of the 'count-desktop-images' tool with the MCP server, specifying name, description, empty input schema, and handler function.server.tool( "count-desktop-images", "统计桌面上的图片文件数量", {}, async () => { try { const imageFiles = await getDesktopImageFiles(); return { content: [{ type: "text", text: `桌面上共有 ${imageFiles.length} 个图片文件。` }] }; } catch (error) { return { content: [{ type: "text", text: `获取图片数量时出错: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } );
- server.ts:27-40 (helper)Core helper function that reads the desktop directory, filters image files using isImageFile, and returns their names.const getDesktopImageFiles = async (): Promise<string[]> => { const desktopPath = getDesktopPath(); try { const files = await fs.readdir(desktopPath); const imagePaths = files.filter(file => { const filePath = path.join(desktopPath, file); return fs.statSync(filePath).isFile() && isImageFile(filePath); }); return imagePaths; } catch (error) { console.error(`Error reading desktop directory: ${error}`, ); return []; } };
- server.ts:19-23 (helper)Helper function to check if a file path corresponds to a supported image extension.const isImageFile = (filePath: string): boolean => { const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.tiff', '.svg']; const ext = path.extname(filePath).toLowerCase(); return imageExtensions.includes(ext); };
- server.ts:13-16 (helper)Helper function to resolve the user's desktop directory path.const getDesktopPath = () => { const { homedir } = os.userInfo(); return path.join(homedir, 'Desktop'); };