@withpica/mcp-server
Version:
MCP Server for withPICA — enables AI assistants to interact with your withPICA music catalog
250 lines • 10.8 kB
JavaScript
// Copyright (c) 2025-2026 Withpica Ltd. All rights reserved.
import { SUBMIT_FEEDBACK_WRITE_PROPERTIES, SUBMIT_FEEDBACK_REQUIRED, } from "@withpica/mcp-utils";
const VALID_CATEGORIES = ["bug_report", "feature_request", "question", "other"];
const FEEDBACK_STATUSES = ["open", "fixed", "wont_fix", "answered"];
const STATUS_TAG = {
open: "open",
fixed: "✓ fixed",
answered: "✓ answered",
wont_fix: "✗ won't fix",
};
/** "general_bug_report" → "bug report". */
function kindLabel(actionType) {
return actionType.replace(/^general_/, "").replace(/_/g, " ");
}
export class FeedbackTools {
pica;
constructor(pica) {
this.pica = pica;
}
getTools() {
return [
{
definition: {
name: "pica_submit_feedback",
scope: "write:feedback",
audiences: ["stdio", "http", "public"],
tier: "write",
description: "File a bug report, feature request, question, or general note " +
"without leaving the conversation. It goes to the withpica team. " +
"Subject is always the authenticated user; no user_id parameter " +
"accepted.",
workflows: "infrastructure",
vernacular_kind: "specialist",
vernacular_reason: "Specialist context; not yet covered by the Creator Question Atlas — pending telemetry-driven addition per ADR-226 Phase 4 substrate writes.",
inputSchema: {
type: "object",
properties: SUBMIT_FEEDBACK_WRITE_PROPERTIES,
required: SUBMIT_FEEDBACK_REQUIRED,
},
},
executor: this.submitFeedback.bind(this),
},
{
definition: {
name: "pica_feedback_list",
scope: "write:feedback",
audiences: ["stdio", "http", "public"],
tier: "read",
description: "List the feedback you have sent PICA (bug reports, feature " +
"requests, questions) and what the PICA team did about each: " +
"open, fixed, answered or won't fix, with the note they wrote " +
"for you. Closes the loop on `pica_submit_feedback` — call it " +
"when the user asks 'did they fix what I reported?' or before " +
"filing what may be the same report again. Newest first; pass " +
"`status` to narrow. Only your own reports in this " +
"organisation — never anyone else's.",
workflows: "infrastructure",
vernacular_kind: "specialist",
vernacular_reason: "Specialist context; not yet covered by the Creator Question Atlas — pending telemetry-driven addition per ADR-226 Phase 4 substrate writes.",
inputSchema: {
type: "object",
properties: {
status: {
type: "string",
enum: [...FEEDBACK_STATUSES],
description: "Optional. open | fixed | answered | wont_fix. Omit to see everything.",
},
limit: {
type: "number",
minimum: 1,
maximum: 100,
description: "Optional. Default 25, max 100.",
},
},
},
},
executor: this.listFeedback.bind(this),
},
];
}
async listFeedback(args) {
const status = FEEDBACK_STATUSES.includes(args.status)
? args.status
: undefined;
const limit = typeof args.limit === "number" && Number.isInteger(args.limit)
? Math.min(Math.max(args.limit, 1), 100)
: 25;
const result = await this.pica.feedback.list({ status, limit });
const items = Array.isArray(result?.items) ? result.items : [];
const summary = result?.summary ?? null;
const card = {
kind: "pica.card.feedback_list",
version: 1,
data: {
count: items.length,
status_filter: status ?? null,
summary,
reports: items.map((r) => ({
id: r.id,
kind: kindLabel(r.action_type),
sent_at: r.created_at,
comment: r.comment,
status: r.status,
note: r.resolution_note,
resolved_at: r.resolved_at,
})),
},
};
if (items.length === 0) {
return {
content: [
{
type: "text",
text: status
? `You have no ${status.replace("_", " ")} reports.`
: "You haven't sent PICA any feedback from this account yet.",
},
],
structuredContent: card,
};
}
const lines = items.slice(0, 10).map((r) => {
const words = (r.comment ?? "").replace(/\s+/g, " ").trim();
const said = words
? ` "${words.length > 80 ? `${words.slice(0, 80)}…` : words}"`
: "";
const note = r.status !== "open" && r.resolution_note
? ` — PICA: ${r.resolution_note}`
: "";
return `[${STATUS_TAG[r.status] ?? r.status}] ${kindLabel(r.action_type)}${said}${note}`;
});
const more = items.length > 10
? `\n\n_+${items.length - 10} more — pass a higher limit to see them._`
: "";
const headline = summary && summary.recentlyClosed > 0
? `${summary.recentlyClosed} of your reports ${summary.recentlyClosed === 1 ? "was" : "were"} closed in the last ${summary.sinceDays} days; ${summary.open} still open.\n\n`
: "";
return {
content: [
{ type: "text", text: `${headline}${lines.join("\n")}${more}` },
],
structuredContent: card,
};
}
async submitFeedback(args) {
const category = args.category;
const description = args.description;
if (!category) {
return {
content: [{ type: "text", text: "Error: category is required." }],
isError: true,
};
}
if (!VALID_CATEGORIES.includes(category)) {
return {
content: [
{
type: "text",
text: `Error: category must be one of ${VALID_CATEGORIES.join(", ")}.`,
},
],
isError: true,
};
}
if (typeof description !== "string" || description.trim().length === 0) {
return {
content: [{ type: "text", text: "Error: description is required." }],
isError: true,
};
}
// ADR-184 rule 2: reject caller-supplied user_id defensively. The
// subject is always the authenticated session/API-key user.
const sentiment = args.sentiment === "positive" || args.sentiment === "negative"
? args.sentiment
: category === "bug_report"
? "negative"
: "positive";
try {
const result = await this.pica.feedback.submit({
actionType: `general_${category}`,
sentiment,
comment: description.trim(),
portal: "mcp",
});
const data = result?.data || result;
const id = data?.id ?? "unknown";
return {
content: [
{
type: "text",
text: `Feedback submitted as ${category.replace("_", " ")}; ` +
`it has gone to the withpica team. Reference ID: ${id}.`,
},
],
structuredContent: {
kind: "pica.card.feedback_submitted",
version: 1,
data: {
id,
category,
sentiment,
},
},
};
}
catch (e) {
// Surface the underlying error to the agent and to the audit log.
// Prior behaviour returned a generic neutral string which caused
// mcp_audit_log rows with error_code=null / error_message=null,
// making triage impossible (ops_issue 808e5fa0, 2026-05-15).
//
// We still report to ops_issues (fire-and-forget) for visibility,
// but the agent now receives the real error message so it can
// decide whether to retry, adjust the payload, or escalate.
//
// structuredContent.error_code + .error_message are read by the
// audit-logger (index.ts:1891-1895) to populate mcp_audit_log.
const msg = e instanceof Error ? e.message : String(e);
const status = e?.status;
const errorCode = status ? `HTTP_${status}` : "UNKNOWN";
void this.pica.opsIssues
.reportIssue({
kind: "tool_failure",
summary: `pica_submit_feedback failed${status ? ` (${status})` : ""}: ${msg}`,
context: {
tool_name: "pica_submit_feedback",
error_code: errorCode,
parameters_shape: Object.keys(args).sort().join(","),
},
})
.catch(() => {
// never block on the ops report — it's fire-and-forget
});
return {
content: [
{
type: "text",
text: `Error submitting feedback: ${msg}`,
},
],
structuredContent: {
error_code: errorCode,
error_message: msg,
},
isError: true,
};
}
}
}
//# sourceMappingURL=feedback.js.map