UNPKG

@withpica/mcp-server

Version:

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

104 lines • 4.16 kB
// Copyright (c) 2025-2026 Withpica Ltd. All rights reserved. /** * `view_url` on the tools that name one record (ADR-362 §2): the write tools * that create or change it, and the reads that inspect it. Added in one place, * the registry (ToolRegistry, after the executor), so every such answer links * the same way: `structuredContent.view_url`, and the same URL on its own line * in the text answer (`open it in pica: <url>`, e1's copy). * * The id is the record the call named (VIEW_URL_TOOLS `args`), else the one * the answer returns (`data.id`: a create, or an inspect by ISWC or ISRC). A call naming several * records, or none, gets no link: a list is a saved view (slice 3). */ import { viewUrl, viewUrlLine, ViewUrlError, } from "../lib/view-url.js"; export const VIEW_URL_TOOLS = { pica_works_create: { kind: "work" }, pica_works_update: { kind: "work", args: ["id"] }, pica_works_inspect: { kind: "work", args: ["id", "work_id"] }, pica_credits_update: { kind: "work", args: ["work_id"] }, pica_people_create: { kind: "person" }, pica_people_update: { kind: "person", args: ["id"] }, pica_people_inspect: { kind: "person", args: ["id"] }, pica_recordings_create: { kind: "recording" }, pica_recordings_update: { kind: "recording", args: ["id"] }, pica_recordings_inspect: { kind: "recording", args: ["id", "recording_id"], }, pica_releases_create: { kind: "release" }, pica_releases_update: { kind: "release", args: ["id"] }, pica_releases_inspect: { kind: "release", args: ["id", "release_id"] }, pica_agreements_create: { kind: "agreement" }, pica_agreements_update: { kind: "agreement", args: ["id"] }, pica_agreements_inspect: { kind: "agreement", args: ["id"] }, }; /** The answer's own record id: `data.id` (formatSuccess) or a top-level `id`. */ function answerId(result) { const sc = result.structuredContent; return sc?.data?.id ?? sc?.id; } /** Put `url` on an answer: `structuredContent.view_url` and its text line. */ function stamp(result, url) { result.structuredContent = { ...(result.structuredContent ?? {}), view_url: url, }; const text = result.content?.find((c) => c.type === "text" && typeof c.text === "string"); if (text && !text.text.includes(url)) { // On its own line after the message, before any JSON that follows it // (formatSuccess writes `${message}\n\n${json}`). const cut = text.text.indexOf("\n\n{"); text.text = cut === -1 ? `${text.text}\n\n${viewUrlLine(url)}` : `${text.text.slice(0, cut)}\n\n${viewUrlLine(url)}${text.text.slice(cut)}`; } } /** * For a tool outside VIEW_URL_TOOLS that names one record in its answer * (an agreement sent for signature, a sample logged on a recording): the * same `view_url` and line. Returns the result; an id that isn't a UUID * leaves it unlinked. */ export function linkTo(result, ref) { try { stamp(result, viewUrl(ref)); } catch (err) { if (!(err instanceof ViewUrlError)) throw err; } return result; } /** * Add `view_url` to a successful answer of a VIEW_URL_TOOLS tool. Never * throws: an id that isn't a UUID, or an answer with no record, gets no link. */ export function attachViewUrl(result, toolName, args) { const rule = VIEW_URL_TOOLS[toolName]; if (!rule || !result || result.isError) return; if (Array.isArray(args.ids)) return; const named = rule.args ?.map((a) => args[a]) .find((v) => typeof v === "string"); const id = named ?? answerId(result); if (typeof id !== "string") return; let url; try { url = viewUrl({ entity: { kind: rule.kind, id } }); } catch (err) { if (err instanceof ViewUrlError) return; throw err; } // A confirm step (confirmation_required) saved nothing yet: no link. const sc = result.structuredContent; if (sc?.status === "confirmation_required") return; stamp(result, url); } //# sourceMappingURL=view-url-results.js.map