get_recommendations
Get personalized music recommendations based on genre preferences and current mood to discover new songs and artists.
Instructions
根据用户喜好获取音乐推荐
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "get_recommendations"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| genre | No | 音乐风格 | |
| mood | No | 心情/氛围 | |
| limit | No | 推荐数量 |
Implementation Reference
- src/index.ts:301-315 (handler)Primary MCP tool handler for 'get_recommendations'. Parses arguments, fetches recommendations from MusicDatabase, formats and returns the response as text content.
private async handleGetRecommendations(args: any) { const { genre, mood, limit = 10 } = args; const recommendations = await this.musicDb.getRecommendations(genre, mood, limit); return { content: [ { type: 'text', text: `🎯 音乐推荐${genre ? ` (${genre})` : ''}${mood ? ` - ${mood}心情` : ''}:\n\n${recommendations.map(song => `🎵 ${song.title}\n👤 ${song.artist}\n💿 ${song.album}\n⭐ ${song.rating}/5\n🆔 ${song.id}\n` ).join('\n')}`, }, ], }; } - src/index.ts:137-158 (registration)Tool registration in the ListToolsRequestHandler response. Defines the tool name, description, and input schema for validation.
{ name: 'get_recommendations', description: '根据用户喜好获取音乐推荐', inputSchema: { type: 'object', properties: { genre: { type: 'string', description: '音乐风格', }, mood: { type: 'string', description: '心情/氛围', }, limit: { type: 'number', description: '推荐数量', default: 10, }, }, }, }, - src/index.ts:181-182 (registration)Switch case registration in CallToolRequestHandler that routes calls to the handler function.
case 'get_recommendations': return await this.handleGetRecommendations(args); - src/music-database.ts:100-123 (helper)Supporting method in MusicDatabase class implementing the recommendation logic: filters by genre/mood, sorts by score, limits results.
async getRecommendations(genre?: string, mood?: string, limit: number = 10): Promise<Song[]> { let filtered = [...this.songs]; if (genre) { filtered = filtered.filter(song => song.genre.toLowerCase().includes(genre.toLowerCase()) ); } // 根据心情推荐(简单的逻辑示例) if (mood) { const moodLower = mood.toLowerCase(); if (moodLower.includes('快乐') || moodLower.includes('开心')) { filtered = filtered.filter(song => song.genre === 'Pop'); } else if (moodLower.includes('安静') || moodLower.includes('放松')) { filtered = filtered.filter(song => song.rating >= 4); } } // 按评分和播放次数排序 filtered.sort((a, b) => (b.rating * b.playCount) - (a.rating * a.playCount)); return filtered.slice(0, limit); }