@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,502 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Absence tools: discover the org's absence types, book an absence, read them back.
|
|
3
|
+
*
|
|
4
|
+
* Absence categories are never hardcoded here -- `GET /events` is the single
|
|
5
|
+
* source of truth, resolved at call time. See docs/resource-management-spec.md.
|
|
6
|
+
*/
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js';
|
|
9
|
+
import { BOOKING_METHOD, buildQuantity, classifyBooking, countWorkingDays, defaultBookingMethod, describeApprovalState, formatMinutes, isAbsenceBooking, isRemoteWorkEvent, remoteWorkEventIds, resolveAbsenceType, } from '../api/bookings-client.js';
|
|
10
|
+
import { contractedMinutes, workingDaysInRange } from '../api/capacity.js';
|
|
11
|
+
import { parseDate } from './time-entries.js';
|
|
12
|
+
import { buildIncludeMap, resolveName } from './include-resolver.js';
|
|
13
|
+
import { coerceBoolean, describePerson, personPattern, resolvePersonId, rethrowToolError, toNumericId, } from './tool-helpers.js';
|
|
14
|
+
/**
|
|
15
|
+
* How the absence is sized: working days in the range and hours on each of them.
|
|
16
|
+
*
|
|
17
|
+
* Both halves have to come from the same source. Taking the hours from the
|
|
18
|
+
* person's pattern but the days from the calendar charges a Mon-Thu contract for
|
|
19
|
+
* the Friday too -- a plain week of leave becomes 40h against 32h contracted and
|
|
20
|
+
* comes back out of get_capacity_overview flagged OVERBOOKED.
|
|
21
|
+
*/
|
|
22
|
+
function sizeAbsence(slices, fromIso, toIso) {
|
|
23
|
+
const workingDays = workingDaysInRange(slices, fromIso, toIso);
|
|
24
|
+
const contracted = contractedMinutes(slices, fromIso, toIso);
|
|
25
|
+
// Dividing back out keeps hoursPerDay * workingDays exactly equal to the
|
|
26
|
+
// contracted minutes, even across a week with uneven days.
|
|
27
|
+
const hoursPerDay = contracted !== null && contracted > 0 && workingDays > 0 ? contracted / workingDays / 60 : null;
|
|
28
|
+
return { workingDays, hoursPerDay };
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Match the caller's absence type against the org's list.
|
|
32
|
+
*
|
|
33
|
+
* Takes the events rather than fetching them: both callers need the full list
|
|
34
|
+
* for something else as well -- naming types in the output, naming the types of
|
|
35
|
+
* conflicting bookings -- so fetching here would double the calls.
|
|
36
|
+
*/
|
|
37
|
+
function matchEvent(events, opts) {
|
|
38
|
+
if (opts.event_id) {
|
|
39
|
+
const byId = events.find((e) => e.id === opts.event_id);
|
|
40
|
+
if (!byId) {
|
|
41
|
+
throw new McpError(ErrorCode.InvalidParams, `No absence type with ID ${opts.event_id}. Call list_absence_types to see the available ones.`);
|
|
42
|
+
}
|
|
43
|
+
return byId;
|
|
44
|
+
}
|
|
45
|
+
const needle = opts.absence_type ?? '';
|
|
46
|
+
let match;
|
|
47
|
+
try {
|
|
48
|
+
match = resolveAbsenceType(events, needle, { includeArchived: opts.includeArchived });
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
throw new McpError(ErrorCode.InvalidParams, error instanceof Error ? error.message : 'Ambiguous absence type');
|
|
52
|
+
}
|
|
53
|
+
if (!match) {
|
|
54
|
+
const available = events
|
|
55
|
+
.filter((e) => opts.includeArchived || !e.attributes.archived_at)
|
|
56
|
+
.map((e) => e.attributes.name)
|
|
57
|
+
.join(', ');
|
|
58
|
+
throw new McpError(ErrorCode.InvalidParams, `Unknown absence type "${needle}". Available in this organisation: ${available || 'none'}.`);
|
|
59
|
+
}
|
|
60
|
+
return match;
|
|
61
|
+
}
|
|
62
|
+
// --- list_absence_types ------------------------------------------------------
|
|
63
|
+
const listAbsenceTypesSchema = z.object({
|
|
64
|
+
include_archived: coerceBoolean.optional().default(false),
|
|
65
|
+
});
|
|
66
|
+
export async function listAbsenceTypesTool(client, args) {
|
|
67
|
+
try {
|
|
68
|
+
const params = listAbsenceTypesSchema.parse(args);
|
|
69
|
+
const response = await client.listEvents();
|
|
70
|
+
const events = (response.data ?? []).filter((e) => params.include_archived || !e.attributes.archived_at);
|
|
71
|
+
if (events.length === 0) {
|
|
72
|
+
return { content: [{ type: 'text', text: 'No absence types are configured.' }] };
|
|
73
|
+
}
|
|
74
|
+
const lines = events.map((event) => {
|
|
75
|
+
const a = event.attributes;
|
|
76
|
+
const remote = isRemoteWorkEvent(event);
|
|
77
|
+
const paid = a.event_type_id === 1 ? 'Paid' : a.event_type_id === 2 ? 'Unpaid' : 'Unknown';
|
|
78
|
+
// Remote work events are *always* event_type_id 2 API-side (see
|
|
79
|
+
// docs/api-spec/resources/events.yaml), so "Unpaid" would read as a claim
|
|
80
|
+
// about working from home instead of the data-model artefact it is.
|
|
81
|
+
const paidSegment = remote ? '' : `${paid} · `;
|
|
82
|
+
const capped = a.limitation_type_id === 4 ? 'no allowance needed' : 'allowance required';
|
|
83
|
+
const method = defaultBookingMethod(event);
|
|
84
|
+
return `• ${a.name} (ID: ${event.id})
|
|
85
|
+
${remote ? 'Remote work' : 'Time off'} · ${paidSegment}${capped} · half days: ${a.half_day_bookings ? 'yes' : 'no'}
|
|
86
|
+
Default booking method: ${method === BOOKING_METHOD.HOURS_PER_DAY ? '1 (hours per day)' : '3 (total hours)'}${a.archived_at ? '\n ARCHIVED' : ''}`;
|
|
87
|
+
});
|
|
88
|
+
return {
|
|
89
|
+
content: [
|
|
90
|
+
{
|
|
91
|
+
type: 'text',
|
|
92
|
+
text: `${events.length} absence type${events.length !== 1 ? 's' : ''} configured:\n\n${lines.join('\n\n')}\n\nPass the name (or ID) to create_absence.`,
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
rethrowToolError(error);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// --- create_absence ----------------------------------------------------------
|
|
102
|
+
const createAbsenceSchema = z
|
|
103
|
+
.object({
|
|
104
|
+
person_id: z.string().min(1).default('me'),
|
|
105
|
+
absence_type: z.string().min(1).optional(),
|
|
106
|
+
event_id: z.string().min(1).optional(),
|
|
107
|
+
date_from: z.string().min(1, 'date_from is required'),
|
|
108
|
+
date_to: z.string().min(1, 'date_to is required'),
|
|
109
|
+
hours_per_day: z.coerce.number().positive().max(24).optional(),
|
|
110
|
+
booking_method_id: z.coerce
|
|
111
|
+
.number()
|
|
112
|
+
.int()
|
|
113
|
+
.refine((v) => v === 1 || v === 3, {
|
|
114
|
+
message: 'booking_method_id must be 1 (hours per day) or 3 (total hours) for absences',
|
|
115
|
+
})
|
|
116
|
+
.optional(),
|
|
117
|
+
note: z.string().optional(),
|
|
118
|
+
allow_overlap: coerceBoolean.optional().default(false),
|
|
119
|
+
confirm: coerceBoolean.optional().default(false),
|
|
120
|
+
})
|
|
121
|
+
.refine((v) => v.absence_type || v.event_id, {
|
|
122
|
+
message: 'Provide either absence_type (name) or event_id',
|
|
123
|
+
});
|
|
124
|
+
/**
|
|
125
|
+
* Absences this person already has in the requested period.
|
|
126
|
+
*
|
|
127
|
+
* The API takes the booking happily and says nothing, so a repeated confirm
|
|
128
|
+
* writes the same week twice -- and get_capacity_overview then reports 80h of
|
|
129
|
+
* absence in a 40h week and calls the person overbooked on the strength of a
|
|
130
|
+
* duplicate. The date filters match on overlap, so what comes back is exactly
|
|
131
|
+
* the conflicting set.
|
|
132
|
+
*/
|
|
133
|
+
async function overlappingAbsences(client, personId, from, to, remoteEventIds) {
|
|
134
|
+
try {
|
|
135
|
+
const response = await client.listBookings({
|
|
136
|
+
person_id: personId,
|
|
137
|
+
after: from,
|
|
138
|
+
before: to,
|
|
139
|
+
limit: 50,
|
|
140
|
+
});
|
|
141
|
+
return (response.data ?? []).filter((b) =>
|
|
142
|
+
// Remote work is not absence anywhere else in this code, and it is not
|
|
143
|
+
// counted against capacity -- so a home-office day is no reason to
|
|
144
|
+
// refuse sick leave on top of it. Refusing would be a message about
|
|
145
|
+
// double-counting that is not true of these two.
|
|
146
|
+
classifyBooking(b, remoteEventIds) === 'time_off' &&
|
|
147
|
+
!b.attributes.canceled &&
|
|
148
|
+
!b.attributes.rejected);
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
// A conflict check must never be the reason a booking cannot be made.
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/** One line per conflicting absence, for the preview and the refusal alike. */
|
|
156
|
+
function describeConflicts(conflicts, events) {
|
|
157
|
+
return conflicts
|
|
158
|
+
.map((b) => {
|
|
159
|
+
const eventId = b.relationships?.event?.data?.id;
|
|
160
|
+
const type = events.find((e) => e.id === eventId)?.attributes.name ?? 'absence';
|
|
161
|
+
return ` • ${type}, ${b.attributes.started_on} to ${b.attributes.ended_on} (booking ${b.id})`;
|
|
162
|
+
})
|
|
163
|
+
.join('\n');
|
|
164
|
+
}
|
|
165
|
+
export async function createAbsenceTool(client, args, config) {
|
|
166
|
+
try {
|
|
167
|
+
const params = createAbsenceSchema.parse(args);
|
|
168
|
+
const personId = resolvePersonId(params.person_id, config);
|
|
169
|
+
const from = parseDate(params.date_from);
|
|
170
|
+
const to = parseDate(params.date_to);
|
|
171
|
+
if (to < from) {
|
|
172
|
+
throw new McpError(ErrorCode.InvalidParams, `date_to (${to}) is before date_from (${from}).`);
|
|
173
|
+
}
|
|
174
|
+
// Fetched here rather than through findEvent, because the same list names
|
|
175
|
+
// the types of any conflicting absences further down -- one call, not two.
|
|
176
|
+
const events = (await client.listEvents()).data ?? [];
|
|
177
|
+
const event = matchEvent(events, {
|
|
178
|
+
absence_type: params.absence_type,
|
|
179
|
+
event_id: params.event_id,
|
|
180
|
+
});
|
|
181
|
+
const method = (params.booking_method_id ?? defaultBookingMethod(event));
|
|
182
|
+
// The person's own pattern sizes the absence; countWorkingDays is only the
|
|
183
|
+
// fallback for somebody with no pattern on file.
|
|
184
|
+
const slices = await personPattern(client, personId);
|
|
185
|
+
const sized = sizeAbsence(slices, from, to);
|
|
186
|
+
const workingDays = slices.length > 0 ? sized.workingDays : countWorkingDays(from, to);
|
|
187
|
+
if (workingDays === 0) {
|
|
188
|
+
throw new McpError(ErrorCode.InvalidParams, `${from} to ${to} contains no working days (weekends only).`);
|
|
189
|
+
}
|
|
190
|
+
if (slices.length > 0 && sized.hoursPerDay === null && params.hours_per_day === undefined) {
|
|
191
|
+
throw new McpError(ErrorCode.InvalidParams, `This person is not contracted to work on any day between ${from} and ${to}, so there is nothing to book off. Pass hours_per_day explicitly to override.`);
|
|
192
|
+
}
|
|
193
|
+
const hoursPerDay = params.hours_per_day ?? sized.hoursPerDay ?? 8;
|
|
194
|
+
let quantity;
|
|
195
|
+
try {
|
|
196
|
+
quantity = buildQuantity(method, { hoursPerDay, workingDays });
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
throw new McpError(ErrorCode.InvalidParams, error instanceof Error ? error.message : 'Invalid booking amount');
|
|
200
|
+
}
|
|
201
|
+
const remoteTypeIds = new Set(events.filter(isRemoteWorkEvent).map((e) => e.id));
|
|
202
|
+
const conflicts = await overlappingAbsences(client, personId, from, to, remoteTypeIds);
|
|
203
|
+
if (!params.confirm) {
|
|
204
|
+
const who = await describePerson(client, personId);
|
|
205
|
+
const remoteHint = isRemoteWorkEvent(event)
|
|
206
|
+
? '\nThis type is remote work: it books working from home, not an absence — the person stays available.'
|
|
207
|
+
: '';
|
|
208
|
+
const conflictHint = conflicts.length > 0
|
|
209
|
+
? `\n\n⚠️ This period is already covered by ${conflicts.length} absence(s):\n${describeConflicts(conflicts, events)}\nBooking on top of them double-counts the time. Pass "allow_overlap": true if that is really what you want.`
|
|
210
|
+
: '';
|
|
211
|
+
return {
|
|
212
|
+
content: [
|
|
213
|
+
{
|
|
214
|
+
type: 'text',
|
|
215
|
+
text: `Absence ready to book:
|
|
216
|
+
|
|
217
|
+
Person: ${who}${params.person_id === 'me' ? ' (me)' : ''}
|
|
218
|
+
Type: ${event.attributes.name} (event ID ${event.id})${remoteHint}
|
|
219
|
+
Period: ${from} to ${to}
|
|
220
|
+
Working days: ${workingDays}
|
|
221
|
+
Hours per day: ${Math.round(hoursPerDay * 100) / 100}
|
|
222
|
+
Booking method: ${method}
|
|
223
|
+
${params.note ? `Note: ${params.note}` : 'No note'}${conflictHint}
|
|
224
|
+
|
|
225
|
+
Whether this needs approval depends on the person's approval policy — the result will say.
|
|
226
|
+
|
|
227
|
+
Call again with "confirm": true to book it.`,
|
|
228
|
+
},
|
|
229
|
+
],
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
// Refused rather than warned: the API accepts the duplicate without a word,
|
|
233
|
+
// and a repeated confirm is the easiest way to produce one. An override
|
|
234
|
+
// stays available for the cases that are genuinely legitimate -- half days,
|
|
235
|
+
// or two different types on the same day.
|
|
236
|
+
if (conflicts.length > 0 && !params.allow_overlap) {
|
|
237
|
+
throw new McpError(ErrorCode.InvalidParams, `This person already has ${conflicts.length} absence(s) covering ${from} to ${to}:\n${describeConflicts(conflicts, events)}\n` +
|
|
238
|
+
'Booking again would count the same time twice. Change the dates, delete the existing entry with delete_booking, or pass "allow_overlap": true to book it anyway.');
|
|
239
|
+
}
|
|
240
|
+
const payload = {
|
|
241
|
+
data: {
|
|
242
|
+
type: 'bookings',
|
|
243
|
+
attributes: {
|
|
244
|
+
// Number('abc') is NaN and serialises to null, which reaches the API
|
|
245
|
+
// as a missing field and comes back as a misleading 422.
|
|
246
|
+
person_id: toNumericId(personId, 'person_id'),
|
|
247
|
+
event_id: toNumericId(event.id, 'event_id'),
|
|
248
|
+
started_on: from,
|
|
249
|
+
ended_on: to,
|
|
250
|
+
booking_method_id: method,
|
|
251
|
+
...quantity,
|
|
252
|
+
...(params.note ? { note: params.note } : {}),
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
};
|
|
256
|
+
const created = await client.createBooking(payload);
|
|
257
|
+
const state = describeApprovalState(created.data);
|
|
258
|
+
const remote = isRemoteWorkEvent(event);
|
|
259
|
+
// Say what was actually booked. "Absence booked" over a home-office entry
|
|
260
|
+
// would be read back as time off by the next person -- and by the model.
|
|
261
|
+
return {
|
|
262
|
+
content: [
|
|
263
|
+
{
|
|
264
|
+
type: 'text',
|
|
265
|
+
text: `${remote ? 'Remote work booked' : 'Absence booked'} (ID: ${created.data.id})
|
|
266
|
+
|
|
267
|
+
Type: ${event.attributes.name}${remote ? ' (remote work — the person is working, not away)' : ''}
|
|
268
|
+
Period: ${created.data.attributes.started_on} to ${created.data.attributes.ended_on}
|
|
269
|
+
Working days: ${created.data.attributes.total_working_days ?? workingDays}
|
|
270
|
+
Status: ${state}
|
|
271
|
+
|
|
272
|
+
${created.data.attributes.approved ? 'No approval was required for this person.' : `This is waiting for approval — it is not confirmed ${remote ? 'remote work' : 'time off'} yet.`}`,
|
|
273
|
+
},
|
|
274
|
+
],
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
catch (error) {
|
|
278
|
+
rethrowToolError(error);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
// --- list_absences -----------------------------------------------------------
|
|
282
|
+
const listAbsencesSchema = z.object({
|
|
283
|
+
person_id: z.string().optional(),
|
|
284
|
+
date_from: z.string().optional(),
|
|
285
|
+
date_to: z.string().optional(),
|
|
286
|
+
absence_type: z.string().optional(),
|
|
287
|
+
approval_status: z.enum(['approved', 'pending', 'rejected', 'canceled']).optional(),
|
|
288
|
+
include_remote_work: coerceBoolean.optional().default(false),
|
|
289
|
+
include_notes: coerceBoolean.optional().default(false),
|
|
290
|
+
limit: z.coerce.number().min(1).max(200).default(50),
|
|
291
|
+
});
|
|
292
|
+
export async function listAbsencesTool(client, args, config) {
|
|
293
|
+
try {
|
|
294
|
+
const params = listAbsencesSchema.parse(args);
|
|
295
|
+
const personId = params.person_id ? resolvePersonId(params.person_id, config) : undefined;
|
|
296
|
+
// The absence types are needed anyway (names for the output, remote work
|
|
297
|
+
// detection), and they double as the server-side filter: selecting every
|
|
298
|
+
// event id returns exactly the absences. `filter[booking_type]` would be
|
|
299
|
+
// the obvious choice and is documented, but the API accepts and then
|
|
300
|
+
// ignores it -- every value answers the unfiltered set (verified live).
|
|
301
|
+
const events = (await client.listEvents()).data ?? [];
|
|
302
|
+
// Archived types are matched here but not in create_absence: a type that
|
|
303
|
+
// was retired last year cannot be booked any more, but its bookings are
|
|
304
|
+
// still history this tool is expected to list.
|
|
305
|
+
const requested = params.absence_type
|
|
306
|
+
? matchEvent(events, { absence_type: params.absence_type, includeArchived: true })
|
|
307
|
+
: null;
|
|
308
|
+
const wanted = requested ? [requested] : events;
|
|
309
|
+
// Asking for a remote work type by name has to override the default filter
|
|
310
|
+
// below, otherwise that question is guaranteed an empty answer -- which
|
|
311
|
+
// reads as "nobody works from home" rather than "you filtered it out".
|
|
312
|
+
const remoteWasAskedFor = requested !== null && isRemoteWorkEvent(requested);
|
|
313
|
+
const response = await client.listBookings({
|
|
314
|
+
after: params.date_from ? parseDate(params.date_from) : undefined,
|
|
315
|
+
before: params.date_to ? parseDate(params.date_to) : undefined,
|
|
316
|
+
person_id: personId,
|
|
317
|
+
event_id: wanted.map((e) => e.id).join(',') || undefined,
|
|
318
|
+
approval_status: params.approval_status,
|
|
319
|
+
limit: params.limit,
|
|
320
|
+
});
|
|
321
|
+
const fetched = response.data ?? [];
|
|
322
|
+
// Kept as a safety net: an org with no event types configured sends no
|
|
323
|
+
// filter at all, and project bookings would otherwise come through.
|
|
324
|
+
let absences = fetched.filter(isAbsenceBooking);
|
|
325
|
+
// Both sources carry the same `absence_type`; taking the union means a
|
|
326
|
+
// booking still classifies if either the sideload or the type list misses it.
|
|
327
|
+
const remoteIds = remoteWorkEventIds(response.included);
|
|
328
|
+
for (const event of events)
|
|
329
|
+
if (isRemoteWorkEvent(event))
|
|
330
|
+
remoteIds.add(event.id);
|
|
331
|
+
// Remote work shares the absence resource but means the person is working.
|
|
332
|
+
// Listing it under "who is off next week?" would report present people as
|
|
333
|
+
// away, so it is hidden unless it was explicitly asked for.
|
|
334
|
+
let hiddenRemote = 0;
|
|
335
|
+
if (!params.include_remote_work && !remoteWasAskedFor) {
|
|
336
|
+
const kept = absences.filter((b) => classifyBooking(b, remoteIds) !== 'remote_work');
|
|
337
|
+
hiddenRemote = absences.length - kept.length;
|
|
338
|
+
absences = kept;
|
|
339
|
+
}
|
|
340
|
+
const remoteNote = hiddenRemote
|
|
341
|
+
? ` ${hiddenRemote} remote work booking${hiddenRemote !== 1 ? 's' : ''} hidden — pass include_remote_work: true to include working from home.`
|
|
342
|
+
: remoteWasAskedFor
|
|
343
|
+
? ' This type is remote work, so working-from-home bookings are included.'
|
|
344
|
+
: '';
|
|
345
|
+
// The page holds absences only now, so a full one means the limit is what
|
|
346
|
+
// cut the list short. The slice just holds the tool to its own promise if
|
|
347
|
+
// the API ever hands back more rows than page[size] asked for.
|
|
348
|
+
const moreAvailable = fetched.length >= params.limit;
|
|
349
|
+
absences = absences.slice(0, params.limit);
|
|
350
|
+
if (absences.length === 0) {
|
|
351
|
+
return {
|
|
352
|
+
content: [
|
|
353
|
+
{
|
|
354
|
+
type: 'text',
|
|
355
|
+
text: `No absences found for these filters.${remoteNote}\n\nNote: a regular API token only sees its own resource planning, so an empty result does not prove none exist for other people.`,
|
|
356
|
+
},
|
|
357
|
+
],
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
const names = buildIncludeMap(response.included);
|
|
361
|
+
const lines = absences.map((booking) => {
|
|
362
|
+
const a = booking.attributes;
|
|
363
|
+
const pid = booking.relationships?.person?.data?.id;
|
|
364
|
+
const eid = booking.relationships?.event?.data?.id;
|
|
365
|
+
const person = resolveName(names, 'people', pid) ?? pid ?? 'Unknown';
|
|
366
|
+
const type = resolveName(names, 'events', eid) ?? `event ${eid ?? '?'}`;
|
|
367
|
+
const total = typeof a.total_time === 'number' ? ` · ${formatMinutes(a.total_time)}` : '';
|
|
368
|
+
return `• ${person} — ${type} (ID: ${booking.id})
|
|
369
|
+
${a.started_on} to ${a.ended_on} · ${a.total_working_days ?? '?'} working day(s)${total}
|
|
370
|
+
Status: ${describeApprovalState(booking, { includeReason: params.include_notes })}${params.include_notes && a.note ? `\n Note: ${a.note}` : ''}`;
|
|
371
|
+
});
|
|
372
|
+
return {
|
|
373
|
+
content: [
|
|
374
|
+
{
|
|
375
|
+
type: 'text',
|
|
376
|
+
text: `${absences.length} absence${absences.length !== 1 ? 's' : ''} found:\n\n${lines.join('\n\n')}\n\nOnly bookings visible to the calling token are listed.${moreAvailable ? ' There may be more -- raise limit or narrow the date range.' : ''}${remoteNote}`,
|
|
377
|
+
},
|
|
378
|
+
],
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
catch (error) {
|
|
382
|
+
rethrowToolError(error);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
// --- definitions -------------------------------------------------------------
|
|
386
|
+
export const listAbsenceTypesDefinition = {
|
|
387
|
+
name: 'list_absence_types',
|
|
388
|
+
description: 'List the absence types configured in this Productive organisation (vacation, sick leave, and so on) with their IDs. Call this first when booking an absence: the types are organisation-specific and must be read at runtime rather than assumed. Each type is marked as time off or as remote work (working from home, which means the person is present). Feed the name or ID into create_absence.',
|
|
389
|
+
inputSchema: {
|
|
390
|
+
type: 'object',
|
|
391
|
+
properties: {
|
|
392
|
+
include_archived: {
|
|
393
|
+
type: 'boolean',
|
|
394
|
+
description: 'Include archived absence types (default false)',
|
|
395
|
+
default: false,
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
required: [],
|
|
399
|
+
},
|
|
400
|
+
annotations: { title: 'List absence types', readOnlyHint: true, openWorldHint: true },
|
|
401
|
+
};
|
|
402
|
+
export const createAbsenceDefinition = {
|
|
403
|
+
name: 'create_absence',
|
|
404
|
+
description: 'Book an absence (vacation, sick leave, unpaid leave, ...) for a person over a date range. Requires confirmation: the first call returns a summary, then repeat with "confirm": true to write it. Identify the type with absence_type (name, matched against list_absence_types) or event_id. A booked absence is NOT automatically approved — whether it needs approval depends on the person\'s approval policy, and the result reports the actual status. An absence that overlaps one the person already has is refused unless allow_overlap is passed.',
|
|
405
|
+
inputSchema: {
|
|
406
|
+
type: 'object',
|
|
407
|
+
properties: {
|
|
408
|
+
person_id: {
|
|
409
|
+
type: 'string',
|
|
410
|
+
description: 'Person the absence is for. "me" uses PRODUCTIVE_USER_ID. Default "me".',
|
|
411
|
+
default: 'me',
|
|
412
|
+
},
|
|
413
|
+
absence_type: {
|
|
414
|
+
type: 'string',
|
|
415
|
+
description: 'Absence type by name, matched case-insensitively against list_absence_types. Either this or event_id is required.',
|
|
416
|
+
},
|
|
417
|
+
event_id: {
|
|
418
|
+
type: 'string',
|
|
419
|
+
description: 'Absence type by ID, as returned by list_absence_types.',
|
|
420
|
+
},
|
|
421
|
+
date_from: {
|
|
422
|
+
type: 'string',
|
|
423
|
+
description: 'First day. Accepts "today", "yesterday" or YYYY-MM-DD (required)',
|
|
424
|
+
},
|
|
425
|
+
date_to: {
|
|
426
|
+
type: 'string',
|
|
427
|
+
description: 'Last day, inclusive. Same formats as date_from (required)',
|
|
428
|
+
},
|
|
429
|
+
hours_per_day: {
|
|
430
|
+
type: 'number',
|
|
431
|
+
description: "Hours absent per working day. Defaults to the person's own contracted hours for that day, so part-time contracts are not overstated.",
|
|
432
|
+
},
|
|
433
|
+
booking_method_id: {
|
|
434
|
+
type: 'number',
|
|
435
|
+
description: 'Override the booking method: 1 = hours per day, 3 = total hours. Derived from the absence type when omitted.',
|
|
436
|
+
enum: [1, 3],
|
|
437
|
+
},
|
|
438
|
+
note: { type: 'string', description: 'Optional note' },
|
|
439
|
+
allow_overlap: {
|
|
440
|
+
type: 'boolean',
|
|
441
|
+
description: 'Book even though the person already has an absence in this period. Off by default: the API accepts duplicates silently and they are then counted twice.',
|
|
442
|
+
default: false,
|
|
443
|
+
},
|
|
444
|
+
confirm: {
|
|
445
|
+
type: 'boolean',
|
|
446
|
+
description: 'Set true to actually book. Call without it first to preview.',
|
|
447
|
+
default: false,
|
|
448
|
+
},
|
|
449
|
+
},
|
|
450
|
+
required: ['date_from', 'date_to'],
|
|
451
|
+
},
|
|
452
|
+
annotations: {
|
|
453
|
+
title: 'Create absence',
|
|
454
|
+
readOnlyHint: false,
|
|
455
|
+
destructiveHint: false,
|
|
456
|
+
idempotentHint: false,
|
|
457
|
+
openWorldHint: true,
|
|
458
|
+
},
|
|
459
|
+
};
|
|
460
|
+
export const listAbsencesDefinition = {
|
|
461
|
+
name: 'list_absences',
|
|
462
|
+
description: 'Read booked absences, filtered by person, date range, type or approval status — for questions like "who is off next week?" or "am I already booked?". Returns person, period, type and approval status. Remote work (working from home) is booked as the same kind of record but means the person is present and working, so it is left out unless include_remote_work is set. Note that a regular API token only sees its own resource planning, so an empty result does not prove nothing exists for other people.',
|
|
463
|
+
inputSchema: {
|
|
464
|
+
type: 'object',
|
|
465
|
+
properties: {
|
|
466
|
+
person_id: {
|
|
467
|
+
type: 'string',
|
|
468
|
+
description: 'Filter by person. "me" uses PRODUCTIVE_USER_ID. Omit for everything visible.',
|
|
469
|
+
},
|
|
470
|
+
date_from: { type: 'string', description: 'Only absences after this date (YYYY-MM-DD)' },
|
|
471
|
+
date_to: { type: 'string', description: 'Only absences before this date (YYYY-MM-DD)' },
|
|
472
|
+
absence_type: {
|
|
473
|
+
type: 'string',
|
|
474
|
+
description: 'Filter by absence type name, matched against list_absence_types',
|
|
475
|
+
},
|
|
476
|
+
approval_status: {
|
|
477
|
+
type: 'string',
|
|
478
|
+
description: 'Filter by approval status',
|
|
479
|
+
enum: ['approved', 'pending', 'rejected', 'canceled'],
|
|
480
|
+
},
|
|
481
|
+
include_remote_work: {
|
|
482
|
+
type: 'boolean',
|
|
483
|
+
description: 'Include remote work (working from home) bookings. Off by default: those people are working, not away. Ignored when absence_type names a remote work type, which is then always included.',
|
|
484
|
+
default: false,
|
|
485
|
+
},
|
|
486
|
+
include_notes: {
|
|
487
|
+
type: 'boolean',
|
|
488
|
+
description: 'Include the free-text note of each absence. Off by default: notes can carry sensitive personal detail.',
|
|
489
|
+
default: false,
|
|
490
|
+
},
|
|
491
|
+
limit: {
|
|
492
|
+
type: 'number',
|
|
493
|
+
description: 'Number of absences to return (1-200)',
|
|
494
|
+
minimum: 1,
|
|
495
|
+
maximum: 200,
|
|
496
|
+
default: 50,
|
|
497
|
+
},
|
|
498
|
+
},
|
|
499
|
+
required: [],
|
|
500
|
+
},
|
|
501
|
+
annotations: { title: 'List absences', readOnlyHint: true, openWorldHint: true },
|
|
502
|
+
};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { formatChangeset } from './changeset.js';
|
|
3
|
+
import { toMcpError } from '../utils/errors.js';
|
|
4
|
+
const ListActivitiesRequestSchema = z.object({
|
|
5
|
+
task_id: z.string().optional(),
|
|
6
|
+
project_id: z.string().optional(),
|
|
7
|
+
person_id: z.string().optional(),
|
|
8
|
+
item_type: z.string().optional(),
|
|
9
|
+
event: z.string().optional(),
|
|
10
|
+
after: z.string().optional(), // ISO 8601 date string
|
|
11
|
+
before: z.string().optional(), // ISO 8601 date string
|
|
12
|
+
days_back: z.coerce.number().min(1).max(365).optional(), // Helper for "last N days"
|
|
13
|
+
limit: z.coerce.number().min(1).max(200).optional(),
|
|
14
|
+
page: z.coerce.number().min(1).optional(),
|
|
15
|
+
});
|
|
16
|
+
export async function listActivities(client, args) {
|
|
17
|
+
try {
|
|
18
|
+
const params = ListActivitiesRequestSchema.parse(args);
|
|
19
|
+
// Helper: Convert days_back to 'after' parameter
|
|
20
|
+
let after = params.after;
|
|
21
|
+
if (params.days_back && !after) {
|
|
22
|
+
const date = new Date();
|
|
23
|
+
date.setDate(date.getDate() - params.days_back);
|
|
24
|
+
after = date.toISOString();
|
|
25
|
+
}
|
|
26
|
+
const response = await client.listActivities({
|
|
27
|
+
task_id: params.task_id,
|
|
28
|
+
project_id: params.project_id,
|
|
29
|
+
person_id: params.person_id,
|
|
30
|
+
item_type: params.item_type,
|
|
31
|
+
event: params.event,
|
|
32
|
+
after,
|
|
33
|
+
before: params.before,
|
|
34
|
+
limit: params.limit,
|
|
35
|
+
page: params.page,
|
|
36
|
+
});
|
|
37
|
+
const activities = response.data;
|
|
38
|
+
let output = `Found ${activities.length} activities`;
|
|
39
|
+
if (response.meta?.total_count) {
|
|
40
|
+
output += ` (${response.meta.total_count} total)`;
|
|
41
|
+
}
|
|
42
|
+
if (params.days_back) {
|
|
43
|
+
output += ` from the last ${params.days_back} days`;
|
|
44
|
+
}
|
|
45
|
+
else if (after || params.before) {
|
|
46
|
+
output += ` within specified date range`;
|
|
47
|
+
}
|
|
48
|
+
output += ':\n\n';
|
|
49
|
+
if (activities.length === 0) {
|
|
50
|
+
output += 'No activities found for the specified criteria.';
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
for (const activity of activities) {
|
|
54
|
+
const createdAt = new Date(activity.attributes.created_at).toLocaleString();
|
|
55
|
+
const event = activity.attributes.event;
|
|
56
|
+
const itemType = activity.attributes.item_type;
|
|
57
|
+
const itemId = activity.attributes.item_id;
|
|
58
|
+
output += `• ${createdAt} - ${event} ${itemType} (ID: ${itemId})`;
|
|
59
|
+
const changes = formatChangeset(activity.attributes.changeset);
|
|
60
|
+
if (changes.length > 0) {
|
|
61
|
+
output += `\n Changes: ${changes.join(', ')}`;
|
|
62
|
+
}
|
|
63
|
+
if (activity.relationships?.creator?.data?.id) {
|
|
64
|
+
output += `\n Creator: Person ID ${activity.relationships.creator.data.id}`;
|
|
65
|
+
}
|
|
66
|
+
output += '\n\n';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (response.links?.next) {
|
|
70
|
+
output += '\nUse page parameter to get more results.';
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
content: [
|
|
74
|
+
{
|
|
75
|
+
type: 'text',
|
|
76
|
+
text: output,
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
throw toMcpError(error);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export const listActivitiesTool = {
|
|
86
|
+
name: 'list_activities',
|
|
87
|
+
description: 'Retrieve the raw activity/audit feed: a chronological stream of create, update, and delete events on items (tasks, projects, etc.), each with a timestamp, event type, item type + ID, the changed fields, and the creator as a raw person ID (names are not resolved). Filter by task_id, project_id, person_id, item_type, event, or a date range (after/before, or the days_back helper); page through results with limit (max 200) and page. For a grouped high-level summary of only updates, use get_recent_updates instead.',
|
|
88
|
+
inputSchema: {
|
|
89
|
+
type: 'object',
|
|
90
|
+
properties: {
|
|
91
|
+
task_id: {
|
|
92
|
+
type: 'string',
|
|
93
|
+
description: 'Filter activities for a specific task ID',
|
|
94
|
+
},
|
|
95
|
+
project_id: {
|
|
96
|
+
type: 'string',
|
|
97
|
+
description: 'Filter activities for a specific project ID',
|
|
98
|
+
},
|
|
99
|
+
person_id: {
|
|
100
|
+
type: 'string',
|
|
101
|
+
description: 'Filter activities by a specific person/user ID',
|
|
102
|
+
},
|
|
103
|
+
item_type: {
|
|
104
|
+
type: 'string',
|
|
105
|
+
description: 'Filter by item type (e.g., "Task", "Project", "Workspace")',
|
|
106
|
+
},
|
|
107
|
+
event: {
|
|
108
|
+
type: 'string',
|
|
109
|
+
description: 'Filter by event type (e.g., "create", "update", "delete")',
|
|
110
|
+
},
|
|
111
|
+
after: {
|
|
112
|
+
type: 'string',
|
|
113
|
+
description: 'Filter activities after this date (ISO 8601 format, e.g., "2024-01-01T00:00:00Z")',
|
|
114
|
+
},
|
|
115
|
+
before: {
|
|
116
|
+
type: 'string',
|
|
117
|
+
description: 'Filter activities before this date (ISO 8601 format)',
|
|
118
|
+
},
|
|
119
|
+
days_back: {
|
|
120
|
+
type: 'number',
|
|
121
|
+
description: 'Filter activities from the last N days (1-365). Alternative to using "after"',
|
|
122
|
+
minimum: 1,
|
|
123
|
+
maximum: 365,
|
|
124
|
+
},
|
|
125
|
+
limit: {
|
|
126
|
+
type: 'number',
|
|
127
|
+
description: 'Maximum number of activities to return (1-200, default: 30)',
|
|
128
|
+
minimum: 1,
|
|
129
|
+
maximum: 200,
|
|
130
|
+
},
|
|
131
|
+
page: {
|
|
132
|
+
type: 'number',
|
|
133
|
+
description: 'Page number for pagination (starts from 1)',
|
|
134
|
+
minimum: 1,
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
additionalProperties: false,
|
|
138
|
+
},
|
|
139
|
+
annotations: { title: 'List activities', readOnlyHint: true, openWorldHint: true },
|
|
140
|
+
};
|