get_random_wallpaper
Retrieve a random wallpaper URL with title and description from Bing Wallpapers, allowing customization of resolution and regional market settings.
Instructions
从Bing壁纸获取随机壁纸,返回壁纸的URL、标题和描述信息
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| market | No | 地区市场,如zh-CN(中国), en-US(美国)等,默认为zh-CN | |
| resolution | No | 壁纸分辨率,可选值:1920x1080, 3840x2160, 1366x768等,默认为1920x1080 |
Implementation Reference
- Core handler implementing the logic to fetch random wallpaper from Bing's HPImageArchive API, parse response, and return structured data.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/models/Project.ts:9-17 (schema)TypeScript interface defining the output schema for wallpaper information.export interface WallpaperInfo { title: string; description: string; imageUrl: string; resolution: string; date: string; market: string; fullUrl: string; }
- src/main/index.ts:84-94 (registration)Registers the 'get_random_wallpaper' MCP tool, defines input schema with Zod, and sets up handler delegating to controller.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 service and formats response for MCP protocol.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 : '未知错误'}`, }, ], }; } }
- Supporting utility for HTTPS requests to the Bing API.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}`)); }); }); } }