UNPKG

@withpica/mcp-server

Version:

MCP Server for withPICA — enables AI assistants to interact with your withPICA music catalog

230 lines • 12.5 kB
// Copyright (c) 2025-2026 Withpica Ltd. All rights reserved. import { typedError } from "./error-envelope.js"; import { parseApiErrorBody } from "./api-error-body.js"; import { webBaseUrl } from "../lib/view-url.js"; import { formatSuccess } from "@withpica/mcp-utils"; const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; const MAX_PARTIES = 50; const TOTAL_BP = 10_000; export class SharedRecordsTools { pica; constructor(pica) { this.pica = pica; } getTools() { return [ { definition: { name: "pica_shared_record_propose_change", scope: "write:catalog", audiences: ["stdio", "http"], tier: "write", description: "Draft a change to the split on a work this catalogue shares with another catalogue (one shared record). " + "Use when the user asks: 'propose that Ana gets 60% on our shared song', 'draft a change taking Ben off the split'. " + "Only an app the user signed in with, as an owner or admin of this catalogue, can draft one; anyone else is refused, and should hand the user the work's page instead. " + "It only drafts: an owner or admin of this catalogue must send it from the link this returns, " + "then the other catalogue says yes or no, and everyone whose share changes is asked to confirm. " + "Give the WHOLE split after the change, in basis points summing to 10000. " + "When it takes someone off the split, a reason is required, and everyone asked sees it. " + "Use it only when the user says what the split should be; never propose a split on your own judgement.", workflows: ["infrastructure"], vernacular_kind: "creator-entry", inputSchema: { type: "object", properties: { shared_record_id: { type: "string", description: "The shared record's id (the work's one record across both catalogues).", }, side: { type: "string", enum: ["composition", "master"], description: "composition: writers and publishers. master: the recording's owners.", }, isrc: { type: "string", description: "For a master change: the recording's ISRC.", }, parties: { type: "array", description: `The whole split after the change, up to ${MAX_PARTIES} people.`, items: { type: "object", properties: { name: { type: "string" }, role: { type: "string", description: "e.g. writer, composer, publisher; for a master: owner or label.", }, share_bp: { type: "integer", description: "Their share in basis points (6000 = 60%).", }, person_id: { type: "string", description: "Their person id in this catalogue, when known.", }, ipi: { type: "string" }, isni: { type: "string" }, }, required: ["name", "role", "share_bp"], }, }, reason: { type: "string", description: "Why the split is changing, in a sentence or two. Required when someone comes off the split.", }, }, required: ["shared_record_id", "side", "parties"], }, annotations: { title: "draft a change to a shared work's split", readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false, }, }, executor: this.propose.bind(this), }, { definition: { name: "pica_shared_records_find", scope: "read:catalog", audiences: ["stdio", "http"], tier: "read", description: "Find this catalogue's songs that it shares with another catalogue as one shared record, by title. " + "Use when the user asks: 'which of our songs do we share with other catalogues?', 'find the shared record for Blue Water'. " + "Each answer gives the shared record's id (what pica_shared_record_propose_change takes), the song's title here, " + "the other catalogue's name and how many changes are open. It shows no people and no shares. " + "Only an app the user signed in with, as an owner or admin of this catalogue, can use it. → then: pica_shared_record_propose_change", workflows: ["infrastructure"], vernacular_kind: "creator-entry", inputSchema: { type: "object", properties: { title: { type: "string", description: "Part of the song's title. Leave out to list every shared song (up to the limit).", }, limit: { type: "integer", minimum: 1, maximum: 50, description: "How many to return, 1 to 50 (default 20).", }, }, }, annotations: { title: "find shared songs", readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false, }, nextSteps: [ { tool: "pica_shared_record_propose_change", reason: "Draft a change to one of these songs' split.", when: "on_success", }, ], }, executor: this.find.bind(this), }, ]; } async find(args) { const title = typeof args.title === "string" && args.title.trim() ? args.title.trim() : undefined; const limit = Number.isInteger(args.limit) ? Math.min(Math.max(args.limit, 1), 50) : undefined; try { const found = await this.pica.sharedRecords.findSongs(title, limit); return formatSuccess(found.length === 0 ? title ? `No shared song here matches "${title}".` : "This catalogue shares no songs with another catalogue." : `Found ${found.length} shared ${found.length === 1 ? "song" : "songs"}.${found.length >= (limit ?? 20) ? (limit ?? 20) >= 50 ? " There may be more: narrow it by title." : " There may be more: narrow it by title, or raise the limit (up to 50)." : ""}`, found); } catch (err) { const body = parseApiErrorBody(err); if (body.status !== undefined && body.status >= 400 && body.status < 500 && body.code && body.code !== "INTERNAL_ERROR") return typedError(body.code, body.message ?? `The search was refused (${body.code}).`); const errorMessage = err instanceof Error ? err.message : String(err); return typedError("SHARED_SONGS_FIND_FAILED", `Failed to find shared songs: ${errorMessage}`); } } async propose(args) { const id = args.shared_record_id; if (typeof id !== "string" || !UUID.test(id)) return typedError("SHARED_RECORD_ID_REQUIRED", "shared_record_id is the shared record's id."); if (args.side !== "composition" && args.side !== "master") return typedError("SIDE_REQUIRED", "side is composition or master."); if (args.side === "master" && (typeof args.isrc !== "string" || !args.isrc.trim())) return typedError("ISRC_REQUIRED", "A master change names the recording's ISRC."); const raw = args.parties; if (!Array.isArray(raw) || raw.length === 0 || raw.length > MAX_PARTIES) return typedError("PARTIES_REQUIRED", `parties is the whole split after the change: 1 to ${MAX_PARTIES} people.`); const parties = []; for (const p of raw) { if (!p || typeof p.name !== "string" || !p.name.trim() || typeof p.role !== "string" || !p.role.trim() || !Number.isInteger(p.share_bp) || p.share_bp < 0) return typedError("INVALID_PARTY", "Each person needs a name, a role and share_bp, a whole number of basis points."); parties.push({ name: p.name.trim(), role: p.role.trim(), share_bp: p.share_bp, ...(typeof p.person_id === "string" && p.person_id ? { person_id: p.person_id } : {}), ...(typeof p.ipi === "string" && p.ipi ? { ipi: p.ipi } : {}), ...(typeof p.isni === "string" && p.isni ? { isni: p.isni } : {}), }); } const sum = parties.reduce((n, p) => n + p.share_bp, 0); if (sum !== TOTAL_BP) return typedError("SPLIT_NOT_WHOLE", `The shares add up to ${(sum / 100).toFixed(2)}%, not 100%. Give the whole split after the change.`); const reason = typeof args.reason === "string" && args.reason.trim() ? args.reason.trim() : undefined; try { const draft = await this.pica.sharedRecords.proposeChange(id, { side: args.side, isrc: args.side === "master" ? args.isrc.trim() : null, parties, reason, }); const url = `${webBaseUrl()}/inspect/shared-records/${id}/changes/${draft.id}`; return formatSuccess(`Drafted. Nothing changes until an owner or admin of this catalogue sends it from ${url}, the other catalogue says yes, and everyone whose share changes confirms.`, { id: draft.id, status: draft.status, url }); } catch (err) { const body = parseApiErrorBody(err); if (body.status !== undefined && body.status >= 400 && body.status < 500 && body.code && body.code !== "INTERNAL_ERROR") return typedError(body.code, body.message ?? `The draft was refused (${body.code}).`); const errorMessage = err instanceof Error ? err.message : String(err); return typedError("SHARED_CHANGE_DRAFT_FAILED", `Failed to draft the change: ${errorMessage}`); } } } //# sourceMappingURL=shared-records.js.map