UNPKG

@withpica/mcp-server

Version:

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

217 lines • 8.24 kB
// Copyright (c) 2025-2026 Withpica Ltd. All rights reserved. /** * The one way an MCP tool links into PICA (ADR-362 §2, founder yes * 2026-11-21). Every link a tool returns comes from `viewUrl`, so: * * - the host is the environment's web origin (`webBaseUrl`), never a * hardcoded one. The hosted route (app/api/mcp/route.ts) passes the origin * for links a person opens, via `setWebOrigin(linkOrigin())`, so a preview * links to its public alias (staging), never production; stdio reads * PICA_API_URL. Pinned by * src/__tests__/lib/view-url.test.ts and, for the route, * app/api/mcp/__tests__/view-url-host.test.ts; * - a link names only things from a fixed list (below) and UUIDs. Free text * (titles, names, search words, notes) never goes in a URL (ADR-362 §1); * a set or a note becomes a saved view (`{ saved_view_id }`, slice 3). * * A name or filter not in these tables does not compile; a gap code or id * that isn't one throws, rather than build a guessed path. * src/__tests__/lib/view-url.test.ts fails on a hand-built * `withpica.com/inspect` link in mcp-server/src/tools or the skills. * * Pure (no SDK, no I/O). The app holds the lists it shares with this file * equal through lib/services/__tests__/view-url-parity.test.ts. */ const DEFAULT_PICA_API_URL = "https://withpica.com/api"; /** Set by the hosted route; null in stdio, which reads PICA_API_URL. */ let configuredOrigin = null; /** * The web origin a person opens links on. The hosted route calls it once per * request with `linkOrigin()`, which is env-only and so the same value for * every request in one deployment: module state cannot carry one caller's * host to another. Without it, a deployment with PICA_API_URL unset linked * to production (2026-11-23, holdout saved-view-links run 45176451830). */ export function setWebOrigin(origin) { configuredOrigin = origin ? origin.replace(/\/+$/, "") : null; } /** * The user-facing web origin: the hosted route's origin first, then * PICA_API_URL without its `/api` (`https://withpica.com/api` → * `https://withpica.com`), then production. */ export function webBaseUrl() { if (configuredOrigin) return configuredOrigin; const apiUrl = process.env.PICA_API_URL || DEFAULT_PICA_API_URL; const trimmed = apiUrl.replace(/\/api\/?$/, ""); return trimmed || apiUrl; } /** The record pages, by kind (an audio file opens its work). */ export const ENTITY_PATHS = { work: "works", person: "people", recording: "recordings", release: "releases", agreement: "agreements", }; /** * The places on a record page a link may open, by kind: a sub-page, an * anchor, or both. Each target exists (app/inspect/works/[id]/page.tsx * #human-made, …/documents, SignOwnLine #sign-sheet; recordings/[id] * #ownership-splits). */ export const ENTITY_SECTIONS = { work: { "human-made": "#human-made", documents: "/documents", "sign-sheet": "/documents#sign-sheet", }, person: {}, recording: { "ownership-splits": "#ownership-splits" }, release: {}, agreement: {}, }; /** The catalogue's tabs (app/inspect/components/CatalogView.tsx CATALOG_TABS). */ export const CATALOGUE_TABS = [ "works", "recordings", "releases", "projects", "people", "audio", "documents", ]; /** * The 10 gap codes. Mirrors lib/services/integrity-gap-codes.ts * INTEGRITY_GAP_CODES (mcp <-> lib isolation, duplicated by hand like * tools/integrity.ts GAP_CODES); lib/services/__tests__/view-url-parity.test.ts * holds the two equal. */ export const GAP_CODES = [ "WORK_NO_RECORDING", "RECORDING_NO_WORK", "RECORDING_NO_RELEASE", "WORK_NO_WRITER", "RECORDING_NO_OWNER", "COLLABORATOR_NOT_INVITED", "COLLABORATOR_NOT_ATTESTED", "WORK_NO_MASTER", "WORK_SPLITS_ABSENT", "WORK_SPLITS_INVALID", ]; /** The documents tab's filters (app/inspect/lib/document-kinds.ts). */ export const DOCUMENTS_SHOW = ["agreements", "split-sheets"]; export class ViewUrlError extends Error { constructor(message) { super(message); this.name = "ViewUrlError"; } } const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; /** A saved view's id: random, url-safe, ~22 characters (ADR-362 §1). */ const SAVED_VIEW_ID = /^[A-Za-z0-9_-]{16,64}$/; function requireUuid(what, value) { if (typeof value !== "string" || !UUID.test(value)) { throw new ViewUrlError(`${what} must be a UUID`); } return value.toLowerCase(); } function requireOneOf(what, list, value) { if (typeof value !== "string" || !list.includes(value)) throw new ViewUrlError(`${what} must be one of: ${list.join(", ")}`); return value; } /** The path and query of a view, without the host. */ export function viewPath(ref, options = {}) { if ("entity" in ref) { const { kind, id, section } = ref.entity; const path = ENTITY_PATHS[requireOneOf("entity kind", Object.keys(ENTITY_PATHS), kind)]; const base = `/inspect/${path}/${requireUuid("entity id", id)}`; if (section === undefined) return base; const sections = ENTITY_SECTIONS[kind]; const suffix = Object.prototype.hasOwnProperty.call(sections, section) ? sections[section] : null; if (!suffix) throw new ViewUrlError(`${kind} has no section "${String(section)}"`); return base + suffix; } if ("saved_view_id" in ref) { if (!SAVED_VIEW_ID.test(ref.saved_view_id)) throw new ViewUrlError("saved_view_id is not a saved view id"); return `/inspect?view=${ref.saved_view_id}`; } if (ref.view.name !== "catalogue") throw new ViewUrlError(`unknown view "${String(ref.view.name)}"`); const f = ref.view.filters ?? {}; const q = new URLSearchParams(); // The gap picks its own tab on the page (gap-filter.ts), so a gap link // names it too: the address reads the same either way. if (f.tab !== undefined) q.set("tab", requireOneOf("tab", CATALOGUE_TABS, f.tab)); if (f.gap !== undefined) q.set("gap", requireOneOf("gap", GAP_CODES, f.gap)); if (f.project !== undefined) q.set("project", requireUuid("project", f.project)); for (const s of f.show ?? []) q.append("show", requireOneOf("show", DOCUMENTS_SHOW, s)); if (options.fromAi) q.set("from", "ai"); const qs = q.toString(); return qs ? `/inspect?${qs}` : "/inspect"; } /** The full link, on this environment's host. */ export function viewUrl(ref, options = {}) { return `${webBaseUrl()}${viewPath(ref, options)}`; } /** The text answer's line for a link (e1's copy, ADR-362 §2). */ export function viewUrlLine(url) { return `open it in pica: ${url}`; } /** `viewUrl`, or null when the ref names something that isn't a view. */ export function safeViewUrl(ref, options = {}) { try { return viewUrl(ref, options); } catch (err) { if (err instanceof ViewUrlError) return null; throw err; } } /** * The tab each gap code's rows live on. Mirrors lib/services/ * integrity-gap-codes.ts ENTITY_TYPE_FOR_CODE through app/inspect/lib/ * gap-filter.ts tabForEntityType (held equal by view-url-parity.test.ts). */ export const GAP_TAB = { WORK_NO_RECORDING: "works", RECORDING_NO_WORK: "recordings", RECORDING_NO_RELEASE: "recordings", WORK_NO_WRITER: "works", RECORDING_NO_OWNER: "recordings", COLLABORATOR_NOT_INVITED: "people", COLLABORATOR_NOT_ATTESTED: "people", WORK_NO_MASTER: "works", WORK_SPLITS_ABSENT: "works", WORK_SPLITS_INVALID: "works", }; /** * The catalogue narrowed to one gap, as an AI issues it (`from=ai`), on its * tab: the same address /inspect's attention row links to. */ export function gapViewUrl(code, project) { return viewUrl({ view: { name: "catalogue", filters: { tab: GAP_TAB[requireOneOf("gap", GAP_CODES, code)], gap: code, ...(project ? { project } : {}), }, }, }, { fromAi: true }); } //# sourceMappingURL=view-url.js.map