UNPKG

@withpica/mcp-server

Version:

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

129 lines • 6.96 kB
// Copyright (c) 2025-2026 Withpica Ltd. All rights reserved. /** * ADR-356 — switch which of your catalogues a connection works in. * * pica_catalogue_current reads where the connection works; pica_catalogue_switch * changes it. Both call /api/admin/catalogues/** (PICA's * lib/services/catalogue-switch.ts), which holds the rules: the switch is * stored on the server against the connection, only for catalogues the person * belongs to, re-checked on every call, and only on a connection given * "switch between the catalogues you belong to" on the connect screen. These * tools pass a refusal on as PICA words it. * * Neither is on ChatGPT's curated list (no `hosts`, ADR-356 D7): a ChatGPT * connection is never given the grant, so it stays in its own catalogue. */ import { ApiError } from "@withpica/mcp-sdk"; import { formatSuccess } from "@withpica/mcp-utils"; import { typedError } from "./error-envelope.js"; import { parseApiErrorBody } from "./api-error-body.js"; const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; /** A refusal PICA worded (4xx), passed on; anything else is thrown. */ function refusal(err) { if (!(err instanceof ApiError) || err.status >= 500 || err.status === 0) throw err; const body = parseApiErrorBody(err); return typedError((body.code ?? `HTTP_${err.status}`).toUpperCase(), body.message ?? "PICA refused this.", { nothing_written: true, retry_safe: false }); } function label(c) { return c.name ?? c.id; } function summary(current) { if (current.ended) { const others = current.switchable.filter((c) => c.id !== current.ended?.id); return (`This connection was switched to ${label(current.ended)}, but you're no longer a member of it, so it refuses every request until you choose where to work. ` + (others.length ? `Switch with pica_catalogue_switch to: ${others.map((c) => `${label(c)} (${c.id})${c.own ? " — your own" : ""}`).join(", ")}.` : `Switch back to your own catalogue (${current.connected_catalogue.id}) with pica_catalogue_switch.`)); } const here = label(current.working_catalogue); const own = label(current.connected_catalogue); const where = current.switched ? `Working in ${here} (switched from ${own}). Every call now works in ${here} until you switch back.` : `Working in ${here}, the catalogue this connection was connected to.`; if (!current.may_switch) return `${where} ${current.why_not ?? ""}`.trim(); const others = current.switchable.filter((c) => !c.current); return others.length ? `${where} Can switch to: ${others.map((c) => `${label(c)} (${c.id})`).join(", ")}.` : `${where} There are no other catalogues to switch to.`; } export class CatalogueSwitchTools { pica; constructor(pica) { this.pica = pica; } getTools() { return [ { definition: { name: "pica_catalogue_current", scope: "read:catalog", audiences: ["http", "public"], tier: "read", description: "Use when the user asks: 'which catalogue am I in?', 'whose catalogue are you working on?', 'what catalogues can I switch to?'. " + "Says which catalogue this connection works in now, which one it was connected to, whether it can switch (and if not, what the person does instead, e.g. reconnect once), " + "and the catalogues it can switch to: each one the person belongs to, including a catalogue they are building for a client. Switch with pica_catalogue_switch. Read-only.", workflows: "infrastructure", vernacular_kind: "creator-entry", inputSchema: { type: "object", properties: {}, required: [] }, }, executor: this.current.bind(this), }, { definition: { name: "pica_catalogue_switch", scope: "read:catalog", audiences: ["http", "public"], tier: "write", description: "Use when the user asks: 'work on Joseph's catalogue', 'switch to my label's catalogue', 'switch back to mine', 'open the catalogue I'm building'. " + "Sets which of the person's catalogues this connection works in. Every later call (search, works, people, imports, everything) then works in that catalogue until switched back, and every answer says which catalogue it was done in. " + "Only a catalogue the person belongs to (pica_catalogue_current lists them, with ids); pass the connection's own catalogue to switch back. " + "The target catalogue's own rules apply in full, e.g. nothing in a catalogue being built is published or signed for the builder. " + "Before switching, finish or drop anything waiting for a confirmation: a confirmation is tied to the catalogue it was made in. " + "It changes no catalogue data: it only sets where this connection works, and every later call is still checked against the person's role in that catalogue.", workflows: "infrastructure", vernacular_kind: "creator-entry", annotations: { idempotentHint: true }, inputSchema: { type: "object", properties: { catalogue_id: { type: "string", format: "uuid", description: "The catalogue to work in, an id from pica_catalogue_current. The connection's own catalogue switches back.", }, }, required: ["catalogue_id"], }, }, executor: this.switchTo.bind(this), }, ]; } async current() { let current; try { current = await this.pica.catalogues.current(); } catch (err) { return refusal(err); } return formatSuccess(summary(current), { ...current }); } async switchTo(args) { const id = args.catalogue_id; if (typeof id !== "string" || !UUID.test(id)) { return typedError("VALIDATION_ERROR", "catalogue_id must be a catalogue id from pica_catalogue_current.", { nothing_written: true, retry_safe: true }); } let current; try { current = await this.pica.catalogues.switch(id); } catch (err) { return refusal(err); } return formatSuccess(summary(current), { ...current }); } } //# sourceMappingURL=catalogue-switch.js.map