Skip to main content
Glama

get-top-tracks

Retrieve a user's top Spotify tracks over a specified time range. Customize results by adjusting the number of tracks, offset, and time frame (short, medium, or long term) using this integrated tool.

Instructions

Get the user's top played tracks over a specified time range

Input Schema

NameRequiredDescriptionDefault
limitNoThe number of tracks to return (1-50, default: 20)
offsetNoThe index of the first track to return (default: 0)
time_rangeNoOver what time frame the affinities are computed. short_term = ~4 weeks, medium_term = ~6 months, long_term = several years (default: medium_term)

Input Schema (JSON Schema)

{ "properties": { "limit": { "description": "The number of tracks to return (1-50, default: 20)", "type": "number" }, "offset": { "description": "The index of the first track to return (default: 0)", "type": "number" }, "time_range": { "description": "Over what time frame the affinities are computed. short_term = ~4 weeks, medium_term = ~6 months, long_term = several years (default: medium_term)", "enum": [ "short_term", "medium_term", "long_term" ], "type": "string" } }, "type": "object" }

Implementation Reference

  • Handler function for the 'get-top-tracks' tool. Parses input using GetTopTracksSchema, calls Spotify API /me/top/tracks endpoint with parameters, formats the top tracks response with details like name, artist, album, ID, duration, URL.
    if (name === "get-top-tracks") { const { limit, offset, time_range } = GetTopTracksSchema.parse(args); const params = new URLSearchParams(); params.append("limit", limit.toString()); params.append("offset", offset.toString()); params.append("time_range", time_range); const topTracks = await spotifyApiRequest(`/me/top/tracks?${params}`); const formattedTracks = topTracks.items .map( (track: any) => ` Track: ${track.name} Artist: ${track.artists.map((a: any) => a.name).join(", ")} Album: ${track.album.name} ID: ${track.id} Duration: ${Math.floor(track.duration_ms / 1000 / 60)}:${( Math.floor(track.duration_ms / 1000) % 60 ) .toString() .padStart(2, "0")} URL: ${track.external_urls.spotify} ---` ) .join("\n"); return { content: [ { type: "text", text: topTracks.items.length > 0 ? `Your top tracks:\n${formattedTracks}` : "No top tracks found for the specified time range.", }, ], }; }
  • Zod schema for validating input parameters to the get-top-tracks tool: limit (1-50), offset (>=0), time_range (short_term|medium_term|long_term).
    const GetTopTracksSchema = z.object({ limit: z.number().min(1).max(50).default(20), offset: z.number().min(0).default(0), time_range: z.enum(["short_term", "medium_term", "long_term"]).default("medium_term"), });
  • index.ts:806-827 (registration)
    Tool registration in ListToolsRequestHandler, including name, description, and inputSchema matching the Zod schema.
    { name: "get-top-tracks", description: "Get the user's top played tracks over a specified time range", inputSchema: { type: "object", properties: { limit: { type: "number", description: "The number of tracks to return (1-50, default: 20)", }, offset: { type: "number", description: "The index of the first track to return (default: 0)", }, time_range: { type: "string", enum: ["short_term", "medium_term", "long_term"], description: "Over what time frame the affinities are computed. short_term = ~4 weeks, medium_term = ~6 months, long_term = several years (default: medium_term)", } } } },

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/imprvhub/mcp-claude-spotify'

If you have feedback or need assistance with the MCP directory API, please join our Discord server