@monadsag/productive-mcp 1.2.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +190 -0
- package/NOTICE +6 -0
- package/README.md +353 -0
- package/build/api/bookings-client.js +249 -0
- package/build/api/capacity.js +279 -0
- package/build/api/client.js +1296 -0
- package/build/api/errors.js +50 -0
- package/build/api/invoice-time-entries.js +256 -0
- package/build/api/pages-tree.js +106 -0
- package/build/api/time-entry-notes.js +75 -0
- package/build/api/types.js +1 -0
- package/build/auth/logo.js +21 -0
- package/build/auth/self-resolver.js +61 -0
- package/build/config/index.js +24 -0
- package/build/index.js +15 -0
- package/build/prompts/timesheet.js +285 -0
- package/build/server.js +55 -0
- package/build/tools/absences.js +502 -0
- package/build/tools/activities.js +140 -0
- package/build/tools/bookings.js +531 -0
- package/build/tools/budget-services.js +168 -0
- package/build/tools/budgets.js +276 -0
- package/build/tools/capacity.js +281 -0
- package/build/tools/changeset.js +41 -0
- package/build/tools/comments.js +469 -0
- package/build/tools/companies.js +73 -0
- package/build/tools/custom-field-resolver.js +92 -0
- package/build/tools/custom-fields.js +155 -0
- package/build/tools/folders.js +516 -0
- package/build/tools/include-resolver.js +45 -0
- package/build/tools/invoice-actions.js +476 -0
- package/build/tools/invoice-defaults.js +65 -0
- package/build/tools/invoice-time-entries.js +320 -0
- package/build/tools/invoices.js +657 -0
- package/build/tools/my-tasks.js +100 -0
- package/build/tools/page-children.js +129 -0
- package/build/tools/pages.js +466 -0
- package/build/tools/people.js +147 -0
- package/build/tools/projects.js +77 -0
- package/build/tools/recent-updates.js +112 -0
- package/build/tools/registry.js +409 -0
- package/build/tools/subtasks.js +245 -0
- package/build/tools/task-backlog.js +106 -0
- package/build/tools/task-dependencies.js +244 -0
- package/build/tools/task-list-move.js +65 -0
- package/build/tools/task-lists.js +535 -0
- package/build/tools/task-reposition.js +199 -0
- package/build/tools/task-status.js +178 -0
- package/build/tools/tasks.js +738 -0
- package/build/tools/time-entries.js +736 -0
- package/build/tools/time-entry-approval.js +103 -0
- package/build/tools/time-entry-delete.js +72 -0
- package/build/tools/time-entry-update.js +126 -0
- package/build/tools/timers.js +195 -0
- package/build/tools/todos.js +342 -0
- package/build/tools/tool-helpers.js +114 -0
- package/build/tools/toolsets.js +153 -0
- package/build/tools/whoami.js +64 -0
- package/build/tools/workflow-statuses.js +81 -0
- package/build/utils/errors.js +67 -0
- package/build/utils/mentions.js +98 -0
- package/npm-shrinkwrap.json +1159 -0
- package/package.json +79 -0
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js';
|
|
3
|
+
import { ProductiveApiError } from '../api/errors.js';
|
|
4
|
+
import { collectPages, durationOf, entryDurations, formatDateDe, reconcile, reconcileByService, subtotalsBy, MAX_PAGE_SIZE, MAX_TIME_ENTRY_PAGES, } from '../api/invoice-time-entries.js';
|
|
5
|
+
import { noteToBullets } from '../api/time-entry-notes.js';
|
|
6
|
+
import { buildIncludeMap, resolveName } from './include-resolver.js';
|
|
7
|
+
import { formatAmount, resolveCompanyName } from './invoices.js';
|
|
8
|
+
import { toMcpError } from '../utils/errors.js';
|
|
9
|
+
/**
|
|
10
|
+
* Above this many entries the per-entry list is dropped and only the summary is
|
|
11
|
+
* returned. The busiest invoice in the live org holds 79, but the page ceiling
|
|
12
|
+
* allows 1000, and 1000 entries of JSON is not a usable tool response.
|
|
13
|
+
*/
|
|
14
|
+
const MAX_FULL_DETAIL_ENTRIES = 300;
|
|
15
|
+
/** One page is plenty: the busiest live invoice carries two line items. */
|
|
16
|
+
const LINE_ITEM_PAGE_SIZE = 200;
|
|
17
|
+
/**
|
|
18
|
+
* How many rows the number lookup below reads. A whole page, not the two an
|
|
19
|
+
* exact match plus one duplicate would need: `filter[number]` is a `contains`
|
|
20
|
+
* match, so a short invoice number ("5") matches every number containing it and
|
|
21
|
+
* the exactly-matching row need not be among the first few. Missing it there is
|
|
22
|
+
* not a harmless "not found" -- the ID lookup then wins and a completely
|
|
23
|
+
* different invoice is audited under the caller's number.
|
|
24
|
+
*/
|
|
25
|
+
const NUMBER_LOOKUP_PAGE_SIZE = 200;
|
|
26
|
+
const getInvoiceTimeEntriesSchema = z.object({
|
|
27
|
+
invoice: z.preprocess((value) => (typeof value === 'number' ? String(value) : value), z.string().trim().min(1, 'Invoice number or ID is required')),
|
|
28
|
+
detail: z.enum(['full', 'summary']).default('full'),
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* A 404 is the only answer that means "this is not an invoice ID".
|
|
32
|
+
*
|
|
33
|
+
* Catching everything would reinterpret an expired token (401) or a rate limit
|
|
34
|
+
* (429) as "invoice not found" and send the caller hunting for the wrong bug.
|
|
35
|
+
*/
|
|
36
|
+
async function getInvoiceOrNull(client, id) {
|
|
37
|
+
try {
|
|
38
|
+
return await client.getInvoice(id);
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
if (error instanceof ProductiveApiError && error.httpStatus === 404)
|
|
42
|
+
return null;
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Accept either the number a human reads off the invoice or the internal ID.
|
|
48
|
+
*
|
|
49
|
+
* Both are digit strings, so both lookups run and the number wins: `20260035`
|
|
50
|
+
* is a plausible ID too, and silently auditing the wrong invoice is the failure
|
|
51
|
+
* this is built to avoid. When the two resolve to different invoices the caller
|
|
52
|
+
* is told rather than blocked.
|
|
53
|
+
*/
|
|
54
|
+
async function resolveInvoice(client, selector) {
|
|
55
|
+
const [byNumber, byId] = await Promise.all([
|
|
56
|
+
client.listInvoices({ number: selector, limit: NUMBER_LOOKUP_PAGE_SIZE }),
|
|
57
|
+
/^\d+$/.test(selector) ? getInvoiceOrNull(client, selector) : Promise.resolve(null),
|
|
58
|
+
]);
|
|
59
|
+
// filter[number] documents a `contains` operator, so a returned row is not
|
|
60
|
+
// proof of an exact match: without this, "2026003" would audit "20260035".
|
|
61
|
+
const exact = (byNumber.data ?? []).filter((row) => row.attributes.number === selector);
|
|
62
|
+
if (exact.length > 1) {
|
|
63
|
+
throw new McpError(ErrorCode.InvalidParams, `Invoice number "${selector}" matches ${exact.length} invoices (IDs ${exact
|
|
64
|
+
.map((row) => row.id)
|
|
65
|
+
.join(', ')}). Pass the invoice ID instead.`);
|
|
66
|
+
}
|
|
67
|
+
if (exact.length === 1) {
|
|
68
|
+
const warnings = [];
|
|
69
|
+
if (byId && byId.data.id !== exact[0].id) {
|
|
70
|
+
warnings.push({
|
|
71
|
+
code: 'ambiguous_selector',
|
|
72
|
+
message: `"${selector}" is both an invoice number (ID ${exact[0].id}) and the ID of a ` +
|
|
73
|
+
`different invoice (${byId.data.id}). Matched by number; pass the ID to force the other.`,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return { invoice: exact[0], included: byNumber.included, matchedBy: 'number', warnings };
|
|
77
|
+
}
|
|
78
|
+
if (byId) {
|
|
79
|
+
return { invoice: byId.data, included: byId.included, matchedBy: 'id', warnings: [] };
|
|
80
|
+
}
|
|
81
|
+
throw new McpError(ErrorCode.InvalidParams, `No invoice found with number or ID "${selector}". Use list_invoices to find it.`);
|
|
82
|
+
}
|
|
83
|
+
/** A related record, or null when the entry carries no such relationship. */
|
|
84
|
+
function relationshipRef(names, type, id, label) {
|
|
85
|
+
if (!id)
|
|
86
|
+
return null;
|
|
87
|
+
// Falling back to `${label} #${id}` and not "Unknown": a sideload can be
|
|
88
|
+
// missing for a deactivated person, and "Unknown" would merge several
|
|
89
|
+
// distinct people into one subtotal row that silently reports the wrong hours.
|
|
90
|
+
return { id, name: resolveName(names, type, id) ?? `${label} #${id}` };
|
|
91
|
+
}
|
|
92
|
+
function toEntryJson(entry, names) {
|
|
93
|
+
const { tracked, billable } = entryDurations(entry);
|
|
94
|
+
const date = entry.attributes.date;
|
|
95
|
+
return {
|
|
96
|
+
id: entry.id,
|
|
97
|
+
date,
|
|
98
|
+
date_formatted: formatDateDe(date),
|
|
99
|
+
person: relationshipRef(names, 'people', entry.relationships?.person?.data?.id, 'Person'),
|
|
100
|
+
service: relationshipRef(names, 'services', entry.relationships?.service?.data?.id, 'Service'),
|
|
101
|
+
task: relationshipRef(names, 'tasks', entry.relationships?.task?.data?.id, 'Task'),
|
|
102
|
+
tracked,
|
|
103
|
+
billable,
|
|
104
|
+
billable_differs: tracked.minutes !== billable.minutes,
|
|
105
|
+
notes: noteToBullets(entry.attributes.note),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Group key for the per-person and per-service breakdowns.
|
|
110
|
+
*
|
|
111
|
+
* An entry whose relationship is missing lands in one `Unattributed` bucket
|
|
112
|
+
* rather than being skipped, so the breakdowns always add back up to the total.
|
|
113
|
+
*/
|
|
114
|
+
function groupKey(names, type, label, idOf) {
|
|
115
|
+
return (entry) => {
|
|
116
|
+
const id = idOf(entry);
|
|
117
|
+
if (!id)
|
|
118
|
+
return { id: null, name: 'Unattributed' };
|
|
119
|
+
return { id, name: resolveName(names, type, id) ?? `${label} #${id}` };
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const personIdOf = (entry) => entry.relationships?.person?.data?.id;
|
|
123
|
+
const serviceIdOf = (entry) => entry.relationships?.service?.data?.id;
|
|
124
|
+
function periodOf(entries) {
|
|
125
|
+
if (entries.length === 0)
|
|
126
|
+
return null;
|
|
127
|
+
const dates = entries.map((entry) => entry.attributes.date).sort();
|
|
128
|
+
return { from: dates[0], to: dates[dates.length - 1] };
|
|
129
|
+
}
|
|
130
|
+
function totalsOf(entries) {
|
|
131
|
+
let tracked = 0;
|
|
132
|
+
let billable = 0;
|
|
133
|
+
for (const entry of entries) {
|
|
134
|
+
const durations = entryDurations(entry);
|
|
135
|
+
tracked += durations.tracked.minutes;
|
|
136
|
+
billable += durations.billable.minutes;
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
tracked: durationOf(tracked),
|
|
140
|
+
billable: durationOf(billable),
|
|
141
|
+
difference: durationOf(billable - tracked),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/** Line item descriptions are rich text too, so they get the same treatment. */
|
|
145
|
+
function describeLineItem(raw) {
|
|
146
|
+
return noteToBullets(raw).join(' ') || '(no description)';
|
|
147
|
+
}
|
|
148
|
+
function invoiceJson(resolved) {
|
|
149
|
+
const attributes = resolved.invoice.attributes;
|
|
150
|
+
const invoicedOn = attributes.invoiced_on;
|
|
151
|
+
return {
|
|
152
|
+
id: resolved.invoice.id,
|
|
153
|
+
number: attributes.number ?? null,
|
|
154
|
+
company: resolveCompanyName(resolved.invoice, resolved.included),
|
|
155
|
+
invoiced_on: invoicedOn ?? null,
|
|
156
|
+
invoiced_on_formatted: invoicedOn ? formatDateDe(invoicedOn) : null,
|
|
157
|
+
amount: formatAmount(attributes.amount_with_tax),
|
|
158
|
+
currency: attributes.currency ?? null,
|
|
159
|
+
state: attributes.finalized_at ? 'finalized' : 'draft',
|
|
160
|
+
matched_by: resolved.matchedBy,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
export async function getInvoiceTimeEntriesTool(client, args) {
|
|
164
|
+
try {
|
|
165
|
+
const params = getInvoiceTimeEntriesSchema.parse(args);
|
|
166
|
+
const resolved = await resolveInvoice(client, params.invoice);
|
|
167
|
+
const warnings = [...resolved.warnings];
|
|
168
|
+
const collected = await collectPages((page) => client.listTimeEntries({
|
|
169
|
+
invoice_id: resolved.invoice.id,
|
|
170
|
+
// The only sort this endpoint accepts that is also the order the report
|
|
171
|
+
// wants. It is not unique, which is what `expected` below guards.
|
|
172
|
+
sort: 'date',
|
|
173
|
+
limit: MAX_PAGE_SIZE,
|
|
174
|
+
page,
|
|
175
|
+
}), MAX_TIME_ENTRY_PAGES, MAX_PAGE_SIZE);
|
|
176
|
+
const lineItemsResponse = await client.listLineItems({
|
|
177
|
+
invoice_id: resolved.invoice.id,
|
|
178
|
+
limit: LINE_ITEM_PAGE_SIZE,
|
|
179
|
+
});
|
|
180
|
+
const lineItems = lineItemsResponse.data ?? [];
|
|
181
|
+
const names = buildIncludeMap(collected.included);
|
|
182
|
+
const personKey = groupKey(names, 'people', 'Person', personIdOf);
|
|
183
|
+
const serviceKey = groupKey(names, 'services', 'Service', serviceIdOf);
|
|
184
|
+
const entries = [...collected.rows].sort(byDateThenPerson(personKey));
|
|
185
|
+
const totals = totalsOf(entries);
|
|
186
|
+
const reconciliation = reconcile(lineItems, totals.billable.minutes, describeLineItem);
|
|
187
|
+
const byPerson = subtotalsBy(entries, personKey);
|
|
188
|
+
const byService = subtotalsBy(entries, serviceKey);
|
|
189
|
+
warnings.push(...collectWarnings({ collected, entries, lineItems, reconciliation, detail: params.detail }));
|
|
190
|
+
const includeEntries = params.detail === 'full' && entries.length <= MAX_FULL_DETAIL_ENTRIES;
|
|
191
|
+
const payload = {
|
|
192
|
+
invoice: invoiceJson(resolved),
|
|
193
|
+
complete: !collected.truncated && !hasCountMismatch(collected, entries),
|
|
194
|
+
warnings,
|
|
195
|
+
totals: {
|
|
196
|
+
entry_count: entries.length,
|
|
197
|
+
period: periodOf(entries),
|
|
198
|
+
tracked: totals.tracked,
|
|
199
|
+
billable: totals.billable,
|
|
200
|
+
difference: totals.difference,
|
|
201
|
+
},
|
|
202
|
+
...(includeEntries ? { entries: entries.map((entry) => toEntryJson(entry, names)) } : {}),
|
|
203
|
+
by_person: byPerson,
|
|
204
|
+
by_service: byService,
|
|
205
|
+
reconciliation: {
|
|
206
|
+
...reconciliation,
|
|
207
|
+
by_service: reconcileByService(reconciliation.line_items, byService),
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
return { content: [{ type: 'text', text: JSON.stringify(payload, null, 2) }] };
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
throw toMcpError(error);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Chronological, then stable.
|
|
218
|
+
*
|
|
219
|
+
* The API is asked for `sort=date` but that is its only accepted sort key and it
|
|
220
|
+
* is not unique, so the order within one day is whatever came back. Sorting
|
|
221
|
+
* again here means two runs of this tool produce byte-identical output, which is
|
|
222
|
+
* what makes the result diffable as an audit document.
|
|
223
|
+
*/
|
|
224
|
+
function byDateThenPerson(person) {
|
|
225
|
+
return (a, b) => {
|
|
226
|
+
const byDate = a.attributes.date.localeCompare(b.attributes.date);
|
|
227
|
+
if (byDate !== 0)
|
|
228
|
+
return byDate;
|
|
229
|
+
const byPerson = person(a).name.localeCompare(person(b).name);
|
|
230
|
+
if (byPerson !== 0)
|
|
231
|
+
return byPerson;
|
|
232
|
+
return Number(a.id) - Number(b.id);
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
function hasCountMismatch(collected, entries) {
|
|
236
|
+
if (collected.truncated || collected.expected === undefined)
|
|
237
|
+
return false;
|
|
238
|
+
return collected.expected !== entries.length;
|
|
239
|
+
}
|
|
240
|
+
function collectWarnings(input) {
|
|
241
|
+
const warnings = [];
|
|
242
|
+
const { collected, entries, lineItems, reconciliation, detail } = input;
|
|
243
|
+
if (collected.truncated) {
|
|
244
|
+
warnings.push({
|
|
245
|
+
code: 'truncated',
|
|
246
|
+
message: `Only the first ${MAX_TIME_ENTRY_PAGES * MAX_PAGE_SIZE} time entries were read and there ` +
|
|
247
|
+
`are more. Every total below is missing time and the reconciliation cannot be trusted.`,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
if (hasCountMismatch(collected, entries)) {
|
|
251
|
+
warnings.push({
|
|
252
|
+
code: 'count_mismatch',
|
|
253
|
+
message: `Productive reports ${collected.expected} entries for this invoice but ${entries.length} ` +
|
|
254
|
+
`were read — rows moved between page requests. The totals below are incomplete.`,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
if (entries.length === 0 && reconciliation.line_item_hours > 0) {
|
|
258
|
+
warnings.push({
|
|
259
|
+
code: 'no_entries_but_billed_hours',
|
|
260
|
+
message: `The invoice bills ${reconciliation.line_item_hours} h but no time entry is attributed to ` +
|
|
261
|
+
`it. Either the line items were written by hand rather than generated from tracked time, ` +
|
|
262
|
+
`or the invoice is still a draft — attribution is written when generate_line_items runs.`,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
// Suppressed when there are no entries at all: the warning above already
|
|
266
|
+
// explains that case, and "entries were edited after finalization" is the
|
|
267
|
+
// wrong story to tell about an invoice that has no entries to edit.
|
|
268
|
+
if (reconciliation.status === 'mismatch' && entries.length > 0) {
|
|
269
|
+
warnings.push({
|
|
270
|
+
code: 'reconciliation_mismatch',
|
|
271
|
+
message: 'Line items and billable time disagree. On a finalized invoice this usually means entries ' +
|
|
272
|
+
'were edited after finalization: line items freeze, the time behind them does not.',
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
if (lineItems.length >= LINE_ITEM_PAGE_SIZE) {
|
|
276
|
+
warnings.push({
|
|
277
|
+
code: 'line_items_truncated',
|
|
278
|
+
message: `Only the first ${LINE_ITEM_PAGE_SIZE} line items were compared.`,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
if (detail === 'full' && entries.length > MAX_FULL_DETAIL_ENTRIES) {
|
|
282
|
+
warnings.push({
|
|
283
|
+
code: 'degraded_to_summary',
|
|
284
|
+
message: `${entries.length} entries is too many for the per-entry list, so it was omitted. ` +
|
|
285
|
+
`The totals and breakdowns below cover all of them.`,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
return warnings;
|
|
289
|
+
}
|
|
290
|
+
export const getInvoiceTimeEntriesDefinition = {
|
|
291
|
+
name: 'get_invoice_time_entries',
|
|
292
|
+
description: 'Audit which time entries an invoice bills, as structured JSON. Returns tracked AND billable ' +
|
|
293
|
+
'time per entry and in total — the two differ in practice (rounding up, non-billable work) — ' +
|
|
294
|
+
'plus per-person and per-service breakdowns and a reconciliation against the invoice line ' +
|
|
295
|
+
'items. Accepts the invoice number (e.g. "20260035") or the internal ID. Note that this ' +
|
|
296
|
+
'returns every time entry note on the invoice, so the response can be large; pass ' +
|
|
297
|
+
'detail: "summary" for totals only.',
|
|
298
|
+
inputSchema: {
|
|
299
|
+
type: 'object',
|
|
300
|
+
required: ['invoice'],
|
|
301
|
+
properties: {
|
|
302
|
+
invoice: {
|
|
303
|
+
type: 'string',
|
|
304
|
+
description: 'Invoice number as printed on the document (e.g. "20260035") or the internal invoice ID. ' +
|
|
305
|
+
'The number wins if a value is both.',
|
|
306
|
+
},
|
|
307
|
+
detail: {
|
|
308
|
+
type: 'string',
|
|
309
|
+
enum: ['full', 'summary'],
|
|
310
|
+
description: '"full" (default) includes every time entry with its notes; "summary" returns only ' +
|
|
311
|
+
'totals, breakdowns and the reconciliation.',
|
|
312
|
+
},
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
annotations: {
|
|
316
|
+
title: 'Get invoice time entries',
|
|
317
|
+
readOnlyHint: true,
|
|
318
|
+
openWorldHint: true,
|
|
319
|
+
},
|
|
320
|
+
};
|