@mindstone/mcp-server-xero 0.0.16 → 0.0.17
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/README.md +2 -1
- package/dist/handlers/create-xero-invoice.handler.js +2 -0
- package/dist/handlers/list-xero-currencies.handler.js +22 -0
- package/dist/handlers/update-xero-invoice.handler.js +2 -0
- package/dist/helpers/format-error.js +78 -9
- package/dist/helpers/xero-currencies.js +23 -0
- package/dist/tools/list/index.js +2 -0
- package/dist/tools/list/list-currencies.tool.js +34 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -116,6 +116,7 @@ NOTE: The `XERO_CLIENT_BEARER_TOKEN` will take precedence over the `XERO_CLIENT_
|
|
|
116
116
|
- `list-aged-receivables-by-contact`: Retrieves aged receivables for a contact
|
|
117
117
|
- `list-aged-payables-by-contact`: Retrieves aged payables for a contact
|
|
118
118
|
- `list-contact-groups`: Retrieve a list of contact groups
|
|
119
|
+
- `list-currencies`: Retrieve currencies enabled in the connected Xero organisation
|
|
119
120
|
- `list-invoice-attachments`: Retrieve files attached to a Xero invoice
|
|
120
121
|
- `create-contact`: Create a new contact
|
|
121
122
|
- `create-credit-note`: Create a new credit note
|
|
@@ -125,7 +126,7 @@ NOTE: The `XERO_CLIENT_BEARER_TOKEN` will take precedence over the `XERO_CLIENT_
|
|
|
125
126
|
- `create-credit-note`: Create a new credit note
|
|
126
127
|
- `create-payroll-timesheet`: Create a new Payroll Timesheet
|
|
127
128
|
- `update-contact`: Update an existing contact
|
|
128
|
-
- `update-invoice`: Update an existing draft invoice. Supports optional `currencyCode`, such as `USD`,
|
|
129
|
+
- `update-invoice`: Update an existing draft invoice. Supports optional `currencyCode`, such as `USD`, when that currency is enabled in the connected Xero organisation.
|
|
129
130
|
- `update-quote`: Update an existing draft quote
|
|
130
131
|
- `update-credit-note`: Update an existing draft credit note
|
|
131
132
|
- `get-invoice-history`: Retrieve the history records for an invoice
|
|
@@ -2,8 +2,10 @@ import { xeroClient } from "../clients/xero-client.js";
|
|
|
2
2
|
import { formatError } from "../helpers/format-error.js";
|
|
3
3
|
import { Invoice } from "xero-node";
|
|
4
4
|
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
5
|
+
import { assertXeroCurrencyEnabled } from "../helpers/xero-currencies.js";
|
|
5
6
|
async function createInvoice(contactId, lineItems, type, reference, date, currencyCode) {
|
|
6
7
|
await xeroClient.authenticate();
|
|
8
|
+
await assertXeroCurrencyEnabled(currencyCode);
|
|
7
9
|
const invoice = {
|
|
8
10
|
type: type,
|
|
9
11
|
contact: {
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { formatError } from "../helpers/format-error.js";
|
|
2
|
+
import { listXeroCurrencies } from "../helpers/xero-currencies.js";
|
|
3
|
+
/**
|
|
4
|
+
* List currencies enabled in the connected Xero organisation.
|
|
5
|
+
*/
|
|
6
|
+
export async function listEnabledXeroCurrencies() {
|
|
7
|
+
try {
|
|
8
|
+
const currencies = await listXeroCurrencies();
|
|
9
|
+
return {
|
|
10
|
+
result: currencies,
|
|
11
|
+
isError: false,
|
|
12
|
+
error: null,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
return {
|
|
17
|
+
result: null,
|
|
18
|
+
isError: true,
|
|
19
|
+
error: formatError(error),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -2,6 +2,7 @@ import { xeroClient } from "../clients/xero-client.js";
|
|
|
2
2
|
import { formatError } from "../helpers/format-error.js";
|
|
3
3
|
import { Invoice } from "xero-node";
|
|
4
4
|
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
5
|
+
import { assertXeroCurrencyEnabled } from "../helpers/xero-currencies.js";
|
|
5
6
|
async function getInvoice(invoiceId) {
|
|
6
7
|
await xeroClient.authenticate();
|
|
7
8
|
// First, get the current invoice to check its status
|
|
@@ -43,6 +44,7 @@ export async function updateXeroInvoice(invoiceId, lineItems, reference, dueDate
|
|
|
43
44
|
error: `Cannot update invoice because it is not a draft. Current status: ${invoiceStatus}`,
|
|
44
45
|
};
|
|
45
46
|
}
|
|
47
|
+
await assertXeroCurrencyEnabled(currencyCode);
|
|
46
48
|
const updatedInvoice = await updateInvoice(invoiceId, lineItems, reference, dueDate, date, contactId, currencyCode);
|
|
47
49
|
if (!updatedInvoice) {
|
|
48
50
|
throw new Error("Invoice update failed.");
|
|
@@ -5,7 +5,8 @@ function isXeroSdkError(error) {
|
|
|
5
5
|
const response = error.response;
|
|
6
6
|
if (typeof response !== "object" || response === null)
|
|
7
7
|
return false;
|
|
8
|
-
return typeof response.statusCode === "number"
|
|
8
|
+
return (typeof response.statusCode === "number" ||
|
|
9
|
+
typeof response.status === "number");
|
|
9
10
|
}
|
|
10
11
|
function formatHttpStatus(status) {
|
|
11
12
|
switch (status) {
|
|
@@ -21,6 +22,69 @@ function formatHttpStatus(status) {
|
|
|
21
22
|
return "";
|
|
22
23
|
}
|
|
23
24
|
}
|
|
25
|
+
function isRecord(value) {
|
|
26
|
+
return typeof value === "object" && value !== null;
|
|
27
|
+
}
|
|
28
|
+
function getString(record, key) {
|
|
29
|
+
const value = record?.[key];
|
|
30
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
31
|
+
}
|
|
32
|
+
function getXeroBody(response) {
|
|
33
|
+
return response.body ?? response.data;
|
|
34
|
+
}
|
|
35
|
+
function getValidationMessages(body) {
|
|
36
|
+
if (!isRecord(body))
|
|
37
|
+
return [];
|
|
38
|
+
const messages = [];
|
|
39
|
+
const collectFromValidationErrors = (value) => {
|
|
40
|
+
if (!Array.isArray(value))
|
|
41
|
+
return;
|
|
42
|
+
for (const validationError of value) {
|
|
43
|
+
if (!isRecord(validationError))
|
|
44
|
+
continue;
|
|
45
|
+
const message = getString(validationError, "Message");
|
|
46
|
+
if (message)
|
|
47
|
+
messages.push(message);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
collectFromValidationErrors(body.ValidationErrors);
|
|
51
|
+
const elements = body.Elements;
|
|
52
|
+
if (Array.isArray(elements)) {
|
|
53
|
+
for (const element of elements) {
|
|
54
|
+
if (!isRecord(element))
|
|
55
|
+
continue;
|
|
56
|
+
collectFromValidationErrors(element.ValidationErrors);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return [...new Set(messages)];
|
|
60
|
+
}
|
|
61
|
+
function formatXeroResponseBody(status, body) {
|
|
62
|
+
if (!isRecord(body))
|
|
63
|
+
return `${status} HTTP error`;
|
|
64
|
+
const problem = isRecord(body.problem)
|
|
65
|
+
? body.problem
|
|
66
|
+
: undefined;
|
|
67
|
+
const title = problem?.title ??
|
|
68
|
+
getString(body, "Type") ??
|
|
69
|
+
getString(body, "httpStatusCode") ??
|
|
70
|
+
"HTTP error";
|
|
71
|
+
const validationMessages = getValidationMessages(body);
|
|
72
|
+
const detail = validationMessages.length > 0
|
|
73
|
+
? validationMessages.join("; ")
|
|
74
|
+
: problem?.detail ??
|
|
75
|
+
getString(body, "Detail") ??
|
|
76
|
+
getString(body, "Message");
|
|
77
|
+
return detail ? `${status} ${title}: ${detail}` : `${status} ${title}`;
|
|
78
|
+
}
|
|
79
|
+
function parseXeroSdkErrorString(error) {
|
|
80
|
+
try {
|
|
81
|
+
const parsed = JSON.parse(error);
|
|
82
|
+
return isRecord(parsed) ? parsed : null;
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
24
88
|
/**
|
|
25
89
|
* Format error messages for return to the LLM.
|
|
26
90
|
*
|
|
@@ -31,24 +95,29 @@ function formatHttpStatus(status) {
|
|
|
31
95
|
export function formatError(error) {
|
|
32
96
|
if (error instanceof AxiosError) {
|
|
33
97
|
const status = error.response?.status;
|
|
34
|
-
const detail = error.response?.data?.Detail;
|
|
35
98
|
if (status !== undefined) {
|
|
36
99
|
const mapped = formatHttpStatus(status);
|
|
37
100
|
if (mapped)
|
|
38
101
|
return mapped;
|
|
102
|
+
return formatXeroResponseBody(status, error.response?.data);
|
|
39
103
|
}
|
|
40
|
-
return
|
|
104
|
+
return "An error occurred while communicating with Xero.";
|
|
105
|
+
}
|
|
106
|
+
if (typeof error === "string") {
|
|
107
|
+
const parsed = parseXeroSdkErrorString(error);
|
|
108
|
+
return isXeroSdkError(parsed)
|
|
109
|
+
? formatError(parsed)
|
|
110
|
+
: "An unexpected error occurred while communicating with Xero.";
|
|
41
111
|
}
|
|
42
112
|
if (isXeroSdkError(error)) {
|
|
43
|
-
const status = error.response.statusCode;
|
|
113
|
+
const status = error.response.statusCode ?? error.response.status;
|
|
114
|
+
if (status === undefined) {
|
|
115
|
+
return "An unexpected error occurred while communicating with Xero.";
|
|
116
|
+
}
|
|
44
117
|
const mapped = formatHttpStatus(status);
|
|
45
118
|
if (mapped)
|
|
46
119
|
return mapped;
|
|
47
|
-
|
|
48
|
-
const problem = body?.problem;
|
|
49
|
-
const title = problem?.title ?? body?.httpStatusCode ?? "HTTP error";
|
|
50
|
-
const detail = problem?.detail ?? body?.Detail;
|
|
51
|
-
return detail ? `${status} ${title}: ${detail}` : `${status} ${title}`;
|
|
120
|
+
return formatXeroResponseBody(status, getXeroBody(error.response));
|
|
52
121
|
}
|
|
53
122
|
if (error instanceof Error) {
|
|
54
123
|
return error.message;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { getClientHeaders } from "./get-client-headers.js";
|
|
3
|
+
export async function listXeroCurrencies() {
|
|
4
|
+
await xeroClient.authenticate();
|
|
5
|
+
const response = await xeroClient.accountingApi.getCurrencies(xeroClient.tenantId, undefined, // where
|
|
6
|
+
undefined, // order
|
|
7
|
+
getClientHeaders());
|
|
8
|
+
return response.body.currencies ?? [];
|
|
9
|
+
}
|
|
10
|
+
export async function assertXeroCurrencyEnabled(currencyCode) {
|
|
11
|
+
if (!currencyCode)
|
|
12
|
+
return;
|
|
13
|
+
const currencies = await listXeroCurrencies();
|
|
14
|
+
const enabledCurrencyCodes = currencies
|
|
15
|
+
.map((currency) => currency.code)
|
|
16
|
+
.filter((code) => Boolean(code));
|
|
17
|
+
if (enabledCurrencyCodes.includes(currencyCode))
|
|
18
|
+
return;
|
|
19
|
+
const enabledList = enabledCurrencyCodes.length
|
|
20
|
+
? enabledCurrencyCodes.join(", ")
|
|
21
|
+
: "none";
|
|
22
|
+
throw new Error(`Currency ${currencyCode} is not enabled in this Xero organisation. Enabled currencies: ${enabledList}. Add ${currencyCode} in Xero before creating or updating invoices in that currency.`);
|
|
23
|
+
}
|
package/dist/tools/list/index.js
CHANGED
|
@@ -4,6 +4,7 @@ import ListAgedReceivablesByContact from "./list-aged-receivables-by-contact.too
|
|
|
4
4
|
import ListBankTransactionsTool from "./list-bank-transactions.tool.js";
|
|
5
5
|
import ListContactsTool from "./list-contacts.tool.js";
|
|
6
6
|
import ListCreditNotesTool from "./list-credit-notes.tool.js";
|
|
7
|
+
import ListCurrenciesTool from "./list-currencies.tool.js";
|
|
7
8
|
import ListInvoicesTool from "./list-invoices.tool.js";
|
|
8
9
|
import ListItemsTool from "./list-items.tool.js";
|
|
9
10
|
import ListManualJournalsTool from "./list-manual-journals.tool.js";
|
|
@@ -27,6 +28,7 @@ import ListInvoiceAttachmentsTool from "./list-invoice-attachments.tool.js";
|
|
|
27
28
|
export const ListTools = [
|
|
28
29
|
ListAccountsTool,
|
|
29
30
|
ListContactsTool,
|
|
31
|
+
ListCurrenciesTool,
|
|
30
32
|
ListCreditNotesTool,
|
|
31
33
|
ListInvoicesTool,
|
|
32
34
|
ListItemsTool,
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { listEnabledXeroCurrencies } from "../../handlers/list-xero-currencies.handler.js";
|
|
2
|
+
import { CreateXeroTool } from "../../helpers/create-xero-tool.js";
|
|
3
|
+
const ListCurrenciesTool = CreateXeroTool("list-currencies", "Lists currencies enabled in the connected Xero organisation. Use this before creating or updating invoices with a non-base currency.", {}, async () => {
|
|
4
|
+
const response = await listEnabledXeroCurrencies();
|
|
5
|
+
if (response.error !== null) {
|
|
6
|
+
return {
|
|
7
|
+
content: [
|
|
8
|
+
{
|
|
9
|
+
type: "text",
|
|
10
|
+
text: `Error listing currencies: ${response.error}`,
|
|
11
|
+
},
|
|
12
|
+
],
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
const currencies = response.result;
|
|
16
|
+
return {
|
|
17
|
+
content: [
|
|
18
|
+
{
|
|
19
|
+
type: "text",
|
|
20
|
+
text: `Found ${currencies?.length || 0} enabled currencies:`,
|
|
21
|
+
},
|
|
22
|
+
...(currencies?.map((currency) => ({
|
|
23
|
+
type: "text",
|
|
24
|
+
text: [
|
|
25
|
+
`Currency: ${currency.code || "No code"}`,
|
|
26
|
+
currency.description
|
|
27
|
+
? `Description: ${currency.description}`
|
|
28
|
+
: null,
|
|
29
|
+
].filter(Boolean).join("\n"),
|
|
30
|
+
})) || []),
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
export default ListCurrenciesTool;
|
package/package.json
CHANGED