UNPKG

@withpica/mcp-server

Version:

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

240 lines • 14.4 kB
// Copyright (c) 2025-2026 Withpica Ltd. All rights reserved. /** * Statements Tools - MCP tool for agent-driven royalty/streaming statement ingestion. * ADR-277: human-approval write surface for financial line-item extraction. */ import { MONEY_READ_NOTE, MONEY_WRITE_NOTE } from "./money-records-copy.js"; import { formatSuccess } from "@withpica/mcp-utils"; export class StatementsTools { pica; constructor(pica) { this.pica = pica; } getTools() { return [ { definition: { name: "pica_statements_pending", scope: "read:finances", audiences: ["stdio", "http", "public"], tier: "read", description: "List uploaded royalty/streaming statement documents the user hasn't imported yet. For each, read it with pica_documents_inspect, extract the line items, and import with pica_statement_ingest_document. Returns lean summaries (id, title, parsing_status) — no financial figures. Statements can't be emailed into PICA: if the user has a statement that isn't listed, ask them to upload it (the result's forwarding field says the same). Statements already imported are not listed here — use pica_statements_query for those." + " " + MONEY_READ_NOTE, workflows: "infrastructure", vernacular_kind: "specialist", vernacular_reason: "Specialist discovery for the statement-ingestion flow, outside the Creator Question Atlas.", annotations: { title: "pending statements", readOnlyHint: true, idempotentHint: true, }, inputSchema: { type: "object", properties: { parsed_status: { type: "string", enum: ["pending", "completed", "all"], description: "filter by parse state", }, limit: { type: "number", minimum: 1, maximum: 50, description: "max rows (default 25, max 50)", }, offset: { type: "number", description: "pagination offset" }, }, }, }, executor: this.listPending.bind(this), }, { definition: { name: "pica_statement_ingest_document", scope: "write:finances", audiences: ["stdio", "http", "public"], tier: "write", // Delegated agents (OAuth/grant) propose; the org owner approves // in-view/email; PICA replays via human-approval-executor (ADR-250). approvalMode: "human", description: "When the user has uploaded a royalty/streaming statement document (PDF, image, or xlsx — NOT csv; csv goes through pica_import_analyze / pica_import_execute instead, a deterministic row-by-row importer with no AI extraction step), read it with pica_documents_inspect, extract the line items, then call this to import them. Money is fallible: extract fewer rows confidently over many rows wrongly, set a per-row confidence, and never guess a figure. Returns a summary; the import only lands after the owner approves. Find pending statements with pica_statements_pending. To undo an import afterwards, use pica_statement_import_remove." + " " + MONEY_WRITE_NOTE, workflows: "infrastructure", vernacular_kind: "specialist", vernacular_reason: "Financial write behind human approval; invoked by exact name in a specialist statement-ingestion flow, outside the Creator Question Atlas.", annotations: { title: "ingest statement document" }, inputSchema: { type: "object", properties: { document_id: { type: "string", description: "the uploaded statement document id", }, source: { type: "string", description: "the society/distributor the statement is from, e.g. 'PRS', 'DistroKid'", }, statement_period: { type: "string", description: "e.g. 'Q4 2025' (optional)", }, rows: { type: "array", description: "one entry per statement line you read", items: { type: "object", properties: { work_title: { type: "string" }, amount: { type: "number" }, currency: { type: "string" }, payment_date: { type: "string", description: "ISO date YYYY-MM-DD", }, stream_quantity: { type: "number", description: "plays/streams; omit for society statements", }, territory: { type: "string" }, revenue_type: { type: "string" }, isrc: { type: "string" }, iswc: { type: "string" }, ipi: { type: "string" }, tunecode: { type: "string" }, writer_name: { type: "string" }, confidence: { type: "number", description: "0-1, your confidence in this line", }, }, required: [ "work_title", "amount", "payment_date", "confidence", ], }, }, }, required: ["document_id", "source", "rows"], }, }, executor: this.ingestDocument.bind(this), }, { definition: { name: "pica_statement_imports", scope: "read:finances", audiences: ["stdio", "http", "public"], tier: "read", description: "Progress of statement imports the owner started on the statements page (a statement file is read in the background, line by line). With import_id: that one import. Without: the latest imports, newest first. Each carries the same view the owner sees: status in plain words (status_label), finished, lines read / written / matched / unmatched, percent, totals (totalOriginalAmount counts the primaryCurrency lines only; totals.totalsByCurrency gives each currency's own total: report them side by side, never added together), lines kept unconverted for want of an exchange rate, and failure_reason when it failed. While an import is unfinished none of its money counts in any total, so don't report its figures as earnings until finished is true. Unmatched lines wait in pica_royalties_unmatched for the owner to match. A finished import that was a mistake is undone with pica_statement_import_remove." + " " + MONEY_READ_NOTE, workflows: "infrastructure", vernacular_kind: "specialist", vernacular_reason: "Progress of a background statement import, for the statement-ingestion flow, outside the Creator Question Atlas.", annotations: { title: "statement import progress", readOnlyHint: true, idempotentHint: true, }, inputSchema: { type: "object", properties: { import_id: { type: "string", description: "one import's id (from a previous call)", }, document_id: { type: "string", description: "only imports of this uploaded statement", }, limit: { type: "number", minimum: 1, maximum: 100, description: "max imports (default 25)", }, offset: { type: "number", description: "pagination offset" }, }, }, }, executor: this.imports.bind(this), }, { definition: { name: "pica_statement_import_remove", // The backing route's resource scope, so a finance-scoped delegated // agent can PROPOSE a removal; the owner's approval substitutes for // destructive:* (ADR-250). scope: "write:finances", audiences: ["stdio", "http", "public"], tier: "destructive", // Delegated agents (OAuth/grant) propose; the org owner approves; // PICA replays via human-approval-executor. A signed-in or // first-party-key caller takes the normal destructive path // (confirmation token). approvalMode: "human", description: "Undo an imported royalty statement: its lines come out of the royalties (including any matched to a song by hand) and its total goes, together or not at all. If the statement was imported from an uploaded document, that document stays, and the same file can be imported again afterwards; a statement imported straight from a file was never kept, so the owner would re-upload it. Use it when the user says a statement was imported by mistake or imported wrong. Take import_batch_id from pica_statements_query (each statement carries importBatchId and canChange — if canChange is false, this caller may not remove it). Refused while that statement is still importing (wait until pica_statement_imports says finished). Stream counts taken from the statement leave the recordings' figures by the next morning (a daily job). For delegated agents this creates a pending human approval: relay the returned message and poll pica_approval_status.", workflows: "infrastructure", vernacular_kind: "specialist", vernacular_reason: "Destructive financial undo behind human approval; invoked by exact name after the user asks to remove a statement, outside the Creator Question Atlas.", annotations: { title: "remove an imported statement", destructiveHint: true, }, inputSchema: { type: "object", properties: { import_batch_id: { type: "string", description: "the statement's importBatchId from pica_statements_query", }, }, required: ["import_batch_id"], }, }, executor: this.removeImport.bind(this), }, ]; } async removeImport(args) { const result = await this.pica.statements.removeImport(String(args.import_batch_id)); return formatSuccess("Statement import removed", { ...result, note: "if the statement came from an uploaded document, that document is still there and can be imported again.", }); } async imports(args) { if (typeof args.import_id === "string" && args.import_id) { const result = await this.pica.statements.getImport(args.import_id); return formatSuccess("Statement import", result); } const result = await this.pica.statements.listImports({ limit: args.limit, offset: args.offset, document_id: args.document_id, }); return formatSuccess("Statement imports", result); } async listPending(args) { const result = await this.pica.statements.listPending({ limit: args.limit, offset: args.offset, parsed_status: args.parsed_status, }); return formatSuccess("Pending statements", result); } async ingestDocument(args) { const result = await this.pica.statements.ingestDocument({ document_id: args.document_id, source: args.source, statement_period: args.statement_period, rows: args.rows, }); return formatSuccess("Statement rows ingested", result); } } //# sourceMappingURL=statements.js.map