search_music
Find music by searching for songs, artists, or albums using keywords. Streamline music discovery with customizable search filters and result limits.
Instructions
搜索音乐,支持按歌曲名、艺术家、专辑搜索
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | 返回结果数量限制 | |
| query | Yes | 搜索关键词 | |
| type | No | 搜索类型 | all |
Implementation Reference
- src/index.ts:199-213 (handler)The main handler function for the 'search_music' tool. It destructures the input arguments, performs the search using MusicDatabase, and formats the results into a text response for the MCP protocol.private async handleSearchMusic(args: any) { const { query, type = 'all', limit = 10 } = args; const results = await this.musicDb.search(query, type, limit); return { content: [ { type: 'text', text: `搜索结果 "${query}":\n\n${results.map(song => `🎵 ${song.title}\n👤 艺术家: ${song.artist}\n💿 专辑: ${song.album}\n⏱️ 时长: ${song.duration}\n🆔 ID: ${song.id}\n` ).join('\n')}`, }, ], }; }
- src/index.ts:43-63 (schema)The input schema definition for the 'search_music' tool, specifying parameters like query (required), type, and limit.inputSchema: { type: 'object', properties: { query: { type: 'string', description: '搜索关键词', }, type: { type: 'string', enum: ['song', 'artist', 'album', 'all'], description: '搜索类型', default: 'all', }, limit: { type: 'number', description: '返回结果数量限制', default: 10, }, }, required: ['query'], },
- src/index.ts:40-64 (registration)Registration of the 'search_music' tool in the ListTools response, including name, description, and input schema.{ name: 'search_music', description: '搜索音乐,支持按歌曲名、艺术家、专辑搜索', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '搜索关键词', }, type: { type: 'string', enum: ['song', 'artist', 'album', 'all'], description: '搜索类型', default: 'all', }, limit: { type: 'number', description: '返回结果数量限制', default: 10, }, }, required: ['query'], }, },
- src/music-database.ts:72-94 (helper)Supporting utility method in MusicDatabase that implements the core search logic by filtering an in-memory array of songs based on query, type, and limit.async search(query: string, type: string = 'all', limit: number = 10): Promise<Song[]> { const lowerQuery = query.toLowerCase(); let filtered = this.songs.filter(song => { switch (type) { case 'song': return song.title.toLowerCase().includes(lowerQuery); case 'artist': return song.artist.toLowerCase().includes(lowerQuery); case 'album': return song.album.toLowerCase().includes(lowerQuery); case 'all': default: return ( song.title.toLowerCase().includes(lowerQuery) || song.artist.toLowerCase().includes(lowerQuery) || song.album.toLowerCase().includes(lowerQuery) ); } }); return filtered.slice(0, limit); }
- src/music-database.ts:1-11 (schema)Type definition for Song objects used in search results.export interface Song { id: string; title: string; artist: string; album: string; year: number; duration: string; genre: string; playCount: number; rating: number; }