get_random_wallpaper
Retrieve random Bing wallpapers with customizable resolution and region settings to refresh desktop backgrounds or find visual inspiration.
Instructions
从Bing壁纸获取随机壁纸,返回壁纸的URL、标题和描述信息
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| resolution | No | 壁纸分辨率,可选值:1920x1080, 3840x2160, 1366x768等,默认为1920x1080 | |
| market | No | 地区市场,如zh-CN(中国), en-US(美国)等,默认为zh-CN |
Implementation Reference
- src/main/index.ts:84-94 (registration)Registers the 'get_random_wallpaper' MCP tool, including input schema with optional resolution and market parameters using Zod, description, and a thin handler delegating to WallpaperController.server.tool( 'get_random_wallpaper', '从Bing壁纸获取随机壁纸,返回壁纸的URL、标题和描述信息', { resolution: z.string().optional().describe('壁纸分辨率,可选值:1920x1080, 3840x2160, 1366x768等,默认为1920x1080'), market: z.string().optional().describe('地区市场,如zh-CN(中国), en-US(美国)等,默认为zh-CN'), }, async ({ resolution = AppConfig.DEFAULT_WALLPAPER_RESOLUTION, market = AppConfig.DEFAULT_MARKET }) => { return await wallpaperController.getRandomWallpaper(resolution, market); } );
- Controller handler that invokes the wallpaper service, stringifies the result as JSON, and formats it into MCP-compatible response structure with error handling.async getRandomWallpaper(resolution: string = '1920x1080', market: string = 'zh-CN'): Promise<{ content: Array<{ type: 'text'; text: string }> }> { try { const wallpaperInfo = await this.wallpaperService.getRandomWallpaper(resolution, market); return { content: [ { type: 'text' as const, text: JSON.stringify(wallpaperInfo, null, 2), }, ], }; } catch (error) { return { content: [ { type: 'text', text: `获取壁纸时出错: ${error instanceof Error ? error.message : '未知错误'}`, }, ], }; } }
- Core tool logic: constructs Bing wallpaper API URL, performs HTTPS request, parses JSON response, extracts image details, and returns structured WallpaperInfo.async getRandomWallpaper(resolution: string = '1920x1080', market: string = 'zh-CN'): Promise<WallpaperInfo> { const apiUrl = `https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=${market}&uhd=1&uhdwidth=${resolution.split('x')[0]}&uhdheight=${resolution.split('x')[1]}`; const wallpaperData = await this.makeHttpRequest(apiUrl); if (!wallpaperData.images || wallpaperData.images.length === 0) { throw new Error('未找到可用的壁纸'); } const wallpaper = wallpaperData.images[0]; const baseUrl = 'https://www.bing.com'; const imageUrl = baseUrl + wallpaper.url; return { title: wallpaper.title, description: wallpaper.copyright, imageUrl: imageUrl, resolution: resolution, date: wallpaper.startdate, market: market, fullUrl: imageUrl }; }
- src/main/index.ts:87-90 (schema)Zod input schema for the tool parameters: optional resolution and market strings.{ resolution: z.string().optional().describe('壁纸分辨率,可选值:1920x1080, 3840x2160, 1366x768等,默认为1920x1080'), market: z.string().optional().describe('地区市场,如zh-CN(中国), en-US(美国)等,默认为zh-CN'), },
- Private helper method to perform HTTPS GET request to Bing API, accumulate response data, parse JSON, with error handling.private makeHttpRequest(url: string): Promise<any> { return new Promise((resolve, reject) => { https.get(url, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { try { const jsonData = JSON.parse(data); resolve(jsonData); } catch (error) { reject(new Error('解析壁纸数据失败')); } }); }).on('error', (error) => { reject(new Error(`获取壁纸失败: ${error.message}`)); }); }); }