UNPKG

@withpica/mcp-server

Version:

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

230 lines • 11.3 kB
// Copyright (c) 2025-2026 Withpica Ltd. All rights reserved. /* eslint-disable @typescript-eslint/no-explicit-any -- ToolExecutor signature uses Record<string, any> */ /** * The in-chat split card (ADR-152 amendment 2026-11-11, slice 2b). * * `pica_split_sheet_card` shows who has signed a song's latest split sheet, * in a card (`ui://pica/split`). From the card the user can: * - sign their own share: one tap opens the song's documents page on PICA, * signed in, where "sign your line" is. Signing is the person's own * answer, so it is never made from the chat (founder, option A, 11-11). * - send someone their link again: the card asks yes first, naming the * person, then calls `pica_split_sheet_remind` for that one person. * * `pica_split_sheet_remind` takes ids only: the email goes to the address on * the person's record, never one passed in. One email per person a day and * 60 an hour per catalogue (sendToSigner, lib/services/split-sheet). * Every client without cards gets the same facts as text. */ import { safeViewUrl } from "../lib/view-url.js"; import { deriveWebBaseUrl } from "./app-tools.js"; const CARD_TOOL = "pica_split_sheet_card"; const REMIND_TOOL = "pica_split_sheet_remind"; export const SPLIT_CARD_URI = "ui://pica/split"; const STATE_WORDS = { signed: "signed", sent: "sent, not signed yet", not_sent: "not sent yet", declined: "said no", expired: "link expired", }; const OUTCOME_WORDS = { sent: "sent", held: "waiting for approval before it's sent", failed: "couldn't send", no_email: "no email on record", reminded_recently: "emailed in the last day, not sent again", too_many: "too many emails sent this hour, try again later", }; // The ids go into a URL path (the SDK's routes, and the card's sign link), // so only a UUID is accepted: "../x" can't steer either (pica-8b, 11-12). const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; const isId = (v) => typeof v === "string" && UUID_RE.test(v); export class SplitCardTools { pica; constructor(pica) { this.pica = pica; } getTools() { return [ { definition: { name: CARD_TOOL, scope: "read:catalog", audiences: ["stdio", "http"], // ADR-352 amendment 2026-11-12: on ChatGPT's curated list. hosts: ["openai"], tier: "read", description: "Use when the user asks: 'who has signed the split sheet for X?', " + "'has everyone signed?', 'remind them to sign'. Shows each person " + "on a work's latest split sheet, their share, and whether they " + "have signed. Call it directly: Claude's chat apps and ChatGPT " + "show it as a card, where the user can send someone their signing " + "link, or send it again (the card asks the user to say yes first) and open the work on PICA " + "to sign their own share. Every other client gets the same list " + "as text. Signing is always the person's own act on PICA, never " + "done from the chat: say 'sent', never 'signed', for someone you " + "reminded. → then: pica_split_sheet_remind (send one person their " + "link again), pica_split_sheet_generate (when the work has no " + "split sheet yet)", // ChatGPT's curated list carries neither tool named above, so its // text points at the card's own button and the PICA link instead. hostDescriptions: { openai: "Use when the user asks: 'who has signed the split sheet for X?', " + "'has everyone signed?', 'remind them to sign'. Shows each person " + "on a work's latest split sheet, their share, and whether they " + "have signed, as a card. On the card the user can send someone " + "their signing link, or send it again (the card asks the user to say yes first) and open the " + "work on PICA. Signing is always the person's own act on PICA, " + "never done from the chat: say 'sent', never 'signed', for " + "someone the card reminded. With no split sheet yet, give the " + "user the PICA link in the result to make one.", }, workflows: ["split-sheet-required"], vernacular_kind: "creator-entry", inputSchema: { type: "object", properties: { work_id: { type: "string", description: "The song." }, }, required: ["work_id"], }, annotations: { title: "show who has signed a song's split sheet", readOnlyHint: true, }, _meta: { ui: { resourceUri: SPLIT_CARD_URI } }, }, executor: this.show.bind(this), }, { definition: { name: REMIND_TOOL, scope: "write:catalog", audiences: ["stdio", "http"], tier: "write", description: "Send one person on a split sheet their signing link again, by " + "email to the address on their person record. Use when the user " + "asks: 'remind Mary to sign', 'send Jo the split sheet again'. " + "One email per person a day and 60 an hour per catalogue; a " + "signed, declined or expired request is refused (make a new " + "sheet). Sending never signs anything: say 'sent', never " + "'signed'. Ask the user before sending. → then: " + "pica_split_sheet_card (see who has signed)", workflows: ["split-sheet-required"], vernacular_kind: "creator-entry", inputSchema: { type: "object", properties: { work_id: { type: "string", description: "The song." }, split_sheet_id: { type: "string", description: "The split sheet, from pica_split_sheet_card.", }, signature_id: { type: "string", description: "The person's signing request on that sheet, from pica_split_sheet_card (signature_id).", }, }, required: ["work_id", "split_sheet_id", "signature_id"], }, annotations: { title: "send one person their split sheet link again", readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: true, }, }, executor: this.remind.bind(this), }, ]; } async show(args) { const workId = args?.work_id; if (!isId(workId)) { return errorResult("work_id is required. Find the song with pica_works_query."); } let status; try { status = await this.pica.splitSheets.signingStatus(workId); } catch { return errorResult("couldn't read who has signed on this work. Check the id with pica_works_query."); } const docsUrl = safeViewUrl({ entity: { kind: "work", id: workId, section: "documents" }, }) ?? `${deriveWebBaseUrl()}/inspect`; const signUrl = safeViewUrl({ entity: { kind: "work", id: workId, section: "sign-sheet" }, }) ?? docsUrl; const people = status?.people ?? []; const sheet = status?.sheet ?? null; let text; if (!sheet) { text = `This work has no split sheet yet. Make one on PICA: ${docsUrl}.`; } else if (people.length === 0) { text = `This work's split sheet has nobody on it to sign yet. See it on PICA: ${docsUrl}.`; } else { const signed = people.filter((p) => p.state === "signed").length; text = `${signed} of ${people.length} signed. ` + people .map((p) => `${p.name}${p.share != null ? ` (${p.share}%)` : ""}: ${personWords(p)}`) .join(". ") + `. To sign their own share, the user opens the work on PICA: ${signUrl}.`; } return { content: [{ type: "text", text }], structuredContent: { work_id: workId, sign_url: signUrl, sheet, people, }, }; } async remind(args) { const workId = args?.work_id; const sheetId = args?.split_sheet_id; const signatureId = args?.signature_id; if (!isId(workId) || !isId(sheetId) || !isId(signatureId)) { return errorResult("work_id, split_sheet_id and signature_id are required. Read them with pica_split_sheet_card."); } let out; try { out = await this.pica.splitSheets.sendToSigner(workId, sheetId, signatureId); } catch { return errorResult("couldn't send it. The request may be signed, declined or expired; check with pica_split_sheet_card."); } const who = out.name || "They"; // PICA's send failed: one more try, then the PICA link, so the model // does not keep retrying a send that keeps failing (e1). const text = Object.hasOwn(OUTCOME_WORDS, out.outcome) && out.outcome !== "failed" ? `${who}: ${OUTCOME_WORDS[out.outcome]}.` : `${who}: Couldn't send. Try once more; if it fails again, give the user the PICA link: ` + `${safeViewUrl({ entity: { kind: "work", id: workId, section: "sign-sheet" } }) ?? `${deriveWebBaseUrl()}/inspect`}.`; return { content: [{ type: "text", text }], structuredContent: { signature_id: signatureId, name: out.name, outcome: out.outcome, }, }; } } function personWords(p) { if (p.state === "sent" || p.state === "not_sent") { if (!p.has_email) return `${STATE_WORDS[p.state]}, no email on record`; } return STATE_WORDS[p.state]; } function errorResult(text) { return { content: [{ type: "text", text }], isError: true }; } //# sourceMappingURL=split-card.js.map