@withpica/mcp-server
Version:
MCP Server for withPICA — enables AI assistants to interact with your withPICA music catalog
77 lines • 3.01 kB
JavaScript
// Copyright (c) 2025-2026 Withpica Ltd. All rights reserved.
const label = (c) => c.name ?? c.id;
/** The one line a switched connection's every answer ends with. */
export function switchedLine(note) {
return note.switchedFrom
? `Working in ${label(note)} (switched from ${label(note.switchedFrom)}).`
: null;
}
/**
* The switch tools answer with where the connection works AFTER the call;
* the request's note was made before it. Read their own answer instead, so a
* switch is never stamped with the catalogue it just left.
*/
function noteFromAnswer(r) {
const data = r.structuredContent?.data;
const here = data?.working_catalogue;
if (!here || typeof here.id !== "string")
return null;
return {
id: here.id,
name: here.name ?? null,
...(data?.switched && data.connected_catalogue
? { switchedFrom: data.connected_catalogue }
: {}),
};
}
/**
* A result whose text block IS its structured content as JSON (search and
* fetch, which OpenAI requires to say the same in both) gets the stamped
* JSON in its text too, so the two stay equal. Any other text is left as it
* is. Returns a new array; the original blocks are not changed.
*/
function sameJsonInText(content, before, after) {
if (!content)
return content;
const compact = JSON.stringify(before);
const pretty = JSON.stringify(before, null, 2);
return content.map((c) => c.type === "text" && c.text === compact
? { ...c, text: JSON.stringify(after) }
: c.type === "text" && c.text === pretty
? { ...c, text: JSON.stringify(after, null, 2) }
: c);
}
export function withWorkingCatalogue(result, requestNote) {
if (!requestNote || !result || typeof result !== "object")
return result;
const r = result;
if (r._meta && "working_catalogue" in r._meta)
return result;
const note = noteFromAnswer(r) ?? requestNote;
const ref = { id: note.id, name: note.name };
const next = {
...r,
_meta: { ...(r._meta ?? {}), working_catalogue: ref },
};
const sc = r.structuredContent;
if (sc && typeof sc === "object" && !Array.isArray(sc)) {
const stamped = { ...sc, working_catalogue: ref };
next.structuredContent = stamped;
next.content = sameJsonInText(r.content, sc, stamped);
}
const line = switchedLine(note);
if (line) {
next.content = [...(next.content ?? []), { type: "text", text: line }];
}
return next;
}
/**
* The arguments a confirmation token is bound to (D3): a token made in a
* switched catalogue carries that catalogue, so it cannot be confirmed after
* switching to another, or back. A connection that has not switched hashes
* exactly the arguments it always did.
*/
export function confirmationArgs(args, note) {
return note?.switchedFrom ? { ...args, __working_catalogue: note.id } : args;
}
//# sourceMappingURL=working-catalogue-answer.js.map