Skip to main content
Glama

search_and_play_music

Search for a track on Spotify and begin playback immediately. This tool combines music search and playback in one operation for voice commands, quick access, and automation workflows.

Instructions

Instantly search for a track and begin playback in one seamless operation for immediate music gratification.

🎯 USE CASES: β€’ Voice-activated music requests with instant playback β€’ Quick music access without browsing interfaces β€’ Party DJ functionality with instant song requests β€’ Smart home integration with spoken music commands β€’ Emergency music solutions when you need a specific song now

πŸ“ WHAT IT RETURNS: β€’ Search results showing the track that was found β€’ Playback confirmation with current track information β€’ Device information where playback started β€’ Error details if track couldn't be found or played β€’ Alternative suggestions if exact match isn't available

πŸ” EXAMPLES: β€’ "Search and play 'Bohemian Rhapsody' by Queen" β€’ "Find and start 'Uptown Funk' immediately" β€’ "Play the first result for 'relaxing piano music'" β€’ "Search 'workout motivation' and start playing"

🎡 SMART PLAYBACK: β€’ Automatically selects the best match from search results β€’ Prioritizes popular and high-quality versions β€’ Starts playback on user's active device β€’ Falls back gracefully if preferred version unavailable β€’ Maintains context for follow-up requests

πŸ’‘ WORKFLOW OPTIMIZATION: β€’ Eliminates manual track selection step β€’ Perfect for hands-free music control β€’ Reduces interaction friction for immediate needs β€’ Great for mood-based music requests β€’ Ideal for social settings and parties

πŸš€ INSTANT GRATIFICATION: β€’ No browsing or selection required β€’ Immediate musical response to requests β€’ Perfect for time-sensitive music needs β€’ Streamlined user experience β€’ Ideal for voice and automation interfaces

⚠️ REQUIREMENTS: β€’ Valid Spotify access token with user-modify-playback-state scope β€’ Active Spotify device must be available β€’ Search query should be specific enough for good matching

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tokenYesSpotify access token for authentication
queryYesSearch query for the track to find and play

Implementation Reference

  • The handler function that executes the tool: destructures args, calls spotifyService.searchMusic, plays the first track found, or throws error if none.
    handler: async (args: any, spotifyService: SpotifyService) => { const { token, query } = args; const result = await spotifyService.searchMusic(token, query); if (result.tracks && result.tracks.items.length > 0) { const track_id = result.tracks.items[0].id; const playResult = await spotifyService.playMusic(token, track_id); return playResult; } throw new Error("No tracks found for the search query"); },
  • The Zod input schema for the tool, requiring Spotify token and search query.
    schema: createSchema({ token: commonSchemas.token(), query: commonSchemas.searchQuery("the track to find and play"), }),
  • Registration of all tools including searchTools (which contains search_and_play_music) into the central allTools registry used by ToolRegistrar.
    export const allTools: ToolsRegistry = { ...albumTools, ...artistTools, ...trackTools, ...playlistTools, ...playbackTools, ...userTools, ...searchTools, };
  • SpotifyService.searchMusic helper method called by the tool handler to perform the search API call.
    async searchMusic( token: string, query: string, type: "track" | "album" | "artist" | "playlist" = "track", limit: number = 10 ): Promise<SearchResult> { const params = { q: query, type: type, limit: Math.min(limit, 50), }; return await this.makeRequest<SearchResult>("search", token, params); }
  • SpotifyService.playMusic helper method called by the tool handler to start playback of the selected track.
    async playMusic( token: string, trackUris: string | string[] | null = null, contextUri: string | null = null, deviceId: string | null = null ): Promise<void> { const data: Record<string, any> = {}; if (trackUris) { data.uris = Array.isArray(trackUris) ? trackUris : [`spotify:track:${this.extractId(trackUris)}`]; } if (contextUri) { data.context_uri = contextUri; } const endpoint = deviceId ? `me/player/play?device_id=${deviceId}` : "me/player/play"; return await this.makeRequest<void>(endpoint, token, {}, "PUT", data);

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/latiftplgu/Spotify-OAuth-MCP-server'

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