get_artist
Retrieve artist information from Spotify to enhance music discovery through natural language queries.
Instructions
Get Artist name
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| method | Yes | Get or post request |
Implementation Reference
- src/index.ts:21-68 (handler)The handler function for the 'get_artist' tool. It fetches artist data from the Spotify API using the provided HTTP method, processes the response, extracts artist names, and returns them in a text content block.async ({ method }) => { const methodUpper = method.toUpperCase(); const artistData = await fetch("https://api.spotify.com/v1/artists?ids=0TnOYISbd1XYRBk9myaseg", { headers: { Authorization: `Bearer ${accessToken}`, }, method }) if (!artistData) { return { content: [ { type: "text", text: "Failed to retrieve tracks", }, ], }; } const allArtistsData = await artistData.json() console.log(allArtistsData) const data = allArtistsData.artists || []; if (data.length===0) { return { content: [ { type: "text", text: `No artists`, }, ], }; } let names = [] for(let i = 0; i<data.length; i++){ names.push(data[i].name) } const finalOutput = `Artist name is ${names}`; return { content: [ { type: "text", text: finalOutput, }, ], }; },
- src/index.ts:18-20 (schema)Input schema defined using Zod, specifying a 'method' parameter as a string with minimum length 3.{ method: z.string().min(3).describe("Get or post request") },
- src/index.ts:15-69 (registration)Registration of the 'get_artist' tool on the MCP server, including name, description, input schema, and handler function.server.tool( "get_artist", "Get Artist name", { method: z.string().min(3).describe("Get or post request") }, async ({ method }) => { const methodUpper = method.toUpperCase(); const artistData = await fetch("https://api.spotify.com/v1/artists?ids=0TnOYISbd1XYRBk9myaseg", { headers: { Authorization: `Bearer ${accessToken}`, }, method }) if (!artistData) { return { content: [ { type: "text", text: "Failed to retrieve tracks", }, ], }; } const allArtistsData = await artistData.json() console.log(allArtistsData) const data = allArtistsData.artists || []; if (data.length===0) { return { content: [ { type: "text", text: `No artists`, }, ], }; } let names = [] for(let i = 0; i<data.length; i++){ names.push(data[i].name) } const finalOutput = `Artist name is ${names}`; return { content: [ { type: "text", text: finalOutput, }, ], }; }, )