@teamnetwork-nz/xero-mcp-server 1.6.0
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 +21 -0
- package/README.md +282 -0
- package/dist/clients/xero-client.js +141 -0
- package/dist/consts/deeplinks.js +24 -0
- package/dist/handlers/approve-xero-payroll-timesheet.handler.js +28 -0
- package/dist/handlers/archive-xero-contact.handler.js +28 -0
- package/dist/handlers/authorise-xero-invoice.handler.js +34 -0
- package/dist/handlers/convert-xero-quote-to-invoice.handler.js +50 -0
- package/dist/handlers/create-xero-bank-transaction.handler.js +51 -0
- package/dist/handlers/create-xero-contact.handler.js +49 -0
- package/dist/handlers/create-xero-credit-note.handler.js +50 -0
- package/dist/handlers/create-xero-invoice.handler.js +53 -0
- package/dist/handlers/create-xero-item.handler.js +59 -0
- package/dist/handlers/create-xero-manual-journal.handler.js +57 -0
- package/dist/handlers/create-xero-payment.handler.js +50 -0
- package/dist/handlers/create-xero-payroll-timesheet.handler.js +28 -0
- package/dist/handlers/create-xero-quote.handler.js +53 -0
- package/dist/handlers/create-xero-tracking-category.handler.js +35 -0
- package/dist/handlers/create-xero-tracking-option.handler.js +31 -0
- package/dist/handlers/delete-xero-invoice-attachment.handler.js +30 -0
- package/dist/handlers/delete-xero-payroll-timesheet.handler.js +28 -0
- package/dist/handlers/download-xero-invoice-attachment.handler.js +23 -0
- package/dist/handlers/download-xero-invoice-pdf.handler.js +25 -0
- package/dist/handlers/get-xero-invoice.handler.js +19 -0
- package/dist/handlers/get-xero-payroll-timesheet.handler.js +28 -0
- package/dist/handlers/list-aged-payables-by-contact.handler.js +37 -0
- package/dist/handlers/list-aged-receivables-by-contact.handler.js +37 -0
- package/dist/handlers/list-xero-accounts.handler.js +32 -0
- package/dist/handlers/list-xero-bank-transactions.handler.js +39 -0
- package/dist/handlers/list-xero-contact-groups.handler.js +34 -0
- package/dist/handlers/list-xero-contacts.handler.js +47 -0
- package/dist/handlers/list-xero-credit-notes.handler.js +42 -0
- package/dist/handlers/list-xero-invoice-attachments.handler.js +20 -0
- package/dist/handlers/list-xero-invoices.handler.js +54 -0
- package/dist/handlers/list-xero-items.handler.js +32 -0
- package/dist/handlers/list-xero-manual-journals.handler.js +33 -0
- package/dist/handlers/list-xero-organisation-details.handler.js +32 -0
- package/dist/handlers/list-xero-payments.handler.js +52 -0
- package/dist/handlers/list-xero-payroll-employee-leave-balances.handler.js +42 -0
- package/dist/handlers/list-xero-payroll-employee-leave-types.handler.js +42 -0
- package/dist/handlers/list-xero-payroll-employee-leave.handler.js +44 -0
- package/dist/handlers/list-xero-payroll-employees.handler.js +31 -0
- package/dist/handlers/list-xero-payroll-leave-periods.handler.js +44 -0
- package/dist/handlers/list-xero-payroll-leave-types.handler.js +40 -0
- package/dist/handlers/list-xero-profit-and-loss.handler.js +52 -0
- package/dist/handlers/list-xero-quotes.handler.js +36 -0
- package/dist/handlers/list-xero-report-balance-sheet.handler.js +34 -0
- package/dist/handlers/list-xero-tax-rates.handler.js +30 -0
- package/dist/handlers/list-xero-timesheets.handler.js +29 -0
- package/dist/handlers/list-xero-tracking-categories.handler.js +29 -0
- package/dist/handlers/list-xero-trial-balance.handler.js +42 -0
- package/dist/handlers/revert-xero-payroll-timesheet.handler.js +28 -0
- package/dist/handlers/update-xero-bank-transaction.handler.js +56 -0
- package/dist/handlers/update-xero-contact.handler.js +66 -0
- package/dist/handlers/update-xero-credit-note.handler.js +61 -0
- package/dist/handlers/update-xero-invoice.handler.js +62 -0
- package/dist/handlers/update-xero-item.handler.js +46 -0
- package/dist/handlers/update-xero-manual-journal.handler.js +46 -0
- package/dist/handlers/update-xero-payroll-timesheet-add-line.handler.js +28 -0
- package/dist/handlers/update-xero-payroll-timesheet-update-line.handler.js +28 -0
- package/dist/handlers/update-xero-quote.handler.js +78 -0
- package/dist/handlers/update-xero-tracking-category.handler.js +43 -0
- package/dist/handlers/update-xero-tracking-options.handler.js +51 -0
- package/dist/handlers/upload-xero-invoice-attachment.handler.js +36 -0
- package/dist/handlers/void-xero-invoice.handler.js +52 -0
- package/dist/helpers/build-where-clause.js +29 -0
- package/dist/helpers/create-xero-tool.js +6 -0
- package/dist/helpers/datetime.js +91 -0
- package/dist/helpers/ensure-error.js +13 -0
- package/dist/helpers/format-aged-report-filter.js +8 -0
- package/dist/helpers/format-error.js +25 -0
- package/dist/helpers/format-line-item.js +13 -0
- package/dist/helpers/format-tracking-option.js +7 -0
- package/dist/helpers/get-client-headers.js +8 -0
- package/dist/helpers/get-deeplink.js +41 -0
- package/dist/helpers/get-download-path.js +16 -0
- package/dist/helpers/get-external-link.js +8 -0
- package/dist/helpers/get-package-version.js +20 -0
- package/dist/index.js +18 -0
- package/dist/resources/register-resources.js +27 -0
- package/dist/server/xero-mcp-server.js +43 -0
- package/dist/tools/create/convert-quote-to-invoice.tool.js +46 -0
- package/dist/tools/create/create-bank-transaction.tool.js +57 -0
- package/dist/tools/create/create-contact.tool.js +56 -0
- package/dist/tools/create/create-credit-note.tool.js +53 -0
- package/dist/tools/create/create-invoice.tool.js +78 -0
- package/dist/tools/create/create-item.tool.js +61 -0
- package/dist/tools/create/create-manual-journal.tool.js +116 -0
- package/dist/tools/create/create-payment.tool.js +71 -0
- package/dist/tools/create/create-payroll-timesheet.tool.js +40 -0
- package/dist/tools/create/create-quote.tool.js +57 -0
- package/dist/tools/create/create-tracking-category.tool.js +28 -0
- package/dist/tools/create/create-tracking-options.tool.js +30 -0
- package/dist/tools/create/index.js +35 -0
- package/dist/tools/create/upload-invoice-attachment.tool.js +49 -0
- package/dist/tools/delete/archive-contact.tool.js +30 -0
- package/dist/tools/delete/delete-invoice-attachment.tool.js +28 -0
- package/dist/tools/delete/delete-payroll-timesheet.tool.js +28 -0
- package/dist/tools/delete/index.js +15 -0
- package/dist/tools/get/get-invoice-attachment.tool.js +33 -0
- package/dist/tools/get/get-invoice-pdf.tool.js +33 -0
- package/dist/tools/get/get-invoice.tool.js +72 -0
- package/dist/tools/get/get-payroll-timesheet.tool.js +50 -0
- package/dist/tools/get/index.js +17 -0
- package/dist/tools/list/index.js +64 -0
- package/dist/tools/list/list-accounts.tool.js +39 -0
- package/dist/tools/list/list-aged-payables-by-contact.tool.js +51 -0
- package/dist/tools/list/list-aged-receivables-by-contact.tool.js +51 -0
- package/dist/tools/list/list-bank-transactions.tool.js +66 -0
- package/dist/tools/list/list-contact-groups.tool.js +48 -0
- package/dist/tools/list/list-contacts.tool.js +80 -0
- package/dist/tools/list/list-credit-notes.tool.js +72 -0
- package/dist/tools/list/list-invoice-attachments.tool.js +42 -0
- package/dist/tools/list/list-invoices.tool.js +98 -0
- package/dist/tools/list/list-items.tool.js +50 -0
- package/dist/tools/list/list-manual-journals.tool.js +80 -0
- package/dist/tools/list/list-organisation-details.tool.js +91 -0
- package/dist/tools/list/list-payments.tool.js +91 -0
- package/dist/tools/list/list-payroll-employee-leave-balances.tool.js +39 -0
- package/dist/tools/list/list-payroll-employee-leave-types.tool.js +57 -0
- package/dist/tools/list/list-payroll-employee-leave.tool.js +43 -0
- package/dist/tools/list/list-payroll-employees.tool.js +49 -0
- package/dist/tools/list/list-payroll-leave-periods.tool.js +45 -0
- package/dist/tools/list/list-payroll-leave-types.tool.js +40 -0
- package/dist/tools/list/list-payroll-timesheets.tool.js +40 -0
- package/dist/tools/list/list-profit-and-loss.tool.js +46 -0
- package/dist/tools/list/list-quotes.tool.js +69 -0
- package/dist/tools/list/list-report-balance-sheet.tool.js +39 -0
- package/dist/tools/list/list-tax-rates.tool.js +49 -0
- package/dist/tools/list/list-tracking-categories.tool.js +39 -0
- package/dist/tools/list/list-trial-balance.tool.js +42 -0
- package/dist/tools/tool-factory.js +24 -0
- package/dist/tools/update/approve-payroll-timesheet.tool.js +29 -0
- package/dist/tools/update/authorise-invoice.tool.js +42 -0
- package/dist/tools/update/index.js +39 -0
- package/dist/tools/update/revert-payroll-timesheet.tool.js +29 -0
- package/dist/tools/update/update-bank-transaction.tool.js +56 -0
- package/dist/tools/update/update-contact.tool.js +69 -0
- package/dist/tools/update/update-credit-note.tool.js +56 -0
- package/dist/tools/update/update-invoice.tool.js +76 -0
- package/dist/tools/update/update-item.tool.js +62 -0
- package/dist/tools/update/update-manual-journal-tool.js +89 -0
- package/dist/tools/update/update-payroll-timesheet-add-line.tool.js +34 -0
- package/dist/tools/update/update-payroll-timesheet-update-line.tool.js +35 -0
- package/dist/tools/update/update-quote.tool.js +61 -0
- package/dist/tools/update/update-tracking-category.tool.js +30 -0
- package/dist/tools/update/update-tracking-options.tool.js +35 -0
- package/dist/tools/update/void-invoice.tool.js +33 -0
- package/dist/types/list-report-balance-sheet-params.js +1 -0
- package/dist/types/timeframeType.js +1 -0
- package/dist/types/tool-definition.js +1 -0
- package/dist/types/tool-list.js +1 -0
- package/dist/types/tool-response.js +1 -0
- package/package.json +38 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
async function addTimesheetLine(timesheetID, timesheetLine) {
|
|
4
|
+
await xeroClient.authenticate();
|
|
5
|
+
// Call the createTimesheetLine endpoint from the PayrollNZApi
|
|
6
|
+
const createdLine = await xeroClient.payrollNZApi.createTimesheetLine(xeroClient.tenantId, timesheetID, timesheetLine);
|
|
7
|
+
return createdLine.body.timesheetLine ?? null;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Add a timesheet line to an existing payroll timesheet in Xero
|
|
11
|
+
*/
|
|
12
|
+
export async function updateXeroPayrollTimesheetAddLine(timesheetID, timesheetLine) {
|
|
13
|
+
try {
|
|
14
|
+
const newLine = await addTimesheetLine(timesheetID, timesheetLine);
|
|
15
|
+
return {
|
|
16
|
+
result: newLine,
|
|
17
|
+
isError: false,
|
|
18
|
+
error: null,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
return {
|
|
23
|
+
result: null,
|
|
24
|
+
isError: true,
|
|
25
|
+
error: formatError(error),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
async function updateTimesheetLine(timesheetID, timesheetLineID, timesheetLine) {
|
|
4
|
+
await xeroClient.authenticate();
|
|
5
|
+
// Call the updateTimesheetLine endpoint from the PayrollNZApi
|
|
6
|
+
const updatedLine = await xeroClient.payrollNZApi.updateTimesheetLine(xeroClient.tenantId, timesheetID, timesheetLineID, timesheetLine);
|
|
7
|
+
return updatedLine.body.timesheetLine ?? null;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Update an existing timesheet line in a payroll timesheet in Xero
|
|
11
|
+
*/
|
|
12
|
+
export async function updateXeroPayrollTimesheetUpdateLine(timesheetID, timesheetLineID, timesheetLine) {
|
|
13
|
+
try {
|
|
14
|
+
const updatedLine = await updateTimesheetLine(timesheetID, timesheetLineID, timesheetLine);
|
|
15
|
+
return {
|
|
16
|
+
result: updatedLine,
|
|
17
|
+
isError: false,
|
|
18
|
+
error: null,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
return {
|
|
23
|
+
result: null,
|
|
24
|
+
isError: true,
|
|
25
|
+
error: formatError(error),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
import { QuoteStatusCodes } from "xero-node";
|
|
4
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
5
|
+
async function getQuote(quoteId) {
|
|
6
|
+
await xeroClient.authenticate();
|
|
7
|
+
// First, get the current quote to check its status
|
|
8
|
+
const response = await xeroClient.accountingApi.getQuote(xeroClient.tenantId, // tenantId
|
|
9
|
+
quoteId, // quoteId
|
|
10
|
+
getClientHeaders());
|
|
11
|
+
return response.body.quotes?.[0];
|
|
12
|
+
}
|
|
13
|
+
async function updateQuote(quoteId, lineItems, reference, terms, title, summary, quoteNumber, contactId, date, expiryDate, existingQuote) {
|
|
14
|
+
// Create quote object with only the fields that are being updated
|
|
15
|
+
const quote = {
|
|
16
|
+
lineItems: lineItems,
|
|
17
|
+
reference: reference,
|
|
18
|
+
terms: terms,
|
|
19
|
+
title: title,
|
|
20
|
+
summary: summary,
|
|
21
|
+
quoteNumber: quoteNumber,
|
|
22
|
+
expiryDate: expiryDate,
|
|
23
|
+
};
|
|
24
|
+
// Only add contact if contactId is provided, otherwise use existing
|
|
25
|
+
if (contactId) {
|
|
26
|
+
quote.contact = { contactID: contactId };
|
|
27
|
+
}
|
|
28
|
+
else if (existingQuote?.contact) {
|
|
29
|
+
quote.contact = existingQuote.contact;
|
|
30
|
+
}
|
|
31
|
+
// Only add date if provided, otherwise use existing
|
|
32
|
+
if (date) {
|
|
33
|
+
quote.date = date;
|
|
34
|
+
}
|
|
35
|
+
else if (existingQuote?.date) {
|
|
36
|
+
quote.date = existingQuote.date;
|
|
37
|
+
}
|
|
38
|
+
const response = await xeroClient.accountingApi.updateQuote(xeroClient.tenantId, quoteId, // quoteId
|
|
39
|
+
{
|
|
40
|
+
quotes: [quote],
|
|
41
|
+
}, // quotes
|
|
42
|
+
undefined, // idempotencyKey
|
|
43
|
+
getClientHeaders());
|
|
44
|
+
return response.body.quotes?.[0];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Update an existing quote in Xero
|
|
48
|
+
*/
|
|
49
|
+
export async function updateXeroQuote(quoteId, lineItems, reference, terms, title, summary, quoteNumber, contactId, date, expiryDate) {
|
|
50
|
+
try {
|
|
51
|
+
const existingQuote = await getQuote(quoteId);
|
|
52
|
+
const quoteStatus = existingQuote?.status;
|
|
53
|
+
// Only allow updates to DRAFT quotes
|
|
54
|
+
if (quoteStatus !== QuoteStatusCodes.DRAFT) {
|
|
55
|
+
return {
|
|
56
|
+
result: null,
|
|
57
|
+
isError: true,
|
|
58
|
+
error: `Cannot update quote because it is not a draft. Current status: ${quoteStatus}`,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const updatedQuote = await updateQuote(quoteId, lineItems, reference, terms, title, summary, quoteNumber, contactId, date, expiryDate, existingQuote);
|
|
62
|
+
if (!updatedQuote) {
|
|
63
|
+
throw new Error("Quote update failed.");
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
result: updatedQuote,
|
|
67
|
+
isError: false,
|
|
68
|
+
error: null,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
return {
|
|
73
|
+
result: null,
|
|
74
|
+
isError: true,
|
|
75
|
+
error: formatError(error),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { TrackingCategory } from "xero-node";
|
|
2
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
3
|
+
import { formatError } from "../helpers/format-error.js";
|
|
4
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
5
|
+
async function getTrackingCategory(trackingCategoryId) {
|
|
6
|
+
await xeroClient.authenticate();
|
|
7
|
+
const response = await xeroClient.accountingApi.getTrackingCategory(xeroClient.tenantId, trackingCategoryId, getClientHeaders());
|
|
8
|
+
return response.body.trackingCategories?.[0];
|
|
9
|
+
}
|
|
10
|
+
async function updateTrackingCategory(trackingCategoryId, existingTrackingCategory, name, status) {
|
|
11
|
+
const trackingCategory = {
|
|
12
|
+
trackingCategoryID: trackingCategoryId,
|
|
13
|
+
name: name ? name : existingTrackingCategory.name,
|
|
14
|
+
status: status ? TrackingCategory.StatusEnum[status] : existingTrackingCategory.status
|
|
15
|
+
};
|
|
16
|
+
await xeroClient.accountingApi.updateTrackingCategory(xeroClient.tenantId, trackingCategoryId, trackingCategory, undefined, // idempotencyKey
|
|
17
|
+
getClientHeaders());
|
|
18
|
+
return trackingCategory;
|
|
19
|
+
}
|
|
20
|
+
export async function updateXeroTrackingCategory(trackingCategoryId, name, status) {
|
|
21
|
+
try {
|
|
22
|
+
const existingTrackingCategory = await getTrackingCategory(trackingCategoryId);
|
|
23
|
+
if (!existingTrackingCategory) {
|
|
24
|
+
throw new Error("Could not find tracking category.");
|
|
25
|
+
}
|
|
26
|
+
const updatedTrackingCategory = await updateTrackingCategory(trackingCategoryId, existingTrackingCategory, name, status);
|
|
27
|
+
if (!updatedTrackingCategory) {
|
|
28
|
+
throw new Error("Failed to update tracking category.");
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
result: existingTrackingCategory,
|
|
32
|
+
isError: false,
|
|
33
|
+
error: null
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
return {
|
|
38
|
+
result: null,
|
|
39
|
+
isError: true,
|
|
40
|
+
error: formatError(error),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { TrackingOption } from "xero-node";
|
|
2
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
3
|
+
import { formatError } from "../helpers/format-error.js";
|
|
4
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
5
|
+
async function getTrackingOptions(trackingCategoryId) {
|
|
6
|
+
await xeroClient.authenticate();
|
|
7
|
+
const response = await xeroClient.accountingApi.getTrackingCategory(xeroClient.tenantId, trackingCategoryId, getClientHeaders());
|
|
8
|
+
return response.body.trackingCategories?.[0].options;
|
|
9
|
+
}
|
|
10
|
+
async function updateTrackingOption(trackingCategoryId, trackingOptionId, existingTrackingOption, name, status) {
|
|
11
|
+
const trackingOption = {
|
|
12
|
+
trackingOptionID: trackingOptionId,
|
|
13
|
+
name: name ? name : existingTrackingOption.name,
|
|
14
|
+
status: status ? TrackingOption.StatusEnum[status] : existingTrackingOption.status
|
|
15
|
+
};
|
|
16
|
+
await xeroClient.accountingApi.updateTrackingOptions(xeroClient.tenantId, trackingCategoryId, trackingOptionId, trackingOption, undefined, // idempotencyKey
|
|
17
|
+
getClientHeaders());
|
|
18
|
+
return trackingOption;
|
|
19
|
+
}
|
|
20
|
+
export async function updateXeroTrackingOption(trackingCategoryId, options) {
|
|
21
|
+
try {
|
|
22
|
+
const existingTrackingOptions = await getTrackingOptions(trackingCategoryId);
|
|
23
|
+
if (!existingTrackingOptions) {
|
|
24
|
+
throw new Error("Could not find tracking options.");
|
|
25
|
+
}
|
|
26
|
+
const updatedTrackingOptions = await Promise.all(options?.map(async (option) => {
|
|
27
|
+
const existingTrackingOption = existingTrackingOptions
|
|
28
|
+
.find(existingOption => existingOption.trackingOptionID === option.trackingOptionId);
|
|
29
|
+
return existingTrackingOption
|
|
30
|
+
? await updateTrackingOption(trackingCategoryId, option.trackingOptionId, existingTrackingOption, option.name, option.status)
|
|
31
|
+
: undefined;
|
|
32
|
+
}));
|
|
33
|
+
if (!updatedTrackingOptions) {
|
|
34
|
+
throw new Error("Failed to update tracking options.");
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
result: updatedTrackingOptions
|
|
38
|
+
.filter(Boolean)
|
|
39
|
+
.map(option => option),
|
|
40
|
+
isError: false,
|
|
41
|
+
error: null
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
return {
|
|
46
|
+
result: null,
|
|
47
|
+
isError: true,
|
|
48
|
+
error: formatError(error),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
4
|
+
import { formatError } from "../helpers/format-error.js";
|
|
5
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
6
|
+
/**
|
|
7
|
+
* Upload a local file as an attachment to an invoice.
|
|
8
|
+
*
|
|
9
|
+
* @param invoiceId Xero invoice ID
|
|
10
|
+
* @param filePath Absolute path to the local file to upload
|
|
11
|
+
* @param fileName The filename to use in Xero (defaults to the basename of filePath)
|
|
12
|
+
* @param includeOnline Whether to show the attachment in the Xero online invoice portal
|
|
13
|
+
*/
|
|
14
|
+
export async function uploadXeroInvoiceAttachment(invoiceId, filePath, fileName, includeOnline = false) {
|
|
15
|
+
try {
|
|
16
|
+
await xeroClient.authenticate();
|
|
17
|
+
if (!fs.existsSync(filePath)) {
|
|
18
|
+
return {
|
|
19
|
+
result: null,
|
|
20
|
+
isError: true,
|
|
21
|
+
error: `File not found: ${filePath}`,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const resolvedName = fileName || path.basename(filePath);
|
|
25
|
+
const fileBuffer = fs.readFileSync(filePath);
|
|
26
|
+
const response = await xeroClient.accountingApi.createInvoiceAttachmentByFileName(xeroClient.tenantId, invoiceId, resolvedName, fileBuffer, includeOnline, undefined, // idempotencyKey
|
|
27
|
+
getClientHeaders());
|
|
28
|
+
const attachment = response.body.attachments?.[0];
|
|
29
|
+
if (!attachment)
|
|
30
|
+
throw new Error("Upload succeeded but no attachment was returned.");
|
|
31
|
+
return { result: attachment, isError: false, error: null };
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
return { result: null, isError: true, error: formatError(error) };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
import { Invoice } from "xero-node";
|
|
4
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
5
|
+
async function setInvoiceStatus(invoiceId, status) {
|
|
6
|
+
await xeroClient.authenticate();
|
|
7
|
+
const response = await xeroClient.accountingApi.updateInvoice(xeroClient.tenantId, invoiceId, { invoices: [{ status }] }, undefined, // unitdp
|
|
8
|
+
undefined, // idempotencyKey
|
|
9
|
+
getClientHeaders());
|
|
10
|
+
return response.body.invoices?.[0];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Void or delete an invoice in Xero.
|
|
14
|
+
* AUTHORISED invoices are set to VOIDED; DRAFT invoices are set to DELETED.
|
|
15
|
+
*/
|
|
16
|
+
export async function voidXeroInvoice(invoiceId) {
|
|
17
|
+
try {
|
|
18
|
+
await xeroClient.authenticate();
|
|
19
|
+
const existing = await xeroClient.accountingApi.getInvoice(xeroClient.tenantId, invoiceId, undefined, getClientHeaders());
|
|
20
|
+
const invoice = existing.body.invoices?.[0];
|
|
21
|
+
if (!invoice) {
|
|
22
|
+
throw new Error("Invoice not found.");
|
|
23
|
+
}
|
|
24
|
+
const { status } = invoice;
|
|
25
|
+
if (status === Invoice.StatusEnum.PAID) {
|
|
26
|
+
return {
|
|
27
|
+
result: null,
|
|
28
|
+
isError: true,
|
|
29
|
+
error: "Cannot void a paid invoice. Allocate a credit note or payment reversal instead.",
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (status === Invoice.StatusEnum.VOIDED || status === Invoice.StatusEnum.DELETED) {
|
|
33
|
+
return {
|
|
34
|
+
result: null,
|
|
35
|
+
isError: true,
|
|
36
|
+
error: `Invoice is already ${status}.`,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
// DRAFT → DELETED, everything else (SUBMITTED, AUTHORISED) → VOIDED
|
|
40
|
+
const newStatus = status === Invoice.StatusEnum.DRAFT
|
|
41
|
+
? Invoice.StatusEnum.DELETED
|
|
42
|
+
: Invoice.StatusEnum.VOIDED;
|
|
43
|
+
const updated = await setInvoiceStatus(invoiceId, newStatus);
|
|
44
|
+
if (!updated) {
|
|
45
|
+
throw new Error("Failed to void invoice.");
|
|
46
|
+
}
|
|
47
|
+
return { result: updated, isError: false, error: null };
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
return { result: null, isError: true, error: formatError(error) };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build DateTime range conditions for a given field.
|
|
3
|
+
* Dates must be in YYYY-MM-DD format.
|
|
4
|
+
*/
|
|
5
|
+
export function dateWhereConditions(field, fromDate, toDate) {
|
|
6
|
+
const parts = [];
|
|
7
|
+
if (fromDate)
|
|
8
|
+
parts.push(`${field} >= DateTime(${fromDate.replace(/-/g, ",")})`);
|
|
9
|
+
if (toDate)
|
|
10
|
+
parts.push(`${field} <= DateTime(${toDate.replace(/-/g, ",")})`);
|
|
11
|
+
return parts;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Build an OR block for multiple status values.
|
|
15
|
+
* e.g. ["DRAFT", "AUTHORISED"] → ["(Status==\"DRAFT\" OR Status==\"AUTHORISED\")"]
|
|
16
|
+
*/
|
|
17
|
+
export function statusWhereConditions(field, statuses) {
|
|
18
|
+
if (!statuses.length)
|
|
19
|
+
return [];
|
|
20
|
+
const inner = statuses.map((s) => `${field}=="${s}"`).join(" OR ");
|
|
21
|
+
return [`(${inner})`];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Join an array of condition strings into a single WHERE clause,
|
|
25
|
+
* returning undefined if there are no conditions.
|
|
26
|
+
*/
|
|
27
|
+
export function joinWhere(parts) {
|
|
28
|
+
return parts.length ? parts.join(" AND ") : undefined;
|
|
29
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Timezone-aware datetime helpers.
|
|
3
|
+
*
|
|
4
|
+
* DATETIMEFORMAT env var controls which timezone is used for all input/output:
|
|
5
|
+
* unset or "local" → system local timezone (auto-detected)
|
|
6
|
+
* "<IANA tz>" → e.g. "Pacific/Auckland", "America/New_York"
|
|
7
|
+
*
|
|
8
|
+
* Dates sent to the Xero API are date-only strings (YYYY-MM-DD) interpreted by
|
|
9
|
+
* Xero in the organisation's own timezone, so no UTC conversion is needed for
|
|
10
|
+
* those. The helpers here handle:
|
|
11
|
+
* 1. "Today" default generation — use the configured timezone, not UTC
|
|
12
|
+
* 2. UTC datetime display — convert API timestamps to the configured timezone
|
|
13
|
+
* 3. Local→UTC parsing — for parameters that Xero treats as full timestamps
|
|
14
|
+
*/
|
|
15
|
+
function getTimezone() {
|
|
16
|
+
const setting = process.env.DATETIMEFORMAT;
|
|
17
|
+
if (!setting || setting === "local") {
|
|
18
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
19
|
+
}
|
|
20
|
+
return setting;
|
|
21
|
+
}
|
|
22
|
+
function dateParts(d, tz) {
|
|
23
|
+
const parts = new Intl.DateTimeFormat("en-CA", {
|
|
24
|
+
timeZone: tz,
|
|
25
|
+
year: "numeric",
|
|
26
|
+
month: "2-digit",
|
|
27
|
+
day: "2-digit",
|
|
28
|
+
hour: "2-digit",
|
|
29
|
+
minute: "2-digit",
|
|
30
|
+
second: "2-digit",
|
|
31
|
+
hour12: false,
|
|
32
|
+
}).formatToParts(d);
|
|
33
|
+
const get = (type) => parts.find((p) => p.type === type)?.value ?? "00";
|
|
34
|
+
return {
|
|
35
|
+
year: get("year"),
|
|
36
|
+
month: get("month"),
|
|
37
|
+
day: get("day"),
|
|
38
|
+
hour: get("hour"),
|
|
39
|
+
minute: get("minute"),
|
|
40
|
+
second: get("second"),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Returns today's date (or today ± offsetDays) as YYYY-MM-DD in the
|
|
45
|
+
* configured timezone.
|
|
46
|
+
*/
|
|
47
|
+
export function localDateString(offsetDays = 0) {
|
|
48
|
+
const tz = getTimezone();
|
|
49
|
+
const d = new Date(Date.now() + offsetDays * 24 * 60 * 60 * 1000);
|
|
50
|
+
const { year, month, day } = dateParts(d, tz);
|
|
51
|
+
return `${year}-${month}-${day}`;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns { year, month, day } for today in the configured timezone.
|
|
55
|
+
* Useful for building Xero WHERE-clause DateTime() values for "today".
|
|
56
|
+
*/
|
|
57
|
+
export function todayComponents() {
|
|
58
|
+
const tz = getTimezone();
|
|
59
|
+
const { year, month, day } = dateParts(new Date(), tz);
|
|
60
|
+
return { year: Number(year), month: Number(month), day: Number(day) };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Formats a UTC Date object or ISO string for human display in the configured
|
|
64
|
+
* timezone (YYYY-MM-DD HH:mm:ss).
|
|
65
|
+
*/
|
|
66
|
+
export function formatUtcDatetime(val) {
|
|
67
|
+
if (!val)
|
|
68
|
+
return "";
|
|
69
|
+
const d = val instanceof Date ? val : new Date(val);
|
|
70
|
+
if (isNaN(d.getTime()))
|
|
71
|
+
return String(val);
|
|
72
|
+
const tz = getTimezone();
|
|
73
|
+
const { year, month, day, hour, minute, second } = dateParts(d, tz);
|
|
74
|
+
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Converts a local date string (YYYY-MM-DD) to a UTC Date, treating the
|
|
78
|
+
* input as midnight in the configured timezone.
|
|
79
|
+
*/
|
|
80
|
+
export function parseLocalDate(dateStr) {
|
|
81
|
+
const tz = getTimezone();
|
|
82
|
+
const [year, month, day] = dateStr.split("-").map(Number);
|
|
83
|
+
// Use noon UTC as reference to stay well away from DST transitions at midnight
|
|
84
|
+
const referenceUtc = Date.UTC(year, month - 1, day, 12, 0, 0);
|
|
85
|
+
const p = dateParts(new Date(referenceUtc), tz);
|
|
86
|
+
// Compute the UTC offset: localAsUtcEpoch − referenceUtc gives offsetMs
|
|
87
|
+
const localAsUtcEpoch = Date.UTC(Number(p.year), Number(p.month) - 1, Number(p.day), Number(p.hour), Number(p.minute), Number(p.second));
|
|
88
|
+
const offsetMs = localAsUtcEpoch - referenceUtc;
|
|
89
|
+
// Local midnight on dateStr, expressed as UTC
|
|
90
|
+
return new Date(Date.UTC(year, month - 1, day, 0, 0, 0) - offsetMs);
|
|
91
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function ensureError(value) {
|
|
2
|
+
if (value instanceof Error)
|
|
3
|
+
return value;
|
|
4
|
+
let stringified = "[Unable to stringify the thrown value]";
|
|
5
|
+
try {
|
|
6
|
+
stringified = JSON.stringify(value);
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
/* empty */
|
|
10
|
+
}
|
|
11
|
+
const error = new Error(`Error thrown: ${stringified}`);
|
|
12
|
+
return error;
|
|
13
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const formatAgedReportFilter = (fromDate, toDate) => {
|
|
2
|
+
if (!fromDate && !toDate) {
|
|
3
|
+
return undefined;
|
|
4
|
+
}
|
|
5
|
+
const formattedFromDate = fromDate ? `from ${fromDate}` : "";
|
|
6
|
+
const formattedToDate = toDate ? `to ${toDate}` : "";
|
|
7
|
+
return `Showing invoices ${formattedFromDate} ${formattedToDate}`;
|
|
8
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AxiosError } from "axios";
|
|
2
|
+
/**
|
|
3
|
+
* Format error messages in a user-friendly way
|
|
4
|
+
*/
|
|
5
|
+
export function formatError(error) {
|
|
6
|
+
if (error instanceof AxiosError) {
|
|
7
|
+
const status = error.response?.status;
|
|
8
|
+
const detail = error.response?.data?.Detail;
|
|
9
|
+
switch (status) {
|
|
10
|
+
case 401:
|
|
11
|
+
return "Authentication failed. Please check your Xero credentials.";
|
|
12
|
+
case 403:
|
|
13
|
+
return "You don't have permission to access this resource in Xero.";
|
|
14
|
+
case 404:
|
|
15
|
+
return "The requested resource was not found in Xero.";
|
|
16
|
+
case 429:
|
|
17
|
+
return "Too many requests to Xero. Please try again in a moment.";
|
|
18
|
+
default:
|
|
19
|
+
return detail || "An error occurred while communicating with Xero.";
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return error instanceof Error
|
|
23
|
+
? error.message
|
|
24
|
+
: `An unexpected error occurred: ${error}`;
|
|
25
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const formatLineItem = (lineItem) => {
|
|
2
|
+
return [
|
|
3
|
+
`Item ID: ${lineItem.item}`,
|
|
4
|
+
`Item Code: ${lineItem.itemCode}`,
|
|
5
|
+
`Description: ${lineItem.description}`,
|
|
6
|
+
`Quantity: ${lineItem.quantity}`,
|
|
7
|
+
`Unit Amount: ${lineItem.unitAmount}`,
|
|
8
|
+
`Account Code: ${lineItem.accountCode}`,
|
|
9
|
+
`Tax Type: ${lineItem.taxType}`,
|
|
10
|
+
`Tracking: ${lineItem.tracking}`,
|
|
11
|
+
`Line Amount: ${lineItem.lineAmount}`,
|
|
12
|
+
].join("\n");
|
|
13
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { contactDeepLink, creditNoteDeepLink, invoiceDeepLink, paymentDeepLink, manualJournalDeepLink, quoteDeepLink, billDeepLink, } from "../consts/deeplinks.js";
|
|
3
|
+
export var DeepLinkType;
|
|
4
|
+
(function (DeepLinkType) {
|
|
5
|
+
DeepLinkType[DeepLinkType["CONTACT"] = 0] = "CONTACT";
|
|
6
|
+
DeepLinkType[DeepLinkType["CREDIT_NOTE"] = 1] = "CREDIT_NOTE";
|
|
7
|
+
DeepLinkType[DeepLinkType["INVOICE"] = 2] = "INVOICE";
|
|
8
|
+
DeepLinkType[DeepLinkType["MANUAL_JOURNAL"] = 3] = "MANUAL_JOURNAL";
|
|
9
|
+
DeepLinkType[DeepLinkType["QUOTE"] = 4] = "QUOTE";
|
|
10
|
+
DeepLinkType[DeepLinkType["PAYMENT"] = 5] = "PAYMENT";
|
|
11
|
+
DeepLinkType[DeepLinkType["BILL"] = 6] = "BILL";
|
|
12
|
+
})(DeepLinkType || (DeepLinkType = {}));
|
|
13
|
+
/**
|
|
14
|
+
* Gets a deep link for a specific type and item ID.
|
|
15
|
+
* This will also fetch the org short code from the Xero client.
|
|
16
|
+
* @param type
|
|
17
|
+
* @param itemId
|
|
18
|
+
* @returns
|
|
19
|
+
*/
|
|
20
|
+
export const getDeepLink = async (type, itemId) => {
|
|
21
|
+
const orgShortCode = await xeroClient.getShortCode();
|
|
22
|
+
if (!orgShortCode) {
|
|
23
|
+
throw new Error("Failed to retrieve organisation short code");
|
|
24
|
+
}
|
|
25
|
+
switch (type) {
|
|
26
|
+
case DeepLinkType.CONTACT:
|
|
27
|
+
return contactDeepLink(orgShortCode, itemId);
|
|
28
|
+
case DeepLinkType.CREDIT_NOTE:
|
|
29
|
+
return creditNoteDeepLink(orgShortCode, itemId);
|
|
30
|
+
case DeepLinkType.MANUAL_JOURNAL:
|
|
31
|
+
return manualJournalDeepLink(itemId);
|
|
32
|
+
case DeepLinkType.INVOICE:
|
|
33
|
+
return invoiceDeepLink(orgShortCode, itemId);
|
|
34
|
+
case DeepLinkType.QUOTE:
|
|
35
|
+
return quoteDeepLink(orgShortCode, itemId);
|
|
36
|
+
case DeepLinkType.PAYMENT:
|
|
37
|
+
return paymentDeepLink(orgShortCode, itemId);
|
|
38
|
+
case DeepLinkType.BILL:
|
|
39
|
+
return billDeepLink(orgShortCode, itemId);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as os from "os";
|
|
3
|
+
import * as path from "path";
|
|
4
|
+
/**
|
|
5
|
+
* Returns the directory to save downloaded files to.
|
|
6
|
+
* Uses XERO_DOWNLOAD_PATH env var if set, otherwise falls back to ~/Downloads.
|
|
7
|
+
* Creates the directory if it doesn't exist.
|
|
8
|
+
*/
|
|
9
|
+
export function getDownloadPath() {
|
|
10
|
+
const dir = process.env.XERO_DOWNLOAD_PATH ||
|
|
11
|
+
path.join(os.homedir(), "Downloads");
|
|
12
|
+
if (!fs.existsSync(dir)) {
|
|
13
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
14
|
+
}
|
|
15
|
+
return dir;
|
|
16
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { readFileSync } from 'fs';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
import { dirname, join } from 'path';
|
|
4
|
+
/**
|
|
5
|
+
* Gets the package version from package.json
|
|
6
|
+
* @returns The package version string
|
|
7
|
+
*/
|
|
8
|
+
export function getPackageVersion() {
|
|
9
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
10
|
+
const __dirname = dirname(__filename);
|
|
11
|
+
const packagePath = join(__dirname, '../../package.json');
|
|
12
|
+
try {
|
|
13
|
+
const packageJson = JSON.parse(readFileSync(packagePath, 'utf-8'));
|
|
14
|
+
return packageJson.version;
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
console.warn('Failed to read package version:', error);
|
|
18
|
+
return '0.0.0'; // Fallback version
|
|
19
|
+
}
|
|
20
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
|
+
import { XeroMcpServer } from "./server/xero-mcp-server.js";
|
|
4
|
+
import { ToolFactory } from "./tools/tool-factory.js";
|
|
5
|
+
import { registerResources } from "./resources/register-resources.js";
|
|
6
|
+
const main = async () => {
|
|
7
|
+
// Create an MCP server
|
|
8
|
+
const server = XeroMcpServer.GetServer();
|
|
9
|
+
ToolFactory(server);
|
|
10
|
+
registerResources(server);
|
|
11
|
+
// Start receiving messages on stdin and sending messages on stdout
|
|
12
|
+
const transport = new StdioServerTransport();
|
|
13
|
+
await server.connect(transport);
|
|
14
|
+
};
|
|
15
|
+
main().catch((error) => {
|
|
16
|
+
console.error("Error:", error);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
});
|