library-tool.ts•3.75 kB
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { SpotifyClient } from "../../spotify-client.js";
import {
saveTrack,
removeTrack,
getSavedTracks,
formatSavedTracks,
getRecentlyPlayedTracks,
formatRecentlyPlayed,
} from "../library.js";
export function registerLibraryTools(server: McpServer, spotifyClient: SpotifyClient) {
// Save Track Tool
server.tool(
"save_track",
"Save a track to user's Spotify library",
{
track_id: z.string().describe("Spotify track ID"),
},
async ({ track_id }) => {
try {
await saveTrack(spotifyClient, track_id);
return {
content: [{ type: "text", text: `Track saved to your library` }],
};
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
return {
content: [{ type: "text", text: `Error saving track: ${errorMessage}` }],
isError: true,
};
}
}
);
// Remove Track Tool
server.tool(
"remove_saved_track",
"Remove a track from user's Spotify library",
{
track_id: z.string().describe("Spotify track ID"),
},
async ({ track_id }) => {
try {
await removeTrack(spotifyClient, track_id);
return {
content: [{ type: "text", text: `Track removed from your library` }],
};
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
return {
content: [{ type: "text", text: `Error removing track: ${errorMessage}` }],
isError: true,
};
}
}
);
// Get Saved Tracks Tool
server.tool(
"get_saved_tracks",
"Get user's saved tracks from Spotify library",
{
limit: z
.number()
.optional()
.default(20)
.describe("Maximum number of tracks to return (default: 20)"),
},
async ({ limit }) => {
try {
const tracks = await getSavedTracks(spotifyClient, limit);
const formatted = formatSavedTracks(tracks);
return {
content: [{ type: "text", text: formatted }],
};
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
return {
content: [{ type: "text", text: `Error getting saved tracks: ${errorMessage}` }],
isError: true,
};
}
}
);
// Get Recently Played Tracks Tool
server.tool(
"get_recently_played",
"Get user's recently played tracks. Returns the last 50 tracks played (max). Useful for getting seed data for recommendations.",
{
limit: z
.number()
.optional()
.default(20)
.describe("Maximum number of tracks to return (1-50, default: 20)"),
after: z
.number()
.optional()
.describe("Unix timestamp in milliseconds. Returns all items after this time."),
before: z
.number()
.optional()
.describe("Unix timestamp in milliseconds. Returns all items before this time."),
},
async ({ limit, after, before }) => {
try {
const tracks = await getRecentlyPlayedTracks(spotifyClient, limit, after, before);
const formatted = formatRecentlyPlayed(tracks);
return {
content: [{ type: "text", text: formatted }],
};
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
return {
content: [{ type: "text", text: `Error getting recently played tracks: ${errorMessage}` }],
isError: true,
};
}
}
);
}