UNPKG

@withpica/mcp-server

Version:

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

165 lines • 7.32 kB
// Copyright (c) 2025-2026 Withpica Ltd. All rights reserved. export const CATALOGUE_ARG = "catalogue"; /** Scopes whose tools act on a catalogue's own records. */ export const CATALOGUE_SCOPES = new Set([ "read:catalog", "write:catalog", "read:people", "write:people", "read:agreements", "write:agreements", "read:files", "write:files", "read:analytics", ]); /** Tools in those scopes that must not act for a client, and why. */ export const CATALOGUE_EXCLUDED_TOOLS = new Set([ // ADR-352 D4: the host calls these with exactly {query} and {id}; an extra // argument breaks the contract they are named for. "search", "fetch", // Its route (/api/mcp/share-send) sends for the credential's own catalogue // and does not read the catalogue header. "pica_share_send", // They manage who looks after a catalogue: acting "as" a client through // them would let an organisation answer or widen its own relationship. "pica_relationships_list", "pica_relationships_answer", "pica_relationships_grant", "pica_relationships_revoke", "pica_relationships_activity", "pica_relationships_step_down", "pica_portfolio_list", "pica_portfolio_briefing", "pica_portfolio_invite", // Hand-overs (ADR-333 D5) act for the organisation that builds the // catalogue, or point the client at a screen; never "as" a client. "pica_handover_create", "pica_handover_send", "pica_handover_answer", // ADR-356: they change or read the connection's own working catalogue; // "as" a client, a switch would move the connection, not act for anyone. "pica_catalogue_current", "pica_catalogue_switch", // ADR-366 §4: the caller's own payouts, a person's money and no // catalogue's, so there is no client to act for (and its route, // /api/admin/payouts/status, reads the credential owner only). "pica_payouts_status", ]); export const CATALOGUE_PROPERTY = { type: "string", format: "uuid", description: "Act for a client catalogue your organisation looks after: its id from pica_portfolio_list. " + "Leave out to act for your own catalogue. What you may do is set by that relationship; a call " + "outside it is refused with the reason.", }; /** * The same argument as ChatGPT's curated list shows it (ADR-352, item 9 * 2026-11-13): pica_portfolio_list is not on that list, so this wording names * no tool. Pinned by openai-curated-surface.test.ts ("names only tools on * the curated list"). */ export const CATALOGUE_PROPERTY_OPENAI = { ...CATALOGUE_PROPERTY, description: "Act for a client catalogue your organisation looks after: that catalogue's id. " + "Leave out to act for your own catalogue. What you may do is set by that relationship; a call " + "outside it is refused with the reason.", }; const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; export function takesCatalogue(definition) { return (definition.scope !== undefined && CATALOGUE_SCOPES.has(definition.scope) && !CATALOGUE_EXCLUDED_TOOLS.has(definition.name)); } /** * Why a `catalogue` value cannot be used for this tool, in plain words, or * null when it can. A value is never silently ignored: a tool that cannot act * for a client refuses rather than acting for the caller's own catalogue. */ export function catalogueRefusal(definition, value) { if (value === undefined) return null; if (!takesCatalogue(definition)) { return `${definition.name} can't act for a client catalogue. Call it without "catalogue" to use your own.`; } if (typeof value !== "string" || !UUID.test(value)) { return `"catalogue" must be a catalogue id from pica_portfolio_list.`; } return null; } /** * What to say when `catalogue` names one the organisation has no relationship * with (the app's no_relationship, lib/services/portfolio-relationship-access.ts). * The likely case is a catalogue the user is BUILDING for a client: before * it is handed over there is no relationship, and the builder works in it as * a member: by switching the connection to it (pica_catalogue_switch, ADR-356) * or by a connection bound to it (pica_portfolio_list lists those under * building). */ export const NOT_LOOKED_AFTER_HINT = "Your organisation doesn't look after that catalogue through a relationship. " + "If it is one you're building for a client (pica_portfolio_list shows those under building), " + "`catalogue` can't reach it: switch to it with pica_catalogue_switch, or, where the connection can't switch (ChatGPT), reconnect and choose it on the connect screen. " + "Otherwise, check the id with pica_portfolio_list."; /** * A tool's answer for a call made in a client catalogue, checked against what * the app actually served (the SDK's CatalogueCallReport). * * Many tools fail their parts soft — an inspect tool turns each refused * section into a null or an error field and still answers "success". In a * client catalogue most routes are not opened (lib/auth/catalogue-routes.ts), * so such a tool could answer "success" with nothing in it. Here: * - every request refused and none answered → a plain refusal naming them; * - some refused → the answer stands, with a line naming what was not * available, so nothing is left out silently; * - a call that refused nothing is returned unchanged; an answer that is * already an error is returned as it is, plus NOT_LOOKED_AFTER_HINT when a * refusal was no_relationship. * Pinned by mcp-server/src/__tests__/tools/catalogue-argument.test.ts. */ export function settleCatalogueAnswer(toolName, answer, report) { if (!answer || report.refused.length === 0) return answer; const paths = [...new Set(report.refused.map((r) => r.path))]; const reasons = [ ...new Set(report.refused.map((r) => r.reason).filter(Boolean)), ]; const notLookedAfter = reasons.includes("no_relationship"); if (answer.isError) { if (!notLookedAfter) return answer; return { ...answer, content: [ ...(answer.content ?? []), { type: "text", text: NOT_LOOKED_AFTER_HINT }, ], }; } if (report.answered === 0) { return { content: [ { type: "text", text: JSON.stringify({ error: "RELATIONSHIP_REFUSED", message: `${toolName} could not be done in this client's catalogue: every part of it was refused (${paths.join(", ")}).`, reason_codes: reasons, ...(notLookedAfter ? { suggestion: NOT_LOOKED_AFTER_HINT } : {}), }, null, 2), }, ], isError: true, }; } return { ...answer, content: [ ...(answer.content ?? []), { type: "text", text: `Not available in this client's catalogue, so left out: ${paths.join(", ")}${reasons.length ? ` (${reasons.join(", ")})` : ""}.`, }, ], }; } //# sourceMappingURL=catalogue-argument.js.map