getAlbumTracks
Retrieve tracks from a Spotify album using album ID. Supports pagination with limit and offset for efficient track listing.
Instructions
Get tracks from a specific album with pagination support
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| albumId | Yes | The Spotify ID of the album | |
| limit | No | Maximum number of tracks to return (1-50) | |
| offset | No | Offset for pagination (0-based index) |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"albumId": {
"description": "The Spotify ID of the album",
"type": "string"
},
"limit": {
"description": "Maximum number of tracks to return (1-50)",
"maximum": 50,
"minimum": 1,
"type": "number"
},
"offset": {
"description": "Offset for pagination (0-based index)",
"minimum": 0,
"type": "number"
}
},
"required": [
"albumId"
],
"type": "object"
}
Implementation Reference
- src/albums.ts:120-174 (handler)The handler function that implements the core logic of fetching tracks from a Spotify album using pagination, formatting the track list with artists and durations, and returning a structured markdown response or error.handler: async (args, _extra: SpotifyHandlerExtra) => { const { albumId, limit = 20, offset = 0 } = args; try { const tracks = await handleSpotifyRequest(async (spotifyApi) => { return await spotifyApi.albums.tracks( albumId, undefined, limit as MaxInt<50>, offset, ); }); if (tracks.items.length === 0) { return { content: [ { type: 'text', text: 'No tracks found in this album', }, ], }; } const formattedTracks = tracks.items .map((track, i) => { if (!track) return `${i + 1}. [Track not found]`; const artists = track.artists.map((a) => a.name).join(', '); const duration = formatDuration(track.duration_ms); return `${offset + i + 1}. "${track.name}" by ${artists} (${duration}) - ID: ${track.id}`; }) .join('\n'); return { content: [ { type: 'text', text: `# Album Tracks (${offset + 1}-${offset + tracks.items.length} of ${tracks.total})\n\n${formattedTracks}`, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `Error getting album tracks: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } },
- src/albums.ts:106-119 (schema)Input schema using Zod for validating tool parameters: albumId (string), optional limit (1-50), optional offset (>=0).schema: { albumId: z.string().describe('The Spotify ID of the album'), limit: z .number() .min(1) .max(50) .optional() .describe('Maximum number of tracks to return (1-50)'), offset: z .number() .min(0) .optional() .describe('Offset for pagination (0-based index)'), },
- src/index.ts:12-14 (registration)Registers the getAlbumTracks tool (via albumTools array) with the MCP server by calling server.tool for each tool in the combined array.[...readTools, ...playTools, ...albumTools].forEach((tool) => { server.tool(tool.name, tool.description, tool.schema, tool.handler); });
- src/albums.ts:299-304 (registration)Exports the array of album-related tools, including getAlbumTracks, which is later imported and registered in index.ts.export const albumTools = [ getAlbums, getAlbumTracks, saveOrRemoveAlbumForUser, checkUsersSavedAlbums, ];