Skip to main content
Glama
igorgarbuz

Spotify MCP Node Server

by igorgarbuz

addTracksToPlaylist

Add multiple songs to a Spotify playlist using track IDs. Specify playlist ID and position for insertion.

Instructions

Add tracks to a Spotify playlist

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
playlistIdYesThe Spotify ID of the playlist
trackIdsYesArray of Spotify track IDs to add
positionNoPosition to insert the tracks (0-based index)

Implementation Reference

  • The handler function that adds tracks to a Spotify playlist by converting track IDs to URIs and calling the Spotify API's addItemsToPlaylist method.
      handler: async (args, extra: SpotifyHandlerExtra) => {
        const { playlistId, trackIds, position } = args;
    
        if (trackIds.length === 0) {
          return {
            content: [
              {
                type: 'text',
                text: 'Error: No track IDs provided',
              },
            ],
          };
        }
    
        try {
          const trackUris = trackIds.map((id) => `spotify:track:${id}`);
    
          await handleSpotifyRequest(async (spotifyApi) => {
            await spotifyApi.playlists.addItemsToPlaylist(
              playlistId,
              trackUris,
              position,
            );
          });
    
          return {
            content: [
              {
                type: 'text',
                text: `Successfully added ${trackIds.length} track${
                  trackIds.length === 1 ? '' : 's'
                } to playlist (ID: ${playlistId})`,
              },
            ],
          };
        } catch (error) {
          return {
            content: [
              {
                type: 'text',
                text: `Error adding tracks to playlist: ${
                  error instanceof Error ? error.message : String(error)
                }`,
              },
            ],
          };
        }
      },
    };
  • Zod schema defining the input parameters: playlistId (string), trackIds (array of strings), position (optional nonnegative number).
    schema: {
      playlistId: z.string().describe('The Spotify ID of the playlist'),
      trackIds: z.array(z.string()).describe('Array of Spotify track IDs to add'),
      position: z
        .number()
        .nonnegative()
        .optional()
        .describe('Position to insert the tracks (0-based index)'),
    },
  • src/write.ts:238-243 (registration)
    Exports the addTracksToPlaylist tool as part of the writeTools array.
    export const writeTools = [
      addToQueue,
      addTracksToPlaylist,
      createPlaylist,
      removeTracksFromPlaylist,
    ];
  • src/index.ts:12-14 (registration)
    Registers all tools from playTools, readTools, and writeTools (including addTracksToPlaylist) with the MCP server.
    [...playTools, ...readTools, ...writeTools].forEach((tool) => {
      server.tool(tool.name, tool.description, tool.schema, tool.handler);
    });
  • Uses the handleSpotifyRequest helper (imported from utils.ts) to perform the API call securely.
    await handleSpotifyRequest(async (spotifyApi) => {
      await spotifyApi.playlists.addItemsToPlaylist(
        playlistId,
        trackUris,
        position,
      );
    });

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observed

TDQS

C2.8/5.0
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. The text merely paraphrases the tool name without revealing side effects, authentication requirements, or post-conditions (e.g., whether tracks are appended or replaced). It does not add value beyond what the name already implies.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single sentence with no filler or redundant text. It is front-loaded with the core action, making it efficient and easy to parse for an AI agent.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

This is a mutation tool with no annotations, no output schema, and a minimal description. It fails to communicate important contextual details such as whether the operation is idempotent, duplicate handling, limits on track count, or required permissions. The description is under-specified for reliable tool selection and invocation.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, with each parameter (playlistId, trackIds, position) having a clear description. The tool description adds nothing extra about parameter usage, but the schema already provides sufficient meaning, so the baseline of 3 applies.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's action ('Add tracks') and resource ('a Spotify playlist'), using a specific verb+resource structure. It is distinct from sibling tools like addToQueue or createPlaylist, which target different operations.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided on when to use this tool versus alternatives such as addToQueue or createPlaylist. The description lacks any context about prerequisites, sequencing, or preferred scenarios, offering zero actionable usage advice.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.